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
Gadget for opcode calldatasize #329
Merged
ed255
merged 4 commits into
privacy-ethereum:main
from
scroll-tech:feat/op-calldatasize
Feb 23, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
602fe20
feat: calldatasize (squashed and rebased)
roynalnaruto 69f59b7
fix: stack values are rlc encoded
roynalnaruto 8fd8442
fix: call context is not rlc encoded while stack is
roynalnaruto 851a7f2
Merge branch 'main' into feat/op-calldatasize
roynalnaruto 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,111 @@ | ||
| use crate::{ | ||
| circuit_input_builder::CircuitInputStateRef, | ||
| operation::{CallContextField, CallContextOp, RW}, | ||
| Error, | ||
| }; | ||
|
|
||
| use eth_types::GethExecStep; | ||
|
|
||
| use super::Opcode; | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| pub(crate) struct Calldatasize; | ||
|
|
||
| impl Opcode for Calldatasize { | ||
| fn gen_associated_ops( | ||
| state: &mut CircuitInputStateRef, | ||
| steps: &[GethExecStep], | ||
| ) -> Result<(), Error> { | ||
| let step = &steps[0]; | ||
| let value = steps[1].stack.last()?; | ||
| state.push_op( | ||
| RW::READ, | ||
| CallContextOp { | ||
| call_id: state.call().call_id, | ||
| field: CallContextField::CallDataLength, | ||
| value, | ||
| }, | ||
| ); | ||
| state.push_stack_op(RW::WRITE, step.stack.last_filled().map(|a| a - 1), value); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod calldatasize_tests { | ||
| use crate::{ | ||
| circuit_input_builder::{ExecStep, TransactionContext}, | ||
| mock::BlockData, | ||
| operation::{CallContextField, CallContextOp, RW}, | ||
| Error, | ||
| }; | ||
| use eth_types::bytecode; | ||
| use eth_types::evm_types::StackAddress; | ||
| use mock::new_single_tx_trace_code_at_start; | ||
| use pretty_assertions::assert_eq; | ||
|
|
||
| #[test] | ||
| fn calldatasize_opcode_impl() -> Result<(), Error> { | ||
| let code = bytecode! { | ||
| #[start] | ||
| CALLDATASIZE | ||
| STOP | ||
| }; | ||
|
|
||
| // Get the execution steps from the external tracer | ||
| let block = | ||
| BlockData::new_from_geth_data(new_single_tx_trace_code_at_start(&code).unwrap()); | ||
|
|
||
| let mut builder = block.new_circuit_input_builder(); | ||
| builder.handle_tx(&block.eth_tx, &block.geth_trace).unwrap(); | ||
|
|
||
| let mut test_builder = block.new_circuit_input_builder(); | ||
| let mut tx = test_builder | ||
| .new_tx(&block.eth_tx, !block.geth_trace.failed) | ||
| .unwrap(); | ||
| let mut tx_ctx = TransactionContext::new(&block.eth_tx, &block.geth_trace).unwrap(); | ||
|
|
||
| // Generate step corresponding to CALLDATASIZE | ||
| let mut step = ExecStep::new( | ||
| &block.geth_trace.struct_logs[0], | ||
| 0, | ||
| test_builder.block_ctx.rwc, | ||
| 0, | ||
| ); | ||
| let mut state_ref = test_builder.state_ref(&mut tx, &mut tx_ctx, &mut step); | ||
|
|
||
| // Get calldatasize from eth tx. | ||
| let call_data_size = block.eth_tx.input.to_vec().len(); | ||
|
|
||
| // Add the read operation. | ||
| state_ref.push_op( | ||
| RW::READ, | ||
| CallContextOp { | ||
| call_id: state_ref.call().call_id, | ||
| field: CallContextField::CallDataLength, | ||
| value: eth_types::U256::from(call_data_size), | ||
| }, | ||
| ); | ||
|
|
||
| // Add the stack write. | ||
| state_ref.push_stack_op( | ||
| RW::WRITE, | ||
| StackAddress::from(1024 - 1), | ||
| eth_types::U256::from(call_data_size), | ||
| ); | ||
|
|
||
| tx.steps_mut().push(step); | ||
| test_builder.block.txs_mut().push(tx); | ||
|
|
||
| // Compare first step bus mapping instance | ||
| assert_eq!( | ||
| builder.block.txs()[0].steps()[0].bus_mapping_instance, | ||
| test_builder.block.txs()[0].steps()[0].bus_mapping_instance, | ||
| ); | ||
|
|
||
| // Compare containers | ||
| assert_eq!(builder.block.container, test_builder.block.container); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
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
198 changes: 198 additions & 0 deletions
198
zkevm-circuits/src/evm_circuit/execution/calldatasize.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,198 @@ | ||
| use std::convert::TryInto; | ||
|
|
||
| use eth_types::ToLittleEndian; | ||
| use halo2::{arithmetic::FieldExt, circuit::Region, plonk::Error}; | ||
|
|
||
| use crate::{ | ||
| evm_circuit::{ | ||
| param::N_BYTES_CALLDATASIZE, | ||
| step::ExecutionState, | ||
| table::CallContextFieldTag, | ||
| util::{ | ||
| common_gadget::SameContextGadget, | ||
| constraint_builder::{ConstraintBuilder, StepStateTransition, Transition}, | ||
| from_bytes, RandomLinearCombination, | ||
| }, | ||
| witness::{Block, Call, ExecStep, Transaction}, | ||
| }, | ||
| util::Expr, | ||
| }; | ||
|
|
||
| use super::ExecutionGadget; | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub(crate) struct CallDataSizeGadget<F> { | ||
| same_context: SameContextGadget<F>, | ||
| call_data_size: RandomLinearCombination<F, N_BYTES_CALLDATASIZE>, | ||
| } | ||
|
|
||
| impl<F: FieldExt> ExecutionGadget<F> for CallDataSizeGadget<F> { | ||
| const NAME: &'static str = "CALLDATASIZE"; | ||
|
|
||
| const EXECUTION_STATE: ExecutionState = ExecutionState::CALLDATASIZE; | ||
|
|
||
| fn configure(cb: &mut ConstraintBuilder<F>) -> Self { | ||
| let opcode = cb.query_cell(); | ||
|
|
||
| // Add lookup constraint in the call context for the calldatasize field. | ||
| let call_data_size = cb.query_rlc(); | ||
| cb.call_context_lookup( | ||
| false.expr(), | ||
| None, | ||
| CallContextFieldTag::CallDataLength, | ||
| from_bytes::expr(&call_data_size.cells), | ||
| ); | ||
|
|
||
| // The calldatasize should be pushed to the top of the stack. | ||
| cb.stack_push(call_data_size.expr()); | ||
|
|
||
| let step_state_transition = StepStateTransition { | ||
| rw_counter: Transition::Delta(2.expr()), | ||
| program_counter: Transition::Delta(1.expr()), | ||
| stack_pointer: Transition::Delta((-1).expr()), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let same_context = SameContextGadget::construct(cb, opcode, step_state_transition, None); | ||
|
|
||
| Self { | ||
| same_context, | ||
| call_data_size, | ||
| } | ||
| } | ||
|
|
||
| fn assign_exec_step( | ||
| &self, | ||
| region: &mut Region<'_, F>, | ||
| offset: usize, | ||
| block: &Block<F>, | ||
| _tx: &Transaction, | ||
| _call: &Call, | ||
| step: &ExecStep, | ||
| ) -> Result<(), Error> { | ||
| self.same_context.assign_exec_step(region, offset, step)?; | ||
|
|
||
| let call_data_size = block.rws[step.rw_indices[1]].stack_value(); | ||
|
|
||
| self.call_data_size.assign( | ||
| region, | ||
| offset, | ||
| Some( | ||
| call_data_size.to_le_bytes()[..N_BYTES_CALLDATASIZE] | ||
| .try_into() | ||
| .unwrap(), | ||
| ), | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use std::collections::HashMap; | ||
|
|
||
| use bus_mapping::evm::OpcodeId; | ||
| use eth_types::{bytecode, Word}; | ||
| use halo2::arithmetic::BaseExt; | ||
| use pairing::bn256::Fr; | ||
|
|
||
| use crate::evm_circuit::{ | ||
| step::ExecutionState, | ||
| table::{CallContextFieldTag, RwTableTag}, | ||
| test::{rand_bytes, run_test_circuit_incomplete_fixed_table}, | ||
| witness::{Block, Bytecode, Call, CodeSource, ExecStep, Rw, RwMap, Transaction}, | ||
| }; | ||
|
|
||
| fn test_ok(call_data_size: usize, is_root: bool) { | ||
| let randomness = Fr::rand(); | ||
| let bytecode = bytecode! { | ||
| #[start] | ||
| CALLDATASIZE | ||
| STOP | ||
| }; | ||
| let bytecode = Bytecode::new(bytecode.to_vec()); | ||
| let call_id = 1; | ||
| let call_data = rand_bytes(call_data_size); | ||
|
|
||
| let mut rw_map = HashMap::new(); | ||
| rw_map.insert( | ||
| RwTableTag::CallContext, | ||
| vec![Rw::CallContext { | ||
| rw_counter: 9, | ||
| is_write: false, | ||
| call_id, | ||
| field_tag: CallContextFieldTag::CallDataLength, | ||
| value: Word::from(call_data_size), | ||
| }], | ||
| ); | ||
| rw_map.insert( | ||
| RwTableTag::Stack, | ||
| vec![Rw::Stack { | ||
| rw_counter: 10, | ||
| is_write: true, | ||
| call_id, | ||
| stack_pointer: 1023, | ||
| value: Word::from(call_data_size), | ||
|
ed255 marked this conversation as resolved.
|
||
| }], | ||
| ); | ||
|
|
||
| let steps = vec![ | ||
| ExecStep { | ||
| execution_state: ExecutionState::CALLDATASIZE, | ||
| rw_indices: vec![(RwTableTag::CallContext, 0), (RwTableTag::Stack, 0)], | ||
| rw_counter: 9, | ||
| program_counter: 0, | ||
| stack_pointer: 1024, | ||
| gas_left: OpcodeId::CALLDATASIZE.constant_gas_cost().as_u64(), | ||
| gas_cost: OpcodeId::CALLDATASIZE.constant_gas_cost().as_u64(), | ||
| opcode: Some(OpcodeId::CALLDATASIZE), | ||
| ..Default::default() | ||
| }, | ||
| ExecStep { | ||
| execution_state: ExecutionState::STOP, | ||
| rw_counter: 11, | ||
| program_counter: 1, | ||
| stack_pointer: 1023, | ||
| gas_left: 0, | ||
| opcode: Some(OpcodeId::STOP), | ||
| ..Default::default() | ||
| }, | ||
| ]; | ||
|
|
||
| let block = Block { | ||
| randomness, | ||
| txs: vec![Transaction { | ||
| id: 1, | ||
| call_data, | ||
| call_data_length: call_data_size, | ||
| steps, | ||
| calls: vec![Call { | ||
| id: call_id, | ||
| is_root, | ||
| is_create: false, | ||
| call_data_length: call_data_size as u64, | ||
| code_source: CodeSource::Account(bytecode.hash), | ||
| ..Default::default() | ||
| }], | ||
| ..Default::default() | ||
| }], | ||
| rws: RwMap(rw_map), | ||
| bytecodes: vec![bytecode], | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| assert_eq!(run_test_circuit_incomplete_fixed_table(block), Ok(())); | ||
| } | ||
|
|
||
| #[test] | ||
| fn calldatasize_gadget_root() { | ||
| test_ok(32, true); | ||
| test_ok(64, true); | ||
| test_ok(96, true); | ||
| test_ok(128, true); | ||
| test_ok(256, true); | ||
| test_ok(512, true); | ||
| test_ok(1024, true); | ||
| } | ||
| } | ||
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.