Skip to content

Commit

Permalink
Merge branch 'feat/guest-example' into feat/guest-example-mem-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Nicolas committed Nov 14, 2024
2 parents e45f67a + 8945aec commit e215a55
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 63 deletions.
23 changes: 8 additions & 15 deletions ceno_zkvm/examples/fibonacci_elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ceno_emul::{
WORD_SIZE, WordAddr,
};
use ceno_zkvm::{
instructions::riscv::{DummyExtraConfig, Rv32imConfig},
instructions::riscv::{DummyExtraConfig, MmuConfig, Rv32imConfig},
scheme::{
PublicValues, constants::MAX_NUM_VARIABLES, mock_prover::MockProver, prover::ZKVMProver,
verifier::ZKVMVerifier,
Expand Down Expand Up @@ -65,6 +65,7 @@ fn main() {
let mut zkvm_cs = ZKVMConstraintSystem::default();

let config = Rv32imConfig::<E>::construct_circuits(&mut zkvm_cs);
let mmu_config = MmuConfig::<E>::construct_circuits(&mut zkvm_cs);
let dummy_config = DummyExtraConfig::<E>::construct_circuits(&mut zkvm_cs);
let prog_config = zkvm_cs.register_table_circuit::<ExampleProgramTableCircuit<E>>();
zkvm_cs.register_global_state::<GlobalState>();
Expand Down Expand Up @@ -95,12 +96,8 @@ fn main() {
};

let reg_init = initial_registers();
config.generate_fixed_traces(
&zkvm_cs,
&mut zkvm_fixed_traces,
&reg_init,
&program_data_init,
);
config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, &reg_init);
mmu_config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, &program_data_init);
dummy_config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces);

let pk = zkvm_cs
Expand Down Expand Up @@ -221,14 +218,10 @@ fn main() {

// assign table circuits
config
.assign_table_circuit(
&zkvm_cs,
&mut zkvm_witness,
&reg_final,
&[],
&program_data_final,
&[],
)
.assign_table_circuit(&zkvm_cs, &mut zkvm_witness, &reg_final, &[], &[])
.unwrap();
mmu_config
.assign_table_circuit(&zkvm_cs, &mut zkvm_witness, &program_data_final)
.unwrap();
// assign program circuit
zkvm_witness
Expand Down
30 changes: 4 additions & 26 deletions ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use ceno_zkvm::{
scheme::{mock_prover::MockProver, prover::ZKVMProver},
state::GlobalState,
tables::{
DynVolatileRamTable, MemFinalRecord, MemTable, ProgramTableCircuit, init_program_data,
init_public_io, initial_registers,
DynVolatileRamTable, MemFinalRecord, MemTable, ProgramTableCircuit, init_public_io,
initial_registers,
},
};
use clap::Parser;
Expand Down Expand Up @@ -121,16 +121,8 @@ fn main() {
);

let reg_init = initial_registers();
// Define program constant here
let program_data: &[u32] = &[];
let program_data_init = init_program_data(program_data);

config.generate_fixed_traces(
&zkvm_cs,
&mut zkvm_fixed_traces,
&reg_init,
&program_data_init,
);
config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, &reg_init);

let pk = zkvm_cs
.clone()
Expand All @@ -154,7 +146,7 @@ fn main() {
let mut vm = VMState::new(CENO_PLATFORM, program.clone());

// init mmio
for record in program_data_init.iter().chain(public_io_init.iter()) {
for record in &public_io_init {
vm.init_memory(record.addr.into(), record.value);
}

Expand Down Expand Up @@ -215,19 +207,6 @@ fn main() {
})
.collect_vec();

// Find the final program_data cycles.
let program_data_final = program_data_init
.iter()
.map(|rec| {
let vma: WordAddr = rec.addr.into();
MemFinalRecord {
addr: rec.addr,
value: rec.value,
cycle: *final_access.get(&vma).unwrap_or(&0),
}
})
.collect_vec();

// Find the final public io cycles.
let public_io_final = public_io_init
.iter()
Expand Down Expand Up @@ -264,7 +243,6 @@ fn main() {
&mut zkvm_witness,
&reg_final,
&mem_final,
&program_data_final,
&public_io_final,
)
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ceno_emul::InsnKind;

mod rv32im;
pub use rv32im::{DummyExtraConfig, Rv32imConfig};
pub use rv32im::{DummyExtraConfig, Rv32imConfig, mmu::MmuConfig};

pub mod arith;
pub mod arith_imm;
Expand Down
26 changes: 5 additions & 21 deletions ceno_zkvm/src/instructions/riscv/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use crate::{
structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses},
tables::{
AndTableCircuit, LtuTableCircuit, MemCircuit, MemFinalRecord, MemInitRecord,
OrTableCircuit, PowTableCircuit, ProgramDataCircuit, PubIOCircuit, RegTableCircuit,
TableCircuit, U5TableCircuit, U8TableCircuit, U14TableCircuit, U16TableCircuit,
XorTableCircuit,
OrTableCircuit, PowTableCircuit, PubIOCircuit, RegTableCircuit, TableCircuit,
U5TableCircuit, U8TableCircuit, U14TableCircuit, U16TableCircuit, XorTableCircuit,
},
};
use ceno_emul::{CENO_PLATFORM, InsnKind, InsnKind::*, StepRecord};
Expand All @@ -46,6 +45,8 @@ use super::{
memory::LwInstruction,
};

