Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smt generation #127

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
3da44c8
First rough plan on trait structure
BGluth Apr 5, 2024
d2d8d2a
Now conditionally uses evm types at compile time
BGluth Apr 18, 2024
16ca49f
Removed the default feature of "mpt"
BGluth Apr 18, 2024
ab682a9
Renamed `processed_block_trace.rs` --> `processed_block_trace_mpt.rs`
BGluth Apr 22, 2024
b95fb2b
SQUASH!!
BGluth Apr 24, 2024
9535203
Removed feature gating for MPT/SMT
BGluth Apr 26, 2024
fccb620
A bit more cleanup
BGluth Apr 26, 2024
526c794
Hack to avoid jemalloc failure
BGluth Apr 29, 2024
76d84d7
add testcase to pass
temaniarpit27 Mar 12, 2024
121d1ee
add support for smt leaf
temaniarpit27 Mar 17, 2024
fe9ec7e
algo for parsing witness
temaniarpit27 Mar 25, 2024
3f0663d
add code for k, v for smt nodes
temaniarpit27 Mar 30, 2024
59a1a04
Cleanip, docs, & clippy pass
BGluth Apr 8, 2024
b4c7777
Prep to split mpt & smt trie logic
BGluth Apr 8, 2024
876dc31
More work on isolating smt logic
BGluth Apr 9, 2024
fdf402b
Major refactor for smt & mpt logic
BGluth Apr 10, 2024
07ce168
REMOVE!! Hacked in smt logic
BGluth Apr 10, 2024
141b535
Impled `process_smt_leaf`
BGluth Apr 10, 2024
88c4d22
implement code, hash
temaniarpit27 Apr 10, 2024
df88647
Disabled logic for hash nodes
BGluth Apr 10, 2024
dfc3bf8
Fixed smt tests
BGluth Apr 10, 2024
24b02b4
Added Will's hash node update
BGluth Apr 11, 2024
206d831
Hash node inserts + another fix
BGluth Apr 11, 2024
c170822
Removed an unused param
BGluth Apr 11, 2024
3d9de85
add testcases, fix parsing error for smt
temaniarpit27 Apr 12, 2024
049a7de
add testcases
temaniarpit27 Apr 12, 2024
f03d362
code instruction add
temaniarpit27 Apr 15, 2024
6e7f1b2
add support for code node
temaniarpit27 Apr 15, 2024
65d44d2
process code node
temaniarpit27 Apr 15, 2024
f37a3ef
More small fixes
BGluth Apr 15, 2024
45b7c22
Added a simple test case
BGluth Apr 15, 2024
179f8ac
first case passed
temaniarpit27 Apr 16, 2024
6f43f5f
add code node support
temaniarpit27 Apr 16, 2024
a4d99ce
add testcases
temaniarpit27 Apr 16, 2024
4dace30
Now buffers smt leaf & hash inserts
BGluth Apr 16, 2024
6447ff8
modify testcase 18
temaniarpit27 Apr 16, 2024
18397e5
Renamed compact tests for better readability
BGluth Apr 16, 2024
5505011
7 - 12 testcases passing
temaniarpit27 Apr 17, 2024
05bb290
add testcases
temaniarpit27 Apr 17, 2024
3af6589
fix testcases
temaniarpit27 Apr 17, 2024
cabd1ba
add 10 testcases
temaniarpit27 Apr 19, 2024
a8d8c4b
Swapped out copied smt logic with lib dep
BGluth May 1, 2024
9475ae6
Finished wiring up of SMT decoding logic
BGluth May 1, 2024
ac13882
Pulled in latest changes from `develop`
BGluth May 7, 2024
d3a659d
Merge branch 'develop' into arpit/pos-2512
BGluth May 8, 2024
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
4 changes: 2 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* @muursh @Nashtare
/evm_arithmetization/ @wborgeaud @muursh @Nashtare
* @muursh @Nashtare @cpubot
/evm_arithmetization/ @wborgeaud @muursh @Nashtare @cpubot
4 changes: 2 additions & 2 deletions evm_arithmetization/src/cpu/cpu_stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use starky::stark::Stark;
use super::columns::CpuColumnsView;
use super::halt;
use super::kernel::constants::context_metadata::ContextMetadata;
use super::membus::NUM_GP_CHANNELS;
use super::membus::{NUM_CHANNELS, NUM_GP_CHANNELS};
use crate::all_stark::{EvmStarkFrame, Table};
use crate::cpu::columns::{COL_MAP, NUM_CPU_COLUMNS};
use crate::cpu::{
byte_unpacking, clock, contextops, control_flow, decode, dup_swap, gas, jumps, membus, memio,
modfp254, pc, push0, shift, simple_logic, stack, syscalls_exceptions,
};
use crate::memory::segments::Segment;
use crate::memory::{NUM_CHANNELS, VALUE_LIMBS};
use crate::memory::VALUE_LIMBS;

