Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit dfc85dc
Author: Mihai <[email protected]>
Date:   Wed Nov 13 15:30:17 2024 +0200

    clean experimental code

commit 618965a
Merge: a21a628 82af85a
Author: Mihai <[email protected]>
Date:   Wed Nov 13 15:27:20 2024 +0200

    merge master

commit a21a628
Author: Mihai <[email protected]>
Date:   Wed Nov 13 13:47:25 2024 +0200

    add stats.rs

commit deb0bd3
Author: Mihai <[email protected]>
Date:   Wed Nov 13 13:45:52 2024 +0200

    stash

commit c200f2f
Author: Mihai <[email protected]>
Date:   Tue Nov 12 17:15:45 2024 +0200

    stash

commit fc45251
Author: Mihai <[email protected]>
Date:   Tue Nov 12 16:21:39 2024 +0200

    stash

commit e2408b3
Author: Mihai <[email protected]>
Date:   Tue Nov 12 16:02:37 2024 +0200

    more stash

commit 5cccc29
Author: Mihai <[email protected]>
Date:   Tue Nov 12 12:32:55 2024 +0200

    stash

commit 4129e52
Author: Mihai <[email protected]>
Date:   Tue Nov 12 10:32:28 2024 +0200

    add ConstraintStats

commit 82af85a
Author: mcalancea <[email protected]>
Date:   Tue Nov 12 09:35:07 2024 +0200

    Extend profiling using `tracing` (#572)

    Improve profiling efforts by:
    - refactoring tracing spans
    - addressing a pitfall regarding spawned threads
    - changing some subscriber configs

commit 54c8114
Author: Matthias Görgens <[email protected]>
Date:   Tue Nov 12 07:34:18 2024 +0700

    Remove some redundant `.into_iter()` (#581)

    Just a minor clean-up while I'm reading through our code.

commit 85f8dd8
Author: Mihai <[email protected]>
Date:   Mon Nov 11 18:43:12 2024 +0200

    stash

commit 4acd8c9
Author: Matthias Görgens <[email protected]>
Date:   Mon Nov 11 16:28:43 2024 +0700

    Remove unused file (#580)

    This finishes work done in #201

commit 311d79e
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Nov 11 08:12:12 2024 +0000

    Bump tempfile from 3.13.0 to 3.14.0 (#578)

commit 0389112
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Nov 11 08:11:57 2024 +0000

    Bump anyhow from 1.0.92 to 1.0.93 (#579)

commit 853f1ba
Author: Cyte Zhang <[email protected]>
Date:   Mon Nov 11 15:55:49 2024 +0800

    Change BaseFold trim API to consume pp & truncate public parameters directly in `trim` (#248)

    Considering that BaseFold trim is almost always used only once in each
    program execution, consume the input public parameter instead of clone
    it to save memory.

    This API change also allows the `trim` function to directly truncate the
    input public parameters.

commit 8e00028
Author: naure <[email protected]>
Date:   Fri Nov 8 14:32:07 2024 +0100

    Dummy Circuit - Basic ECALL (#369)

    _Issue #359 and #567_

    * The `DummyInstruction` implements all the communications of a step:
    state, fetch, registers, memory. But it does not verify calculations:
    any value can be written out.

    * Placeholder circuits for missing implementations, including unknown
    ecalls.

    * More precise register op assignment, see #570.

    ---------

    Co-authored-by: Aurélien Nicolas <[email protected]>

commit a060e15
Author: Matthias Görgens <[email protected]>
Date:   Fri Nov 8 18:33:25 2024 +0800

    Remove unimplmented and unused functions (#576)
  • Loading branch information
mcalancea committed Nov 13, 2024
1 parent 22df0e4 commit 8ae6401
Show file tree
Hide file tree
Showing 43 changed files with 852 additions and 575 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion ceno_emul/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod vm_state;
pub use vm_state::VMState;

mod rv32im;
pub use rv32im::{DecodedInstruction, EmuContext, InsnCodes, InsnFormat, InsnKind};
pub use rv32im::{DecodedInstruction, EmuContext, InsnCategory, InsnCodes, InsnFormat, InsnKind};

mod elf;
pub use elf::Program;
Expand Down
3 changes: 3 additions & 0 deletions ceno_emul/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ pub struct Platform {
pub rom_end: Addr,
pub ram_start: Addr,
pub ram_end: Addr,
/// If true, ecall instructions are no-op instead of trap. Testing only.
pub unsafe_ecall_nop: bool,
}

pub const CENO_PLATFORM: Platform = Platform {
rom_start: 0x2000_0000,
rom_end: 0x3000_0000 - 1,
ram_start: 0x8000_0000,
ram_end: 0xFFFF_FFFF,
unsafe_ecall_nop: false,
};

impl Platform {
Expand Down
4 changes: 2 additions & 2 deletions ceno_emul/src/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub struct DecodedInstruction {
}

#[derive(Clone, Copy, Debug)]
enum InsnCategory {
pub enum InsnCategory {
Compute,
Branch,
Load,
Expand Down Expand Up @@ -196,7 +196,7 @@ impl InsnKind {
pub struct InsnCodes {
pub format: InsnFormat,
pub kind: InsnKind,
category: InsnCategory,
pub category: InsnCategory,
pub(crate) opcode: u32,
pub(crate) func3: u32,
pub(crate) func7: u32,
Expand Down
25 changes: 24 additions & 1 deletion ceno_emul/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{collections::HashMap, fmt, mem};

use crate::{
CENO_PLATFORM, PC_STEP_SIZE,
CENO_PLATFORM, InsnKind, PC_STEP_SIZE,
addr::{ByteAddr, Cycle, RegIdx, Word, WordAddr},
encode_rv32,
rv32im::DecodedInstruction,
};

Expand Down Expand Up @@ -187,6 +188,28 @@ impl StepRecord {
)
}

/// Create a test record for an ECALL instruction that can do anything.
pub fn new_ecall_any(cycle: Cycle, pc: ByteAddr) -> StepRecord {
let value = 1234;
Self::new_insn(
cycle,
Change::new(pc, pc + PC_STEP_SIZE),
encode_rv32(InsnKind::EANY, 0, 0, 0, 0),
Some(value),
Some(value),
Some(Change::new(value, value)),
Some(WriteOp {
addr: CENO_PLATFORM.ram_start().into(),
value: Change {
before: value,
after: value,
},
previous_cycle: 0,
}),
0,
)
}

#[allow(clippy::too_many_arguments)]
fn new_insn(
cycle: Cycle,
Expand Down
15 changes: 12 additions & 3 deletions ceno_emul/src/vm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use super::rv32im::EmuContext;
use crate::{
Program,
PC_STEP_SIZE, Program,
addr::{ByteAddr, RegIdx, Word, WordAddr},
platform::Platform,
rv32im::{DecodedInstruction, Emulator, TrapCause},
Expand Down Expand Up @@ -117,12 +117,21 @@ impl EmuContext for VMState {
// Expect an ecall to terminate the program: function HALT with argument exit_code.
fn ecall(&mut self) -> Result<bool> {
let function = self.load_register(self.platform.reg_ecall())?;
let arg0 = self.load_register(self.platform.reg_arg0())?;
if function == self.platform.ecall_halt() {
let exit_code = self.load_register(self.platform.reg_arg0())?;
tracing::debug!("halt with exit_code={}", exit_code);
tracing::debug!("halt with exit_code={}", arg0);

self.halt();
Ok(true)
} else if self.platform.unsafe_ecall_nop {
// Treat unknown ecalls as all powerful instructions:
// Read two registers, write one register, write one memory word, and branch.
tracing::warn!("ecall ignored: syscall_id={}", function);
self.store_register(DecodedInstruction::RD_NULL as RegIdx, 0)?;
let addr = self.platform.ram_start().into();
self.store_memory(addr, self.peek_memory(addr))?;
self.set_pc(ByteAddr(self.pc) + PC_STEP_SIZE);
Ok(true)
} else {
self.trap(TrapCause::EcallError)
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tracing-subscriber.workspace = true
clap = { version = "4.5", features = ["derive"] }
generic_static = "0.2"
rand.workspace = true
tempfile = "3.13"
tempfile = "3.14"
thread_local = "1.1"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/benches/riscv_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn bench_add(c: &mut Criterion) {
zkvm_fixed_traces.register_opcode_circuit::<AddInstruction<E>>(&zkvm_cs);

let param = Pcs::setup(1 << MAX_NUM_VARIABLES).unwrap();
let (pp, vp) = Pcs::trim(&param, 1 << MAX_NUM_VARIABLES).unwrap();
let (pp, vp) = Pcs::trim(param, 1 << MAX_NUM_VARIABLES).unwrap();

let pk = zkvm_cs
.clone()
Expand Down
52 changes: 40 additions & 12 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::{collections::BTreeMap, panic, time::Instant};

use ceno_zkvm::{
declare_program,
Expand All @@ -19,16 +19,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 @@ -92,23 +93,36 @@ 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");
let (pp, vp) = Pcs::trim(pcs_param, 1 << MAX_NUM_VARIABLES).expect("Basefold trim");
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 @@ -120,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 All @@ -138,6 +154,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 @@ -274,6 +291,15 @@ 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");

MockProver::assert_satisfied_full(
zkvm_cs.clone(),
zkvm_fixed_traces.clone(),
Expand All @@ -284,14 +310,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 @@ -336,4 +363,5 @@ fn main() {
}
};
}
exit_span!(top_level);
}
10 changes: 5 additions & 5 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 @@ -10,12 +10,12 @@ use crate::{
chip_handler::utils::rlc_chip_record,
error::ZKVMError,
expression::{Expression, Fixed, Instance, WitIn},
structs::{ProvingKey, RAMType, VerifyingKey, WitnessId},
structs::{ProvingKey, RAMType, VerifyingKey, WitnessId, ZKVMConstraintSystem},
witness::RowMajorMatrix,
};

/// namespace used for annotation, preserve meta info during circuit construction
#[derive(Clone, Debug)]
#[derive(Clone, Debug, 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
1 change: 1 addition & 0 deletions ceno_zkvm/src/instructions/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod branch;
pub mod config;
pub mod constants;
pub mod divu;
pub mod dummy;
pub mod ecall;
pub mod jump;
pub mod logic;
Expand Down
19 changes: 19 additions & 0 deletions ceno_zkvm/src/instructions/riscv/divu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ff_ext::ExtensionField;
use super::{
RIVInstruction,
constants::{UINT_LIMBS, UInt},
dummy::DummyInstruction,
r_insn::RInstructionConfig,
};
use crate::{
Expand Down Expand Up @@ -33,12 +34,30 @@ pub struct ArithConfig<E: ExtensionField> {

pub struct ArithInstruction<E, I>(PhantomData<(E, I)>);

pub struct DivOp;
impl RIVInstruction for DivOp {
const INST_KIND: InsnKind = InsnKind::DIV;
}
pub type DivDummy<E> = DummyInstruction<E, DivOp>; // TODO: implement DivInstruction.

pub struct DivUOp;
impl RIVInstruction for DivUOp {
const INST_KIND: InsnKind = InsnKind::DIVU;
}
pub type DivUInstruction<E> = ArithInstruction<E, DivUOp>;

pub struct RemOp;
impl RIVInstruction for RemOp {
const INST_KIND: InsnKind = InsnKind::REM;
}
pub type RemDummy<E> = DummyInstruction<E, RemOp>; // TODO: implement RemInstruction.

pub struct RemuOp;
impl RIVInstruction for RemuOp {
const INST_KIND: InsnKind = InsnKind::REMU;
}
pub type RemuDummy<E> = DummyInstruction<E, RemuOp>; // TODO: implement RemuInstruction.

impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E, I> {
type InstructionConfig = ArithConfig<E>;

Expand Down
Loading

0 comments on commit 8ae6401

Please sign in to comment.