Skip to content

Commit

Permalink
Implement JumpDest fetching from RPC.
Browse files Browse the repository at this point in the history
Automated testing missing.
  • Loading branch information
einar-polygon committed Aug 7, 2024
1 parent 52ab1aa commit 6613802
Show file tree
Hide file tree
Showing 30 changed files with 407 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[build]
# https://github.com/rust-lang/rust/pull/124129
# https://github.com/dtolnay/linkme/pull/88
rustflags = ["-Z", "linker-features=-lld"]
rustflags = ["-C", "target-cpu=native", "-Z", "linker-features=-lld"]
27 changes: 27 additions & 0 deletions Cargo.lock

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

26 changes: 24 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ criterion = "0.5.1"
dotenvy = "0.15.7"
either = "1.12.0"
enum-as-inner = "0.6.0"
enumn = "0.1.13"
env_logger = "0.11.3"
eth_trie = "0.4.0"
ethereum-types = "0.14.1"
Expand Down Expand Up @@ -92,7 +91,6 @@ ruint = "1.12.3"
serde = "1.0.203"
serde_json = "1.0.118"
serde_path_to_error = "0.1.16"
serde_with = "3.8.1"
sha2 = "0.10.8"
static_assertions = "1.1.0"
thiserror = "1.0.61"
Expand Down Expand Up @@ -133,3 +131,27 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
trybuild = "1.0"

[profile.release]
opt-level=3
debug=true
incremental=true
debug-assertions=true
lto=false
overflow-checks=false

[profile.test]
opt-level=3
debug=true
incremental=true
debug-assertions=true
lto=false
overflow-checks=false

[profile.dev]
opt-level=3
debug=true
incremental=true
debug-assertions=true
lto=false
overflow-checks=false
6 changes: 4 additions & 2 deletions evm_arithmetization/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ homepage.workspace = true
keywords.workspace = true

[dependencies]
__compat_primitive_types = { workspace = true }
anyhow = { workspace = true }
bytes = { workspace = true }
env_logger = { workspace = true }
ethereum-types = { workspace = true }
hex = { workspace = true, optional = true }
hex = { workspace = true }
hex-literal = { workspace = true }
itertools = { workspace = true }
keccak-hash = { workspace = true }
Expand Down Expand Up @@ -47,6 +48,7 @@ serde_json = { workspace = true }
# Local dependencies
mpt_trie = { workspace = true }
zk_evm_proc_macro = { workspace = true }
alloy.workspace = true

[dev-dependencies]
criterion = { workspace = true }
Expand All @@ -55,7 +57,7 @@ ripemd = { workspace = true }

[features]
default = ["parallel"]
asmtools = ["hex"]
#asmtools = ["hex"]
parallel = [
"plonky2/parallel",
"plonky2_maybe_rayon/parallel",
Expand Down
1 change: 1 addition & 0 deletions evm_arithmetization/benches/fibonacci_25m_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs> {
prev_hashes: vec![H256::default(); 256],
cur_hash: H256::default(),
},
jumpdest_table: Default::default(),
})
}

Expand Down
54 changes: 53 additions & 1 deletion evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ use log::Level;
use mpt_trie::partial_trie::PartialTrie;
use plonky2::field::types::Field;

//use alloy::primitives::Address;
use crate::byte_packing::byte_packing_stark::BytePackingOp;
use crate::cpu::columns::CpuColumnsView;
use crate::cpu::kernel::aggregator::KERNEL;
use crate::cpu::kernel::constants::global_metadata::GlobalMetadata;
use crate::generation::debug_inputs;
use crate::generation::mpt::load_all_mpts;
use crate::generation::prover_input::{
get_proofs_and_jumpdests, CodeDb, ContextJumpDests, JumpDestTableProcessed,
JumpDestTableWitness,
};
use crate::generation::rlp::all_rlp_prover_inputs_reversed;
use crate::generation::state::{
all_withdrawals_prover_inputs_reversed, GenerationState, GenerationStateCheckpoint,
Expand Down Expand Up @@ -90,6 +95,51 @@ pub(crate) fn simulate_cpu_and_get_user_jumps<F: Field>(
}
}

