| CHIP Number | 0036 |
|---|---|
| Title | keccak256 CLVM operator |
| Description | Add a CLVM operator to support Ethereum addresses |
| Author | Arvid Norberg, Rigidity |
| Editor | Dan Perry |
| Comments-URI | CHIPs repo, PR #131 |
| Status | Final |
| Category | Standards Track |
| Sub-Category | Chialisp |
| Created | 2024-10-25 |
| Requires | None |
| Replaces | None |
| Superseded-By | None |
This CHIP will add a new keccak256 operator to the CLVM in order to enable the support of Ethereum addresses. This operator will be made accessible from behind the softfork guard, which requires a soft fork of Chia's consensus.
Throughout this document, we'll use the following terms:
- Chialisp - The high-level programming language from which Chia coins are constructed
- CLVM - Chialisp Virtual Machine, where the bytecode from compiled Chialisp is executed. Also commonly refers to the compiled bytecode itself
- Keccak-256 - The same hashing standard Ethereum uses. A few notes on this standard:
- The Keccak-256 standard referred to in this CHIP is specifically version 3 of the winning submission to the NIST SHA-3 contest by Bertoni, Daemen, Peeters, and Van Assche in 2011
- This standard is not the same as the final SHA-3 standard, which NIST released in 2015
- The EVM uses an opcode called
SHA3, and Solidity has an instruction calledsha3. However, these do not refer to the final SHA-3 standard. They do refer to the sameKeccak-256standard used in this CHIP - In order to avoid confusion, we will not use the terms
SHA3,sha3, orSHA-3further in this CHIP. Instead, the termsKeccak-256andkeccak256will be used to denote the cryptographic standard used in Ethereum
CLVM currently includes an atomic operator called sha256. This operator calculates and returns the SHA-256 hash of the input atom(s).
This CHIP will add an atomic operator to the CLVM called keccak256, which will calculate and return the Keccak-256 hash of the input atom(s). The primary reason to add this operator is to support Ethereum addresses, which also rely on Keccak-256.
If this CHIP is accepted, the keccak256 operator will be added to the CLVM. If the operator could be called directly, it would break compatibility, and therefore would require a hard fork of Chia's blockchain. However, CLVM includes a softfork operator specifically to define new CLVM operators without requiring a hard fork.
This CHIP will therefore use the softfork operator, which itself requires a soft fork of Chia's blockchain. After the fork's activation, the operator will need to be called from inside the softfork guard. This process is detailed in the softfork usage section.
As with all forks, there will be a risk of a chain split. The soft fork could also fail to be adopted. This might happen if an insufficient number of nodes have upgraded to include the changes introduced by this CHIP prior to the fork's block height.
The operator will be introduced in multiple phases:
- Pre-CHIP: Prior to block
6 800 000(six million, eight hundred thousand), the new operator will be undefined. Any attempt to call it will returnNIL. - Soft fork: A soft fork will activate at block
6 800 000. From that block forward, the new operator will exhibit the functionality laid out in this CHIP. It will need to be called from inside thesoftforkguard. - Hard fork (hypothetical): In the event that a hard fork is enacted after the code from this CHIP has been added to the codebase, this hypothetical hard fork could include adding the operator from this CHIP to the core CLVM operator set. If this were to happen, the operator could be also be called from outside the
softforkguard. (Note that the operator would still be callable from inside thesoftforkguard if desired.)
This CHIP's design was primarily chosen to support Ethereum addresses. It was implemented in a manner consistent with the Keccak-256 standard.
The new operator will incur a CLVM cost, as detailed below. If this CHIP is adopted, the new operator will be optional when designing Chia coins.
Opcode: 64
Functionality: Calculate and return the Keccak-256 hash of the input atom(s)
Arguments:
- If zero arguments, the Keccak-256 hash of an empty string will be returned
- If one or more arguments:
- Each argument (if more than one) will be concatenated
- The Keccak-256 hash of the resulting string will be returned
Usage: (keccak256 A B …)
CLVM Cost: 50 base, 160 per argument, 2 per byte
As explained in the Backwards Compatibility section, starting with block 6 800 000, the operators introduced in this CHIP will be available from inside the softfork guard.
The following rules apply when calling the keccak256 operator by using the softfork operator:
- The
softforkoperator works likeapply(a) but takes two additional parameters,costandextension - The syntax is therefore
softfork (<cost> <extension> <quoted-program> <arguments>) - The
costparameter is the CLVM cost of executing thequoted-programwith the specifiedarguments. If this cost mismatches the actual cost of executing the program, thesoftforkoperator will raise an exception - The
extensionparameter is an integer indicating the set of extensions available in thesoftforkguard.extension 0referred to theCoin ID,BLS, etc operators described in CHIP-11.extension 1will refer to the operator from this CHIP, as well as those fromextension 0.
- Just like the
aoperator, thequoted-programparameter is quoted and executed from inside thesoftforkguard. - A client that does not recognize the
extensionspecifier must:- In consensus mode, ignore the whole
softfork, returnnulland charge the specifiedcost - In mempool mode, fail the program immediately
- In consensus mode, ignore the whole
Since softfork guards always return null, the only useful outcome of executing one is terminating (i.e. failing) the program or not.
The cost of executing the softfork operator is 140. This counts against the cost specified in its arguments.
An example of using the softfork operator to call the keccak256 operator is as follows:
(softfork
(q . 842) ; expected cost (including cost of softfork itself)
(q . 1) ; extension 1
(q a ; defer execution of if-branches
(i
(=
(keccak256
(q . f)
(q . oobar)
)
(q . 0x38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e)
)
(q . 0) ; if encoding matches, return 0
(q x) ; if encoding mismatches, raise
)
(q . ())) ; environment to apply
(q . ())) ; environment to softfork
Test cases for the keccak256 operator are located in the clvm_rs repository.
Additional tests were added as part of PR #18988 of the chia-blockchain repository.
- keccak256 and softfork implementation in Rust
- softfork implementation in the
chia-blockchainrepository
Chia Network, Inc. has conducted an internal review of the code involved with this CHIP.
None
Copyright and related rights waived via CC0.