Skip to content

Commit

Permalink
add: print utils
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jul 8, 2024
1 parent 5324ee4 commit 88a966a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#[allow(dead_code)]
mod preflight_simulator;
#[allow(dead_code)]
mod utilities;
#[allow(dead_code)]
mod vm_specs;

// STARK tables -------------
Expand Down
20 changes: 12 additions & 8 deletions src/stark_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)]
Expand Down Expand Up @@ -114,6 +114,8 @@ where
})
.collect::<Vec<[F; NUMBER_OF_COLS]>>();

debug_table("CPU", ROW_HEADINGS, &trace);

// Need to pad the trace to a len of some power of 2
let pow2_len = trace
.len()
Expand Down Expand Up @@ -185,6 +187,8 @@ mod tests {
verifier::verify_stark_proof,
};

use crate::vm_specs::Program;

use super::*;

#[test]
Expand Down
82 changes: 68 additions & 14 deletions src/stark_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = <C as GenericConfig<D>>::F;
type S = MemoryStark<F, D>;
type PR = StarkProofWithPublicInputs<GoldilocksField, C, 2>;

#[test]
fn test_nil_program() {
const D: usize = 2;
type C = PoseidonGoldilocksConfig;
type F = <C as GenericConfig<D>>::F;
type S = MemoryStark<F, D>;
type PR = StarkProofWithPublicInputs<GoldilocksField, C, 2>;

let stark = S::new();
let mut config = StarkConfig::standard_fast_config();
// Need to do this since our table is small. Need atleast 1<<5
Expand All @@ -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::<F, D>::generate_trace(&simulation);
let proof: Result<PR, anyhow::Error> = 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());
}
}
16 changes: 16 additions & 0 deletions src/utilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use plonky2::hash::hash_types::RichField;
use prettytable::Table;

pub fn debug_table<F: RichField, const COLS: usize>(
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();
}
17 changes: 0 additions & 17 deletions src/utility_macros.rs

This file was deleted.

0 comments on commit 88a966a

Please sign in to comment.