Skip to content

Commit

Permalink
Merge branch 'master' into bg/div-rem-remu-opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Gillespie committed Dec 11, 2024
2 parents 4425c13 + a879b84 commit 0158e01
Show file tree
Hide file tree
Showing 39 changed files with 746 additions and 453 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ jobs:
- name: Run example
env:
RAYON_NUM_THREADS: 2
run: cargo run --release --package ceno_zkvm --example riscv_opcodes --target ${{ matrix.target }} -- --start 10 --end 11
RUSTFLAGS: "-C opt-level=3"
run: cargo run --package ceno_zkvm --example riscv_opcodes --target ${{ matrix.target }} -- --start 10 --end 11

- name: Run fibonacci
env:
RAYON_NUM_THREADS: 8
RUST_LOG: debug
run: cargo run --release --package ceno_zkvm --bin e2e --target ${{ matrix.target }} -- --platform=sp1 ceno_zkvm/examples/fibonacci.elf
RUSTFLAGS: "-C opt-level=3"
run: cargo run --package ceno_zkvm --bin e2e --target ${{ matrix.target }} -- --platform=sp1 ceno_zkvm/examples/fibonacci.elf
2 changes: 1 addition & 1 deletion .github/workflows/lints.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
run: |
cargo make --version || cargo install cargo-make
- name: Check code format
run: cargo make fmt-all-check
run: cargo fmt --all --check

