Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0152b2f
wip
Aug 18, 2022
fa58c80
Cleanup and implement ToScalar for bool
Aug 18, 2022
214da7e
Remove extra is_success rw lookup
Aug 18, 2022
c01fec4
Constraint copy length
Aug 18, 2022
d6f4a27
clean up comments
Aug 18, 2022
16dde29
clippy and use correct values for copy event
Aug 18, 2022
8eebb45
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 17, 2022
39557be
wip
Sep 19, 2022
f84dfd0
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 19, 2022
b96ff25
cleanup
Sep 19, 2022
7503d6e
cleanup
Sep 19, 2022
7ca57ed
Add create copy event
Sep 20, 2022
7581924
wip
Sep 20, 2022
790025e
wip
Sep 21, 2022
12c3aa6
wip
Sep 22, 2022
3af27ed
cleanup
Sep 22, 2022
f7a3ca8
check for 0 copy length
Sep 22, 2022
267047a
Remove nonroot from name
Sep 22, 2022
4b31b4b
Update MemoryAddressGadget comments
Sep 22, 2022
6d3caa3
cleanup
Sep 22, 2022
5314a17
cleanup
Sep 22, 2022
4837ea7
Update todo
Sep 22, 2022
a96e3eb
cleanup
Sep 22, 2022
2a4e4ec
Cleanup bus mapping
Sep 22, 2022
e2a801c
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 22, 2022
b68defe
Merge branch 'main' into feat/return_cleanup
roynalnaruto Sep 23, 2022
de196d9
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 23, 2022
58e744d
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 26, 2022
a90e5e5
Don't make a copy event if nothing is copied
Sep 22, 2022
0a78b0b
Update to match specs
Sep 27, 2022
82b058f
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 27, 2022
7a3e705
Use built in call id and cleanup
Sep 28, 2022
1726b73
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 28, 2022
832c6c0
Add is_persistent constraint
Sep 28, 2022
8eafc1b
Use is_root and is_create from cb
Sep 28, 2022
7e292e5
Merge branch 'main' into feat/return_cleanup
z2trillion Sep 29, 2022
3b1dcfb
Constrain step state transition when root
Sep 29, 2022
1c3f6d1
Add memory expansion gadget
Sep 29, 2022
d1e2a8a
Constrain gas left and memory word size for root
Sep 30, 2022
cb08931
Calculate gas left correctly
Sep 30, 2022
3949303
cleanup
Sep 30, 2022
6c3568e
Use correct new memory word size
Sep 30, 2022
3312b9c
Rename input variable
Sep 30, 2022
6019bed
Add condition for is_create in rw_counter increase
Sep 30, 2022
ea16210
Merge branch 'main' into feat/return_cleanup
roynalnaruto Oct 5, 2022
98c8cc2
Fix test and cleanup
Oct 5, 2022
060ca43
Merge branch 'main' into feat/return_cleanup
z2trillion Oct 5, 2022
2fb9fd8
Set enable_memory default to false
Oct 5, 2022
834f65b
Constrain is_success to be boolean
Oct 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 98 additions & 4 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
Error,
};
use eth_types::{
evm_types::{Gas, MemoryAddress, OpcodeId, StackAddress},
Address, GethExecStep, ToAddress, ToBigEndian, Word, H256,
evm_types::{gas_utils::memory_expansion_gas_cost, Gas, MemoryAddress, OpcodeId, StackAddress},
Address, GethExecStep, ToAddress, ToBigEndian, ToWord, Word, H256,
};
use ethers_core::utils::{get_contract_address, get_create2_address};
use std::cmp::max;
Expand Down Expand Up @@ -814,8 +814,8 @@ impl<'a> CircuitInputStateRef<'a> {
callee_account.code_hash = code_hash;
}

// Handle reversion if this call doens't end successfully
if !self.call()?.is_success {
// Handle reversion if this call doesn't end successfully
if !call.is_success {
self.handle_reversion();
}

Expand All @@ -824,6 +824,100 @@ impl<'a> CircuitInputStateRef<'a> {
Ok(())
}

/// Bus mapping for the RestoreContextGadget as used in RETURN.
// TODO: unify this with restore context bus mapping for STOP.
// TODO: unify this with the `handle return function above.`
pub fn handle_restore_context(
&mut self,
steps: &[GethExecStep],
exec_step: &mut ExecStep,
) -> Result<(), Error> {
let call = self.call()?.clone();
let caller = self.caller()?.clone();
self.call_context_read(
exec_step,
call.call_id,
CallContextField::CallerId,
caller.call_id.into(),
);

let geth_step = &steps[0];
let geth_step_next = &steps[1];

let [last_callee_return_data_offset, last_callee_return_data_length] = match geth_step.op {
OpcodeId::STOP => [Word::zero(); 2],
OpcodeId::REVERT | OpcodeId::RETURN => {
let offset = geth_step.stack.nth_last(0)?;
let length = geth_step.stack.nth_last(1)?;
// This is the convention we are using for memory addresses so that there is no
// memory expansion cost when the length is 0.
if length.is_zero() {
[Word::zero(); 2]
} else {
[offset, length]
}
}
_ => unreachable!(),
};

let curr_memory_word_size = (exec_step.memory_size as u64) / 32;
let next_memory_word_size = if !last_callee_return_data_length.is_zero() {
std::cmp::max(
(last_callee_return_data_offset + last_callee_return_data_length + 31).as_u64()
/ 32,
curr_memory_word_size,
)
} else {
curr_memory_word_size
};

let memory_expansion_gas_cost =
memory_expansion_gas_cost(curr_memory_word_size, next_memory_word_size);
let gas_refund = geth_step.gas.0 - memory_expansion_gas_cost;
let caller_gas_left = geth_step_next.gas.0 - gas_refund;

for (field, value) in [
(CallContextField::IsRoot, (caller.is_root as u64).into()),
(
CallContextField::IsCreate,
(caller.is_create() as u64).into(),
),
(CallContextField::CodeHash, caller.code_hash.to_word()),
(CallContextField::ProgramCounter, geth_step_next.pc.0.into()),
(
CallContextField::StackPointer,
geth_step_next.stack.stack_pointer().0.into(),
),
(CallContextField::GasLeft, caller_gas_left.into()),
(
CallContextField::MemorySize,
self.caller_ctx()?.memory.word_size().into(),
),
(
CallContextField::ReversibleWriteCounter,
self.caller_ctx()?.reversible_write_counter.into(),
),
] {
self.call_context_read(exec_step, caller.call_id, field, value);
}

for (field, value) in [
(CallContextField::LastCalleeId, call.call_id.into()),
(
CallContextField::LastCalleeReturnDataOffset,
last_callee_return_data_offset,
),
(
CallContextField::LastCalleeReturnDataLength,
last_callee_return_data_length,
),
] {
self.call_context_write(exec_step, caller.call_id, field, value);
}

Ok(())
}

/// Push a copy event to the state.
pub fn push_copy(&mut self, copy: CopyEvent) {
self.block.add_copy_event(copy);
Expand Down
Loading