forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add circuit for RETURNDATASIZE opcode (privacy-scaling-explorations#703)
* Add circuit for RETURNDATASIZE opcode * Update tests * update last call info in return (#2) Signed-off-by: smtmfft <[email protected]> Signed-off-by: smtmfft <[email protected]> * Update tests * Update tests * Update format in zkevm-circuits/src/evm_circuit/execution.rs * Change unwrap to expect in returndatasize.rs * Add circuit for RETURNDATASIZE opcode * Update tests * update last call info in return (#2) Signed-off-by: smtmfft <[email protected]> Signed-off-by: smtmfft <[email protected]> * Update tests * Update tests * Update format in zkevm-circuits/src/evm_circuit/execution.rs * Change unwrap to expect in returndatasize.rs * according to RETURN spec, callee info update happens only in case C, (#3) i.e., only if it is not root call. RETURN spec: The `RETURN` opcode terminates the call successfully with return data for the caller. It behaves differently in different scenarios: - `is_create` and `is_root` - A. Returns the specified memory chunk as deployment code. - B. End the execution - `is_create` and `not is_root` - A. Returns the specified memory chunk as deployment code. - C. Restores caller's context and switch to it. - `not is_create` and `is_root` - B. End the execution - `not is_create` and `not is_root` - D. Returns the specified memory chunk to the caller. - C. Restores caller's context and switch to it. Signed-off-by: smtmfft <[email protected]> Signed-off-by: smtmfft <[email protected]> Co-authored-by: smtmfft <[email protected]> * Fix the issues with rustfmt * Fix merge conflicts * Update * Add tests * Resolve merge conflicts * Update bus-mapping return.rs, use N_BYTES_U64 for return_data_size and update tests * Revert change in bus-mapping return * Update bus-mapping returndatasize_tests Signed-off-by: smtmfft <[email protected]> Signed-off-by: smtmfft <[email protected]> Co-authored-by: smtmfft <[email protected]> Co-authored-by: smtmfft <[email protected]> Co-authored-by: Rohit Narurkar <[email protected]> Co-authored-by: Carlos Pérez <[email protected]> Co-authored-by: z2trillion <[email protected]> Co-authored-by: adria0.eth <[email protected]>
- Loading branch information
1 parent
044d995
commit 103ddd7
Showing
5 changed files
with
343 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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,163 @@ | ||
use crate::{ | ||
circuit_input_builder::{CircuitInputStateRef, ExecStep}, | ||
operation::CallContextField, | ||
Error, | ||
}; | ||
|
||
use eth_types::GethExecStep; | ||
|
||
use super::Opcode; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub(crate) struct Returndatasize; | ||
|
||
impl Opcode for Returndatasize { | ||
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)?; | ||
let value = geth_steps[1].stack.last()?; | ||
state.call_context_read( | ||
&mut exec_step, | ||
state.call()?.call_id, | ||
CallContextField::LastCalleeReturnDataLength, | ||
value, | ||
); | ||
|
||
state.stack_write( | ||
&mut exec_step, | ||
geth_step.stack.last_filled().map(|a| a - 1), | ||
value, | ||
)?; | ||
|
||
Ok(vec![exec_step]) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod returndatasize_tests { | ||
use crate::circuit_input_builder::CircuitsParams; | ||
use crate::{ | ||
circuit_input_builder::ExecState, | ||
mock::BlockData, | ||
operation::{CallContextField, CallContextOp, StackOp, RW}, | ||
}; | ||
use eth_types::{ | ||
bytecode, | ||
evm_types::{OpcodeId, StackAddress}, | ||
geth_types::GethData, | ||
word, Word, | ||
}; | ||
use mock::test_ctx::{helpers::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn test_ok() { | ||
let return_data_size = 0x20; | ||
|
||
// // deployed contract | ||
// PUSH1 0x20 | ||
// PUSH1 0 | ||
// PUSH1 0 | ||
// CALLDATACOPY | ||
// PUSH1 0x20 | ||
// PUSH1 0 | ||
// RETURN | ||
// | ||
// bytecode: 0x6020600060003760206000F3 | ||
// | ||
// // constructor | ||
// PUSH12 0x6020600060003760206000F3 | ||
// PUSH1 0 | ||
// MSTORE | ||
// PUSH1 0xC | ||
// PUSH1 0x14 | ||
// RETURN | ||
// | ||
// bytecode: 0x6B6020600060003760206000F3600052600C6014F3 | ||
let code = bytecode! { | ||
PUSH21(word!("6B6020600060003760206000F3600052600C6014F3")) | ||
PUSH1(0) | ||
MSTORE | ||
|
||
PUSH1 (0x15) | ||
PUSH1 (0xB) | ||
PUSH1 (0) | ||
CREATE | ||
|
||
PUSH1 (0x20) | ||
PUSH1 (0x20) | ||
PUSH1 (0x20) | ||
PUSH1 (0) | ||
PUSH1 (0) | ||
DUP6 | ||
PUSH2 (0xFFFF) | ||
CALL | ||
|
||
RETURNDATASIZE | ||
|
||
STOP | ||
}; | ||
// Get the execution steps from the external tracer | ||
let block: GethData = TestContext::<2, 1>::new( | ||
None, | ||
account_0_code_account_1_no_code(code), | ||
tx_from_1_to_0, | ||
|block, _tx| block.number(0xcafeu64), | ||
) | ||
.unwrap() | ||
.into(); | ||
|
||
let mut builder = BlockData::new_from_geth_data_with_params( | ||
block.clone(), | ||
CircuitsParams { | ||
max_rws: 512, | ||
..Default::default() | ||
}, | ||
) | ||
.new_circuit_input_builder(); | ||
builder | ||
.handle_block(&block.eth_block, &block.geth_traces) | ||
.unwrap(); | ||
|
||
let step = builder.block.txs()[0] | ||
.steps() | ||
.iter() | ||
.find(|step| step.exec_state == ExecState::Op(OpcodeId::RETURNDATASIZE)) | ||
.unwrap(); | ||
|
||
let call_id = builder.block.txs()[0].calls()[0].call_id; | ||
assert_eq!( | ||
{ | ||
let operation = | ||
&builder.block.container.call_context[step.bus_mapping_instance[0].as_usize()]; | ||
(operation.rw(), operation.op()) | ||
}, | ||
( | ||
RW::READ, | ||
&CallContextOp { | ||
call_id, | ||
field: CallContextField::LastCalleeReturnDataLength, | ||
value: Word::from(return_data_size), | ||
} | ||
) | ||
); | ||
assert_eq!( | ||
{ | ||
let operation = | ||
&builder.block.container.stack[step.bus_mapping_instance[1].as_usize()]; | ||
(operation.rw(), operation.op()) | ||
}, | ||
( | ||
RW::WRITE, | ||
&StackOp::new( | ||
call_id, | ||
StackAddress::from(1021), | ||
Word::from(return_data_size) | ||
) | ||
) | ||
); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.