- name: Run clippy
env:
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ tracing = { version = "0.1", features = [
tracing-forest = { version = "0.1.6" }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[profile.dev]
lto = "thin"

[profile.release]
lto = "thin"
15 changes: 0 additions & 15 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,11 @@ args = [
command = "cargo"
workspace = false

[tasks.fmt-all-check]
args = ["fmt", "--all", "--", "--check"]
command = "cargo"
workspace = false

[tasks.fmt-all]
args = ["fmt", "--all"]
command = "cargo"
workspace = false

[tasks.clippy-all]
args = ["clippy", "--all-features", "--all-targets", "--", "-D", "warnings"]
command = "cargo"
workspace = false

[tasks.fmt]
args = ["fmt", "-p", "ceno_zkvm", "--", "--check"]
command = "cargo"
workspace = false

[tasks.riscv_stats]
args = ["run", "--bin", "riscv_stats"]
command = "cargo"
Expand Down
5 changes: 2 additions & 3 deletions ceno_emul/src/vm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ impl VMState {
/// 32 architectural registers + 1 register RD_NULL for dark writes to x0.
pub const REG_COUNT: usize = 32 + 1;

pub fn new(platform: Platform, program: Program) -> Self {
pub fn new(platform: Platform, program: Arc<Program>) -> Self {
let pc = program.entry;
let program = Arc::new(program);

let mut vm = Self {
pc,
Expand All @@ -52,7 +51,7 @@ impl VMState {
}

pub fn new_from_elf(platform: Platform, elf: &[u8]) -> Result<Self> {
let program = Program::load_elf(elf, u32::MAX)?;
let program = Arc::new(Program::load_elf(elf, u32::MAX)?);
Ok(Self::new(platform, program))
}

Expand Down
9 changes: 6 additions & 3 deletions ceno_emul/tests/test_vm_trace.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(clippy::unusual_byte_groupings)]
use anyhow::Result;
use std::collections::{BTreeMap, HashMap};
use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};

use ceno_emul::{
CENO_PLATFORM, Cycle, EmuContext, InsnKind, Platform, Program, StepRecord, Tracer, VMState,
Expand All @@ -24,7 +27,7 @@ fn test_vm_trace() -> Result<()> {
})
.collect(),
);
let mut ctx = VMState::new(CENO_PLATFORM, program);
let mut ctx = VMState::new(CENO_PLATFORM, Arc::new(program));

let steps = run(&mut ctx)?;

Expand Down Expand Up @@ -52,7 +55,7 @@ fn test_empty_program() -> Result<()> {
vec![],
BTreeMap::new(),
);
let mut ctx = VMState::new(CENO_PLATFORM, empty_program);
let mut ctx = VMState::new(CENO_PLATFORM, Arc::new(empty_program));
let res = run(&mut ctx);
assert!(matches!(res, Err(e) if e.to_string().contains("InstructionAccessFault")),);
Ok(())
Expand Down
4 changes: 4 additions & 0 deletions ceno_zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ name = "riscv_add"
[[bench]]
harness = false
name = "fibonacci"

[[bench]]
harness = false
name = "fibonacci_witness"
31 changes: 19 additions & 12 deletions ceno_zkvm/benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@ use std::{
use ceno_emul::{CENO_PLATFORM, Platform, Program, WORD_SIZE};
use ceno_zkvm::{
self,
e2e::{run_e2e_gen_witness, run_e2e_proof},
e2e::{Checkpoint, run_e2e_with_checkpoint},
};
use criterion::*;

use goldilocks::GoldilocksExt2;
use mpcs::BasefoldDefault;

criterion_group! {
name = fibonacci;
name = fibonacci_prove_group;
config = Criterion::default().warm_up_time(Duration::from_millis(20000));
targets = bench_e2e
targets = fibonacci_prove,
}

criterion_main!(fibonacci);
criterion_main!(fibonacci_prove_group);

const NUM_SAMPLES: usize = 10;

fn bench_e2e(c: &mut Criterion) {
type Pcs = BasefoldDefault<E>;
type Pcs = BasefoldDefault<E>;
type E = GoldilocksExt2;

// Relevant init data for fibonacci run
fn setup() -> (Program, Platform, u32, u32) {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("examples/fibonacci.elf");
let stack_size = 32768;
let heap_size = 2097152;
let elf_bytes = fs::read(&file_path).expect("read elf file");
let program = Program::load_elf(&elf_bytes, u32::MAX).unwrap();

// use sp1 platform
let platform = Platform {
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
stack_top: 0x0020_0400,
Expand All @@ -44,6 +46,11 @@ fn bench_e2e(c: &mut Criterion) {
..CENO_PLATFORM
};

(program, platform, stack_size, heap_size)
}

fn fibonacci_prove(c: &mut Criterion) {
let (program, platform, stack_size, heap_size) = setup();
for max_steps in [1usize << 20, 1usize << 21, 1usize << 22] {
// expand more input size once runtime is acceptable
let mut group = c.benchmark_group(format!("fibonacci_max_steps_{}", max_steps));
Expand All @@ -58,18 +65,20 @@ fn bench_e2e(c: &mut Criterion) {
|b| {
b.iter_with_setup(
|| {
run_e2e_gen_witness::<E, Pcs>(
run_e2e_with_checkpoint::<E, Pcs>(
program.clone(),
platform.clone(),
stack_size,
heap_size,
vec![],
max_steps,
Checkpoint::PrepE2EProving,
)
},
|(prover, _, zkvm_witness, pi, _, _, _)| {
|(_, run_e2e_proof)| {
let timer = Instant::now();
let _ = run_e2e_proof(prover, zkvm_witness, pi);

run_e2e_proof();
println!(
"Fibonacci::create_proof, max_steps = {}, time = {}",
max_steps,
Expand All @@ -82,6 +91,4 @@ fn bench_e2e(c: &mut Criterion) {

group.finish();
}

type E = GoldilocksExt2;
}
83 changes: 83 additions & 0 deletions ceno_zkvm/benches/fibonacci_witness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{fs, path::PathBuf, time::Duration};

use ceno_emul::{CENO_PLATFORM, Platform, Program, WORD_SIZE};
use ceno_zkvm::{
self,
e2e::{Checkpoint, run_e2e_with_checkpoint},
};
use criterion::*;

use goldilocks::GoldilocksExt2;
use mpcs::BasefoldDefault;

criterion_group! {
name = fibonacci;
config = Criterion::default().warm_up_time(Duration::from_millis(20000));
targets = fibonacci_witness
}

criterion_main!(fibonacci);

const NUM_SAMPLES: usize = 10;
type Pcs = BasefoldDefault<E>;
type E = GoldilocksExt2;

// Relevant init data for fibonacci run
fn setup() -> (Program, Platform, u32, u32) {
let mut file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
file_path.push("examples/fibonacci.elf");
let stack_size = 32768;
let heap_size = 2097152;
let elf_bytes = fs::read(&file_path).expect("read elf file");
let program = Program::load_elf(&elf_bytes, u32::MAX).unwrap();

let platform = Platform {
// The stack section is not mentioned in ELF headers, so we repeat the constant STACK_TOP here.
stack_top: 0x0020_0400,
rom: program.base_address
..program.base_address + (program.instructions.len() * WORD_SIZE) as u32,
ram: 0x0010_0000..0xFFFF_0000,
unsafe_ecall_nop: true,
..CENO_PLATFORM
};

(program, platform, stack_size, heap_size)
}

fn fibonacci_witness(c: &mut Criterion) {
let (program, platform, stack_size, heap_size) = setup();

let max_steps = usize::MAX;
let mut group = c.benchmark_group(format!("fib_wit_max_steps_{}", max_steps));
group.sample_size(NUM_SAMPLES);

// Benchmark the proving time
group.bench_function(
BenchmarkId::new(
"fibonacci_witness",
format!("fib_wit_max_steps_{}", max_steps),
),
|b| {
b.iter_with_setup(
|| {
run_e2e_with_checkpoint::<E, Pcs>(
program.clone(),
platform.clone(),
stack_size,
heap_size,
vec![],
max_steps,
Checkpoint::PrepWitnessGen,
)
},
|(_, generate_witness)| {
generate_witness();
},
);
},
);

group.finish();

type E = GoldilocksExt2;
}
4 changes: 2 additions & 2 deletions ceno_zkvm/benches/riscv_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use goldilocks::{Goldilocks, GoldilocksExt2};
use itertools::Itertools;
use mpcs::{BasefoldDefault, PolynomialCommitmentScheme};
use multilinear_extensions::mle::IntoMLE;
use transcript::Transcript;
use transcript::{BasicTranscript, Transcript};

cfg_if::cfg_if! {
if #[cfg(feature = "flamegraph")] {
Expand Down Expand Up @@ -87,7 +87,7 @@ fn bench_add(c: &mut Criterion) {
|wits_in| {
let timer = Instant::now();
let num_instances = 1 << instance_num_vars;
let mut transcript = Transcript::new(b"riscv");
let mut transcript = BasicTranscript::new(b"riscv");
let commit =
Pcs::batch_commit_and_write(&prover.pk.pp, &wits_in, &mut transcript)
.unwrap();
Expand Down
13 changes: 4 additions & 9 deletions ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{panic, time::Instant};
use std::{panic, sync::Arc, time::Instant};

use ceno_zkvm::{
declare_program,
Expand Down Expand Up @@ -27,7 +27,7 @@ use itertools::Itertools;
use mpcs::{Basefold, BasefoldRSParams, PolynomialCommitmentScheme};
use sumcheck::macros::{entered_span, exit_span};
use tracing_subscriber::{EnvFilter, Registry, fmt, fmt::format::FmtSpan, layer::SubscriberExt};
use transcript::Transcript;
use transcript::BasicTranscript as Transcript;
const PROGRAM_SIZE: usize = 16;
// For now, we assume registers
// - x0 is not touched,
Expand Down Expand Up @@ -177,7 +177,7 @@ fn main() {
// init vm.x1 = 1, vm.x2 = -1, vm.x3 = step_loop
let public_io_init = init_public_io(&[1, u32::MAX, step_loop]);

let mut vm = VMState::new(CENO_PLATFORM, program.clone());
let mut vm = VMState::new(CENO_PLATFORM, Arc::new(program.clone()));

// init memory mapped IO
for record in &public_io_init {
Expand Down Expand Up @@ -290,12 +290,7 @@ fn main() {
trace_report.save_json("report.json");
trace_report.save_table("report.txt");

MockProver::assert_satisfied_full(
zkvm_cs.clone(),
zkvm_fixed_traces.clone(),
&zkvm_witness,
&pi,
);
MockProver::assert_satisfied_full(&zkvm_cs, zkvm_fixed_traces.clone(), &zkvm_witness, &pi);

let timer = Instant::now();

Expand Down
Loading

0 comments on commit 0158e01

Please sign in to comment.