Skip to content

Commit

Permalink
Merge branch 'master' into feat/guest-example
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Nicolas committed Nov 15, 2024
2 parents 9c81398 + e7bebcd commit 3a78b11
Show file tree
Hide file tree
Showing 18 changed files with 590 additions and 103 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 @@ -32,6 +32,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 @@ -24,12 +24,14 @@ transcript = { path = "../transcript" }
itertools.workspace = true
num-traits.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
48 changes: 38 additions & 10 deletions ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ 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;
use goldilocks::GoldilocksExt2;
use itertools::Itertools;
use mpcs::{Basefold, BasefoldRSParams, PolynomialCommitmentScheme};
use sumcheck::{entered_span, exit_span};
use tracing_flame::FlameLayer;
use tracing_subscriber::{EnvFilter, Registry, fmt, layer::SubscriberExt};
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 @@ -89,17 +90,29 @@ fn main() {
.collect(),
);
let (flame_layer, _guard) = FlameLayer::with_file("./tracing.folded").unwrap();
let mut fmt_layer = fmt::layer()
.compact()
.with_span_events(FmtSpan::CLOSE)
.with_thread_ids(false)
.with_thread_names(false);
fmt_layer.set_ansi(false);

// Take filtering directives from RUST_LOG env_var
// Directive syntax: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives
// Example: RUST_LOG="info" cargo run.. to get spans/events at info level; profiling spans are info
// Example: RUST_LOG="[sumcheck]" cargo run.. to get only events under the "sumcheck" span
let filter = EnvFilter::from_default_env();

let subscriber = Registry::default()
.with(
fmt::layer()
.compact()
.with_thread_ids(false)
.with_thread_names(false),
)
.with(EnvFilter::from_default_env())
.with(fmt_layer)
.with(filter)
.with(flame_layer.with_threads_collapsed(true));
tracing::subscriber::set_global_default(subscriber).unwrap();

let top_level = entered_span!("TOPLEVEL");

let keygen = entered_span!("KEYGEN");

// keygen
let pcs_param = Pcs::setup(1 << MAX_NUM_VARIABLES).expect("Basefold PCS setup");
let (pp, vp) = Pcs::trim(pcs_param, 1 << MAX_NUM_VARIABLES).expect("Basefold trim");
Expand All @@ -118,6 +131,8 @@ fn main() {
&program,
);

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

let reg_init = initial_registers();

config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces);
Expand All @@ -129,6 +144,7 @@ fn main() {
.expect("keygen failed");
let vk = pk.get_vk();

exit_span!(keygen);
// proving
let prover = ZKVMProver::new(pk);
let verifier = ZKVMVerifier::new(vk);
Expand Down Expand Up @@ -238,6 +254,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 All @@ -248,14 +274,15 @@ fn main() {
let timer = Instant::now();

let transcript = Transcript::new(b"riscv");

let mut zkvm_proof = prover
.create_proof(zkvm_witness, pi, transcript)
.expect("create_proof failed");

println!(
"riscv_opcodes::create_proof, instance_num_vars = {}, time = {}",
instance_num_vars,
timer.elapsed().as_secs_f64()
timer.elapsed().as_secs()
);

let transcript = Transcript::new(b"riscv");
Expand Down Expand Up @@ -300,4 +327,5 @@ fn main() {
}
};
}
exit_span!(top_level);
}
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");
}
8 changes: 4 additions & 4 deletions ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ceno_emul::Addr;
use itertools::Itertools;
use std::{collections::HashMap, marker::PhantomData};
use itertools::{Itertools, chain};
use std::{collections::HashMap, iter::once, marker::PhantomData};

use ff_ext::ExtensionField;
use mpcs::PolynomialCommitmentScheme;
Expand All @@ -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 Expand Up @@ -49,7 +49,7 @@ impl NameSpace {
let mut name = String::new();

let mut needs_separation = false;
for ns in ns.iter().chain(Some(&this).into_iter()) {
for ns in chain!(ns, once(&this)) {
if needs_separation {
name += "/";
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<E: ExtensionField> ZKVMConstraintSystem<E> {
) -> Result<ZKVMProvingKey<E, PCS>, ZKVMError> {
let mut vm_pk = ZKVMProvingKey::new(pp, vp);

for (c_name, cs) in self.circuit_css.into_iter() {
for (c_name, cs) in self.circuit_css {
// fixed_traces is optional
// verifier will check it existent if cs.num_fixed > 0
let fixed_traces = if cs.num_fixed > 0 {
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 3a78b11

Please sign in to comment.