/// Creates the vector of `Columns` corresponding to the General Purpose
/// channels when calling the Keccak sponge: the CPU reads the output of the
Expand Down
8 changes: 7 additions & 1 deletion evm_arithmetization/src/cpu/kernel/asm/core/call_gas.asm
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ global xfer_cost:
%jump(after_xfer_cost)
xfer_cost_nonzero:
// stack: cost, is_call_or_staticcall, is_call_or_callcode, address, gas, kexit_info, value, retdest
%add_const(@GAS_CALLVALUE)
SWAP5
// stack: kexit_info, is_call_or_staticcall, is_call_or_callcode, address, gas, cost, value, retdest
PUSH @GAS_CALLVALUE
// stack: call_value_gas, kexit_info, is_call_or_staticcall, is_call_or_callcode, address, gas, cost, value, retdest
%charge_gas
// stack: kexit_info, is_call_or_staticcall, is_call_or_callcode, address, gas, cost, value, retdest
SWAP5
// stack: cost, is_call_or_staticcall, is_call_or_callcode, address, gas, kexit_info, value, retdest
%jump(after_xfer_cost)

Expand Down
2 changes: 0 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/core/process_txn.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// After the transaction data has been parsed into a normalized set of fields
// (see NormalizedTxnField), this routine processes the transaction.

// TODO: Save checkpoints in @CTX_METADATA_STATE_TRIE_CHECKPOINT_PTR and @SEGMENT_STORAGE_TRIE_CHECKPOINT_PTRS.

// Pre stack: retdest
// Post stack: success, leftover_gas
global process_normalized_txn:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ insert_touched_addresses_found:

/// Remove the address from the list.
/// Panics if the address is not in the list.
/// TODO: Unused?
global remove_touched_addresses:
// stack: addr, retdest
%mload_global_metadata(@GLOBAL_METADATA_TOUCHED_ADDRESSES_LEN)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Division modulo 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141, the Secp256k1 scalar field order
/// To replace with more efficient method using non-determinism later.

%macro mulmodn_secp_scalar
// stack: x, y
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Division modulo 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f, the Secp256k1 base field order
/// To replace with more efficient method using non-determinism later.

// Returns y * (x^-1) where the inverse is taken modulo N
%macro moddiv_secp_base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// - If there are more than one, update the branch node and return it.
// - If there is exactly one, transform the branch node into an leaf/extension node and return it.
// Assumes that `num_nibbles>0` and that the value of the branch node is zero.
// TODO: May need to revisit these assumptions depending on how the receipt trie is implemented.
global mpt_delete_branch:
// stack: node_type, node_payload_ptr, num_nibbles, key, retdest
POP
Expand Down
15 changes: 14 additions & 1 deletion evm_arithmetization/src/cpu/kernel/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::collections::{BTreeSet, HashMap};

use anyhow::anyhow;
use ethereum_types::{BigEndianHash, U256};
use log::Level;
use mpt_trie::partial_trie::PartialTrie;
use plonky2::field::types::Field;

Expand Down Expand Up @@ -528,7 +529,7 @@ impl<F: Field> State<F> for Interpreter<F> {
if registers.is_kernel {
log_kernel_instruction(self, op);
} else {
log::debug!("User instruction: {:?}", op);
self.log_debug(format!("User instruction: {:?}", op));
}

let generation_state = self.get_mut_generation_state();
Expand All @@ -547,6 +548,18 @@ impl<F: Field> State<F> for Interpreter<F> {

self.perform_state_op(op, row)
}

fn log_debug(&self, msg: String) {
if !self.is_jumpdest_analysis {
log::debug!("{}", msg);
}
}

fn log(&self, level: Level, msg: String) {
if !self.is_jumpdest_analysis {
log::log!(level, "{}", msg);
}
}
}

