Skip to content

Commit

Permalink
Add circuit reports to riscv_opcodes example (#585)
Browse files Browse the repository at this point in the history
implement circuit statistics
  • Loading branch information
mcalancea authored Nov 15, 2024
1 parent 82af85a commit e7bebcd
Show file tree
Hide file tree
Showing 11 changed files with 446 additions and 2 deletions.
101 changes: 101 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ paste = "1"
plonky2 = "0.2"
poseidon = { path = "./poseidon" }
pprof = { version = "0.13", features = ["flamegraph"] }
prettytable-rs = "^0.10"
rand = "0.8"
rand_chacha = { version = "0.3", features = ["serde1"] }
rand_core = "0.6"
Expand Down
5 changes: 5 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ args = ["fmt", "-p", "ceno_zkvm", "--", "--check"]
command = "cargo"
workspace = false

[tasks.riscv_stats]
args = ["run", "--bin", "riscv_stats"]
command = "cargo"
workspace = false

[tasks.clippy]
args = [
"clippy",
Expand Down
2 changes: 2 additions & 0 deletions ceno_zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ transcript = { path = "../transcript" }

itertools.workspace = true
paste.workspace = true
prettytable-rs.workspace = true
strum.workspace = true
strum_macros.workspace = true
tracing.workspace = true
tracing-flame.workspace = true
tracing-subscriber.workspace = true


clap = { version = "4.5", features = ["derive"] }
generic_static = "0.2"
rand.workspace = true
Expand Down
15 changes: 14 additions & 1 deletion ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ceno_emul::{
};
use ceno_zkvm::{
scheme::{PublicValues, constants::MAX_NUM_VARIABLES, verifier::ZKVMVerifier},
stats::{StaticReport, TraceReport},
structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses},
};
use ff_ext::ff::Field;
Expand All @@ -29,7 +30,6 @@ use sumcheck::{entered_span, exit_span};
use tracing_flame::FlameLayer;
use tracing_subscriber::{EnvFilter, Registry, fmt, fmt::format::FmtSpan, layer::SubscriberExt};
use transcript::Transcript;

const PROGRAM_SIZE: usize = 16;
// For now, we assume registers
// - x0 is not touched,
Expand Down Expand Up @@ -122,6 +122,7 @@ fn main() {
let mut zkvm_cs = ZKVMConstraintSystem::default();

let config = Rv32imConfig::<E>::construct_circuits(&mut zkvm_cs);

let prog_config = zkvm_cs.register_table_circuit::<ExampleProgramTableCircuit<E>>();
zkvm_cs.register_global_state::<GlobalState>();

Expand All @@ -133,6 +134,8 @@ fn main() {
&program,
);

let static_report = StaticReport::new(&zkvm_cs);

let reg_init = initial_registers();
// Define program constant here
let program_data: &[u32] = &[];
Expand Down Expand Up @@ -288,6 +291,16 @@ fn main() {
.assign_table_circuit::<ExampleProgramTableCircuit<E>>(&zkvm_cs, &prog_config, &program)
.unwrap();

// get instance counts from witness matrices
let trace_report = TraceReport::new_via_witnesses(
&static_report,
&zkvm_witness,
"EXAMPLE_PROGRAM in riscv_opcodes.rs",
);

trace_report.save_json("report.json");
trace_report.save_table("report.txt");

MockProver::assert_satisfied_full(
zkvm_cs.clone(),
zkvm_fixed_traces.clone(),
Expand Down
18 changes: 18 additions & 0 deletions ceno_zkvm/src/bin/riscv_stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::collections::BTreeMap;

use ceno_zkvm::{
instructions::riscv::Rv32imConfig,
stats::{StaticReport, TraceReport},
structs::ZKVMConstraintSystem,
};
use goldilocks::GoldilocksExt2;
type E = GoldilocksExt2;
fn main() {
let mut zkvm_cs = ZKVMConstraintSystem::default();

let _ = Rv32imConfig::<E>::construct_circuits(&mut zkvm_cs);
let static_report = StaticReport::new(&zkvm_cs);
let report = TraceReport::new(&static_report, BTreeMap::new(), "no program");
report.save_table("riscv_stats.txt");
println!("INFO: generated riscv_stats.txt");
}
2 changes: 1 addition & 1 deletion ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

/// namespace used for annotation, preserve meta info during circuit construction
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default, serde::Serialize)]
pub struct NameSpace {
namespace: Vec<String>,
}
Expand Down
1 change: 1 addition & 0 deletions ceno_zkvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod expression;
pub mod gadgets;
mod keygen;
pub mod state;
pub mod stats;
pub mod structs;
mod uint;
mod utils;
Expand Down
Loading

0 comments on commit e7bebcd

Please sign in to comment.