pub mod mmu;

pub struct Rv32imConfig<E: ExtensionField> {
// ALU Opcodes.
pub add_config: <AddInstruction<E> as Instruction<E>>::InstructionConfig,
Expand Down Expand Up @@ -115,7 +116,6 @@ pub struct Rv32imConfig<E: ExtensionField> {
// RW tables.
pub reg_config: <RegTableCircuit<E> as TableCircuit<E>>::TableConfig,
// pub mem_config: <MemCircuit<E> as TableCircuit<E>>::TableConfig,
pub program_data_config: <ProgramDataCircuit<E> as TableCircuit<E>>::TableConfig,
// pub public_io_config: <PubIOCircuit<E> as TableCircuit<E>>::TableConfig,
}

Expand Down Expand Up @@ -192,7 +192,6 @@ impl<E: ExtensionField> Rv32imConfig<E> {
// let mem_config = cs.register_table_circuit::<MemCircuit<E>>();

// RO tables
let program_data_config = cs.register_table_circuit::<ProgramDataCircuit<E>>();
// let public_io_config = cs.register_table_circuit::<PubIOCircuit<E>>();

Self {
Expand Down Expand Up @@ -258,7 +257,6 @@ impl<E: ExtensionField> Rv32imConfig<E> {

reg_config,
// mem_config,
program_data_config,
// public_io_config,
}
}
Expand All @@ -268,7 +266,6 @@ impl<E: ExtensionField> Rv32imConfig<E> {
cs: &ZKVMConstraintSystem<E>,
fixed: &mut ZKVMFixedTraces<E>,
reg_init: &[MemInitRecord],
program_data_init: &[MemInitRecord],
) {
// alu
fixed.register_opcode_circuit::<AddInstruction<E>>(cs);
Expand Down Expand Up @@ -331,11 +328,7 @@ impl<E: ExtensionField> Rv32imConfig<E> {
fixed.register_table_circuit::<PowTableCircuit<E>>(cs, &self.pow_config, &());

fixed.register_table_circuit::<RegTableCircuit<E>>(cs, &self.reg_config, reg_init);
fixed.register_table_circuit::<ProgramDataCircuit<E>>(
cs,
&self.program_data_config,
program_data_init,
);

// fixed.register_table_circuit::<PubIOCircuit<E>>(cs, &self.public_io_config, &());
}

Expand Down Expand Up @@ -452,7 +445,6 @@ impl<E: ExtensionField> Rv32imConfig<E> {
witness: &mut ZKVMWitnesses<E>,
reg_final: &[MemFinalRecord],
mem_final: &[MemFinalRecord],
program_data_final: &[MemFinalRecord],
public_io_final: &[MemFinalRecord],
) -> Result<(), ZKVMError> {
witness.assign_table_circuit::<U16TableCircuit<E>>(cs, &self.u16_range_config, &())?;
Expand All @@ -473,14 +465,6 @@ impl<E: ExtensionField> Rv32imConfig<E> {
// witness
// .assign_table_circuit::<MemCircuit<E>>(cs, &self.mem_config, mem_final)
// .unwrap();
// assign program_data finalization.
witness
.assign_table_circuit::<ProgramDataCircuit<E>>(
cs,
&self.program_data_config,
program_data_final,
)
.unwrap();

// witness
// .assign_table_circuit::<PubIOCircuit<E>>(cs, &self.public_io_config, public_io_final)
Expand Down
46 changes: 46 additions & 0 deletions ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use ff_ext::ExtensionField;

use crate::{
error::ZKVMError,
structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses},
tables::{MemFinalRecord, MemInitRecord, ProgramDataCircuit, TableCircuit},
};

pub struct MmuConfig<E: ExtensionField> {
pub program_data_config: <ProgramDataCircuit<E> as TableCircuit<E>>::TableConfig,
}

impl<E: ExtensionField> MmuConfig<E> {
pub fn construct_circuits(cs: &mut ZKVMConstraintSystem<E>) -> Self {
let program_data_config = cs.register_table_circuit::<ProgramDataCircuit<E>>();
Self {
program_data_config,
}
}

pub fn generate_fixed_traces(
&self,
cs: &ZKVMConstraintSystem<E>,
fixed: &mut ZKVMFixedTraces<E>,
program_data_init: &[MemInitRecord],
) {
fixed.register_table_circuit::<ProgramDataCircuit<E>>(
cs,
&self.program_data_config,
program_data_init,
);
}

pub fn assign_table_circuit(
&self,
cs: &ZKVMConstraintSystem<E>,
witness: &mut ZKVMWitnesses<E>,
program_data_final: &[MemFinalRecord],
) -> Result<(), ZKVMError> {
witness.assign_table_circuit::<ProgramDataCircuit<E>>(
cs,
&self.program_data_config,
program_data_final,
)
}
}

0 comments on commit e215a55

Please sign in to comment.