impl<F: Field> Transition<F> for Interpreter<F> {
Expand Down
20 changes: 11 additions & 9 deletions evm_arithmetization/src/cpu/kernel/tests/ecc/ecrecover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::str::FromStr;

use anyhow::Result;
use anyhow::{anyhow, Result};
use ethereum_types::U256;
use plonky2::field::goldilocks_field::GoldilocksField as F;

Expand Down Expand Up @@ -32,17 +33,18 @@ fn test_invalid_ecrecover(hash: &str, v: &str, r: &str, s: &str) -> Result<()> {
#[test]
fn test_ecrecover_real_block() -> Result<()> {
let f = include_str!("ecrecover_test_data");
let convert_v = |v| match v {
// TODO: do this properly.
"0" => "0x1b",
"1" => "0x1c",
"37" => "0x1b",
"38" => "0x1c",
_ => panic!("Invalid v."),
let convert_v = {
let mut map = HashMap::new();
map.insert("0", "0x1b");
map.insert("1", "0x1c");
map.insert("37", "0x1b");
map.insert("38", "0x1c");

move |v: &str| map.get(v).copied().ok_or_else(|| anyhow!("Invalid v."))
};
for line in f.lines().filter(|s| !s.starts_with("//")) {
let line = line.split_whitespace().collect::<Vec<_>>();
test_valid_ecrecover(line[4], convert_v(line[0]), line[1], line[2], line[3])?;
test_valid_ecrecover(line[4], convert_v(line[0])?, line[1], line[2], line[3])?;
}
Ok(())
}
Expand Down
11 changes: 0 additions & 11 deletions evm_arithmetization/src/generation/prover_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ impl<F: Field> GenerationState<F> {

if self.jumpdest_table.is_none() {
self.generate_jumpdest_table()?;
log::debug!("jdt = {:?}", self.jumpdest_table);
}

let Some(jumpdest_table) = &mut self.jumpdest_table else {
Expand All @@ -282,11 +281,6 @@ impl<F: Field> GenerationState<F> {
if let Some(ctx_jumpdest_table) = jumpdest_table.get_mut(&context)
&& let Some(next_jumpdest_address) = ctx_jumpdest_table.pop()
{
log::debug!(
"jumpdest_table_len = {:?}, ctx_jumpdest_table.len = {:?}",
jd_len,
ctx_jumpdest_table.len()
);
Ok((next_jumpdest_address + 1).into())
} else {
jumpdest_table.remove(&context);
Expand All @@ -308,11 +302,6 @@ impl<F: Field> GenerationState<F> {
if let Some(ctx_jumpdest_table) = jumpdest_table.get_mut(&context)
&& let Some(next_jumpdest_proof) = ctx_jumpdest_table.pop()
{
log::debug!(
"jumpdest_table_len = {:?}, ctx_jumpdest_table.len = {:?}",
jd_len,
ctx_jumpdest_table.len()
);
Ok(next_jumpdest_proof.into())
} else {
Err(ProgramError::ProverInputError(
Expand Down
23 changes: 21 additions & 2 deletions evm_arithmetization/src/generation/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::{anyhow, bail};
use ethereum_types::{Address, BigEndianHash, H160, H256, U256};
use itertools::Itertools;
use keccak_hash::keccak;
use log::Level;
use plonky2::field::types::Field;

use super::mpt::{load_all_mpts, TrieRootPtrs};
Expand Down Expand Up @@ -162,7 +163,7 @@ pub(crate) trait State<F: Field> {
}
} else {
#[cfg(not(test))]
log::info!("CPU halted after {} cycles", self.get_clock());
self.log_info(format!("CPU halted after {} cycles", self.get_clock()));
return Ok(());
}
}
Expand Down Expand Up @@ -254,6 +255,24 @@ pub(crate) trait State<F: Field> {
let opcode = read_code_memory(generation_state, &mut row);
(row, opcode)
}

/// Logs `msg` in `debug` mode.
#[inline]
fn log_debug(&self, msg: String) {
log::debug!("{}", msg);
}

/// Logs `msg` in `info` mode.
#[inline]
fn log_info(&self, msg: String) {
log::info!("{}", msg);
}

/// Logs `msg` at `level`.
#[inline]
fn log(&self, level: Level, msg: String) {
log::log!(level, "{}", msg);
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -490,7 +509,7 @@ impl<F: Field> State<F> for GenerationState<F> {
if registers.is_kernel {
log_kernel_instruction(self, op);
} else {
log::debug!("User instruction: {:?}", op);
self.log_debug(format!("User instruction: {:?}", op));
}
fill_op_flag(op, &mut row);

Expand Down
3 changes: 0 additions & 3 deletions evm_arithmetization/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ pub mod columns;
pub mod memory_stark;
pub mod segments;

// TODO: Move to CPU module, now that channels have been removed from the memory
// table.
pub(crate) const NUM_CHANNELS: usize = crate::cpu::membus::NUM_CHANNELS;
/// The number of limbs holding the value at a memory address.
/// Eight limbs of 32 bits can hold a `U256`.
pub(crate) const VALUE_LIMBS: usize = 8;
20 changes: 13 additions & 7 deletions evm_arithmetization/src/witness/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ pub(crate) fn generate_keccak_general<F: Field, T: Transition<F>>(
val.low_u32() as u8
})
.collect_vec();
log::debug!("Hashing {:?}", input);

let hash = keccak(&input);
push_no_write(generation_state, hash.into_uint());

state.log_debug(format!("Hashing {:?}", input));

keccak_sponge_log(state, base_address, input);

state.push_memory(log_in1);
Expand Down Expand Up @@ -702,7 +703,10 @@ pub(crate) fn generate_syscall<F: Field, T: Transition<F>>(

push_with_write(state, &mut row, syscall_info)?;

log::debug!("Syscall to {}", KERNEL.offset_name(new_program_counter));
state.log_debug(format!(
"Syscall to {}",
KERNEL.offset_name(new_program_counter)
));
byte_packing_log(state, base_address, bytes);

state.push_arithmetic(range_check_op);
Expand Down Expand Up @@ -748,11 +752,10 @@ pub(crate) fn generate_exit_kernel<F: Field, T: Transition<F>>(
generation_state.registers.program_counter = program_counter;
generation_state.registers.is_kernel = is_kernel_mode;
generation_state.registers.gas_used = gas_used_val;
log::debug!(
state.log_debug(format!(
"Exiting to {}, is_kernel={}",
program_counter,
is_kernel_mode
);
program_counter, is_kernel_mode
));

state.push_cpu(row);

Expand Down Expand Up @@ -971,7 +974,10 @@ pub(crate) fn generate_exception<F: Field, T: Transition<F>>(
push_with_write(generation_state, &mut row, exc_info)?;
byte_packing_log(state, base_address, bytes);

log::debug!("Exception to {}", KERNEL.offset_name(new_program_counter));
state.log_debug(format!(
"Exception to {}",
KERNEL.offset_name(new_program_counter)
));
state.push_arithmetic(range_check_op);
state.push_cpu(row);

Expand Down
16 changes: 9 additions & 7 deletions evm_arithmetization/src/witness/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,16 @@ pub(crate) fn log_kernel_instruction<F: Field, S: State<F>>(state: &mut S, op: O
} else {
log::Level::Trace
};
log::log!(
state.log(
level,
"Cycle {}, ctx={}, pc={}, instruction={:?}, stack={:?}",
state.get_clock(),
state.get_context(),
KERNEL.offset_name(pc),
op,
state.get_generation_state().stack(),
format!(
"Cycle {}, ctx={}, pc={}, instruction={:?}, stack={:?}",
state.get_clock(),
state.get_context(),
KERNEL.offset_name(pc),
op,
state.get_generation_state().stack(),
),
);

assert!(pc < KERNEL.code.len(), "Kernel PC is out of range: {}", pc);
Expand Down
13 changes: 7 additions & 6 deletions evm_arithmetization/tests/self_balance_gas_cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,12 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
code_hash,
// Storage map: { 1 => 5 }
storage_root: HashedPartialTrie::from(Node::Leaf {
// TODO: Could do keccak(pad32(1))
nibbles: Nibbles::from_str(
"0xb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6",
)
.unwrap(),
nibbles: Nibbles::from_h256_be(keccak(pad32(1))),
value: vec![5],
})
.hash(),
..AccountRlp::default()
};

let mut expected_state_trie_after = HashedPartialTrie::from(Node::Empty);
expected_state_trie_after.insert(
beneficiary_nibbles,
Expand Down Expand Up @@ -191,6 +186,12 @@ fn self_balance_gas_cost() -> anyhow::Result<()> {
verify_proof(&all_stark, proof, &config)
}

fn pad32(byte: u8) -> Vec<u8> {
let mut data = vec![0; 31];
data.push(byte);
data
}

fn init_logger() {
let _ = try_init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
}
23 changes: 13 additions & 10 deletions mpt_trie/src/debug_tools/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ impl DiffPoint {
}
}

// TODO: Redo display method so this is more readable...
impl Display for DiffPoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Point Diff {{depth: {}, ", self.depth)?;
write!(f, "Path: ({}), ", self.path)?;
write!(f, "Key: {:x} ", self.key)?;
write!(f, "A info: {} ", self.a_info)?;
write!(f, "B info: {}}}", self.b_info)
writeln!(f, "Point Diff {{")?;
writeln!(f, " Depth: {},", self.depth)?;
writeln!(f, " Path: ({}),", self.path)?;
writeln!(f, " Key: {:x},", self.key)?;
writeln!(f, " A info: {},", self.a_info)?;
writeln!(f, " B info: {}", self.b_info)?;
write!(f, "}}")
}
}

Expand All @@ -136,18 +137,20 @@ pub struct NodeInfo {
hash: H256,
}

// TODO: Redo display method so this is more readable...
impl Display for NodeInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(key: {:x} ", self.key)?;
write!(f, "NodeInfo {{ Key: 0x{:x}, ", self.key)?;

match &self.value {
Some(v) => write!(f, "Value: 0x{}, ", hex::encode(v))?,
None => write!(f, "Value: N/A, ")?,
}

write!(f, "Node type: {} ", self.node_type)?;
write!(f, "Trie hash: {:x})", self.hash)
write!(
f,
"Node type: {}, Trie hash: 0x{:x} }}",
self.node_type, self.hash
)
}
}

Expand Down
Loading