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 270
Spec for opcode ISZERO
#157
Merged
icemelon
merged 17 commits into
privacy-ethereum:master
from
scroll-tech:feat/opcode-is-zero
Mar 31, 2022
Merged
Changes from all commits
Commits
Show all changes
17 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 98fb255
simplify
0xmountaintop b0a815f
Update src/zkevm_specs/evm/execution/iszero.py
0xmountaintop 5480280
Merge remote-tracking branch 'upstream/master' into feat/opcode-is-zero
silathdiir 0bae65b
Fix to sum all limbs of a RLC then assert zero.
silathdiir 1f898ac
Merge remote-tracking branch 'upstream/master' into feat/opcode-is-zero
silathdiir e9ed211
Revert the code of sum all limbs of a RLC.
silathdiir 1de59c5
Update specs/opcode/15ISZERO.md
silathdiir efa7140
Update specs/opcode/15ISZERO.md
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,26 @@ | ||
| # ISZERO opcode | ||
|
|
||
| ## Procedure | ||
|
|
||
| Pop an EVM word `value` from the stack. If it is zero, push `1` back to the stack. Otherwise push `0` to stack. | ||
|
|
||
| ## Constraints | ||
|
|
||
| 1. state transition: | ||
| - gc + 2 (1 stack read + 1 stack write) | ||
| - stack_pointer + 0 (one pop and one push) | ||
| - pc + 1 | ||
| - gas + 3 | ||
| 2. 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: | ||
| - the stack is empty: `1024 == stack_pointer` | ||
| 2. out of gas: remaining gas is not enough | ||
|
|
||
| ## Code | ||
|
|
||
| See `src/zkevm_specs/evm/execution/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.same(), | ||
| ) | ||
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.