Skip to content

Commit

Permalink
Reviewable.
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 8a81dde commit 6255f00
Show file tree
Hide file tree
Showing 32 changed files with 472 additions and 40 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"]
28 changes: 28 additions & 0 deletions Cargo.lock

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

29 changes: 27 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 @@ -94,7 +93,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"
smt_trie = { path = "smt_trie", version = "0.1.0" }
static_assertions = "1.1.0"
Expand All @@ -110,6 +108,8 @@ u4 = "0.1.0"
uint = "0.9.5"
url = "2.5.2"
winnow = "0.6.13"
evm-disassembler = "0.5.0"
pretty_assertions = "1.4.0"

# zero-bin related dependencies
ops = { path = "zero_bin/ops" }
Expand All @@ -128,4 +128,29 @@ proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
trybuild = "1.0"

zk_evm_proc_macro = { path = "proc_macro" }

[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
7 changes: 5 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 All @@ -46,6 +47,8 @@ serde_json = { workspace = true }
# Local dependencies
mpt_trie = { workspace = true }
zk_evm_proc_macro = { workspace = true }
pretty_assertions = { workspace = true }
alloy.workspace = true

[dev-dependencies]
criterion = { workspace = true }
Expand All @@ -55,7 +58,7 @@ sha2 = { 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 @@ -173,6 +173,7 @@ fn prepare_setup() -> anyhow::Result<GenerationInputs> {
prev_hashes: vec![H256::default(); 256],
cur_hash: H256::default(),
},
jumpdest_table: Default::default(),
})
}

Expand Down
87 changes: 85 additions & 2 deletions evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
use core::cmp::Ordering;
use std::collections::{BTreeSet, HashMap};

use alloy::primitives::Address;
use anyhow::anyhow;
use ethereum_types::{BigEndianHash, U256};
use ethereum_types::{BigEndianHash, H256, U256};
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 +96,81 @@ pub(crate) fn simulate_cpu_and_get_user_jumps<F: Field>(
}
}

// todo: remove. for easy comparison only
pub(crate) fn simulate_cpu_and_get_user_jumps_rpc<F: Field>(
final_label: &str,
state: &GenerationState<F>,
) -> Option<JumpDestTableProcessed> {
match state.jumpdest_table {
Some(_) => None,
None => {
let halt_pc = KERNEL.global_labels[final_label];
let initial_context = state.registers.context;
let mut interpreter =
Interpreter::new_with_state_and_halt_condition(state, halt_pc, initial_context);

log::debug!("Simulating CPU for jumpdest analysis.");

let _ = interpreter.run();

log::trace!("jumpdest table = {:?}", interpreter.jumpdest_table);

//debug_assert_eq!(&interpreter.jumpdest_table, &state.inputs.jumpdest_table);
let jumpdest_table = set_jumpdest_analysis_inputs_rpc(
&state.inputs.jumpdest_table,
&state.inputs.contract_code,
);

Some(jumpdest_table)
}
}
}

/// Computes the 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 @@ -350,6 +431,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 +589,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 @@ -152,6 +152,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 @@ -293,6 +294,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
10 changes: 10 additions & 0 deletions evm_arithmetization/src/cpu/kernel/tests/core/jumpdest_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,13 @@ fn test_verify_non_jumpdest() -> Result<()> {
}
Ok(())
}

#[test]
fn compare_jumpdest_tables() -> Result<()> {
let jumpdest_tbl_sim = todo!();
let jumpdest_tbl_rpc = todo!();

assert_eq!(jumpdest_tbl_sim, jumpdest_tbl_rpc);

Ok(())
}
8 changes: 7 additions & 1 deletion evm_arithmetization/src/generation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::collections::{BTreeSet, 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 @@ -76,6 +78,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 @@ -223,6 +228,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 6255f00

Please sign in to comment.