diff --git a/Cargo.toml b/Cargo.toml index 2a36e25..9a59d7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" plonky2 = { git = "https://github.com/0xPolygonZero/plonky2", rev = "76da138" } starky = { git = "https://github.com/0xPolygonZero/plonky2", rev = "76da138" } anyhow = "1.0.86" +prettytable-rs = "0.10.0" diff --git a/src/lib.rs b/src/lib.rs index e64e379..dff7fa6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,8 @@ #[allow(dead_code)] mod preflight_simulator; #[allow(dead_code)] +mod utilities; +#[allow(dead_code)] mod vm_specs; // STARK tables ------------- diff --git a/src/stark_cpu.rs b/src/stark_cpu.rs index 3f742dc..b49bf66 100644 --- a/src/stark_cpu.rs +++ b/src/stark_cpu.rs @@ -22,20 +22,15 @@ use starky::{ ConstraintConsumer, RecursiveConstraintConsumer, }, - evaluation_frame::{ - StarkEvaluationFrame, - StarkFrame, - }, + evaluation_frame::StarkFrame, stark::Stark, util::trace_rows_to_poly_values, }; use crate::{ preflight_simulator::PreflightSimulation, - vm_specs::{ - Instruction, - Program, - }, + utilities::debug_table, + vm_specs::Instruction, }; // Table description: @@ -53,6 +48,11 @@ use crate::{ const NUM_DYNAMIC_COLS: usize = 5; const NUM_OPCODE_ONEHOT: usize = 11; const NUMBER_OF_COLS: usize = NUM_DYNAMIC_COLS + NUM_OPCODE_ONEHOT + 1; +const ROW_HEADINGS: [&str; NUMBER_OF_COLS] = [ + "clk", "pc", "r0", "r1", "loc", "op_add", "op_sub", "op_mul", "op_div", + "op_shl", "op_shr", "op_jz", "op_jnz", "op_lb", "op_sb", "op_halt", + "is_exec", +]; const PUBLIC_INPUTS: usize = 0; #[derive(Clone, Copy)] @@ -114,6 +114,8 @@ where }) .collect::>(); + debug_table("CPU", ROW_HEADINGS, &trace); + // Need to pad the trace to a len of some power of 2 let pow2_len = trace .len() @@ -185,6 +187,8 @@ mod tests { verifier::verify_stark_proof, }; + use crate::vm_specs::Program; + use super::*; #[test] diff --git a/src/stark_memory.rs b/src/stark_memory.rs index 868a29e..ae700da 100644 --- a/src/stark_memory.rs +++ b/src/stark_memory.rs @@ -22,20 +22,16 @@ use starky::{ ConstraintConsumer, RecursiveConstraintConsumer, }, - evaluation_frame::{ - StarkEvaluationFrame, - StarkFrame, - }, + evaluation_frame::StarkFrame, stark::Stark, util::trace_rows_to_poly_values, }; +use std::cmp::Ordering; use crate::{ preflight_simulator::PreflightSimulation, - vm_specs::{ - Instruction, - Program, - }, + utilities::debug_table, + vm_specs::Instruction, }; // Table description: @@ -46,6 +42,8 @@ use crate::{ // +---------------+-------+-------+-------+-------+---------+-------------+ // const NUMBER_OF_COLS: usize = 7; +const ROW_HEADINGS: [&str; NUMBER_OF_COLS] = + ["addr", "clk", "val", "is_lb", "is_sb", "is_init", "is_exec"]; const PUBLIC_INPUTS: usize = 0; #[derive(Clone, Copy)] @@ -125,6 +123,31 @@ where F::ONE, ]); }); + + // We need this since we want table to be sorted by `(MemoryLocation, Clock)` + trace.sort_by(|a, b| { + let addrs = (a[0].to_canonical_u64(), b[0].to_canonical_u64()); + let clocks = (a[1].to_canonical_u64(), b[1].to_canonical_u64()); + if addrs.0 < addrs.1 { + return Ordering::Less; + } else if addrs.0 > addrs.1 { + return Ordering::Greater; + } else { + if clocks.0 > clocks.1 { + return Ordering::Greater; + } + } + Ordering::Less + }); + + debug_table("memory", ROW_HEADINGS, &trace); + // + //println!("---- MEMORY STARK ----"); + //for row in &trace { + // println!("{:?}", row); + //} + //println!("---- MEMORY STARK ----"); + // Need to pad the trace to a len of some power of 2 let pow2_len = trace .len() @@ -196,16 +219,18 @@ mod tests { verifier::verify_stark_proof, }; + use crate::vm_specs::Program; + use super::*; + const D: usize = 2; + type C = PoseidonGoldilocksConfig; + type F = >::F; + type S = MemoryStark; + type PR = StarkProofWithPublicInputs; + #[test] fn test_nil_program() { - const D: usize = 2; - type C = PoseidonGoldilocksConfig; - type F = >::F; - type S = MemoryStark; - type PR = StarkProofWithPublicInputs; - let stark = S::new(); let mut config = StarkConfig::standard_fast_config(); // Need to do this since our table is small. Need atleast 1<<5 @@ -229,4 +254,33 @@ mod tests { let verification = verify_stark_proof(stark, proof.unwrap(), &config); assert!(verification.is_ok()); } + + #[test] + fn test_nil_code_program_init_memory() { + let stark = S::new(); + let mut config = StarkConfig::standard_fast_config(); + // Need to do this since our table is small. Need atleast 1<<5 + // sized table to not affect this + config + .fri_config + .cap_height = 1; + let mut program = Program::default(); + program + .memory_init + .insert(20, 211); + let simulation = PreflightSimulation::simulate(&program); + assert!(simulation.is_ok()); + let simulation = simulation.unwrap(); + let trace = MemoryStark::::generate_trace(&simulation); + let proof: Result = prove( + stark.clone(), + &config, + trace, + &[], + &mut TimingTree::default(), + ); + assert!(proof.is_ok()); + let verification = verify_stark_proof(stark, proof.unwrap(), &config); + assert!(verification.is_ok()); + } } diff --git a/src/utilities.rs b/src/utilities.rs new file mode 100644 index 0000000..e9ceafb --- /dev/null +++ b/src/utilities.rs @@ -0,0 +1,16 @@ +use plonky2::hash::hash_types::RichField; +use prettytable::Table; + +pub fn debug_table( + table_name: &str, + headings: [&str; COLS], + values: &Vec<[F; COLS]>, +) { + let mut table = Table::new(); + table.add_row(headings.into()); + for row in values { + table.add_row(row.into()); + } + println!("TRACE OUTPUT: {}\n", table_name); + table.printstd(); +} diff --git a/src/utility_macros.rs b/src/utility_macros.rs deleted file mode 100644 index 63a8fc6..0000000 --- a/src/utility_macros.rs +++ /dev/null @@ -1,17 +0,0 @@ -//! While macros shouln't really be used in a "toy" implementation, -//! they can help a lot! But they are also cumbersome to use. In the -//! sense that they are hard to read. -//! -//! Macros below are intended to be easy to read, comments are provided -//! where necessary - -macro_rules! derive_get_number_of_columns { - ($structure: ident) => { - impl $structure { - pub const fn get_number_of_columns() -> usize { - // `u8` is guaranteed to have a `size_of` of 1 - std::mem::size_of::<$structure>(); - } - } - }; -}