This repository was archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
Add CALLVALUE and CALLDATASIZE #96
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # CALLVALUE opcode | ||
|
|
||
| ## Procedure | ||
|
|
||
| The `CALLVALUE` opcode gets the call value (msg.value) from the current call. | ||
|
|
||
| ## EVM behaviour | ||
|
|
||
| The `CALLVALUE` opcode loads a `word` (32 bytes of data) from call context -> | ||
| call value, then pushes the `word` to the stack. | ||
|
|
||
| ## Circuit behaviour | ||
|
|
||
| 1. Construct call context table in rw table | ||
| 2. Do busmapping lookup for call context call value read operation | ||
| 3. Do busmapping lookup for stack write operation | ||
|
|
||
| ## Constraints | ||
|
|
||
| 1. opId = 0x34 | ||
| 2. State transition: | ||
| - gc + 2 (1 stack write, 1 call context read) | ||
| - stack_pointer - 1 | ||
| - pc + 1 | ||
| - gas + 2 | ||
| 3. Lookups: 2 | ||
| - `word` is in the rw table {call context, call ID, call value} | ||
| - `word` is on top of stack | ||
|
|
||
| ## Exceptions | ||
|
|
||
| 1. stack overflow: stack is full, stack pointer = 0 | ||
| 2. out of gas: remaining gas is not enough | ||
|
|
||
| ## Code | ||
|
|
||
| Please refer to `src/zkevm_specs/evm/execution/callvalue.py`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # CALLDATASIZE opcode | ||
|
|
||
| ## Procedure | ||
|
|
||
| The `CALLDATASIZE` opcode gets the call data size (msg.data.size) from the current call. | ||
|
|
||
| ## EVM behaviour | ||
|
|
||
| The `CALLDATASIZE` opcode loads a `u64` (5 bytes of data) from call context -> | ||
| call data length, then pushes the `u64` to the stack. | ||
|
|
||
| ## Circuit behaviour | ||
|
|
||
| 1. Construct call context table in rw table | ||
| 2. Do busmapping lookup for call context call data length read operation | ||
| 3. Do busmapping lookup for stack write operation | ||
|
|
||
| ## Constraints | ||
|
|
||
| 1. opId = 0x36 | ||
| 2. State transition: | ||
| - gc + 2 (1 stack write, 1 call context read) | ||
| - stack_pointer - 1 | ||
| - pc + 1 | ||
| - gas + 2 | ||
| 3. Lookups: 2 | ||
| - `u64` is in the rw table {call context, call ID, call data length} | ||
| - `u64` is on top of stack | ||
|
|
||
| ## Exceptions | ||
|
|
||
| 1. stack overflow: stack is full, stack pointer = 0 | ||
| 2. out of gas: remaining gas is not enough | ||
|
|
||
| ## Code | ||
|
|
||
| Please refer to `src/zkevm_specs/evm/execution/calldatasize.py`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| from ..instruction import Instruction, Transition | ||
| from ..table import CallContextFieldTag | ||
| from ..opcode import Opcode | ||
| from ...util.param import N_BYTES_MEMORY_ADDRESS | ||
|
|
||
|
|
||
| def calldatasize(instruction: Instruction): | ||
| opcode = instruction.opcode_lookup(True) | ||
| instruction.constrain_equal(opcode, Opcode.CALLDATASIZE) | ||
|
|
||
| # check [rw_table, call_context] table for call data length and compare | ||
| # against stack top after push. | ||
| instruction.constrain_equal( | ||
| instruction.int_to_rlc( | ||
| instruction.call_context_lookup(CallContextFieldTag.CallDataLength), | ||
| # NOTE: We can replace this with N_BYTES_WORD if we reuse the 32 | ||
| # byte RLC constraint in all places. See: | ||
| # https://github.com/appliedzkp/zkevm-specs/issues/101 | ||
| N_BYTES_MEMORY_ADDRESS, | ||
| ), | ||
| instruction.stack_push(), | ||
| ) | ||
|
|
||
| instruction.step_state_transition_in_same_context( | ||
| opcode, | ||
| rw_counter=Transition.delta(2), | ||
| program_counter=Transition.delta(1), | ||
| stack_pointer=Transition.delta(-1), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from ..instruction import Instruction, Transition | ||
| from ..table import CallContextFieldTag | ||
| from ..opcode import Opcode | ||
|
|
||
|
|
||
| def callvalue(instruction: Instruction): | ||
| opcode = instruction.opcode_lookup(True) | ||
| instruction.constrain_equal(opcode, Opcode.CALLVALUE) | ||
|
|
||
| # check [rw_table, call_context] table for call value and compare against | ||
| # stack top after push. | ||
| instruction.constrain_equal( | ||
| instruction.call_context_lookup(CallContextFieldTag.Value), | ||
| instruction.stack_push(), | ||
| ) | ||
|
|
||
| instruction.step_state_transition_in_same_context( | ||
| opcode, | ||
| rw_counter=Transition.delta(2), | ||
| program_counter=Transition.delta(1), | ||
| stack_pointer=Transition.delta(-1), | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import pytest | ||
|
|
||
| from zkevm_specs.evm import ( | ||
| ExecutionState, | ||
| StepState, | ||
| Opcode, | ||
| verify_steps, | ||
| Tables, | ||
| RWTableTag, | ||
| RW, | ||
| CallContextFieldTag, | ||
| Bytecode, | ||
| ) | ||
| from zkevm_specs.util import rand_fp, RLC, U64 | ||
| from zkevm_specs.util.param import N_BYTES_U64 | ||
|
|
||
|
|
||
| TESTING_DATA = ( | ||
| 0x00, | ||
| 0x10, | ||
| 0x302010, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("calldatasize", TESTING_DATA) | ||
| def test_calldatasize(calldatasize: U64): | ||
| randomness = rand_fp() | ||
|
|
||
| bytecode = Bytecode().calldatasize() | ||
| bytecode_hash = RLC(bytecode.hash(), randomness) | ||
|
|
||
| tables = Tables( | ||
| block_table=set(), | ||
| tx_table=set(), | ||
| bytecode_table=set(bytecode.table_assignments(randomness)), | ||
| rw_table=set( | ||
| [ | ||
| (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.CallDataLength, calldatasize, 0, 0), | ||
| (10, RW.Write, RWTableTag.Stack, 1, 1023, RLC(calldatasize, randomness, N_BYTES_U64), 0, 0), | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| verify_steps( | ||
| randomness=randomness, | ||
| tables=tables, | ||
| steps=[ | ||
| StepState( | ||
| execution_state=ExecutionState.CALLDATASIZE, | ||
| rw_counter=9, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=0, | ||
| stack_pointer=1024, | ||
| gas_left=2, | ||
| ), | ||
| StepState( | ||
| execution_state=ExecutionState.STOP, | ||
| rw_counter=11, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=1, | ||
| stack_pointer=1023, | ||
| gas_left=0, | ||
| ), | ||
| ], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import pytest | ||
|
|
||
| from zkevm_specs.evm import ( | ||
| ExecutionState, | ||
| StepState, | ||
| Opcode, | ||
| verify_steps, | ||
| Tables, | ||
| RWTableTag, | ||
| RW, | ||
| CallContextFieldTag, | ||
| Bytecode, | ||
| ) | ||
| from zkevm_specs.util import rand_fp, RLC, U256 | ||
| from zkevm_specs.util.param import N_BYTES_WORD | ||
|
|
||
|
|
||
| TESTING_DATA = ( | ||
| 0x00, | ||
| 0x10, | ||
| 0x302010, | ||
| 0xF0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0F, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("callvalue", TESTING_DATA) | ||
| def test_callvalue(callvalue: U256): | ||
| randomness = rand_fp() | ||
|
|
||
| callvalue_rlc = RLC(callvalue, randomness, N_BYTES_WORD) | ||
|
|
||
| bytecode = Bytecode().callvalue() | ||
| bytecode_hash = RLC(bytecode.hash(), randomness) | ||
|
|
||
| tables = Tables( | ||
| block_table=set(), | ||
| tx_table=set(), | ||
| bytecode_table=set(bytecode.table_assignments(randomness)), | ||
| rw_table=set( | ||
| [ | ||
| (9, RW.Read, RWTableTag.CallContext, 1, CallContextFieldTag.Value, callvalue_rlc, 0, 0), | ||
| (10, RW.Write, RWTableTag.Stack, 1, 1023, callvalue_rlc, 0, 0), | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| verify_steps( | ||
| randomness=randomness, | ||
| tables=tables, | ||
| steps=[ | ||
| StepState( | ||
| execution_state=ExecutionState.CALLVALUE, | ||
| rw_counter=9, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=0, | ||
| stack_pointer=1024, | ||
| gas_left=2, | ||
| ), | ||
| StepState( | ||
| execution_state=ExecutionState.STOP, | ||
| rw_counter=11, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=1, | ||
| stack_pointer=1023, | ||
| gas_left=0, | ||
| ), | ||
| ], | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ed255 Should we also test the scenario where this is not the root (
is_root=False) so that we testCALLDATASIZEfor internal calls as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's a very interesting case to test. Nevertheless I think we'll have to wait for this #82 to be merged so that we have
CALLs in order to achieve a test situation whereis_root=False.So I'd say that we can delay this for the future.