diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..59c4bba2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +Paper.aux +Paper.bbl +Paper.blg +Paper.log +Paper.out +Paper.pdf +Version.tex + diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..d2130c78 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +sudo: required +before_install: +- sudo apt-get -qq update +- sudo apt-get install texlive texlive-latex3 +- sudo apt install texlive-latex-extra +script: +- ./build.sh +deploy: + provider: script + script: ./travis_deploy.sh + skip_cleanup: true + on: + branch: master +env: + global: + - ENCRYPTION_LABEL="19a81de38b62" + - COMMIT_AUTHOR_EMAIL="chris@ethereum.org" + - COMMIT_AUTHOR="Travis CI" + - PUSH_REPO="git@github.com:ethereum/yellowpaper.git" diff --git a/Biblio.bib b/Biblio.bib index 4314259f..9421693e 100644 --- a/Biblio.bib +++ b/Biblio.bib @@ -1,3 +1,26 @@ +@Misc{EIP-100, + url = "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-100.md", + author = "Vitalik Buterin", + title = "EIP-100: Change difficulty adjustment to target mean block time including uncles", + year = "2016", + month = "April", +} + +@Misc{EIP-649, + url = "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-649.md", + author = "Afri Schoedon and Vitalik Buterin", + title = "EIP-649: Metropolis Difficulty Bomb Delay and Block Reward Reduction", + year = "2017", + month = "June", +} + +@Misc{EIP-2, + url = "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2.md", + title = "EIP-2: Homestead Hard-fork Changes", + author = "Vitalik Buterin", + year = "2015", +} + @Misc{cryptoeprint:2013:881, Note = {{http://eprint.iacr.org/}}, Url = {{Cryptology ePrint Archive, Report 2013/881}}, @@ -102,7 +125,7 @@ @InProceedings{miller1997future } @article{buterin2013ethereum, - url = {{http://ethereum.org/ethereum.html}}, + url = {{https://github.com/ethereum/wiki/wiki/White-Paper}}, author = {Vitalik Buterin}, title = {{Ethereum: A Next-Generation Smart Contract and Decentralized Application Platform}}, year = {{2013}}, @@ -141,4 +164,4 @@ @article{FowlerNollVo1991FNVHash author = {Glenn Fowler, Landon Curt Noll, Phong Vo}, title = {{Fowler–Noll–Vo hash function}}, year = {{1991}}, -} \ No newline at end of file +} diff --git a/LICENCE.md b/LICENCE.md new file mode 100644 index 00000000..22dc42ec --- /dev/null +++ b/LICENCE.md @@ -0,0 +1,175 @@ +## creative commons + +# Attribution-ShareAlike 4.0 International + +Creative Commons Corporation (“Creative Commons”) is not a law firm and does not provide legal services or legal advice. Distribution of Creative Commons public licenses does not create a lawyer-client or other relationship. Creative Commons makes its licenses and related information available on an “as-is” basis. Creative Commons gives no warranties regarding its licenses, any material licensed under their terms and conditions, or any related information. Creative Commons disclaims all liability for damages resulting from their use to the fullest extent possible. + +### Using Creative Commons Public Licenses + +Creative Commons public licenses provide a standard set of terms and conditions that creators and other rights holders may use to share original works of authorship and other material subject to copyright and certain other rights specified in the public license below. The following considerations are for informational purposes only, are not exhaustive, and do not form part of our licenses. + +* __Considerations for licensors:__ Our public licenses are intended for use by those authorized to give the public permission to use material in ways otherwise restricted by copyright and certain other rights. Our licenses are irrevocable. Licensors should read and understand the terms and conditions of the license they choose before applying it. Licensors should also secure all rights necessary before applying our licenses so that the public can reuse the material as expected. Licensors should clearly mark any material not subject to the license. This includes other CC-licensed material, or material used under an exception or limitation to copyright. [More considerations for licensors](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensors). + +* __Considerations for the public:__ By using one of our public licenses, a licensor grants the public permission to use the licensed material under specified terms and conditions. If the licensor’s permission is not necessary for any reason–for example, because of any applicable exception or limitation to copyright–then that use is not regulated by the license. Our licenses grant only permissions under copyright and certain other rights that a licensor has authority to grant. Use of the licensed material may still be restricted for other reasons, including because others have copyright or other rights in the material. A licensor may make special requests, such as asking that all changes be marked or described. Although not required by our licenses, you are encouraged to respect those requests where reasonable. [More considerations for the public](http://wiki.creativecommons.org/Considerations_for_licensors_and_licensees#Considerations_for_licensees). + +## Creative Commons Attribution-ShareAlike 4.0 International Public License + +By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions. + +### Section 1 – Definitions. + +a. __Adapted Material__ means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image. + +b. __Adapter's License__ means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License. + +c. __BY-SA Compatible License__ means a license listed at [creativecommons.org/compatiblelicenses](http://creativecommons.org/compatiblelicenses), approved by Creative Commons as essentially the equivalent of this Public License. + +d. __Copyright and Similar Rights__ means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. + +e. __Effective Technological Measures__ means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements. + +f. __Exceptions and Limitations__ means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material. + +g. __License Elements__ means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution and ShareAlike. + +h. __Licensed Material__ means the artistic or literary work, database, or other material to which the Licensor applied this Public License. + +i. __Licensed Rights__ means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license. + +j. __Licensor__ means the individual(s) or entity(ies) granting rights under this Public License. + +k. __Share__ means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them. + +l. __Sui Generis Database Rights__ means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world. + +m. __You__ means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning. + +### Section 2 – Scope. + +a. ___License grant.___ + + 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to: + + A. reproduce and Share the Licensed Material, in whole or in part; and + + B. produce, reproduce, and Share Adapted Material. + + 2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions. + + 3. __Term.__ The term of this Public License is specified in Section 6(a). + + 4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material. + + 5. __Downstream recipients.__ + + A. __Offer from the Licensor – Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License. + + B. __Additional offer from the Licensor – Adapted Material.__ Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply. + + C. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material. + + 6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i). + +b. ___Other rights.___ + + 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise. + + 2. Patent and trademark rights are not licensed under this Public License. + + 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties. + +### Section 3 – License Conditions. + +Your exercise of the Licensed Rights is expressly made subject to the following conditions. + +a. ___Attribution.___ + + 1. If You Share the Licensed Material (including in modified form), You must: + + A. retain the following if it is supplied by the Licensor with the Licensed Material: + + i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated); + + ii. a copyright notice; + + iii. a notice that refers to this Public License; + + iv. a notice that refers to the disclaimer of warranties; + + v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; + + B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and + + C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License. + + 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information. + + 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable. + +b. ___ShareAlike.___ + +In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply. + +1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-SA Compatible License. + +2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material. + +3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply. + +### Section 4 – Sui Generis Database Rights. + +Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material: + +a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database; + +b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and + +c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database. + +For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights. + +### Section 5 – Disclaimer of Warranties and Limitation of Liability. + +a. __Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.__ + +b. __To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.__ + +c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability. + +### Section 6 – Term and Termination. + +a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically. + +b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates: + + 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or + + 2. upon express reinstatement by the Licensor. + + For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License. + +c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License. + +d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. + +### Section 7 – Other Terms and Conditions. + +a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed. + +b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.t stated herein are separate from and independent of the terms and conditions of this Public License. + +### Section 8 – Interpretation. + +a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License. + +b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions. + +c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor. + +d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority. + +``` +Creative Commons is not a party to its public licenses. Notwithstanding, Creative Commons may elect to apply one of its public licenses to material it publishes and in those instances will be considered the “Licensor.” Except for the limited purpose of indicating that material is shared under a Creative Commons public license or as otherwise permitted by the Creative Commons policies published at [creativecommons.org/policies](http://creativecommons.org/policies), Creative Commons does not authorize the use of the trademark “Creative Commons” or any other trademark or logo of Creative Commons without its prior written consent including, without limitation, in connection with any unauthorized modifications to any of its public licenses or any other arrangements, understandings, or agreements concerning use of licensed material. For the avoidance of doubt, this paragraph does not form part of the public licenses. + +Creative Commons may be contacted at [creativecommons.org](http://creativecommons.org/). +``` diff --git a/Paper.tex b/Paper.tex index 81cec519..496107f2 100644 --- a/Paper.tex +++ b/Paper.tex @@ -1,6 +1,5 @@ \documentclass[9pt,oneside]{amsart} %\usepackage{tweaklist} -\usepackage{url} \usepackage{cancel} \usepackage{xspace} \usepackage{graphicx} @@ -20,6 +19,25 @@ \usepackage[usenames,dvipsnames]{xcolor} \usepackage{afterpage} \usepackage{tikz} +\usepackage[bookmarks=true, unicode=true, pdftitle={Ethereum Yellow Paper: a formal specification of Ethereum, a programmable blockchain}, pdfauthor={Gavin Wood and others as per https://github.com/ethereum/yellowpaper/commits/master},pdfkeywords={Ethereum, Yellow Paper, blockchain, virtual machine, cryptography, decentralised, singleton, transaction, generalised},pdfborder={0 0 0.5 [1 3]}]{hyperref} +%,pagebackref=true +\usepackage[ocgcolorlinks]{ocgx2} %For getting links to line and page break. https://tex.stackexchange.com/a/409479/143781 +\usepackage{tabu} %requires array. + +%This should be the last package before \input{Version.tex} +\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} +% "hyperref loads the url package internally. Use \PassOptionsToPackage{hyphens}{url}\usepackage{hyperref} to pass the option to the url package when it is loaded by hyperref. This avoids any package option clashes." Source: . +% Note also this: "If the \PassOptionsToPackage{hyphens}{url} approach does not work, maybe it's "because you're trying to load the url package with a specific option, but it's being loaded by one of your packages before that with a different set of options. Try loading the url package earlier than the package that requires it. If it's loaded by the document class, try using \RequirePackage[hyphens]{url} before the document class." Source: . + +% For formatting +%\usepackage{underscore} +%\usepackage{lipsum} % to generate filler text for testing of document rendering +\usepackage[english]{babel} +\usepackage[autostyle]{csquotes} +\MakeOuterQuote{"} +% Put before hyperref + +\input{Version.tex} \newcommand{\hcancel}[1]{% \tikz[baseline=(tocancel.base)]{ @@ -29,9 +47,6 @@ }% \definecolor{lightyellow}{rgb}{1,0.98,0.9} -\definecolor{lightpink}{rgb}{1,0.94,0.95} - -\newcommand{\firsthomesteadblock}{\ensuremath{N_H}} \DeclarePairedDelimiter{\ceil}{\lceil}{\rceil} \newcommand*\eg{e.g.\@\xspace} @@ -39,19 +54,18 @@ \newcommand*\ie{i.e.\@\xspace} %\renewcommand{\itemhook}{\setlength{\topsep}{0pt} \setlength{\itemsep}{0pt}\setlength{\leftmargin}{15pt}} -\title{Ethereum: A Secure Decentralised Generalised Transaction Ledger \\ {\smaller \textbf{Homestead revision}}} +\title{Ethereum: A Secure Decentralised Generalised Transaction Ledger \\ {\smaller \textbf{Byzantium version \YellowPaperVersionNumber{}}}} \author{ Dr. Gavin Wood\\ - Founder, Ethereum \& Ethcore\\ - gavin@ethcore.io + Founder, Ethereum \& Parity\\ + gavin@parity.io } \begin{document} \pagecolor{lightyellow} -%\pagecolor{lightpink} \begin{abstract} -The blockchain paradigm when coupled with cryptographically-secured transactions has demonstrated its utility through a number of projects, not least Bitcoin. Each such project can be seen as a simple application on a decentralised, but singleton, compute resource. We can call this paradigm a transactional singleton machine with shared-state. +The blockchain paradigm when coupled with cryptographically-secured transactions has demonstrated its utility through a number of projects, with Bitcoin being one of the most notable ones. Each such project can be seen as a simple application on a decentralised, but singleton, compute resource. We can call this paradigm a transactional singleton machine with shared-state. Ethereum implements this paradigm in a generalised manner. Furthermore it provides a plurality of such resources, each with a distinct state and operating code but able to interact through a message-passing framework with others. We discuss its design, implementation issues, the opportunities it provides and the future hurdles we envisage. \end{abstract} @@ -63,7 +77,7 @@ \section{Introduction}\label{sec:introduction} -With ubiquitous internet connections in most places of the world, global information transmission has become incredibly cheap. Technology-rooted movements like Bitcoin have demonstrated, through the power of the default, consensus mechanisms and voluntary respect of the social contract that it is possible to use the internet to make a decentralised value-transfer system, shared across the world and virtually free to use. This system can be said to be a very specialised version of a cryptographically secure, transaction-based state machine. Follow-up systems such as Namecoin adapted this original ``currency application'' of the technology into other applications albeit rather simplistic ones. +With ubiquitous internet connections in most places of the world, global information transmission has become incredibly cheap. Technology-rooted movements like Bitcoin have demonstrated through the power of the default, consensus mechanisms, and voluntary respect of the social contract, that it is possible to use the internet to make a decentralised value-transfer system that can be shared across the world and virtually free to use. This system can be said to be a very specialised version of a cryptographically secure, transaction-based state machine. Follow-up systems such as Namecoin adapted this original ``currency application'' of the technology into other applications albeit rather simplistic ones. Ethereum is a project which attempts to build the generalised technology; technology on which all transaction-based state machine concepts may be built. Moreover it aims to provide to the end-developer a tightly integrated end-to-end system for building software on a hitherto unexplored compute paradigm in the mainstream: a trustful object messaging compute framework. @@ -95,7 +109,7 @@ \subsection{Previous Work} \label{ch:previous} \section{The Blockchain Paradigm} \label{ch:overview} -Ethereum, taken as a whole, can be viewed as a transaction-based state machine: we begin with a genesis state and incrementally execute transactions to morph it into some final state. It is this final state which we accept as the canonical ``version'' of the world of Ethereum. The state can include such information as account balances, reputations, trust arrangements, data pertaining to information of the physical world; in short, anything that can currently be represented by a computer is admissible. Transactions thus represent a valid arc between two states; the `valid' part is important---there exist far more invalid state changes than valid state changes. Invalid state changes might, \eg be things such as reducing an account balance without an equal and opposite increase elsewhere. A valid state transition is one which comes about through a transaction. Formally: +Ethereum, taken as a whole, can be viewed as a transaction-based state machine: we begin with a genesis state and incrementally execute transactions to morph it into some final state. It is this final state which we accept as the canonical ``version'' of the world of Ethereum. The state can include such information as account balances, reputations, trust arrangements, data pertaining to information of the physical world; in short, anything that can currently be represented by a computer is admissible. Transactions thus represent a valid arc between two states; the `valid' part is important---there exist far more invalid state changes than valid state changes. Invalid state changes might, \eg, be things such as reducing an account balance without an equal and opposite increase elsewhere. A valid state transition is one which comes about through a transaction. Formally: \begin{equation} \boldsymbol{\sigma}_{t+1} \equiv \Upsilon(\boldsymbol{\sigma}_t, T) \end{equation} @@ -145,21 +159,23 @@ \subsection{Which History?} The scheme we use in order to generate consensus is a simplified version of the GHOST protocol introduced by \cite{cryptoeprint:2013:881}. This process is described in detail in section \ref{ch:ghost}. +Sometimes, a path follows a new protocol from a particular height. This document describes one version of the protocol. In order to follow back the history of a path, one might need to reference multiple versions of this document. + \section{Conventions}\label{ch:conventions} I use a number of typographical conventions for the formal notation, some of which are quite particular to the present work: The two sets of highly structured, `top-level', state values, are denoted with bold lowercase Greek letters. They fall into those of world-state, which are denoted $\boldsymbol{\sigma}$ (or a variant thereupon) and those of machine-state, $\boldsymbol{\mu}$. -Functions operating on highly structured values are denoted with an upper-case greek letter, \eg $\Upsilon$, the Ethereum state transition function. +Functions operating on highly structured values are denoted with an upper-case Greek letter, \eg $\Upsilon$, the Ethereum state transition function. For most functions, an uppercase letter is used, e.g. $C$, the general cost function. These may be subscripted to denote specialised variants, \eg $C_\text{\tiny SSTORE}$, the cost function for the {\tiny SSTORE} operation. For specialised and possibly externally defined functions, I may format as typewriter text, \eg the Keccak-256 hash function (as per the winning entry to the SHA-3 contest) is denoted $\texttt{KEC}$ (and generally referred to as plain Keccak). Also $\texttt{KEC512}$ is referring to the Keccak 512 hash function. Tuples are typically denoted with an upper-case letter, \eg $T$, is used to denote an Ethereum transaction. This symbol may, if accordingly defined, be subscripted to refer to an individual component, \eg $T_n$, denotes the nonce of said transaction. The form of the subscript is used to denote its type; \eg uppercase subscripts refer to tuples with subscriptable components. -Scalars and fixed-size byte sequences (or, synonymously, arrays) are denoted with a normal lower-case letter, \eg $n$ is used in the document to denote a transaction nonce. Those with a particularly special meaning may be greek, \eg $\delta$, the number of items required on the stack for a given operation. +Scalars and fixed-size byte sequences (or, synonymously, arrays) are denoted with a normal lower-case letter, \eg $n$ is used in the document to denote a transaction nonce. Those with a particularly special meaning may be Greek, \eg $\delta$, the number of items required on the stack for a given operation. -Arbitrary-length sequences are typically denoted as a bold lower-case letter, \eg $\mathbf{o}$ is used to denote the byte-sequence given as the output data of a message call. For particularly important values, a bold uppercase letter may be used. +Arbitrary-length sequences are typically denoted as a bold lower-case letter, \eg $\mathbf{o}$ is used to denote the byte sequence given as the output data of a message call. For particularly important values, a bold uppercase letter may be used. Throughout, we assume scalars are positive integers and thus belong to the set $\mathbb{P}$. The set of all byte sequences is $\mathbb{B}$, formally defined in Appendix \ref{app:rlp}. If such a set of sequences is restricted to those of a particular length, it is denoted with a subscript, thus the set of all byte sequences of length $32$ is named $\mathbb{B}_{32}$ and the set of all positive integers smaller than $2^{256}$ is named $\mathbb{P}_{256}$. This is formally defined in section \ref{ch:block}. @@ -231,17 +247,20 @@ \subsection{World State} \label{ch:state} \quad v(x) \equiv x_n \in \mathbb{P}_{256} \wedge x_b \in \mathbb{P}_{256} \wedge x_s \in \mathbb{B}_{32} \wedge x_c \in \mathbb{B}_{32} \end{equation} -\subsection{Homestead} \label{ch:homestead} +An account is \textit{empty} when it has no code, zero nonce and zero balance: +\begin{equation} +\mathtt{\tiny EMPTY}(\boldsymbol{\sigma}, a) \quad\equiv\quad \boldsymbol{\sigma}[a]_c = \texttt{\small KEC}\big(()\big) \wedge \boldsymbol{\sigma}[a]_n = 0 \wedge \boldsymbol{\sigma}[a]_b = 0 +\end{equation} +Even callable precompiled contracts can have an empty account state. This is because their account states do not usually contain the code describing its behavior. -A significant block number for compatibility with the public network is the block marking the transition between the {\it Frontier} and {\it Homestead} phases of the platform, which we denote with the symbol \firsthomesteadblock, defined thus +An account is \textit{dead} when its account state is non-existent or empty: \begin{equation} -\firsthomesteadblock \equiv 1,\! 150,\! 000 +\mathtt{\tiny DEAD}(\boldsymbol{\sigma}, a) \quad\equiv\quad \boldsymbol{\sigma}[a] = \varnothing \vee \mathtt{\tiny EMPTY}(\boldsymbol{\sigma}, a) \end{equation} -The protocol was upgraded at this block, so this symbol appears in some equations to account for the changes. \subsection{The Transaction} \label{ch:transaction} -A transaction (formally, $T$) is a single cryptographically-signed instruction constructed by an actor externally to the scope of Ethereum. While is assumed that the ultimate external actor will be human in nature, software tools will be used in its construction and dissemination\footnote{Notably, such `tools' could ultimately become so causally removed from their human-based initiation---or humans may become so causally-neutral---that there could be a point at which they rightly be considered autonomous agents. \eg contracts may offer bounties to humans for being sent transactions to initiate their execution.}. There are two types of transactions: those which result in message calls and those which result in the creation of new accounts with associated code (known informally as `contract creation'). Both types specify a number of common fields: +A transaction (formally, $T$) is a single cryptographically-signed instruction constructed by an actor externally to the scope of Ethereum. While it is assumed that the ultimate external actor will be human in nature, software tools will be used in its construction and dissemination\footnote{Notably, such `tools' could ultimately become so causally removed from their human-based initiation---or humans may become so causally-neutral---that there could be a point at which they rightly be considered autonomous agents. \eg contracts may offer bounties to humans for being sent transactions to initiate their execution.}. There are two types of transactions: those which result in message calls and those which result in the creation of new accounts with associated code (known informally as `contract creation'). Both types specify a number of common fields: \begin{description} \item[nonce] A scalar value equal to the number of transactions sent by the sender; formally $T_n$. @@ -271,7 +290,7 @@ \subsection{The Transaction} \label{ch:transaction} \begin{equation} L_T(T) \equiv \begin{cases} (T_n, T_p, T_g, T_t, T_v, T_\mathbf{i}, T_w, T_r, T_s) & \text{if} \; T_t = \varnothing\\ -(T_n, T_p, T_g, T_t, T_v, T_\mathbf{d}, T_w, T_r, T_s) & \text{otherwise} +(T_n, T_p, T_g, T_t, T_v, T_\mathbf{d}, T_w, T_r, T_s) & \text{otherwise} \end{cases} \end{equation} @@ -288,15 +307,15 @@ \subsection{The Transaction} \label{ch:transaction} \mathbb{P}_n = \{ P: P \in \mathbb{P} \wedge P < 2^n \} \end{equation} -The address hash $T_\mathbf{t}$ is slightly different: it is either a 20-byte address hash or, in the case of being a contract-creation transaction (and thus formally equal to $\varnothing$), it is the RLP empty byte-series and thus the member of $\mathbb{B}_0$: +The address hash $T_\mathbf{t}$ is slightly different: it is either a 20-byte address hash or, in the case of being a contract-creation transaction (and thus formally equal to $\varnothing$), it is the RLP empty byte sequence and thus the member of $\mathbb{B}_0$: \begin{equation} -T_t \in \begin{cases} \mathbb{B}_{20} & \text{if} \quad T_t \neq \varnothing \\ +T_\mathbf{t} \in \begin{cases} \mathbb{B}_{20} & \text{if} \quad T_t \neq \varnothing \\ \mathbb{B}_{0} & \text{otherwise}\end{cases} \end{equation} \subsection{The Block} \label{ch:block} -The block in Ethereum is the collection of relevant pieces of information (known as the block \textit{header}), $H$, together with information corresponding to the comprised transactions, $\mathbf{T}$, and a set of other block headers $\mathbf{U}$ that are known to have a parent equal to the present block's parent's parent (such blocks are known as \textit{ommers}\footnote{\textit{ommer} is the most prevalent (not saying much) gender-neutral term to mean ``sibling of parent''; see \url{http://nonbinary.org/wiki/Gender_neutral_language#Family_Terms}}). The block header contains several pieces of information: +The block in Ethereum is the collection of relevant pieces of information (known as the block \textit{header}), $H$, together with information corresponding to the comprised transactions, $\mathbf{T}$, and a set of other block headers $\mathbf{U}$ that are known to have a parent equal to the present block's parent's parent (such blocks are known as \textit{ommers}\footnote{\textit{ommer} is the most prevalent (not saying much) gender-neutral term to mean ``sibling of parent''; see \url{https://nonbinary.miraheze.org/wiki/Gender_neutral_language\#Aunt.2FUncle}}). The block header contains several pieces of information: %\textit{TODO: Introduce logs} @@ -314,8 +333,8 @@ \subsection{The Block} \label{ch:block} \item[gasUsed] A scalar value equal to the total gas used in transactions in this block; formally $H_g$. \item[timestamp] A scalar value equal to the reasonable output of Unix's time() at this block's inception; formally $H_s$. \item[extraData] An arbitrary byte array containing data relevant to this block. This must be 32 bytes or fewer; formally $H_x$. -\item[mixHash] A 256-bit hash which proves combined with the nonce that a sufficient amount of computation has been carried out on this block; formally $H_m$. -\item[nonce] A 64-bit hash which proves combined with the mix-hash that a sufficient amount of computation has been carried out on this block; formally $H_n$. +\item[mixHash] A 256-bit hash which, combined with the nonce, proves that a sufficient amount of computation has been carried out on this block; formally $H_m$. +\item[nonce] A 64-bit hash which, combined with the mix-hash, proves that a sufficient amount of computation has been carried out on this block; formally $H_n$. \end{description} The other two components in the block are simply a list of ommer block headers (of the same format as above) and a series of the transactions. Formally, we can refer to a block $B$: @@ -325,18 +344,24 @@ \subsection{The Block} \label{ch:block} \subsubsection{Transaction Receipt} -In order to encode information about a transaction concerning which it may be useful to form a zero-knowledge proof, or index and search, we encode a receipt of each transaction containing certain information from concerning its execution. Each receipt, denoted $B_\mathbf{R}[i]$ for the $i$th transaction) is placed in an index-keyed trie and the root recorded in the header as $H_e$. +In order to encode information about a transaction concerning which it may be useful to form a zero-knowledge proof, or index and search, we encode a receipt of each transaction containing certain information from concerning its execution. +Each receipt, denoted $B_\mathbf{R}[i]$ for the $i$th transaction, is placed in an index-keyed trie and the root recorded in the header as $H_e$. -The transaction receipt is a tuple of four items comprising the post-transaction state, $R_{\boldsymbol{\sigma}}$, the cumulative gas used in the block containing the transaction receipt as of immediately after the transaction has happened, $R_u$, the set of logs created through execution of the transaction, $R_\mathbf{l}$ and the Bloom filter composed from information in those logs, $R_b$: +The transaction receipt is a tuple of four items comprising the cumulative gas used in the block containing the transaction receipt as of immediately after the transaction has happened, $R_u$, the set of logs created through execution of the transaction, $R_\mathbf{l}$ and the Bloom filter composed from information in those logs, $R_b$ and the status code of the transaction, $R_{s'}$: \begin{equation} -R \equiv (R_{\boldsymbol{\sigma}}, R_u, R_b, R_\mathbf{l}) +R \equiv (R_u, R_b, R_\mathbf{l}, R_{s'}) \end{equation} The function $L_R$ trivially prepares a transaction receipt for being transformed into an RLP-serialised byte array: \begin{equation} -L_R(R) \equiv (\mathtt{\small TRIE}(L_S(R_{\boldsymbol{\sigma}})), R_u, R_b, R_\mathbf{l}) +L_R(R) \equiv (0 \in \mathbb{B}_{256}, R_u, R_b, R_\mathbf{l}) +\end{equation} +where $0 \in \mathbb{B}_{256}$ replaces the pre-transaction state root that existed in previous versions of the protocol. + +We assert that the status code $R_{s'}$ is an integer. +\begin{equation} +R_{s'} \in \mathbb{P} \end{equation} -thus the post-transaction state, $R_{\boldsymbol{\sigma}}$ is encoded into a trie structure, the root of which forms the first item. We assert $R_u$, the cumulative gas used is a positive integer and that the logs Bloom, $R_b$, is a hash of size 2048 bits (256 bytes): \begin{equation} @@ -358,7 +383,7 @@ \subsubsection{Transaction Receipt} M(O) \equiv \bigvee_{t \in \{O_a\} \cup O_\mathbf{t}} \big( M_{3:2048}(t) \big) \end{equation} -where $M_{3:2048}$ is a specialised Bloom filter that sets three bits out of 2048, given an arbitrary byte series. It does this through taking the low-order 11 bits of each of the first three pairs of bytes in a Keccak-256 hash of the byte series. Formally: +where $M_{3:2048}$ is a specialised Bloom filter that sets three bits out of 2048, given an arbitrary byte sequence. It does this through taking the low-order 11 bits of each of the first three pairs of bytes in a Keccak-256 hash of the byte sequence./footnote{11 bits $= 2^2048$, and the low-order 11 bits is the modulo 2048 of the operand, which is in this case is "each of the first three pairs of bytes in a Keccak-256 hash of the byte sequence."} Formally: \begin{eqnarray} M_{3:2048}(\mathbf{x}: \mathbf{x} \in \mathbb{B}) & \equiv & \mathbf{y}: \mathbf{y} \in \mathbb{B}_{256} \quad \text{where:}\\ \mathbf{y} & = & (0, 0, ..., 0) \quad \text{except:}\\ @@ -439,7 +464,6 @@ \subsubsection{Block Header Validity} \end{equation} \newcommand{\mindifficulty}{D_0} -\newcommand{\frontiermod}{\ensuremath{\varsigma_1}} \newcommand{\homesteadmod}{\ensuremath{\varsigma_2}} \newcommand{\expdiffsymb}{\ensuremath{\epsilon}} \newcommand{\diffadjustment}{x} @@ -448,7 +472,6 @@ \subsubsection{Block Header Validity} \begin{equation} D(H) \equiv \begin{dcases} \mindifficulty & \text{if} \quad H_i = 0\\ -\text{max}\!\left(\mindifficulty, {P(H)_H}_d + \diffadjustment\times\frontiermod + \expdiffsymb \right) & \text{if} \quad H_i<\firsthomesteadblock\\ \text{max}\!\left(\mindifficulty, {P(H)_H}_d + \diffadjustment\times\homesteadmod + \expdiffsymb \right) & \text{otherwise}\\ \end{dcases} \end{equation} @@ -460,23 +483,26 @@ \subsubsection{Block Header Validity} \diffadjustment \equiv \left\lfloor\frac{{P(H)_H}_d}{2048}\right\rfloor \end{equation} \begin{equation} -\frontiermod \equiv \begin{cases} -1 & \text{if} \quad H_s < {P(H)_H}_s + 13 \\ --1 & \text{otherwise} \\ -\end{cases} -\end{equation} -\begin{equation} -\homesteadmod \equiv \text{max}\left( 1 - \left\lfloor\frac{H_s - {P(H)_H}_s}{10}\right\rfloor, -99 \right) -\end{equation} -\begin{equation} -\expdiffsymb \equiv \left\lfloor 2^{ \left\lfloor H_i \div 100000 \right\rfloor - 2 } \right\rfloor +\homesteadmod \equiv \text{max}\left( x - \left\lfloor\frac{H_s - {P(H)_H}_s}{9}\right\rfloor, -99 \right) \end{equation} +\begin{equation*} +x \equiv \begin{cases} +1 & \text{if} \, \lVert P(H)_\mathbf{U}\rVert = 0 \\ +2 & \text{otherwise} +\end{cases} +\end{equation*} +\begin{align} +\expdiffsymb &\equiv \left\lfloor 2^{ \left\lfloor H'_i \div 100000 \right\rfloor - 2 } \right\rfloor \\ +H'_i &\equiv \max(H_i - 3000000, 0) +\end{align} + +Note that $\mindifficulty$ is the difficulty of the genesis block. The \textit{Homestead} difficulty parameter, $\homesteadmod$, is used to affect a dynamic homeostasis of time between blocks, as the time between blocks varies, as discussed below, as implemented in EIP-2 \cite{EIP-2}. In the Homestead release, the exponential difficulty symbol, $\expdiffsymb$ causes the difficulty to slowly increase (every 100,000 blocks) at an exponential rate, and thus increasing the block time difference, and putting time pressure on transitioning to proof-of-stake. This effect, known as the "difficulty bomb", or "ice age", was explained in EIP-649 \cite{EIP-649} and delayed and implemented earlier in EIP-2 \cite{EIP-2}. $\homesteadmod$ was also modified in EIP-100 with the use of $x$, the adjustment factor above, and the demoninator 9, in order to target the mean block time including uncle blocks \cite{EIP-100}. Finally, in the Byzantium release, with EIP-649, the ice age was delayed by creating a fake block number, $H'_i$, which is obtained by substracting three million from the actual block number, which in other words reduced $\expdiffsymb$ and the time difference between blocks, in order to allow more time to develop proof-of-stake and preventing the network from "freezing" up. The canonical gas limit $H_l$ of a block of header $H$ must fulfil the relation: \begin{eqnarray} & & H_l < {P(H)_H}_l + \left\lfloor\frac{{P(H)_H}_l}{1024}\right\rfloor \quad \wedge \\ & & H_l > {P(H)_H}_l - \left\lfloor\frac{{P(H)_H}_l}{1024}\right\rfloor \quad \wedge \\ -& & H_l \geqslant 125000 +& & H_l \geqslant 5000 \end{eqnarray} $H_s$ is the timestamp of block $H$ and must fulfil the relation: @@ -503,7 +529,7 @@ \subsubsection{Block Header Validity} & & H_g \le H_l \quad \wedge \\ & & H_l < {P(H)_H}_l + \left\lfloor\frac{{P(H)_H}_l}{1024}\right\rfloor \quad \wedge \\ & & H_l > {P(H)_H}_l - \left\lfloor\frac{{P(H)_H}_l}{1024}\right\rfloor \quad \wedge \\ -& & H_l \geqslant 125000 \quad \wedge \\ +& & H_l \geqslant 5000 \quad \wedge \\ & & H_s > {P(H)_H}_s \quad \wedge \\ & & H_i = {P(H)_H}_i +1 \quad \wedge \\ & & \lVert H_x \rVert \le 32 @@ -544,27 +570,25 @@ \section{Transaction Execution} \label{ch:transactions} \subsection{Substate} Throughout transaction execution, we accrue certain information that is acted upon immediately following the transaction. We call this \textit{transaction substate}, and represent it as $A$, which is a tuple: \begin{equation} -A \equiv (A_\mathbf{s}, A_\mathbf{l}, A_r) +A \equiv (A_\mathbf{s}, A_\mathbf{l}, A_\mathbf{t}, A_r) \end{equation} -The tuple contents include $A_\mathbf{s}$, the suicide set: a set of accounts that will be discarded following the transaction's completion. $A_\mathbf{l}$ is the log series: this is a series of archived and indexable `checkpoints' in VM code execution that allow for contract-calls to be easily tracked by onlookers external to the Ethereum world (such as decentralised application front-ends). Finally there is $A_r$, the refund balance, increased through using the {\small SSTORE} instruction in order to reset contract storage to zero from some non-zero value. Though not immediately refunded, it is allowed to partially offset the total execution costs. +The tuple contents include $A_\mathbf{s}$, the self-destruct set: a set of accounts that will be discarded following the transaction's completion. $A_\mathbf{l}$ is the log series: this is a series of archived and indexable `checkpoints' in VM code execution that allow for contract-calls to be easily tracked by onlookers external to the Ethereum world (such as decentralised application front-ends). $A_\mathbf{t}$ is the set of touched accounts, of which the empty ones are deleted at the end of a transaction. Finally there is $A_r$, the refund balance, increased through using the {\small SSTORE} instruction in order to reset contract storage to zero from some non-zero value. Though not immediately refunded, it is allowed to partially offset the total execution costs. -For brevity, we define the empty substate $A^0$ to have no suicides, no logs and a zero refund balance: +We define the empty substate $A^0$ to have no self-destructs, no logs, no touched accounts and a zero refund balance: \begin{equation} -A^0 \equiv (\varnothing, (), 0) +A^0 \equiv (\varnothing, (), \varnothing, 0) \end{equation} \subsection{Execution} We define intrinsic gas $g_0$, the amount of gas this transaction requires to be paid prior to execution, as follows: \begin{align} g_0 \equiv {} & \sum_{i \in T_\mathbf{i}, T_\mathbf{d}} \begin{cases} G_{txdatazero} & \text{if} \quad i = 0 \\ G_{txdatanonzero} & \text{otherwise} \end{cases} \\ -{} & + \begin{cases} G_\text{txcreate} & \text{if} \quad T_t = \varnothing \wedge H_i \geq \firsthomesteadblock \\ 0 & \text{otherwise} \end{cases} \\ +{} & + \begin{cases} G_\text{txcreate} & \text{if} \quad T_t = \varnothing \\ 0 & \text{otherwise} \end{cases} \\ {} & + G_{transaction} \end{align} -where $T_\mathbf{i},T_\mathbf{d}$ means the series of bytes of the transaction's associated data and initialisation EVM-code, depending on whether the transaction is for contract-creation or message-call. $G_\text{txcreate}$ is added if the transaction is contract-creating, but not if a result of EVM-code or before the {\it Homestead transition}. $G$ is fully defined in Appendix \ref{app:fees}. - -%todo Explain g_d reason? +where $T_\mathbf{i},T_\mathbf{d}$ means the series of bytes of the transaction's associated data and initialisation EVM-code, depending on whether the transaction is for contract-creation or message-call. $G_\text{txcreate}$ is added if the transaction is contract-creating, but not if a result of EVM-code. $G$ is fully defined in Appendix \ref{app:fees}. The up-front cost $v_0$ is calculated as: \begin{equation} @@ -577,13 +601,13 @@ \subsection{Execution} S(T) & \neq & \varnothing \quad \wedge \\ \boldsymbol{\sigma}[S(T)] & \neq & \varnothing \quad \wedge \\ T_n & = & \boldsymbol{\sigma}[S(T)]_n \quad \wedge \\ -g_0 & \leqslant & T_g \quad \wedge \\ +g_0 & \leqslant & T_g \quad \wedge \\ v_0 & \leqslant & \boldsymbol{\sigma}[S(T)]_b \quad \wedge \\ T_g & \leqslant & {B_H}_l - \ell(B_\mathbf{R})_u \end{array} \end{equation} -Note the final condition; the sum of the transaction's gas limit, $T_g$, and the gas utilised in this block prior, given by $\ell(B_\mathbf{R})_u$, must be no greater than the block's \textbf{gasLimit}, ${B_H}_l$. +Note the final condition; the sum of the transaction's gas limit, $T_g$, and the gas utilised in this block prior, given by $\ell(B_\mathbf{R})_u$, must be no greater than the block's \textbf{gasLimit}, ${B_H}_l$. The execution of a valid transaction begins with an irrevocable change made to the state: the nonce of the account of the sender, $S(T)$, is incremented by one and the balance is reduced by part of the up-front cost, $T_gT_p$. The gas available for the proceeding computation, $g$, is defined as $T_g - g_0$. The computation, whether contract creation or a message call, results in an eventual state (which may legally be equivalent to the current state), the change to which is deterministic and never invalid: there can be no invalid transactions from this point. @@ -594,11 +618,11 @@ \subsection{Execution} \boldsymbol{\sigma}_0[S(T)]_n & \equiv & \boldsymbol{\sigma}[S(T)]_n + 1 \end{eqnarray} -Evaluating $\boldsymbol{\sigma}_P$ from $\boldsymbol{\sigma}_0$ depends on the transaction type; either contract creation or message call; we define the tuple of post-execution provisional state $\boldsymbol{\sigma}_P$, remaining gas $g'$ and substate $A$: +Evaluating $\boldsymbol{\sigma}_P$ from $\boldsymbol{\sigma}_0$ depends on the transaction type; either contract creation or message call; we define the tuple of post-execution provisional state $\boldsymbol{\sigma}_P$, remaining gas $g'$, substate $A$ and status code $s'$: \begin{equation} -(\boldsymbol{\sigma}_P, g', A) \equiv \begin{cases} -\Lambda(\boldsymbol{\sigma}_0, S(T), T_o, &\\ \quad\quad g, T_p, T_v, T_\mathbf{i}, 0) & \text{if} \quad T_t = \varnothing \\ -\Theta_{3}(\boldsymbol{\sigma}_0, S(T), T_o, &\\ \quad\quad T_t, T_t, g, T_p, T_v, T_v, T_\mathbf{d}, 0) & \text{otherwise} +(\boldsymbol{\sigma}_P, g', A, s') \equiv \begin{cases} +\Lambda_{4}(\boldsymbol{\sigma}_0, S(T), T_o, &\\ \quad\quad g, T_p, T_v, T_\mathbf{i}, 0, \top) & \text{if} \quad T_t = \varnothing \\ +\Theta_{4}(\boldsymbol{\sigma}_0, S(T), T_o, &\\ \quad\quad T_t, T_t, g, T_p, T_v, T_v, T_\mathbf{d}, 0, \top) & \text{otherwise} \end{cases} \end{equation} @@ -608,7 +632,7 @@ \subsection{Execution} \end{equation} and $T_o$ is the original transactor, which can differ from the sender in the case of a message call or contract creation not directly triggered by a transaction but coming from the execution of EVM-code. -Note we use $\Theta_{3}$ to denote the fact that only the first three components of the function's value are taken; the final represents the message-call's output value (a byte array) and is unused in the context of transaction evaluation. +Note we use $\Theta_{4}$ and $\Lambda_{4}$ to denote the fact that only the first four components of the functions' values are taken; the final represents the message-call's output value (a byte array) and is unused in the context of transaction evaluation. After the message call or contract creation is processed, the state is finalised by determining the amount to be refunded, $g^*$ from the remaining gas, $g'$, plus some allowance from the refund counter, to the sender at the original rate. \begin{equation} @@ -625,32 +649,29 @@ \subsection{Execution} m & \equiv & {B_H}_c \end{eqnarray} -The final state, $\boldsymbol{\sigma}'$, is reached after deleting all accounts that appear in the suicide list: +The final state, $\boldsymbol{\sigma}'$, is reached after deleting all accounts that either appear in the self-destruct list or are touched and empty: \begin{eqnarray} \boldsymbol{\sigma}' & \equiv & \boldsymbol{\sigma}^* \quad \text{except} \\ -\forall i \in A_\mathbf{s}: \boldsymbol{\sigma}'[i] & \equiv & \varnothing +\forall i \in A_\mathbf{s}: \boldsymbol{\sigma}'[i] & = & \varnothing \\ +\forall i \in A_\mathbf{t}: \boldsymbol{\sigma}'[i] & = & \varnothing \quad\text{if}\quad \mathtt{\tiny DEAD}(\boldsymbol{\sigma}^*\kern -2pt, i) \end{eqnarray} -And finally, we specify $\Upsilon^g$, the total gas used in this transaction and $\Upsilon^\mathbf{l}$, the logs created by this transaction: +And finally, we specify $\Upsilon^g$, the total gas used in this transaction, $\Upsilon^\mathbf{l}$, the logs created by this transaction and $\Upsilon^{s}$, the status code of this transaction: \begin{eqnarray} \Upsilon^g(\boldsymbol{\sigma}, T) & \equiv & T_g - g' \\ -\Upsilon^\mathbf{l}(\boldsymbol{\sigma}, T) & \equiv & A_\mathbf{l} +\Upsilon^\mathbf{l}(\boldsymbol{\sigma}, T) & \equiv & A_\mathbf{l} \\ +\Upsilon^s(\boldsymbol{\sigma}, T) & \equiv & s' \end{eqnarray} These are used to help define the transaction receipt, discussed later. -%In the case that $s = m$ then we simply return the Ether back to the sender/miner, collapsing the exception into: -%\begin{eqnarray} -%\boldsymbol{\sigma}'[s]_b & \equiv & \boldsymbol{\sigma}_P[s]_b + g -%\end{eqnarray} - \section{Contract Creation} \label{ch:create} There are a number of intrinsic parameters used when creating an account: sender ($s$), original transactor ($o$), available gas ($g$), gas price ($p$), endowment ($v$) together with an arbitrary length byte array, $\mathbf{i}$, the initialisation EVM code and finally the present depth of the message-call/contract-creation stack ($e$). -We define the creation function formally as the function $\Lambda$, which evaluates from these values, together with the state $\boldsymbol{\sigma}$ to the tuple containing the new state, remaining gas and accrued transaction substate $(\boldsymbol{\sigma}', g', A)$, as in section \ref{ch:transactions}: +We define the creation function formally as the function $\Lambda$, which evaluates from these values, together with the state $\boldsymbol{\sigma}$ to the tuple containing the new state, remaining gas, accrued transaction substate and an error message $(\boldsymbol{\sigma}', g', A, \mathbf{o})$, as in section \ref{ch:transactions}: \begin{equation} -(\boldsymbol{\sigma}', g', A) \equiv \Lambda(\boldsymbol{\sigma}, s, o, g, p, v, \mathbf{i}, e) +(\boldsymbol{\sigma}', g', A, s', \mathbf{o}) \equiv \Lambda(\boldsymbol{\sigma}, s, o, g, p, v, \mathbf{i}, e, w) \end{equation} The address of the new account is defined as being the rightmost 160 bits of the Keccak hash of the RLP encoding of the structure containing only the sender and the nonce. Thus we define the resultant address for the new account $a$: @@ -660,13 +681,17 @@ \section{Contract Creation} \label{ch:create} where $\mathtt{\tiny KEC}$ is the Keccak 256-bit hash function, $\mathtt{\tiny RLP}$ is the RLP encoding function, $\mathcal{B}_{a..b}(X)$ evaluates to binary value containing the bits of indices in the range $[a, b]$ of the binary data $X$ and $\boldsymbol{\sigma}[x]$ is the address state of $x$ or $\varnothing$ if none exists. Note we use one fewer than the sender's nonce value; we assert that we have incremented the sender account's nonce prior to this call, and so the value used is the sender's nonce at the beginning of the responsible transaction or VM operation. -The account's nonce is initially defined as zero, the balance as the value passed, the storage as empty and the code hash as the Keccak 256-bit hash of the empty string; the sender's balance is also reduced by the value passed. Thus the mutated state becomes $\boldsymbol{\sigma}^*$: +The account's nonce is initially defined as one, the balance as the value passed, the storage as empty and the code hash as the Keccak 256-bit hash of the empty string; the sender's balance is also reduced by the value passed. Thus the mutated state becomes $\boldsymbol{\sigma}^*$: \begin{equation} \boldsymbol{\sigma}^* \equiv \boldsymbol{\sigma} \quad \text{except:} \end{equation} \begin{eqnarray} -\boldsymbol{\sigma}^*[a] &\equiv& \big( 0, v + v', \mathtt{\tiny TRIE}(\varnothing), \mathtt{\tiny KEC}\big(()\big) \big) \\ -\boldsymbol{\sigma}^*[s]_b &\equiv& \boldsymbol{\sigma}[s]_b - v +\boldsymbol{\sigma}^*[a] &=& \big( 1, v + v', \mathtt{\tiny TRIE}(\varnothing), \mathtt{\tiny KEC}\big(()\big) \big) \\ +\boldsymbol{\sigma}^*[s] &=& \begin{cases} +\varnothing & \text{if}\ \boldsymbol{\sigma}[s] = \varnothing \ \wedge\ v = 0 \\ +\mathbf{a}^* & \text{otherwise} +\end{cases} \\ +\mathbf{a}^* &\equiv& (\boldsymbol{\sigma}[s]_n, \boldsymbol{\sigma}[s]_b - v, \boldsymbol{\sigma}[s]_\mathbf{s}, \boldsymbol{\sigma}[s]_c) \end{eqnarray} where $v'$ is the account's pre-existing value, in the event it was previously in existence: @@ -682,7 +707,7 @@ \section{Contract Creation} \label{ch:create} Finally, the account is initialised through the execution of the initialising EVM code $\mathbf{i}$ according to the execution model (see section \ref{ch:model}). Code execution can effect several events that are not internal to the execution state: the account's storage can be altered, further accounts can be created and further message calls can be made. As such, the code execution function $\Xi$ evaluates to a tuple of the resultant state $\boldsymbol{\sigma}^{**}$, available gas remaining $g^{**}$, the accrued substate $A$ and the body code of the account $\mathbf{o}$. \begin{equation} -(\boldsymbol{\sigma}^{**}, g^{**}, A, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}^*, g, I) \\ +(\boldsymbol{\sigma}^{**}, g^{**}, A, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}^*, g, I, \{s, a\}) \\ \end{equation} where $I$ contains the parameters of the execution environment as defined in section \ref{ch:model}, that is: \begin{eqnarray} @@ -693,51 +718,58 @@ \section{Contract Creation} \label{ch:create} I_s & \equiv & s \\ I_v & \equiv & v \\ I_\mathbf{b} & \equiv & \mathbf{i} \\ -I_e & \equiv & e +I_e & \equiv & e \\ +I_w & \equiv & w \end{eqnarray} $I_\mathbf{d}$ evaluates to the empty tuple as there is no input data to this call. $I_H$ has no special treatment and is determined from the blockchain. -Code execution depletes gas, and gas may not go below zero, thus execution may exit before the code has come to a natural halting state. In this (and several other) exceptional cases we say an Out-of-Gas exception has occurred: The evaluated state is defined as being the empty set, $\varnothing$, and the entire create operation should have no effect on the state, effectively leaving it as it was immediately prior to attempting the creation. +Code execution depletes gas, and gas may not go below zero, thus execution may exit before the code has come to a natural halting state. In this (and several other) exceptional cases we say an out-of-gas (OOG) exception has occurred: The evaluated state is defined as being the empty set, $\varnothing$, and the entire create operation should have no effect on the state, effectively leaving it as it was immediately prior to attempting the creation. If the initialization code completes successfully, a final contract-creation cost is paid, the code-deposit cost, $c$, proportional to the size of the created contract's code: \begin{equation} c \equiv G_{codedeposit} \times |\mathbf{o}| \end{equation} -If there is not enough gas remaining to pay this, \ie $g^{**} < c$, then we also declare an Out-of-Gas exception. +If there is not enough gas remaining to pay this, \ie $g^{**} < c$, then we also declare an out-of-gas exception. -The gas remaining will be zero in any such exceptional condition, \ie if the creation was conducted as the reception of a transaction, then this doesn't affect payment of the intrinsic cost of contract creation; it is paid regardless. However, the value of the transaction is not transferred to the aborted contract's address when we are Out-of-Gas. +The gas remaining will be zero in any such exceptional condition, \ie if the creation was conducted as the reception of a transaction, then this doesn't affect payment of the intrinsic cost of contract creation; it is paid regardless. However, the value of the transaction is not transferred to the aborted contract's address when we are out-of-gas. -If such an exception does not occur, then the remaining gas is refunded to the originator and the now-altered state is allowed to persist. Thus formally, we may specify the resultant state, gas and substate as $(\boldsymbol{\sigma}', g', A)$ where: +If such an exception does not occur, then the remaining gas is refunded to the originator and the now-altered state is allowed to persist. Thus formally, we may specify the resultant state, gas, substate and status code as $(\boldsymbol{\sigma}', g', A, s')$ where: \begin{align} \quad g' &\equiv \begin{cases} -0 & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \\ -g^{**} & \text{if} \quad g^{**} 24576\big) \end{align} The exception in the determination of $\boldsymbol{\sigma}'$ dictates that $\mathbf{o}$, the resultant byte sequence from the execution of the initialisation code, specifies the final body code for the newly-created account. -Note that the intention from block \firsthomesteadblock\ onwards ({\it Homestead}) is that the result is either a successfully created new contract with its endowment, or no new contract with no transfer of value. Before {\it Homestead}, if there is not enough gas to pay $c$, an account at the new contract's address is created, along with all the initialisation side-effects, and the value is transferred, but no contract code is deployed. +Note that intention is that the result is either a successfully created new contract with its endowment, or no new contract with no transfer of value. \subsection{Subtleties} -Note that while the initialisation code is executing, the newly created address exists but with no intrinsic body code. Thus any message call received by it during this time causes no code to be executed. If the initialisation execution ends with a {\small SUICIDE} instruction, the matter is moot since the account will be deleted before the transaction is completed. For a normal {\small STOP} code, or if the code returned is otherwise empty, then the state is left with a zombie account, and any remaining balance will be locked into the account forever. +Note that while the initialisation code is executing, the newly created address exists but with no intrinsic body code. Thus any message call received by it during this time causes no code to be executed. If the initialisation execution ends with a {\small SELFDESTRUCT} instruction, the matter is moot since the account will be deleted before the transaction is completed. For a normal {\small STOP} code, or if the code returned is otherwise empty, then the state is left with a zombie account, and any remaining balance will be locked into the account forever. \section{Message Call} \label{ch:call} In the case of executing a message call, several parameters are required: sender ($s$), transaction originator ($o$), recipient ($r$), the account whose code is to be executed ($c$, usually the same as recipient), available gas ($g$), value ($v$) and gas price ($p$) together with an arbitrary length byte array, $\mathbf{d}$, the input data of the call and finally the present depth of the message-call/contract-creation stack ($e$). Aside from evaluating to a new state and transaction substate, message calls also have an extra component---the output data denoted by the byte array $\mathbf{o}$. This is ignored when executing transactions, however message calls can be initiated due to VM-code execution and in this case this information is used. \begin{equation} -(\boldsymbol{\sigma}', g', A, \mathbf{o}) \equiv \Theta(\boldsymbol{\sigma}, s, o, r, c, g, p, v, \tilde{v}, \mathbf{d}, e) +(\boldsymbol{\sigma}', g', A, s', \mathbf{o}) \equiv \Theta(\boldsymbol{\sigma}, s, o, r, c, g, p, v, \tilde{v}, \mathbf{d}, e, w) \end{equation} Note that we need to differentiate between the value that is to be transferred, $v$, from the value apparent in the execution context, $\tilde{v}$, for the {\small DELEGATECALL} instruction. @@ -752,17 +784,27 @@ \section{Message Call} \label{ch:call} \boldsymbol{\sigma}_1 \equiv \boldsymbol{\sigma}_1' \quad \text{except:} \\ \end{equation} \begin{equation} -\boldsymbol{\sigma}_1[s]_b \equiv \boldsymbol{\sigma}_1'[s]_b - v +\boldsymbol{\sigma}_1[s] \equiv \begin{cases} +\varnothing & \text{if}\ \boldsymbol{\sigma}_1'[s] = \varnothing \ \wedge\ v = 0 \\ +\mathbf{a}_1 &\text{otherwise} +\end{cases} +\end{equation} +\begin{equation} +\mathbf{a}_1 \equiv (\boldsymbol{\sigma}_1'[s]_n, \boldsymbol{\sigma}_1'[s]_b - v, \boldsymbol{\sigma}_1'[s]_\mathbf{s}, \boldsymbol{\sigma}_1'[s]_c) \end{equation} \begin{equation} \text{and}\quad \boldsymbol{\sigma}_1' \equiv \boldsymbol{\sigma} \quad \text{except:} \\ \end{equation} \begin{equation} \begin{cases} -\boldsymbol{\sigma}_1'[r] \equiv (v, 0, \mathtt{\tiny KEC}(()), \mathtt{\tiny TRIE}(\varnothing)) & \text{if} \quad \boldsymbol{\sigma}[r] = \varnothing \\ -\boldsymbol{\sigma}_1'[r]_b \equiv \boldsymbol{\sigma}[r]_b + v & \text{otherwise} +\boldsymbol{\sigma}_1'[r] \equiv (0, v, \mathtt{\tiny TRIE}(\varnothing), \mathtt{\tiny KEC}(())) & \text{if} \quad \boldsymbol{\sigma}[r] = \varnothing \wedge v \neq 0 \\ +\boldsymbol{\sigma}_1'[r] \equiv \varnothing & \text{if}\quad \boldsymbol{\sigma}[r] = \varnothing \wedge v = 0 \\ +\boldsymbol{\sigma}_1'[r] \equiv \mathbf{a}_1' & \text{otherwise} \end{cases} \end{equation} +\begin{equation} +\mathbf{a}_1' \equiv (\boldsymbol{\sigma}[r]_n, \boldsymbol{\sigma}[r]_b + v, \boldsymbol{\sigma}[r]_\mathbf{s}, \boldsymbol{\sigma}[r]_c) +\end{equation} The account's associated code (identified as the fragment whose Keccak hash is $\boldsymbol{\sigma}[c]_c$) is executed according to the execution model (see section \ref{ch:model}). Just as with contract creation, if the execution halts in an exceptional fashion (i.e. due to an exhausted gas supply, stack underflow, invalid jump destination or invalid instruction), then no gas is refunded to the caller and the state is reverted to the point immediately prior to balance transfer (i.e. $\boldsymbol{\sigma}$). @@ -771,12 +813,24 @@ \section{Message Call} \label{ch:call} \boldsymbol{\sigma} & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \\ \boldsymbol{\sigma}^{**} & \text{otherwise} \end{cases} \\ -(\boldsymbol{\sigma}^{**}, g', \mathbf{s}, \mathbf{o}) & \equiv & \begin{cases} -\Xi_{\mathtt{ECREC}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad r = 1 \\ -\Xi_{\mathtt{SHA256}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad r = 2 \\ -\Xi_{\mathtt{RIP160}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad r = 3 \\ -\Xi_{\mathtt{ID}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad r = 4 \\ -\Xi(\boldsymbol{\sigma}_1, g, I) & \text{otherwise} \end{cases} \\ +g' & \equiv & \begin{cases} +0 & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \ \wedge \ \mathbf{o} = \varnothing \\ +g^{**} & \text{otherwise} +\end{cases} \\ \nonumber +s' & \equiv & \begin{cases} +0 & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \\ +1 & \text{otherwise} +\end{cases} \\ +\qquad (\boldsymbol{\sigma}^{**}, g^{**}, A, \mathbf{o}) & \equiv & \begin{cases} +\Xi_{\mathtt{ECREC}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 1 \\ +\Xi_{\mathtt{SHA256}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 2 \\ +\Xi_{\mathtt{RIP160}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 3 \\ +\Xi_{\mathtt{ID}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 4 \\ +\Xi_{\mathtt{EXPMOD}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 5 \\ +\Xi_{\mathtt{BN\_ADD}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 6 \\ +\Xi_{\mathtt{BN\_MUL}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 7 \\ +\Xi_{\mathtt{SNARKV}}(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{if} \quad r = 8 \\ +\Xi(\boldsymbol{\sigma}_1, g, I, \mathbf{t}) & \text{otherwise} \end{cases} \\ I_a & \equiv & r \\ I_o & \equiv & o \\ I_p & \equiv & p \\ @@ -784,12 +838,15 @@ \section{Message Call} \label{ch:call} I_s & \equiv & s \\ I_v & \equiv & \tilde{v} \\ I_e & \equiv & e \\ +I_w & \equiv & w \\ +\mathbf{t} & \equiv & \{s, r\} \\ +\\ \nonumber \text{Let} \; \mathtt{\tiny KEC}(I_\mathbf{b}) & = & \boldsymbol{\sigma}[c]_c \end{eqnarray} It is assumed that the client will have stored the pair $(\mathtt{\tiny KEC}(I_\mathbf{b}), I_\mathbf{b})$ at some point prior in order to make the determination of $I_\mathbf{b}$ feasible. -As can be seen, there are four exceptions to the usage of the general execution framework $\Xi$ for evaluation of the message call: these are four so-called `precompiled' contracts, meant as a preliminary piece of architecture that may later become \textit{native extensions}. The four contracts in addresses 1, 2, 3 and 4 execute the elliptic curve public key recovery function, the SHA2 256-bit hash scheme, the RIPEMD 160-bit hash scheme and the identity function respectively. +As can be seen, there are eight exceptions to the usage of the general execution framework $\Xi$ for evaluation of the message call: these are eight so-called `precompiled' contracts, meant as a preliminary piece of architecture that may later become \textit{native extensions}. The eight contracts in addresses 1 to 8 execute the elliptic curve public key recovery function, the SHA2 256-bit hash scheme, the RIPEMD 160-bit hash scheme, the identity function, arbitrary precision modular exponentiation, elliptic curve addition, elliptic curve scalar multiplication and an elliptic curve pairing check respectively. Their full formal definition is in Appendix \ref{app:precompiled}. @@ -803,7 +860,7 @@ \subsection{Basics} The machine does not follow the standard von Neumann architecture. Rather than storing program code in generally-accessible memory or storage, it is stored separately in a virtual ROM interactable only through a specialised instruction. -The machine can have exceptional execution for several reasons, including stack underflows and invalid instructions. Like the out-of-gas (OOG) exception, they do not leave state changes intact. Rather, the machine halts immediately and reports the issue to the execution agent (either the transaction processor or, recursively, the spawning execution environment) which will deal with it separately. +The machine can have exceptional execution for several reasons, including stack underflows and invalid instructions. Like the out-of-gas exception, they do not leave state changes intact. Rather, the machine halts immediately and reports the issue to the execution agent (either the transaction processor or, recursively, the spawning execution environment) which will deal with it separately. \subsection{Fees Overview} @@ -813,31 +870,8 @@ \subsection{Fees Overview} Storage fees have a slightly nuanced behaviour---to incentivise minimisation of the use of storage (which corresponds directly to a larger state database on all nodes), the execution fee for an operation that clears an entry in the storage is not only waived, a qualified refund is given; in fact, this refund is effectively paid up-front since the initial usage of a storage location costs substantially more than normal usage. -%More formally, given an instruction, it is possible to calculate the gas cost of executing it as follows: -% -%\begin{itemize} -%\item {\small SHA3} costs $G_{sha3}$ gas -%\item {\small SLOAD} costs $G_{sload}$ gas -%\item {\small BALANCE} costs $G_{balance}$ gas -%\item {\small SSTORE} costs $d.G_{sstore}$ gas where: -%\begin{itemize} -%\item $d = 2$ if the new value of the storage is non-zero and the old is zero; -%\item $d = 0$ if the new value of the storage is zero and the old is non-zero; -%\item $d = 1$ otherwise. -%\end{itemize} -%\item {\small CALL} costs $G_{call}$, though additional gas may be taken for the execution of the account's associated code, if non-empty. -%\item {\small CREATE} costs $G_{create}$, though additional gas may be taken for the execution of the account initialisation code. -%\item {\small STOP} costs $G_{stop}$ gas -%\item {\small SUICIDE} costs $G_{suicide}$ gas -%\item All other operations cost $G_{step}$ gas. -%\end{itemize} -% -%Additionally, when memory is accessed with {\small MSTORE}, {\small MSTORE8}, {\small MLOAD}, {\small CALLDATACOPY}, {\small CODECOPY}, {\small RETURN}, {\small SHA3}, {\small CREATE} or {\small CALL}, the memory should be enlarged to the smallest multiple of words such that all addressed bytes now fit in it. - See Appendix \ref{app:vm} for a rigorous definition of the EVM gas cost. -%Whenever a higher memory index is referenced, the fee difference to take it to the higher usage from the original (lower) usage is charged. Notably, because {\small MSTORE} and {\small MLOAD} operate on word lengths, they implicitly increase the highest-accessed index to 31 greater than their target index. - \subsection{Execution Environment} In addition to the system state $\boldsymbol{\sigma}$, and the remaining gas for computation $g$, there are several pieces of important information used in the execution environment that the execution agent must provide; these are contained in the tuple $I$: @@ -852,11 +886,18 @@ \subsection{Execution Environment} \item $I_\mathbf{b}$, the byte array that is the machine code to be executed. \item $I_H$, the block header of the present block. \item $I_e$, the depth of the present message-call or contract-creation (i.e. the number of {\small CALL}s or {\small CREATE}s being executed at present). +\item $I_w$, the permission to make modifications to the state. \end{itemize} -The execution model defines the function $\Xi$, which can compute the resultant state $\boldsymbol{\sigma}'$, the remaining gas $g'$, the suicide list $\mathbf{s}$, the log series $\mathbf{l}$, the refunds $r$ and the resultant output, $\mathbf{o}$, given these definitions: +The execution model defines the function $\Xi$, which can compute the resultant state $\boldsymbol{\sigma}'$, the remaining gas $g'$, the accrued substate $A$ and the resultant output, $\mathbf{o}$, given these definitions. For the present context, we will define it as: \begin{equation} -(\boldsymbol{\sigma}', g', \mathbf{s}, \mathbf{l}, r, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}, g, I) +(\boldsymbol{\sigma}', g', A, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}, g, I) +\end{equation} + +where we will remember that $A$, the accrued substate is defined as the tuple of the suicides set $\mathbf{s}$, the log series $\mathbf{l}$, the touched accounts $\mathbf{t}$ and the refunds $r$: + +\begin{equation} +A \equiv (\mathbf{s}, \mathbf{l}, \mathbf{t}, r) \end{equation} \subsection{Execution Overview} @@ -865,28 +906,33 @@ \subsection{Execution Overview} The empty sequence, denoted $()$, is not equal to the empty set, denoted $\varnothing$; this is important when interpreting the output of $H$, which evaluates to $\varnothing$ when execution is to continue but a series (potentially empty) when execution should halt. \begin{eqnarray} -\Xi(\boldsymbol{\sigma}, g, I) & \equiv & X_{0,1,2,4}\big((\boldsymbol{\sigma}, \boldsymbol{\mu}, A^0, I)\big) \\ +\Xi(\boldsymbol{\sigma}, g, I, T) & \equiv & (\boldsymbol{\sigma}'\!, \boldsymbol{\mu}'_g, A, \mathbf{o}) \\ +(\boldsymbol{\sigma}', \boldsymbol{\mu}'\!, A, ..., \mathbf{o}) & \equiv & X\big((\boldsymbol{\sigma}, \boldsymbol{\mu}, A^0\!, I)\big) \\ \boldsymbol{\mu}_g & \equiv & g \\ \boldsymbol{\mu}_{pc} & \equiv & 0 \\ \boldsymbol{\mu}_\mathbf{m} & \equiv & (0, 0, ...) \\ \boldsymbol{\mu}_i & \equiv & 0 \\ -\boldsymbol{\mu}_\mathbf{s} & \equiv & () +\boldsymbol{\mu}_\mathbf{s} & \equiv & () \\ +\boldsymbol{\mu}_\mathbf{o} & \equiv & () \end{eqnarray} -\begin{equation} +\begin{equation} \label{eq:X-def} X\big( (\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I) \big) \equiv \begin{cases} -\big(\varnothing, \boldsymbol{\mu}, A^0, I, ()\big) & \text{if} \quad Z(\boldsymbol{\sigma}, \boldsymbol{\mu}, I)\\ -O(\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I) \cdot \mathbf{o} & \text{if} \quad \mathbf{o} \neq \varnothing\\ -X\big(O(\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I)\big) & \text{otherwise}\\ +\big(\varnothing, \boldsymbol{\mu}, A^0, I, \varnothing\big) & \text{if} \quad Z(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \\ +\big(\varnothing, \boldsymbol{\mu}', A^0, I, \mathbf{o}\big) & \text{if} \quad w = \text{\small REVERT} \\ +O(\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I) \cdot \mathbf{o} & \text{if} \quad \mathbf{o} \neq \varnothing \\ +X\big(O(\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I)\big) & \text{otherwise} \\ \end{cases} \end{equation} where \begin{eqnarray} \mathbf{o} & \equiv & H(\boldsymbol{\mu}, I) \\ -(a, b, c) \cdot d & \equiv & (a, b, c, d) +(a, b, c, d) \cdot e & \equiv & (a, b, c, d, e) \\ +\boldsymbol{\mu}' & \equiv & \boldsymbol{\mu}\ \text{except:} \\ +\boldsymbol{\mu}'_g & \equiv & \boldsymbol{\mu}_g - C(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \end{eqnarray} -Note that we must drop the fourth value in the tuple returned by $X$ to correctly evaluate $\Xi$, hence the subscript $X_{0,1,2,4}$. +Note that, when we evaluate $\Xi$, we drop the fourth element $I'$ and extract the remaining gas $\boldsymbol{\mu}'_g$ from the resultant machine state $\boldsymbol{\mu}'$. $X$ is thus cycled (recursively here, but implementations are generally expected to use a simple iterative loop) until either $Z$ becomes true indicating that the present state is exceptional and that the machine must be halted and any changes discarded or until $H$ becomes a series (rather than the empty set) indicating that the machine has reached a controlled halt. @@ -914,11 +960,21 @@ \subsubsection{Exceptional Halting} \mathbf{\delta}_w = \varnothing \quad \vee \\ \lVert\boldsymbol{\mu}_\mathbf{s}\rVert < \mathbf{\delta}_w \quad \vee \\ ( w \in \{ \text{\small JUMP}, \text{\small JUMPI} \} \quad \wedge \\ \quad \boldsymbol{\mu}_\mathbf{s}[0] \notin D(I_\mathbf{b}) ) \quad \vee \\ -\lVert\boldsymbol{\mu}_\mathbf{s}\rVert - \mathbf{\delta}_w + \mathbf{\alpha}_w > 1024 \quad +( w = \text{\small RETURNDATACOPY} \wedge \\ \quad \boldsymbol{\mu}_\mathbf{s}[1] + \boldsymbol{\mu}_\mathbf{s}[2] > \lVert\boldsymbol{\mu}_\mathbf{o}\rVert) \quad \vee \\ + \lVert\boldsymbol{\mu}_\mathbf{s}\rVert - \mathbf{\delta}_w + \mathbf{\alpha}_w > 1024 \quad \vee \\ + \neg I_w \wedge W(w, \boldsymbol{\mu}) +\end{array} +\end{equation} +where +\begin{equation} +W(w, \boldsymbol{\mu}) \equiv \begin{array}[t]{l} +w \in \{\text{\small CREATE}, \text{\small SSTORE}, \text{\small SELFDESTRUCT}\} \quad \vee \\ +\text{\small LOG0} \le w \wedge w \le \text{\small LOG4} \quad \vee \\ +w \in \{\text{\small CALL}, \text{\small CALLCODE}\} \wedge \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \end{array} \end{equation} -This states that the execution is in an exceptional halting state if there is insufficient gas, if the instruction is invalid (and therefore its $\delta$ subscript is undefined), if there are insufficient stack items, if a {\small JUMP}/{\small JUMPI} destination is invalid or the new stack size would be larger then 1024. The astute reader will realise that this implies that no instruction can, through its execution, cause an exceptional halt. +This states that the execution is in an exceptional halting state if there is insufficient gas, if the instruction is invalid (and therefore its $\delta$ subscript is undefined), if there are insufficient stack items, if a {\small JUMP}/{\small JUMPI} destination is invalid, the new stack size would be larger than 1024 or state modification is attempted during a static call. The astute reader will realise that this implies that no instruction can, through its execution, cause an exceptional halt. \subsubsection{Jump Destination Validity} @@ -952,13 +1008,13 @@ \subsubsection{Normal Halting} The normal halting function $H$ is defined: \begin{equation} H(\boldsymbol{\mu}, I) \equiv \begin{cases} -H_{\text{\tiny RETURN}}(\boldsymbol{\mu}) & \text{if} \quad w = \text{\small RETURN} \\ -() & \text{if} \quad w \in \{ \text{\small STOP}, \text{\small SUICIDE} \} \\ +H_{\text{\tiny RETURN}}(\boldsymbol{\mu}) & \text{if} \quad w \in \{\text{\small RETURN}, \text{\small REVERT}\} \\ +() & \text{if} \quad w \in \{ \text{\small STOP}, \text{\small SELFDESTRUCT} \} \\ \varnothing & \text{otherwise} \end{cases} \end{equation} -The data-returning halt operation, \text{\small RETURN}, has a special function $H_{\text{\tiny RETURN}}$, defined in Appendix \ref{app:vm}. +The data-returning halt operations, \text{\small RETURN} and \text{\small REVERT}, have a special function $H_{\text{\tiny RETURN}}$, defined in Appendix \ref{app:vm}. \subsection{The Execution Cycle} @@ -967,7 +1023,7 @@ \subsection{The Execution Cycle} O\big((\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I)\big) & \equiv & (\boldsymbol{\sigma}', \boldsymbol{\mu}', A', I) \\ \Delta & \equiv & \mathbf{\alpha}_w - \mathbf{\delta}_w \\ \lVert\boldsymbol{\mu}'_\mathbf{s}\rVert & \equiv & \lVert\boldsymbol{\mu}_\mathbf{s}\rVert + \Delta \\ -\quad \forall x \in [\mathbf{\alpha}_w, \lVert\boldsymbol{\mu}'_\mathbf{s}\rVert): \boldsymbol{\mu}'_\mathbf{s}[x] & \equiv & \boldsymbol{\mu}_\mathbf{s}[x+\Delta] +\quad \forall x \in [\mathbf{\alpha}_w, \lVert\boldsymbol{\mu}'_\mathbf{s}\rVert): \boldsymbol{\mu}'_\mathbf{s}[x] & \equiv & \boldsymbol{\mu}_\mathbf{s}[x-\Delta] \end{eqnarray} The gas is reduced by the instruction's gas cost and for most instructions, the program counter increments on each cycle, for the three exceptions, we assume a function $J$, subscripted by one of two instructions, which evaluates to the according value: @@ -980,7 +1036,7 @@ \subsection{The Execution Cycle} \end{cases} \end{eqnarray} -In general, we assume the memory, suicide list and system state don't change: +In general, we assume the memory, self-destruct set and system state don't change: \begin{eqnarray} \boldsymbol{\mu}'_\mathbf{m} & \equiv & \boldsymbol{\mu}_\mathbf{m} \\ \boldsymbol{\mu}'_i & \equiv & \boldsymbol{\mu}_i \\ @@ -1024,7 +1080,7 @@ \subsection{Ommer Validation} where $k$ denotes the ``is-kin'' property: \begin{equation} -k(U, H, n) \equiv \begin{cases} false & \text{if} \quad n = 0 \\ +k(U, H, n) \equiv \begin{cases} false & \text{if} \quad n = 0 \\ s(U, H) &\\ \quad \vee \; k(U, P(H)_H, n - 1) & \text{otherwise} \end{cases} @@ -1049,17 +1105,23 @@ \subsection{Reward Application} The application of rewards to a block involves raising the balance of the accounts of the beneficiary address of the block and each ommer by a certain amount. We raise the block's beneficiary account by $R_b$; for each ommer, we raise the block's beneficiary by an additional $\frac{1}{32}$ of the block reward and the beneficiary of the ommer gets rewarded depending on the block number. Formally we define the function $\Omega$: \begin{eqnarray} +\\ \nonumber \Omega(B, \boldsymbol{\sigma}) & \equiv & \boldsymbol{\sigma}': \boldsymbol{\sigma}' = \boldsymbol{\sigma} \quad \text{except:} \\ -\boldsymbol{\sigma}'[{B_H}_c]_b & = & \boldsymbol{\sigma}[{B_H}_c]_b + (1 + \frac{\lVert B_\mathbf{U}\rVert}{32})R_b \\ -\forall_{U \in B_\mathbf{U}}: \\ \nonumber - \boldsymbol{\sigma}'[U_c]_b & = & \boldsymbol{\sigma}[U_c]_b + (1 + \frac{1}{8} (U_i - {B_H}_i)) R_b +\qquad\boldsymbol{\sigma}'[{B_H}_c]_b & = & \boldsymbol{\sigma}[{B_H}_c]_b + (1 + \frac{\lVert B_\mathbf{U}\rVert}{32})R_b \\ +\qquad\forall_{U \in B_\mathbf{U}}: \\ \nonumber +\boldsymbol{\sigma}'[U_c] & = & \begin{cases} +\varnothing &\text{if}\ \boldsymbol{\sigma}[U_c] = \varnothing\ \wedge\ R = 0 \\ +\mathbf{a}' &\text{otherwise} +\end{cases} \\ +\mathbf{a}' &\equiv& (\boldsymbol{\sigma}[U_c]_n, \boldsymbol{\sigma}[U_c]_b + R, \boldsymbol{\sigma}[U_c]_\mathbf{s}, \boldsymbol{\sigma}[U_c]_c) \\ +R & \equiv & (1 + \frac{1}{8} (U_i - {B_H}_i)) R_b \end{eqnarray} If there are collisions of the beneficiary addresses between ommers and the block (i.e. two ommers with the same beneficiary address or an ommer with the same beneficiary address as the present block), additions are applied cumulatively. -We define the block reward as 5 Ether: +We define the block reward as 3 Ether: \begin{equation} -\text{Let} \quad R_b = 5 \times 10^{18} +\text{Let} \quad R_b = 3 \times 10^{18} \end{equation} \subsection{State \& Nonce Validation}\label{sec:statenoncevalidation} @@ -1085,29 +1147,35 @@ \subsection{State \& Nonce Validation}\label{sec:statenoncevalidation} As specified at the beginning of the present work, $\Pi$ is the state-transition function, which is defined in terms of $\Omega$, the block finalisation function and $\Upsilon$, the transaction-evaluation function, both now well-defined. -As previously detailed, $\mathbf{R}[n]_{\boldsymbol{\sigma}}$, $\mathbf{R}[n]_\mathbf{l}$ and $\mathbf{R}[n]_u$ are the $n$th corresponding states, logs and cumulative gas used after each transaction ($\mathbf{R}[n]_b$, the fourth component in the tuple, has already been defined in terms of the logs). The former is defined simply as the state resulting from applying the corresponding transaction to the state resulting from the previous transaction (or the block's initial state in the case of the first such transaction): +As previously detailed, $\mathbf{R}[n]_{s'}$, $\mathbf{R}[n]_\mathbf{l}$ and $\mathbf{R}[n]_u$ are the $n$th corresponding status code, logs and cumulative gas used after each transaction ($\mathbf{R}[n]_b$, the fourth component in the tuple, has already been defined in terms of the logs). We also define the $n$th state $\boldsymbol{\sigma}[n]$, which is defined simply as the state resulting from applying the corresponding transaction to the state resulting from the previous transaction (or the block's initial state in the case of the first such transaction): \begin{equation} -\mathbf{R}[n]_{\boldsymbol{\sigma}} = \begin{cases} \Gamma(B) & \text{if} \quad n < 0 \\ \Upsilon(\mathbf{R}[n - 1]_{\boldsymbol{\sigma}}, B_\mathbf{T}[n]) & \text{otherwise} \end{cases} +\boldsymbol{\sigma}[n] = \begin{cases} \Gamma(B) & \text{if} \quad n < 0 \\ \Upsilon(\boldsymbol{\sigma}[n - 1], B_\mathbf{T}[n]) & \text{otherwise} \end{cases} \end{equation} In the case of $B_\mathbf{R}[n]_u$, we take a similar approach defining each item as the gas used in evaluating the corresponding transaction summed with the previous item (or zero, if it is the first), giving us a running total: \begin{equation} \mathbf{R}[n]_u = \begin{cases} 0 & \text{if} \quad n < 0 \\ \begin{array}[b]{l} -\Upsilon^g(\mathbf{R}[n - 1]_{\boldsymbol{\sigma}}, B_\mathbf{T}[n])\\ \quad + \mathbf{R}[n-1]_u +\Upsilon^g(\boldsymbol{\sigma}[n - 1], B_\mathbf{T}[n])\\ \quad + \mathbf{R}[n-1]_u \end{array} & \text{otherwise} \end{cases} \end{equation} For $\mathbf{R}[n]_\mathbf{l}$, we utilise the $\Upsilon^\mathbf{l}$ function that we conveniently defined in the transaction execution function. \begin{equation} -\mathbf{R}[n]_\mathbf{l} = -\Upsilon^\mathbf{l}(\mathbf{R}[n - 1]_{\boldsymbol{\sigma}}, B_\mathbf{T}[n]) +\mathbf{R}[n]_\mathbf{l} = +\Upsilon^\mathbf{l}(\boldsymbol{\sigma}[n - 1], B_\mathbf{T}[n]) \end{equation} -Finally, we define $\Pi$ as the new state given the block reward function $\Omega$ applied to the final transaction's resultant state, $\ell(B_\mathbf{R})_{\boldsymbol{\sigma}}$: +We define $\mathbf{R}[n]_{s'}$ in a similar manner. \begin{equation} -\Pi(\boldsymbol{\sigma}, B) \equiv \Omega(B, \ell(\mathbf{R})_{\boldsymbol{\sigma}}) +\mathbf{R}[n]_{s'} = +\Upsilon^{s}(\boldsymbol{\sigma}[n - 1], B_\mathbf{T}[n]) +\end{equation} + +Finally, we define $\Pi$ as the new state given the block reward function $\Omega$ applied to the final transaction's resultant state, $\ell(\boldsymbol{\sigma})$: +\begin{equation} +\Pi(\boldsymbol{\sigma}, B) \equiv \Omega(B, \ell(\boldsymbol{\sigma})) \end{equation} Thus the complete block-transition mechanism, less $\mathtt{PoW}$, the proof-of-work function is defined. @@ -1131,7 +1199,7 @@ \subsection{Mining Proof-of-Work} \label{ch:pow} Where $H_{\hcancel{n}}$ is the new block's header but \textit{without} the nonce and mix-hash components; $H_n$ is the nonce of the header; $\mathbf{d}$ is a large data set needed to compute the mixHash and $H_d$ is the new block's difficulty value (i.e. the block difficulty from section \ref{ch:ghost}). $\mathtt{PoW}$ is the proof-of-work function which evaluates to an array with the first item being the mixHash and the second item being a pseudo-random number cryptographically dependent on $H$ and $\mathbf{d}$. The underlying algorithm is called Ethash and is described below. \subsubsection{Ethash} -Ethash is the planned PoW algorithm for Ethereum 1.0. It is the latest version of Dagger-Hashimoto, introduced by \cite{dagger} and \cite{hashimoto}, although it can no longer appropriately be called that since many of the original features of both algorithms have been drastically changed in the last month of research and development. The general route that the algorithm takes is as follows: +Ethash is the PoW algorithm for Ethereum 1.0. It is the latest version of Dagger-Hashimoto, introduced by \cite{dagger} and \cite{hashimoto}, although it can no longer appropriately be called that since many of the original features of both algorithms have been drastically changed in the last month of research and development. The general route that the algorithm takes is as follows: There exists a seed which can be computed for each block by scanning through the block headers up until that point. From the seed, one can compute a pseudorandom cache, $J_{cacheinit}$ bytes in initial size. Light clients store the cache. From the cache, we can generate a dataset, $J_{datasetinit}$ bytes in initial size, with the property that each item in the dataset depends on only a small number of items from the cache. Full clients and miners store the dataset. The dataset grows linearly with time. @@ -1139,7 +1207,7 @@ \subsubsection{Ethash} \section{Implementing Contracts} -There are several patterns of contracts engineering that allow particular useful behaviours; two of these that I will briefly discuss are data feeds and random numbers. +There are several patterns of contracts engineering that allow particular useful behaviours; two of these that I will briefly discuss are data feeds and random numbers. \subsection{Data Feeds} A data feed contract is one which provides a single service: it gives access to information from the external world within Ethereum. The accuracy and timeliness of this information is not guaranteed and it is the task of a secondary contract author---the contract that utilises the data feed---to determine how much trust can be placed in any single data feed. @@ -1153,7 +1221,7 @@ \section{Future Directions} \label{ch:future} The state database won't be forced to maintain all past state trie structures into the future. It should maintain an age for each node and eventually discard nodes that are neither recent enough nor checkpoints; checkpoints, or a set of nodes in the database that allow a particular block's state trie to be traversed, could be used to place a maximum limit on the amount of computation needed in order to retrieve any state throughout the blockchain. -Blockchain consolidation could be used in order to reduce the amount of blocks a client would need to download to act as a full, mining, node. A compressed archive of the trie structure at given points in time (perhaps one in every 10000th block) could be maintained by the peer network, effectively recasting the genesis block. This would reduce the amount to be downloaded to a single archive plus a hard maximum limit of blocks. +Blockchain consolidation could be used in order to reduce the amount of blocks a client would need to download to act as a full, mining, node. A compressed archive of the trie structure at given points in time (perhaps one in every 10,000th block) could be maintained by the peer network, effectively recasting the genesis block. This would reduce the amount to be downloaded to a single archive plus a hard maximum limit of blocks. Finally, blockchain compression could perhaps be conducted: nodes in state trie that haven't sent/received a transaction in some constant amount of blocks could be thrown out, reducing both Ether-leakage and the growth of the state database. @@ -1171,7 +1239,11 @@ \section{Conclusion} \label{ch:conclusion} \section{Acknowledgements} -Important maintenance, useful corrections and suggestions were provided by a number of others from the Ethereum DEV organisation and Ethereum community at large including Christoph Jentzsch, Gustav Simonsson, Aeron Buchanan, Pawe\l{} Bylica, Jutta Steiner, Nick Savers, Viktor Tr\'{o}n, Marko Simovic and, of course, Vitalik Buterin. +Many thanks to Aeron Buchanan for authoring the \textit{Homestead} revisions, Christoph Jentzsch for authoring the Ethash algorithm and Yoichi Hirai for doing most of the EIP-150 changes. Important maintenance, useful corrections and suggestions were provided by a number of others from the Ethereum DEV organisation and Ethereum community at large including Gustav Simonsson, Pawe\l{} Bylica, Jutta Steiner, Nick Savers, Viktor Tr\'{o}n, Marko Simovic, Giacomo Tazzari and, of course, Vitalik Buterin. + +\section{Availability} + +The source of this paper is maintained at \url{https://github.com/ethereum/yellowpaper/}. An auto-generated PDF is located at \url{https://ethereum.github.io/yellowpaper/paper.pdf}. \bibliography{Biblio} \bibliographystyle{plainnat} @@ -1302,7 +1374,7 @@ \section{Hex-Prefix Encoding}\label{app:hexprefix} Thus the high nibble of the first byte contains two flags; the lowest bit encoding the oddness of the length and the second-lowest encoding the flag $t$. The low nibble of the first byte is zero in the case of an even number of nibbles and the first nibble in the case of an odd number. All remaining nibbles (now an even number) fit properly into the remaining bytes. \section{Modified Merkle Patricia Tree}\label{app:trie} -The modified Merkle Patricia tree (trie) provides a persistent data structure to map between arbitrary-length binary data (byte arrays). It is defined in terms of a mutable data structure to map between 256-bit binary fragments and arbitrary-length binary data, typically implemented as a database. The core of the trie, and its sole requirement in terms of the protocol specification is to provide a single value that identifies a given set of key-value pairs, which may be either a 32 byte sequence or the empty byte sequence. It is left as an implementation consideration to store and maintain the structure of the trie in a manner the allows effective and efficient realisation of the protocol. +The modified Merkle Patricia tree (trie) provides a persistent data structure to map between arbitrary-length binary data (byte arrays). It is defined in terms of a mutable data structure to map between 256-bit binary fragments and arbitrary-length binary data, typically implemented as a database. The core of the trie, and its sole requirement in terms of the protocol specification is to provide a single value that identifies a given set of key-value pairs, which may be either a 32 byte sequence or the empty byte sequence. It is left as an implementation consideration to store and maintain the structure of the trie in a manner that allows effective and efficient realisation of the protocol. Formally, we assume the input value $\mathfrak{I}$, a set containing pairs of byte sequences: \begin{equation} @@ -1341,7 +1413,7 @@ \section{Modified Merkle Patricia Tree}\label{app:trie} In a manner similar to a radix tree, when the trie is traversed from root to leaf, one may build a single key-value pair. The key is accumulated through the traversal, acquiring a single nibble from each branch node (just as with a radix tree). Unlike a radix tree, in the case of multiple keys sharing the same prefix or in the case of a single key having a unique suffix, two optimising nodes are provided. Thus while traversing, one may potentially acquire multiple nibbles from each of the other two node types, extension and leaf. There are three kinds of nodes in the trie: \begin{description} \item[Leaf] A two-item structure whose first item corresponds to the nibbles in the key not already accounted for by the accumulation of keys and branches traversed from the root. The hex-prefix encoding method is used and the second parameter to the function is required to be $true$. -\item[Extension] A two-item structure whose first item corresponds to a series of nibbles of size greater than one that are shared by at least two distinct keys past the accumulation of nibbles keys and branches as traversed from the root. The hex-prefix encoding method is used and the second parameter to the function is required to be $false$. +\item[Extension] A two-item structure whose first item corresponds to a series of nibbles of size greater than one that are shared by at least two distinct keys past the accumulation of the keys of nibbles and the keys of branches as traversed from the root. The hex-prefix encoding method is used and the second parameter to the function is required to be $false$. \item[Branch] A 17-item structure whose first sixteen items correspond to each of the sixteen possible nibble values for the keys at this point in their traversal. The 17th item is used in the case of this being a terminator node and thus a key being ended at this point in its traversal. \end{description} @@ -1368,8 +1440,8 @@ \subsection{Trie Database} \section{Precompiled Contracts}\label{app:precompiled} For each precompiled contract, we make use of a template function, $\Xi_{\mathtt{PRE}}$, which implements the out-of-gas checking. -\begin{equation} -\Xi_{\mathtt{PRE}}(\boldsymbol{\sigma}, g, I) \equiv \begin{cases} +\begin{equation} \label{eq:pre} +\Xi_{\mathtt{PRE}}(\boldsymbol{\sigma}, g, I, T) \equiv \begin{cases} (\varnothing, 0, A^0, ()) & \text{if} \quad g < g_r \\ (\boldsymbol\sigma, g - g_r, A^0, \mathbf{o}) & \text{otherwise}\end{cases} \end{equation} @@ -1409,13 +1481,165 @@ \section{Precompiled Contracts}\label{app:precompiled} \mathtt{\small RIPEMD160}(\mathbf{i} \in \mathbb{B}) & \equiv & o \in \mathbb{B}_{20} \end{eqnarray} -Finally, the fourth contract, the identity function $\Xi_{\mathtt{ID}}$ simply defines the output as the input: +The fourth contract, the identity function $\Xi_{\mathtt{ID}}$ simply defines the output as the input: \begin{eqnarray} \Xi_{\mathtt{ID}} &\equiv& \Xi_{\mathtt{PRE}} \quad \text{where:} \\ g_r &=& 15 + 3\Big\lceil \dfrac{|I_\mathbf{d}|}{32} \Big\rceil\\ \mathbf{o} &=& I_\mathbf{d} \end{eqnarray} +The fifth contract performs arbitrary-precision exponentiation under modulo. Here, $0 ^ 0$ is taken to be one, and $x \bmod 0$ is zero for all $x$. The first word in the input specifies the number of bytes that the first non-negative integer $B$ occupies. The second word in the input specifies the number of bytes that the second non-negative integer $E$ occupies. The third word in the input specifies the number of bytes that the third non-negative integer $M$ occupies. These three words are followed by $B$, $E$ and $M$. The rest of the input is discarded. Whenever the input is too short, the missing bytes are considered to be zero. The output is encoded big-endian into the same format as $M$'s. + +\begin{eqnarray} +\Xi_{\mathtt{EXPMOD}} &\equiv& \Xi_{\mathtt{PRE}} \quad \text{except:} \\ +g_r &=& \Big\lfloor\frac{f\big(\max(\ell_M,\ell_B)\big)\max(\ell'_E,1)}{G_{quaddivisor}}\Big\rfloor \\ +f(x) &\equiv& \begin{cases} +x^2 & \text{if}\ x \le 64 \\ +\Big\lfloor\dfrac{x^2}{4}\Big\rfloor + 96 x - 3072 & \text{if}\ 64 < x \le 1024 \\ +\Big\lfloor\dfrac{x^2}{16}\Big\rfloor + 480x - 199680 & \text{otherwise} +\end{cases}\\ +\ell'_E &=& \begin{cases} +0 & \text{if}\ \ell_E\le 32\wedge E=0 \\ +\lfloor \log_2(E)\rfloor &\text{if}\ \ell_E\le 32 \wedge E \neq 0 \\ +8(\ell_E - 32) + \lfloor \log_2(i[(96+\ell_B)..(127+\ell_B)]) \rfloor & \text{if}\ 32 < \ell_E \wedge i[(96 + \ell_B)..(127 + \ell_B)]\neq 0 \\ +8(\ell_E - 32) & \text{otherwise} \\ +\end{cases} \\ +\mathbf o &=& (B^E\bmod M)\in\mathbb P_{8\ell_M} \\ +\ell_B &\equiv& i[0..31] \\ +\ell_E &\equiv& i[32..63] \\ +\ell_M &\equiv& i[64..95] \\ +B &\equiv& i[96..(95+\ell_B)] \\ +E &\equiv& i[(96+\ell_B)..(95+\ell_B+\ell_E)] \\ +M &\equiv& i[(96+\ell_B+\ell_E)..(95+\ell_B+\ell_E+\ell_M)] \\ +i[x] &\equiv& \begin{cases} +I_{\mathbf d}[x] &\text{if}\ x < |I_{\mathbf d}| \\ +0 &\text{otherwise} +\end{cases} +\end{eqnarray} + +\subsection{zkSNARK Related Precompiled Contracts} + +We choose two numbers, both of which are prime. +\begin{eqnarray} +p &\equiv& 21888242871839275222246405745257275088696311157297823662689037894645226208583 \\ +q &\equiv& 21888242871839275222246405745257275088548364400416034343698204186575808495617 +\end{eqnarray} +Since $p$ is a prime number, $\{0, 1, \ldots, p - 1\}$ forms a field with addition and multiplication modulo $p$. We call this field $F_p$. + +We define a set~$C_1$ with +\begin{equation} +C_1\equiv\{(X,Y)\in F_p\times F_p\mid Y^2=X^3+3\}\cup\{(0,0)\} +\end{equation} +We define a binary operation $+$ on $C_1$ with +\begin{eqnarray}\label{eq:ec-addition} +(X_1, Y_1) + (X_2, Y_2)&\equiv&\begin{cases} +(X,Y)&\text{if}\ X_1\neq X_2\\ +(0,0)&\text{otherwise} +\end{cases}\\ +X&\equiv&\lambda^2-X_1-X_2\\ +Y&\equiv&\lambda(X_1-X)-Y_1\\ +\lambda&\equiv&\frac{Y_2-Y_1}{X_2-X_1} +\end{eqnarray} + +$(C_1,+)$ is known to form a group. We define the scalar multiplication $\cdot$ with +\begin{equation}\label{eq:ec-scalar-multiplication} +n\cdot P\equiv(0,0)+\underbrace{P+\cdots+P}_{n} +\end{equation} +for a natural number $n$ and a point $P$ in $C_1$. + +We define $P_1$ to be a point $(1,2)$ on $C_1$. Let $G_1$ be the subgroup of $(C_1,+)$ generated by $P_1$. $G_1$ is known to be a cyclic group of order $q$. For a point $P$ in $G_1$, we define $\log_{P_1}(P)$ to be the smallest natural number $n$ satisfying $n\cdot P_1=P$. $\log_{P_1}(P)$ is at most $q-1$. + +Let $F_{p^2}$ be a field $F_p[i]/(i+1)$. We define a set $C_2$ with +\begin{equation} +C_2\equiv\{(X,Y)\in F_{p^2}\times F_{p^2}\mid Y^2=X^3+3\}\cup\{(0,0)\} +\end{equation} +We define a binary operation $+$ and a scalar multiplication $\cdot$ with the same equations (\ref{eq:ec-addition}) and (\ref{eq:ec-scalar-multiplication}). $(C_2,+)$ is also known to be a group. We define $P_2$ in $C_2$ with +\begin{eqnarray} +P_2&\equiv& +(11559732032986387107991004021392285783925812861821192530917403151452391805634 \times i\\\nonumber &&+ 10857046999023057135944570762232829481370756359578518086990519993285655852781,\\\nonumber && 4082367875863433681332203403145435568316851327593401208105741076214120093531 \times i\\\nonumber &&+ 8495653923123431417604973247489272438418190587263600148770280649306958101930) +\end{eqnarray} +We define $G_2$ to be the subgroup of $(C_2,+)$ generated by $P_2$. $G_2$ is known to be a cyclic group of order $q$. For a point $P$ in $G_2$, we define $\log_{P_2}(P)$ be the smallest natural number $n$ satisfying $n\cdot P_2=P$. With this definition, $\log_{P_2}(P)$ is at most $q-1$. + +A 32 byte number $\mathbf{x}\in\mathbf{P}_{256}$ might and might not represent an element of $F_p$. +\begin{equation} +\delta_p(\mathbf x)\equiv\begin{cases} +\mathbf x&\text{if}\ \mathbf x 0 \\ -G_{verylow} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[2] \div 32\rceil & \text{if} \quad w = \text{\small CALLDATACOPY} \lor \text{\small CODECOPY} \\ -G_{ext} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[3] \div 32\rceil & \text{if} \quad w = \text{\small EXTCODECOPY} \\ +G_{verylow} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[2] \div 32\rceil & \text{if} \quad w = \text{\small CALLDATACOPY} \lor \text{\small CODECOPY} \lor \text{\small RETURNDATACOPY} \\ +G_{extcode} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[3] \div 32\rceil & \text{if} \quad w = \text{\small EXTCODECOPY} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1] & \text{if} \quad w = \text{\small LOG0} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+G_{logtopic} & \text{if} \quad w = \text{\small LOG1} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+2G_{logtopic} & \text{if} \quad w = \text{\small LOG2} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+3G_{logtopic} & \text{if} \quad w = \text{\small LOG3} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+4G_{logtopic} & \text{if} \quad w = \text{\small LOG4} \\ C_\text{\tiny CALL}(\boldsymbol{\sigma}, \boldsymbol{\mu}) & \text{if} \quad w = \text{\small CALL} \lor \text{\small CALLCODE} \lor \text{\small DELEGATECALL} \\ +C_\text{\tiny SELFDESTRUCT}(\boldsymbol{\sigma}, \boldsymbol{\mu}) & \text{if} \quad w = \text{\small SELFDESTRUCT} \\ G_{create} & \text{if} \quad w = \text{\small CREATE}\\ G_{sha3}+G_{sha3word} \lceil \mathbf{s}[1] \div 32 \rceil & \text{if} \quad w = \text{\small SHA3}\\ G_{jumpdest} & \text{if} \quad w = \text{\small JUMPDEST}\\ @@ -1557,7 +1792,9 @@ \subsection{Gas Cost} G_{low} & \text{if} \quad w \in W_{low}\\ G_{mid} & \text{if} \quad w \in W_{mid}\\ G_{high} & \text{if} \quad w \in W_{high}\\ -G_{ext} & \text{if} \quad w \in W_{ext} +G_{extcode} & \text{if} \quad w \in W_{extcode}\\ +G_{balance} & \text{if} \quad w = \text{\small BALANCE}\\ +G_{blockhash} & \text{if} \quad w = \text{\small BLOCKHASH}\\ \end{cases} \end{equation} \begin{equation} @@ -1568,14 +1805,14 @@ \subsection{Gas Cost} where: \begin{equation} -C_{memory}(a) \equiv G_{memory} \cdot a + \Big\lfloor \dfrac{a^2}{512} \Big\rfloor +C_{mem}(a) \equiv G_{memory} \cdot a + \Big\lfloor \dfrac{a^2}{512} \Big\rfloor \end{equation} -with $C_\text{\tiny CALL}$ and $C_\text{\tiny SSTORE}$ as specified in the appropriate section below. We define the following subsets of instructions: +with $C_\text{\tiny CALL}$, $C_\text{\tiny SELFDESTRUCT}$ and $C_\text{\tiny SSTORE}$ as specified in the appropriate section below. We define the following subsets of instructions: -$W_{zero}$ = \{{\small STOP}, {\small SUICIDE}, {\small RETURN}\} +$W_{zero}$ = \{{\small STOP}, {\small RETURN}, {\small REVERT}\} -$W_{base}$ = \{{\small ADDRESS}, {\small ORIGIN}, {\small CALLER}, {\small CALLVALUE}, {\small CALLDATASIZE}, {\small CODESIZE}, {\small GASPRICE}, {\small COINBASE},\newline \noindent\hspace*{1cm} {\small TIMESTAMP}, {\small NUMBER}, {\small DIFFICULTY}, {\small GASLIMIT}, {\small POP}, {\small PC}, {\small MSIZE}, {\small GAS}\} +$W_{base}$ = \{{\small ADDRESS}, {\small ORIGIN}, {\small CALLER}, {\small CALLVALUE}, {\small CALLDATASIZE}, {\small CODESIZE}, {\small GASPRICE}, {\small COINBASE},\newline \noindent\hspace*{1cm} {\small TIMESTAMP}, {\small NUMBER}, {\small DIFFICULTY}, {\small GASLIMIT}, {\small RETURNDATASIZE}, {\small POP}, {\small PC}, {\small MSIZE}, {\small GAS}\} $W_{verylow}$ = \{{\small ADD}, {\small SUB}, {\small NOT}, {\small LT}, {\small GT}, {\small SLT}, {\small SGT}, {\small EQ}, {\small ISZERO}, {\small AND}, {\small OR}, {\small XOR}, {\small BYTE}, {\small CALLDATALOAD}, \newline \noindent\hspace*{1cm} {\small MLOAD}, {\small MSTORE}, {\small MSTORE8}, {\small PUSH*}, {\small DUP*}, {\small SWAP*}\} @@ -1585,13 +1822,13 @@ \subsection{Gas Cost} $W_{high}$ = \{{\small JUMPI}\} -$W_{ext}$ = \{{\small BALANCE}, {\small EXTCODESIZE}, {\small BLOCKHASH}\} +$W_{extcode}$ = \{{\small EXTCODESIZE}\} Note the memory cost component, given as the product of $G_{memory}$ and the maximum of 0 \& the ceiling of the number of words in size that the memory must be over the current number of words, $\boldsymbol{\mu}_i$ in order that all accesses reference valid memory whether for read or write. Such accesses must be for non-zero number of bytes. Referencing a zero length range (e.g. by attempting to pass it as the input range to a CALL) does not require memory to be extended to the beginning of the range. $\boldsymbol{\mu}'_i$ is defined as this new maximum number of words of active memory; special-cases are given where these two are not equal. -Note also that $C_{memory}$ is the memory cost function (the expansion function being the difference between the cost before and after). It is a polynomial, with the higher-order coefficient divided and floored, and thus linear up to 724B of memory used, after which it costs substantially more. +Note also that $C_{mem}$ is the memory cost function (the expansion function being the difference between the cost before and after). It is a polynomial, with the higher-order coefficient divided and floored, and thus linear up to 724B of memory used, after which it costs substantially more. While defining the instruction set, we defined the memory-expansion for range function, $M$, thus: @@ -1602,6 +1839,12 @@ \subsection{Gas Cost} \end{cases} \end{equation} +Another useful function is ``all but one 64th'' function~$L$ defined as: + +\begin{equation} +L(n) \equiv n - \lfloor n / 64 \rfloor +\end{equation} + \subsection{Instruction Set} As previously specified in section \ref{ch:model}, these definitions take place in the final context there. In particular we assume $O$ is the EVM state-progression function and define the terms pertaining to the next cycle's state $(\boldsymbol{\sigma}', \boldsymbol{\mu}')$ such that: @@ -1614,7 +1857,7 @@ \subsection{Instruction Set} \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{0s: Stop and Arithmetic Operations}} \\ -\multicolumn{5}{l}{All arithmetic is modulo $2^{256}$ unless otherwise noted.} \vspace{5pt} \\ +\multicolumn{5}{l}{All arithmetic is modulo $2^{256}$ unless otherwise noted. The zero-th power of zero $0^0$ is defined to be one.} \vspace{5pt} \\ \textbf{Value} & \textbf{Mnemonic} & $\delta$ & $\alpha$ & \textbf{Description} \vspace{5pt} \\ 0x00 & {\small STOP} & 0 & 0 & Halts execution. \\ \midrule @@ -1639,7 +1882,7 @@ \subsection{Instruction Set} &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0\\ \boldsymbol{\mu}_\mathbf{s}[0] \bmod \boldsymbol{\mu}_\mathbf{s}[1] & \text{otherwise}\end{cases}$ \\ \midrule 0x07 & {\small SMOD} & 2 & 1 & Signed modulo remainder operation. \\ -&&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0\\ \mathbf{sgn} (\boldsymbol{\mu}_\mathbf{s}[0]) |\boldsymbol{\mu}_\mathbf{s}[0]| \bmod |\boldsymbol{\mu}_\mathbf{s}[1]| & \text{otherwise}\end{cases}$ \\ +&&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0\\ \mathbf{sgn} (\boldsymbol{\mu}_\mathbf{s}[0]) (|\boldsymbol{\mu}_\mathbf{s}[0]| \bmod |\boldsymbol{\mu}_\mathbf{s}[1]|) & \text{otherwise}\end{cases}$ \\ &&&& Where all values are treated as two's complement signed 256-bit integers. \\ \midrule 0x08 & {\small ADDMOD} & 3 & 1 & Modulo addition operation. \\ @@ -1656,27 +1899,28 @@ \subsection{Instruction Set} 0x0b & {\small SIGNEXTEND} & 2 & 1 & Extend length of two's complement signed integer. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \begin{cases} \boldsymbol{\mu}_\mathbf{s}[1]_t &\text{if} \quad i \leqslant t \quad \text{where} \; t = 256 - 8(\boldsymbol{\mu}_\mathbf{s}[0] + 1) \\ \boldsymbol{\mu}_\mathbf{s}[1]_i &\text{otherwise} \end{cases}$ \\ \multicolumn{5}{l}{$\boldsymbol{\mu}_\mathbf{s}[x]_i$ gives the $i$th bit (counting from zero) of $\boldsymbol{\mu}_\mathbf{s}[x]$} \vspace{5pt} \\ +\midrule \end{tabular*} \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{10s: Comparison \& Bitwise Logic Operations}} \\ \textbf{Value} & \textbf{Mnemonic} & $\delta$ & $\alpha$ & \textbf{Description} \vspace{5pt} \\ -0x10 & {\small LT} & 2 & 1 & Less-than comparision. \\ +0x10 & {\small LT} & 2 & 1 & Less-than comparison. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] < \boldsymbol{\mu}_\mathbf{s}[1] \\ 0 & \text{otherwise} \end{cases}$ \\ \midrule -0x11 & {\small GT} & 2 & 1 & Greater-than comparision. \\ +0x11 & {\small GT} & 2 & 1 & Greater-than comparison. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] > \boldsymbol{\mu}_\mathbf{s}[1] \\ 0 & \text{otherwise} \end{cases}$ \\ \midrule -0x12 & {\small SLT} & 2 & 1 & Signed less-than comparision. \\ +0x12 & {\small SLT} & 2 & 1 & Signed less-than comparison. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] < \boldsymbol{\mu}_\mathbf{s}[1] \\ 0 & \text{otherwise} \end{cases}$ \\ &&&& Where all values are treated as two's complement signed 256-bit integers. \\ \midrule -0x13 & {\small SGT} & 2 & 1 & Signed greater-than comparision. \\ +0x13 & {\small SGT} & 2 & 1 & Signed greater-than comparison. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] > \boldsymbol{\mu}_\mathbf{s}[1] \\ 0 & \text{otherwise} \end{cases}$ \\ &&&& Where all values are treated as two's complement signed 256-bit integers. \\ \midrule -0x14 & {\small EQ} & 2 & 1 & Equality comparision. \\ +0x14 & {\small EQ} & 2 & 1 & Equality comparison. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] = \boldsymbol{\mu}_\mathbf{s}[1] \\ 0 & \text{otherwise} \end{cases}$ \\ \midrule 0x15 & {\small ISZERO} & 1 & 1 & Simple not operator. \\ @@ -1743,6 +1987,8 @@ \subsection{Instruction Set} 0x37 & {\small CALLDATACOPY} & 3 & 0 & Copy input data in current environment to memory. \\ &&&& $\forall_{i \in \{ 0 \dots \boldsymbol{\mu}_\mathbf{s}[2] - 1\} } \boldsymbol{\mu}'_\mathbf{m}[\boldsymbol{\mu}_\mathbf{s}[0] + i ] \equiv \begin{cases} I_\mathbf{d}[\boldsymbol{\mu}_\mathbf{s}[1] + i] & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] + i < \lVert I_\mathbf{d} \rVert \\ 0 & \text{otherwise} \end{cases}$\\ +&&&& The additions in $\boldsymbol{\mu}_\mathbf{s}[1] + i$ are not subject to the $2^{256}$ modulo. \\ +&&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[2])$ \\ &&&& This pertains to the input data passed with the message call instruction or transaction. \\ \midrule 0x38 & {\small CODESIZE} & 0 & 1 & Get size of code running in current environment. \\ @@ -1751,18 +1997,31 @@ \subsection{Instruction Set} 0x39 & {\small CODECOPY} & 3 & 0 & Copy code running in current environment to memory. \\ &&&& $\forall_{i \in \{ 0 \dots \boldsymbol{\mu}_\mathbf{s}[2] - 1\} } \boldsymbol{\mu}'_\mathbf{m}[\boldsymbol{\mu}_\mathbf{s}[0] + i ] \equiv \begin{cases} I_\mathbf{b}[\boldsymbol{\mu}_\mathbf{s}[1] + i] & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] + i < \lVert I_\mathbf{b} \rVert \\ \text{\small STOP} & \text{otherwise} \end{cases}$\\ +&&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[2])$ \\ +&&&& The additions in $\boldsymbol{\mu}_\mathbf{s}[1] + i$ are not subject to the $2^{256}$ modulo. \\ \midrule 0x3a & {\small GASPRICE} & 0 & 1 & Get price of gas in current environment. \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_p$ \\ &&&& This is gas price specified by the originating transaction.\\ \midrule 0x3b & {\small EXTCODESIZE} & 1 & 1 & Get size of an account's code. \\ -&&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \lVert \boldsymbol{\sigma}[\boldsymbol{\mu}_s[0] \mod 2^{160}]_c \rVert$ \\ +&&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \lVert \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}]_c \rVert$ \\ \midrule 0x3c & {\small EXTCODECOPY} & 4 & 0 & Copy an account's code to memory. \\ &&&& $\forall_{i \in \{ 0 \dots \boldsymbol{\mu}_\mathbf{s}[3] - 1\} } \boldsymbol{\mu}'_\mathbf{m}[\boldsymbol{\mu}_\mathbf{s}[1] + i ] \equiv \begin{cases} \mathbf{c}[\boldsymbol{\mu}_\mathbf{s}[2] + i] & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] + i < \lVert \mathbf{c} \rVert \\ \text{\small STOP} & \text{otherwise} \end{cases}$\\ -&&&& where $\mathbf{c} \equiv \boldsymbol{\sigma}[\boldsymbol{\mu}_s[0] \mod 2^{160}]_c$ \\ +&&&& where $\mathbf{c} \equiv \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}]_c$ \\ +&&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[1], \boldsymbol{\mu}_\mathbf{s}[3])$ \\ +&&&& The additions in $\boldsymbol{\mu}_\mathbf{s}[2] + i$ are not subject to the $2^{256}$ modulo. \\ +\midrule +0x3d & {\small RETURNDATASIZE} & 0 & 1 & Get size of output data from the previous call from the current environment. \\ +&&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv \lVert \boldsymbol{\mu}_\mathbf{o} \rVert$ \\ +\midrule +0x3e & {\small RETURNDATACOPY} & 3 & 0 & Copy output data from the previous call to memory. \\ +&&&& $\forall_{i \in \{ 0 \dots \boldsymbol{\mu}_\mathbf{s}[2] - 1\} } \boldsymbol{\mu}'_\mathbf{m}[\boldsymbol{\mu}_\mathbf{s}[0] + i ] \equiv +\begin{cases} \boldsymbol{\mu}_\mathbf{o}[\boldsymbol{\mu}_\mathbf{s}[1] + i] & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] + i < \lVert \boldsymbol{\mu}_\mathbf{o} \rVert \\ 0 & \text{otherwise} \end{cases}$\\ +&&&& The additions in $\boldsymbol{\mu}_\mathbf{s}[1] + i$ are not subject to the $2^{256}$ modulo. \\ +&&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[2])$ \\ \bottomrule \end{tabular*} @@ -1922,6 +2181,8 @@ \subsection{Instruction Set} \multicolumn{5}{c}{\textbf{a0s: Logging Operations}} \vspace{5pt} \\ \multicolumn{5}{l}{For all logging operations, the state change is to append an additional log entry on to the substate's log series:}\\ \multicolumn{5}{l}{$A'_\mathbf{l} \equiv A_\mathbf{l} \cdot (I_a, \mathbf{t}, \boldsymbol{\mu}_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[0] \dots (\boldsymbol{\mu}_\mathbf{s}[0] + \boldsymbol{\mu}_\mathbf{s}[1] - 1) ])$}\\ +\multicolumn{5}{l}{and to update the memory consumption counter:}\\ +\multicolumn{5}{l}{$\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[1])$}\\ \multicolumn{5}{l}{The entry's topic series, $\mathbf{t}$, differs accordingly:}\vspace{5pt} \\ \textbf{Value} & \textbf{Mnemonic} & $\delta$ & $\alpha$ & \textbf{Description} \vspace{5pt} \\ 0xa0 & {\small LOG0} & 2 & 0 & Append log record with no topics. \\ @@ -1943,50 +2204,58 @@ \subsection{Instruction Set} \textbf{Value} & \textbf{Mnemonic} & $\delta$ & $\alpha$ & \textbf{Description} \vspace{5pt} \\ 0xf0 & {\small CREATE} & 3 & 1 & Create a new account with associated code. \\ &&&& $\mathbf{i} \equiv \boldsymbol{\mu}_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[1] \dots (\boldsymbol{\mu}_\mathbf{s}[1] + \boldsymbol{\mu}_\mathbf{s}[2] - 1) ]$ \\ -&&&& $(\boldsymbol{\sigma}', \boldsymbol{\mu}'_g, A^+) \equiv \begin{cases}\Lambda(\boldsymbol{\sigma}^*, I_a, I_o, \boldsymbol{\mu}_g, I_p, \boldsymbol{\mu}_\mathbf{s}[0], \mathbf{i}, I_e + 1) & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\; I_e < 1024\\ \big(\boldsymbol{\sigma}, \boldsymbol{\mu}_g, \varnothing\big) & \text{otherwise} \end{cases}$ \\ +&&&& $(\boldsymbol{\sigma}', \boldsymbol{\mu}'_g, A^+, \mathbf{o}) \equiv \begin{cases}\Lambda(\boldsymbol{\sigma}^*, I_a, I_o, L(\boldsymbol{\mu}_g), I_p, \boldsymbol{\mu}_\mathbf{s}[0], \mathbf{i}, I_e + 1, I_w) & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\; I_e < 1024\\ \big(\boldsymbol{\sigma}, \boldsymbol{\mu}_g, \varnothing\big) & \text{otherwise} \end{cases}$ \\ &&&& $\boldsymbol{\sigma}^* \equiv \boldsymbol{\sigma} \quad \text{except} \quad \boldsymbol{\sigma}^*[I_a]_n = \boldsymbol{\sigma}[I_a]_n + 1$ \\ -&&&& $A' \equiv A \Cup A^+$ which implies: $A'_\mathbf{s} \equiv A_\mathbf{s} \cup A^+_\mathbf{s} \quad \wedge \quad A'_\mathbf{l} \equiv A_\mathbf{l} \cdot A^+_\mathbf{l} \quad \wedge \quad A'_\mathbf{r} \equiv A_\mathbf{r} + A^+_\mathbf{r}$ \\ +&&&& $A' \equiv A \Cup A^+$ which abbreviates: $A'_\mathbf{s} \equiv A_\mathbf{s} \cup A^+_\mathbf{s} \quad \wedge \quad A'_\mathbf{l} \equiv A_\mathbf{l} \cdot A^+_\mathbf{l} \quad \wedge \quad A'_\mathbf{t} \equiv A_\mathbf{t} \cup A^+_\mathbf{t}$ \\ +&&&& $ \wedge \quad A'_\mathbf{r} \equiv A_\mathbf{r} + A^+_\mathbf{r}$ \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv x$ \\ -&&&& where $x=0$ if the code execution for this operation failed due to an exceptional halting \\ -&&&& $Z(\boldsymbol{\sigma}^*, \boldsymbol{\mu}, I) = \top$ or $I_e = 1024$ \\ +&&&& where $x=0$ if the code execution for this operation failed due to an exceptional halting\\ +&&&& (or for a \text{\small REVERT}) $\boldsymbol{\sigma}' = \varnothing$, or $I_e = 1024$ \\ &&&& (the maximum call depth limit is reached) or $\boldsymbol{\mu}_\mathbf{s}[0] > \boldsymbol{\sigma}[I_a]_b$ (balance of the caller is too \\ &&&& low to fulfil the value transfer); and otherwise $x=A(I_a, \boldsymbol{\sigma}[I_a]_n)$, the address of the newly \\ -&&&& created account, otherwise. \\ +&&&& created account. \\ &&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[1], \boldsymbol{\mu}_\mathbf{s}[2])$ \\ +&&&& $\boldsymbol{\mu}'_\mathbf{o} \equiv ()$ \\ &&&& Thus the operand order is: value, input offset, input size. \\ \midrule 0xf1 & {\small CALL} & 7 & 1 & Message-call into an account. \\ &&&& $\mathbf{i} \equiv \boldsymbol{\mu}_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[3] \dots (\boldsymbol{\mu}_\mathbf{s}[3] + \boldsymbol{\mu}_\mathbf{s}[4] - 1) ]$ \\ -&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}, I_a, I_o, t, t,\\ \quad C_{\text{\tiny CALLGAS}}(\boldsymbol{\mu}), I_p, \boldsymbol{\mu}_\mathbf{s}[2], \boldsymbol{\mu}_\mathbf{s}[2], \mathbf{i}, I_e + 1)\end{array} & \begin{array}{l}\text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge \\ \quad\quad I_e < 1024\end{array}\\ (\boldsymbol{\sigma}, g, \varnothing, \mathbf{o}) & \text{otherwise} \end{cases}$ \\ +&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}, I_a, I_o, t, t,\\ \quad C_{\text{\tiny CALLGAS}}(\boldsymbol{\mu}), I_p, \boldsymbol{\mu}_\mathbf{s}[2], \boldsymbol{\mu}_\mathbf{s}[2], \mathbf{i}, I_e + 1, I_w)\end{array} & \begin{array}{l}\text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge \\ \quad\quad I_e < 1024\end{array}\\ (\boldsymbol{\sigma}, g, \varnothing, ()) & \text{otherwise} \end{cases}$ \\ &&&& $n \equiv \min(\{ \boldsymbol{\mu}_\mathbf{s}[6], |\mathbf{o}|\})$ \\ &&&& $\boldsymbol{\mu}'_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[5] \dots (\boldsymbol{\mu}_\mathbf{s}[5] + n - 1) ] = \mathbf{o}[0 \dots (n - 1)]$ \\ +&&&& $\boldsymbol{\mu}'_\mathbf{o} = \mathbf{o}$ \\ &&&& $\boldsymbol{\mu}'_g \equiv \boldsymbol{\mu}_g + g'$ \\ &&&& $\boldsymbol{\mu}'_\mathbf{s}[0] \equiv x$ \\ &&&& $A' \equiv A \Cup A^+$ \\ &&&& $t \equiv \boldsymbol{\mu}_\mathbf{s}[1] \mod 2^{160}$ \\ &&&& where $x=0$ if the code execution for this operation failed due to an exceptional halting \\ -&&&& $Z(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) = \top$ or if \\ +&&&& (or for a \text{\small REVERT}) $\boldsymbol{\sigma}' = \varnothing$ or if \\ &&&& $\boldsymbol{\mu}_\mathbf{s}[2] > \boldsymbol{\sigma}[I_a]_b$ (not enough funds) or $I_e = 1024$ (call depth limit reached); $x=1$ \\ &&&& otherwise. \\ &&&& $\boldsymbol{\mu}'_i \equiv M(M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[3], \boldsymbol{\mu}_\mathbf{s}[4]), \boldsymbol{\mu}_\mathbf{s}[5], \boldsymbol{\mu}_\mathbf{s}[6])$ \\ &&&& Thus the operand order is: gas, to, value, in offset, in size, out offset, out size. \\ -&&&& $C_{\text{\tiny CALL}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv G_{call} + \boldsymbol{\mu}_\mathbf{s}[0] + C_{\text{\tiny CALLXFER}}(\boldsymbol{\mu}) + C_{\text{\tiny CALLNEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu})$ \\ -&&&& $C_{\text{\tiny CALLXFER}}(\boldsymbol{\mu}) \equiv \begin{cases} +&&&& $C_{\text{\tiny CALL}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv C_{\text{\tiny GASCAP}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) + C_{\text{\tiny EXTRA}}(\boldsymbol{\sigma}, \boldsymbol{\mu})$ \\ +&&&& $C_{\text{\tiny CALLGAS}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} +C_{\text{\tiny GASCAP}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) + G_{callstipend} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ +C_{\text{\tiny GASCAP}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) & \text{otherwise} +\end{cases}$ \\ +&&&& $C_{\text{\tiny GASCAP}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} +\min\{ L(\boldsymbol{\mu}_g - C_{\text{\tiny EXTRA}}(\boldsymbol{\sigma}, \boldsymbol{\mu})), \boldsymbol{\mu}_{\mathbf{s}}[0] \} & \text{if} \quad \boldsymbol{\mu}_g \ge C_{\text{\tiny EXTRA}}(\boldsymbol{\sigma}, \boldsymbol{\mu})\\ +\boldsymbol{\mu}_{\mathbf{s}}[0] & \text{otherwise} +\end{cases}$\\ +&&&& $C_{\text{\tiny EXTRA}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv G_{call} + C_{\text{\tiny XFER}}(\boldsymbol{\mu}) + C_{\text{\tiny NEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu})$\\ +&&&& $C_{\text{\tiny XFER}}(\boldsymbol{\mu}) \equiv \begin{cases} G_{callvalue} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ 0 & \text{otherwise} \end{cases}$ \\ -&&&& $C_{\text{\tiny CALLNEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} -G_{callnewaccount} & \text{if} \quad \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[1] \mod 2^{160}] = \varnothing \\ +&&&& $C_{\text{\tiny NEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} +G_{newaccount} & \text{if} \quad \mathtt{\tiny DEAD}(\boldsymbol{\sigma}, \boldsymbol{\mu}_\mathbf{s}[1] \mod 2^{160}) \wedge \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ 0 & \text{otherwise} \end{cases}$ \\ -&&&& $C_{\text{\tiny CALLGAS}}(\boldsymbol{\mu}) \equiv \begin{cases} -\boldsymbol{\mu}_\mathbf{s}[0] + G_{callstipend} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ -\boldsymbol{\mu}_\mathbf{s}[0] & \text{otherwise} -\end{cases}$ \\ \midrule 0xf2 & {\small CALLCODE} & 7 & 1 & Message-call into this account with an alternative account's code. \\ &&&& Exactly equivalent to {\small CALL} except: \\ -&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}^*, I_a, I_o, I_a, t,\\\quad C_{\text{\tiny CALLGAS}}(\boldsymbol{\mu}), I_p, \boldsymbol{\mu}_\mathbf{s}[2], \boldsymbol{\mu}_\mathbf{s}[2], \mathbf{i}, I_e + 1)\end{array} & \begin{array}{l}\text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\\ \quad\quad{}I_e < 1024\end{array} \\ (\boldsymbol{\sigma}, g, \varnothing, \mathbf{o}) & \text{otherwise} \end{cases}$ \\ +&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}^*, I_a, I_o, I_a, t,\\\quad C_{\text{\tiny CALLGAS}}(\boldsymbol{\mu}), I_p, \boldsymbol{\mu}_\mathbf{s}[2], \boldsymbol{\mu}_\mathbf{s}[2], \mathbf{i}, I_e + 1, I_w)\end{array} & \begin{array}{l}\text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\\ \quad\quad{}I_e < 1024\end{array} \\ (\boldsymbol{\sigma}, g, \varnothing, ()) & \text{otherwise} \end{cases}$ \\ &&&& Note the change in the fourth parameter to the call $\Theta$ from the 2nd stack value $\boldsymbol{\mu}_\mathbf{s}[1]$\\ &&&& (as in {\small CALL}) to the present address $I_a$. This means that the recipient is in fact the\\ &&&& same account as at present, simply that the code is overwritten.\\ @@ -2002,27 +2271,52 @@ \subsection{Instruction Set} \midrule 0xf4 & {\small DELEGATECALL} & 6 & 1 & Message-call into this account with an alternative account's code, but persisting\\ &&&& the current values for {\it sender} and {\it value}. \\ -&&&& Exactly equivalent to {\small CALL} except: \\ -&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}^*, I_s, I_o, I_a, t,\\\quad \boldsymbol{\mu}_\mathbf{s}[0], I_p, 0, \boldsymbol{\mu}_\mathbf{s}[2], \mathbf{i}, I_e + 1)\end{array} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\; I_e < 1024 \\ (\boldsymbol{\sigma}, g, \varnothing, \mathbf{o}) & \text{otherwise} \end{cases}$ \\ -&&&& Note the changes (in addition to that of the fourth parameter) to the second and eighth\\ -&&&& parameters to the call $\Theta$.\\ +&&&& Compared with {\small CALL}, {\small DELEGATECALL} takes one fewer arguments. The omitted\\ +&&&& argument is $\boldsymbol{\mu}_\mathbf{s}[2]$. As a result, $\boldsymbol{\mu}_\mathbf{s}[3]$, $\boldsymbol{\mu}_\mathbf{s}[4]$, $\boldsymbol{\mu}_\mathbf{s}[5]$ and $\boldsymbol{\mu}_\mathbf{s}[6]$ in the definition of {\small CALL} \\ +&&&& should respectively be replaced with $\boldsymbol{\mu}_\mathbf{s}[2]$, $\boldsymbol{\mu}_\mathbf{s}[3]$, $\boldsymbol{\mu}_\mathbf{s}[4]$ and $\boldsymbol{\mu}_\mathbf{s}[5]$. \\ +&&&& Otherwise exactly equivalent to {\small CALL} except: \\ +&&&& $(\boldsymbol{\sigma}', g', A^+, \mathbf{o}) \equiv \begin{cases}\begin{array}{l}\Theta(\boldsymbol{\sigma}^*, I_s, I_o, I_a, t,\\\quad \boldsymbol{\mu}_\mathbf{s}[0], I_p, 0, I_v, \mathbf{i}, I_e + 1, I_w)\end{array} & \text{if} \quad I_v \leqslant \boldsymbol{\sigma}[I_a]_b \;\wedge\; I_e < 1024 \\ (\boldsymbol{\sigma}, g, \varnothing, ()) & \text{otherwise} \end{cases}$ \\ +&&&& Note the changes (in addition to that of the fourth parameter) to the second \\ +&&&& and ninth parameters to the call $\Theta$.\\ &&&& This means that the recipient is in fact the same account as at present, simply\\ &&&& that the code is overwritten {\it and} the context is almost entirely identical.\\ \midrule -0xff & {\small SUICIDE} & 1 & 0 & Halt execution and register account for later deletion. \\ +0xfa & {\small STATICCALL} & 6 & 1 & Static message-call into an account. \\ +&&&& Exactly equivalent to {\small CALL} except: \\ +&&&& The argument $\boldsymbol{\mu}_\mathbf{s}[2]$ is replaced with $0$. \\ +&&&& The deeper argument $\boldsymbol{\mu}_\mathbf{s}[3]$, $\boldsymbol{\mu}_\mathbf{s}[4]$, $\boldsymbol{\mu}_\mathbf{s}[5]$ and $\boldsymbol{\mu}_\mathbf{s}[6]$ are respectively replaced with \\ +&&&& $\boldsymbol{\mu}_\mathbf{s}[2]$, $\boldsymbol{\mu}_\mathbf{s}[3]$, $\boldsymbol{\mu}_\mathbf{s}[4]$ and $\boldsymbol{\mu}_\mathbf{s}[5]$. \\ +&&&& The last argument of $\Theta$ is $\bot$. \\ +\midrule +0xfd & {\small REVERT} & 2 & 0 & Halt execution reverting state changes but returning data and remaining gas. \\ +&&&& The effect of this operation is described in (\ref{eq:X-def}). \\ +&&&& For the gas calculation, we use the memory expansion function, \\ +&&&& $\boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[1])$ \\ +\midrule +0xfe & {\small INVALID} & $\varnothing$ & $\varnothing$ & Designated invalid instruction. \\ +\midrule +0xff & {\small SELFDESTRUCT} & 1 & 0 & Halt execution and register account for later deletion. \\ &&&& $A'_\mathbf{s} \equiv A_\mathbf{s} \cup \{ I_a \}$ \\ -&&&& $\boldsymbol{\sigma}'[\boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}]_b \equiv \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}]_b + \boldsymbol{\sigma}[I_a]_b$ \\ -&&&& $\boldsymbol{\sigma}'[I_a]_b \equiv 0$ \\ +&&&& $\boldsymbol{\sigma}'[r] \equiv \begin{cases} +\varnothing &\text{if}\ \boldsymbol{\sigma}[r] = \varnothing\ \wedge\ \boldsymbol{\sigma}[I_a]_b = 0\\ +(\boldsymbol{\sigma}[r]_n, \boldsymbol{\sigma}[r]_b + \boldsymbol{\sigma}[I_a]_b, \boldsymbol{\sigma}[r]_\mathbf{s}, \boldsymbol{\sigma}[r]_c) & \text{if}\ r \neq I_a \\ +(\boldsymbol{\sigma}[r]_n, 0, \boldsymbol{\sigma}[r]_\mathbf{s}, \boldsymbol{\sigma}[r]_c) & \text{otherwise} +\end{cases}$\\ +&&&& where $r = \boldsymbol{\mu}_\mathbf{s}[0] \bmod 2^{160}$\\ +&&&& $\boldsymbol{\sigma}'[I_a]_b = 0$ \\ &&&& $A'_{r} \equiv A_{r} + \begin{cases} -R_{suicide} & \text{if} \quad I_a \notin A_\mathbf{s} \\ +R_{selfdestruct} & \text{if} \quad I_a \notin A_\mathbf{s} \\ +0 & \text{otherwise} +\end{cases}$ \\ +&&&& $C_{\text{\tiny SELFDESTRUCT}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv G_{selfdestruct} + \begin{cases} +G_{newaccount} & \text{if} \quad n \\ 0 & \text{otherwise} \end{cases}$ \\ +&&&& $n \equiv \mathtt{\tiny DEAD}(\boldsymbol{\sigma}, \boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}) \wedge \boldsymbol{\sigma}[I_a]_b \neq 0$ \\ \bottomrule \end{tabular*} -%\section{Low-level Lisp-like Language}\label{app:lll} -%The Low-level Lisp-like Language is a language created in order to efficiently author low-level programs (contracts) without having to resort to EVM-Assembly. - +\hypertarget{GenesisBlock}{} \section{Genesis Block}\label{app:genesis} The genesis block is 15 items, and is specified thus: @@ -2032,7 +2326,7 @@ \section{Genesis Block}\label{app:genesis} Where $0_{256}$ refers to the parent hash, a 256-bit hash which is all zeroes; $0_{160}$ refers to the beneficiary address, a 160-bit hash which is all zeroes; $0_{2048}$ refers to the log bloom, 2048-bit of all zeros; $2^{17}$ refers to the difficulty; the transaction trie root, receipt trie root, gas used, block number and extradata are both $0$, being equivalent to the empty byte array. The sequences of both ommers and transactions are empty and represented by $()$. $\mathtt{\tiny KEC}\big( (42) \big)$ refers to the Keccak hash of a byte array of length one whose first and only byte is of value 42, used for the nonce. $\mathtt{\tiny KEC}\big(\mathtt{\tiny RLP}\big( () \big)\big)$ value refers to the hash of the ommer lists in RLP, both empty lists. -The proof-of-concept series include a development premine, making the state root hash some value $stateRoot$. Also $time$ will be set to the intial timestamp of the genesis block. The latest documentation should be consulted for those values. +The proof-of-concept series include a development premine, making the state root hash some value $stateRoot$. Also $time$ will be set to the initial timestamp of the genesis block. The latest documentation should be consulted for those values. \section{Ethash}\label{app:ethash} \subsection{Definitions} @@ -2061,7 +2355,7 @@ \subsection{Size of dataset and cache} \begin{equation} E_{epoch}(H_i) = \left\lfloor\frac{H_i}{J_{epoch}}\right\rfloor \end{equation} -The size of the dataset growth by $J_{datasetgrowth}$ bytes, and the size of the cache by $J_{cachegrowth}$ bytes, every epoch. In order to avoid regularity leading to cyclic behavior, the size must be a prime number. Therefore the size is reduced by a multiple of $J_{mixbytes}$, for the dataset, and $J_{hashbytes}$ for the cache. +The size of the dataset growth by $J_{datasetgrowth}$ bytes, and the size of the cache by $J_{cachegrowth}$ bytes, every epoch. In order to avoid regularity leading to cyclic behavior, the size must be a prime number. Therefore the size is reduced by a multiple of $J_{mixbytes}$, for the dataset, and $J_{hashbytes}$ for the cache. Let $d_{size} = \lVert \mathbf{d} \rVert$ be the size of the dataset. Which is calculated using \begin{equation} d_{size} = E_{prime}(J_{datasetinit} + J_{datasetgrowth} \cdot E_{epoch} - J_{mixbytes}, J_{mixbytes}) @@ -2073,11 +2367,11 @@ \subsection{Size of dataset and cache} \begin{equation} E_{prime}(x, y) = \begin{cases} x & \text{if} \quad x / y \in \mathbb{P} \\ -E_{prime}(x - 1 \cdot y, y) & \text{otherwise} +E_{prime}((x - 1) \cdot y, y) & \text{otherwise} \end{cases} \end{equation} \subsection{Dataset generation} -In order the generate the dataset we need the cache $\mathbf{c}$, which is an array of bytes. It depends on the cache size $c_{size}$ and the seed hash $\mathbf{s} \in \mathbb{B}_{32}$. +In order to generate the dataset we need the cache $\mathbf{c}$, which is an array of bytes. It depends on the cache size $c_{size}$ and the seed hash $\mathbf{s} \in \mathbb{B}_{32}$. \subsubsection{Seed hash} The seed hash is different for every epoch. For the first epoch it is the Keccak-256 hash of a series of 32 bytes of zeros. For every other epoch it is always the Keccak-256 hash of the previous seed hash: \begin{equation} @@ -2092,16 +2386,16 @@ \subsubsection{Seed hash} With $\mathbf{0}_{32}$ being 32 bytes of zeros. \subsubsection{Cache} -The cache production process involves using the seed hash to first sequentially filling up $c_{size}$ bytes of memory, then performing $J_{cacherounds}$ passes of the RandMemoHash algorithm created by \cite{lerner2014randmemohash}. The intial cache $\mathbf{c'}$, being an array of arrays of single bytes, will be constructed as follows. +The cache production process involves using the seed hash to first sequentially filling up $c_{size}$ bytes of memory, then performing $J_{cacherounds}$ passes of the RandMemoHash algorithm created by \cite{lerner2014randmemohash}. The initial cache $\mathbf{c'}$, being an array of arrays of single bytes, will be constructed as follows. -We define the array $\mathbf{c}_{i}$, consisting of 64 single bytes, as the $i$th element of the intial cache: +We define the array $\mathbf{c}_{i}$, consisting of 64 single bytes, as the $i$th element of the initial cache: \begin{equation} \mathbf{c}_{i} = \begin{cases} \texttt{KEC512}(\mathbf{s}) & \text{if} \quad i = 0 \quad \\ \texttt{KEC512}(\mathbf{c}_{i-1}) & \text{otherwise} \end{cases} \end{equation} -Therefore $ \mathbf{c'}$ can be defined as +Therefore $ \mathbf{c'}$ can be defined as \begin{equation} \mathbf{c'}[i] = \mathbf{c}_{i} \quad \forall \quad i < n \end{equation} @@ -2128,7 +2422,7 @@ \subsubsection{Cache} \text{with} \quad \mathbf{x'} = \mathbf{x} \quad \text{except} \quad \mathbf{x'}[j] = E_{rmh}(\mathbf{x}, j) \quad \forall \quad j < i \end{multline} -\subsubsection{Full dataset calculation} \label{dataset} +\subsubsection{Full dataset calculation} \label{dataset} Essentially, we combine data from $J_{parents}$ pseudorandomly selected cache nodes, and hash that to compute the dataset. The entire dataset is then generated by a number of items, each $J_{hashbytes}$ bytes in size: \begin{equation} \mathbf{d}[i] = E_{datasetitem}(\mathbf{c}, i) \quad \forall \quad i < \left\lfloor\frac{d_{size}}{J_{hashbytes}}\right\rfloor @@ -2155,7 +2449,7 @@ \subsubsection{Full dataset calculation} \label{dataset} \end{equation} \subsection{Proof-of-work function} -Essentially, we maintain a "mix" $J_{mixbytes}$ bytes wide, and repeatedly sequentially fetch $J_{mixbytes}$ bytes from the full dataset and use the $E_\text{\tiny FNV}$ function to combine it with the mix. $J_{mixbytes}$ bytes of sequential access are used so that each round of the algorithm always fetches a full page from RAM, minimizing translation lookaside buffer misses which ASICs would theoretically be able to avoid. +Essentially, we maintain a ``mix'' $J_{mixbytes}$ bytes wide, and repeatedly sequentially fetch $J_{mixbytes}$ bytes from the full dataset and use the $E_\text{\tiny FNV}$ function to combine it with the mix. $J_{mixbytes}$ bytes of sequential access are used so that each round of the algorithm always fetches a full page from RAM, minimizing translation lookaside buffer misses which ASICs would theoretically be able to avoid. If the output of this algorithm is below the desired target, then the nonce is valid. Note that the extra application of \texttt{KEC} at the end ensures that there exists an intermediate nonce which can be provided to prove that at least a small amount of work was done; this quick outer PoW verification can be used for anti-DDoS purposes. It also serves to provide statistical assurance that the result is an unbiased, 256 bit number. @@ -2206,4 +2500,10 @@ \subsection{Proof-of-work function} \end{cases} \end{equation} +\section{Anomalies on the Main Network} + +\subsection{Deletion of an Account Dispite Out-of-gas} + +At block 2675119, in the transaction \texttt{0xcf416c536ec1a19ed1fb89e4ec7ffb3cf73aa413b3aa9b77d60e4fd81a4296ba}, an account at address 0x03 was called and an out-of-gas occurred during the call. Against the equation (\ref{eq:pre}), this added 0x03 in the set of touched addresses, and this transaction turned $\boldsymbol{\sigma}[0x03]$ into $\varnothing$. + \end{document} diff --git a/README.md b/README.md index fd57a332..302f6977 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,26 @@ -# yellowpaper +# Ethereum Yellow Paper +[![License: CC BY-SA 4.0](https://img.shields.io/badge/License-CC%20BY--SA%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by-sa/4.0/) [![Gitter](https://badges.gitter.im/ethereum/yellowpaper.svg)](https://gitter.im/ethereum/yellowpaper?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -The paper comes as a single ``latex`` file ``Paper.tex``. +The Yellow Paper is a formal definition of the Ethereum protocol, originally by Gavin Wood, currently maintained by Nick Savers and with contributions from many people around the world. -It can be viewed in ``PDF`` format with ``pdflatex Paper.tex`` (local install of a current free tex distribution required). +It is free culture work, licensed under the Creative Commons Attribution Share-Alike (CC-BY-SA) version 4.0. -After creating ``Paper.pdf`` for the first time and every time the bibliography file (``Biblio.bib``) is updated, you will also need to run ``bibtex Paper`` and then ``pdflater Paper`` twice (e.g. ``bibtex Paper && pdflatex Paper && pdflatex Paper``) in order to correctly incorporate all the bibliography references. +## Usage -There are also some online tools like http://latex.informatik.uni-halle.de/latex-online/latex.php you can use for -compiling/preview. +The paper comes as a single ``latex`` file ``Paper.tex``. The latest version is generally available as a PDF at https://ethereum.github.io/yellowpaper/paper.pdf or just [yellowpaper.io](http://yellowpaper.io/). If you find that the borders for links block too much text when viewing the PDF in the browser, you can instead download it and open and view it with a PDF viewer application such as Adobe Acrobat or Evince, where the borders are less likely to display over text. +## How to build + +The paper also comes as a single ``latex`` file ``Paper.tex``, which is built as a PDF as follows. + +``` +git clone https://github.com/ethereum/yellowpaper.git +cd yellowpaper +./build.sh +``` +This will create a PDF version of the Yellow Paper. Following building, you can also use standard `pdflatex` tools like http://latex.informatik.uni-halle.de/latex-online/latex.php for compiling/preview. + +## Other language versions +- [Chinese](https://github.com/yuange1024/ethereum_yellowpaper) translated by YuanGe and GaoTianlu diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..7f8c44cf --- /dev/null +++ b/build.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -e + +if [ -d ".git" ]; then + +SHA=`git rev-parse --short --verify HEAD` +DATE=`git show -s --format="%cd" --date=short HEAD` +REV="$SHA - $DATE" + +else + +REV="unknown revision" + +fi + +echo "\newcommand{\YellowPaperVersionNumber}{$REV}" > Version.tex + +pdflatex -interaction=errorstopmode -halt-on-error Paper.tex && \ +bibtex Paper && \ +pdflatex -interaction=errorstopmode -halt-on-error Paper.tex && \ +pdflatex -interaction=errorstopmode -halt-on-error Paper.tex && \ +pdflatex -interaction=errorstopmode -halt-on-error Paper.tex diff --git a/deploykey.enc b/deploykey.enc new file mode 100644 index 00000000..bc9eb1cb Binary files /dev/null and b/deploykey.enc differ diff --git a/ocgbase.sty b/ocgbase.sty new file mode 100644 index 00000000..80b2a055 --- /dev/null +++ b/ocgbase.sty @@ -0,0 +1,352 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% ocgbase.sty +% +% low-level macros for OCG creation, marking optional content and +% for managment of global (document-wide) OCG related lists; +% +% (automatic) OCG configuration in the PDF catalog +% +% Copyright 2015--\today, Alexander Grahn +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Support package for ocgx2.sty, media9.sty, animate.sty +% +% Supported workflows: +% +% pdflatex, lualatex +% latex-->dvips-->ps2pdf or Distiller +% latex-->dvipdfmx +% xelatex +% +% for `dvipdfmx', set it as document class option +% +% +% Commands defined: +% +% \ocgbase_new_ocg:nnn +% \ocgbase@new@ocg (LaTeX2e version) +% #1: name (as shown in the Layers Tab of the Reader GUI) +% #2: usage dict (may be empty), see PDF reference: +% http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/ +% pdf_reference_1-7.pdf#G9.3858276 +% #3: initial visibility (1|0|true|false|on|off|visible|invisible) +% +% \ocgbase_last_ocg: +% \ocgbase@last@ocg (LaTeX2e version) +% inserts ID of PDF object created during most recent call of +% \ocgbase_new_ocg:nnn +% +% -------- +% +% \ocgbase_tree_node_begin:n +% \ocgbase_tree_node_end: +% \ocgbase@tree@node@begin (LaTeX2e versions) +% \ocgbase@tree@node@end +% #1: OCG PDF object +% macro pair (begin and end) for inserting OCG object and its children +% into Order hierarchy (shown as tree structure in the viewers `Layers' tab +% +% -------- +% +% \ocgbase_add_to_off_list:n +% \ocgbase@add@to@off@list (LaTeX2e version) +% #1: PDF object ID of OCG +% macro for setting initial visibility to `off' +% +% -------- +% +% \ocgbase_del_from_off_list:n +% \ocgbase@del@from@off@list (LaTeX2e version) +% #1: PDF object ID of OCG +% macro for setting initial visibility to `on' +% +% -------- +% +% \ocgbase_add_ocg_to_radiobtn_grp:nnn +% \ocgbase@add@ocg@to@radiobtn@grp +% add OCG #2 (obj ref) to radio button group `#1' (string), +% #3: (0|1|false|true) list OCG as part of group `#1' in the Layers Tab) +% +% -------- +% +% \ocgbase_oc_bdc:n +% \ocgbase@oc@bdc +% #1: OCG obj ref +% mark begin of optional content belonging to OCG #1 in the current +% content stream +% +% \ocgbase_oc_emc: +% \ocgbase@oc@emc +% mark end of optional content in the current content stream +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% This work may be distributed and/or modified under the +% conditions of the LaTeX Project Public License. +% +% The latest version of this license is in +% http://mirrors.ctan.org/macros/latex/base/lppl.txt +% +% This work has the LPPL maintenance status `maintained'. +% +% The Current Maintainer of this work is A. Grahn. + +\RequirePackage{expl3} +\RequirePackage{pdfbase} + +\def\g@ocgbase@date@tl{2017/09/29} +\def\g@ocgbase@version@tl{0.12} + +\ProvidesExplPackage{ocgbase}{\g@ocgbase@date@tl}{\g@ocgbase@version@tl} +{support package for ocgx2.sty} + +\msg_set:nnnn{ocgbase}{support~outdated}{ + Support~package~`#1'~too~old. +}{ + Get~an~up~to~date~version~of~`#1'.\\ + Aborting. +} +\@ifpackagelater{pdfbase}{2017/09/29}{}{ + \msg_error:nnn{ocgbase}{support~outdated}{pdfbase.sty} + \tex_endinput:D +} + +\tl_new:N\g_ocgbase_ocgs_tl %takes ocg object refs +\seq_new:N\g_ocgbase_offocgs_seq + +\pbs_at_end_dvi:n{ + \tl_if_empty:NF\g_ocgbase_ocgs_tl{ + %global OCG array + \pbs_pdfobj:nnn{}{array}{\g_ocgbase_ocgs_tl} + \tl_set:Nx\l_ocgbase_ocgarray_tl{\pbs_pdflastobj:} + \tl_new:N\l_ocgbase_offocgentry_tl + %global OFF list + \seq_if_empty:NF\g_ocgbase_offocgs_seq{ + \pbs_pdfobj:nnn{}{array}{\seq_use:Nn\g_ocgbase_offocgs_seq{~}} + \tl_set:Nx\l_ocgbase_offocgentry_tl{/OFF~\pbs_pdflastobj:} + } + %global Order list + \tl_new:N\l_ocgbase_ocgorderentry_tl + \tl_new:N\l_ocgbase_ocgorder_tl + \tl_if_exist:cT{g_ocgbase_nd_0_chld_tl}{ + \ocgbase_build_order:Nn\l_ocgbase_ocgorder_tl{ + \tl_use:c{g_ocgbase_nd_0_chld_tl} + } + } + \tl_if_empty:NF\l_ocgbase_ocgorder_tl{ + \pbs_pdfobj:nnn{}{array}{\l_ocgbase_ocgorder_tl} + \tl_set:Nx\l_ocgbase_ocgorderentry_tl{/Order~\pbs_pdflastobj:} + } + %generate RBGroups entry (radio button groups) + \tl_new:N\l_ocgbase_rbtn_groups_tl + \seq_map_inline:Nn\g_ocgbase_rbtn_groups_seq{ + \int_compare:nT{\seq_count:c{g_ocgbase_rbtn_group_#1_seq}>\c_one}{ + \tl_put_right:Nx\l_ocgbase_rbtn_groups_tl{ + ~[\seq_use:cn{g_ocgbase_rbtn_group_#1_seq}{~}] + } + } + } + \tl_new:N\l_ocgbase_rbgroupsentry_tl + \tl_if_empty:NF\l_ocgbase_rbtn_groups_tl{ + \pbs_pdfobj:nnn{}{array}{\l_ocgbase_rbtn_groups_tl} + \tl_set:Nx\l_ocgbase_rbgroupsentry_tl{/RBGroups~\pbs_pdflastobj:} + } + \pbs_pdfcatalog:n{ + /OCProperties~<< + /OCGs~\l_ocgbase_ocgarray_tl + /D~<< + /AS~[ + <> + <> + <> + ] + /BaseState/ON~\l_ocgbase_offocgentry_tl + \l_ocgbase_ocgorderentry_tl + \l_ocgbase_rbgroupsentry_tl + /ListMode/VisiblePages + >> + >> + } + } +} + +%macro for inserting new OCG object +\cs_new_nopar:Nn\ocgbase_new_ocg:nnn{ + \pbs_pdfobj:nnn{}{dict}{ + /Type/OCG/Name~(#1)~\str_if_eq_x:nnF{#2}{}{/Usage<<#2>>} + } + \tl_gput_right:Nx\g_ocgbase_ocgs_tl{~\pbs_pdflastobj:} + \bool_if:nT{ + \str_if_eq_x_p:nn{#3}{0} || + \str_if_eq_x_p:nn{#3}{off} || + \str_if_eq_x_p:nn{#3}{false} || + \str_if_eq_x_p:nn{#3}{invisible} + }{ + \ocgbase_add_to_off_list:n{\pbs_pdflastobj:} + } + \tl_gset:Nx\g_ocgbase_last_ocg_tl{\pbs_pdflastobj:} +} + +\cs_new_nopar:Nn\ocgbase_last_ocg:{\g_ocgbase_last_ocg_tl} + +\int_new:N\g_ocgbase_nd_int %node id +\seq_new:N\g_ocgbase_tree_nd_stack_seq %stack with open ocg node id +\seq_new:N\g_ocgbase_tree_ocg_stack_seq %stack with open ocg obj number +\seq_gpush:Nn\g_ocgbase_tree_nd_stack_seq{0} %push root node +\seq_gpush:Nn\g_ocgbase_tree_ocg_stack_seq{null} %push root node + +%macro for starting OCG object (and nested children) insertion into Order +%hierarchy (shown as tree structure in the viewers `Layers' tab +\cs_new:Nn\ocgbase_tree_node_begin:n{ % #1: OCG obj + %get the parent node from stack + \seq_get:NN\g_ocgbase_tree_nd_stack_seq\l__ocgbase_prnt_tl + \tl_if_exist:cTF{g_ocgbase_nd_\l__ocgbase_prnt_tl _chld_tl}{ + %parent has >=1 children (i. e. my older siblings), traverse them + \tl_set:Nv\l__ocgbase_prev_sbl_tl{g_ocgbase_nd_\l__ocgbase_prnt_tl _chld_tl} + \tl_set:Nx\l__ocgbase_cur_ocg_tl{#1} + \ocgbase_traverse_siblings:NN\l__ocgbase_prev_sbl_tl\l__ocgbase_cur_ocg_tl + \str_if_empty:NTF\l__ocgbase_cur_ocg_tl{ + %I am the first child of my parent to refer to OCG #1 + \int_gincr:N\g_ocgbase_nd_int + \tl_set:Nx\l__ocgbase_cur_nd_tl{\int_use:N\g_ocgbase_nd_int} + %set myself as my next-older sibling's `next sibling' + \tl_gset:cV{ + g_ocgbase_nd_\l__ocgbase_prev_sbl_tl _sbl_tl}\l__ocgbase_cur_nd_tl + }{ + %there is already a sibling referring to OCG #1; no new node needs be + %created + \tl_set:NV\l__ocgbase_cur_nd_tl\l__ocgbase_prev_sbl_tl + } + }{ + %I am the very first child of my parent + \int_gincr:N\g_ocgbase_nd_int + \tl_set:Nx\l__ocgbase_cur_nd_tl{\int_use:N\g_ocgbase_nd_int} + %set myself as my parent's first child + \tl_gset:cV{g_ocgbase_nd_\l__ocgbase_prnt_tl _chld_tl}\l__ocgbase_cur_nd_tl + } + %set the OCG I am referring to + \tl_gset:cx{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _ocg_tl}{#1} + %push current node and its associated OCG obj on the stacks + \seq_gpush:NV\g_ocgbase_tree_nd_stack_seq\l__ocgbase_cur_nd_tl + \seq_gpush:Nx\g_ocgbase_tree_ocg_stack_seq{#1} +} + +%macro that ends insertion of OCG and sub-OCGs into Order tree +\cs_new:Nn\ocgbase_tree_node_end:{ + \seq_get:NN\g_ocgbase_tree_nd_stack_seq\l_tempa_tl + \seq_get:NN\g_ocgbase_tree_ocg_stack_seq\l_tempb_tl + \str_if_eq_x:nnT{ + \cs_if_exist_use:c{g_ocgbase_nd_\l_tempa_tl _ocg_tl} + }{ + \l_tempb_tl + }{ + \seq_gpop:NN\g_ocgbase_tree_nd_stack_seq\g_trash_tl + \seq_gpop:NN\g_ocgbase_tree_ocg_stack_seq\g_trash_tl + } +} + +% helper macro; traverses siblings to find either +% the node which refers to the same OCG (arg #2 remains un-modified), or +% the last sibling inserted (arg #2 is cleared); +% the node id of the sibling found is returned in arg #1 +\cs_new:Nn\ocgbase_traverse_siblings:NN{ + % #1: current node (in/out), #2: OCG obj (in/out) + \str_if_eq_x:nnF{#2}{\tl_use:c{g_ocgbase_nd_#1_ocg_tl}}{ + \tl_if_exist:cTF{g_ocgbase_nd_#1_sbl_tl}{ + \tl_set:Nv#1{g_ocgbase_nd_#1_sbl_tl} + \ocgbase_traverse_siblings:NN#1#2 + }{ + \tl_clear:N#2 + } + } +} + +\cs_new:Nn\ocgbase_build_order:Nn{ + % #1: tl var to which the OCG order is written (output) + % #2: starting node id (input; usually `1') + \tl_set:Nx\l__ocgbase_cur_nd_tl{#2} + % first, append the OCG obj the current node is referring to + \tl_put_right:Nx#1{~\tl_use:c{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _ocg_tl}} + % second, traverse the tree starting with the first child node + \tl_if_exist:cT{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _chld_tl}{ + \seq_gpush:NV\g_ocgbase_tree_nd_stack_seq\l__ocgbase_cur_nd_tl + \tl_put_right:Nn#1{~[} + \ocgbase_build_order:Nn#1{ + \tl_use:c{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _chld_tl}} + \tl_put_right:Nn#1{~]} + \seq_gpop:NN\g_ocgbase_tree_nd_stack_seq\l__ocgbase_cur_nd_tl + } + % third, traverse the tree starting with the next sibling node + \tl_if_exist:cT{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _sbl_tl}{ + \ocgbase_build_order:Nn#1{ + \tl_use:c{g_ocgbase_nd_\l__ocgbase_cur_nd_tl _sbl_tl}} + } +} + +%macro for appending an OCG object to the global `OFF' list +%(initial non-visibility) +\cs_new_nopar:Nn\ocgbase_add_to_off_list:n{ + \seq_if_in:NxF\g_ocgbase_offocgs_seq{#1}{ + \seq_gput_right:Nx\g_ocgbase_offocgs_seq{#1} + } +} + +%macro for removing an OCG object from global `OFF' list +%(initial non-visibility) +\cs_new_nopar:Nn\ocgbase_del_from_off_list:n{ + \seq_if_in:NxT\g_ocgbase_offocgs_seq{#1}{ + \ocgbase_seq_gremove_all:Nx\g_ocgbase_offocgs_seq{#1} + } +} +\cs_set_eq:NN\ocgbase_seq_gremove_all:Nn\seq_gremove_all:Nn +\cs_generate_variant:Nn\ocgbase_seq_gremove_all:Nn{Nx} + +\seq_new:N\g_ocgbase_rbtn_groups_seq +\cs_new_nopar:Nn\ocgbase_add_ocg_to_radiobtn_grp:nn{ + % #1: rbtn group name, + % #2: OCG obj ref + \seq_if_exist:cF{g_ocgbase_rbtn_group_#1_seq}{ + \seq_new:c{g_ocgbase_rbtn_group_#1_seq} + \seq_gput_right:Nx\g_ocgbase_rbtn_groups_seq{#1} + } + \seq_if_in:cxF{g_ocgbase_rbtn_group_#1_seq}{#2}{ + \seq_gput_right:cx{g_ocgbase_rbtn_group_#1_seq}{#2} + } +} + +% OC-marked content +\cs_new_nopar:Nn\ocgbase_oc_bdc:n{\pbs_pdfbdc:nn{/OC}{#1}} +\cs_new_nopar:Nn\ocgbase_oc_emc:{\pbs_pdfemc:} + +%stack of PDF obj references of currently open OCGs +\seq_new:N\g_ocgbase_open_stack_seq +%push OCG to stack +\cs_new_nopar:Nn\ocgbase_open_stack_push:n{ + \seq_gpush:Nx\g_ocgbase_open_stack_seq{#1}} +%pop OCG from stack into tl +\cs_new_nopar:Nn\ocgbase_open_stack_pop:N{ + \seq_gpop:NN\g_ocgbase_open_stack_seq#1} + +%command that inserts /OC <> entry; +%for use within annotation/xobject dicts +\cs_new_nopar:Nn\ocgbase_insert_oc:{ + \seq_if_empty:NF\g_ocgbase_open_stack_seq{ + /OC~<> + } +} + +%l2e versions +\cs_gset_eq:NN\ocgbase@new@ocg\ocgbase_new_ocg:nnn +\cs_gset_eq:NN\ocgbase@last@ocg\ocgbase_last_ocg: +\cs_gset_eq:NN\ocgbase@tree@node@begin\ocgbase_tree_node_begin:n +\cs_gset_eq:NN\ocgbase@tree@node@end\ocgbase_tree_node_end: +\cs_gset_eq:NN\ocgbase@add@to@off@list\ocgbase_add_to_off_list:n +\cs_gset_eq:NN\ocgbase@del@from@off@list\ocgbase_del_from_off_list:n +\cs_gset_eq:NN\ocgbase@add@ocg@to@radiobtn@grp\ocgbase_add_ocg_to_radiobtn_grp:nn +\cs_gset_eq:NN\ocgbase@oc@bdc\ocgbase_oc_bdc:n +\cs_gset_eq:NN\ocgbase@oc@emc\ocgbase_oc_emc: +\cs_gset_eq:NN\ocgbase@insert@oc\ocgbase_insert_oc: +\cs_gset_eq:NN\ocgbase@open@stack@pop\ocgbase_open_stack_pop:N +\cs_gset_eq:NN\ocgbase@open@stack@push\ocgbase_open_stack_push:n diff --git a/ocgx2.sty b/ocgx2.sty new file mode 100644 index 00000000..71efe68e --- /dev/null +++ b/ocgx2.sty @@ -0,0 +1,1015 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% ocgx2.sty +% +% Copyright 2015--\today, Alexander Grahn +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% The intent of this package is to be a drop-in replacement for the already +% existing CTAN package `ocgx' by Paul Gaborit, and also for `ocg-p' and `ocg'. +% +% It re-implements the functionality of the ocg, ocgx and ocg-p packages +% and adds support for all known engines and backends including +% latex+dvips+ps2pdf, xelatex, latex+dvipdfmx, lualatex. +% +% With ocgx2, PDF layers may extend across page breaks. +% +% Adds some minor improvements, such as package options, remembering option +% settings of reopened ocgs, correct behaviour of ocg switching links that were +% themselves placed on layers, compatibility with the animate and media9 +% packages. +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% This work may be distributed and/or modified under the +% conditions of the LaTeX Project Public License. +% +% The latest version of this license is in +% http://mirrors.ctan.org/macros/latex/base/lppl.txt +% +% This work has the LPPL maintenance status `maintained'. +% +% The Current Maintainer of this work is A. Grahn. + +\RequirePackage{xparse} +\RequirePackage{l3keys2e} + +\def\g@ocgxii@date@tl{2017/09/29} +\def\g@ocgxii@version@tl{0.32} + +\ProvidesExplPackage{ocgx2}{\g@ocgxii@date@tl}{\g@ocgxii@version@tl} +{ports `ocgx' functionality to dvips+ps2pdf, xelatex and dvipdfmx} + +%creating global definitions +\cs_new:Npn\ocgxii@newkey#1#2{\tl_gset:cx{#1}{#2}} + +\AtBeginDocument{ + \iow_now:Nx\@mainaux{ + \token_to_str:N\providecommand\token_to_str:N\ocgxii@newkey[2]{} + } + \iow_now:Nx\@mainaux{ + \token_to_str:N\providecommand\token_to_str:N\ocgxii@ocg@stack@on@page[2]{} + } + \iow_now:Nx\@mainaux{ + \token_to_str:N\providecommand + \token_to_str:N\ocgxii@lnkcol@stack@on@page[2]{} + } +} + +\msg_set:nnn{ocgx2}{missing~package}{ + Package~`#1'~must~be~loaded~before~ocgx2.\\\\ + Put\\\\ + \space\space\string\usepackage#2{#1}\\ + \space\space\string\usepackage[ocgcolorlinks]{ocgx2}\\\\ + to~the~preamble~of~your~document. +} + +\msg_set:nnn{ocgx2}{beamer~and~ocgcolorlinks}{ + Option~`ocgcolorlinks'~cannot~be~used~with~class~Beamer.\\\\ + Using~`colorlinks'~instead. +} + +%package opts +%unknown package option error message +\msg_set:nnnn{ocgx2}{unknown~package~option}{Unknown~package~option~`#1'.}{ + Package option~'#1'~is~unknown;\\ + perhaps~it~is~spelled~incorrectly. +} + +\bool_new:N\g_ocgxii_dvipdfmx_bool +\bool_new:N\l_ocgxii_tikz_bool +\bool_new:N\l_ocgxii_ocgcolorlinks_bool +\bool_new:N\g_ocgxii_showingui_bool +\bool_new:N\l_ocgxii_showingui_bool + +\keys_define:nn{ocgx2}{ + xetex .code:n = { + \PassOptionsToPackage{xetex}{pdfbase} + }, + + dvipdfmx .code:n = { + \PassOptionsToPackage{dvipdfmx}{pdfbase} + \bool_gset_true:N\g_ocgxii_dvipdfmx_bool + }, + + viewocg .choice:, + viewocg / always .code:n={ + \tl_gset:Nn\g_ocgxii_view_tl{/View<>}}, + viewocg / never .code:n={ + \tl_gset:Nn\g_ocgxii_view_tl{/View<>}}, + viewocg / ifvisible .code:n={\tl_gclear_new:N\g_ocgxii_view_tl}, + viewocg .default:n={ifvisible}, + + printocg .choice:, + printocg / always .code:n={ + \tl_gset:Nn\g_ocgxii_print_tl{/Print<>}}, + printocg / never .code:n={ + \tl_gset:Nn\g_ocgxii_print_tl{/Print<>}}, + printocg / ifvisible .code:n={\tl_gclear_new:N\g_ocgxii_print_tl}, + printocg .default:n={ifvisible}, + + exportocg .choice:, + exportocg / always .code:n={ + \tl_gset:Nn\g_ocgxii_export_tl{/Export<>}}, + exportocg / never .code:n={ + \tl_gset:Nn\g_ocgxii_export_tl{/Export<>}}, + exportocg / ifvisible .code:n={\tl_gclear_new:N\g_ocgxii_export_tl}, + exportocg .default:n={ifvisible}, + + showingui .choices:nn = {true,false,always,never,iffirstuse}{ + \bool_if:nTF{ + \str_if_eq_x_p:nn{#1}{false} || + \str_if_eq_x_p:nn{#1}{never} + }{ + \bool_gset_false:N\g_ocgxii_showingui_bool + }{ + \bool_gset_true:N\g_ocgxii_showingui_bool + } + }, + showingui .default:n={true}, + + listintoolbar .meta:n = {showingui=#1}, + listintoolbar .default:n={true}, + + tikz .bool_set:N = \l_ocgxii_tikz_bool, + tikz .default:n = true, + + ocgcolorlinks .bool_set:N = \l_ocgxii_ocgcolorlinks_bool, + ocgcolorlinks .default:n = true, + + unknown .code:n = { + \msg_error:nnx{ocgx2}{unknown~package~option}{\l_keys_key_tl} + } +} + +%package options preset +\keys_set:nn{ocgx2}{viewocg,printocg,exportocg,showingui,tikz=false} + +%process package options +\ProcessKeysOptions{ocgx2} +\sys_if_engine_xetex:T{ + \bool_gset_true:N\g_ocgxii_dvipdfmx_bool + %we use the period `.' from this downscaled font at the end of ocgcolorlinks, + %preventing empty links from flooding the page with link color + \font\g_ocgxii_lmroman_tl="[lmroman5-regular.otf]"~scaled~1 +} + +\RequirePackage{ocgbase} %also loads pdfbase.sty + +%re-implement ocg-p's `ocg' environment +\DeclareDocumentEnvironment{ocg}{O{}mmm}{ + \ocgxii_beginocg:nnnn{#1}{#2}{#3}{#4} +}{ + \ocgxii_endocg: +} + +\cs_new_nopar:Nn\ocgxii_beginocg:nnnn{ + \group_begin: + \ocgxii_reset_cmd_opts: % ... to the user-set package options + \tl_if_exist:cTF{ocgxii@#3}{ %re-open existing layer + \tl_set:Nx\l_tempa_tl{\tl_use:c{ocgxii@#3.opts},#1} + \tl_gset:cx{ocgxii@#3.opts}{\l_tempa_tl} %new options appended + \keys_set:nV{ocgx2/user}\l_tempa_tl + \bool_if:nTF{ %initial visibility + \str_if_eq_x_p:nn{#4}{1} || + \str_if_eq_x_p:nn{#4}{on} || + \str_if_eq_x_p:nn{#4}{true} + }{ + \ocgbase_del_from_off_list:n{\tl_use:c{ocgxii@#3}} + }{ + \ocgbase_add_to_off_list:n{\tl_use:c{ocgxii@#3}} + } + }{ + \tl_set:Nx\l_tempa_tl{#1} + \tl_gset:cx{ocgxii@#3.opts}{\l_tempa_tl} + \keys_set:nV{ocgx2/user}\l_tempa_tl + \ocgbase_new_ocg:nnn{#2}{ + \l_ocgxii_view_tl\l_ocgxii_print_tl\l_ocgxii_export_tl + }{#4} + \tl_gset:cx{ocgxii@#3}{\ocgbase_last_ocg:} + \iow_now:Nx\@mainaux{ + \token_to_str:N\ocgxii@newkey{ocgx2.ocg.#3}{\ocgbase_last_ocg:} + } + } + \bool_if:nT{ + !\cs_if_exist:cTF{ocgx2.ocg.#3}{ + \str_if_eq_x_p:nn{\tl_use:c{ocgx2.ocg.#3}}{\tl_use:c{ocgxii@#3}} + }{ + \c_false_bool + } + }{ + \tl_if_exist:NF\g_ocgxii_rerunwarned_tl{ + \tl_new:N\g_ocgxii_rerunwarned_tl + \AtEndDocument{\msg_warning:nn{ocgx2}{rerun}} + } + } + \tl_gset:cx{ocgx2.ocg.#3}{\tl_use:c{ocgxii@#3}} + \seq_map_inline:Nn\l_ocgxii_rbgrps_seq{% process list of radio btn groups + \ocgbase_add_ocg_to_radiobtn_grp:nn{##1}{\tl_use:c{ocgxii@#3}} + } + \ocgbase_open_stack_push:n{\tl_use:c{ocgxii@#3}} + \ocgxii_stack_shipout:NN\ocgxii@ocg@stack@on@page\g_ocgbase_open_stack_seq + % insert OCG into Order tree + \bool_if:NT\l_ocgxii_showingui_bool{ + \ocgbase_tree_node_begin:n{\tl_use:c{ocgxii@#3}} + } + \group_end: + \ocgbase_oc_bdc:n{\tl_use:c{ocgxii@#3}} + \ignorespaces +} + +\cs_new_nopar:Nn\ocgxii_endocg:{ + \unskip + \ocgbase_oc_emc: + \ocgbase_tree_node_end: + \ocgbase_open_stack_pop:N\l_trash_tl + \ocgxii_stack_shipout:NN\ocgxii@ocg@stack@on@page\g_ocgbase_open_stack_seq +} + +\cs_new_nopar:Nn\ocgxii_stack_shipout:NN{ + \iow_shipout_x:Nx\@mainaux{ + \token_to_str:N#1{ + \exp_not:N\int_use:N\g_ocgxii_page_int + }{\seq_use:Nn#2{,}} + } +} + +\cs_new_nopar:Npn\ocgxii@ocg@stack@on@page#1#2{ + \seq_gset_from_clist:cn{g_pending_ocgs_on_#1_seq}{#2} + %re-add braces around items for dvips + \bool_if:nT{\sys_if_output_dvi_p: && !\g_ocgxii_dvipdfmx_bool}{ + \seq_map_inline:cn{g_pending_ocgs_on_#1_seq}{ + \seq_gpop_left:cN{g_pending_ocgs_on_#1_seq}\l_trash_tl + \seq_gput_right:cn{g_pending_ocgs_on_#1_seq}{{##1}} + } + } +} +\ocgxii@ocg@stack@on@page{0}{} %initialize + +\cs_new_nopar:Npn\ocgxii@lnkcol@stack@on@page#1#2{ + \seq_gset_from_clist:cn{g_pending_lnkcols_on_#1_seq}{#2} + %re-add braces around items + \seq_map_inline:cn{g_pending_lnkcols_on_#1_seq}{ + \seq_gpop_left:cN{g_pending_lnkcols_on_#1_seq}\l_trash_tl + \seq_gput_right:cn{g_pending_lnkcols_on_#1_seq}{{##1}} + } +} +\ocgxii@lnkcol@stack@on@page{0}{} %initialize + +%end-of-page action +\pbs_eop_action:n{ + \seq_if_exist:cT{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}{ + %check whether end-of-page link colour stack has settled + \iow_shipout:Nx\@mainaux{ + \token_to_str:N\ocgxii@newkey{ocgx2.oldlnkcol.\int_use:N\g_ocgxii_page_int}{ + \seq_use:cn{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}{,} + } + } + \bool_if:nT{ + !\cs_if_exist:cTF{ocgx2.oldlnkcol.\int_use:N\g_ocgxii_page_int}{ + \str_if_eq_x_p:nn{ + \tl_use:c{ocgx2.oldlnkcol.\int_use:N\g_ocgxii_page_int} + }{ + \seq_use:cn{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}{,} + } + }{ + \c_false_bool + } + }{ + \tl_if_exist:NF\g_ocgxii_rerunwarned_tl{ + \tl_new:N\g_ocgxii_rerunwarned_tl + \AtEndDocument{\msg_warning:nn{ocgx2}{rerun}} + } + } + % now close the colourlink opened last + \seq_get:cNT{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}\l_tmpa_tl{ + \tl_gset:Nx\g_ocgxii_lnkcol_tl{{\l_tmpa_tl}} + \ocgxii_colourlink_end: + } + } + %check whether end-of-page ocg stack has settled + \iow_shipout:Nx\@mainaux{ + \token_to_str:N\ocgxii@newkey{ocgx2.oldstack.\int_use:N\g_ocgxii_page_int}{ + \seq_use:cn{g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq}{,} + } + } + \bool_if:nT{ + !\cs_if_exist:cTF{ocgx2.oldstack.\int_use:N\g_ocgxii_page_int}{ + \str_if_eq_x_p:nn{ + \tl_use:c{ocgx2.oldstack.\int_use:N\g_ocgxii_page_int} + }{ + \seq_use:cn{g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq}{,} + } + }{ + \c_false_bool + } + }{ + \tl_if_exist:NF\g_ocgxii_rerunwarned_tl{ + \tl_new:N\g_ocgxii_rerunwarned_tl + \AtEndDocument{\msg_warning:nn{ocgx2}{rerun}} + } + } + %now close pending ocgs + \seq_map_variable:cNn{ + g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq + }\l_tmpb_tl{\ocgbase_oc_emc:} +} + +%begin-of-page action +\pbs_bop_action:n{ + % re-open all pending ocgs in original order + \seq_set_eq:Nc\l_ocgxii_pending_ocgs_seq{ + g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq + } + \seq_reverse:N\l_ocgxii_pending_ocgs_seq + \seq_map_variable:NNn\l_ocgxii_pending_ocgs_seq\l_tmpa_tl{ + \ocgbase_oc_bdc:n{\l_tmpa_tl} + } + % re-open the colourlink opened last + \seq_get:cNT{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}\l_tmpa_tl{ + \tl_gset:Nx\g_ocgxii_lnkcol_tl{{\l_tmpa_tl}} + \ocgxii_colourlink_begin: + } + \int_gincr:N\g_ocgxii_page_int + % copy pending ocg stack from previous page, if it has not been initialized + % yet from aux file + \seq_if_exist:cF{g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq}{ + \seq_gset_eq:cc{ + g_pending_ocgs_on_\int_use:c{g_ocgxii_page_int}_seq + }{ + g_pending_ocgs_on_\int_eval:n{\g_ocgxii_page_int-\c_one}_seq + } + } + %the same for link colour stack + \seq_if_exist:cF{g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq}{ + \seq_gset_eq:cc{ + g_pending_lnkcols_on_\int_use:c{g_ocgxii_page_int}_seq + }{ + g_pending_lnkcols_on_\int_eval:n{\g_ocgxii_page_int-\c_one}_seq + } + } +} +\int_new:N\g_ocgxii_page_int %abs. page counter + +\cs_new_nopar:Nn\ocgxii_ocglist_reset:{ + \tl_clear_new:N\l_ocgxii_u_list_tl + \tl_clear_new:N\l_ocgxii_d_list_tl + \tl_clear_new:N\l_ocgxii_e_list_tl + \tl_clear_new:N\l_ocgxii_x_list_tl +} + +\cs_new_nopar:Nn\ocgxii_ocglist_build:Nn{ + \tl_set:Nx\l_ocglistarg_tl{#2}\tl_trim_spaces:N\l_ocglistarg_tl + \seq_set_split:NnV\l_ocgxii_ocglistarg_seq{~}\l_ocglistarg_tl + \seq_map_variable:NNn\l_ocgxii_ocglistarg_seq\l_tempa_tl{ + \ocgxii_process_ocgref:NN#1\l_tempa_tl + } +} + +\cs_new:Nn\ocgxii_commalist_process:n{ + \seq_set_split:Nnn\l_tmpa_seq{,}{#1} + \ocgxii_ocglist_build:Nn\l_ocgxii_e_list_tl{\seq_item:Nn\l_tmpa_seq{1}} + \ocgxii_ocglist_build:Nn\l_ocgxii_x_list_tl{\seq_item:Nn\l_tmpa_seq{2}} + \ocgxii_ocglist_build:Nn\l_ocgxii_d_list_tl{\seq_item:Nn\l_tmpa_seq{3}} + \ocgxii_ocglist_build:Nn\l_ocgxii_u_list_tl{\seq_item:Nn\l_tmpa_seq{4}} +} + +\cs_new_nopar:Nn\ocgxii_ocglist_process_idlist:nn{ + \ocgxii_ocglist_reset: + \tl_set:Nx\l_ocgxii_opt_tl{#1}\tl_remove_all:Nn\l_ocgxii_opt_tl{~} + \str_case_x:nnF{\l_ocgxii_opt_tl}{ + {triggerocg=onmouseup}{ + \ocgxii_ocglist_build:Nn\l_ocgxii_u_list_tl{#2} + } + {triggerocg=onmousedown}{ + \ocgxii_ocglist_build:Nn\l_ocgxii_d_list_tl{#2} + } + {triggerocg=onmouseenter}{ + \ocgxii_ocglist_build:Nn\l_ocgxii_e_list_tl{#2} + } + {triggerocg=onmouseexit}{ + \ocgxii_ocglist_build:Nn\l_ocgxii_x_list_tl{#2} + } + {triggerocg=allactions}{ + \ocgxii_commalist_process:n{#2} + } + }{ + \msg_error:nnx{ocgx2}{unknown~option}{\l_ocgxii_opt_tl} + } +} + +\int_new:N\g_ocgxii_widcount_int% widget counter + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% re-implement commands from ocgx.sty (all engines including ps2pdf [gs>=9.15]) +% adding optional `*` (arg 1) -> non-breakable link instead of plain (multiline) +% Link; +% adding optional 2nd argument -> Button Widget (non-breakable) with one of +% various mouse triggers (`troggerocgs` option from ocg-p) +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\DeclareDocumentCommand\switchocg{s o m +m}{ + \ocgxii_actionsocg:nnnnnn{#1}{#2}{#3}{}{}{#4} +} + +\DeclareDocumentCommand\showocg{s o m +m}{ + \ocgxii_actionsocg:nnnnnn{#1}{#2}{}{#3}{}{#4} +} + +\DeclareDocumentCommand\hideocg{s o m +m}{ + \ocgxii_actionsocg:nnnnnn{#1}{#2}{}{}{#3}{#4} +} + +\DeclareDocumentCommand\actionsocg{s o m m m +m}{ + \ocgxii_actionsocg:nnnnnn{#1}{#2}{#3}{#4}{#5}{#6} +} + +\bool_new:N\l_ocgxii_mouse_triggers_bool +\bool_new:N\l_ocgxii_nobreak_bool +\cs_new:Nn\ocgxii_actionsocg:nnnnnn{ + \leavevmode + \bool_set_false:N\l_ocgxii_mouse_triggers_bool + \bool_set_false:N\l_ocgxii_nobreak_bool + % explicitly non-breakable? + \bool_if:nT{#1}{\bool_set_true:N\l_ocgxii_nobreak_bool} + % + %clear actions for various mouse triggers (e,d,x) + \tl_clear:N\l_ocgxii_toswitch_e_tl + \tl_clear:N\l_ocgxii_toswitch_x_tl + \tl_clear:N\l_ocgxii_toswitch_d_tl + \tl_clear:N\l_ocgxii_toshow_e_tl + \tl_clear:N\l_ocgxii_toshow_x_tl + \tl_clear:N\l_ocgxii_toshow_d_tl + \tl_clear:N\l_ocgxii_tohide_e_tl + \tl_clear:N\l_ocgxii_tohide_x_tl + \tl_clear:N\l_ocgxii_tohide_d_tl + % + %process *all* mouse triggers (e,d,u,x) + \ocgxii_ocglist_process_idlist:nn{ + \IfValueTF{#2}{#2}{triggerocg=onmouseup} + }{#3} + \tl_set_eq:NN\l_ocgxii_toswitch_e_tl\l_ocgxii_e_list_tl + \tl_set_eq:NN\l_ocgxii_toswitch_x_tl\l_ocgxii_x_list_tl + \tl_set_eq:NN\l_ocgxii_toswitch_d_tl\l_ocgxii_d_list_tl + \tl_set_eq:NN\l_ocgxii_toswitch_u_tl\l_ocgxii_u_list_tl + \ocgxii_ocglist_process_idlist:nn{ + \IfValueTF{#2}{#2}{triggerocg=onmouseup} + }{#4} + \tl_set_eq:NN\l_ocgxii_toshow_e_tl\l_ocgxii_e_list_tl + \tl_set_eq:NN\l_ocgxii_toshow_x_tl\l_ocgxii_x_list_tl + \tl_set_eq:NN\l_ocgxii_toshow_d_tl\l_ocgxii_d_list_tl + \tl_set_eq:NN\l_ocgxii_toshow_u_tl\l_ocgxii_u_list_tl + \ocgxii_ocglist_process_idlist:nn{ + \IfValueTF{#2}{#2}{triggerocg=onmouseup} + }{#5} + \tl_set_eq:NN\l_ocgxii_tohide_e_tl\l_ocgxii_e_list_tl + \tl_set_eq:NN\l_ocgxii_tohide_x_tl\l_ocgxii_x_list_tl + \tl_set_eq:NN\l_ocgxii_tohide_d_tl\l_ocgxii_d_list_tl + \tl_set_eq:NN\l_ocgxii_tohide_u_tl\l_ocgxii_u_list_tl + %any triggers apart from mouse-up? + \str_if_eq_x:nnF{ + \l_ocgxii_toswitch_e_tl\l_ocgxii_toswitch_x_tl\l_ocgxii_toswitch_d_tl + \l_ocgxii_toshow_e_tl\l_ocgxii_toshow_x_tl\l_ocgxii_toshow_d_tl + \l_ocgxii_tohide_e_tl\l_ocgxii_tohide_x_tl\l_ocgxii_tohide_d_tl + }{}{ + \bool_set_true:N\l_ocgxii_mouse_triggers_bool + } + % + \bool_if:nTF{\l_ocgxii_nobreak_bool || \l_ocgxii_mouse_triggers_bool}{ + \hbox_set:Nn\l_tmpa_box{#6} + \pbs_pdfannot:nnnn{ + \dim_use:N\box_wd:N\l_tmpa_box}{ + \dim_use:N\box_ht:N\l_tmpa_box}{ + \dim_use:N\box_dp:N\l_tmpa_box + }{ + \bool_if:NTF\l_ocgxii_mouse_triggers_bool{ + % e,d,x mouse triggers require (non-breakable) /Widget annot with AA + % (additional actions) dict + /Subtype/Widget/Ff~65536/FT/Btn/BS<> + /T~(ocgx2@\int_use:N\g_ocgxii_widcount_int) + /AA << + \str_if_eq_x:nnF{}{ + \l_ocgxii_toswitch_u_tl\l_ocgxii_toshow_u_tl\l_ocgxii_tohide_u_tl + }{ + /U <> + } + \str_if_eq_x:nnF{}{ + \l_ocgxii_toswitch_d_tl\l_ocgxii_toshow_d_tl\l_ocgxii_tohide_d_tl + }{ + /D <> + } + \str_if_eq_x:nnF{}{ + \l_ocgxii_toswitch_e_tl\l_ocgxii_toshow_e_tl\l_ocgxii_tohide_e_tl + }{ + /E <> + } + \str_if_eq_x:nnF{}{ + \l_ocgxii_toswitch_x_tl\l_ocgxii_toshow_x_tl\l_ocgxii_tohide_x_tl + }{ + /X <> + } + >> + }{ + %mouse-up only only needs annot with /Link subtype + /Subtype/Link + /A <> + /Border~[0~0~0] + } + \cs_if_exist:NT\@pdfhighlight{ + \ifx\@pdfhighlight\@empty\else/H\@pdfhighlight\fi + } + }\box_use_clear:N\l_tmpa_box + \bool_if:NT\l_ocgxii_mouse_triggers_bool{ + \pbs_appendtofields:n{\pbs_pdflastann:} + \int_gincr:N\g_ocgxii_widcount_int + } + }{ + %line-breakable annotation + \pbs_pdflink:nn{ + /Subtype/Link + /A <> + %look and feel of hyperref links, if hyperref has been loaded + \cs_if_exist:NTF\Hy@setpdfborder{ + \Hy@setpdfborder\g_ocgxii_patch_tl + \ifx\@pdfhighlight\@empty\else/H\@pdfhighlight\fi + \ifx\@linkbordercolor\relax\else/C[\@linkbordercolor]\fi + \ifHy@pdfa /F~4\fi + }{ + /Border~[0~0~0] + } + }{ + \cs_if_exist:NTF\Hy@colorlink{ + \Hy@colorlink\@linkcolor#6\Hy@endcolorlink\Hy@VerboseLinkStop + }{#6} + } + } +} + +%mimic commands from ocg-p +\cs_new:Npn\toggleocgs{\switchocg*} +\cs_new:Npn\showocgs{\showocg*} +\cs_new:Npn\hideocgs{\hideocg*} +\cs_new:Npn\setocgs{\actionsocg*} +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\cs_new:Nn\ocgxii_process_ocgref:NN{ + \str_if_eq_x:nnF{#2}{}{ + \tl_if_exist:cTF{ocgx2.ocg.#2}{ + \tl_put_right:Nx#1{~\tl_use:c{ocgx2.ocg.#2}} + }{ + \msg_warning:nnx{ocgx2}{undefined~OCG}{#2} + \tl_if_exist:NF\g_ocgxii_refundefwarned_tl{ + \tl_new:N\g_ocgxii_refundefwarned_tl + \AtEndDocument{\msg_warning:nn{ocgx2}{undefined~OCGs}} + } + } + } +} + +%command opts +\keys_define:nn{ocgx2/user}{ + viewocg .choice:, + viewocg / always .code:n={ + \tl_set:Nn\l_ocgxii_view_tl{/View<>}}, + viewocg / never .code:n={ + \tl_set:Nn\l_ocgxii_view_tl{/View<>}}, + viewocg / ifvisible .code:n={ + \tl_clear:N\l_ocgxii_view_tl}, + viewocg .default:n={ifvisible}, + + printocg .choice:, + printocg / always .code:n={ + \tl_set:Nn\l_ocgxii_print_tl{/Print<>}}, + printocg / never .code:n={ + \tl_set:Nn\l_ocgxii_print_tl{/Print<>}}, + printocg / ifvisible .code:n={ + \tl_clear:N\l_ocgxii_print_tl}, + printocg .default:n={ifvisible}, + + exportocg .choice:, + exportocg / always .code:n={ + \tl_set:Nn\l_ocgxii_export_tl{/Export<>}}, + exportocg / never .code:n={ + \tl_set:Nn\l_ocgxii_export_tl{/Export<>}}, + exportocg / ifvisible .code:n={\tl_clear:N\l_ocgxii_export_tl}, + exportocg .default:n={ifvisible}, + + showingui .choices:nn = {true,false,always,never,iffirstuse}{ + \bool_if:nTF{ + \str_if_eq_x_p:nn{#1}{false} || + \str_if_eq_x_p:nn{#1}{never} + }{ + \bool_set_false:N\l_ocgxii_showingui_bool + }{ + \bool_set_true:N\l_ocgxii_showingui_bool + } + }, + showingui .default:n={true}, + + listintoolbar .meta:n = {showingui=#1}, + listintoolbar .default:n={true}, + + radiobtngrp .code:n = { + \seq_if_in:NxF\l_ocgxii_rbgrps_seq{#1}{ + \seq_put_right:Nx\l_ocgxii_rbgrps_seq{#1} + } + }, + radiobtngrp .value_required:n = {true} +} + +\cs_new:Nn\ocgxii_reset_cmd_opts:{ + \tl_set_eq:NN\l_ocgxii_view_tl\g_ocgxii_view_tl + \tl_set_eq:NN\l_ocgxii_print_tl\g_ocgxii_print_tl + \tl_set_eq:NN\l_ocgxii_export_tl\g_ocgxii_export_tl + \bool_set_eq:NN\l_ocgxii_showingui_bool\g_ocgxii_showingui_bool + %stack of radio button group names the current ocg belongs to + \seq_clear_new:N\l_ocgxii_rbgrps_seq +} + +\msg_set:nnn{ocgx2}{rerun}{Rerun~to~get~OCG~references~right!} +\msg_set:nnn{ocgx2}{undefined~OCG}{ + Line~\msg_line_number: :~OCG~`#1'~is~not~defined. +} +\msg_set:nnn{ocgx2}{undefined~OCGs}{There~were~undefined~OCGs!} +\msg_set:nnn{ocgx2}{unknown~option}{ + Line~\msg_line_number: :~unknown~option~`#1'. +} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% patch hyperref to ensure compatibility with our `ocgcolorlinks' option +% +% Plus: +% +% * add `ocgcolorlinks' support to all drivers +% +% * allows for `ocgcolorlinks' extending over +% +% line-breaks AND page-breaks +% +% with pdftex, luatex, xetex, dvipdfmx drivers +% +% based on Ben Lerner's idea +% http://tex.stackexchange.com/a/104227; +% with some improvements +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\cs_new_nopar:Nn\ocgxii_colourlink_begin:{} +\cs_new_nopar:Nn\ocgxii_colourlink_end:{} +\cs_new_nopar:Nn\ocgxii_colourlink_nobreak_begin:{ + \hbox_set:Nw\l_tmpa_box\color@begingroup + \tl_set_eq:NN\color@setgroup\group_begin: +} +\cs_new_nopar:Nn\ocgxii_colourlink_nobreak_end:{ + \color@endgroup\hbox_set_end: + \mbox{ + \ocgbase_oc_bdc:n{\ocgxii@OCPrint} + \hbox_to_zero:n{\box_use:N\l_tmpa_box\hss} + \ocgbase_oc_emc: + \ocgbase_oc_bdc:n{\ocgxii@OCView} + \group_begin: + \exp_after:wN\HyColor@UseColor\l_ocgxii_lnkcol_tl + \box_use_clear:N\l_tmpa_box + \group_end: + \ocgbase_oc_emc: + } +} + +\seq_new:N\g_ocgxii_lnk_color_seq %stack of colours of currently open links +\tl_new:N\g_ocgxii_patch_tl % `BorderArrayPatch' for dvips + +\bool_if:nTF{\sys_if_output_dvi_p: && !\g_ocgxii_dvipdfmx_bool}{ + % non-breakable links in dvips + \cs_new_nopar:Nn\ocgxii_enable_ocglinks:{ + \def\Hy@colorlink##1{ + \group_begin: + \tl_set:Nn\l_ocgxii_lnkcol_tl{##1} + \ocgxii_colourlink_nobreak_begin: + } + \def\Hy@endcolorlink{ + \ocgxii_colourlink_nobreak_end: + \group_end: + } + } + \tl_gset:Nn\g_ocgxii_patch_tl{BorderArrayPatch} +}{ + % pdftex,luatex,xetex,dvipdfmx: + % ocgcolorlinks that extend over line and page breaks + \cs_new_nopar:Nn\ocgxii_enable_ocglinks:{ + \def\Hy@colorlink##1{ + \ifx\Hy@setbreaklinks\@gobble\else + \Hy@breaklinkstrue + \fi + \ifHy@breaklinks + \seq_get_left:NNT\g_ocgxii_lnk_color_seq\l_tmpa_tl{ + \tl_gset_eq:NN\g_ocgxii_lnkcol_tl\l_tmpa_tl + \ocgxii_colourlink_end: + } + \group_begin: + \ocgxii_colourlink_begin: + \seq_gpush:Nx\g_ocgxii_lnk_color_seq{{##1}} + \ocgxii_stack_shipout:NN\ocgxii@lnkcol@stack@on@page\g_ocgxii_lnk_color_seq + \else + \group_begin: + \tl_set:Nn\l_ocgxii_lnkcol_tl{##1} + \ocgxii_colourlink_nobreak_begin: + \fi + } + \def\Hy@endcolorlink{ + \ifHy@breaklinks + \seq_gpop:NN\g_ocgxii_lnk_color_seq\l_tmpa_tl + \tl_gset_eq:NN\g_ocgxii_lnkcol_tl\l_tmpa_tl + \ocgxii_stack_shipout:NN\ocgxii@lnkcol@stack@on@page\g_ocgxii_lnk_color_seq + \ocgxii_colourlink_end: + \group_end: + \seq_get_left:NNT\g_ocgxii_lnk_color_seq\l_tmpa_tl{ + \tl_gset_eq:NN\g_ocgxii_lnkcol_tl\l_tmpa_tl + \ocgxii_colourlink_begin: + } + \else + \ocgxii_colourlink_nobreak_end: + \group_end: + \fi + } + } + \bool_if:NT\l_ocgxii_ocgcolorlinks_bool{ + \cs_gset_nopar:Nn\ocgxii_colourlink_begin:{ + \pbs_literal:nn{page}{q~7~Tr} + } + \cs_gset_nopar:Nn\ocgxii_colourlink_end:{ + %this should keep empty link annots from flooding the page with link colour + \sys_if_output_pdf:TF{ + % with pdftex/luatex we provide a default glyph (`.') which we place + % beyond the page limits + \vbox_to_zero:n{ + \vss + \hbox_overlap_right:n{\skip_horizontal:n{2\paperwidth}.} + \skip_vertical:n{2\paperheight} + } + }{ + %with XeLaTeX we place an invisibly downscaled `.' from the LM font + %directly after the link text + \sys_if_engine_xetex:T{\hbox_overlap_left:n{\g_ocgxii_lmroman_tl .}} + } + \ocgbase_oc_bdc:n{\ocgxii@OCPrint} + \pbs_literal:nn{page}{-88888~-88888~99999~99999~re~f} + \ocgbase_oc_emc: + \ocgbase_oc_bdc:n{\ocgxii@OCView} + \group_begin: + \exp_after:wN\HyColor@UseColor\g_ocgxii_lnkcol_tl + \pbs_literal:nn{page}{-88888~-88888~99999~99999~re~f} + \group_end: + \ocgbase_oc_emc: + \pbs_literal:nn{page}{0~Tr~Q} + } + } +} + +% user command for protecting graphical content (external file, inline +% [e. g. TikZ], \fbox{...}) inside breakable ocgcolorlink +\bool_if:NTF\l_ocgxii_ocgcolorlinks_bool{ + \DeclareDocumentCommand\ocglinkprotect{m}{ + \seq_get_left:NNT\g_ocgxii_lnk_color_seq\l_tmpa_tl{ + \tl_gset_eq:NN\g_ocgxii_lnkcol_tl\l_tmpa_tl + \ocgxii_colourlink_end: + \group_begin: + \tl_set_eq:NN\l_ocgxii_lnkcol_tl\l_tmpa_tl + \ocgxii_colourlink_nobreak_begin: + } + \sys_if_output_pdf:TF{ + \hbox_set:Nn\l_tmpb_box{#1} + \hbox_to_wd:nn{\box_wd:N\l_tmpb_box}{ + \vrule~width~\c_zero_dim~height~\box_ht:N\l_tmpb_box~ + depth~\box_dp:N\l_tmpb_box + \pbs_pdfxform:nnnnn{1}{0}{}{}{\l_tmpb_box} + \pbs_pdfrefxform:n{\pbs_pdflastxform:} + } + }{#1} + \seq_get_left:NNT\g_ocgxii_lnk_color_seq\l_tmpa_tl{ + \ocgxii_colourlink_nobreak_end: + \group_end: + \ocgxii_colourlink_begin: + } + } +}{ + \DeclareDocumentCommand\ocglinkprotect{m}{#1} +} + +% option ocgcolorlinks and beamer are not compatible +\bool_if:NT\l_ocgxii_ocgcolorlinks_bool{ + \@ifclassloaded{beamer}{ + \bool_set_false:N\l_ocgxii_ocgcolorlinks_bool + \hypersetup{colorlinks} + \msg_warning:nn{ocgx2}{beamer~and~ocgcolorlinks} + }{} +} + +\bool_if:NT\l_ocgxii_ocgcolorlinks_bool{ + \@ifpackageloaded{hyperref}{ + \Hy@colorlinkstrue + \AtBeginDocument{ + \ocgbase_new_ocg:nnn{OCView}{/Print<>}{on} + \tl_gset:Nx\ocgxii@OCView{\ocgbase_last_ocg:} + \tl_gset:cx{ocgxii@OCView.opts}{showingui=never,printocg=never} + \ocgbase_new_ocg:nnn{OCPrint}{/Print<>}{off} + \tl_gset:Nx\ocgxii@OCPrint{\ocgbase_last_ocg:} + \tl_gset:cx{ocgxii@OCPrint.opts}{showingui=never,printocg=always} + \ocgxii_enable_ocglinks: + \iow_now:Nx\@mainaux{ + \token_to_str:N\ocgxii@newkey{ocgx2.ocg.OCView}{\ocgxii@OCView} + } + \iow_now:Nx\@mainaux{ + \token_to_str:N\ocgxii@newkey{ocgx2.ocg.OCPrint}{\ocgxii@OCPrint} + } + } + }{ + \msg_error:nnn{ocgx2}{missing~package}{hyperref} + } +} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +% TikZ related code follows (to be enabled with package option `tikz') + +\bool_if:NF\l_ocgxii_tikz_bool{\endinput} +\ExplSyntaxOff +\RequirePackage{tikz} +\usetikzlibrary{calc} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Copyright notice: The code that follows until the end of the file was +% taken from Paul Gaborit's `tikzlibraryocgx.code.tex' with minor additions. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\tikzset{ + ocg/.style={ocg/.cd,#1,/tikz/.cd}, + ocg={ + % parameters + name/.store in=\ocgxii@name, + opts/.store in=\ocgxii@opts, + visibility/.store in=\ocgxii@visibility, + status/.is choice, + status/visible/.style={visibility=1}, + status/invisible/.style={visibility=0}, + status/true/.style={visibility=1}, + status/false/.style={visibility=0}, + status/on/.style={visibility=1}, + status/off/.style={visibility=0}, + status/1/.style={visibility=1}, + status/0/.style={visibility=0}, + % default values + name=, + opts=, + status=on, + % ref + ref/.style={ + /tikz/execute at begin scope={% + \begin{ocg}[\ocgxii@opts]{% + \ifx\empty\ocgxii@name\empty#1\else\ocgxii@name\fi% + }{#1}{\ocgxii@visibility}}, + /tikz/execute at end scope={\end{ocg}}, + }, + }, + switch ocg/.style={ + postaction={ + path picture={ + \path let + \p1 = (path picture bounding box.south west), + \p2 = (path picture bounding box.north east), + \p3 = (\x2-\x1,\y2-\y1) + in + (path picture bounding box.center) + node[inner sep=0pt,anchor=center,outer sep=0pt] + {\switchocg*{#1}{\phantom{\rule{\x3}{\y3}}}}; + } + }, + }, + switch ocg with mark on/.style 2 args={ + postaction={ + path picture={ + \begin{ocg}[showingui=false]{#1}{#1}{1} + \draw + (path picture bounding box.south west) + -- + (path picture bounding box.north east) + (path picture bounding box.south east) + -- + (path picture bounding box.north west) + ; + \end{ocg} + }, + switch ocg={#1 #2}, + } + }, + switch ocg with mark off/.style 2 args={ + postaction={ + path picture={ + \begin{ocg}[showingui=false]{#1}{#1}{0} + \draw + (path picture bounding box.south west) + -- + (path picture bounding box.north east) + (path picture bounding box.south east) + -- + (path picture bounding box.north west) + ; + \end{ocg} + }, + switch ocg={#1 #2}, + } + }, + show ocg/.style={ + postaction={ + path picture={ + \path let + \p1 = (path picture bounding box.south west), + \p2 = (path picture bounding box.north east), + \p3 = (\x2-\x1,\y2-\y1) + in + (path picture bounding box.center) + node[inner sep=0pt,anchor=center] + {\showocg*{#1}{\phantom{\rule{\x3}{\y3}}}}; + }, + }, + }, + hide ocg/.style={ + postaction={ + path picture={ + \path let + \p1 = (path picture bounding box.south west), + \p2 = (path picture bounding box.north east), + \p3 = (\x2-\x1,\y2-\y1) + in + (path picture bounding box.center) + node[inner sep=0pt,anchor=center] + {\hideocg*{#1}{\phantom{\rule{\x3}{\y3}}}}; + }, + }, + }, + actions ocg/.style n args={3}{ + postaction={ + path picture={ + \path let + \p1 = (path picture bounding box.south west), + \p2 = (path picture bounding box.north east), + \p3 = (\x2-\x1,\y2-\y1) + in + (path picture bounding box.center) + node[inner sep=0pt,anchor=center] + {\actionsocg*{#1}{#2}{#3}{\phantom{\rule{\x3}{\y3}}}}; + }, + }, + }, +} diff --git a/travis_deploy.sh b/travis_deploy.sh new file mode 100755 index 00000000..766c275c --- /dev/null +++ b/travis_deploy.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +set -e + +SHA=`git rev-parse --verify HEAD` + +git config user.name "$COMMIT_AUTHOR" +git config user.email "$COMMIT_AUTHOR_EMAIL" +git checkout --orphan gh-pages +git rm --cached -r . +echo "# Automatic build" > README.md +echo "Built pdf from \`$SHA\`. See https://github.com/ethereum/yellowpaper/ for details." >> README.md +echo "The generated pdf is here: https://ethereum.github.io/yellowpaper/paper.pdf" >> README.md +echo '' > index.html +mv Paper.pdf paper.pdf +git add -f README.md index.html paper.pdf +git commit -m "Built pdf from {$SHA}." + +ENCRYPTED_KEY_VAR="encrypted_${ENCRYPTION_LABEL}_key" +ENCRYPTED_IV_VAR="encrypted_${ENCRYPTION_LABEL}_iv" +ENCRYPTED_KEY=${!ENCRYPTED_KEY_VAR} +ENCRYPTED_IV=${!ENCRYPTED_IV_VAR} +openssl aes-256-cbc -K $ENCRYPTED_KEY -iv $ENCRYPTED_IV -in deploykey.enc -out deploykey -d +chmod 600 deploykey +eval `ssh-agent -s` +ssh-add deploykey + +git push -f "$PUSH_REPO" gh-pages