/// Computes the JUMPDEST proofs for each context.
///
/// # Arguments
///
/// - `jumpdest_table_rpc`: The raw table received from RPC.
/// - `code_db`: The corresponding database of contract code used in the trace.
pub(crate) fn set_jumpdest_analysis_inputs_rpc(
jumpdest_table_rpc: &JumpDestTableWitness,
code_db: &CodeDb,
) -> JumpDestTableProcessed {
let ctx_proofs = jumpdest_table_rpc
.0
.iter()
.flat_map(|(code_addr, ctx_jumpdests)| {
prove_context_jumpdests(&code_db[code_addr], ctx_jumpdests)
})
.collect();
JumpDestTableProcessed(ctx_proofs)
}

/// Orchestrates the proving of all contexts in a specific bytecode.
///
/// # Arguments
///
/// - `ctx_jumpdests`: Map from `ctx` to its list of offsets to reached
/// `JUMPDEST`s.
/// - `code`: The bytecode for the contexts. This is the same for all contexts.
fn prove_context_jumpdests(
code: &[u8],
ctx_jumpdests: &ContextJumpDests,
) -> HashMap<usize, Vec<usize>> {
ctx_jumpdests
.0
.iter()
.map(|(&ctx, jumpdests)| {
let proofs = jumpdests
.last()
.map_or(Vec::default(), |&largest_address| {
get_proofs_and_jumpdests(code, largest_address, jumpdests.clone())
});
(ctx, proofs)
})
.collect()
}

impl<F: Field> Interpreter<F> {
/// Returns an instance of `Interpreter` given `GenerationInputs`, and
/// assuming we are initializing with the `KERNEL` code.
Expand Down Expand Up @@ -362,6 +412,8 @@ impl<F: Field> Interpreter<F> {
}

pub(crate) fn add_jumpdest_offset(&mut self, offset: usize) {
// println!("SIM: ({:?}, {:?})", &self.generation_state.registers.context,
// offset);
if let Some(jumpdest_table) = self
.jumpdest_table
.get_mut(&self.generation_state.registers.context)
Expand Down Expand Up @@ -506,7 +558,7 @@ impl<F: Field> State<F> for Interpreter<F> {
let registers = self.generation_state.registers;
let (mut row, opcode) = self.base_row();

let op = decode(registers, opcode)?;
let op: Operation = decode(registers, opcode)?;

fill_op_flag(op, &mut row);

Expand Down
2 changes: 2 additions & 0 deletions evm_arithmetization/src/cpu/kernel/tests/add11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn test_add11_yml() {
prev_hashes: vec![H256::default(); 256],
cur_hash: H256::default(),
},
jumpdest_table: Default::default(),
};

let initial_stack = vec![];
Expand Down Expand Up @@ -376,6 +377,7 @@ fn test_add11_yml_with_exception() {
prev_hashes: vec![H256::default(); 256],
cur_hash: H256::default(),
},
jumpdest_table: Default::default(),
};

let initial_stack = vec![];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,4 @@ fn test_verify_non_jumpdest() -> Result<()> {
}
}
Ok(())
}
}
6 changes: 6 additions & 0 deletions evm_arithmetization/src/generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use anyhow::anyhow;
use ethereum_types::{Address, BigEndianHash, H256, U256};
// use hex::ToHex;
use log::log_enabled;
use mpt_trie::partial_trie::{HashedPartialTrie, PartialTrie};
use plonky2::field::extension::Extendable;
Expand All @@ -10,6 +11,7 @@ use plonky2::field::types::Field;
use plonky2::hash::hash_types::RichField;
use plonky2::timed;
use plonky2::util::timing::TimingTree;
use prover_input::JumpDestTableWitness;
use serde::{Deserialize, Serialize};
use starky::config::StarkConfig;
use GlobalMetadata::{
Expand Down Expand Up @@ -78,6 +80,9 @@ pub struct GenerationInputs {
/// The hash of the current block, and a list of the 256 previous block
/// hashes.
pub block_hashes: BlockHashes,

/// A jumptable describing each JUMPDEST reached(jumped to?).
pub jumpdest_table: JumpDestTableWitness,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -237,6 +242,7 @@ pub fn generate_traces<F: RichField + Extendable<D>, const D: usize>(
timing: &mut TimingTree,
) -> anyhow::Result<([Vec<PolynomialValues<F>>; NUM_TABLES], PublicValues)> {
debug_inputs(&inputs);
// TODO
let mut state = GenerationState::<F>::new(inputs.clone(), &KERNEL.code)
.map_err(|err| anyhow!("Failed to parse all the initial prover inputs: {:?}", err))?;

Expand Down
Loading

0 comments on commit 6613802

Please sign in to comment.