From a767a69eeafe8013d73f988e9985295e49322338 Mon Sep 17 00:00:00 2001 From: Pierre Hong Date: Mon, 11 Dec 2023 17:15:47 +0800 Subject: [PATCH 1/3] DEL: tx_idx --- core/src/storage/db.rs | 31 +++++++++++++++++++++++++++++++ core/src/trace/trace.rs | 14 -------------- core/src/vm/memory.rs | 5 ----- executor/src/lib.rs | 24 ------------------------ executor/src/load_tx.rs | 2 -- executor/src/storage.rs | 15 ++------------- executor/src/tape.rs | 5 ----- executor/src/trace.rs | 5 ----- zk-vm/src/lib.rs | 3 --- zk-vm/src/test.rs | 7 +------ 10 files changed, 34 insertions(+), 77 deletions(-) diff --git a/core/src/storage/db.rs b/core/src/storage/db.rs index 36e81caf..0a83bae0 100644 --- a/core/src/storage/db.rs +++ b/core/src/storage/db.rs @@ -25,6 +25,7 @@ pub struct RocksDB { pub enum Database { MerkleTree, StateKeeper, + Sequencer, } #[derive(Debug)] @@ -43,6 +44,13 @@ pub enum StateKeeperColumnFamily { FactoryDeps, } +#[derive(Debug, Clone, Copy)] +enum SequencerColumnFamily { + State, + Contracts, + FactoryDeps, +} + impl MerkleTreeColumnFamily { fn all() -> &'static [Self] { &[Self::Tree, Self::LeafIndices] @@ -62,6 +70,12 @@ impl StateKeeperColumnFamily { } } +impl SequencerColumnFamily { + fn all() -> &'static [Self] { + &[Self::State, Self::Contracts, Self::FactoryDeps] + } +} + impl std::fmt::Display for MerkleTreeColumnFamily { fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { let value = match self { @@ -86,6 +100,17 @@ impl std::fmt::Display for StateKeeperColumnFamily { } } +impl std::fmt::Display for SequencerColumnFamily { + fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + let value = match self { + Self::State => "state", + Self::Contracts => "contracts", + Self::FactoryDeps => "factory_deps", + }; + write!(formatter, "{}", value) + } +} + impl RocksDB { pub fn new>(database: Database, path: P, tune_options: bool) -> Self { let options = Self::rocksdb_options(tune_options); @@ -102,6 +127,12 @@ impl RocksDB { }); DB::open_cf_descriptors(&options, path, cfs).expect("failed to init rocksdb") } + Database::Sequencer => { + let cfs = SequencerColumnFamily::all().iter().map(|cf| { + ColumnFamilyDescriptor::new(cf.to_string(), Self::rocksdb_options(tune_options)) + }); + DB::open_cf_descriptors(&options, path, cfs).expect("failed to init rocksdb") + } }; Self { diff --git a/core/src/trace/trace.rs b/core/src/trace/trace.rs index 1d4449fb..b209fd82 100644 --- a/core/src/trace/trace.rs +++ b/core/src/trace/trace.rs @@ -65,7 +65,6 @@ pub enum ComparisonOperation { #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct MemoryTraceCell { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub addr: GoldilocksField, pub clk: GoldilocksField, @@ -108,7 +107,6 @@ pub struct BuiltinSelector { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Step { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub call_sc_cnt: GoldilocksField, pub clk: u32, @@ -180,7 +178,6 @@ pub struct CmpRow { #[derive(Debug, Copy, Clone, Default, Serialize, Deserialize)] pub struct PoseidonChunkRow { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub clk: u32, pub opcode: GoldilocksField, @@ -270,7 +267,6 @@ pub struct HashTrace( #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct StorageRow { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub clk: u32, pub diff_clk: u32, @@ -300,7 +296,6 @@ pub struct StorageHashRow { #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct TapeRow { - pub tx_idx: GoldilocksField, pub is_init: bool, pub opcode: GoldilocksField, pub addr: GoldilocksField, @@ -310,7 +305,6 @@ pub struct TapeRow { #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct SCCallRow { - pub tx_idx: GoldilocksField, pub caller_env_idx: GoldilocksField, pub addr_storage: Address, pub addr_code: Address, @@ -446,7 +440,6 @@ impl Trace { ext_cnt: GoldilocksField, filter_tape_looking: GoldilocksField, addr_code: Address, - tx_idx: GoldilocksField, env_idx: GoldilocksField, call_sc_cnt: GoldilocksField, storage_access_idx: GoldilocksField, @@ -466,7 +459,6 @@ impl Trace { ext_cnt, filter_tape_looking, addr_code, - tx_idx, env_idx, call_sc_cnt, storage_access_idx, @@ -482,7 +474,6 @@ impl Trace { root: [GoldilocksField; 4], addr: [GoldilocksField; 4], value: [GoldilocksField; 4], - tx_idx: GoldilocksField, env_idx: GoldilocksField, ) { self.builtin_storage.push(StorageRow { @@ -492,14 +483,12 @@ impl Trace { root, addr, value, - tx_idx, env_idx, }); } pub fn insert_sccall( &mut self, - tx_idx: GoldilocksField, caller_env_idx: GoldilocksField, addr_storage: Address, addr_code: Address, @@ -511,7 +500,6 @@ impl Trace { clk_callee_end: GoldilocksField, ) { self.sc_call.push(SCCallRow { - tx_idx, caller_env_idx, addr_storage, addr_code, @@ -526,7 +514,6 @@ impl Trace { pub fn insert_poseidon_chunk( &mut self, - tx_idx: GoldilocksField, env_idx: GoldilocksField, clk: u32, opcode: GoldilocksField, @@ -540,7 +527,6 @@ impl Trace { is_ext_line: GoldilocksField, ) { self.builtin_poseidon_chunk.push(PoseidonChunkRow { - tx_idx, env_idx, clk, opcode, diff --git a/core/src/vm/memory.rs b/core/src/vm/memory.rs index 4994ff1d..c8938251 100644 --- a/core/src/vm/memory.rs +++ b/core/src/vm/memory.rs @@ -11,7 +11,6 @@ pub const HP_START_ADDR: u64 = GoldilocksField::ORDER - 2 * MEM_SPAN_SIZE; #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct MemoryCell { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub clk: u32, pub is_rw: GoldilocksField, @@ -41,7 +40,6 @@ impl MemoryTree { filter_looked_for_main: GoldilocksField, region_prophet: GoldilocksField, region_heap: GoldilocksField, - tx_idx: GoldilocksField, env_idx: GoldilocksField, ) -> Result { // look up the previous value in the appropriate address trace and add (clk, @@ -52,7 +50,6 @@ impl MemoryTree { if let Some(mem_data) = read_mem_res { let last_value = mem_data.last().expect("empty address trace").value; let new_value = MemoryCell { - tx_idx, env_idx, is_rw, clk, @@ -81,7 +78,6 @@ impl MemoryTree { region_prophet: GoldilocksField, region_heap: GoldilocksField, value: GoldilocksField, - tx_idx: GoldilocksField, env_idx: GoldilocksField, ) { // add a memory access to the appropriate address trace; if this is the first @@ -95,7 +91,6 @@ impl MemoryTree { region_prophet, region_heap, value, - tx_idx, env_idx, }; self.trace diff --git a/executor/src/lib.rs b/executor/src/lib.rs index 474e8f00..4f4e4f73 100644 --- a/executor/src/lib.rs +++ b/executor/src/lib.rs @@ -99,7 +99,6 @@ macro_rules! memory_op { GoldilocksField::from_canonical_u64(FilterLockForMain::True as u64), region_prophet, region_heap, - $v.tx_idx, $v.env_idx, )?; }; @@ -118,7 +117,6 @@ macro_rules! memory_op { region_prophet, region_heap, $value, - $v.tx_idx, $v.env_idx, ); }; @@ -142,7 +140,6 @@ macro_rules! aux_insert { ext_cnt: $ext_cnt, filter_tape_looking: $filter_tape_looking, addr_code: $ctx_code_regs_status, - tx_idx: $v.tx_idx, env_idx: $v.env_idx, call_sc_cnt: $v.call_sc_cnt, storage_access_idx: $v.storage_access_idx, @@ -193,7 +190,6 @@ enum MemRangeType { } #[derive(Debug)] pub struct Process { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub call_sc_cnt: GoldilocksField, pub clk: u32, @@ -221,7 +217,6 @@ pub struct Process { impl Process { pub fn new() -> Self { Self { - tx_idx: Default::default(), env_idx: Default::default(), call_sc_cnt: Default::default(), clk: 0, @@ -311,7 +306,6 @@ impl Process { GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64), GoldilocksField::ZERO, GoldilocksField::ZERO, - self.tx_idx, self.env_idx, )? .to_canonical_u64(); @@ -329,7 +323,6 @@ impl Process { GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64), GoldilocksField::ZERO, GoldilocksField::ZERO, - self.tx_idx, self.env_idx, )? .to_canonical_u64(); @@ -389,7 +382,6 @@ impl Process { GoldilocksField::from_canonical_u64(1_u64), GoldilocksField::from_canonical_u64(0_u64), GoldilocksField(value.get_number() as u64), - self.tx_idx, self.env_idx, ); self.psp += GoldilocksField::ONE; @@ -1104,7 +1096,6 @@ impl Process { GoldilocksField::ZERO, GoldilocksField::ZERO, ctx_code_regs_status.clone(), - self.tx_idx, self.env_idx, self.call_sc_cnt, self.storage_access_idx, @@ -1115,7 +1106,6 @@ impl Process { self.register_selector.aux1 = GoldilocksField::from_canonical_u64(self.clk as u64); let register_selector = self.register_selector.clone(); end_step = Some(Step { - tx_idx: self.tx_idx, env_idx: GoldilocksField::default(), call_sc_cnt: self.call_sc_cnt, tp: self.tp, @@ -1194,7 +1184,6 @@ impl Process { tree_key, store_value, tree_key_default(), - self.tx_idx, self.env_idx, ); self.storage_access_idx += GoldilocksField::ONE; @@ -1326,7 +1315,6 @@ impl Process { tree_key, tree_key_default(), read_value, - self.tx_idx, self.env_idx, ); @@ -1419,7 +1407,6 @@ impl Process { let mut hash_input_value = [GoldilocksField::ZERO; POSEIDON_INPUT_VALUE_LEN]; if !program.pre_exe_flag { program.trace.insert_poseidon_chunk( - self.tx_idx, self.env_idx, self.clk, self.opcode, @@ -1453,7 +1440,6 @@ impl Process { hash_input_value.clone_from_slice(&input[0..POSEIDON_INPUT_VALUE_LEN]); hash_cap.clone_from_slice(&hash_pre[POSEIDON_INPUT_VALUE_LEN..]); program.trace.insert_poseidon_chunk( - self.tx_idx, self.env_idx, self.clk, self.opcode, @@ -1495,7 +1481,6 @@ impl Process { hash_input_value.clone_from_slice(&input[0..POSEIDON_INPUT_VALUE_LEN]); hash_cap.clone_from_slice(&hash_pre[POSEIDON_INPUT_VALUE_LEN..]); program.trace.insert_poseidon_chunk( - self.tx_idx, self.env_idx, self.clk, self.opcode, @@ -1587,7 +1572,6 @@ impl Process { let registers_status = registers_status.clone(); tape_copy!(self, let value = self.tape.read( - self.tx_idx, tape_addr, self.clk, GoldilocksField::from_canonical_u64(1 << Opcode::TLOAD as u64), @@ -1603,7 +1587,6 @@ impl Process { region_prophet, region_heap, value, - self.tx_idx, self.env_idx ), ctx_regs_status, ctx_code_regs_status, registers_status, zone_length, mem_base_addr, tape_base_addr, aux_steps, mem_addr, tape_addr, is_rw, region_prophet, region_heap, value); @@ -1663,11 +1646,9 @@ impl Process { GoldilocksField::from_canonical_u64(FilterLockForMain::True as u64), region_prophet, region_heap, - self.tx_idx, self.env_idx )?, self.tape.write( - self.tx_idx, tape_addr, self.clk, GoldilocksField::from_canonical_u64(1 << Opcode::TSTORE as u64), @@ -1743,7 +1724,6 @@ impl Process { if !program.pre_exe_flag { program.trace.insert_sccall( - self.tx_idx, self.env_idx, self.addr_storage, self.addr_code, @@ -1770,7 +1750,6 @@ impl Process { GoldilocksField::ZERO, GoldilocksField::ZERO, ctx_code_regs_status.clone(), - self.tx_idx, self.env_idx, self.call_sc_cnt, self.storage_access_idx, @@ -1796,7 +1775,6 @@ impl Process { GoldilocksField::ONE, GoldilocksField::ZERO, self.addr_code, - self.tx_idx, self.env_idx, self.call_sc_cnt, self.storage_access_idx, @@ -1876,7 +1854,6 @@ impl Process { GoldilocksField::from_canonical_u64(0_u64), GoldilocksField::from_canonical_u64(1_u64), GoldilocksField(HP_START_ADDR + 1), - self.tx_idx, self.env_idx, ); @@ -2028,7 +2005,6 @@ impl Process { GoldilocksField::ZERO, GoldilocksField::ZERO, ctx_code_regs_status, - self.tx_idx, self.env_idx, self.call_sc_cnt, storage_acc_id_status, diff --git a/executor/src/load_tx.rs b/executor/src/load_tx.rs index 27025ea5..46595efc 100644 --- a/executor/src/load_tx.rs +++ b/executor/src/load_tx.rs @@ -10,7 +10,6 @@ use serde_derive::{Deserialize, Serialize}; pub fn load_tx_calldata(process: &mut Process, calldate: &Vec) { for data in calldate { process.tape.write( - process.tx_idx, process.tp.to_canonical_u64(), 0, GoldilocksField::from_canonical_u64(0), @@ -52,7 +51,6 @@ macro_rules! load_ctx_to_tape { .read_u64::() .expect("failed to deserialize value"); process.tape.write( - process.tx_idx, tp + addr as u64, 0, GoldilocksField::from_canonical_u64(0), diff --git a/executor/src/storage.rs b/executor/src/storage.rs index d049d5ae..106e907e 100644 --- a/executor/src/storage.rs +++ b/executor/src/storage.rs @@ -10,7 +10,6 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)] pub struct StorageCell { - pub tx_idx: GoldilocksField, pub env_idx: GoldilocksField, pub clk: u32, pub op: GoldilocksField, @@ -21,16 +20,11 @@ pub struct StorageCell { impl Ord for StorageCell { fn cmp(&self, other: &Self) -> Ordering { - let mut order = self.tx_idx.0.cmp(&other.tx_idx.0); + let mut order = self.env_idx.0.cmp(&other.env_idx.0); if order.is_ne() { return order; - } else { - order = self.env_idx.0.cmp(&other.env_idx.0); - if order.is_ne() { - return order; - } - order = self.clk.cmp(&other.clk); } + order = self.clk.cmp(&other.clk); return order; } @@ -95,7 +89,6 @@ impl StorageTree { addr: TreeKey, root: ZkHash, value: TreeValue, - tx_idx: GoldilocksField, env_idx: GoldilocksField, ) -> TreeValue { // look up the previous value in the appropriate address trace and add (clk, @@ -112,7 +105,6 @@ impl StorageTree { addr, root, value: last_value, - tx_idx, env_idx, }; addr_trace.push(new_value); @@ -124,7 +116,6 @@ impl StorageTree { addr, root, value, - tx_idx, env_idx, }; vec![new_value] @@ -141,7 +132,6 @@ impl StorageTree { addr: TreeKey, value: TreeValue, root: ZkHash, - tx_idx: GoldilocksField, env_idx: GoldilocksField, ) { // add a memory access to the appropriate address trace; if this is the first @@ -152,7 +142,6 @@ impl StorageTree { addr, value, root, - tx_idx, env_idx, }; self.trace diff --git a/executor/src/tape.rs b/executor/src/tape.rs index 372402a6..859ff31d 100644 --- a/executor/src/tape.rs +++ b/executor/src/tape.rs @@ -6,7 +6,6 @@ use std::collections::BTreeMap; #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub struct TapeCell { - pub tx_idx: GoldilocksField, pub clk: u32, pub is_init: GoldilocksField, pub op: GoldilocksField, @@ -24,7 +23,6 @@ pub struct TapeTree { impl TapeTree { pub fn read( &mut self, - tx_idx: GoldilocksField, addr: u64, clk: u32, op: GoldilocksField, @@ -38,7 +36,6 @@ impl TapeTree { if let Some(tape_data) = read_res { let last_value = tape_data.last().expect("empty address trace").value; let new_value = TapeCell { - tx_idx, clk, op, is_init: tape_data.last().expect("empty address trace").is_init, @@ -68,7 +65,6 @@ impl TapeTree { pub fn write( &mut self, - tx_idx: GoldilocksField, addr: u64, clk: u32, op: GoldilocksField, @@ -79,7 +75,6 @@ impl TapeTree { // add a memory access to the appropriate address trace; if this is the first // time we access this address, initialize address trace. let new_cell = TapeCell { - tx_idx, clk, op, is_init, diff --git a/executor/src/trace.rs b/executor/src/trace.rs index 9d441dad..359d683b 100644 --- a/executor/src/trace.rs +++ b/executor/src/trace.rs @@ -62,7 +62,6 @@ pub fn gen_memory_table( if first_row_flag { let rc_value = GoldilocksField::ZERO; let trace_cell = MemoryTraceCell { - tx_idx: cell.tx_idx, env_idx: cell.env_idx, addr: GoldilocksField::from_canonical_u64(canonical_addr), clk: GoldilocksField::from_canonical_u64(cell.clk as u64), @@ -115,7 +114,6 @@ pub fn gen_memory_table( } diff_clk = GoldilocksField::ZERO; let trace_cell = MemoryTraceCell { - tx_idx: cell.tx_idx, env_idx: cell.env_idx, addr: GoldilocksField::from_canonical_u64(canonical_addr), clk: GoldilocksField::from_canonical_u64(cell.clk as u64), @@ -156,7 +154,6 @@ pub fn gen_memory_table( } let trace_cell = MemoryTraceCell { - tx_idx: cell.tx_idx, env_idx: cell.env_idx, addr: GoldilocksField::from_canonical_u64(canonical_addr), clk: GoldilocksField::from_canonical_u64(cell.clk as u64), @@ -377,7 +374,6 @@ pub fn gen_storage_table( root, item.1.addr, item.1.value, - item.1.tx_idx, item.1.env_idx, ); @@ -399,7 +395,6 @@ pub fn gen_tape_table(process: &mut Process, program: &mut Program) -> Result<() for (addr, cells) in process.tape.trace.iter() { for tape_row in cells { program.trace.tape.push(TapeRow { - tx_idx: tape_row.tx_idx, is_init: tape_row.is_init.is_one(), opcode: tape_row.op, addr: GoldilocksField::from_canonical_u64(*addr), diff --git a/zk-vm/src/lib.rs b/zk-vm/src/lib.rs index e3d961f3..6ac84664 100644 --- a/zk-vm/src/lib.rs +++ b/zk-vm/src/lib.rs @@ -219,7 +219,6 @@ impl OlaVM { pub fn execute_tx( &mut self, - tx_idx: GoldilocksField, caller_addr: TreeValue, code_exe_addr: TreeValue, calldata: Vec, @@ -227,7 +226,6 @@ impl OlaVM { let mut env_idx = 0; let mut sc_cnt = 0; let mut process = Arc::new(Mutex::new(Process::new())); - mutex_data!(process).tx_idx = tx_idx; mutex_data!(process).env_idx = GoldilocksField::from_canonical_u64(env_idx); mutex_data!(process).call_sc_cnt = GoldilocksField::from_canonical_u64(sc_cnt); mutex_data!(process).addr_storage = caller_addr; @@ -275,7 +273,6 @@ impl OlaVM { process = Arc::new(Mutex::new(Process::new())); mutex_data!(process).tape = tape_tree; mutex_data!(process).tp = tp.clone(); - mutex_data!(process).tx_idx = tx_idx; mutex_data!(process).env_idx = GoldilocksField::from_canonical_u64(sc_cnt); mutex_data!(process).call_sc_cnt = GoldilocksField::from_canonical_u64(sc_cnt); diff --git a/zk-vm/src/test.rs b/zk-vm/src/test.rs index ef245b78..39316e02 100644 --- a/zk-vm/src/test.rs +++ b/zk-vm/src/test.rs @@ -72,12 +72,7 @@ pub mod tests { GoldilocksField::from_canonical_u64(3965482278), ]; - let res = node.execute_tx( - GoldilocksField::from_canonical_u64(5), - caller_address, - caller_exe_address, - calldata, - ); + let res = node.execute_tx(caller_address, caller_exe_address, calldata); if res.is_ok() { println!("run tx success:{:?}", res); From 1470efb5ec8c925b90eb07c4c81e5b85a7bf4f0f Mon Sep 17 00:00:00 2001 From: web3Softcloud Date: Mon, 11 Dec 2023 17:28:24 +0800 Subject: [PATCH 2/3] MOD: rm tx_idx in trace generation. --- circuits/src/generation/cpu.rs | 2 +- circuits/src/generation/memory.rs | 2 +- circuits/src/generation/poseidon_chunk.rs | 2 +- circuits/src/generation/sccall.rs | 2 +- circuits/src/generation/tape.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/circuits/src/generation/cpu.rs b/circuits/src/generation/cpu.rs index 3a9cac8f..df4f951e 100644 --- a/circuits/src/generation/cpu.rs +++ b/circuits/src/generation/cpu.rs @@ -61,7 +61,7 @@ pub fn generate_cpu_trace(steps: &Vec) -> [Vec; cpu::NUM_ for (i, s) in steps.iter().enumerate() { // env related columns. - trace[cpu::COL_TX_IDX][i] = F::from_canonical_u64(s.tx_idx.0); + trace[cpu::COL_TX_IDX][i] = F::ZERO; trace[cpu::COL_ENV_IDX][i] = F::from_canonical_u64(s.env_idx.0); trace[cpu::COL_CALL_SC_CNT][i] = F::from_canonical_u64(s.call_sc_cnt.0); diff --git a/circuits/src/generation/memory.rs b/circuits/src/generation/memory.rs index 57a217be..fe445c1d 100644 --- a/circuits/src/generation/memory.rs +++ b/circuits/src/generation/memory.rs @@ -49,7 +49,7 @@ pub fn generate_memory_trace( let mut trace: Vec> = vec![vec![F::ZERO; num_padded_rows]; memory::NUM_MEM_COLS]; for (i, c) in cells.iter().enumerate() { - trace[memory::COL_MEM_TX_IDX][i] = F::from_canonical_u64(c.tx_idx.to_canonical_u64()); + trace[memory::COL_MEM_TX_IDX][i] = F::ZERO; trace[memory::COL_MEM_ENV_IDX][i] = F::from_canonical_u64(c.env_idx.to_canonical_u64()); trace[memory::COL_MEM_IS_RW][i] = F::from_canonical_u64(c.is_rw.to_canonical_u64()); trace[memory::COL_MEM_ADDR][i] = F::from_canonical_u64(c.addr.to_canonical_u64()); diff --git a/circuits/src/generation/poseidon_chunk.rs b/circuits/src/generation/poseidon_chunk.rs index e2926265..d4028a79 100644 --- a/circuits/src/generation/poseidon_chunk.rs +++ b/circuits/src/generation/poseidon_chunk.rs @@ -19,7 +19,7 @@ pub fn generate_poseidon_chunk_trace( }; let mut trace: Vec> = vec![vec![F::ZERO; num_padded_rows]; NUM_POSEIDON_CHUNK_COLS]; for (i, c) in cells.iter().enumerate() { - trace[COL_POSEIDON_CHUNK_TX_IDX][i] = F::from_canonical_u64(c.tx_idx.0); + trace[COL_POSEIDON_CHUNK_TX_IDX][i] = F::ZERO; trace[COL_POSEIDON_CHUNK_ENV_IDX][i] = F::from_canonical_u64(c.env_idx.0); trace[COL_POSEIDON_CHUNK_CLK][i] = F::from_canonical_u32(c.clk); trace[COL_POSEIDON_CHUNK_OPCODE][i] = F::from_canonical_u64(c.opcode.0); diff --git a/circuits/src/generation/sccall.rs b/circuits/src/generation/sccall.rs index d7056a78..47ab4422 100644 --- a/circuits/src/generation/sccall.rs +++ b/circuits/src/generation/sccall.rs @@ -23,7 +23,7 @@ pub fn generate_sccall_trace(cells: &[SCCallRow]) -> [Vec; NUM_ let mut trace: Vec> = vec![vec![F::ZERO; num_padded_rows]; NUM_COL_SCCALL]; for (i, c) in cells.iter().enumerate() { // trace[COL_TAPE_TX_IDX][i] = F::from_canonical_u64(c.tx_idx); - trace[COL_SCCALL_TX_IDX][i] = F::from_canonical_u64(c.tx_idx.to_canonical_u64()); + trace[COL_SCCALL_TX_IDX][i] = F::ZERO; trace[COL_SCCALL_CALLER_ENV_IDX][i] = F::from_canonical_u64(c.caller_env_idx.to_canonical_u64()); for j in 0..CTX_REGISTER_NUM { diff --git a/circuits/src/generation/tape.rs b/circuits/src/generation/tape.rs index e04f1f3b..ce3c2d59 100644 --- a/circuits/src/generation/tape.rs +++ b/circuits/src/generation/tape.rs @@ -21,7 +21,7 @@ pub fn generate_tape_trace(cells: &[TapeRow]) -> [Vec; NUM_COL_ let mut trace: Vec> = vec![vec![F::ZERO; num_padded_rows]; NUM_COL_TAPE]; for (i, c) in cells.iter().enumerate() { - trace[COL_TAPE_TX_IDX][i] = F::from_canonical_u64(c.tx_idx.to_canonical_u64()); + trace[COL_TAPE_TX_IDX][i] = F::ZERO; trace[COL_TAPE_IS_INIT_SEG][i] = if c.is_init { F::ONE } else { F::ZERO }; trace[COL_TAPE_OPCODE][i] = F::from_canonical_u64(c.opcode.to_canonical_u64()); trace[COL_TAPE_ADDR][i] = F::from_canonical_u64(c.addr.to_canonical_u64()); From 21b70b1739ab782ab58419df873fc11f27e0b779 Mon Sep 17 00:00:00 2001 From: Pierre Hong Date: Tue, 12 Dec 2023 12:25:47 +0800 Subject: [PATCH 3/3] MOD: vm inf MOD: calldata order MOD: fmt --- assembler/src/encoder.rs | 2 +- assembler/test_data/asm/fibo_loop.json | 199 +++++++++++++++++- circuits/benches/fibo_loop.rs | 14 +- circuits/benches/sqrt_prophet.rs | 13 +- .../src/builtins/bitwise/bitwise_stark.rs | 7 +- .../builtins/rangecheck/rangecheck_stark.rs | 8 +- circuits/src/cpu/cpu_stark.rs | 8 +- .../generation/ctl_test/debug_trace_print.rs | 7 +- .../generation/ctl_test/poseidon_chunk_mem.rs | 8 +- circuits/src/program/program_stark.rs | 11 +- circuits/src/stark/ola_stark.rs | 19 +- circuits/src/test_utils.rs | 12 +- client/src/main.rs | 15 +- core/src/program/mod.rs | 4 +- core/src/state/error.rs | 2 + core/src/state/mod.rs | 13 ++ core/src/state/state_storage.rs | 33 +++ core/src/types/merkle_tree/mod.rs | 7 + core/src/vm/transaction.rs | 2 +- executor/src/lib.rs | 37 ++-- executor/src/load_tx.rs | 7 +- executor/src/tests.rs | 29 +-- .../trace_analyzer/generate_table.py | 10 +- interpreter/src/interpreter/executor.rs | 2 +- zk-vm/src/lib.rs | 64 +++--- zk-vm/src/test.rs | 4 +- 26 files changed, 391 insertions(+), 146 deletions(-) diff --git a/assembler/src/encoder.rs b/assembler/src/encoder.rs index 4d207b19..8db4c858 100644 --- a/assembler/src/encoder.rs +++ b/assembler/src/encoder.rs @@ -110,7 +110,7 @@ pub(crate) fn encode_to_binary(bundle: RelocatedAsmBundle) -> Result bool { diff --git a/assembler/test_data/asm/fibo_loop.json b/assembler/test_data/asm/fibo_loop.json index fe53c51b..0d152553 100644 --- a/assembler/test_data/asm/fibo_loop.json +++ b/assembler/test_data/asm/fibo_loop.json @@ -1,4 +1,199 @@ { - "program": "main:\n.LBL0_0:\nadd r9 r9 4\nmstore [r9,-2] r9\nmov r1 10\ncall fib_non_recursive\nadd r9 r9 -4\nend\nfib_non_recursive:\n.LBL2_0:\nadd r9 r9 5\nmov r0 r1\nmstore [r9,-1] r0\nmov r0 0\nmstore [r9,-2] r0\nmov r0 1\nmstore [r9,-3] r0\nmov r0 1\nmstore [r9,-4] r0\nmov r0 2\nmstore [r9,-5] r0\njmp .LBL2_1\n.LBL2_1:\nmload r0 [r9,-5]\nmload r1 [r9,-1]\ngte r0 r1 r0\ncjmp r0 .LBL2_2\njmp .LBL2_4\n.LBL2_2:\nmload r1 [r9,-2]\nmload r2 [r9,-3]\nadd r0 r1 r2\nmstore [r9,-4] r0\nmload r0 [r9,-3]\nmstore [r9,-2] r0\nmload r0 [r9,-4]\nmstore [r9,-3] r0\njmp .LBL2_3\n.LBL2_3:\nmload r1 [r9,-5]\nadd r0 r1 1\nmstore [r9,-5] r0\njmp .LBL2_1\n.LBL2_4:\nmload r0 [r9,-4]\nadd r9 r9 -5\nret", - "prophets": [] + "program": "chain_id:\n.LBL8_0:\n add r9 r9 2\n mov r0 7\n mstore [r9,-1] r0\n mload r0 [r9,-1]\n add r1 r9 -2\n mov r2 0\n tload r1 r2 r0\n mload r0 [r9,-2]\n add r9 r9 -2\n ret\nexe_address:\n.LBL9_0:\n add r9 r9 6\n mov r0 8\n mstore [r9,-1] r0\n mload r1 [r9,-1]\n.PROPHET9_0:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-1]\n mov r2 1\n tload r0 r2 r1\n mload r0 [r9,-6]\n mload r0 [r0]\n mstore [r9,-5] r0\n mload r0 [r9,-6]\n mload r0 [r0,+1]\n mstore [r9,-4] r0\n mload r0 [r9,-6]\n mload r0 [r0,+2]\n mstore [r9,-3] r0\n mload r0 [r9,-6]\n mload r0 [r0,+3]\n mstore [r9,-2] r0\n mload r0 [r9,-6]\n add r9 r9 -6\n ret\nfib_non_recursive:\n.LBL10_0:\n add r9 r9 5\n mov r0 r1\n mstore [r9,-1] r0\n mload r0 [r9,-1]\n eq r0 r0 0\n gte r1 r0 0\n neq r0 r0 0\n and r1 r1 r0\n cjmp r1 .LBL10_1\n jmp .LBL10_2\n.LBL10_1:\n mov r0 0\n add r9 r9 -5\n ret\n.LBL10_2:\n jmp .LBL10_3\n.LBL10_3:\n mov r0 0\n mstore [r9,-2] r0\n mov r0 1\n mstore [r9,-3] r0\n mov r0 2\n mstore [r9,-4] r0\n mov r0 2\n mstore [r9,-5] r0\n jmp .LBL10_4\n.LBL10_4:\n mload r0 [r9,-5]\n mload r1 [r9,-1]\n gte r0 r1 r0\n gte r1 r0 0\n neq r0 r0 0\n and r1 r1 r0\n cjmp r1 .LBL10_5\n jmp .LBL10_6\n.LBL10_5:\n mload r1 [r9,-2]\n mload r2 [r9,-3]\n add r0 r1 r2\n mstore [r9,-4] r0\n mload r1 [r9,-4]\n mov r2 3\n.PROPHET10_0:\n mload r0 [r9,-3]\n mstore [r9,-2] r0\n mload r0 [r9,-4]\n mstore [r9,-3] r0\n mload r1 [r9,-5]\n mov r2 3\n.PROPHET10_1:\n mload r0 [r9,-5]\n add r3 r0 1\n mstore [r9,-5] r3\n jmp .LBL10_4\n.LBL10_6:\n mload r0 [r9,-4]\n add r9 r9 -5\n ret\nbench_fib_non_recursive:\n.LBL11_0:\n add r9 r9 11\n mstore [r9,-2] r9\n mov r0 r1\n mov r1 r2\n mstore [r9,-3] r0\n mstore [r9,-4] r1\n mov r0 0\n mstore [r9,-5] r0\n mload r1 [r9,-3]\n mov r2 3\n.PROPHET11_0:\n mload r1 [r9,-4]\n mov r2 3\n.PROPHET11_1:\n mov r0 0\n mstore [r9,-6] r0\n jmp .LBL11_1\n.LBL11_1:\n mload r0 [r9,-6]\n mload r1 [r9,-4]\n gte r2 r1 r0\n neq r0 r0 r1\n and r2 r2 r0\n gte r0 r2 0\n neq r1 r2 0\n and r0 r0 r1\n cjmp r0 .LBL11_2\n jmp .LBL11_3\n.LBL11_2:\n mload r1 [r9,-3]\n call fib_non_recursive\n mstore [r9,-7] r0\n mload r1 [r9,-7]\n mov r2 3\n.PROPHET11_2:\n mload r0 [r9,-5]\n add r0 r0 1\n mstore [r9,-9] r0\n mload r0 [r9,-9]\n mstore [r9,-5] r0\n mload r1 [r9,-5]\n mov r2 3\n.PROPHET11_3:\n mload r0 [r9,-6]\n add r0 r0 1\n mstore [r9,-8] r0\n mload r0 [r9,-8]\n mstore [r9,-6] r0\n jmp .LBL11_1\n.LBL11_3:\n mload r0 [r9,-5]\n add r9 r9 -11\n ret\nmain:\n.LBL12_0:\n add r9 r9 10\n mstore [r9,-2] r9\n mov r0 14\n mstore [r9,-3] r0\n mov r0 1\n mstore [r9,-8] r0\n mload r0 [r9,-3]\n mstore [r9,-5] r0\n mload r1 [r9,-5]\n.PROPHET12_0:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-5]\n mov r3 0\n not r1 r1\n add r1 r1 1\n add r1 r3 r1\n add r0 r0 r1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r1 [r9,-5]\n mov r3 1\n tload r0 r3 r1\n mload r0 [r9,-7]\n mload r0 [r0]\n mstore [r9,-5] r0\n mload r0 [r9,-7]\n add r0 r0 1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r0 [r0]\n mstore [r9,-4] r0\n mload r0 [r9,-5]\n mload r1 [r9,-3]\n add r2 r0 r1\n mstore [r9,-5] r2\n mload r1 [r9,-5]\n.PROPHET12_1:\n mov r0 psp\n mload r0 [r0]\n mstore [r9,-6] r0\n mload r0 [r9,-6]\n mload r1 [r9,-5]\n mov r2 0\n not r1 r1\n add r1 r1 1\n add r1 r2 r1\n add r0 r0 r1\n mstore [r9,-7] r0\n mload r0 [r9,-7]\n mload r1 [r9,-5]\n mov r2 1\n tload r0 r2 r1\n mload r0 [r9,-4]\n eq r1 r0 1015130275\n cjmp r1 .LBL12_1\n jmp .LBL12_2\n.LBL12_1:\n mload r0 [r9,-7]\n mload r1 [r0,+0]\n mload r0 [r9,-7]\n mload r2 [r0,+1]\n call bench_fib_non_recursive\n mov r2 r0\n mov r1 2\n.PROPHET12_2:\n mov r0 psp\n mload r0 [r0]\n mov r1 1\n mstore [r0,-1] r1\n mstore [r0,-2] r2\n add r0 r0 -2\n tstore r0 2\n jmp .LBL12_3\n.LBL12_2:\n jmp .LBL12_3\n.LBL12_3:\n mov r0 0\n add r9 r9 -10\n end\n", + "prophets": [ + { + "label": ".PROPHET9_0", + "code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}", + "inputs": [ + { + "name": "cid.len", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [ + { + "name": "cid.addr", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ] + }, + { + "label": ".PROPHET10_0", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET10_1", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET11_0", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET11_1", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET11_2", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET11_3", + "code": "%{\n entry() {\n printf(cid.base, cid.flag);\n }\n%}", + "inputs": [ + { + "name": "cid.base", + "length": 1, + "is_ref": false, + "is_input_output": false + }, + { + "name": "cid.flag", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [] + }, + { + "label": ".PROPHET12_0", + "code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}", + "inputs": [ + { + "name": "cid.len", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [ + { + "name": "cid.addr", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ] + }, + { + "label": ".PROPHET12_1", + "code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}", + "inputs": [ + { + "name": "cid.len", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [ + { + "name": "cid.addr", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ] + }, + { + "label": ".PROPHET12_2", + "code": "%{\n entry() {\n cid.addr = malloc(cid.len);\n }\n%}", + "inputs": [ + { + "name": "cid.len", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ], + "outputs": [ + { + "name": "cid.addr", + "length": 1, + "is_ref": false, + "is_input_output": false + } + ] + } + ] } \ No newline at end of file diff --git a/circuits/benches/fibo_loop.rs b/circuits/benches/fibo_loop.rs index a1687055..5d537b4d 100644 --- a/circuits/benches/fibo_loop.rs +++ b/circuits/benches/fibo_loop.rs @@ -8,7 +8,7 @@ use circuits::stark::verifier::verify_proof; use core::merkle_tree::tree::AccountTree; use core::program::Program; use core::types::{Field, GoldilocksField}; -use core::vm::transaction::init_tx_context; +use core::vm::transaction::init_tx_context_mock; use core::vm::vm_state::Address; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use executor::load_tx::init_tape; @@ -43,7 +43,7 @@ pub fn test_by_asm_json(path: String) { let mut process = Process::new(); let now = Instant::now(); - let calldata = [47u64, 1000u64, 2u64, 4185064725u64] + let calldata = [4185064725u64, 2u64, 47u64, 1000u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); @@ -54,14 +54,12 @@ pub fn test_by_asm_json(path: String) { Address::default(), Address::default(), Address::default(), - &init_tx_context(), + &init_tx_context_mock(), ); - let _ = process.execute( - &mut program, - &mut Some(prophets), - &mut AccountTree::new_test(), - ); + program.prophets = prophets; + + let _ = process.execute(&mut program, &mut AccountTree::new_test()); info!( "exec time:{}, len:{}", now.elapsed().as_millis(), diff --git a/circuits/benches/sqrt_prophet.rs b/circuits/benches/sqrt_prophet.rs index f20bfc90..265d0bb9 100644 --- a/circuits/benches/sqrt_prophet.rs +++ b/circuits/benches/sqrt_prophet.rs @@ -8,7 +8,7 @@ use circuits::stark::verifier::verify_proof; use core::merkle_tree::tree::AccountTree; use core::program::Program; use core::types::{Field, GoldilocksField}; -use core::vm::transaction::init_tx_context; +use core::vm::transaction::init_tx_context_mock; use core::vm::vm_state::Address; use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use executor::load_tx::init_tape; @@ -42,7 +42,7 @@ pub fn test_by_asm_json(path: String) { let mut process = Process::new(); let now = Instant::now(); - let calldata = [1073741824u64, 16000u64, 2u64, 3509365327u64] + let calldata = [3509365327u64, 2u64, 1073741824u64, 16000u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); @@ -53,14 +53,11 @@ pub fn test_by_asm_json(path: String) { Address::default(), Address::default(), Address::default(), - &init_tx_context(), + &init_tx_context_mock(), ); - let _ = process.execute( - &mut program, - &mut Some(prophets), - &mut AccountTree::new_test(), - ); + program.prophets = prophets; + let _ = process.execute(&mut program, &mut AccountTree::new_test()); info!("exec time:{}", now.elapsed().as_millis()); let mut ola_stark = OlaStark::default(); let now = Instant::now(); diff --git a/circuits/src/builtins/bitwise/bitwise_stark.rs b/circuits/src/builtins/bitwise/bitwise_stark.rs index 405ae175..8336b1da 100644 --- a/circuits/src/builtins/bitwise/bitwise_stark.rs +++ b/circuits/src/builtins/bitwise/bitwise_stark.rs @@ -450,13 +450,10 @@ mod tests { program.instructions.push(inst.to_string()); } + program.prophets = prophets; let mut process = Process::new(); process.addr_storage = Address::default(); - let _ = process.execute( - &mut program, - &mut Some(prophets), - &mut AccountTree::new_test(), - ); + let _ = process.execute(&mut program, &mut AccountTree::new_test()); let (rows, bitwise_beta) = generate_bitwise_trace::(&program.trace.builtin_bitwise_combined); diff --git a/circuits/src/builtins/rangecheck/rangecheck_stark.rs b/circuits/src/builtins/rangecheck/rangecheck_stark.rs index 892671b9..b80f2603 100644 --- a/circuits/src/builtins/rangecheck/rangecheck_stark.rs +++ b/circuits/src/builtins/rangecheck/rangecheck_stark.rs @@ -185,18 +185,14 @@ mod tests { } let mut program: Program = Program::default(); - + program.prophets = prophets; for inst in instructions { program.instructions.push(inst.to_string()); } let mut process = Process::new(); process.addr_storage = Address::default(); - let _ = process.execute( - &mut program, - &mut Some(prophets), - &mut AccountTree::new_test(), - ); + let _ = process.execute(&mut program, &mut AccountTree::new_test()); let rows = generate_rc_trace::(&program.trace.builtin_rangecheck); let len = rows[0].len(); diff --git a/circuits/src/cpu/cpu_stark.rs b/circuits/src/cpu/cpu_stark.rs index fda95b5c..29ddf0f9 100644 --- a/circuits/src/cpu/cpu_stark.rs +++ b/circuits/src/cpu/cpu_stark.rs @@ -1035,19 +1035,19 @@ mod tests { #[test] fn test_ola_vote() { let db_name = "vote_test".to_string(); - let init_calldata = [3u64, 1u64, 2u64, 3u64, 4u64, 2817135588u64] + let init_calldata = [2817135588u64, 4u64, 3u64, 1u64, 2u64, 3u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let vote_calldata = [2u64, 1u64, 2791810083u64] + let vote_calldata = [2791810083u64, 1u64, 2u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_proposal_calldata = [0u64, 3186728800u64] + let winning_proposal_calldata = [3186728800u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_name_calldata = [0u64, 363199787u64] + let winning_name_calldata = [363199787u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); diff --git a/circuits/src/generation/ctl_test/debug_trace_print.rs b/circuits/src/generation/ctl_test/debug_trace_print.rs index 4eb1d974..d263e447 100644 --- a/circuits/src/generation/ctl_test/debug_trace_print.rs +++ b/circuits/src/generation/ctl_test/debug_trace_print.rs @@ -15,7 +15,7 @@ use core::{ crypto::{hash::Hasher, ZkHasher}, merkle_tree::log::{StorageLog, WitnessStorageLog}, types::merkle_tree::{encode_addr, tree_key_default}, - vm::transaction::init_tx_context, + vm::transaction::init_tx_context_mock, }; use executor::{ load_tx::init_tape, @@ -221,7 +221,7 @@ pub fn get_exec_trace( caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); } @@ -245,7 +245,8 @@ pub fn get_exec_trace( previous_value: tree_key_default(), }); - let res = process.execute(&mut program, &mut Some(prophets), &mut db); + program.prophets = prophets; + let res = process.execute(&mut program, &mut db); match res { Ok(_) => {} Err(e) => { diff --git a/circuits/src/generation/ctl_test/poseidon_chunk_mem.rs b/circuits/src/generation/ctl_test/poseidon_chunk_mem.rs index 5c6af1ca..ae00eaa8 100644 --- a/circuits/src/generation/ctl_test/poseidon_chunk_mem.rs +++ b/circuits/src/generation/ctl_test/poseidon_chunk_mem.rs @@ -16,19 +16,19 @@ fn print_poseidon_chunk_mem_ctl_info() { let program_file_name: String = "vote.json".to_string(); let db_name = "vote_test".to_string(); - let init_calldata = [3u64, 1u64, 2u64, 3u64, 4u64, 2817135588u64] + let init_calldata = [2817135588u64, 4u64, 3u64, 1u64, 2u64, 3u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let vote_calldata = [2u64, 1u64, 2791810083u64] + let vote_calldata = [2791810083u64, 1u64, 2u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_proposal_calldata = [0u64, 3186728800u64] + let winning_proposal_calldata = [3186728800u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_name_calldata = [0u64, 363199787u64] + let winning_name_calldata = [363199787u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); diff --git a/circuits/src/program/program_stark.rs b/circuits/src/program/program_stark.rs index 3a42926f..079ce616 100644 --- a/circuits/src/program/program_stark.rs +++ b/circuits/src/program/program_stark.rs @@ -121,7 +121,7 @@ mod tests { use crate::program::columns::NUM_PROG_COLS; use crate::{program::program_stark::ProgramStark, stark::stark::Stark}; use assembler::encoder::encode_asm_from_json_file; - use core::vm::transaction::init_tx_context; + use core::vm::transaction::init_tx_context_mock; use core::{ merkle_tree::tree::AccountTree, program::Program, @@ -181,15 +181,12 @@ mod tests { Address::default(), Address::default(), Address::default(), - &init_tx_context(), + &init_tx_context_mock(), ); } - let _ = process.execute( - &mut program, - &mut Some(prophets), - &mut AccountTree::new_test(), - ); + program.prophets = prophets; + let _ = process.execute(&mut program, &mut AccountTree::new_test()); let insts = program .instructions .iter() diff --git a/circuits/src/stark/ola_stark.rs b/circuits/src/stark/ola_stark.rs index 9d74da77..d8a6cc87 100644 --- a/circuits/src/stark/ola_stark.rs +++ b/circuits/src/stark/ola_stark.rs @@ -660,7 +660,7 @@ mod tests { use core::types::account::Address; use core::types::merkle_tree::{encode_addr, tree_key_default}; use core::types::{Field, GoldilocksField}; - use core::vm::transaction::init_tx_context; + use core::vm::transaction::init_tx_context_mock; use executor::load_tx::init_tape; use executor::trace::{gen_storage_hash_table, gen_storage_table}; use executor::Process; @@ -687,7 +687,7 @@ mod tests { #[test] fn fibo_loop_test() { - let calldata = [10u64, 1u64, 2u64, 4185064725u64] + let calldata = [4185064725u64, 2u64, 10u64, 1u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); @@ -731,7 +731,7 @@ mod tests { #[test] fn test_ola_prophet_sqrt() { - let calldata = [144u64, 10u64, 2u64, 3509365327u64] + let calldata = [3509365327u64, 2u64, 144u64, 10u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); @@ -779,19 +779,19 @@ mod tests { fn test_ola_vote() { let db_name = "vote_test".to_string(); - let init_calldata = [3u64, 1u64, 2u64, 3u64, 4u64, 2817135588u64] + let init_calldata = [2817135588u64, 4u64, 3u64, 1u64, 2u64, 3u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let vote_calldata = [2u64, 1u64, 2791810083u64] + let vote_calldata = [2791810083u64, 1u64, 2u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_proposal_calldata = [0u64, 3186728800u64] + let winning_proposal_calldata = [3186728800u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); - let winning_name_calldata = [0u64, 363199787u64] + let winning_name_calldata = [363199787u64, 0u64] .iter() .map(|v| GoldilocksField::from_canonical_u64(*v)) .collect_vec(); @@ -887,7 +887,7 @@ mod tests { caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); } @@ -911,7 +911,8 @@ mod tests { previous_value: tree_key_default(), }); - let res = process.execute(&mut program, &mut Some(prophets), &mut db); + program.prophets = prophets; + let res = process.execute(&mut program, &mut db); match res { Ok(_) => {} Err(e) => { diff --git a/circuits/src/test_utils.rs b/circuits/src/test_utils.rs index 66ef1075..bf71679d 100644 --- a/circuits/src/test_utils.rs +++ b/circuits/src/test_utils.rs @@ -14,7 +14,7 @@ use plonky2_util::log2_strict; use crate::stark::{constraint_consumer::ConstraintConsumer, vars::StarkEvaluationVars}; use core::merkle_tree::tree::AccountTree; -use core::vm::transaction::init_tx_context; +use core::vm::transaction::init_tx_context_mock; pub fn test_stark_with_asm_path( path: String, @@ -93,7 +93,7 @@ pub fn test_stark_with_asm_path( caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); } @@ -117,7 +117,8 @@ pub fn test_stark_with_asm_path( previous_value: tree_key_default(), }); - let res = process.execute(&mut program, &mut Some(prophets), &mut db); + program.prophets = prophets; + let res = process.execute(&mut program, &mut db); match res { Ok(_) => {} Err(e) => { @@ -230,6 +231,7 @@ pub fn simple_test_stark( instructions: Vec::new(), trace: Default::default(), debug_info: program.debug_info, + prophets, pre_exe_flag: false, }; @@ -270,7 +272,7 @@ pub fn simple_test_stark( caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); } @@ -294,7 +296,7 @@ pub fn simple_test_stark( previous_value: tree_key_default(), }); - let res = process.execute(&mut program, &mut Some(prophets), &mut db); + let res = process.execute(&mut program, &mut db); match res { Ok(_) => {} Err(e) => { diff --git a/client/src/main.rs b/client/src/main.rs index 4ba8a70e..a0f52e34 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -12,7 +12,7 @@ use core::merkle_tree::tree::AccountTree; use core::program::binary_program::BinaryProgram; use core::program::Program; use core::trace::trace::Trace; -use core::vm::transaction::init_tx_context; +use core::vm::transaction::init_tx_context_mock; use core::vm::vm_state::Address; use executor::load_tx::init_tape; use executor::Process; @@ -108,6 +108,7 @@ fn main() { } let mut program: Program = Program::default(); + program.prophets = prophets; for inst in instructions { program.instructions.push(inst.to_string()); @@ -115,10 +116,8 @@ fn main() { let now = Instant::now(); let mut process = Process::new(); - let mut args: Vec<_> = calldata.drain(2..).collect(); - calldata.reverse(); - args.extend(calldata); - if args.len() < 2 { + + if calldata.len() < 2 { panic!("args length must larger than 2"); } @@ -146,17 +145,16 @@ fn main() { ]; init_tape( &mut process, - args, + calldata, caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); process .execute( &mut program, - &mut Some(prophets), &mut AccountTree::new_db_test("./db_test".to_string()), ) .expect("OlaVM execute fail"); @@ -184,6 +182,7 @@ fn main() { instructions: trace.raw_binary_instructions.clone(), trace, debug_info: None, + prophets: HashMap::new(), pre_exe_flag: false, }; diff --git a/core/src/program/mod.rs b/core/src/program/mod.rs index 793f9908..dd9ed90a 100644 --- a/core/src/program/mod.rs +++ b/core/src/program/mod.rs @@ -1,8 +1,9 @@ +use crate::program::binary_program::OlaProphet; use crate::trace::trace::Trace; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::types::Field64; use serde::{Deserialize, Serialize}; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; pub mod binary_program; pub mod decoder; @@ -19,6 +20,7 @@ pub struct Program { pub instructions: Vec, pub trace: Trace, pub debug_info: Option>, + pub prophets: HashMap, pub pre_exe_flag: bool, } diff --git a/core/src/state/error.rs b/core/src/state/error.rs index 30aa2b1c..17d080bb 100644 --- a/core/src/state/error.rs +++ b/core/src/state/error.rs @@ -8,4 +8,6 @@ pub enum StateError { VmExecError(String), #[error("VM json serde error")] JsonSerdeError(#[from] serde_json::Error), + #[error("VM json serde error")] + GetProgramError(String), } diff --git a/core/src/state/mod.rs b/core/src/state/mod.rs index 90fe2e41..626d573e 100644 --- a/core/src/state/mod.rs +++ b/core/src/state/mod.rs @@ -56,6 +56,19 @@ where return Ok(code_hash); } + pub fn save_program( + &mut self, + code_hash: &TreeValue, + contract: &Vec, + ) -> Result<(), StateError> { + self.state_storage.save_program(&code_hash, &contract)?; + Ok(()) + } + + pub fn get_program(&self, code_hashes: &TreeValue) -> Result, StateError> { + self.state_storage.get_program(code_hashes) + } + pub fn get_contracts( &self, code_hashes: &Vec, diff --git a/core/src/state/state_storage.rs b/core/src/state/state_storage.rs index 829dafec..c2150bc1 100644 --- a/core/src/state/state_storage.rs +++ b/core/src/state/state_storage.rs @@ -47,6 +47,39 @@ impl StateStorage { self.db.write(batch).map_err(StateError::StorageIoError) } + pub fn save_program( + &mut self, + code_hash: &TreeValue, + code: &Vec, + ) -> Result<(), StateError> { + let mut batch = WriteBatch::default(); + let cf = self + .db + .cf_state_keeper_handle(StateKeeperColumnFamily::Contracts); + let code_key = tree_key_to_u8_arr(code_hash); + + batch.put_cf(cf, &code_key, code); + self.db.write(batch).map_err(StateError::StorageIoError) + } + + pub fn get_program(&self, code_hash: &TreeValue) -> Result, StateError> { + let cf = self + .db + .cf_state_keeper_handle(StateKeeperColumnFamily::Contracts); + + let mut res = self.db.get_cf(cf, tree_key_to_u8_arr(code_hash)); + + if let Ok(res) = res { + if res.is_some() { + return Ok(res.unwrap()); + } else { + return Err(StateError::GetProgramError("program empty".to_string())); + } + } + + Err(StateError::GetProgramError(res.err().unwrap().to_string())) + } + pub fn get_contracts( &self, code_hashes: &Vec, diff --git a/core/src/types/merkle_tree/mod.rs b/core/src/types/merkle_tree/mod.rs index 4aa0e703..45fce068 100644 --- a/core/src/types/merkle_tree/mod.rs +++ b/core/src/types/merkle_tree/mod.rs @@ -180,3 +180,10 @@ pub fn tree_key_to_leaf_index(value: &TreeKey) -> LevelIndex { let index = tree_key_to_u256(value); LevelIndex((ROOT_TREE_DEPTH as u16, index)) } + +pub fn field_arr_to_u8_arr(value: &Vec) -> Vec { + value.iter().fold(Vec::new(), |mut key_vec, item| { + key_vec.extend(item.0.to_le_bytes().to_vec()); + key_vec + }) +} diff --git a/core/src/vm/transaction.rs b/core/src/vm/transaction.rs index 20d39ad0..9e86dcef 100644 --- a/core/src/vm/transaction.rs +++ b/core/src/vm/transaction.rs @@ -14,7 +14,7 @@ pub struct TxCtxInfo { pub tx_hash: [GoldilocksField; 4], } -pub fn init_tx_context() -> TxCtxInfo { +pub fn init_tx_context_mock() -> TxCtxInfo { TxCtxInfo { block_number: GoldilocksField::from_canonical_u64(3), block_timestamp: GoldilocksField::from_canonical_u64(1692846754), diff --git a/executor/src/lib.rs b/executor/src/lib.rs index 4f4e4f73..70fcd466 100644 --- a/executor/src/lib.rs +++ b/executor/src/lib.rs @@ -1795,7 +1795,6 @@ impl Process { pub fn execute( &mut self, program: &mut Program, - prophets: &mut Option>, account_tree: &mut AccountTree, ) -> Result { let instrs_len = program.instructions.len() as u64; @@ -1806,6 +1805,19 @@ impl Process { while pc < instrs_len { pc = self.execute_decode(program, pc, instrs_len).unwrap(); } + // init heap ptr + self.memory.write( + HP_START_ADDR, + 0, //write, clk is 0 + GoldilocksField::from_canonical_u64(0 as u64), + GoldilocksField::from_canonical_u64(MemoryType::ReadWrite as u64), + GoldilocksField::from_canonical_u64(MemoryOperation::Write as u64), + GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64), + GoldilocksField::from_canonical_u64(0_u64), + GoldilocksField::from_canonical_u64(1_u64), + GoldilocksField(HP_START_ADDR + 1), + self.env_idx, + ); } let decode_time = start.elapsed(); debug!("decode_time: {}", decode_time.as_secs()); @@ -1817,11 +1829,6 @@ impl Process { let mut start = Instant::now(); - let mut prophets_insert = HashMap::new(); - if prophets.is_some() { - prophets_insert = prophets.clone().unwrap(); - } - // todo : why need clear? //self.storage_log.clear(); let mut end_step = None; @@ -1843,20 +1850,6 @@ impl Process { } program.trace.builtin_poseidon.extend(prog_hash_rows); - // init heap ptr - self.memory.write( - HP_START_ADDR, - 0, //write, clk is 0 - GoldilocksField::from_canonical_u64(0 as u64), - GoldilocksField::from_canonical_u64(MemoryType::ReadWrite as u64), - GoldilocksField::from_canonical_u64(MemoryOperation::Write as u64), - GoldilocksField::from_canonical_u64(FilterLockForMain::False as u64), - GoldilocksField::from_canonical_u64(0_u64), - GoldilocksField::from_canonical_u64(1_u64), - GoldilocksField(HP_START_ADDR + 1), - self.env_idx, - ); - loop { self.register_selector = RegisterSelector::default(); let registers_status = self.registers; @@ -1975,8 +1968,8 @@ impl Process { _ => panic!("not match opcode:{}", opcode), } - if prophets_insert.get(&pc_status).is_some() { - self.prophet(&mut prophets_insert[&pc_status].clone())? + if program.prophets.get(&pc_status).is_some() { + self.prophet(&mut program.prophets[&pc_status].clone())? } if print_vm_state { diff --git a/executor/src/load_tx.rs b/executor/src/load_tx.rs index 46595efc..d1744db9 100644 --- a/executor/src/load_tx.rs +++ b/executor/src/load_tx.rs @@ -70,15 +70,18 @@ load_ctx_to_tape!(load_tx_context, TxCtxInfo); pub fn init_tape( process: &mut Process, - calldata: Vec, + mut calldata: Vec, caller_exe_addr: Address, callee_addr: Address, callee_exe_addr: Address, ctx_info: &TxCtxInfo, ) { + let mut args: Vec<_> = calldata.drain(2..).collect(); + calldata.reverse(); + args.extend(calldata); let tp_start = load_tx_context(process, ctx_info); process.tp = GoldilocksField::from_canonical_u64(tp_start as u64); - load_tx_calldata(process, &calldata); + load_tx_calldata(process, &args); let ctx_addr_len = load_ctx_addr_info( process, &init_ctx_addr_info(caller_exe_addr, callee_addr, callee_exe_addr), diff --git a/executor/src/tests.rs b/executor/src/tests.rs index 708c6c6b..d9590983 100644 --- a/executor/src/tests.rs +++ b/executor/src/tests.rs @@ -13,7 +13,7 @@ use core::program::Program; use core::types::account::Address; use core::types::merkle_tree::tree_key_default; use core::types::merkle_tree::{decode_addr, encode_addr}; -use core::vm::transaction::init_tx_context; +use core::vm::transaction::init_tx_context_mock; use log::{debug, LevelFilter}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::field::types::Field; @@ -27,9 +27,9 @@ fn executor_run_test_program( print_trace: bool, call_data: Option>, ) { - // let _ = env_logger::builder() - // .filter_level(LevelFilter::Debug) - // .try_init(); + let _ = env_logger::builder() + .filter_level(LevelFilter::Info) + .try_init(); let file = File::open(bin_file_path).unwrap(); let reader = BufReader::new(file); @@ -53,6 +53,7 @@ fn executor_run_test_program( instructions: Vec::new(), trace: Default::default(), debug_info: program.debug_info, + prophets: prophets, pre_exe_flag: false, }; @@ -92,7 +93,7 @@ fn executor_run_test_program( caller_addr, callee, callee_exe_addr, - &init_tx_context(), + &init_tx_context_mock(), ); } process.addr_code = callee_exe_addr; @@ -117,7 +118,7 @@ fn executor_run_test_program( previous_value: tree_key_default(), }); - let res = process.execute(&mut program, &mut Some(prophets), &mut account_tree); + let res = process.execute(&mut program, &mut account_tree); if res.is_err() { gen_dump_file(&mut process, &mut program); @@ -189,11 +190,18 @@ fn call_test() { #[test] fn fibo_use_loop_decode() { + let calldata = vec![ + GoldilocksField::from_canonical_u64(10), + GoldilocksField::from_canonical_u64(1), + GoldilocksField::from_canonical_u64(2), + GoldilocksField::from_canonical_u64(1015130275), + ]; + executor_run_test_program( "../assembler/test_data/bin/fibo_loop.json", "fib_loop_trace.txt", true, - None, + Some(calldata), ); } @@ -454,7 +462,6 @@ fn gen_storage_table_test() { store_val, tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); store_val[3] = GoldilocksField::from_canonical_u64(5); @@ -465,7 +472,6 @@ fn gen_storage_table_test() { store_val, tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); @@ -476,7 +482,6 @@ fn gen_storage_table_test() { tree_key_default(), tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); @@ -487,7 +492,6 @@ fn gen_storage_table_test() { tree_key_default(), tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); @@ -501,7 +505,6 @@ fn gen_storage_table_test() { store_val, tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); @@ -513,7 +516,6 @@ fn gen_storage_table_test() { store_val, tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); @@ -524,7 +526,6 @@ fn gen_storage_table_test() { tree_key_default(), tree_key_default(), GoldilocksField::ZERO, - GoldilocksField::ZERO, ); hash.push(tree_key_default()); diff --git a/infrastructure/trace_analyzer/generate_table.py b/infrastructure/trace_analyzer/generate_table.py index a5db074f..9ad42c0f 100644 --- a/infrastructure/trace_analyzer/generate_table.py +++ b/infrastructure/trace_analyzer/generate_table.py @@ -34,7 +34,6 @@ class OpcodeValue(Enum): class JsonMainTraceColumnType(Enum): - TX_IDX = 'tx_idx' ENV_IDX = 'env_idx' SC_CNT = 'call_sc_cnt' CLK = 'clk' @@ -77,7 +76,6 @@ class JsonMainTraceColumnType(Enum): SEL_SLOAD = 'sel_sload' class MainTraceColumnType(Enum): - TX_IDX = 'tx_idx' ENV_IDX = 'env_idx' SC_CNT = 'call_sc_cnt' CLK = 'clk' @@ -180,7 +178,6 @@ class MainTraceColumnType(Enum): class MemoryTraceColumnType(Enum): - TX_IDX = 'tx_idx' ENV_IDX = 'env_idx' ADDR = 'addr' CLK = 'clk' @@ -238,7 +235,6 @@ class ComparisonTraceColumnType(Enum): FILTER_LOOKING_FOR_RANGE_CHECK = 'filter_looking_rc' class StorageTraceColumnType(Enum): - TX_IDX = 'tx_idx' ENV_IDX = 'env_idx' CLK = 'clk' OP = 'opcode' @@ -296,7 +292,6 @@ class PoseidonHashTraceColumnType(Enum): FILTER_LOOKED_STORAGE_BRANCH = 'filter_looked_storage_branch' class PoseidonChunkTraceColumnType(Enum): - TX_IDX = 'tx_idx' ENV_IDX = 'env_idx' CLK = 'clk' OPCODE = "opcode" @@ -310,7 +305,6 @@ class PoseidonChunkTraceColumnType(Enum): IS_EXT_LINE = 'is_ext_line' class TapeTraceColumnType(Enum): - TX_IDX = 'tx_idx' IS_INIT = "is_init" OPCODE = "opcode" ADDR = "addr" @@ -429,8 +423,8 @@ def main(): worksheet.write(row_index, col, reg) col += 1 elif data.value == 'asm': - if trace_json["raw_instructions"] != {}: - worksheet.write(row_index, col, '{0}'.format(trace_json["raw_instructions"]['{0}'.format(row["pc"])])) + # if trace_json["raw_instructions"] != {}: + # worksheet.write(row_index, col, '{0}'.format(trace_json["raw_instructions"]['{0}'.format(row["pc"])])) col += 1 else: if data.value == "instruction" or data.value == "opcode" or data.value == "aux0": diff --git a/interpreter/src/interpreter/executor.rs b/interpreter/src/interpreter/executor.rs index 57128bb3..1ec1f2cf 100644 --- a/interpreter/src/interpreter/executor.rs +++ b/interpreter/src/interpreter/executor.rs @@ -18,7 +18,7 @@ use crate::utils::number::NumberRet::{Multiple, Single}; use crate::utils::number::{Number, NumberResult, NumberRet}; use core::types::PrimeField64; use core::vm::memory::MemoryTree; -use log::debug; +use log::{debug, info}; #[macro_export] macro_rules! ident_lookup { diff --git a/zk-vm/src/lib.rs b/zk-vm/src/lib.rs index 6ac84664..dc14491a 100644 --- a/zk-vm/src/lib.rs +++ b/zk-vm/src/lib.rs @@ -14,17 +14,19 @@ use ola_core::state::NodeState; use ola_core::storage::db::{Database, RocksDB}; use ola_core::trace::trace::Trace; use ola_core::types::account::Address; -use ola_core::types::merkle_tree::{encode_addr, tree_key_default, TreeValue}; +use ola_core::types::merkle_tree::{encode_addr, tree_key_default, tree_key_to_u8_arr, TreeValue}; use ola_core::types::GoldilocksField; use ola_core::types::{Field, PrimeField64}; use ola_core::vm::error::ProcessorError; use ola_core::vm::transaction::TxCtxInfo; use ola_core::vm::vm_state::{SCCallType, VMState}; +use ola_core::crypto::hash::Hasher; use ola_core::merkle_tree::log::{StorageLog, WitnessStorageLog}; +use ola_core::types::storage::field_arr_to_u8_arr; use std::collections::{BTreeMap, HashMap}; use std::fs::File; -use std::io::BufReader; +use std::io::{BufReader, Write}; use std::ops::Not; use std::path::Path; use std::sync::{Arc, Mutex}; @@ -78,6 +80,14 @@ impl OlaVM { self.ola_state.save_contract(contract) } + pub fn save_program( + &mut self, + code_hash: &TreeValue, + contract: &Vec, + ) -> Result<(), StateError> { + self.ola_state.save_program(code_hash, contract) + } + pub fn save_prophet(&mut self, code_hash: &TreeValue, prophet: &str) -> Result<(), StateError> { self.ola_state.save_prophet(code_hash, prophet) } @@ -90,6 +100,10 @@ impl OlaVM { self.ola_state.save_debug_info(code_hash, debug_info) } + pub fn get_program(&mut self, code_hashes: &TreeValue) -> Result, StateError> { + self.ola_state.get_program(code_hashes) + } + pub fn get_contracts( &mut self, code_hashes: &Vec, @@ -127,9 +141,8 @@ impl OlaVM { &mut self, process: &mut Process, program: &mut Program, - prophets: &mut Option>, ) -> Result { - process.execute(program, prophets, &mut self.account_tree) + process.execute(program, &mut self.account_tree) } pub fn contract_run( @@ -143,39 +156,40 @@ impl OlaVM { let code_hash = self.get_contract_map(&exe_code_addr)?; if get_code { - let contract = self.get_contract(&code_hash)?; - for inst in &contract { - program - .instructions - .push(format!("0x{:x}", inst.to_canonical_u64())); + let contract = self.get_program(&code_hash)?; + let bin_program: BinaryProgram = + serde_json::from_str(std::str::from_utf8(&contract.to_vec()).unwrap()).unwrap(); + + let instructions = bin_program.bytecode.split("\n"); + let code: Vec<_> = instructions + .clone() + .map(|e| { + GoldilocksField::from_canonical_u64(u64::from_str_radix(&e[2..], 16).unwrap()) + }) + .collect(); + let mut prophets = HashMap::new(); + for item in bin_program.prophets { + prophets.insert(item.host as u64, item); } + program.debug_info = bin_program.debug_info; + program.prophets = prophets; - if let Ok(debug_str) = self.get_debug_info(&code_hash) { - let debug_info = - serde_json::from_str::>(&debug_str).unwrap(); - program.debug_info = Some(debug_info); + for inst in instructions { + program.instructions.push(inst.to_string()); } + process.program_log.push(WitnessStorageLog { storage_log: StorageLog::new_read_log(exe_code_addr, code_hash), previous_value: tree_key_default(), }); + program .trace .addr_program_hash - .insert(encode_addr(&exe_code_addr), contract); + .insert(encode_addr(&exe_code_addr), code); } - let prophet = self.get_prophet(&code_hash).unwrap(); - let mut prophets = HashMap::new(); - for item in serde_json::from_str::>(&prophet)? { - prophets.insert(item.host as u64, item); - } - - let res = self.vm_run( - process, - program, - &mut prophets.is_empty().not().then(|| prophets), - ); + let res = self.vm_run(process, program); if let Ok(vm_state) = res { Ok(vm_state) } else { diff --git a/zk-vm/src/test.rs b/zk-vm/src/test.rs index 39316e02..33813ba1 100644 --- a/zk-vm/src/test.rs +++ b/zk-vm/src/test.rs @@ -6,7 +6,7 @@ pub mod tests { use ola_core::types::merkle_tree::TreeValue; use ola_core::types::Field; use ola_core::types::GoldilocksField; - use ola_core::vm::transaction::init_tx_context; + use ola_core::vm::transaction::init_tx_context_mock; use std::fs::File; use std::io::Write; use tempfile::TempDir; @@ -46,7 +46,7 @@ pub mod tests { TempDir::new() .expect("failed get temporary directory for RocksDB") .path(), - init_tx_context(), + init_tx_context_mock(), ); let _code_hash = node .manual_deploy(