diff --git a/ceno_zkvm/examples/fibonacci_elf.rs b/ceno_zkvm/examples/fibonacci_elf.rs index f03696eef..d8e20ee2c 100644 --- a/ceno_zkvm/examples/fibonacci_elf.rs +++ b/ceno_zkvm/examples/fibonacci_elf.rs @@ -96,8 +96,13 @@ fn main() { }; let reg_init = initial_registers(); - config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, ®_init); - mmu_config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, &program_data_init); + config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces); + mmu_config.generate_fixed_traces( + &zkvm_cs, + &mut zkvm_fixed_traces, + ®_init, + &program_data_init, + ); dummy_config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces); let pk = zkvm_cs @@ -218,10 +223,16 @@ fn main() { // assign table circuits config - .assign_table_circuit(&zkvm_cs, &mut zkvm_witness, ®_final, &[]) + .assign_table_circuit(&zkvm_cs, &mut zkvm_witness) .unwrap(); mmu_config - .assign_table_circuit(&zkvm_cs, &mut zkvm_witness, &program_data_final) + .assign_table_circuit( + &zkvm_cs, + &mut zkvm_witness, + ®_final, + &program_data_final, + &[], + ) .unwrap(); // assign program circuit zkvm_witness diff --git a/ceno_zkvm/examples/riscv_opcodes.rs b/ceno_zkvm/examples/riscv_opcodes.rs index 8e3703984..89cfaa8d0 100644 --- a/ceno_zkvm/examples/riscv_opcodes.rs +++ b/ceno_zkvm/examples/riscv_opcodes.rs @@ -2,7 +2,7 @@ use std::{panic, time::Instant}; use ceno_zkvm::{ declare_program, - instructions::riscv::{Rv32imConfig, constants::EXIT_PC}, + instructions::riscv::{MmuConfig, Rv32imConfig, constants::EXIT_PC}, scheme::{mock_prover::MockProver, prover::ZKVMProver}, state::GlobalState, tables::{MemFinalRecord, ProgramTableCircuit, init_public_io, initial_registers}, @@ -106,6 +106,7 @@ fn main() { let mut zkvm_cs = ZKVMConstraintSystem::default(); let config = Rv32imConfig::::construct_circuits(&mut zkvm_cs); + let mmu_config = MmuConfig::::construct_circuits(&mut zkvm_cs); let prog_config = zkvm_cs.register_table_circuit::>(); zkvm_cs.register_global_state::(); @@ -119,7 +120,8 @@ fn main() { let reg_init = initial_registers(); - config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, ®_init); + config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces); + mmu_config.generate_fixed_traces(&zkvm_cs, &mut zkvm_fixed_traces, ®_init, &[]); let pk = zkvm_cs .clone() @@ -219,7 +221,16 @@ fn main() { // assign table circuits config - .assign_table_circuit(&zkvm_cs, &mut zkvm_witness, ®_final, &public_io_final) + .assign_table_circuit(&zkvm_cs, &mut zkvm_witness) + .unwrap(); + mmu_config + .assign_table_circuit( + &zkvm_cs, + &mut zkvm_witness, + ®_final, + &[], + &public_io_final, + ) .unwrap(); // assign program circuit diff --git a/ceno_zkvm/src/instructions/riscv/rv32im.rs b/ceno_zkvm/src/instructions/riscv/rv32im.rs index 24d9c4fa4..4ba008a09 100644 --- a/ceno_zkvm/src/instructions/riscv/rv32im.rs +++ b/ceno_zkvm/src/instructions/riscv/rv32im.rs @@ -20,9 +20,8 @@ use crate::{ }, structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses}, tables::{ - AndTableCircuit, LtuTableCircuit, MemFinalRecord, MemInitRecord, OrTableCircuit, - PowTableCircuit, PubIOCircuit, RegTableCircuit, TableCircuit, U5TableCircuit, - U8TableCircuit, U14TableCircuit, U16TableCircuit, XorTableCircuit, + AndTableCircuit, LtuTableCircuit, OrTableCircuit, PowTableCircuit, TableCircuit, + U5TableCircuit, U8TableCircuit, U14TableCircuit, U16TableCircuit, XorTableCircuit, }, }; use ceno_emul::{CENO_PLATFORM, InsnKind, InsnKind::*, StepRecord}; @@ -112,10 +111,6 @@ pub struct Rv32imConfig { pub xor_table_config: as TableCircuit>::TableConfig, pub ltu_config: as TableCircuit>::TableConfig, pub pow_config: as TableCircuit>::TableConfig, - - // RW tables. - pub reg_config: as TableCircuit>::TableConfig, - pub public_io_config: as TableCircuit>::TableConfig, } impl Rv32imConfig { @@ -186,12 +181,6 @@ impl Rv32imConfig { let ltu_config = cs.register_table_circuit::>(); let pow_config = cs.register_table_circuit::>(); - // RW tables - let reg_config = cs.register_table_circuit::>(); - - // RO tables - let public_io_config = cs.register_table_circuit::>(); - Self { // alu opcodes add_config, @@ -252,9 +241,6 @@ impl Rv32imConfig { xor_table_config, ltu_config, pow_config, - - reg_config, - public_io_config, } } @@ -262,7 +248,6 @@ impl Rv32imConfig { &self, cs: &ZKVMConstraintSystem, fixed: &mut ZKVMFixedTraces, - reg_init: &[MemInitRecord], ) { // alu fixed.register_opcode_circuit::>(cs); @@ -323,10 +308,6 @@ impl Rv32imConfig { fixed.register_table_circuit::>(cs, &self.xor_table_config, &()); fixed.register_table_circuit::>(cs, &self.ltu_config, &()); fixed.register_table_circuit::>(cs, &self.pow_config, &()); - - fixed.register_table_circuit::>(cs, &self.reg_config, reg_init); - - fixed.register_table_circuit::>(cs, &self.public_io_config, &()); } pub fn assign_opcode_circuit( @@ -440,8 +421,6 @@ impl Rv32imConfig { &self, cs: &ZKVMConstraintSystem, witness: &mut ZKVMWitnesses, - reg_final: &[MemFinalRecord], - public_io_final: &[MemFinalRecord], ) -> Result<(), ZKVMError> { witness.assign_table_circuit::>(cs, &self.u16_range_config, &())?; witness.assign_table_circuit::>(cs, &self.u14_range_config, &())?; @@ -453,15 +432,6 @@ impl Rv32imConfig { witness.assign_table_circuit::>(cs, &self.ltu_config, &())?; witness.assign_table_circuit::>(cs, &self.pow_config, &())?; - // assign register finalization. - witness - .assign_table_circuit::>(cs, &self.reg_config, reg_final) - .unwrap(); - - witness - .assign_table_circuit::>(cs, &self.public_io_config, public_io_final) - .unwrap(); - Ok(()) } } diff --git a/ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs b/ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs index d98b424a9..2f5d8480d 100644 --- a/ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs +++ b/ceno_zkvm/src/instructions/riscv/rv32im/mmu.rs @@ -3,18 +3,30 @@ use ff_ext::ExtensionField; use crate::{ error::ZKVMError, structs::{ZKVMConstraintSystem, ZKVMFixedTraces, ZKVMWitnesses}, - tables::{MemFinalRecord, MemInitRecord, ProgramDataCircuit, TableCircuit}, + tables::{ + MemFinalRecord, MemInitRecord, ProgramDataCircuit, PubIOCircuit, RegTableCircuit, + TableCircuit, + }, }; pub struct MmuConfig { + pub reg_config: as TableCircuit>::TableConfig, pub program_data_config: as TableCircuit>::TableConfig, + pub public_io_config: as TableCircuit>::TableConfig, } impl MmuConfig { pub fn construct_circuits(cs: &mut ZKVMConstraintSystem) -> Self { + let reg_config = cs.register_table_circuit::>(); + let program_data_config = cs.register_table_circuit::>(); + + let public_io_config = cs.register_table_circuit::>(); + Self { + reg_config, program_data_config, + public_io_config, } } @@ -22,25 +34,42 @@ impl MmuConfig { &self, cs: &ZKVMConstraintSystem, fixed: &mut ZKVMFixedTraces, + reg_init: &[MemInitRecord], program_data_init: &[MemInitRecord], ) { + fixed.register_table_circuit::>(cs, &self.reg_config, reg_init); + fixed.register_table_circuit::>( cs, &self.program_data_config, program_data_init, ); + + fixed.register_table_circuit::>(cs, &self.public_io_config, &()); } pub fn assign_table_circuit( &self, cs: &ZKVMConstraintSystem, witness: &mut ZKVMWitnesses, + reg_final: &[MemFinalRecord], program_data_final: &[MemFinalRecord], + public_io_final: &[MemFinalRecord], ) -> Result<(), ZKVMError> { + witness.assign_table_circuit::>(cs, &self.reg_config, reg_final)?; + witness.assign_table_circuit::>( cs, &self.program_data_config, program_data_final, - ) + )?; + + witness.assign_table_circuit::>( + cs, + &self.public_io_config, + public_io_final, + )?; + + Ok(()) } }