GhaSShee


yellowpaper


[yellowpaper](/yellowpaper/jp-Paper.pdf) # Reform ## world state $\sigma$ - $\sigma [a] _ n$ : the number of transactions( $ a $ is EOA)/contracts(Contract) created - $\sigma [a] _ b$ : a's balance ## Header $ H $ $\mathbb{B}_{32}$ : the type of keccak256 hash - $H_p : \mathbb{B}_{32}$ : parent Hash - $H_o : \mathbb{B}_{32}$ : ommer Hash - $H_r : \mathbb{B}_{32}$ : state Root - $H_t : \mathbb{B}_{32}$ : transactions Root - $H_e : \mathbb{B}_{32}$ : reciepts Root - $H_m : \mathbb{B}_{32}$ : mix Hash (proof Hash) $\mathbb{B}_{20}$ : the type of addresses - $H_c : \mathbb{B}_{20}$ : beneficiary : miner Address $\mathbb{B}_{256}$ : the type of 2048 bits - $H_b : \mathbb{B}_{256}$ : logs Bloom : the type of Bloom Filter $\mathbb{N}$ : the type of Natural Numbers - $H_l$ : - $H_t$ : transactionsRoot ## important information used in the execution environment $ I $ - $I_\mathbb{b}$ : the byte array that is the machine code to be executed - $I_a$ : account who owns the code - $I_e$ : the depth of the present message-call - $I_o$ : sender of the transaction which originated this execution - $I_p$ : the price of gas in the transaction that originated this execution - ## sets - $\mathbb{O}$ := { `00`,`01`, ... ,`FF` } : the set of bytes - $\mathbb{B}$ : the set of byte arrays - $\mathbb{T}$ : tree ~~~ type T = B | L type L = list B | list L type B = list O ~~~ # 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. 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. # Introduction 世界中の数多の土地でインターネットがありふれたものとなると同時に、 地球内の情報転送が信じられないほどに安価なものとなりました。 2009年に始まったビットコインのような技術志向の動向は、 そのコミュニティの力を通じて、 コンセンサスメカニズムと「ソーシャルコントラクト」の貢献的な側面に対して、 インターネットを利用することで非中央集約型の価値転送システムを構築し、 世界中で共有し、仮想的に自由に使用できる、という裏付けをしました。 このシステムは、「暗号学的に強固なトランザクション基調の状態マシン」の特別版と言えます。 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. ## Driving Factors There are many goals of this project; one key goal is to facilitate transactions between consenting individuals who would otherwise have no means to trust one another. This may be due to geographical separation, interfacing difficulty, or perhaps the incompatibility, incompetence, unwillingness, expense, uncertainty, inconvenience or corruption of existing legal systems. By specifying a state-change system through a rich and unambiguous language, and furthermore architecting a system such that we can reasonably expect that an agreement will be thus enforced autonomously, we can provide a means to this end. Dealings in this proposed system would have several attributes not often found in the real world. The incorruptibility of judgement, often difficult to find, comes naturally from a disinterested algorithmic interpreter. Transparency, or being able to see exactly how a state or judgement came about through the transaction log and rules or instructional codes, never happens perfectly in human-based systems since natural language is necessarily vague, information is often lacking, and plain old prejudices are difficult to shake. Overall, I wish to provide a system such that users can be guaranteed that no matter with which other individuals, systems or organisations they interact, they can do so with absolute confidence in the possible outcomes and how those outcomes might come about. ## Previous Work $\cite{buterin2013ethereum}$ first proposed the kernel of this work in late November, 2013. Though now evolved in many ways, the key functionality of a block-chain with a Turing-complete language and an effectively unlimited inter-transaction storage capability remains unchanged. Hashcash, introduced by \cite{back2002hashcash} (in a five-year retrospective), provided the first work into the usage of a cryptographic proof of computational expenditure as a means of transmitting a value signal over the Internet. Though not widely adopted, the work was later utilised and expanded upon by \cite{nakamoto2008bitcoin} in order to devise a cryptographically secure mechanism for coming to a decentralised social consensus over the order and contents of a series of cryptographically signed financial transactions. The fruits of this project, Bitcoin, provided a first glimpse into a decentralised transaction ledger. Other projects built on Bitcoin's success; the alt-coins introduced numerous other currencies through alteration to the protocol. Some of the best known are Litecoin and Primecoin, discussed by \cite{sprankel2013technical}. Other projects sought to take the core value content mechanism of the protocol and repurpose it; \cite{aron2012bitcoin} discusses, for example, the Namecoin project which aims to provide a decentralised name-resolution system. Other projects still aim to build upon the Bitcoin network itself, leveraging the large amount of value placed in the system and the vast amount of computation that goes into the consensus mechanism. The Mastercoin project, first proposed by \cite{mastercoin2013willett}, aims to build a richer protocol involving many additional high-level features on top of the Bitcoin protocol through utilisation of a number of auxiliary parts to the core protocol. The Coloured Coins project, proposed by \cite{colouredcoins2012rosenfeld}, takes a similar but more simplified strategy, embellishing the rules of a transaction in order to break the fungibility of Bitcoin's base currency and allow the creation and tracking of tokens through a special ``chroma-wallet''-protocol-aware piece of software. Additional work has been done in the area with discarding the decentralisation foundation; Ripple, discussed by \cite{boutellier2014pirates}, has sought to create a ``federated'' system for currency exchange, effectively creating a new financial clearing system. It has demonstrated that high efficiency gains can be made if the decentralisation premise is discarded. Early work on smart contracts has been done by \cite{szabo1997formalizing} and \cite{miller1997future}. Around the 1990s it became clear that algorithmic enforcement of agreements could become a significant force in human cooperation. Though no specific system was proposed to implement such a system, it was proposed that the future of law would be heavily affected by such systems. In this light, Ethereum may be seen as a general implementation of such a \textit{crypto-law} system. # The Blockchian Paradigm Ethereum の全体像とは、「トランザクションベースな状態マシン」です。 genesis state からトランザクションを次々と実行し、ある final state へと「射」を適用します。 その終状態こそが、 Etereum world の主要な version として我々が共有します。 状態には、アカウントの残高、評判、信託、物理世界の情報のようなデータを、 端的に言えば、現在コンピュータで表現できるものは全て保持できます。 トランザクションは、二つの状態をつなぐ、"有効な"「橋」を表します。 有効であることは、とても重要です。--- 有効な状態変化よりも無効なものの方がはるかに多いのです。 無効な状態遷移とは、例えば、アカウントの残高が減った時に、同じ分だけ増える他方が、 全く存在しないというようなものです。 有効な状態遷移は、概ねトランザクションを通じて生成され、 形式的には: 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, e.g. 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}$ ここで、 - $\Upsilon$ : 状態遷移関数 - $\boldsymbol{\sigma}$ : 状態 - $\Upsilon$ : blockchain に記録され、 - $\boldsymbol{\sigma}$ : 状態つまり、storage は記録さない。 - $\Upsilon$ は、構成要素に 任意の計算実行 を 許可する - $\boldsymbol{\sigma}$ は、構成要素に トランザクション間の任意の状態を store 可能にした where $\Upsilon$ is the Ethereum state transition function. In Ethereum, $\Upsilon$ , together with $\boldsymbol{\sigma}$ are considerably more powerful then any existing comparable system; $\Upsilon$ allows components to carry out arbitrary computation, while $\boldsymbol{\sigma}$ allows components to store arbitrary state between transactions. トランザクションはブロックに集約されます。 ブロックとブロックは、暗号学的ハッシュ値を参照方法として利用した、鎖で繋がれています。 ブロックは記録簿として機能し、一連のトランザクションをまとめて、 以前のブロックと終状態を表す識別子と一緒に、記録します。 (しかしながら、終状態全てを store しているわけではない---というのはあまりにも大きすぎるからだ)。 ブロックとブロックはノードが採掘するインセンティブによってトランザクションの連なりに対して区切りを挿入します。 このインセンティブ挿入は状態遷移関数として実行され、 報酬を受けるアカウントに対し価値を付与します。 Transactions are collated into blocks; blocks are chained together using a cryptographic hash as a means of reference. Blocks function as a journal, recording a series of transactions together with the previous block and an identifier for the final state (though do not store the final state itself---that would be far too big). They also punctuate the transaction series with incentives for nodes to \textit{mine}. This incentivisation takes places as a state-transition function, adding value to a nominated account. 採掘は労力を費やすプロセスのことであり、 他のすべての潜在的な競争者のブロックの上に、ある一連のトランザクション(ブロック)をつまみあげ、それを支えています。 それは、暗号学的に強力な証明のおかげで達成されています。 この技術はproof-of-workとして知られており、Mining Proof-Of-Work section で詳しく述べます。 Mining is the process of dedicating effort (working) to bolster one series of transactions (a block) over any other potential competitor block. It is achieved thanks to a cryptographically secure proof. This scheme is known as a proof-of-work and is discussed in detail in Mining Proof-Of-Work section . 形式的には、次のように展開します: Formally, we expand to: $$ \begin{eqnarray} \boldsymbol{\sigma}_{t+1} & \equiv & \Pi(\boldsymbol{\sigma}_t, B) \\ B & \equiv & (..., (T_0, T_1, ...) ) \\ \Pi(\boldsymbol{\sigma}, B) & \equiv & \Omega(B, \Upsilon(\Upsilon(\boldsymbol{\sigma}, T_0), T_1) ...) \end{eqnarray} $$ ここで - $\Omega$ : ブロック生成関数(採掘者に報酬も支払う) - $B$ : ブロック - $\Pi$ : ブロック状態遷移関数 Where $\Omega$ is the block-finalisation state transition function (a function that rewards a nominated party); $B$ is this block, which includes a series of transactions amongst some other components; and $\Pi$ is the block-level state-transition function. これはブロックチェーンパラダイムの基礎であり、 Ethereum はじめ最新の分散型コンセンサスベースのトランザクションシステムの背骨を構成するモデルです。 This is the basis of the blockchain paradigm, a model that forms the backbone of not only Ethereum, but all decentralised consensus-based transaction systems to date. ## Value In order to incentivise computation within the network, there needs to be an agreed method for transmitting value. To address this issue, Ethereum has an intrinsic currency, Ether, known also as ${ \small ETH }$ and sometimes referred to by the Old English $\DH{}$ . The smallest subdenomination of Ether, and thus the one in which all integer values of the currency are counted, is the Wei. One Ether is defined as being $10^{18}$ Wei. There exist other subdenominations of Ether: Multiplier | Name ------------|------ $10^0$ | Wei $10^{12}$ | Szabo $10^{15}$ | Finney $10^{18}$ | Ether Throughout the present work, any reference to value, in the context of Ether, currency, a balance or a payment, should be assumed to be counted in Wei. ## Which History? Since the system is decentralised and all parties have an opportunity to create a new block on some older pre-existing block, the resultant structure is necessarily a tree of blocks. In order to form a consensus as to which path, from root (the genesis block) to leaf (the block containing the most recent transactions) through this tree structure, known as the blockchain, there must be an agreed-upon scheme. If there is ever a disagreement between nodes as to which root-to-leaf path down the block tree is the `best' blockchain, then a $\textit{fork}$ occurs. This would mean that past a given point in time (block), multiple states of the system may coexist: some nodes believing one block to contain the canonical transactions, other nodes believing some other block to be canonical, potentially containing radically different or incompatible transactions. This is to be avoided at all costs as the uncertainty that would ensue would likely kill all confidence in the entire system. 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 Blocktree to Blockchain section . # Conventions 私は形式的な表記に際し、数々の書式の慣習を使用します。 そのうちのいくつかは、現在の仕事に特化したものです。: I use a number of typographical conventions for the formal notation, some of which are quite particular to the present work: 高構造化された top-level の状態値である、ただ2つの集合は、Greek bold lowercase で表します。 - world-state 集合 $\boldsymbol{\sigma}$ あるいは `storage` - machine-state 集合 $\boldsymbol{\mu}$ あるいは `stack` 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}$ . 高構造化された値を扱う関数はギリシア語の大文字で表記します。 例えば、 Ethereum 状態遷移関数 $\Upsilon$ です。 Functions operating on highly structured values are denoted with an upper-case greek letter, e.g. $\Upsilon$ , the Ethereum state transition function. 関数には、大抵大文字を使います。 $C$ は、一般コスト関数 です。 特別変数をとり、特別な関数を表し、 $C_{\tiny\text{SSTORE}}$ は, 命令 $\tiny\text{SSTORE}$ のコスト関数です。 外部定義可能な特別な関数は、例えば、 Keccak-256 ハッシュ関数は $\texttt{KEC}$ とします。(また、単に Keccak と表記) 同様に $\texttt{KEC512}$ は Keccak 512 Hash 関数を表します。 For most functions, an uppercase letter is used, e.g. $ C $ , the general cost function. These may be subscripted to denote specialised variants, e.g. $ 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, e.g. 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. - タプルは大文字で表記する - 例 $T$ : トランザクション。 - この記号には、状況に応じて定義される、ある要素を参照する二次表記がある。 - 例 $T_n$ : は トランザクション の nonce。 二次表記は、型の表記にも使用され、例として、大文字の二次表記は、タプル中の二次表記可能な要素を参照します。 Tuples are typically denoted with an upper-case letter, e.g. $ T $ , is used to denote an Ethereum transaction. This symbol may, if accordingly defined, be subscripted to refer to an individual component, e.g. $ T_n $ , denotes the nonce of said transaction. The form of the subscript is used to denote its type; e.g. uppercase subscripts refer to tuples with subscriptable components. - スカラー値 や 固定長バイト列 は 標準の 小文字 で表記する。 - 例 $n$ : トランザクションの nonce 。 - ある特別な意味を持たせたい場合は ギリシア文字 を用いる。 - 例 $\delta$ : 与えられた命令でスタック上で必要とされるアイテムの数。 Scalars and fixed-size byte sequences (or, synonymously, arrays) are denoted with a normal lower-case letter, e.g. $ n $ is used in the document to denote a transaction nonce. Those with a particularly special meaning may be greek, e.g. $\delta$ , the number of items required on the stack for a given operation. 任意長文字列は、ふつう 太い小文字 で表記する。 例 $\mathbf{o}$ : メッセージコールの出力値(バイト列)。 特に重要な値には、太い大文字 を使います。 Arbitrary-length sequences are typically denoted as a bold lower-case letter, e.g. $\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. 一貫して、スカラー値 は正の整数値であり、つまり集合 $\mathbb{P}$ の要素となる。 バイト列の全集合は $\mathbb{B}$ であり、Appendix RLP で形式的に定義されています。 それらの データの集合 がある特定の長さである場合、二次表記を用い、 $\mathbb{B}_{32}$ : 全バイト列長が $32$ 。 $2^{256}$ より小さい正整数の全集合は、 $\mathbb{P}_{256}$ と命名しています。 これは、 The Block sec. で形式的に定義します。 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 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 The Block sec. 角型の括弧は個々の要素や2次列の参照に使用されます。 - $\boldsymbol{\mu}_\mathbf{s}[0]$ : machine stack 上の最初のアイテム。 二次列には、範囲を表すのに省略符号が使用され、両端の極限値も含みます。 - $\boldsymbol{\mu}_\mathbf{m}[0..31]$ : machine memory の最初の32個のアイテム。 Square brackets are used to index into and reference individual components or subsequences of sequences, e.g. $ \boldsymbol{\mu}_\mathbf{s}[0] $ denotes the first item on the machine's stack. For subsequences, ellipses are used to specify the intended range, to include elements at both limits, e.g. $ \boldsymbol{\mu}_\mathbf{m}[0..31] $ denotes the first 32 items of the machine's memory. global state $\boldsymbol{\sigma}$ の場合、 それはアカウント列であり、それ自身タプルであり、 角型の括弧は、個々のアカウントを参照するために使います。 In the case of the global state $ \boldsymbol{\sigma} $ , which is a sequence of accounts, themselves tuples, the square brackets are used to reference an individual account. ある値の細かな違いを考えるとき、 あるスコープ内で定義された次の規則に従います。 前処理の施していない入力値を $\Box$ とすると $\Box'$ は処理を施し使える状態となった値を表します。 また中間の値は $\Box^*$ や $\Box^{**}$ などと表記します。 特別な状況では、可読性を高め、意味合いの一義性を確保するために、 アルファ数字 の 二次表記 で 中間の値 を表し、 とりわけ注目する場合に使います。 When considering variants of existing values, I follow the rule that within a given scope for definition, if we assume that the unmodified `input' value be denoted by the placeholder $ \Box $ then the modified and utilisable value is denoted as $ \Box' $ , and intermediate values would be $ \Box^* $ , $ \Box^{**} $ \&c. On very particular occasions, in order to maximise readability and only if unambiguous in meaning, I may use alpha-numeric subscripts to denote intermediate values, especially those of particular note. 関数 $f$ に対し、 $f^*$ は、引数の範囲がデータ列に限らない、よく似た、引数の幅が広い、より抽象化された関数写像を表します。 section block 章で形式的に定義します。 When considering the use of existing functions, given a function $ f $ , the function $ f^* $ denotes a similar, element-wise version of the function mapping instead between sequences. It is formally defined in The Block section. 始終、幾多の役立つ関数を定義します。 よくつかうものとして $\ell$ があり、与えられたデータ列の最後のアイテム を評価し、返します。 I define a number of useful functions throughout. One of the more common is $\ell$ , which evaluates to the last item in the given sequence: $$ \begin{equation} \ell(\mathbf{x}) \equiv \mathbf{x}[\lVert \mathbf{x} \rVert - 1] \end{equation} $$ # Blocks, State and Transactions Ethereumの背景となる概念を説明しましたが、ここでは、transaction, block, state の意味について詳細に議論します。 Having introduced the basic concepts behind Ethereum, we will discuss the meaning of a transaction, a block and the state in more detail. ## World State world state $(\textit{state})$ は アドレス(160バイトの識別子)から、 アカウントの状態( RLPとして識別番号が付与されたデータ構造、Appendix RLP 参照 ) を返す写像です。 ブロックチェーン上には store されませんが、 実装に際し、この写像は、パトリシア木 ( $\textit{trie}$ , Modified Merkle Patricia Tree section ) の中で保持されます。 この trie は、バイト配列からバイト配列への写像を保持するデータベースバックエンドを要します。 このデータベースを state データベースと命名します。 これにはたくさんの利点があります。 まずこのtrie構造の root node は、暗号学的にすべての内部データに依存しており、それが故、 そのハッシュ値は全システムの状態を強固に管理する識別番号として利用できます。 つぎに、変更が加えられることのないデータ構造なので、 root hashを単に置き換えることにより、それ以前のすべての状態を呼び出すことができます。 ブロックチェーン上に以前の root hash を store してあるので 些細な変更で前の状態に戻すことができます。 The world state (\textit{state}), is a mapping between addresses (160-bit identifiers) and account states (a data structure serialised as RLP, see Appendix RLP). Though not stored on the blockchain, it is assumed that the implementation will maintain this mapping in a modified Merkle Patricia tree ( $\textit{trie}$ , see Modified Merkle Patricia Tree section ). The trie requires a simple database backend that maintains a mapping of bytearrays to bytearrays; we name this underlying database the state database. This has a number of benefits; firstly the root node of this structure is cryptographically dependent on all internal data and as such its hash can be used as a secure identity for the entire system state. Secondly, being an immutable data structure, it allows any previous state (whose root hash is known) to be recalled by simply altering the root hash accordingly. Since we store all such root hashes in the blockchain, we are able to trivially revert to old states. アカウントの状態は次の4つのフィールドにより構成されます。 The account state comprises the following four fields: nonce: スカラー値。このアカウントアドレスが送信するトランザクション番号。 コントラクトアカウント の場合は、この アカウント が作成する contract-creations 番号。 アカウントアドレス $a$ が状態 $\boldsymbol{\sigma}$ にあるとき、 $\boldsymbol{\sigma}[a]_n$ と表記します。 A scalar value equal to the number of transactions sent from this address or, in the case of accounts with associated code, the number of contract-creations made by this account. For account of address $ a $ in state $ \boldsymbol{\sigma} $ , this would be formally denoted $ \boldsymbol{\sigma}[a]_n $ . balance: スカラー値。アカウントアドレスが所有するWeiの量。 $\boldsymbol{\sigma}[a]_b$ と表記する。 A scalar value equal to the number of Wei owned by this address. Formally denoted $\boldsymbol{\sigma}[a]_b$ . storageRoot: アカウントの storage の中身を符号化した パトリシア木のルートノードの256bitハッシュ値を格納した配列( 256bit integer 値間の写像)です。 この値は、 「256bit整数値の key を Keccak256 にかけた値 から、RLP符号化された256bit整数値への写像」として、 trieの中に符号化されています。 アドレスを入力値としてストレージルートを返す。 このハッシュ値は形式的に $\boldsymbol{\sigma}[a]_s$ と表記する。 A 256-bit hash of the root node of a Merkle Patricia tree that encodes the storage contents of the account (a mapping between 256-bit integer values), encoded into the trie as a mapping from the Keccak 256-bit hash of the 256-bit integer keys to the RLP-encoded 256-bit integer values. The hash is formally denoted $\boldsymbol{\sigma}[a]_s$ . codeHash: アカウントの EVM code の hash 。 これは、accont Address が message call をうけとると実行される code です。 これは 変更不可能 です。上の3つと異なり、コンストラクションの後は一切変更されません。 コードの断片はすべて、state データベース に含まれ、それぞれの hash により配置され、 最新のデータ取得ができます。 この hash は形式的に、 $\boldsymbol{\sigma}[a]_c$ と表記し、 code を $\mathbf{b}$ とすると、 $\small\texttt{KEC} (\mathbf{b}) = \boldsymbol{\sigma}[a]_c$ で与えられます。 The hash of the EVM code of this account---this is the code that gets executed should this address receive a message call; it is immutable and thus, unlike all other fields, cannot be changed after construction. All such code fragments are contained in the state database under their corresponding hashes for later retrieval. This hash is formally denoted $ \boldsymbol{\sigma}[a]_c $ , and thus the code may be denoted as $ \mathbf{b} $ , trie の root hash を参照するのでなく、内部の key/value pair を参照したいので、 次のように表記を簡略化します。 Since I typically wish to refer not to the trie's root hash but to the underlying set of key/value pairs stored within, I define a convenient equivalence: $$ \begin{equation} \small\texttt{ TRIE}\big(L_I^*(\boldsymbol{\sigma}[a]_\mathbf{s})\big) \equiv \boldsymbol{\sigma}[a]_s \end{equation} $$ trie における key / value ペアを引数にとる崩壊関数である $ L_I^* $ は、 元となる関数 $ L_I $ の引数の定義域が広いものです。 The collapse function for the set of key/value pairs in the trie, $ L_I^* $ , is defined as the element-wise transformation of the base function $ L_I $ , given as: $$ \begin{equation} L_I\big( (k, v) \big) \equiv \big( \small\texttt{KEC}(k), \small\texttt{RLP}(v)\big) \end{equation} $$ where: $$ \begin{equation} k \in \mathbb{B}_{32} \quad \wedge \quad v \in \mathbb{P} \end{equation} $$ $\boldsymbol{\sigma}[a]_\mathbf{s}$ は account の物理的要因ではなく、後々の serialisation に貢献します。 It shall be understood that $ \boldsymbol{\sigma}[a]_\mathbf{s} $ is not a `physical' member of the account and does not contribute to its later serialisation. $\textbf{codeHash} field$ が空列の Keccak-256 hash であれば、( $\boldsymbol{\sigma}[a]_c = \small\texttt{KEC}\big(()\big)$ )、node は シンプルな、あるいは non-constarct account として参照されます。 If the $ \textbf{codeHash} $ field is the Keccak-256 hash of the empty string, i.e. $ \boldsymbol{\sigma}[a]_c = \small\texttt{KEC}\big(()\big) $ , then the node represents a simple account, sometimes referred to as a ``non-contract'' account. world-state 崩壊関数 $L_S$ : Thus we may define a world-state collapse function $L_S$ : $$ \begin{equation} L_S(\boldsymbol{\sigma}) \equiv \{ p(a): \boldsymbol{\sigma}[a] \neq \varnothing \} \end{equation} $$ where $$ \begin{equation} p(a) \equiv \big(\small\texttt{KEC}(a), \small\texttt{RLP}\big( (\boldsymbol{\sigma}[a]_n, \boldsymbol{\sigma}[a]_b, \boldsymbol{\sigma}[a]_s, \boldsymbol{\sigma}[a]_c) \big) \big) \end{equation} $$ This function, $L_S$ , is used alongside the trie function to provide a short identity (hash) of the world state. We assume: $$ \begin{equation} \forall a: \boldsymbol{\sigma}[a] = \varnothing \; \vee \; (a \in \mathbb{B}_{20} \; \wedge \; v(\boldsymbol{\sigma}[a])) \end{equation} $$ where $v$ is the account validity function: $$ \begin{equation} \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} $$ $$ \begin{equation} \small\texttt{TRIE}\big(L_I^*(\boldsymbol{\sigma}[a]_\mathbf{s})\big) \equiv \boldsymbol{\sigma}[a]_s \end{equation} $$ ## The Transaction トランザクション $ T $ とは、暗号学的に署名された一つの命令で、Ethereum のスコープの外から、an actor によって構築されるものです。当然その actor とは、人間であるということが究極的には考えられますが、ソフトウェアツールがトランザクションの構築と頒布において使用されるでしょう。 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 ( 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. e.g. 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: - [nonce] A scalar value equal to the number of transactions sent by the sender; formally $T_n$ . - [gasPrice] A scalar value equal to the number of Wei to be paid per unit of \textit{gas} for all computation costs incurred as a result of the execution of this transaction; formally $T_p$ . - [gasLimit] A scalar value equal to the maximum amount of gas that should be used in executing this transaction. This is paid up-front, before any computation is done and may not be increased later; formally $T_g$ . - [to] The 160-bit address of the message call's recipient or, for a contract creation transaction, $\varnothing$ , used here to denote the only member of $ \mathbb{B}_0 $ ; formally $T_t$ . - [value] A scalar value equal to the number of Wei to be transferred to the message call's recipient or, in the case of contract creation, as an endowment to the newly created account; formally $T_v$ . - [v, r, s] Values corresponding to the signature of the transaction and used to determine the sender of the transaction; formally $T_w$ , $T_r$ and $T_s$ . This is expanded in Appendix \ref{app:signing}. Additionally, a contract creation transaction contains: - [init] An unlimited size byte array specifying the EVM-code for the account initialisation procedure, formally $ T_\mathbf{i} $ . \textbf{init} is an EVM-code fragment; it returns the \textbf{body}, a second fragment of code that executes each time the account receives a message call (either through a transaction or due to the internal execution of code). \textbf{init} is executed only once at account creation and gets discarded immediately thereafter. In contrast, a message call transaction contains: - [data] An unlimited size byte array specifying the input data of the message call, formally $ T_\mathbf{d} $ . Appendix \ref{app:signing} specifies the function, $ S $ , which maps transactions to the sender, and happens through the ECDSA of the SECP-256k1 curve, using the hash of the transaction (excepting the latter three signature fields) as the datum to sign. For the present we simply assert that the sender of a given transaction $ T $ can be represented with $ S(T) $ . $$ \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} \end{cases} \end{equation} $$ Here, we assume all components are interpreted by the RLP as integer values, with the exception of the arbitrary length byte arrays $ T _ \mathbf{i} $ and $ T _ \mathbf{d} $ . $$ \begin{equation} \begin{array}[t]{lclclc} T_n \in \mathbb{P}_{256} & \wedge & T_v \in \mathbb{P}_{256} & \wedge & T_p \in \mathbb{P}_{256} & \wedge \\ T_g \in \mathbb{P}_{256} & \wedge & T_w \in \mathbb{P}_5 & \wedge & T_r \in \mathbb{P}_{256} & \wedge \\ T_s \in \mathbb{P}_{256} & \wedge & T_\mathbf{d} \in \mathbb{B} & \wedge & T_\mathbf{i} \in \mathbb{B} \end{array} \end{equation} $$ where $$ \begin{equation} \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 $ : $$ \begin{equation} T_t \in \begin{cases} \mathbb{B}_{20} & \text{if} \quad T_t \neq \varnothing \\ \mathbb{B}_{0} & \text{otherwise}\end{cases} \end{equation} $$ ## The 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: %\textit{TODO: Introduce logs} - [parentHash] The Keccak 256-bit hash of the parent block's header, in its entirety; formally $ H_p $ . - [ommersHash] The Keccak 256-bit hash of the ommers list portion of this block; formally $ H_o $ . - [beneficiary] The 160-bit address to which all fees collected from the successful mining of this block be transferred; formally $ H_c $ . - [stateRoot] The Keccak 256-bit hash of the root node of the state trie, after all transactions are executed and finalisations applied; formally $ H_r $ . - [transactionsRoot] The Keccak 256-bit hash of the root node of the trie structure populated with each transaction in the transactions list portion of the block; formally $ H_t $ . - [receiptsRoot] The Keccak 256-bit hash of the root node of the trie structure populated with the receipts of each transaction in the transactions list portion of the block; formally $ H_e $ . - [logsBloom] The Bloom filter composed from indexable information (logger address and log topics) contained in each log entry from the receipt of each transaction in the transactions list; formally $ H_b $ . - [difficulty] A scalar value corresponding to the difficulty level of this block. This can be calculated from the previous block's difficulty level and the timestamp; formally $ H_d $ . - [number] A scalar value equal to the number of ancestor blocks. The genesis block has a number of zero; formally $ H_i $ . - [gasLimit] A scalar value equal to the current limit of gas expenditure per block; formally $ H_l $ . - [gasUsed] A scalar value equal to the total gas used in transactions in this block; formally $ H_g $ . - [timestamp] A scalar value equal to the reasonable output of Unix's time() at this block's inception; formally $ H_s $ . - [extraData] An arbitrary byte array containing data relevant to this block. This must be 32 bytes or fewer; formally $ H_x $ . - [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 $ . - [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 $ . 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 $ : $$ \begin{equation} B \equiv (B_H, B_\mathbf{T}, B_\mathbf{U}) \end{equation} $$ ## 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 $ . 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 $ : $$ \begin{equation} R \equiv (R_{\boldsymbol{\sigma}}, R_u, R_b, R_\mathbf{l}) \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 (\small\mathtt{TRIE}(L_S(R_{\boldsymbol{\sigma}})), R_u, R_b, R_\mathbf{l}) \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} R_u \in \mathbb{P} \quad \wedge \quad R_b \in \mathbb{B}_{256} \end{equation} $$ %Notably $ B_\mathbf{T} $ does not get serialised into the block by the block preparation function $ L_B $ ; it is merely a convenience equivalence. The log entries, $ R_\mathbf{l} $ , is a series of log entries, termed, for example, $ (O_0, O_1, ...) $ . A log entry, $ O $ , is a tuple of a logger's address, $ O_a $ , a series of 32-bytes log topics, $ O_\mathbf{t} $ and some number of bytes of data, $ O_\mathbf{d} $ : $$\begin{equation} O \equiv (O_a, ({O_\mathbf{t}}_0, {O_\mathbf{t}}_1, ...), O_\mathbf{d}) \end{equation} \begin{equation} O_a \in \mathbb{B}_{20} \quad \wedge \quad \forall_{t \in O_\mathbf{t}}: t \in \mathbb{B}_{32} \quad \wedge \quad O_\mathbf{d} \in \mathbb{B} \end{equation} $$ We define the Bloom filter function, $ M $ , to reduce a log entry include a single 256-byte hash: $$ \begin{equation} M(O) \equiv \bigvee_{t \in \{O_a\} \cup O_\mathbf{t}} \big( M_{3:2048}(t) \big) \end{equation} $$ where $ M _ {3:512} $ 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: $$ \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:}\\ \forall_{i \in \{0, 2, 4\}}&:& \mathcal{B}_{m(\mathbf{x}, i)}(\mathbf{y}) = 1\\ m(\mathbf{x}, i) &\equiv& \tiny\mathtt{KEC}(\mathbf{x})[i, i + 1] \bmod 2048 \end{eqnarray} $$ where $ \mathcal{B} $ is the bit reference function such that $ \mathcal{B}_j(\mathbf{x}) $ equals the bit of index $ j $ (indexed from 0) in the byte array $ \mathbf{x} $ . ## Holistic Validity We can assert a block's validity if and only if it satisfies several conditions: it must be internally consistent with the ommer and transaction block hashes and the given transactions $ B _ \mathbf{T} $ ( as specified in sec $ \ref{ch:finalisation} $ ), when executed in order on the base state $ \boldsymbol{\sigma} $ (derived from the final state of the parent block), result in a new state of the identity $ H_r $ : $$ \begin{equation} \begin{array}[t]{lclc} H_r &\equiv& \small\mathtt{TRIE}(L_S(\Pi(\boldsymbol{\sigma}, B))) & \wedge \\ H_o &\equiv& \small\mathtt{KEC}(\small\mathtt{RLP}(L_H^*(B_\mathbf{U}))) & \wedge \\ H_t &\equiv& \small\mathtt{TRIE}(\{\forall i < \lVert B_\mathbf{T} \rVert, i \in \mathbb{P}: p(i, L_T(B_\mathbf{T}[i]))\}) & \wedge \\ H_e &\equiv& \small\mathtt{TRIE}(\{\forall i < \lVert B_\mathbf{T} \rVert, i \in \mathbb{P}: p(i, L_R(B_\mathbf{R}[i]))\}) & \wedge \\ H_b &\equiv& \bigvee_{\mathbf{r} \in B_\mathbf{R}} \big( \mathbf{r}_b \big) \end{array} \end{equation} $$ where $ p(k, v) $ is simply the pairwise RLP transformation, in this case, the first being the index of the transaction in the block and the second being the transaction receipt: $$ \begin{equation} p(k, v) \equiv \big( \small\mathtt{RLP}(k), \small\mathtt{RLP}(v) \big) \end{equation} $$ Furthermore: $$ \begin{equation} \small\mathtt{TRIE}(L_S(\boldsymbol{\sigma})) = {P(B_H)_H}_r \end{equation} $$ Thus $ \small\texttt{TRIE}(L_S(\boldsymbol{\sigma})) $ is the root node hash of the Merkle Patricia tree structure containing the key-value pairs of the state $ \boldsymbol{\sigma} $ with values encoded using RLP, and $ P(B_H) $ is the parent block of $ B $ , defined directly. The values stemming from the computation of transactions, specifically the transaction receipts, $ B_\mathbf{R} $ , and that defined through the transactions state-accumulation function, $ \Pi $ , are formalised later in section \ref{sec:statenoncevalidation}. ## Serialisation The function $ L_B $ and $ L_H $ are the preparation functions for a block and block header respectively. Much like the transaction receipt preparation function $ L_R $ , we assert the types and order of the structure for when the RLP transformation is required: $$ \begin{eqnarray} \quad L_H(H) & \equiv & (\begin{array}[t]{l}H_p, H_o, H_c, H_r, H_t, H_e, H_b, H_d,\\ H_i, H_l, H_g, H_s, H_x, H_m, H_n \; )\end{array} \\ \quad L_B(B) & \equiv & \big( L_H(B_H), L_T^*(B_\mathbf{T}), L_H^*(B_\mathbf{U}) \big) \end{eqnarray} $$ With $ L_T^* $ and $ L_H^* $ being element-wise sequence transformations, thus: $$ \begin{equation} f^*\big( (x_0, x_1, ...) \big) \equiv \big( f(x_0), f(x_1), ... \big) \quad \text{for any function} \; f \end{equation} $$ The component types are defined thus: $$ \begin{equation} \begin{array}[t]{lclclcl} H_p \in \mathbb{B}_{32} & \wedge & H_o \in \mathbb{B}_{32} & \wedge & H_c \in \mathbb{B}_{20} & \wedge \\ H_r \in \mathbb{B}_{32} & \wedge & H_t \in \mathbb{B}_{32} & \wedge & H_e \in \mathbb{B}_{32} & \wedge \\ H_b \in \mathbb{B}_{256} & \wedge & H_d \in \mathbb{P} & \wedge & H_i \in \mathbb{P} & \wedge \\ H_l \in \mathbb{P} & \wedge & H_g \in \mathbb{P} & \wedge & H_s \in \mathbb{P}_{256} & \wedge \\ H_x \in \mathbb{B} & \wedge & H_m \in \mathbb{B}_{32} & \wedge & H_n \in \mathbb{B}_{8} \end{array} \end{equation} $$ where $$ \begin{equation} \mathbb{B}_n = \{ B: B \in \mathbb{B} \wedge \lVert B \rVert = n \} \end{equation} $$ We now have a rigorous specification for the construction of a formal block structure. The RLP function $ \small\texttt{RLP} $ (see Appendix \ref{app:rlp}) provides the canonical method for transforming this structure into a sequence of bytes ready for transmission over the wire or storage locally. ## Block Header Validity We define $P(B_H)$ to be the parent block of $B$ , formally: $$ \begin{equation} P(H)\equiv B' : \mathtt{KEC}(\mathtt{RLP}(B '_H))=H_p \end{equation}$$ The block number is the parent's block number incremented by one: $$ \begin{equation} H_i \equiv P(H) _ {H \tiny i} + 1 \end{equation} $$ The canonical difficulty of a block of header $ H $ is defined as $ D(H) $ : $$ \begin{equation} D(H) \equiv \begin{cases} 131072 & \text{if} \quad H_i = 0\\ P(H)_{H \tiny d} + \left\lfloor\frac{P(H)_{H \tiny d}}{2048}\right\rfloor & \text{if} \quad H_s < P(H)_{H \tiny s} + 13\\ \max \{ 131072, x \} & \text{otherwise}\\ \end{cases} \end{equation} $$ where: $$ \begin{equation} x = P(H)_{H \tiny d} - \left\lfloor\frac{P(H)_{H \tiny d}}{2048}\right\rfloor \end{equation} $$ The canonical gas limit $ H_l $ of a block of header $ H $ must fulfil the relation: $$ \begin{eqnarray} & & H_l < P(H)_{H \tiny l} + \left\lfloor\frac{P(H)_{H \tiny l}}{1024}\right\rfloor \quad \wedge \\ & & H_l > P(H)_{H \tiny l} - \left\lfloor\frac{P(H)_{H \tiny l}}{1024}\right\rfloor \quad \wedge \\ & & H_l \geqslant 5000 \end{eqnarray} $$ $ H_s $ is the timestamp of block $ H $ and must fulfil the relation: $$ \begin{equation} H_s > P(H)_{H \tiny s} \end{equation} $$ This mechanism enforces a homeostasis in terms of the time between blocks; a smaller period between the last two blocks results in an increase in the difficulty level and thus additional computation required, lengthening the likely next period. Conversely, if the period is too large, the difficulty, and expected time to the next block, is reduced. The nonce, $ H_n $ , must satisfy the relations: $$ \begin{equation} n \leqslant \frac{2^{256}}{H_d} \quad \wedge \quad m = H_m \end{equation} $$ with $ (n, m) = \mathtt{PoW}(H_{\hcancel{n}}, H_n, \mathbf{d}) $ . Where $ H_{\hcancel{n}} $ is the new block's header $ H $ , but \textit{without} the nonce and mix-hash components, $ \mathbf{d} $ being the current DAG, a large data set needed to compute the mix-hash, and $ \mathtt{PoW} $ is the proof-of-work function (see section \ref{ch:pow}): this evaluates to an array with the first item being the mix-hash, to proof that a correct DAG has been used, and the second item being a pseudo-random number cryptographically dependent on $ H $ and $ \mathbf{d} $ . Given an approximately uniform distribution in the range $ [0, 2^{64}) $ , the expected time to find a solution is proportional to the difficulty, $ H_d $ . This is the foundation of the security of the blockchain and is the fundamental reason why a malicious node cannot propagate newly created blocks that would otherwise overwrite (``rewrite'') history. Because the nonce must satisfy this requirement, and because its satisfaction depends on the contents of the block and in turn its composed transactions, creating new, valid, blocks is difficult and, over time, requires approximately the total compute power of the trustworthy portion of the mining peers. Thus we are able to define the block header validity function $ V(H) $ : $$ \begin{eqnarray} V(H) & \equiv & n \leqslant \frac{2^{256}}{H_d} \wedge m = H_m \quad \wedge \\ & & H_d = D(H) \quad \wedge \\ & & H_g \le H_l \quad \wedge \\ & & H_l < P(H)_{H \tiny l} + \left\lfloor\frac{P(H)_{H \tiny l}}{1024}\right\rfloor \quad \wedge \\ & & H_l > P(H)_{H \tiny l} - \left\lfloor\frac{P(H)_{H \tiny l}}{1024}\right\rfloor \quad \wedge \\ & & H_l \geqslant 125000 \quad \wedge \\ & & H_s > P(H)_{H \tiny s} \quad \wedge \\ & & H_i = P(H)_{H \tiny i} +1 \quad \wedge \\ & & \lVert H_x \rVert \le 32 \end{eqnarray} $$ where $ (n, m) = \mathtt{PoW}(H_{\hcancel{n}}, H_n, \mathbf{d}) $ Noting additionally that \textbf{extraData} must be at most 32 bytes. # Gas and Payment In order to avoid issues of network abuse and to sidestep the inevitable questions stemming from Turing completeness, all programmable computation in Ethereum is subject to fees. The fee schedule is specified in units of \textit{gas} (see Appendix \ref{app:fees} for the fees associated with various computation). Thus any given fragment of programmable computation (this includes creating contracts, making message calls, utilising and accessing account storage and executing operations on the virtual machine) has a universally agreed cost in terms of gas. Every transaction has a specific amount of gas associated with it: \textbf{gasLimit}. This is the amount of gas which is implicitly purchased from the sender's account balance. The purchase happens at the according \textbf{gasPrice}, also specified in the transaction. The transaction is considered invalid if the account balance cannot support such a purchase. It is named \textbf{gasLimit} since any unused gas at the end of the transaction is refunded (at the same rate of purchase) to the sender's account. Gas does not exist outside of the execution of a transaction. Thus for accounts with trusted code associated, a relatively high gas limit may be set and left alone. In general, Ether used to purchase gas that is not refunded is delivered to the \textit{beneficiary} address, the address of an account typically under the control of the miner. Transactors are free to specify any \textbf{gasPrice} that they wish, however miners are free to ignore transactions as they choose. A higher gas price on a transaction will therefore cost the sender more in terms of Ether and deliver a greater value to the miner and thus will more likely be selected for inclusion by more miners. Miners, in general, will choose to advertise the minimum gas price for which they will execute transactions and transactors will be free to canvas these prices in determining what gas price to offer. Since there will be a (weighted) distribution of minimum acceptable gas prices, transactors will necessarily have a trade-off to make between lowering the gas price and maximising the chance that their transaction will be mined in a timely manner. ## Determining Computation Costs # Transaction Execution The execution of a transaction is the most complex part of the Ethereum protocol: it defines the state transition function $ \Upsilon $ . It is assumed that any transactions executed first pass the initial tests of intrinsic validity. These include: $$ \begin{enumerate} - The transaction is well-formed RLP, with no additional trailing bytes; - the transaction signature is valid; - the transaction nonce is valid (equivalent to the sender account's current nonce); - the gas limit is no smaller than the intrinsic gas, $ g_0 $ , used by the transaction; - the sender account balance contains at least the cost, $ v_0 $ , required in up-front payment. \end{enumerate} $$ Formally, we consider the function $ \Upsilon $ , with $ T $ being a transaction and $ \boldsymbol{\sigma} $ the state: $$ \begin{equation} \boldsymbol{\sigma}' = \Upsilon(\boldsymbol{\sigma}, T) \end{equation} $$ Thus $ \boldsymbol{\sigma}' $ is the post-transactional state. We also define $ \Upsilon^g $ to evaluate to the amount of gas used in the execution of a transaction and $ \Upsilon^\mathbf{l} $ to evaluate to the transaction's accrued log items, both to be formally defined later. ## 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) \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. For brevity, we define the empty substate $ A^0 $ to have no suicides, no logs and a zero refund balance: $$ \begin{equation} A^0 \equiv (\varnothing, (), 0) \end{equation} $$ ## Execution We define intrinsic gas $ g_0 $ , the amount of gas this transaction requires to be paid prior to execution, as follows: $$ \begin{equation} 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} + G_{transaction}\\ \end{equation} $$ 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 $ is defined in Appendix \ref{app:fees}. %todo Explain g_d reason? The up-front cost $ v_0 $ is calculated as: $$ \begin{equation} v_0 \equiv T_g T_p + T_v \end{equation} $$ The validity is determined as: $$ \begin{equation} \begin{array}[t]{rcl} 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 \\ 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 $ . 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 the up-front cost, $ v_0 $ . 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. We define the checkpoint state $ \boldsymbol{\sigma}_0 $ : $$ \begin{eqnarray} \boldsymbol{\sigma}_0 & \equiv & \boldsymbol{\sigma} \quad \text{except:} \\ \boldsymbol{\sigma}_0[S(T)]_b & \equiv & \boldsymbol{\sigma}[S(T)]_b - T_g T_p \\ \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 $ : $$ \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_\mathbf{d}, 0) & \text{otherwise} \end{cases} \end{equation} $$ where $ g $ is the amount of gas remaining after deducting the basic amount required to pay for the existence of the transaction: $$ \begin{equation} g \equiv T_g - g_0 \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. 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} g^* \equiv g' + \min \{ \Big\lfloor \dfrac{T_g - g'}{2} \Big\rfloor, A_r \} \end{equation} $$ The total refundable amount is the legitimately remaining gas $ g' $ , added to $ A_r $ , with the latter component being capped up to a maximum of half (rounded down) of the total amount used $ T_g - g' $ . The Ether for the gas is given to the miner, whose address is specified as the beneficiary of the present block $ B $ . So we define the pre-final state $ \boldsymbol{\sigma}^* $ in terms of the provisional state $ \boldsymbol{\sigma}_P $ : $$ \begin{eqnarray} \boldsymbol{\sigma}^* & \equiv & \boldsymbol{\sigma}_P \quad \text{except} \\ \boldsymbol{\sigma}^*[S(T)]_b & \equiv & \boldsymbol{\sigma}_P[S(T)]_b + g^* T_p \\ \boldsymbol{\sigma}^*[m]_b & \equiv & \boldsymbol{\sigma}_P[m]_b + (T_g - g^*) T_p \\ m & \equiv & {B_H}_c \end{eqnarray} $$ The final state, $ \boldsymbol{\sigma}' $ , is reached after deleting all accounts that appear in the suicide list: $$ \begin{eqnarray} \boldsymbol{\sigma}' & \equiv & \boldsymbol{\sigma}^* \quad \text{except} \\ \forall i \in A_\mathbf{s}: \boldsymbol{\sigma}'[i] & \equiv & \varnothing \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: \begin{eqnarray} \Upsilon^g(\boldsymbol{\sigma}, T) & \equiv & T_g - g' \\ \Upsilon^\mathbf{l}(\boldsymbol{\sigma}, T) & \equiv & A_\mathbf{l} \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} # Contract Creation There are 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}: \begin{equation} (\boldsymbol{\sigma}', g', A) \equiv \Lambda(\boldsymbol{\sigma}, s, o, g, p, v, \mathbf{i}, e) \end{equation} The address of the new account is defined as being the rightmost 160 bits of the Keccak hash of RLP encoding of the structure containing only the sender and the nonce. Thus we define the resultant address for the new account $ a $ : \begin{equation} a \equiv \mathcal{B}_{96..255}\Big(\mathtt{\tiny KEC}\Big(\mathtt{\tiny RLP}\big(\;(s, \boldsymbol{\sigma}[s]_n - 1)\;\big)\Big)\Big) \end{equation} 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}^* $ : \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 \end{eqnarray} where $ v' $ is the account's pre-existing value, in the event it was previously in existence: \begin{equation} v' \equiv \begin{cases} 0 & \text{if} \quad \boldsymbol{\sigma}[a] = \varnothing\\ \boldsymbol{\sigma}[a]_b & \text{otherwise} \end{cases} \end{equation} %It is asserted that the state database will also change such that it defines the pair $ (\mathtt{\tiny KEC}(\mathbf{b}), \mathbf{b}) $ . 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{b} $ . Code execution depletes gas; thus it 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. The gas remaining should be zero in any such exceptional condition. If the creation was conducted as the reception of a transaction, then this doesn't affect payment of the intrinsic cost: it is paid regardless. If such an exception does not occur, then the remaining gas is refunded to the originator and the now-altered state is allowed to persevere. Thus formally, we may specify the resultant state, gas and substate as $ (\boldsymbol{\sigma}', g', A) $ where: \begin{equation} (\boldsymbol{\sigma}^{**}, g^{**}, A, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}^*, g, I) \\ \end{equation} \begin{eqnarray} \quad g' & \equiv & \begin{cases} 0 & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \\ g^{**} & \text{if} \quad g^{**} < c \\ g^{**} - c & \text{otherwise} \\ \end{cases} \\ \quad \boldsymbol{\sigma}' & \equiv & \begin{cases} \boldsymbol{\sigma} & \text{if} \quad \boldsymbol{\sigma}^{**} = \varnothing \\ \boldsymbol{\sigma}^{**} & \text{if} \quad g^{**} < c \\ \boldsymbol{\sigma}^{**} \quad \text{except:} & \\ \quad\boldsymbol{\sigma}'[a]_c = \small\texttt{KEC}(\mathbf{o}) & \text{otherwise} \end{cases} \end{eqnarray} Where $ I $ contains the parameters of the execution environment as defined in section \ref{ch:model}. \begin{eqnarray} I_a & \equiv & a \\ I_o & \equiv & o \\ I_p & \equiv & p \\ I_\mathbf{d} & \equiv & () \\ I_s & \equiv & s \\ I_v & \equiv & v \\ I_\mathbf{b} & \equiv & \mathbf{i} \\ I_e & \equiv & e \end{eqnarray} where $ c $ is the code-deposit cost: \begin{equation} c \equiv G_{codedeposit} \times |\mathbf{o}| \end{equation} $ 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. The exception in the determination of $ \boldsymbol{\sigma}' $ dictates that the resultant byte sequence from the execution of the initialisation code specifies the final body code for the newly-created account, with $ \boldsymbol{\sigma}'[a]_c $ being the Keccak 256-bit hash of the newly created account's body code and $ \mathbf{o} $ the output byte sequence of the code execution. No code is deposited in the state if the gas does not cover the additional per-byte contract deposit fee, however, the value is still transferred and the execution side-effects take place. ## 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. # Message 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, \mathbf{d}, e) \end{equation} We define $ \boldsymbol{\sigma}_1 $ , the first transitional state as the original state but with the value transferred from sender to recipient: \begin{equation} \boldsymbol{\sigma}_1[r]_b \equiv \boldsymbol{\sigma}[r]_b + v \quad\wedge\quad \boldsymbol{\sigma}_1[s]_b \equiv \boldsymbol{\sigma}[s]_b - v \end{equation} Throughout the present work, it is assumed that if $ \boldsymbol{\sigma}_1[r] $ was originally undefined, it will be created as an account with no code or state and zero balance and nonce. Thus the previous equation should be taken to mean: \begin{equation} \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 \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} \end{cases} \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} $ ). \begin{eqnarray} \boldsymbol{\sigma}' & \equiv & \begin{cases} \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 a = 1 \\ \Xi_{\mathtt{SHA256}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad a = 2 \\ \Xi_{\mathtt{RIP160}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad a = 3 \\ \Xi_{\mathtt{ID}}(\boldsymbol{\sigma}_1, g, I) & \text{if} \quad a = 4 \\ \Xi(\boldsymbol{\sigma}_1, g, I) & \text{otherwise} \end{cases} \\ I_a & \equiv & a \\ I_o & \equiv & o \\ I_p & \equiv & p \\ I_d & \equiv & d \\ I_\mathbf{d} & \equiv & \mathbf{d} \\ I_s & \equiv & s \\ I_v & \equiv & v \\ I_e & \equiv & e \\ \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. Their full formal definition is in Appendix \ref{app:precompiled}. # Execution Model The execution model specifies how the system state is altered given a series of bytecode instructions and a small tuple of environmental data. This is specified through a formal model of a virtual state machine, known as the Ethereum Virtual Machine (EVM). It is a \textit{quasi-}Turing-complete machine; the \textit{quasi} qualification comes from the fact that the computation is intrinsically bounded through a parameter, \textit{gas}, which limits the total amount of computation done. ## Basics The EVM is a simple stack-based architecture. The word size of the machine (and thus size of stack item) is 256-bit. This was chosen to facilitate the Keccak-256 hash scheme and elliptic-curve computations. The memory model is a simple word-addressed byte array. The stack has a maximum size of $ 1024 $ . The machine also has an independent storage model; this is similar in concept to the memory but rather than a byte array, it is a word-addressable word array. Unlike memory, which is volatile, storage is non volatile and is maintained as part of the system state. All locations in both storage and memory are well-defined initially as zero. 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. ## Fees Overview Fees (denominated in gas) are charged under three distinct circumstances, all three as prerequisite to the execution of an operation. The first and most common is the fee intrinsic to the computation of the operation (see Appendix \ref{app:fees}). Secondly, gas may be deducted in order to form the payment for a subordinate message call or contract creation; this forms part of the payment for {\small CREATE}, {\small CALL} and {\small CALLCODE}. Finally, gas may be paid due to an increase in the usage of the memory. Over an account's execution, the total fee for memory-usage payable is proportional to smallest multiple of 32 bytes that are required such that all memory indices (whether for read or write) are included in the range. This is paid for on a just-in-time basis; as such, referencing an area of memory at least 32 bytes greater than any previously indexed memory will certainly result in an additional memory usage fee. Due to this fee it is highly unlikely addresses will ever go above 32-bit bounds. That said, implementations must be able to manage this eventuality. 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} %- {\small SHA3} costs $ G_{sha3} $ gas %- {\small SLOAD} costs $ G_{sload} $ gas %- {\small BALANCE} costs $ G_{balance} $ gas %- {\small SSTORE} costs $ d.G_{sstore} $ gas where: %\begin{itemize} %- $ d = 2 $ if the new value of the storage is non-zero and the old is zero; %- $ d = 0 $ if the new value of the storage is zero and the old is non-zero; %- $ d = 1 $ otherwise. %\end{itemize} %- {\small CALL} costs $ G_{call} $ , though additional gas may be taken for the execution of the account's associated code, if non-empty. %- {\small CREATE} costs $ G_{create} $ , though additional gas may be taken for the execution of the account initialisation code. %- {\small STOP} costs $ G_{stop} $ gas %- {\small SUICIDE} costs $ G_{suicide} $ gas %- 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. ## 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 $ : \begin{itemize} - $ I_a $ , the address of the account which owns the code that is executing. - $ I_o $ , the sender address of the transaction that originated this execution. - $ I_p $ , the price of gas in the transaction that originated this execution. - $ I_\mathbf{d} $ , the byte array that is the input data to this execution; if the execution agent is a transaction, this would be the transaction data. - $ I_s $ , the address of the account which caused the code to be executing; if the execution agent is a transaction, this would be the transaction sender. - $ I_v $ , the value, in Wei, passed to this account as part of the same procedure as execution; if the execution agent is a transaction, this would be the transaction value. - $ I_\mathbf{b} $ , the byte array that is the machine code to be executed. - $ I_H $ , the block header of the present block. - $ 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). \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: \begin{equation} (\boldsymbol{\sigma}', g', \mathbf{s}, \mathbf{l}, r, \mathbf{o}) \equiv \Xi(\boldsymbol{\sigma}, g, I) \end{equation} ## Execution Overview We must now define the $ \Xi $ function. In most practical implementations this will be modelled as an iterative progression of the pair comprising the full system state, $ \boldsymbol{\sigma} $ and the machine state, $ \boldsymbol{\mu} $ . Formally, we define it recursively with a function $ X $ . This uses an iterator function $ O $ (which defines the result of a single cycle of the state machine) together with functions $ Z $ which determines if the present state is an exceptional halting state of the machine and $ H $ , specifying the output data of the instruction if and only if the present state is a normal halting state of the machine. 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) \\ \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 & () \end{eqnarray} \begin{equation} 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}\\ \end{cases} \end{equation} where \begin{eqnarray} \mathbf{o} & \equiv & H(\boldsymbol{\mu}, I) \\ (a, b, c) \cdot d & \equiv & (a, b, c, d) \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} $ . $ 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. ## Machine State The machine state $ \boldsymbol{\mu} $ is defined as the tuple $ (g, pc, \mathbf{m}, i, \mathbf{s}) $ which are the gas available, the program counter $ pc \in \mathbb{P}_{256} $ , the memory contents, the active number of words in memory (counting continuously from position 0), and the stack contents. The memory contents $ \boldsymbol{\mu}_\mathbf{m} $ are a series of zeroes of size $ 2^{256} $ . For the ease of reading, the instruction mnemonics, written in small-caps (e.g. \space {\small ADD}), should be interpreted as their numeric equivalents; the full table of instructions and their specifics is given in Appendix \ref{app:vm}. For the purposes of defining $ Z $ , $ H $ and $ O $ , we define $ w $ as the current operation to be executed: \begin{equation}\label{eq:currentoperation} w \equiv \begin{cases} I_\mathbf{b}[\boldsymbol{\mu}_{pc}] & \text{if} \quad \boldsymbol{\mu}_{pc} < \lVert I_\mathbf{b} \rVert \\ \small\text{STOP} & \text{otherwise} \end{cases} \end{equation} We also assume the fixed amounts of $ \mathbf{\delta} $ and $ \mathbf{\alpha} $ , specifying the stack items removed and added, both subscriptable on the instruction and an instruction cost function $ C $ evaluating to the full cost, in gas, of executing the given instruction. ## Exceptional Halting The exceptional halting function $ Z $ is defined as: \begin{equation} Z(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \equiv \begin{array}[t]{l} \boldsymbol{\mu}_g < C(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \quad \vee \\ \mathbf{\delta}_w = \varnothing \quad \vee \\ \lVert\boldsymbol{\mu}_\mathbf{s}\rVert < \mathbf{\delta}_w \quad \vee \\ ( w \in \{ \small\text{JUMP}, \small\text{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 \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. ## Jump Destination Validity We previously used $ D $ as the function to determine the set of valid jump destinations given the code that is being run. We define this as any position in the code occupied by a {\small JUMPDEST} instruction. All such positions must be on valid instruction boundaries, rather than sitting in the data portion of {\small PUSH} operations and must appear within the explicitly defined portion of the code (rather than in the implicitly defined {\small STOP} operations that trail it). Formally: \begin{equation} D(\mathbf{c}) \equiv D_J(\mathbf{c, 0}) \cap Y(\mathbf{c}, 0) \end{equation} where: \begin{equation} Y(\mathbf{c}, i) \equiv \begin{cases} \{\} & \text{if} \quad i \geqslant |\mathbf{c}| \\ \{ i \} \cup Y(\mathbf{c}, N(i, \mathbf{c}[i])) & \text{otherwise} \\ \end{cases} \end{equation} \begin{equation} D_J(\mathbf{c}, i) \equiv \begin{cases} \{\} & \text{if} \quad i \geqslant |\mathbf{c}| \\ \{ i \} \cup D_J(\mathbf{c}, N(i, \mathbf{c}[i])) & \text{if} \quad \mathbf{c}[i] = \small\text{JUMPDEST} \\ D_J(\mathbf{c}, N(i, \mathbf{c}[i])) & \text{otherwise} \\ \end{cases} \end{equation} where $ N $ is the next valid instruction position in the code, skipping the data of a {\small PUSH} instruction, if any: \begin{equation} N(i, w) \equiv \begin{cases} i + w - \small\text{PUSH1} + 2 & \text{if} \quad w \in [\small\text{PUSH1}, \small\text{PUSH32}] \\ i + 1 & \text{otherwise} \end{cases} \end{equation} ## Normal Halting The normal halting function $ H $ is defined: \begin{equation} H(\boldsymbol{\mu}, I) \equiv \begin{cases} H_{\tiny\text{RETURN}}(\boldsymbol{\mu}) & \text{if} \quad w = \small\text{RETURN} \\ () & \text{if} \quad w \in \{ \small\text{STOP}, \small\text{SUICIDE} \} \\ \varnothing & \text{otherwise} \end{cases} \end{equation} The data-returning halt operation, \small\text{RETURN}, has a special function $ H_{\tiny\text{RETURN}} $ , defined in Appendix \ref{app:vm}. ## The Execution Cycle Stack items are added or removed from the left-most, lower-indexed portion of the series; all other items remain unchanged: \begin{eqnarray} 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] \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: \begin{eqnarray} \quad \boldsymbol{\mu}'_{g} & \equiv & \boldsymbol{\mu}_{g} - C(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \\ \quad \boldsymbol{\mu}'_{pc} & \equiv & \begin{cases} J_{\text{JUMP}}(\boldsymbol{\mu}) & \text{if} \quad w = \small\text{JUMP} \\ J_{\text{JUMPI}}(\boldsymbol{\mu}) & \text{if} \quad w = \small\text{JUMPI} \\ N(\boldsymbol{\mu}_{pc}, w) & \text{otherwise} \end{cases} \end{eqnarray} In general, we assume the memory, suicide list and system state don't change: \begin{eqnarray} \boldsymbol{\mu}'_\mathbf{m} & \equiv & \boldsymbol{\mu}_\mathbf{m} \\ \boldsymbol{\mu}'_i & \equiv & \boldsymbol{\mu}_i \\ A' & \equiv & A \\ \boldsymbol{\sigma}' & \equiv & \boldsymbol{\sigma} \end{eqnarray} However, instructions do typically alter one or several components of these values. Altered components listed by instruction are noted in Appendix \ref{app:vm}, alongside values for $ \alpha $ and $ \delta $ and a formal description of the gas requirements. # Blocktree to Blockchain (ghost protocol) The canonical blockchain is a path from root to leaf through the entire block tree. In order to have consensus over which path it is, conceptually we identify the path that has had the most computation done upon it, or, the \textit{heaviest} path. Clearly one factor that helps determine the heaviest path is the block number of the leaf, equivalent to the number of blocks, not counting the unmined genesis block, in the path. The longer the path, the greater the total mining effort that must have been done in order to arrive at the leaf. This is akin to existing schemes, such as that employed in Bitcoin-derived protocols. Since a block header includes the difficulty, the header alone is enough to validate the computation done. Any block contributes toward the total computation or \textit{total difficulty} of a chain. Thus we define the total difficulty of block $ B $ recursively as: \begin{eqnarray} B_t & \equiv & B'_t + B_d \\ B' & \equiv & P(B_H) \end{eqnarray} As such given a block $ B $ , $ B_t $ is its total difficulty, $ B' $ is its parent block and $ B_d $ is its difficulty. # Block Finalisation The process of finalising a block involves four stages: \begin{enumerate} - Validate (or, if mining, determine) ommers; - validate (or, if mining, determine) transactions; - apply rewards; - verify (or, if mining, compute a valid) state and nonce. \end{enumerate} ## Ommer Validation The validation of ommer headers means nothing more than verifying that each ommer header is both a valid header and satisfies the relation of $ N $ th-generation ommer to the present block where $ N \leq 6 $ . The maximum of ommer headers is two. Formally: \begin{equation} \lVert B_\mathbf{U} \rVert \leqslant 2 \bigwedge_{U \in B_\mathbf{U}} V(U) \; \wedge \; k(U, P(B_H), 6) \end{equation} where $ k $ denotes the ``is-kin'' property: \begin{equation} k(U, H, n) \equiv \begin{cases} false & \text{if} \quad n = 0 \\ s(U, H) &\\ \quad \vee \; k(U, P(H), n - 1) & \text{otherwise} \end{cases} \end{equation} and $ s $ denotes the ``is-sibling'' property: \begin{equation} s(U, H) \equiv (P(H) = P(U)\; \wedge \; H \neq U \; \wedge \; U \notin B(H)_\mathbf{U}) \end{equation} where $ B(H) $ is the block of the corresponding header $ H $ . ## Transaction Validation %where $ s[i] $ equals the root of the state trie immediately after the execution of the transaction $ B_\mathbf{T}[i] $ , and $ g[i] $ the total gas used immediately after said transaction. The given \textbf{gasUsed} must correspond faithfully to the transactions listed: $ {B_H}_u $ , the total gas used in the block, must be equal to the accumulated gas used according to the final transaction: \begin{equation} {B_H}_g = \ell(\mathbf{R})_u \end{equation} ## 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} \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{|B_\mathbf{U}|}{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 \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: \begin{equation} \text{Let} \quad R_b = 5 \times 10^{18} \end{equation} ## State & Nonce Validation We may now define the function, $ \Gamma $ , that maps a block $ B $ to its initiation state: \begin{equation} \Gamma(B) \equiv \begin{cases} \boldsymbol{\sigma}_0 & \text{if} \quad P(B_H) = \varnothing \\ \boldsymbol{\sigma}_i: \mathtt{\small TRIE}(L_S(\boldsymbol{\sigma}_i)) = {P(B_H)_H}_r & \text{otherwise} \end{cases} \end{equation} Here, $ \mathtt{\small TRIE}(L_S(\boldsymbol{\sigma}_i)) $ means the hash of the root node of a trie of state $ \boldsymbol{\sigma}_i $ ; it is assumed that implementations will store this in the state database, trivial and efficient since the trie is by nature an immutable data structure. And finally define $ \Phi $ , the block transition function, which maps an incomplete block $ B $ to a complete block $ B' $ : \begin{eqnarray} \Phi(B) & \equiv & B': \quad B' = B^* \quad \text{except:} \\ B'_n & = & n: \quad x \leqslant \frac{2^{256}}{H_d} \\ B'_m & = & m \quad \text{with } (x, m) = \mathtt{PoW}(B^*_{\hcancel{n}}, n, \mathbf{d}) \\ B^* & \equiv & B \quad \text{except:} \quad B'_r = r(\Pi(\Gamma(B), B)) \end{eqnarray} With $ \mathbf{d} $ being a dataset as specified in appendix \ref{app:ethash}. 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): \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} \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 \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]) \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}} $ : \begin{equation} \Pi(\boldsymbol{\sigma}, B) \equiv \Omega(B, \ell(\mathbf{R})_{\boldsymbol{\sigma}}) \end{equation} Thus the complete block-transition mechanism, less $ \mathtt{PoW} $ , the proof-of-work function is defined. ## Mining Proof-of-Work The mining proof-of-work (PoW) exists as a cryptographically secure nonce that proves beyond reasonable doubt that a particular amount of computation has been expended in the determination of some token value $ n $ . It is utilised to enforce the blockchain security by giving meaning and credence to the notion of difficulty (and, by extension, total difficulty). However, since mining new blocks comes with an attached reward, the proof-of-work not only functions as a method of securing confidence that the blockchain will remain canonical into the future, but also as a wealth distribution mechanism. For both reasons, there are two important goals of the proof-of-work function; firstly, it should be as accessible as possible to as many people as possible. The requirement of, or reward from, specialised and uncommon hardware should be minimised. This makes the distribution model as open as possible, and, ideally, makes the act of mining a simple swap from electricity to Ether at roughly the same rate for anyone around the world. Secondly, it should not be possible to make super-linear profits, and especially not so with a high initial barrier. Such a mechanism allows a well-funded adversary to gain a troublesome amount of the network's total mining power and as such gives them a super-linear reward (thus skewing distribution in their favour) as well as reducing the network security. One plague of the Bitcoin world is ASICs. These are specialised pieces of compute hardware that exist only to do a single task. In Bitcoin's case the task is the SHA256 hash function. While ASICs exist for a proof-of-work function, both goals are placed in jeopardy. Because of this, a proof-of-work function that is ASIC-resistant (i.e. difficult or economically inefficient to implement in specialised compute hardware) has been identified as the proverbial silver bullet. Two directions exist for ASIC resistance; firstly make it sequential memory-hard, i.e. engineer the function such that the determination of the nonce requires a lot of memory and bandwidth such that the memory cannot be used in parallel to discover multiple nonces simultaneously. The second is to make the type of computation it would need to do general-purpose; the meaning of ``specialised hardware'' for a general-purpose task set is, naturally, general purpose hardware and as such commodity desktop computers are likely to be pretty close to ``specialised hardware'' for the task. For Ethereum 1.0 we have choosen the first path. More formally, the proof-of-work function takes the form of $ \mathtt{PoW} $ : \begin{equation} m = H_m \quad \wedge \quad n \leqslant \frac{2^{256}}{H_d} \quad \text{with} \quad (m, n) = \mathtt{PoW}(H_{\hcancel{n}}, H_n, \mathbf{d}) \end{equation} 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: 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 intial 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. Mining involves grabbing random slices of the dataset and hashing them together. Verification can be done with low memory by using the cache to regenerate the specific pieces of the dataset that you need, so you only need to store the cache. The large dataset is updated once every $ J_{epoch} $ blocks, so the vast majority of a miner's effort will be reading the dataset, not making changes to it. The mentioned parameters as well as the algorithm is explained in detail in appendix \ref{app:ethash}. # 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. ## 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. The general pattern involves a single contract within Ethereum which, when given a message call, replies with some timely information concerning an external phenomenon. An example might be the local temperature of New York City. This would be implemented as a contract that returned that value of some known point in storage. Of course this point in storage must be maintained with the correct such temperature, and thus the second part of the pattern would be for an external server to run an Ethereum node, and immediately on discovery of a new block, creates a new valid transaction, sent to the contract, updating said value in storage. The contract's code would accept such updates only from the identity contained on said server. ## Random Numbers Providing random numbers within a deterministic system is, naturally, an impossible task. However, we can approximate with pseudo-random numbers by utilising data which is generally unknowable at the time of transacting. Such data might include the block's hash, the block's timestamp and the block's beneficiary address. In order to make it hard for malicious miner to control those values, one should use the {\small BLOCKHASH} operation in order to use hashes of the previous 256 blocks as pseudo-random numbers. For a series of such numbers, a trivial solution would be to add some constant amount and hashing the result. # Future Directions 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. 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. ## Scalability Scalability remains an eternal concern. With a generalised state transition function, it becomes difficult to partition and parallelise transactions to apply the divide-and-conquer strategy. Unaddressed, the dynamic value-range of the system remains essentially fixed and as the average transaction value increases, the less valuable of them become ignored, being economically pointless to include in the main ledger. However, several strategies exist that may potentially be exploited to provide a considerably more scalable protocol. Some form of hierarchical structure, achieved by either consolidating smaller lighter-weight chains into the main block or building the main block through the incremental combination and adhesion (through proof-of-work) of smaller transaction sets may allow parallelisation of transaction combination and block-building. Parallelism could also come from a prioritised set of parallel blockchains, consolidated each block and with duplicate or invalid transactions thrown out accordingly. Finally, verifiable computation, if made generally available and efficient enough, may provide a route to allow the proof-of-work to be the verification of final state. # Conclusion I have introduced, discussed and formally defined the protocol of Ethereum. Through this protocol the reader may implement a node on the Ethereum network and join others in a decentralised secure social operating system. Contracts may be authored in order to algorithmically specify and autonomously enforce rules of interaction. # 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. \bibliography{Biblio} \bibliographystyle{plainnat} \end{multicols} \appendix # Terminology - [External Actor] A person or other entity able to interface to an Ethereum node, but external to the world of Ethereum. It can interact with Ethereum through depositing signed Transactions and inspecting the blockchain and associated state. Has one (or more) intrinsic Accounts. - [Address] A 160-bit code used for identifying Accounts. - [Account] Accounts have an intrinsic balance and transaction count maintained as part of the Ethereum state. They also have some (possibly empty) EVM Code and a (possibly empty) Storage State associated with them. Though homogenous, it makes sense to distinguish between two practical types of account: those with empty associated EVM Code (thus the account balance is controlled, if at all, by some external entity) and those with non-empty associated EVM Code (thus the account represents an Autonomous Object). Each Account has a single Address that identifies it. - [Transaction] A piece of data, signed by an External Actor. It represents either a Message or a new Autonomous Object. Transactions are recorded into each block of the blockchain. - [Autonomous Object] A notional object existent only within the hypothetical state of Ethereum. Has an intrinsic address and thus an associated account; the account will have non-empty associated EVM Code. Incorporated only as the Storage State of that account. - [Storage State] The information particular to a given Account that is maintained between the times that the Account's associated EVM Code runs. - [Message] Data (as a set of bytes) and Value (specified as Ether) that is passed between two Accounts, either through the deterministic operation of an Autonomous Object or the cryptographically secure signature of the Transaction. - [Message Call] The act of passing a message from one Account to another. If the destination account is associated with non-empty EVM Code, then the VM will be started with the state of said Object and the Message acted upon. If the message sender is an Autonomous Object, then the Call passes any data returned from the VM operation. - [Gas] The fundamental network cost unit. Paid for exclusively by Ether (as of PoC-4), which is converted freely to and from Gas as required. Gas does not exist outside of the internal Ethereum computation engine; its price is set by the Transaction and miners are free to ignore Transactions whose Gas price is too low. - [Contract] Informal term used to mean both a piece of EVM Code that may be associated with an Account or an Autonomous Object. - [Object] Synonym for Autonomous Object. - [App] An end-user-visible application hosted in the Ethereum Browser. - [Ethereum Browser] (aka Ethereum Reference Client) A cross-platform GUI of an interface similar to a simplified browser (a la Chrome) that is able to host sandboxed applications whose backend is purely on the Ethereum protocol. - [Ethereum Virtual Machine] (aka EVM) The virtual machine that forms the key part of the execution model for an Account's associated EVM Code. - [Ethereum Runtime Environment] (aka ERE) The environment which is provided to an Autonomous Object executing in the EVM. Includes the EVM but also the structure of the world state on which the EVM relies for certain I/O instructions including CALL \& CREATE. - [EVM Code] The bytecode that the EVM can natively execute. Used to formally specify the meaning and ramifications of a message to an Account. - [EVM Assembly] The human-readable form of EVM-code. - [LLL] The Lisp-like Low-level Language, a human-writable language used for authoring simple contracts and general low-level language toolkit for trans-compiling to. # Recursive Length Prefix (RLP) This is a serialisation method for encoding arbitrarily structured binary data (byte arrays). We define the set of possible structures $ \mathbb{T} $ : \begin{eqnarray} \mathbb{T} & \equiv & \mathbb{L} \cup \mathbb{B} \\ \mathbb{L} & \equiv & \{ \mathbf{t}: \mathbf{t} = ( \mathbf{t}[0], \mathbf{t}[1], ... ) \; \wedge \; \forall_{n < \lVert \mathbf{t} \rVert} \; \mathbf{t}[n] \in \mathbb{T} \} \\ \mathbb{B} & \equiv & \{ \mathbf{b}: \mathbf{b} = ( \mathbf{b}[0], \mathbf{b}[1], ... ) \; \wedge \; \forall_{n < \lVert \mathbf{b} \rVert} \; \mathbf{b}[n] \in \mathbb{Y} \} \end{eqnarray} Where $ \mathbb{Y} $ is the set of bytes. Thus $ \mathbb{B} $ is the set of all sequences of bytes (otherwise known as byte-arrays, and a leaf if imagined as a tree), $ \mathbb{L} $ is the set of all tree-like (sub-)structures that are not a single leaf (a branch node if imagined as a tree) and $ \mathbb{T} $ is the set of all byte-arrays and such structural sequences. We define the RLP function as $ \mathtt{\tiny RLP} $ through two sub-functions, the first handling the instance when the value is a byte array, the second when it is a sequence of further values: \begin{equation} \mathtt{\tiny RLP}(\mathbf{x}) \equiv \begin{cases} R_b(\mathbf{x}) & \text{if} \quad \mathbf{x} \in \mathbb{B} \\ R_l(\mathbf{x}) & \text{otherwise} \end{cases} \end{equation} If the value to be serialised is a byte-array, the RLP serialisation takes one of three forms: \begin{itemize} - If the byte-array contains solely a single byte and that single byte is less than 128, then the input is exactly equal to the output. - If the byte-array contains fewer than 56 bytes, then the output is equal to the input prefixed by the byte equal to the length of the byte array plus 128. - Otherwise, the output is equal to the input prefixed by the minimal-length byte-array which when interpreted as a big-endian integer is equal to the length of the input byte array, which is itself prefixed by the number of bytes required to faithfully encode this length value plus 183. \end{itemize} Formally, we define $ R_b $ : \begin{eqnarray} R_b(\mathbf{x}) & \equiv & \begin{cases} \mathbf{x} & \text{if} \quad \lVert \mathbf{x} \rVert = 1 \wedge \mathbf{x}[0] < 128 \\ (128 + \lVert \mathbf{x} \rVert) \cdot \mathbf{x} & \text{else if} \quad \lVert \mathbf{x} \rVert < 56 \\ \big(183 + \big\lVert \mathtt{\tiny BE}(\lVert \mathbf{x} \rVert) \big\rVert \big) \cdot \mathtt{\tiny BE}(\lVert \mathbf{x} \rVert) \cdot \mathbf{x} & \text{otherwise} \end{cases} \\ \mathtt{\tiny BE}(x) & \equiv & (b_0, b_1, ...): b_0 \neq 0 \wedge x = \sum_{n = 0}^{n < \lVert \mathbf{b} \rVert - 1} b_n \cdot 256^{\lVert \mathbf{b} \rVert - 1 - n} \\ (a) \cdot (b, c) \cdot (d, e) & = & (a, b, c, d, e) \end{eqnarray} Thus $ \mathtt{\tiny BE} $ is the function that expands a positive integer value to a big-endian byte array of minimal length and the dot operator performs sequence concatenation. If instead, the value to be serialised is a sequence of other items then the RLP serialisation takes one of two forms: \begin{itemize} - If the concatenated serialisations of each contained item is less than 56 bytes in length, then the output is equal to that concatenation prefixed by the byte equal to the length of this byte array plus 192. - Otherwise, the output is equal to the concatenated serialisations prefixed by the minimal-length byte-array which when interpreted as a big-endian integer is equal to the length of the concatenated serialisations byte array, which is itself prefixed by the number of bytes required to faithfully encode this length value plus 247. \end{itemize} Thus we finish by formally defining $ R_l $ : \begin{eqnarray} R_l(\mathbf{x}) & \equiv & \begin{cases} (192 + \lVert s(\mathbf{x}) \rVert) \cdot s(\mathbf{x}) & \text{if} \quad \lVert s(\mathbf{x}) \rVert < 56 \\ \big(247 + \big\lVert \mathtt{\tiny BE}(\lVert s(\mathbf{x}) \rVert) \big\rVert \big) \cdot \mathtt{\tiny BE}(\lVert s(\mathbf{x}) \rVert) \cdot s(\mathbf{x}) & \text{otherwise} \end{cases} \\ s(\mathbf{x}) & \equiv & \mathtt{\tiny RLP}(\mathbf{x}_0) \cdot \mathtt{\tiny RLP}(\mathbf{x}_1) ... \end{eqnarray} If RLP is used to encode a scalar, defined only as a positive integer ( $ \mathbb{P} $ or any $ x $ for $ \mathbb{P}_x $ ), it must be specified as the shortest byte array such that the big-endian interpretation of it is equal. Thus the RLP of some positive integer $ i $ is defined as: $$ \begin{equation} \mathtt{\tiny RLP}(i : i \in \mathbb{P}) \equiv \mathtt{\tiny RLP}(\mathtt{\tiny BE}(i)) \end{equation} $$ When interpreting RLP data, if an expected fragment is decoded as a scalar and leading zeroes are found in the byte sequence, clients are required to consider it non-canonical and treat it in the same manner as otherwise invalid RLP data, dismissing it completely. There is no specific canonical encoding format for signed or floating-point values. # Hex-Prefix Encoding Hex-prefix encoding is an efficient method of encoding an arbitrary number of nibbles as a byte array. It is able to store an additional flag which, when used in the context of the trie (the only context in which it is used), disambiguates between node types. It is defined as the function $ \mathtt{\tiny HP} $ which maps from a sequence of nibbles (represented by the set $ \mathbb{Y} $ ) together with a boolean value to a sequence of bytes (represented by the set $ \mathbb{B} $ ): $$ \begin{eqnarray} \mathtt{\tiny HP}(\mathbf{x}, t): \mathbf{x} \in \mathbb{Y} & \equiv & \begin{cases} (16f(t), 16\mathbf{x}[0] + \mathbf{x}[1], 16\mathbf{x}[2] + \mathbf{x}[3], ...) & \text{if} \quad \lVert \mathbf{x} \rVert \; \text{is even} \\ (16(f(t) + 1) + \mathbf{x}[0], 16\mathbf{x}[1] + \mathbf{x}[2], 16\mathbf{x}[3] + \mathbf{x}[4], ...) & \text{otherwise} \end{cases} \\ f(t) & \equiv & \begin{cases} 2 & \text{if} \quad t \neq 0 \\ 0 & \text{otherwise} \end{cases} \end{eqnarray} $$ 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. # Modified Merkle Patricia Tree 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 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. Formally, we assume the input value $ \mathfrak{I} $ , a set containing pairs of byte sequences: \begin{equation} \mathfrak{I} = \{ (\mathbf{k}_0 \in \mathbb{B}, \mathbf{v}_0 \in \mathbb{B}), (\mathbf{k}_1 \in \mathbb{B}, \mathbf{v}_1 \in \mathbb{B}), ... \} \end{equation} When considering such a sequence, we use the common numeric subscript notation to refer to a tuple's key or value, thus: \begin{equation} \forall_{I \in \mathfrak{I}} I \equiv (I_0, I_1) \end{equation} Any series of bytes may also trivially be viewed as a series of nibbles, given an endian-specific notation; here we assume big-endian. Thus: \begin{eqnarray} y(\mathfrak{I}) & = & \{ (\mathbf{k}_0' \in \mathbb{Y}, \mathbf{v}_0 \in \mathbb{B}), (\mathbf{k}_1' \in \mathbb{Y}, \mathbf{v}_1 \in \mathbb{B}), ... \} \\ \forall_n \quad \forall_{i: i < 2\lVert\mathbf{k}_n\rVert} \quad \mathbf{k}_n'[i] & \equiv & \begin{cases} \lfloor \mathbf{k}_n[i \div 2] \div 16 \rfloor & \text{if} \; i \; \text{is even} \\ \mathbf{k}_n[\lfloor i \div 2 \rfloor] \bmod 16 & \text{otherwise} \end{cases} \end{eqnarray} We define the function $ \small\texttt{TRIE} $ , which evaluates to the root of the trie that represents this set when encoded in this structure: \begin{equation} \small\texttt{TRIE}(\mathfrak{I}) \equiv \small\texttt{KEC}(c(\mathfrak{I}, 0)) \end{equation} We also assume a function $ n $ , the trie's node cap function. When composing a node, we use RLP to encode the structure. As a means of reducing storage complexity, for nodes whose composed RLP is fewer than 32 bytes, we store the RLP directly; for those larger we assert prescience of the byte array whose Keccak hash evaluates to our reference. Thus we define in terms of $ c $ , the node composition function: \begin{equation} n(\mathfrak{I}, i) \equiv \begin{cases} () & \text{if} \quad \mathfrak{I} = \varnothing \\ c(\mathfrak{I}, i) & \text{if} \quad \lVert c(\mathfrak{I}, i)\rVert < 32 \\ \small\texttt{KEC}(c(\mathfrak{I}, i)) & \text{otherwise} \end{cases} \end{equation} 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 shared 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: - [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 $ . - [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 $ . - [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. A branch is then only used when necessary; no branch nodes may exist that contain only a single non-zero entry. We may formally define this structure with the structural composition function $ c $ : \begin{equation} c(\mathfrak{I}, i) \equiv \begin{cases} \small\texttt{RLP}\Big( \big(\small\texttt{HP}(I_0[i .. (\lVert I_0\rVert - 1)], true), I_1 \big) \Big) & \text{if} \quad \lVert \mathfrak{I} \rVert = 1 \quad \text{where} \; \exists I: I \in \mathfrak{I} \\ \small\texttt{RLP}\Big( \big(\small\texttt{HP}(I_0[i .. (j - 1)], false), n(\mathfrak{I}, j) \big) \Big) & \text{if} \quad i \ne j \quad \text{where} \; j = \arg \max_x : \exists \mathbf{l}: \lVert \mathbf{l} \rVert = x : \forall_{I \in \mathfrak{I}}: I_0[0 .. (x - 1)] = \mathbf{l} \\ \small\texttt{RLP}\Big( (u(0), u(1), ..., u(15), v) \Big) & \text{otherwise} \quad \text{where} \begin{array}[t]{rcl} u(j) & \equiv & n(\{ I : I \in \mathfrak{I} \wedge I_0[i] = j \}, i + 1) \\ v & = & \begin{cases} I_1 & \text{if} \quad \exists I: I \in \mathfrak{I} \wedge \lVert I_0 \rVert = i \\ () & \text{otherwise} \end{cases} \end{array} \end{cases} \end{equation} ## Trie Database Thus no explicit assumptions are made concerning what data is stored and what is not, since that is an implementation-specific consideration; we simply define the identity function mapping the key-value set $ \mathfrak{I} $ to a 32-byte hash and assert that only a single such hash exists for any $ \mathfrak{I} $ , which though not strictly true is accurate within acceptable precision given the Keccak hash's collision resistance. In reality, a sensible implementation will not fully recompute the trie root hash for each set. A reasonable implementation will maintain a database of nodes determined from the computation of various tries or, more formally, it will memoise the function $ c $ . This strategy uses the nature of the trie to both easily recall the contents of any previous key-value set and to store multiple such sets in a very efficient manner. Due to the dependency relationship, Merkle-proofs may be constructed with an $ O(\log N) $ space requirement that can demonstrate a particular leaf must exist within a trie of a given root hash. # Precompiled Contracts 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} (\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} The precompiled contracts each use these definitions and provide specifications for the $ \mathbf{o} $ (the output data) and $ g_r $ , the gas requirements. For the elliptic curve DSA recover VM execution function, we also define $ \mathbf{d} $ to be the input data, well-defined for an infinite length by appending zeroes as required. Importantly in the case of an invalid signature ( $ \mathtt{\tiny ECDSARECOVER}(h, v, r, s) = \varnothing $ ), then we have no output. \begin{eqnarray} \Xi_{\mathtt{ECREC}} &\equiv& \Xi_{\mathtt{PRE}} \quad \text{where:} \\ g_r &=& 3000\\ |\mathbf{o}| &=& \begin{cases} 0 & \text{if} \quad \mathtt{\tiny ECDSARECOVER}(h, v, r, s) = \varnothing\\ 32 & \text{otherwise} \end{cases}\\ \text{if} \quad |\mathbf{o}| = 32: &&\\ \mathbf{o}[0..11] &=& 0 \\ \mathbf{o}[12..31] &=& \mathtt{\tiny KEC}\big(\mathtt{\tiny ECDSARECOVER}(h, v, r, s)\big)[12..31] \quad \text{where:}\\ \mathbf{d}[0..(|I_\mathbf{d}|-1)] &=& I_\mathbf{d}\\ \mathbf{d}[|I_\mathbf{d}|..] &=& (0, 0, ...) \\ h &=& \mathbf{d}[0..31]\\ v &=& \mathbf{d}[32..63]\\ r &=& \mathbf{d}[64..95]\\ s &=& \mathbf{d}[96..127] \end{eqnarray} The two hash functions, RIPEMD-160 and SHA2-256 are more trivially defined as an almost pass-through operation. Their gas usage is dependent on the input data size, a factor rounded up to the nearest number of words. \begin{eqnarray} \Xi_{\mathtt{SHA256}} &\equiv& \Xi_{\mathtt{PRE}} \quad \text{where:} \\ g_r &=& 60 + 12\Big\lceil \dfrac{|I_\mathbf{d}|}{32} \Big\rceil\\ \mathbf{o}[0..31] &=& \mathtt{\tiny SHA256}(I_\mathbf{d})\\ \Xi_{\mathtt{RIP160}} &\equiv& \Xi_{\mathtt{PRE}} \quad \text{where:} \\ g_r &=& 600 + 120\Big\lceil \dfrac{|I_\mathbf{d}|}{32} \Big\rceil\\ \mathbf{o}[0..11] &=& 0 \\ \mathbf{o}[12..31] &=& \mathtt{\tiny RIPEMD160}(I_\mathbf{d})\\ \end{eqnarray} For the purposes here, we assume we have well-defined standard cryptographic functions for RIPEMD-160 and SHA2-256 of the form: \begin{eqnarray} \mathtt{\small SHA256}(\mathbf{i} \in \mathbb{B}) & \equiv & o \in \mathbb{B}_{32} \\ \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: \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} # signing Transactions The method of signing transactions is similar to the `Electrum style signatures'; it utilises the SECP-256k1 curve as described by \cite{gura2004comparing}. It is assumed that the sender has a valid private key $ p_r $ , a randomly selected positive integer % to avoid line break in range definition in the range $ (1, \mathtt{\tiny secp256k1n} - 1) $ represented as a byte array of length 32 in big-endian form. We assert the functions $ \mathtt{\small ECDSASIGN} $ , $ \mathtt{\small ECDSARESTORE} $ and $ \mathtt{\small ECDSAPUBKEY} $ . These are formally defined in the literature. \begin{eqnarray} \mathtt{\small ECDSAPUBKEY}(p_r \in \mathbb{B}_{32}) & \equiv & p_u \in \mathbb{B}_{64} \\ \mathtt{\small ECDSASIGN}(e \in \mathbb{B}_{32}, p_r \in \mathbb{B}_{32}) & \equiv & (v \in \mathbb{B}_{1}, r \in \mathbb{B}_{32}, s \in \mathbb{B}_{32}) \\ \mathtt{\small ECDSARECOVER}(e \in \mathbb{B}_{32}, v \in \mathbb{B}_{1}, r \in \mathbb{B}_{32}, s \in \mathbb{B}_{32}) & \equiv & p_u \in \mathbb{B}_{64} \end{eqnarray} Where $ p_u $ is the public key, assumed to be a byte array of size 64 (formed from the concatenation of two positive integers each $ < 2^{256} $ ) and $ p_r $ is the private key, a byte array of size 32 (or a single positive integer in the aforementioned range). It is assumed that $ v $ is the `recovery id', a 1 byte value specifying the sign and finiteness of the curve point; this value is in the range of $ [27, 30] $ , however we declare the upper two possibilities, representing infinite values, invalid. We declare that a signature is invalid unless the following is true: \begin{eqnarray} 0 < r < \mathtt{\tiny secp256k1n} \quad\wedge\quad \\ 0 < s < \mathtt{\tiny secp256k1n} \quad\wedge\quad \\ v \in \{27,28\} \end{eqnarray} where: \begin{eqnarray} \mathtt{\tiny secp256k1n} &=& 115792089237316195423570985008687907852837564279074904382605163141518161494337 %\mathtt{\tiny secp256k1p} &=& 2^{256} - 2^{32} - 977\\ \end{eqnarray} For a given private key, $ p_r $ , the Ethereum address $ A(p_r) $ (a 160-bit value) to which it corresponds is defined as the right most 160-bits of the Keccak hash of the corresponding ECDSA public key: \begin{equation} A(p_r) = \mathcal{B}_{96..255}\big(\mathtt{\tiny KEC}\big( \mathtt{\small ECDSAPUBKEY}(p_r) \big) \big) \end{equation} The message hash, $ h(T) $ , to be signed is the Keccak hash of the transaction without the latter three signature components, formally described as $ T_r $ , $ T_s $ and $ T_w $ : \begin{eqnarray} L_S(T) & \equiv & \begin{cases} (T_n, T_p, T_g, T_t, T_v, T_\mathbf{i}) & \text{if} \; T_t = 0\\ (T_n, T_p, T_g, T_t, T_v, T_\mathbf{d}) & \text{otherwise} \end{cases} \\ h(T) & \equiv & \mathtt{\small KEC}( L_S(T) ) \end{eqnarray} The signed transaction $ G(T, p_r) $ is defined as: \begin{eqnarray} G(T, p_r) \equiv T \quad \text{except:} \\ (T_w, T_r, T_s) = \mathtt{\small ECDSASIGN}(h(T), p_r) \end{eqnarray} We may then define the sender function $ S $ of the transaction as: \begin{equation} S(T) \equiv \mathcal{B}_{96..255}\big(\mathtt{\tiny KEC}\big( \mathtt{\small ECDSARECOVER}(h(T), T_w, T_r, T_s) \big) \big) \end{equation} The assertion that the sender of the a signed transaction equals the address of the signer should be self-evident: \begin{equation} \forall T: \forall p_r: S(G(T, p_r)) \equiv A(p_r) \end{equation} # Fee Schedule The fee schedule $ G $ is a tuple of 31 scalar values corresponding to the relative costs, in gas, of a number of abstract operations that a transaction may effect. $$ \begin{tabular*}{\columnwidth}[h]{lrl} \toprule Name & Value & Description* \\ \midrule $ G_{zero} $ & 0 & Nothing paid for operations of the set {\small $ W_{zero} $ }. \\ $ G_{base} $ & 2 & Amount of gas to pay for operations of the set {\small $ W_{base} $ }. \\ $ G_{verylow} $ & 3 & Amount of gas to pay for operations of the set {\small $ W_{verylow} $ }. \\ $ G_{low} $ & 5 & Amount of gas to pay for operations of the set {\small $ W_{low} $ }. \\ $ G_{mid} $ & 8 & Amount of gas to pay for operations of the set {\small $ W_{mid} $ }. \\ $ G_{high} $ & 10 & Amount of gas to pay for operations of the set {\small $ W_{high} $ }. \\ $ G_{ext} $ & 20 & Amount of gas to pay for operations of the set {\small $ W_{ext} $ }. \\ $ G_{sload} $ & 50 & Paid for a {\small SLOAD} operation. \\ $ G_{jumpdest} $ & 1 & Paid for a {\small JUMPDEST} operation. \\ $ G_{sset} $ & 20000 & Paid for an {\small SSTORE} operation when the storage value is set to non-zero from zero. \\ $ G_{sreset} $ & 5000 & Paid for an {\small SSTORE} operation when the storage value's zeroness remains unchanged or is set to zero. \\ $ R_{sclear} $ & 15000 & Refund given (added into refund counter) when the storage value is set to zero from non-zero. \\ $ R_{suicide} $ & 24000 & Refund given (added into refund counter) for suiciding an account. \\ $ G_{create} $ & 32000 & Paid for a {\small CREATE} operation. \\ $ G_{codedeposit} $ & 200 & Paid per byte for a {\small CREATE} operation to succeed in placing code into state. \\ $ G_{call} $ & 40 & Paid for a {\small CALL} operation. \\ $ G_{callvalue} $ & 9000 & Paid for a non-zero value transfer as part of the {\small CALL} operation. \\ $ G_{callstipend} $ & 2300 & A stipend for the called contract subtracted from $ G_{callvalue} $ for a non-zero value transfer. \\ $ G_{callnewaccount} $ & 25000 & Paid for a {\small CALL} operation to a not previously excisting account. \\ $ G_{exp} $ & 10 & Partial payment for an {\small EXP} operation. \\ $ G_{expbyte} $ & 10 & Partial payment when multiplied by $ \lceil\log_{256}(exponent)\rceil $ for the {\small EXP} operation. \\ $ G_{memory} $ & 3 & Paid for every additional word when expanding memory. \\ $ G_{txdatazero} $ & 4 & Paid for every zero byte of data or code for a transaction. \\ $ G_{txdatanonzero} $ & 68 & Paid for every non-zero byte of data or code for a transaction. \\ $ G_{transaction} $ & 21000 & Paid for every transaction. \\ $ G_{log} $ & 375 & Partial payment for a {\small LOG} operation. \\ $ G_{logdata} $ & 8 & Paid for each byte in a {\small LOG} operation's data. \\ $ G_{logtopic} $ & 375 & Paid for each topic of a {\small LOG} operation. \\ $ G_{sha3} $ & 30 & Paid for each {\small SHA3} operation. \\ $ G_{sha3word} $ & 6 & Paid for each word (rounded up) for input data to a {\small SHA3} operation. \\ $ G_{copy} $ & 3 & Partial payment for {\small *COPY} operations, multiplied by words copied, rounded up. \\ %extern u256 const c_copyGas; ///< Multiplied by the number of 32-byte words that are copied (round up) for any *COPY operation and added. \bottomrule \end{tabular*} $$ # Virtual Machine Specification When interpreting 256-bit binary values as integers, the representation is big-endian. When a 256-bit machine datum is converted to and from a 160-bit address or hash, the rightwards (low-order for BE) 20 bytes are used and the left most 12 are discarded or filled with zeroes, thus the integer values (when the bytes are interpreted as big-endian) are equivalent. ## Gas Cost The general gas cost function, $C$ , is defined as: $$ \begin{equation} C(\boldsymbol{\sigma}, \boldsymbol{\mu}, I) \equiv C_{memory}(\boldsymbol{\mu}' _ i)-C _ {memory}(\boldsymbol{\mu} _ i) + \begin{cases} C_\tiny\text{SSTORE}(\boldsymbol{\sigma}, \boldsymbol{\mu}) & \text{if} \quad w = \small\text{SSTORE} \\ G_{exp} & \text{if} \quad w = \small\text{EXP} \wedge \boldsymbol{\mu} _ \mathbf{s}[1] = 0 \\ G_{exp} + G_{expbyte}\times(1+\lfloor\log_{256}(\boldsymbol{\mu}_\mathbf{s}[1])\rfloor) & \text{if} \quad w = \small\text{EXP} \wedge \boldsymbol{\mu}_\mathbf{s}[1] > 0 \\ G_{verylow} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[2] \div 32\rceil & \text{if} \quad w = \small\text{CALLDATACOPY} \lor \small\text{CODECOPY} \\ G_{ext} + G_{copy}\times\lceil\boldsymbol{\mu}_\mathbf{s}[3] \div 32\rceil & \text{if} \quad w = \small\text{EXTCODECOPY} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1] & \text{if} \quad w = \small\text{LOG0} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+G_{logtopic} & \text{if} \quad w = \small\text{LOG1} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+2G_{logtopic} & \text{if} \quad w = \small\text{LOG2} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+3G_{logtopic} & \text{if} \quad w = \small\text{LOG3} \\ G_{log}+G_{logdata}\times\boldsymbol{\mu}_\mathbf{s}[1]+4G_{logtopic} & \text{if} \quad w = \small\text{LOG4} \\ C_\tiny\text{CALL}(\boldsymbol{\sigma}, \boldsymbol{\mu}) & \text{if} \quad w = \small\text{CALL} \lor \small\text{CALLCODE} \\ G_{create} & \text{if} \quad w = \small\text{CREATE}\\ G_{sha3}+G_{sha3word} \lceil \mathbf{s}[1] \div 32 \rceil & \text{if} \quad w = \small\text{SHA3}\\ G_{jumpdest} & \text{if} \quad w = \small\text{JUMPDEST}\\ G_{sload} & \text{if} \quad w = \small\text{SLOAD}\\ G_{zero} & \text{if} \quad w \in W_{zero}\\ G_{base} & \text{if} \quad w \in W_{base}\\ G_{verylow} & \text{if} \quad w \in W_{verylow}\\ 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} \end{cases} \end{equation} $$ where: $$ \begin{equation} C _ {memory}(a) \equiv G _ {memory} \cdot a + \Big\lfloor \dfrac{a^2}{512} \Big\rfloor \end{equation} $$ $$ \begin{equation} w \equiv \begin{cases} I_\mathbf{b}[\boldsymbol{\mu} _ {pc}] & \text{if} \quad \boldsymbol{\mu} _ {pc} < \lVert I _ \ mathbf{b} \rVert\\ \small\text{STOP} & \text{otherwise} \end{cases} \end{equation} $$ with $ C _ \tiny\text{CALL} $ and $ C _ \tiny\text{SSTORE} $ as specified in the appropriate section below. We define the following subsets of instructions: $ W _ {zero} $ = \{ { \small STOP }, {\small SUICIDE}, {\small RETURN} \} $ 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 _ {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*}\} $ W_{low} $ = \{ {\small MUL}, {\small DIV}, {\small SDIV}, {\small MOD}, {\small SMOD}, {\small SIGNEXTEND}\} $ W_{mid} $ = \{ {\small ADDMOD}, {\small MULMOD}, {\small JUMP}\} $ W_{high} $ = \{ {\small JUMPI}\} $ W_{ext} $ = \{ {\small BALANCE}, {\small EXTCODESIZE}, {\small BLOCKHASH}\} 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. While defining the instruction set, we defined the memory-expansion for range function, $ M $ , thus: \begin{equation} M(s, f, l) \equiv \begin{cases} s & \text{if} \quad l = 0 \\ \max(s, \ceil{ (f + l) \div 32 }) & \text{otherwise} \end{cases} \end{equation} ## 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: \begin{equation} O(\boldsymbol{\sigma}, \boldsymbol{\mu}, A, I) \equiv (\boldsymbol{\sigma}', \boldsymbol{\mu}', A', I) \quad \text{with exceptions, as noted} \end{equation} Here given are the various exceptions to the state transition rules given in section \ref{ch:model} specified for each instruction, together with the additional instruction-specific definitions of $ J $ and $ C $ . For each instruction, also specified is $ \alpha $ , the additional items placed on the stack and $ \delta $ , the items removed from stack, as defined in section \ref{ch:model}. $$ \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} \\ 0x00 & {\small STOP} & 0 & 0 & Halts execution. \\ \midrule 0x01 & {\small ADD} & 2 & 1 & Addition operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[0] + \boldsymbol{\mu}_\mathbf{s}[1] $ \\ \midrule 0x02 & {\small MUL} & 2 & 1 & Multiplication operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[0] \times \boldsymbol{\mu}_\mathbf{s}[1] $ \\ \midrule 0x03 & {\small SUB} & 2 & 1 & Subtraction operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[0] - \boldsymbol{\mu}_\mathbf{s}[1] $ \\ \midrule 0x04 & {\small DIV} & 2 & 1 & Integer division operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0\\ \lfloor\boldsymbol{\mu}_\mathbf{s}[0] \div \boldsymbol{\mu}_\mathbf{s}[1]\rfloor & \text{otherwise}\end{cases} $ \\ \midrule 0x05 & {\small SDIV} & 2 & 1 & Signed integer division operation (truncated). \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0\\ -2^{255} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] = -2^{255} \wedge \quad \boldsymbol{\mu}_\mathbf{s}[1] = -1\\ \mathbf{sgn} (\boldsymbol{\mu}_\mathbf{s}[0] \div \boldsymbol{\mu}_\mathbf{s}[1]) \lfloor |\boldsymbol{\mu}_\mathbf{s}[0] \div \boldsymbol{\mu}_\mathbf{s}[1]| \rfloor & \text{otherwise}\end{cases} $ \\ &&&& Where all values are treated as two's complement signed 256-bit integers. \\ &&&& Note the overflow semantic when $ -2^{255} $ is negated.\\ \midrule 0x06 & {\small MOD} & 2 & 1 & Modulo remainder operation. \\ &&&& $ \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} $ \\ &&&& Where all values are treated as two's complement signed 256-bit integers. \\ \midrule 0x08 & {\small ADDMOD} & 3 & 1 & Modulo addition operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] = 0\\ (\boldsymbol{\mu}_\mathbf{s}[0] + \boldsymbol{\mu}_\mathbf{s}[1]) \mod \boldsymbol{\mu}_\mathbf{s}[2] & \text{otherwise}\end{cases} $ \\ &&&& All intermediate calculations of this operation are not subject to the $ 2^{256} $ modulo. \\ \midrule 0x09 & {\small MULMOD} & 3 & 1 & Modulo multiplication operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}0 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] = 0\\ (\boldsymbol{\mu}_\mathbf{s}[0] \times \boldsymbol{\mu}_\mathbf{s}[1]) \mod \boldsymbol{\mu}_\mathbf{s}[2] & \text{otherwise}\end{cases} $ \\ &&&& All intermediate calculations of this operation are not subject to the $ 2^{256} $ modulo. \\ \midrule 0x0a & {\small EXP} & 2 & 1 & Exponential operation. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[0] ^ {\boldsymbol{\mu}_\mathbf{s}[1] } $ \\ \midrule 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} \\ \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{10s: Comparison \& Bitwise Logic Operations}} \\ 0x10 & {\small LT} & 2 & 1 & Less-than comparision. \\ &&&& $ \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. \\ &&&& $ \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. \\ &&&& $ \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. \\ &&&& $ \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. \\ &&&& $ \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. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0] = 0 \\ 0 & \text{otherwise} \end{cases} $ \\ \midrule 0x16 & {\small AND} & 2 & 1 & Bitwise AND operation. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \boldsymbol{\mu}_\mathbf{s}[0]_i \wedge \boldsymbol{\mu}_\mathbf{s}[1]_i $ \\ \midrule 0x17 & {\small OR} & 2 & 1 & Bitwise OR operation. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \boldsymbol{\mu}_\mathbf{s}[0]_i \vee \boldsymbol{\mu}_\mathbf{s}[1]_i $ \\ \midrule 0x18 & {\small XOR} & 2 & 1 & Bitwise XOR operation. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \boldsymbol{\mu}_\mathbf{s}[0]_i \oplus \boldsymbol{\mu}_\mathbf{s}[1]_i $ \\ \midrule 0x19 & {\small NOT} & 1 & 1 & Bitwise NOT operation. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \begin{cases} 1 & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[0]_i = 0 \\ 0 & \text{otherwise} \end{cases} $ \\ \midrule 0x1a & {\small BYTE} & 2 & 1 & Retrieve single byte from word. \\ &&&& $ \forall i \in [0..255]: \boldsymbol{\mu}'_\mathbf{s}[0]_i \equiv \begin{cases} \boldsymbol{\mu}_\mathbf{s}[1]_{(i + 8\boldsymbol{\mu}_\mathbf{s}[0])} & \text{if} \quad i < 8 \wedge \boldsymbol{\mu}_\mathbf{s}[0] < 32 \\ 0 & \text{otherwise} \end{cases} $ \\ &&&& For Nth byte, we count from the left (i.e. N=0 would be the most significant in big endian). \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{20s: SHA3}} \vspace{5pt} \\ 0x20 & {\small SHA3} & 2 & 1 & Compute Keccak-256 hash. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \mathtt{\tiny Keccak}(\boldsymbol{\mu}_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[0] \dots (\boldsymbol{\mu}_\mathbf{s}[0] + \boldsymbol{\mu}_\mathbf{s}[1] - 1) ]) $ \\ &&&& $ \boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[1]) $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{30s: Environmental Information}} \vspace{5pt} \\ 0x30 & {\small ADDRESS} & 0 & 1 & Get address of currently executing account. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_a $ \\ \midrule 0x31 & {\small BALANCE} & 1 & 1 & Get balance of the given account. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \begin{cases}\boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[0]]_b& \text{if} \quad \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[0] \mod 2^{160}] \neq \varnothing\\0&\text{otherwise}\end{cases} $ \\ \midrule 0x32 & {\small ORIGIN} & 0 & 1 & Get execution origination address. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_o $ \\ &&&& This is the sender of original transaction; it is never an account with non-empty \\ &&&& associated code. \\ \midrule 0x33 & {\small CALLER} & 0 & 1 & Get caller address. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_s $ \\ &&&& This is the address of the account that is directly responsible for this execution. \\ \midrule 0x34 & {\small CALLVALUE} & 0 & 1 & Get deposited value by the instruction/transaction responsible for this execution. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_v $ \\ \midrule 0x35 & {\small CALLDATALOAD} & 1 & 1 & Get input data of current environment. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv I_\mathbf{d}[ \boldsymbol{\mu}_\mathbf{s}[0] \dots (\boldsymbol{\mu}_\mathbf{s}[0] + 31) ] \quad \text{with} \quad I_\mathbf{d}[x] = 0 \quad \text{if} \quad x \geqslant \lVert I_\mathbf{d} \rVert $ \\ &&&& This pertains to the input data passed with the message call instruction or transaction. \\ \midrule 0x36 & {\small CALLDATASIZE} & 0 & 1 & Get size of input data in current environment. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \lVert I_\mathbf{d} \rVert $ \\ &&&& This pertains to the input data passed with the message call instruction or transaction. \\ \midrule 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} $ \\ &&&& 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. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \lVert I_\mathbf{b} \rVert $ \\ \midrule 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 \\ \small\text{STOP} & \text{otherwise} \end{cases} $ \\ \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 $ \\ \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 \\ \small\text{STOP} & \text{otherwise} \end{cases} $ \\ &&&& where $ \mathbf{c} \equiv \boldsymbol{\sigma}[\boldsymbol{\mu}_s[0] \mod 2^{160}]_c $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{40s: Block Information}} \vspace{5pt} \\ 0x40 & {\small BLOCKHASH} & 1 & 1 & Get the hash of one of the 256 most recent complete blocks. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv P(I_{H_p}, \boldsymbol{\mu}_\mathbf{s}[0], 0) $ \\ &&&& where $ P $ is the hash of a block of a particular number, up to a maximum age.\\ &&&& 0 is left on the stack if the looked for block number is greater than the current block number \\ &&&& or more than 256 blocks behind the current block. \\ &&&& $ P(h, n, a) \equiv \begin{cases} 0 & \text{if} \quad n > H_i \vee a = 256 \vee h = 0 \\ h & \text{if} \quad n = H_i \\ P(H_p, n, a + 1) & \text{otherwise} \end{cases} $ \\ &&&& and we assert the header $ H $ can be determined as its hash is the parent hash \\ &&&& in the block following it. \\ \midrule 0x41 & {\small COINBASE} & 0 & 1 & Get the block's beneficiary address. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv {I_H}_c $ \\ \midrule 0x42 & {\small TIMESTAMP} & 0 & 1 & Get the block's timestamp. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv {I_H}_s $ \\ \midrule 0x43 & {\small NUMBER} & 0 & 1 & Get the block's number. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv {I_H}_i $ \\ \midrule 0x44 & {\small DIFFICULTY} & 0 & 1 & Get the block's difficulty. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv {I_H}_d $ \\ \midrule 0x45 & {\small GASLIMIT} & 0 & 1 & Get the block's gas limit. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv {I_H}_l $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{50s: Stack, Memory, Storage and Flow Operations}} \vspace{5pt} \\ 0x50 & {\small POP} & 1 & 0 & Remove item from stack. \\ \midrule 0x51 & {\small MLOAD} & 1 & 1 & Load word from memory. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{m}[\boldsymbol{\mu}_\mathbf{s}[0] \dots (\boldsymbol{\mu}_\mathbf{s}[0] + 31) ] $ \\ &&&& $ \boldsymbol{\mu}'_i \equiv \max(\boldsymbol{\mu}_i, \ceil{ (\boldsymbol{\mu}_\mathbf{s}[0] + 32) \div 32 }) $ \\ \midrule 0x52 & {\small MSTORE} & 2 & 0 & Save word to memory. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[0] \dots (\boldsymbol{\mu}_\mathbf{s}[0] + 31) ] \equiv \boldsymbol{\mu}_\mathbf{s}[1] $ \\ &&&& $ \boldsymbol{\mu}'_i \equiv \max(\boldsymbol{\mu}_i, \ceil{ (\boldsymbol{\mu}_\mathbf{s}[0] + 32) \div 32 }) $ \\ \midrule 0x53 & {\small MSTORE8} & 2 & 0 & Save byte to memory. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[0] ] \equiv (\boldsymbol{\mu}_\mathbf{s}[1] \bmod 256) $ \\ &&&& $ \boldsymbol{\mu}'_i \equiv \max(\boldsymbol{\mu}_i, \ceil{ (\boldsymbol{\mu}_\mathbf{s}[0] + 1) \div 32 }) $ \\ \midrule 0x54 & {\small SLOAD} & 1 & 1 & Load word from storage. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\sigma}[I_a]_\mathbf{s}[\boldsymbol{\mu}_\mathbf{s}[0]] $ \\ \midrule 0x55 & {\small SSTORE} & 2 & 0 & Save word to storage. \\ &&&& $ \boldsymbol{\sigma}'[I_a]_\mathbf{s}[ \boldsymbol{\mu}_\mathbf{s}[0] ] \equiv \boldsymbol{\mu}_\mathbf{s}[1] $ \\ &&&& $ C_{\tiny\text{SSTORE}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} G_{sset} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] \neq 0 \; \wedge \; \boldsymbol{\sigma}[I_a]_\mathbf{s}[\boldsymbol{\mu}_\mathbf{s}[0]] = 0 \\ G_{sreset} & \text{otherwise} \end{cases} $ \\ &&&& $ A'_{r} \equiv A_{r} + \begin{cases} R_{sclear} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] = 0 \; \wedge \; \boldsymbol{\sigma}[I_a]_\mathbf{s}[\boldsymbol{\mu}_\mathbf{s}[0]] \neq 0 \\ 0 & \text{otherwise} \end{cases} $ \\ \midrule 0x56 & {\small JUMP} & 1 & 0 & Alter the program counter. \\ &&&& $ J_{\tiny\text{JUMP}}(\boldsymbol{\mu}) \equiv \boldsymbol{\mu}_\mathbf{s}[0] $ \\ &&&& This has the effect of writing said value to $ \boldsymbol{\mu}_{pc} $ . See section \ref{ch:model}. \\ \midrule 0x57 & {\small JUMPI} & 2 & 0 & Conditionally alter the program counter. \\ &&&& $ J_{\tiny\text{JUMPI}}(\boldsymbol{\mu}) \equiv \begin{cases} \boldsymbol{\mu}_\mathbf{s}[0] & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[1] \neq 0 \\ \boldsymbol{\mu}_{pc} + 1 & \text{otherwise} \end{cases} $ \\ &&&& This has the effect of writing said value to $ \boldsymbol{\mu}_{pc} $ . See section \ref{ch:model}. \\ \midrule 0x58 & {\small PC} & 0 & 1 & Get the value of the program counter \textit{prior} to the increment \\ &&&& corresponding to this instruction. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_{pc} $ \\ \midrule 0x59 & {\small MSIZE} & 0 & 1 & Get the size of active memory in bytes. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv 32\boldsymbol{\mu}_{i} $ \\ \midrule 0x5a & {\small GAS} & 0 & 1 & Get the amount of available gas, including the corresponding reduction \\ &&&& for the cost of this instruction. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_{g} $ \\ \midrule 0x5b & {\small JUMPDEST} & 0 & 0 & Mark a valid destination for jumps. \\ &&&& This operation has no effect on machine state during execution. \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{60s \& 70s: Push Operations}} \vspace{5pt} \\ 0x60 & {\small PUSH1} & 0 & 1 & Place 1 byte item on stack. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv c(\boldsymbol{\mu}_{pc} + 1) $ \\ &&&& $ \text{where} \quad c(x) \equiv \begin{cases} I_\mathbf{b}[x] & \text{if} \quad x < \lVert I_\mathbf{b} \rVert \\ 0 & \text{otherwise} \end{cases} $ \\ &&&& The bytes are read in line from the program code's bytes array. \\ &&&& The function $ c $ ensures the bytes default to zero if they extend past the limits.\\ &&&& The byte is right-aligned (takes the lowest significant place in big endian). \\ \midrule 0x61 & {\small PUSH2} & 0 & 1 & Place 2-byte item on stack. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{c}\big( (\boldsymbol{\mu}_{pc} + 1) \dots (\boldsymbol{\mu}_{pc} + 2) \big) $ \\ &&&& with $ \boldsymbol{c}(\boldsymbol{x}) \equiv (c(\boldsymbol{x}_0), ..., c(\boldsymbol{x}_{\lVert x \rVert -1})) $ with $ c $ as defined as above. \\ &&&& The bytes are right-aligned (takes the lowest significant place in big endian). \\ \midrule \multicolumn{1}{c}{\vdots} & \multicolumn{1}{c}{\vdots} & \vdots & \vdots & \multicolumn{1}{c}{\vdots} \\ \midrule 0x7f & {\small PUSH32} & 0 & 1 & Place 32-byte (full word) item on stack. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{c}\big((\boldsymbol{\mu}_{pc} + 1) \dots (\boldsymbol{\mu}_{pc} + 32) \big) $ \\ &&&& where $ \boldsymbol{c} $ is defined as above. \\ &&&& The bytes are right-aligned (takes the lowest significant place in big endian). \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{80s: Duplication Operations}} \vspace{5pt} \\ 0x80 & {\small DUP1} & 1 & 2 & Duplicate 1st stack item. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[0] $ \\ \midrule 0x81 & {\small DUP2} & 2 & 3 & Duplicate 2nd stack item. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[1] $ \\ \midrule \multicolumn{1}{c}{\vdots} & \multicolumn{1}{c}{\vdots} & \vdots & \vdots & \multicolumn{1}{c}{\vdots} \\ \midrule 0x8f & {\small DUP16} & 16 & 17 & Duplicate 16th stack item. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[15] $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{90s: Exchange Operations}} \vspace{5pt} \\ 0x90 & {\small SWAP1} & 2 & 2 & Exchange 1st and 2nd stack items. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[1] $ \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[1] \equiv \boldsymbol{\mu}_\mathbf{s}[0] $ \\ \midrule 0x91 & {\small SWAP2} & 3 & 3 & Exchange 1st and 3rd stack items. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[2] $ \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[2] \equiv \boldsymbol{\mu}_\mathbf{s}[0] $ \\ \midrule \multicolumn{1}{c}{\vdots} & \multicolumn{1}{c}{\vdots} & \vdots & \vdots & \multicolumn{1}{c}{\vdots} \\ \midrule 0x9f & {\small SWAP16} & 17 & 17 & Exchange 1st and 17th stack items. \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[0] \equiv \boldsymbol{\mu}_\mathbf{s}[16] $ \\ &&&& $ \boldsymbol{\mu}'_\mathbf{s}[16] \equiv \boldsymbol{\mu}_\mathbf{s}[0] $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \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}{The entry's topic series, $ \mathbf{t} $ , differs accordingly:}\vspace{5pt} \\ 0xa0 & {\small LOG0} & 2 & 0 & Append log record with no topics. \\ &&&& $ \mathbf{t} \equiv () $ \\ \midrule 0xa1 & {\small LOG1} & 3 & 0 & Append log record with one topic. \\ &&&& $ \mathbf{t} \equiv (\boldsymbol{\mu}_\mathbf{s}[2]) $ \\ \midrule \multicolumn{1}{c}{\vdots} & \multicolumn{1}{c}{\vdots} & \vdots & \vdots & \multicolumn{1}{c}{\vdots} \\ \midrule 0xa4 & {\small LOG4} & 6 & 0 & Append log record with four topics. \\ &&&& $ \mathbf{t} \equiv (\boldsymbol{\mu}_\mathbf{s}[2], \boldsymbol{\mu}_\mathbf{s}[3], \boldsymbol{\mu}_\mathbf{s}[4], \boldsymbol{\mu}_\mathbf{s}[5]) $ \\ \bottomrule \end{tabular*} $$ $$ \begin{tabular*}{\columnwidth}[h]{rlrrl} \toprule \multicolumn{5}{c}{\textbf{f0s: System operations}} \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}^* \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} $ \\ &&&& $ \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 $ \\ &&&& (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. \\ &&&& $ \boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[1], \boldsymbol{\mu}_\mathbf{s}[2]) $ \\ &&&& 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_{\tiny\text{CALLGAS}}(\boldsymbol{\mu}), I_p, \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} $ \\ &&&& $ 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}'_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 \\ &&&& $ \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_{\tiny\text{CALL}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv G_{call} + \boldsymbol{\mu}_\mathbf{s}[0] + C_{\tiny\text{CALLXFER}}(\boldsymbol{\mu}) + C_{\tiny\text{CALLNEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) $ \\ &&&& $ C_{\tiny\text{CALLXFER}}(\boldsymbol{\mu}) \equiv \begin{cases} G_{callvalue} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ 0 & \text{otherwise} \end{cases} $ \\ &&&& $ C_{\tiny\text{CALLNEW}}(\boldsymbol{\sigma}, \boldsymbol{\mu}) \equiv \begin{cases} G_{callnewaccount} & \text{if} \quad \boldsymbol{\sigma}[\boldsymbol{\mu}_\mathbf{s}[1] \mod 2^{160}] = \varnothing \\ 0 & \text{otherwise} \end{cases} $ \\ &&&& $ C_{\tiny\text{CALLGAS}}(\boldsymbol{\mu}) \equiv \begin{cases} \boldsymbol{\mu}_\mathbf{s}[0] + G_{callstipend} & \text{if} \quad \boldsymbol{\mu}_\mathbf{s}[2] \neq 0 \\ 0 & \text{otherwise} \end{cases} $ \\ \midrule 0xf2 & {\small CALLCODE} & 7 & 1 & Message-call into this account with 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 \boldsymbol{\mu}_\mathbf{s}[0], I_p, \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 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 overridden altered.\\ \midrule 0xf3 & {\small RETURN} & 2 & 0 & Halt execution returning output data. \\ &&&& $ H_{\tiny\text{RETURN}}(\boldsymbol{\mu}) \equiv \boldsymbol{\mu}_\mathbf{m}[ \boldsymbol{\mu}_\mathbf{s}[0] \dots ( \boldsymbol{\mu}_\mathbf{s}[0] + \boldsymbol{\mu}_\mathbf{s}[1] - 1 ) ] $ \\ &&&& This has the effect of halting the execution at this point with output defined.\\ &&&& See section \ref{ch:model}. \\ &&&& $ \boldsymbol{\mu}'_i \equiv M(\boldsymbol{\mu}_i, \boldsymbol{\mu}_\mathbf{s}[0], \boldsymbol{\mu}_\mathbf{s}[1]) $ \\ \midrule 0xff & {\small SUICIDE} & 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 $ \\ &&&& $ A'_{r} \equiv A_{r} + \begin{cases} R_{suicide} & \text{if} \quad I_a \notin A_\mathbf{s} \\ 0 & \text{otherwise} \end{cases} $ \\\bottomrule \end{tabular*} $$ # Low-level Lisp-like Language %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. # Genesis Block The genesis block is 15 items, and is specified thus: \begin{equation} \big( \big( 0_{256}, \mathtt{\tiny KEC}\big(\mathtt{\tiny RLP}\big( () \big)\big), 0_{160}, stateRoot, 0, 0, 0_{2048}, 2^{17}, 0, 0, 3141592, time, 0, 0_{256}, \mathtt{\tiny KEC}\big( (42) \big) \big), (), () \big) \end{equation} 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. # Ethash ## Definitions We employ the following definitions: $ J_{wordbytes} $ & 4 & Bytes in word. \\ $ J_{datasetinit} $ & $ 2^{30} $ & Bytes in dataset at genesis. \\ $ J_{datasetgrowth} $ & $ 2^{23} $ & Dataset growth per epoch. \\ $ J_{cacheinit} $ & $ 2^{24} $ & Bytes in cache at genesis. \\ $ J_{cachegrowth} $ & $ 2^{17} $ & Cache growth per epoch. \\ $ J_{epoch} $ & 30000 & Blocks per epoch. \\ $ J_{mixbytes} $ & 128 & mix length in bytes. \\ $ J_{hashbytes} $ & 64 & Hash length in bytes. \\ $ J_{parents} $ & 256 & Number of parents of each dataset element. \\ $ J_{cacherounds} $ & 3 & Number of rounds in cache production. \\ $ J_{accesses} $ & 64 & Number of accesses in hashimoto loop. \\ ## Size of dataset and cache The size for Ethash's cache $ \mathbf{c} \in \mathbb{B} $ and dataset $ \mathbf{d} \in \mathbb{B} $ depend on the epoch, which in turn depends on the block number. $$ \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. 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}) \end{equation} $$ The size of the cache, $ c_{size} $ , is calculated using $$ \begin{equation} c_{size} = E_{prime}(J_{cacheinit} + J_{cachegrowth} \cdot E_{epoch} - J_{hashbytes}, J_{hashbytes}) \end{equation} $$ $$ \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} \end{cases} \end{equation} $$ ### 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} $ . ### 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} \mathbf{s} = C_{seedhash}(H_i) \end{equation} $$ $$ \begin{equation} C_{seedhash}(H_i) = \begin{cases} \texttt{KEC}(\mathbf{0}_{32}) & \text{if} \quad E_{epoch}(H_i) = 0 \quad \\ \texttt{KEC}(C_{seedhash}(H_i - J_{epoch})) & \text{otherwise} \end{cases} \end{equation} $$ With $ \mathbf{0}_{32} $ being 32 bytes of zeros. ## 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. We define the array $ \mathbf{c}_{i} $ , consisting of 64 single bytes, as the $ i $ th element of the intial 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 $$ \begin{equation} \mathbf{c'}[i] = \mathbf{c}_{i} \quad \forall \quad i < n \end{equation} $$ $$ \begin{equation} n = \left\lfloor\frac{c_{size}}{J_{hashbytes}}\right\rfloor \end{equation} $$ The cache is calculated by performing $ J_{cacherounds} $ rounds of the RandMemoHash algorithm to the inital cache $ \mathbf{c'} $ : $$ \begin{equation} \mathbf{c} = E_{cacherounds}(\mathbf{c'}, J_{cacherounds}) \end{equation} $$ $$ \begin{equation} E_{cacherounds}(\mathbf{x}, y) = \begin{cases} \mathbf{x} & \text{if} \quad y = 0 \quad \\ E_\tiny\text{RMH}(\mathbf{x}) & \text{if} \quad y = 1 \quad \\ E_{cacherounds}(E_\tiny\text{RMH}(\mathbf{x}), y -1 ) & \text{otherwise} \end{cases} \end{equation} $$ Where a single round modifies each subset of the cache as follows: $$ \begin{equation} E_\tiny\text{RMH}(\mathbf{x}) = \lbrace E_{rmh}(\mathbf{x}, 0), E_{rmh}(\mathbf{x}, 1), ... , E_{rmh}(\mathbf{x}, n - 1) \rbrace \end{equation} $$ $$ \begin{multline} E_{rmh}(\mathbf{x}, i) = \texttt{KEC512}(\mathbf{x'}[(i - 1 + n) \mod n] \oplus \mathbf{x'}[\mathbf{x'}[i][0] \mod n]) \\ \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} $$ ## Full dataset calculation 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 $ d_{size} $ 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 \end{equation} $$ In order to calculate the single item we use an algorithm inspired by the FNV hash (\cite{FowlerNollVo1991FNVHash}) in some cases as a non-associative substitute for XOR. $$ \begin{equation} E_\tiny\text{FNV}(\mathbf{x}, \mathbf{y}) = (\mathbf{x} \cdot (\mathrm{0x01000193} \oplus \mathbf{y})) \mod 2^{32} \end{equation} $$ The single item of the dataset can now be calculated as: $$ \begin{equation} E_{datasetitem}(\mathbf{c}, i) = E_{parents}(\mathbf{c}, i, -1, \varnothing) \end{equation} $$ $$ \begin{equation} E_{parents}(\mathbf{c}, i, p, \mathbf{m}) = \begin{cases} E_{parents}(\mathbf{c}, i, p +1, E_{mix}(\mathbf{m}, \mathbf{c}, i, p + 1)) & \text{if} \quad p < J_{parents} -2 \\ E_{mix}(\mathbf{m}, \mathbf{c}, i, p + 1) & \text{otherwise} \end{cases} $$ $$ \end{equation} \begin{equation} E_{mix}(\mathbf{m}, \mathbf{c}, i, p) = \begin{cases} \texttt{KEC512}(\mathbf{c}[i \mod c_{size}] \oplus i) & \text{if} \quad p = 0 \\ E_\tiny\text{FNV}(\mathbf{m}, \mathbf{c}[E_\tiny\text{FNV}(i \oplus p, \mathbf{m}[p \mod \lfloor J_{hashbytes} / J_{wordbytes} \rfloor]) \mod c_{size}] & \text{otherwise} \end{cases} \end{equation} $$ ## 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_\tiny\text{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. The PoW-function returns an array with the compressed mix as its first item and the Keccak-256 hash of the concatenation of the compressed mix with the seed hash as the second item: \begin{equation} \mathtt{PoW}(H_{\hcancel{n}}, H_n, \mathbf{d}) = \lbrace \mathbf{m}_c(\mathtt{\small KEC}(\mathtt{\small RLP}(L_H(H_{\hcancel{n}}))), H_n, \mathbf{d}), \texttt{KEC}(\mathbf{s}_h(\mathtt{\small KEC}(\mathtt{\small RLP}(L_H(H_{\hcancel{n}}))), H_n) + \mathbf{m}_c(\mathtt{\small KEC}(\mathtt{\small RLP}(L_H(H_{\hcancel{n}}))), H_n, \mathbf{d})) \rbrace \end{equation} With $ H_{\hcancel{n}} $ being the hash of the header without the nonce. The compressed mix $ \mathbf{m}_c $ is obtained as follows: \begin{equation} \mathbf{m}_c(\mathbf{h}, \mathbf{n}, \mathbf{d}) = E_{compress}(E_{accesses}(\mathbf{d}, \sum_{i = 0}^{n_{mix}} \mathbf{s}_h(\mathbf{h}, \mathbf{n}), \mathbf{s}_h(\mathbf{h}, \mathbf{n}), -1), -4) \end{equation} The seed hash being: \begin{equation} \mathbf{s}_h(\mathbf{h}, \mathbf{n}) = \texttt{KEC512}(\mathbf{h} + E_{revert}(\mathbf{n})) \end{equation} $ E_{revert}(\mathbf{n}) $ returns the reverted bytes sequence of the nonce $ \mathbf{n} $ : \begin{equation} E_{revert}(\mathbf{n})[i] = \mathbf{n}[\lVert \mathbf{n} \rVert -i] \end{equation} We note that the `` $ + $ ''-operator between two byte sequences results in the concatenation of both sequences. The dataset $ \mathbf{d} $ is obtained as described in section \ref{dataset}. The number of replicated sequences in the mix is: \begin{equation} n_{mix} = \left\lfloor\frac{J_{mixbytes}}{J_{hashbytes}}\right\rfloor \end{equation} In order to add random dataset nodes to the mix, the $ E_{accesses} $ function is used: \begin{equation} E_{accesses}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i) = \begin{cases} E_{mixdataset}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i) & \text{if} \quad i = J_{accesses} -2 \\ E_{accesses}(E_{mixdataset}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i), \mathbf{s}, i + 1) & \text{otherwise} \end{cases} \end{equation} \begin{equation} E_{mixdataset}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i) = E_\tiny\text{FNV}(\mathbf{m}, E_{newdata}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i) \end{equation} $ E_{newdata} $ returns an array with $ n_{mix} $ elements: \begin{equation} E_{newdata}(\mathbf{d}, \mathbf{m}, \mathbf{s}, i)[j] = \mathbf{d}[E_\tiny\text{FNV}(i \oplus \mathbf{s}[0], \mathbf{m}[i \mod \left\lfloor\frac{J_{mixbytes}}{J_{wordbytes}}\right\rfloor]) \mod \left\lfloor\frac{d_{size} / J_{hashbytes}}{n_{mix}}\right\rfloor \cdot n_{mix} + j] \quad \forall \quad j < n_{mix} \end{equation} The mix is compressed as follows: \begin{equation} E_{compress}(\mathbf{m}, i) = \begin{cases} \mathbf{m} & \text{if} \quad i \geqslant \lVert \mathbf{m} \rVert - 8 \\ E_{compress}(E_\tiny\text{FNV}(E_\tiny\text{FNV}(E_\tiny\text{FNV}(\mathbf{m}[i + 4], \mathbf{m}[i + 5]), \mathbf{m}[i + 6]), \mathbf{m}[i + 7]), i + 8) & \text{otherwise} \end{cases} \end{equation} ## Mining In order to mine a valid block, one repeatedly chooses a random nonce $ \mathbf{n}_{rand} \in \mathbb{B}_{8} $ and calculates the $ \mathtt{PoW} $ function until \begin{equation} \mathtt{PoW}(H_{\hcancel{n}}, \mathbf{n}_{rand}, \mathbf{d})[1] \leqslant \frac{2^{256}}{H_d}\quad. \end{equation} $ H_d $ being the current difficulty of the block. \end{document}