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 839
Circuit for ErrorInvalidOpcode state
#1089
Merged
han0110
merged 13 commits into
privacy-ethereum:main
from
scroll-tech:execution/error-invalid-opcode
Feb 10, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
491ce31
Implement `ErrorInvalidOpcode` state.
silathdiir 93ce388
Replace `1 - *` with `not::expr`.
silathdiir 2fd0815
Merge branch 'main' into execution/error-invalid-opcode
silathdiir 78fb30e
Merge branch 'main' into execution/error-invalid-opcode
silathdiir 537748b
Add a `LtGadget` to avoid mock prover assigning a value which is grea…
silathdiir 5f45dac
Merge branch 'main' into execution/error-invalid-opcode
silathdiir 915e869
Revert "Add a `LtGadget` to avoid mock prover assigning a value which…
silathdiir bdd39e5
Assign InvalidOpcode to `exec_step.error` directly. Since it has been…
silathdiir 759ed13
Delete `internal` test cases since it has no effects for invalid opco…
silathdiir 1a36b45
Delete mapping `OpcodeId::INVALID` to dummy function in `fn_gen_assoc…
silathdiir dc34c79
Revert "Delete `internal` test cases since it has no effects for inva…
silathdiir b8cbf2e
Fix to only keep one call data offset and length pair for internal test.
silathdiir 5333f8e
Merge branch 'main' into execution/error-invalid-opcode
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
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,24 @@ | ||
| use crate::circuit_input_builder::{CircuitInputStateRef, ExecStep}; | ||
| use crate::error::ExecError; | ||
| use crate::evm::Opcode; | ||
| use crate::Error; | ||
| use eth_types::GethExecStep; | ||
|
|
||
| #[derive(Debug, Copy, Clone)] | ||
| pub(crate) struct InvalidOpcode; | ||
|
|
||
| impl Opcode for InvalidOpcode { | ||
| fn gen_associated_ops( | ||
| state: &mut CircuitInputStateRef, | ||
| geth_steps: &[GethExecStep], | ||
| ) -> Result<Vec<ExecStep>, Error> { | ||
| let geth_step = &geth_steps[0]; | ||
| let mut exec_step = state.new_step(geth_step)?; | ||
| exec_step.error = Some(ExecError::InvalidOpcode); | ||
|
|
||
| state.gen_restore_context_ops(&mut exec_step, geth_steps)?; | ||
| state.handle_return(geth_step)?; | ||
|
|
||
| Ok(vec![exec_step]) | ||
| } | ||
| } |
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
219 changes: 219 additions & 0 deletions
219
zkevm-circuits/src/evm_circuit/execution/error_invalid_opcode.rs
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,219 @@ | ||
| use crate::evm_circuit::execution::ExecutionGadget; | ||
| use crate::evm_circuit::step::ExecutionState; | ||
| use crate::evm_circuit::table::{FixedTableTag, Lookup}; | ||
| use crate::evm_circuit::util::common_gadget::RestoreContextGadget; | ||
| use crate::evm_circuit::util::constraint_builder::Transition::{Delta, Same}; | ||
| use crate::evm_circuit::util::constraint_builder::{ConstraintBuilder, StepStateTransition}; | ||
| use crate::evm_circuit::util::{not, CachedRegion, Cell}; | ||
| use crate::evm_circuit::witness::{Block, Call, ExecStep, Transaction}; | ||
| use crate::table::CallContextFieldTag; | ||
| use eth_types::Field; | ||
| use gadgets::util::Expr; | ||
| use halo2_proofs::circuit::Value; | ||
| use halo2_proofs::plonk::Error; | ||
|
|
||
| /// Gadget for invalid opcodes. It verifies by a fixed lookup for | ||
| /// ResponsibleOpcode. | ||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct ErrorInvalidOpcodeGadget<F> { | ||
| opcode: Cell<F>, | ||
| rw_counter_end_of_reversion: Cell<F>, | ||
| restore_context: RestoreContextGadget<F>, | ||
| } | ||
|
|
||
| impl<F: Field> ExecutionGadget<F> for ErrorInvalidOpcodeGadget<F> { | ||
| const NAME: &'static str = "ErrorInvalidOpcode"; | ||
|
|
||
| const EXECUTION_STATE: ExecutionState = ExecutionState::ErrorInvalidOpcode; | ||
|
|
||
| fn configure(cb: &mut ConstraintBuilder<F>) -> Self { | ||
| let opcode = cb.query_cell(); | ||
| cb.opcode_lookup(opcode.expr(), 1.expr()); | ||
| cb.add_lookup( | ||
| "Responsible opcode lookup", | ||
| Lookup::Fixed { | ||
| tag: FixedTableTag::ResponsibleOpcode.expr(), | ||
| values: [ | ||
| Self::EXECUTION_STATE.as_u64().expr(), | ||
| opcode.expr(), | ||
| 0.expr(), | ||
| ], | ||
| }, | ||
| ); | ||
|
|
||
| // Current call must be failed. | ||
| let rw_counter_end_of_reversion = cb.query_cell(); | ||
| cb.call_context_lookup(false.expr(), None, CallContextFieldTag::IsSuccess, 0.expr()); | ||
| cb.call_context_lookup( | ||
| false.expr(), | ||
| None, | ||
| CallContextFieldTag::RwCounterEndOfReversion, | ||
| rw_counter_end_of_reversion.expr(), | ||
| ); | ||
|
|
||
| // Go to EndTx only when is_root. | ||
| let is_to_end_tx = cb.next.execution_state_selector([ExecutionState::EndTx]); | ||
| cb.require_equal( | ||
| "Go to EndTx only when is_root", | ||
| cb.curr.state.is_root.expr(), | ||
| is_to_end_tx, | ||
| ); | ||
|
|
||
| // When it's a root call. | ||
| cb.condition(cb.curr.state.is_root.expr(), |cb| { | ||
| // Do step state transition | ||
| cb.require_step_state_transition(StepStateTransition { | ||
| call_id: Same, | ||
| rw_counter: Delta(2.expr() + cb.curr.state.reversible_write_counter.expr()), | ||
| ..StepStateTransition::any() | ||
| }); | ||
| }); | ||
|
|
||
| // When it is an internal call, need to restore caller's state as finishing this | ||
| // call. Restore caller state to next StepState. | ||
| let restore_context = cb.condition(not::expr(cb.curr.state.is_root.expr()), |cb| { | ||
| RestoreContextGadget::construct( | ||
| cb, | ||
| 0.expr(), | ||
| 0.expr(), | ||
| 0.expr(), | ||
| 0.expr(), | ||
| 0.expr(), | ||
| 0.expr(), | ||
| ) | ||
| }); | ||
|
|
||
| // Constrain `RwCounterEndOfReversion`. | ||
| let rw_counter_end_of_step = | ||
| cb.curr.state.rw_counter.expr() + cb.rw_counter_offset() - 1.expr(); | ||
| cb.require_equal( | ||
| "rw_counter_end_of_reversion = rw_counter_end_of_step + reversible_counter", | ||
| rw_counter_end_of_reversion.expr(), | ||
| rw_counter_end_of_step + cb.curr.state.reversible_write_counter.expr(), | ||
| ); | ||
|
|
||
| Self { | ||
| opcode, | ||
| rw_counter_end_of_reversion, | ||
| restore_context, | ||
| } | ||
| } | ||
|
|
||
| fn assign_exec_step( | ||
| &self, | ||
| region: &mut CachedRegion<'_, '_, F>, | ||
| offset: usize, | ||
| block: &Block<F>, | ||
| _: &Transaction, | ||
| call: &Call, | ||
| step: &ExecStep, | ||
| ) -> Result<(), Error> { | ||
| let opcode = F::from(step.opcode.unwrap().as_u64()); | ||
| self.opcode.assign(region, offset, Value::known(opcode))?; | ||
|
|
||
| self.rw_counter_end_of_reversion.assign( | ||
| region, | ||
| offset, | ||
| Value::known(F::from(call.rw_counter_end_of_reversion as u64)), | ||
| )?; | ||
|
|
||
| self.restore_context | ||
| .assign(region, offset, block, call, step, 2) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use crate::evm_circuit::test::rand_bytes; | ||
| use crate::test_util::CircuitTestBuilder; | ||
| use eth_types::bytecode::Bytecode; | ||
| use eth_types::{bytecode, ToWord, Word}; | ||
| use lazy_static::lazy_static; | ||
| use mock::TestContext; | ||
|
|
||
| lazy_static! { | ||
| static ref TESTING_INVALID_CODES: [Vec<u8>; 6] = [ | ||
| // Single invalid opcode | ||
| vec![0x0e], | ||
| vec![0x4f], | ||
| vec![0xa5], | ||
| vec![0xf6], | ||
| vec![0xfe], | ||
| // Multiple invalid opcodes | ||
| vec![0x5c, 0x5e, 0x5f], | ||
| ]; | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_opcode_root() { | ||
| for invalid_code in TESTING_INVALID_CODES.iter() { | ||
| test_root_ok(invalid_code); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_opcode_internal() { | ||
| for invalid_code in TESTING_INVALID_CODES.iter() { | ||
| test_internal_ok(0x20, 0x00, invalid_code); | ||
| } | ||
| } | ||
|
|
||
| fn test_root_ok(invalid_code: &[u8]) { | ||
| let mut code = Bytecode::default(); | ||
| invalid_code.iter().for_each(|b| { | ||
| code.write(*b, true); | ||
| }); | ||
|
|
||
| CircuitTestBuilder::new_from_test_ctx( | ||
| TestContext::<2, 1>::simple_ctx_with_bytecode(code).unwrap(), | ||
| ) | ||
| .run(); | ||
| } | ||
|
|
||
| fn test_internal_ok(call_data_offset: usize, call_data_length: usize, invalid_code: &[u8]) { | ||
| let (addr_a, addr_b) = (mock::MOCK_ACCOUNTS[0], mock::MOCK_ACCOUNTS[1]); | ||
|
|
||
| // Code B gets called by code A, so the call is an internal call. | ||
| let mut code_b = Bytecode::default(); | ||
| invalid_code.iter().for_each(|b| { | ||
| code_b.write(*b, true); | ||
| }); | ||
|
|
||
| // code A calls code B. | ||
| let pushdata = rand_bytes(8); | ||
| let code_a = bytecode! { | ||
| // populate memory in A's context. | ||
| PUSH8(Word::from_big_endian(&pushdata)) | ||
| PUSH1(0x00) // offset | ||
| MSTORE | ||
| // call ADDR_B. | ||
| PUSH1(0x00) // retLength | ||
| PUSH1(0x00) // retOffset | ||
| PUSH32(call_data_length) // argsLength | ||
| PUSH32(call_data_offset) // argsOffset | ||
| PUSH1(0x00) // value | ||
| PUSH32(addr_b.to_word()) // addr | ||
| PUSH32(0x1_0000) // gas | ||
| CALL | ||
| STOP | ||
| }; | ||
|
|
||
| let ctx = TestContext::<3, 1>::new( | ||
| None, | ||
| |accs| { | ||
| accs[0].address(addr_b).code(code_b); | ||
| accs[1].address(addr_a).code(code_a); | ||
| accs[2] | ||
| .address(mock::MOCK_ACCOUNTS[3]) | ||
| .balance(Word::from(1_u64 << 20)); | ||
| }, | ||
| |mut txs, accs| { | ||
| txs[0].to(accs[1].address).from(accs[2].address); | ||
| }, | ||
| |block, _tx| block, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| CircuitTestBuilder::new_from_test_ctx(ctx).run(); | ||
| } | ||
| } | ||
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
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.