Skip to content

Commit

Permalink
implement oog constant state (privacy-scaling-explorations#746)
Browse files Browse the repository at this point in the history
* oog constant constraints

* add call failure constraint

* add resotre context in the buss mapping side

* refactor restore context lookup ops

* use reversible_write_counter to catch up with handle_reversion new buss mappings

* add fixed constant gas look up

* fix rebesae issue

* remove debug info

* add opcode lookup

* some fixes

* resolve conflict

Co-authored-by: Zhang Zhuo <[email protected]>
  • Loading branch information
DreamWuGit and lispc authored Nov 1, 2022
1 parent 247bcff commit 6984977
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 64 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,4 +1128,82 @@ impl<'a> CircuitInputStateRef<'a> {
}
Ok(())
}

pub(crate) fn gen_restore_context_ops(
&mut self,
exec_step: &mut ExecStep,
geth_steps: &[GethExecStep],
) -> Result<(), Error> {
let geth_step = &geth_steps[0];
let call = self.call()?.clone();
if !call.is_success {
// add call failure ops for exception cases
self.call_context_read(
exec_step,
call.call_id,
CallContextField::IsSuccess,
0u64.into(),
);
self.call_context_read(
exec_step,
call.call_id,
CallContextField::IsPersistent,
0u64.into(),
);
if call.is_root {
return Ok(());
}
}

let caller = self.caller()?.clone();
self.call_context_read(
exec_step,
call.call_id,
CallContextField::CallerId,
caller.call_id.into(),
);

let geth_step_next = &geth_steps[1];
let caller_ctx = self.caller_ctx()?;
let caller_gas_left = if call.is_success {
geth_step_next.gas.0 - geth_step.gas.0
} else {
geth_step_next.gas.0
};

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,
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, 0.into()),
(CallContextField::LastCalleeReturnDataLength, 0.into()),
] {
self.call_context_write(exec_step, caller.call_id, field, value);
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ pub fn gen_associated_ops(
);

exec_step.error = Some(exec_error);
if exec_step.oog_or_stack_error() {
state.gen_restore_context_ops(&mut exec_step, geth_steps)?;
}
// for `oog_or_stack_error` error message will be returned by geth_step error
// field, when this kind of error happens, no more proceeding
if geth_step.op.is_call_or_create() && !exec_step.oog_or_stack_error() {
Expand Down
48 changes: 3 additions & 45 deletions bus-mapping/src/evm/opcodes/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
operation::CallContextField,
Error,
};
use eth_types::{GethExecStep, ToWord};
use eth_types::GethExecStep;

/// Placeholder structure used to implement [`Opcode`] trait over it
/// corresponding to the [`OpcodeId::STOP`](crate::evm::OpcodeId::STOP)
Expand Down Expand Up @@ -36,50 +36,8 @@ impl Opcode for Stop {
// The following part corresponds to
// Instruction.step_state_transition_to_restored_context
// in python spec, and should be reusable among all expected halting opcodes or
// exceptions. TODO: Refactor it as a helper function.
let caller = state.caller()?.clone();
state.call_context_read(
&mut exec_step,
call.call_id,
CallContextField::CallerId,
caller.call_id.into(),
);

let geth_step_next = &geth_steps[1];
let caller_ctx = state.caller_ctx()?;
let caller_gas_left = geth_step_next.gas.0 - geth_step.gas.0;
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,
caller_ctx.memory.word_size().into(),
),
(
CallContextField::ReversibleWriteCounter,
state.caller_ctx()?.reversible_write_counter.into(),
),
] {
state.call_context_read(&mut exec_step, caller.call_id, field, value);
}

for (field, value) in [
(CallContextField::LastCalleeId, call.call_id.into()),
(CallContextField::LastCalleeReturnDataOffset, 0.into()),
(CallContextField::LastCalleeReturnDataLength, 0.into()),
] {
state.call_context_write(&mut exec_step, caller.call_id, field, value);
}
// exceptions.
state.gen_restore_context_ops(&mut exec_step, geth_steps)?;
}

state.handle_return(geth_step)?;
Expand Down
3 changes: 3 additions & 0 deletions eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ subtle = "2.4"
sha3 = "0.10"
num = "0.4"
num-bigint = { version = "0.4" }
strum_macros = "0.24"
strum = "0.24"


3 changes: 2 additions & 1 deletion eth-types/src/evm_types/opcode_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use regex::Regex;
use serde::{de, Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use strum_macros::EnumIter;

/// Opcode enum. One-to-one corresponding to an `u8` value.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Hash)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Hash, EnumIter)]
pub enum OpcodeId {
/// `STOP`
STOP,
Expand Down
Loading

0 comments on commit 6984977

Please sign in to comment.