forked from privacy-ethereum/zkevm-specs
-
Notifications
You must be signed in to change notification settings - Fork 77
[WIP] opcode ISZERO
#40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c0f69d8
Implement opcode `ISZERO`.
silathdiir da3a5bf
Fix wrong delta for pointers.
silathdiir eff6770
Fix the wrong stack pointer.
silathdiir 3997634
refactor
0xmountaintop c05af4b
fix
0xmountaintop eda049b
Add markdown.
silathdiir cc182a3
Fix `stack underflow`.
silathdiir bd8efc9
Update specs/opcode/15ISZERO.md
silathdiir d1b99a8
Remove opcode check in markdown.
silathdiir 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # ISZERO opcodes | ||
|
|
||
| ## Procedure | ||
|
|
||
| ### EVM behavior | ||
|
|
||
| Pop an EVM word `value` from the stack. If it is zero, push `1` back to the stask. Otherwise push `0` to stack. | ||
|
|
||
| ### Circuit behavior | ||
|
|
||
| The IsZeroGadget takes argument of `value: [u8;32]` and `result: [u8;32]`. | ||
|
|
||
| If `value` is zero, it validates `result === 1`. Otherwise it validates `result === 0`. | ||
|
|
||
| We annotate stack as \[value, ...\] and \[result, ...\]. | ||
|
silathdiir marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Constraints | ||
|
|
||
| 1. opId = OpcodeId(0x15) | ||
|
silathdiir marked this conversation as resolved.
Outdated
|
||
| 2. state transition: | ||
| - gc + 2 (1 stack read + 1 stack write) | ||
| - stack_pointer + 0 (one pop and one push) | ||
| - pc + 1 | ||
| - gas + 3 | ||
| 3. Lookups: 2 busmapping lookups | ||
| - `value` is at the top of the stack | ||
| - `result`, is at the new top of the stack | ||
|
|
||
| ## Exceptions | ||
|
|
||
| 1. stack underflow: `1023 <= stack_pointer <= 1023` | ||
|
silathdiir marked this conversation as resolved.
Outdated
silathdiir marked this conversation as resolved.
Outdated
|
||
| 2. out of gas: remaining gas is not enough | ||
|
|
||
| ## Code | ||
|
|
||
| See `src/zkevm_specs/opcode/iszero.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from ..instruction import Instruction, Transition | ||
| from ..opcode import Opcode | ||
|
|
||
|
|
||
| def iszero(instruction: Instruction): | ||
| opcode = instruction.opcode_lookup(True) | ||
|
|
||
| value = instruction.stack_pop() | ||
|
|
||
| instruction.constrain_equal( | ||
| instruction.is_zero(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(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import pytest | ||
|
|
||
| from typing import Optional | ||
| from zkevm_specs.evm import ( | ||
| ExecutionState, | ||
| StepState, | ||
| Opcode, | ||
| verify_steps, | ||
| Tables, | ||
| Block, | ||
| Bytecode, | ||
| RWDictionary, | ||
| ) | ||
| from zkevm_specs.util import rand_fq, rand_word, RLC | ||
|
|
||
|
|
||
| TESTING_DATA = ( | ||
| bytes([0]), | ||
| bytes([7]), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value_be_bytes", TESTING_DATA) | ||
| def test_iszero(value_be_bytes: bytes): | ||
| randomness = rand_fq() | ||
|
|
||
| value = int.from_bytes(value_be_bytes, "big") | ||
| result = 0x1 if value == 0x0 else 0x0 | ||
| value = RLC(value, randomness) | ||
| result = RLC(result, randomness) | ||
|
|
||
| bytecode = Bytecode().push1(value_be_bytes).iszero().stop() | ||
| bytecode_hash = RLC(bytecode.hash(), randomness) | ||
|
|
||
| tables = Tables( | ||
| block_table=set(Block().table_assignments(randomness)), | ||
| tx_table=set(), | ||
| bytecode_table=set(bytecode.table_assignments(randomness)), | ||
| rw_table=set(RWDictionary(9).stack_read(1, 1023, value).stack_write(1, 1023, result).rws), | ||
| ) | ||
|
|
||
| verify_steps( | ||
| randomness=randomness, | ||
| tables=tables, | ||
| steps=[ | ||
| StepState( | ||
| execution_state=ExecutionState.ISZERO, | ||
| rw_counter=9, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=2, | ||
| stack_pointer=1023, | ||
| gas_left=3, | ||
| ), | ||
| StepState( | ||
| execution_state=ExecutionState.STOP, | ||
| rw_counter=11, | ||
| call_id=1, | ||
| is_root=True, | ||
| is_create=False, | ||
| code_source=bytecode_hash, | ||
| program_counter=3, | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.