From 580e087404555df04685a68dc67a62d820c528d7 Mon Sep 17 00:00:00 2001 From: Mihai Date: Thu, 21 Nov 2024 10:18:50 +0200 Subject: [PATCH 01/18] remove maybeuninit; need to fix mock_prover.rs --- ceno_zkvm/src/expression.rs | 8 ++--- ceno_zkvm/src/gadgets/div.rs | 2 +- ceno_zkvm/src/gadgets/is_lt.rs | 16 ++++----- ceno_zkvm/src/gadgets/is_zero.rs | 4 +-- ceno_zkvm/src/gadgets/signed_ext.rs | 2 +- ceno_zkvm/src/instructions.rs | 6 ++-- ceno_zkvm/src/instructions/riscv/arith.rs | 2 +- ceno_zkvm/src/instructions/riscv/arith_imm.rs | 2 +- ceno_zkvm/src/instructions/riscv/b_insn.rs | 2 +- .../instructions/riscv/branch/beq_circuit.rs | 2 +- .../src/instructions/riscv/branch/blt.rs | 2 +- .../src/instructions/riscv/branch/bltu.rs | 2 +- ceno_zkvm/src/instructions/riscv/config.rs | 6 ++-- ceno_zkvm/src/instructions/riscv/divu.rs | 2 +- .../instructions/riscv/dummy/dummy_circuit.rs | 4 +-- .../src/instructions/riscv/ecall/halt.rs | 2 +- .../src/instructions/riscv/ecall_insn.rs | 2 +- ceno_zkvm/src/instructions/riscv/i_insn.rs | 2 +- ceno_zkvm/src/instructions/riscv/im_insn.rs | 2 +- ceno_zkvm/src/instructions/riscv/insn_base.rs | 14 ++++---- ceno_zkvm/src/instructions/riscv/j_insn.rs | 2 +- .../src/instructions/riscv/jump/auipc.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/jal.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/jalr.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/lui.rs | 2 +- .../instructions/riscv/logic/logic_circuit.rs | 4 +-- .../riscv/logic_imm/logic_imm_circuit.rs | 4 +-- .../src/instructions/riscv/memory/gadget.rs | 2 +- .../src/instructions/riscv/memory/load.rs | 2 +- .../src/instructions/riscv/memory/store.rs | 2 +- ceno_zkvm/src/instructions/riscv/mulh.rs | 4 +-- ceno_zkvm/src/instructions/riscv/r_insn.rs | 2 +- ceno_zkvm/src/instructions/riscv/s_insn.rs | 2 +- ceno_zkvm/src/instructions/riscv/shift.rs | 2 +- ceno_zkvm/src/instructions/riscv/shift_imm.rs | 2 +- ceno_zkvm/src/instructions/riscv/slt.rs | 2 +- ceno_zkvm/src/instructions/riscv/slti.rs | 2 +- ceno_zkvm/src/instructions/riscv/sltu.rs | 2 +- ceno_zkvm/src/instructions/riscv/u_insn.rs | 2 +- ceno_zkvm/src/scheme/mock_prover.rs | 9 ++--- ceno_zkvm/src/scheme/tests.rs | 2 +- ceno_zkvm/src/tables/mod.rs | 2 +- ceno_zkvm/src/tables/program.rs | 2 +- ceno_zkvm/src/uint.rs | 16 ++++----- ceno_zkvm/src/witness.rs | 33 ++++++++----------- multilinear_extensions/src/util.rs | 6 ---- multilinear_extensions/src/virtual_poly.rs | 22 ++++++------- 47 files changed, 101 insertions(+), 119 deletions(-) diff --git a/ceno_zkvm/src/expression.rs b/ceno_zkvm/src/expression.rs index 9d65177e0..8429142fc 100644 --- a/ceno_zkvm/src/expression.rs +++ b/ceno_zkvm/src/expression.rs @@ -756,12 +756,8 @@ impl WitIn { ) } - pub fn assign( - &self, - instance: &mut [MaybeUninit], - value: E::BaseField, - ) { - instance[self.id as usize] = MaybeUninit::new(value); + pub fn assign(&self, instance: &mut [E::BaseField], value: E::BaseField) { + instance[self.id as usize] = value; } } diff --git a/ceno_zkvm/src/gadgets/div.rs b/ceno_zkvm/src/gadgets/div.rs index c9d86ab87..f24b9f5e1 100644 --- a/ceno_zkvm/src/gadgets/div.rs +++ b/ceno_zkvm/src/gadgets/div.rs @@ -53,7 +53,7 @@ impl DivConfig { pub fn assign_instance<'a>( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, divisor: &Value<'a, u32>, quotient: &Value<'a, u32>, diff --git a/ceno_zkvm/src/gadgets/is_lt.rs b/ceno_zkvm/src/gadgets/is_lt.rs index 3885b7257..e5afe48bf 100644 --- a/ceno_zkvm/src/gadgets/is_lt.rs +++ b/ceno_zkvm/src/gadgets/is_lt.rs @@ -52,7 +52,7 @@ impl AssertLTConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], lkm: &mut LkMultiplicity, lhs: u64, rhs: u64, @@ -106,7 +106,7 @@ impl IsLtConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], lkm: &mut LkMultiplicity, lhs: u64, rhs: u64, @@ -118,7 +118,7 @@ impl IsLtConfig { pub fn assign_instance_signed( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], lkm: &mut LkMultiplicity, lhs: SWord, rhs: SWord, @@ -184,7 +184,7 @@ impl InnerLtConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], lkm: &mut LkMultiplicity, lhs: u64, rhs: u64, @@ -202,7 +202,7 @@ impl InnerLtConfig { // TODO: refactor with the above function pub fn assign_instance_signed( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], lkm: &mut LkMultiplicity, lhs: SWord, rhs: SWord, @@ -256,7 +256,7 @@ impl AssertSignedLtConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, lhs: SWord, rhs: SWord, @@ -299,7 +299,7 @@ impl SignedLtConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, lhs: SWord, rhs: SWord, @@ -351,7 +351,7 @@ impl InnerSignedLtConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, lhs: SWord, rhs: SWord, diff --git a/ceno_zkvm/src/gadgets/is_zero.rs b/ceno_zkvm/src/gadgets/is_zero.rs index f7d749354..48174d823 100644 --- a/ceno_zkvm/src/gadgets/is_zero.rs +++ b/ceno_zkvm/src/gadgets/is_zero.rs @@ -64,7 +64,7 @@ impl IsZeroConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], x: F, ) -> Result<(), ZKVMError> { let (is_zero, inverse) = if x.is_zero_vartime() { @@ -117,7 +117,7 @@ impl IsEqualConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], a: F, b: F, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/gadgets/signed_ext.rs b/ceno_zkvm/src/gadgets/signed_ext.rs index d1dc8ed62..de50a1f94 100644 --- a/ceno_zkvm/src/gadgets/signed_ext.rs +++ b/ceno_zkvm/src/gadgets/signed_ext.rs @@ -84,7 +84,7 @@ impl SignedExtendConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, val: u64, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 63314cbee..5d36ab5b8 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -37,7 +37,7 @@ pub trait Instruction { // assign single instance giving step from trace fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError>; @@ -79,11 +79,11 @@ pub trait Instruction { let padding_instance = match Self::padding_strategy() { InstancePaddingStrategy::Zero => { - vec![MaybeUninit::new(E::BaseField::ZERO); num_witin] + vec![E::BaseField::ZERO; num_witin] } InstancePaddingStrategy::RepeatLast if steps.is_empty() => { tracing::debug!("No {} steps to repeat, using zero padding", Self::name()); - vec![MaybeUninit::new(E::BaseField::ZERO); num_witin] + vec![E::BaseField::ZERO; num_witin] } InstancePaddingStrategy::RepeatLast => raw_witin[steps.len() - 1].to_vec(), }; diff --git a/ceno_zkvm/src/instructions/riscv/arith.rs b/ceno_zkvm/src/instructions/riscv/arith.rs index c1d9d4412..3afb0ff39 100644 --- a/ceno_zkvm/src/instructions/riscv/arith.rs +++ b/ceno_zkvm/src/instructions/riscv/arith.rs @@ -106,7 +106,7 @@ impl Instruction for ArithInstruction::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/arith_imm.rs b/ceno_zkvm/src/instructions/riscv/arith_imm.rs index f5aed7f0f..18a1121f8 100644 --- a/ceno_zkvm/src/instructions/riscv/arith_imm.rs +++ b/ceno_zkvm/src/instructions/riscv/arith_imm.rs @@ -57,7 +57,7 @@ impl Instruction for AddiInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/b_insn.rs b/ceno_zkvm/src/instructions/riscv/b_insn.rs index c39a1babe..fb7d49ebc 100644 --- a/ceno_zkvm/src/instructions/riscv/b_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/b_insn.rs @@ -88,7 +88,7 @@ impl BInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs b/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs index 8c82d6834..a7b5ce4bb 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs @@ -72,7 +72,7 @@ impl Instruction for BeqCircuit { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/branch/blt.rs b/ceno_zkvm/src/instructions/riscv/branch/blt.rs index c5e0798f2..8ccc6b64f 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/blt.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/blt.rs @@ -66,7 +66,7 @@ impl Instruction for BltCircuit { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/branch/bltu.rs b/ceno_zkvm/src/instructions/riscv/branch/bltu.rs index 896bf19da..5e4ab4065 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/bltu.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/bltu.rs @@ -75,7 +75,7 @@ impl Instruction for BltuCircuit fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [std::mem::MaybeUninit], + instance: &mut [std::mem::E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/config.rs b/ceno_zkvm/src/instructions/riscv/config.rs index cd13e1148..8c91f180e 100644 --- a/ceno_zkvm/src/instructions/riscv/config.rs +++ b/ceno_zkvm/src/instructions/riscv/config.rs @@ -25,7 +25,7 @@ pub struct MsbInput<'a> { impl MsbInput<'_> { pub fn assign( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], config: &MsbConfig, lk_multiplicity: &mut LkMultiplicity, ) -> (u8, u8) { @@ -61,7 +61,7 @@ pub struct UIntLtuInput<'a> { impl UIntLtuInput<'_> { pub fn assign( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], config: &UIntLtuConfig, lk_multiplicity: &mut LkMultiplicity, ) -> bool { @@ -138,7 +138,7 @@ pub struct UIntLtInput<'a> { impl UIntLtInput<'_> { pub fn assign( &self, - instance: &mut [MaybeUninit], + instance: &mut [F], config: &UIntLtConfig, lk_multiplicity: &mut LkMultiplicity, ) -> bool { diff --git a/ceno_zkvm/src/instructions/riscv/divu.rs b/ceno_zkvm/src/instructions/riscv/divu.rs index 8d25f583f..d6b69996c 100644 --- a/ceno_zkvm/src/instructions/riscv/divu.rs +++ b/ceno_zkvm/src/instructions/riscv/divu.rs @@ -124,7 +124,7 @@ impl Instruction for ArithInstruction], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs b/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs index 1fa7dc4c2..b1d0c2bec 100644 --- a/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs @@ -69,7 +69,7 @@ impl Instruction for DummyInstruction::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -214,7 +214,7 @@ impl DummyConfig { fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/ecall/halt.rs b/ceno_zkvm/src/instructions/riscv/ecall/halt.rs index 711ed51f4..17a4f5f99 100644 --- a/ceno_zkvm/src/instructions/riscv/ecall/halt.rs +++ b/ceno_zkvm/src/instructions/riscv/ecall/halt.rs @@ -65,7 +65,7 @@ impl Instruction for HaltInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/ecall_insn.rs b/ceno_zkvm/src/instructions/riscv/ecall_insn.rs index 8196f65c1..64e6fada5 100644 --- a/ceno_zkvm/src/instructions/riscv/ecall_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/ecall_insn.rs @@ -68,7 +68,7 @@ impl EcallInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/i_insn.rs b/ceno_zkvm/src/instructions/riscv/i_insn.rs index 76fb2478c..1b6b5d17c 100644 --- a/ceno_zkvm/src/instructions/riscv/i_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/i_insn.rs @@ -57,7 +57,7 @@ impl IInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/im_insn.rs b/ceno_zkvm/src/instructions/riscv/im_insn.rs index 82ecf22ce..4733eb02a 100644 --- a/ceno_zkvm/src/instructions/riscv/im_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/im_insn.rs @@ -62,7 +62,7 @@ impl IMInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/insn_base.rs b/ceno_zkvm/src/instructions/riscv/insn_base.rs index 9588cb34f..5c9698966 100644 --- a/ceno_zkvm/src/instructions/riscv/insn_base.rs +++ b/ceno_zkvm/src/instructions/riscv/insn_base.rs @@ -59,7 +59,7 @@ impl StateInOut { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], // lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -107,7 +107,7 @@ impl ReadRS1 { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -161,7 +161,7 @@ impl ReadRS2 { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -217,7 +217,7 @@ impl WriteRD { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -275,7 +275,7 @@ impl ReadMEM { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -328,7 +328,7 @@ impl WriteMEM { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -437,7 +437,7 @@ impl MemAddr { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lkm: &mut LkMultiplicity, addr: Word, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/j_insn.rs b/ceno_zkvm/src/instructions/riscv/j_insn.rs index 8dc65e539..562cbd8db 100644 --- a/ceno_zkvm/src/instructions/riscv/j_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/j_insn.rs @@ -53,7 +53,7 @@ impl JInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/jump/auipc.rs b/ceno_zkvm/src/instructions/riscv/jump/auipc.rs index 28be09c66..9ad183784 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/auipc.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/auipc.rs @@ -71,7 +71,7 @@ impl Instruction for AuipcInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/jump/jal.rs b/ceno_zkvm/src/instructions/riscv/jump/jal.rs index 44facf944..464a812ef 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/jal.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/jal.rs @@ -62,7 +62,7 @@ impl Instruction for JalInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/jump/jalr.rs b/ceno_zkvm/src/instructions/riscv/jump/jalr.rs index 0339a6b0a..53cafda80 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/jalr.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/jalr.rs @@ -110,7 +110,7 @@ impl Instruction for JalrInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/jump/lui.rs b/ceno_zkvm/src/instructions/riscv/jump/lui.rs index 1ac67f64f..5c078c544 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/lui.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/lui.rs @@ -48,7 +48,7 @@ impl Instruction for LuiInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs b/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs index 1fbdccbe2..f5610ae21 100644 --- a/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs @@ -50,7 +50,7 @@ impl Instruction for LogicInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -102,7 +102,7 @@ impl LogicConfig { fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs b/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs index 4efd100fc..1b3070660 100644 --- a/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs @@ -50,7 +50,7 @@ impl Instruction for LogicInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -103,7 +103,7 @@ impl LogicConfig { fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/memory/gadget.rs b/ceno_zkvm/src/instructions/riscv/memory/gadget.rs index 6539c1325..327b147be 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/gadget.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/gadget.rs @@ -147,7 +147,7 @@ impl MemWordChange { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, shift: u32, diff --git a/ceno_zkvm/src/instructions/riscv/memory/load.rs b/ceno_zkvm/src/instructions/riscv/memory/load.rs index 72dbd53f1..5df4869cd 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/load.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/load.rs @@ -198,7 +198,7 @@ impl Instruction for LoadInstruction], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/memory/store.rs b/ceno_zkvm/src/instructions/riscv/memory/store.rs index 18b134e57..dd5512fa8 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/store.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/store.rs @@ -127,7 +127,7 @@ impl Instruction fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/mulh.rs b/ceno_zkvm/src/instructions/riscv/mulh.rs index be467de8d..533733c39 100644 --- a/ceno_zkvm/src/instructions/riscv/mulh.rs +++ b/ceno_zkvm/src/instructions/riscv/mulh.rs @@ -267,7 +267,7 @@ impl Instruction for MulhInstructionBas fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { @@ -373,7 +373,7 @@ impl Signed { pub fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, val: &Value, ) -> Result { diff --git a/ceno_zkvm/src/instructions/riscv/r_insn.rs b/ceno_zkvm/src/instructions/riscv/r_insn.rs index 79bf50eb0..16447b8cf 100644 --- a/ceno_zkvm/src/instructions/riscv/r_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/r_insn.rs @@ -61,7 +61,7 @@ impl RInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/s_insn.rs b/ceno_zkvm/src/instructions/riscv/s_insn.rs index c328bc3b7..d1036e78f 100644 --- a/ceno_zkvm/src/instructions/riscv/s_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/s_insn.rs @@ -70,7 +70,7 @@ impl SInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/shift.rs b/ceno_zkvm/src/instructions/riscv/shift.rs index 97b82aaf3..d1f0c46ca 100644 --- a/ceno_zkvm/src/instructions/riscv/shift.rs +++ b/ceno_zkvm/src/instructions/riscv/shift.rs @@ -163,7 +163,7 @@ impl Instruction for ShiftLogicalInstru fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [std::mem::MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut crate::witness::LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), crate::error::ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/shift_imm.rs b/ceno_zkvm/src/instructions/riscv/shift_imm.rs index 44cb01288..7044954e4 100644 --- a/ceno_zkvm/src/instructions/riscv/shift_imm.rs +++ b/ceno_zkvm/src/instructions/riscv/shift_imm.rs @@ -144,7 +144,7 @@ impl Instruction for ShiftImmInstructio fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/slt.rs b/ceno_zkvm/src/instructions/riscv/slt.rs index 2f1a3af97..88a18df54 100644 --- a/ceno_zkvm/src/instructions/riscv/slt.rs +++ b/ceno_zkvm/src/instructions/riscv/slt.rs @@ -60,7 +60,7 @@ impl Instruction for SltInstruction { fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/slti.rs b/ceno_zkvm/src/instructions/riscv/slti.rs index d88f0336c..aee632438 100644 --- a/ceno_zkvm/src/instructions/riscv/slti.rs +++ b/ceno_zkvm/src/instructions/riscv/slti.rs @@ -96,7 +96,7 @@ impl Instruction for SetLessThanImmInst fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/sltu.rs b/ceno_zkvm/src/instructions/riscv/sltu.rs index 26f15b9bc..46ed77a03 100644 --- a/ceno_zkvm/src/instructions/riscv/sltu.rs +++ b/ceno_zkvm/src/instructions/riscv/sltu.rs @@ -78,7 +78,7 @@ impl Instruction for ArithInstruction::BaseField>], + instance: &mut [::BaseField], lkm: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/instructions/riscv/u_insn.rs b/ceno_zkvm/src/instructions/riscv/u_insn.rs index 613ea1dcc..f59618b1d 100644 --- a/ceno_zkvm/src/instructions/riscv/u_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/u_insn.rs @@ -51,7 +51,7 @@ impl UInstructionConfig { pub fn assign_instance( &self, - instance: &mut [MaybeUninit<::BaseField>], + instance: &mut [::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/scheme/mock_prover.rs b/ceno_zkvm/src/scheme/mock_prover.rs index 1504fd562..5cf7941a9 100644 --- a/ceno_zkvm/src/scheme/mock_prover.rs +++ b/ceno_zkvm/src/scheme/mock_prover.rs @@ -654,10 +654,7 @@ impl<'a, E: ExtensionField + Hash> MockProver { for table_expr in &cs.lk_table_expressions { for row in fixed.iter_rows() { // TODO: Find a better way to obtain the row content. - let row = row - .iter() - .map(|v| unsafe { (*v).assume_init() }.into()) - .collect::>(); + // let row = row.iter().map(|v| *v.into()).collect::>(); let rlc_record = eval_by_expr_with_fixed(&row, &[], &challenge, &table_expr.values); t_vec.push(rlc_record.to_canonical_u64_vec()); } @@ -1378,7 +1375,7 @@ mod tests { fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], input: AssertLtCircuitInput, lk_multiplicity: &mut LkMultiplicity, ) -> Result<(), ZKVMError> { @@ -1492,7 +1489,7 @@ mod tests { fn assign_instance( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], input: LtCircuitInput, lk_multiplicity: &mut LkMultiplicity, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/scheme/tests.rs b/ceno_zkvm/src/scheme/tests.rs index 04edee440..8b87ed2ce 100644 --- a/ceno_zkvm/src/scheme/tests.rs +++ b/ceno_zkvm/src/scheme/tests.rs @@ -71,7 +71,7 @@ impl Instruction for Test fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], _lk_multiplicity: &mut LkMultiplicity, _step: &StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index 2ef7e293a..28fd779b0 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -51,7 +51,7 @@ pub trait TableCircuit { if num_padding_instances > 0 { let nthreads = std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); - let padding_instance = vec![MaybeUninit::new(E::BaseField::ZERO); num_witin]; + let padding_instance = vec![E::BaseField::ZERO; num_witin]; let num_padding_instance_per_batch = if num_padding_instances > 256 { num_padding_instances.div_ceil(nthreads) } else { diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index ec6cf6c46..2d9417279 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -164,7 +164,7 @@ impl TableCircuit .for_each(|(row, i)| { let pc = pc_base + (i * PC_STEP_SIZE) as u32; let insn = DecodedInstruction::new(program.instructions[i]); - let values = InsnRecord::from_decoded(pc, &insn); + let values: InsnRecord<_> = InsnRecord::from_decoded(pc, &insn); // Copy all the fields. for (col, val) in config.record.as_slice().iter().zip_eq(values.as_slice()) { diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index afb231d8a..642d8368b 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -181,20 +181,20 @@ impl UIntLimbs { pub fn assign_value + Default + From + Copy>( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], value: Value, ) { self.assign_limbs(instance, value.as_u16_limbs()) } - pub fn assign_add_outcome(&self, instance: &mut [MaybeUninit], value: &ValueAdd) { + pub fn assign_add_outcome(&self, instance: &mut [E::BaseField], value: &ValueAdd) { self.assign_limbs(instance, &value.limbs); self.assign_carries(instance, &value.carries); } pub fn assign_mul_outcome( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, value: &ValueMul, ) -> Result<(), ZKVMError> { @@ -203,7 +203,7 @@ impl UIntLimbs { self.assign_carries_auxiliary(instance, lkm, &value.carries, value.max_carry_value) } - pub fn assign_limbs(&self, instance: &mut [MaybeUninit], limbs_values: &[u16]) { + pub fn assign_limbs(&self, instance: &mut [E::BaseField], limbs_values: &[u16]) { assert!( limbs_values.len() <= Self::NUM_LIMBS, "assign input length mismatch. input_len={}, NUM_CELLS={}", @@ -217,14 +217,14 @@ impl UIntLimbs { .map(|v| E::BaseField::from(*v as u64)) .chain(std::iter::repeat(E::BaseField::ZERO)), ) { - instance[wire.id as usize] = MaybeUninit::new(limb); + instance[wire.id as usize] = limb; } } } pub fn assign_carries + Copy>( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], carry_values: &[T], ) { assert!( @@ -243,14 +243,14 @@ impl UIntLimbs { .map(|v| E::BaseField::from(Into::::into(*v))) .chain(std::iter::repeat(E::BaseField::ZERO)), ) { - instance[wire.id as usize] = MaybeUninit::new(carry); + instance[wire.id as usize] = carry; } } } pub fn assign_carries_auxiliary + Copy>( &self, - instance: &mut [MaybeUninit], + instance: &mut [E::BaseField], lkm: &mut LkMultiplicity, carry_values: &[T], max_carry: u64, diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 7acc9ad50..d2960a914 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -9,10 +9,7 @@ use std::{ sync::Arc, }; -use multilinear_extensions::{ - mle::{DenseMultilinearExtension, IntoMLEs}, - util::create_uninit_vec, -}; +use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLEs}; use rayon::{ iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}, slice::ParallelSliceMut, @@ -28,31 +25,31 @@ use crate::{ #[macro_export] macro_rules! set_val { ($ins:ident, $field:expr, $val:expr) => { - $ins[$field.id as usize] = MaybeUninit::new($val.into()); + $ins[$field.id as usize] = $val.into(); }; } #[macro_export] macro_rules! set_fixed_val { ($ins:ident, $field:expr, $val:expr) => { - $ins[$field.0] = MaybeUninit::new($val); + $ins[$field.0] = $val; }; } #[derive(Clone)] pub struct RowMajorMatrix { // represent 2D in 1D linear memory and avoid double indirection by Vec> to improve performance - values: Vec>, + values: Vec, num_padding_rows: usize, num_col: usize, } -impl RowMajorMatrix { +impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize) -> Self { let num_total_rows = next_pow2_instance_padding(num_rows); let num_padding_rows = num_total_rows - num_rows; RowMajorMatrix { - values: create_uninit_vec(num_total_rows * num_col), + values: vec![T::default(); num_total_rows * num_col], num_padding_rows, num_col, } @@ -66,29 +63,26 @@ impl RowMajorMatrix { self.num_padding_rows } - pub fn iter_rows(&self) -> Chunks> { + pub fn iter_rows(&self) -> Chunks { self.values.chunks(self.num_col) } - pub fn iter_mut(&mut self) -> ChunksMut> { + pub fn iter_mut(&mut self) -> ChunksMut { self.values.chunks_mut(self.num_col) } - pub fn par_iter_mut(&mut self) -> rayon::slice::ChunksMut> { + pub fn par_iter_mut(&mut self) -> rayon::slice::ChunksMut { self.values.par_chunks_mut(self.num_col) } - pub fn par_batch_iter_mut( - &mut self, - num_rows: usize, - ) -> rayon::slice::ChunksMut> { + pub fn par_batch_iter_mut(&mut self, num_rows: usize) -> rayon::slice::ChunksMut { self.values.par_chunks_mut(num_rows * self.num_col) } pub fn par_batch_iter_padding_mut( &mut self, num_rows: usize, - ) -> rayon::slice::ChunksMut<'_, MaybeUninit> { + ) -> rayon::slice::ChunksMut<'_, T> { let valid_instance = self.num_instances(); self.values[valid_instance * self.num_col..] .as_mut() @@ -102,7 +96,8 @@ impl RowMajorMatrix { .par_iter_mut() .skip(i) .step_by(self.num_col) - .map(|v| unsafe { mem::replace(v, mem::MaybeUninit::uninit()).assume_init() }) + //.map(|v| unsafe { mem::replace(v, mem::MaybeUninit::uninit()).assume_init() }) + .map(|v| *v) .collect::>() }) .collect() @@ -118,7 +113,7 @@ impl RowMajorMatrix { } impl Index for RowMajorMatrix { - type Output = [MaybeUninit]; + type Output = [F]; fn index(&self, idx: usize) -> &Self::Output { &self.values[self.num_col * idx..][..self.num_col] diff --git a/multilinear_extensions/src/util.rs b/multilinear_extensions/src/util.rs index a0a8e56a2..8e41a1368 100644 --- a/multilinear_extensions/src/util.rs +++ b/multilinear_extensions/src/util.rs @@ -20,12 +20,6 @@ pub fn ceil_log2(x: usize) -> usize { usize_bits - (x - 1).leading_zeros() as usize } -pub fn create_uninit_vec(len: usize) -> Vec> { - let mut vec: Vec> = Vec::with_capacity(len); - unsafe { vec.set_len(len) }; - vec -} - #[inline(always)] pub fn largest_even_below(n: usize) -> usize { if n % 2 == 0 { n } else { n.saturating_sub(1) } diff --git a/multilinear_extensions/src/virtual_poly.rs b/multilinear_extensions/src/virtual_poly.rs index 72c5b2649..f40829a70 100644 --- a/multilinear_extensions/src/virtual_poly.rs +++ b/multilinear_extensions/src/virtual_poly.rs @@ -2,7 +2,7 @@ use std::{cmp::max, collections::HashMap, marker::PhantomData, mem::MaybeUninit, use crate::{ mle::{ArcDenseMultilinearExtension, DenseMultilinearExtension, MultilinearExtension}, - util::{bit_decompose, create_uninit_vec, max_usable_threads}, + util::{bit_decompose, max_usable_threads}, }; use ark_std::{end_timer, iterable::Iterable, rand::Rng, start_timer}; use ff::{Field, PrimeField}; @@ -313,7 +313,7 @@ impl VirtualPolynomial { let mut flattened_ml_extensions = vec![]; let mut hm = HashMap::new(); for mle in self.flattened_ml_extensions.iter() { - let mle_ptr = Arc::as_ptr(mle) as usize; + let mle_ptr = Arc::as_ptr(&mle) as usize; let index = self.raw_pointers_lookup_table.get(&mle_ptr).unwrap(); let mle_ext_field = mle.as_ref().to_ext_field(); @@ -383,16 +383,16 @@ pub fn build_eq_x_r_vec_sequential(r: &[E]) -> Vec { // 1 1 1 1 -> r0 * r1 * r2 * r3 // we will need 2^num_var evaluations - let mut evals = create_uninit_vec(1 << r.len()); + let mut evals = vec![E::ZERO; (1 << r.len())]; build_eq_x_r_helper_sequential(r, &mut evals, E::ONE); - unsafe { std::mem::transmute(evals) } + evals } /// A helper function to build eq(x, r)*init via dynamic programing tricks. /// This function takes 2^num_var iterations, and per iteration with 1 multiplication. -fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [MaybeUninit], init: E) { - buf[0] = MaybeUninit::new(init); +fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [E], init: E) { + buf[0] = init; for (i, r) in r.iter().rev().enumerate() { let next_size = 1 << (i + 1); @@ -402,10 +402,10 @@ fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [MaybeUn // buf[2*j + 1] = r * buf[j] // buf[2*j] = (1 - r) * buf[j] (0..next_size).step_by(2).rev().for_each(|index| { - let prev_val = unsafe { buf[index >> 1].assume_init() }; + let prev_val = buf[index >> 1]; let tmp = *r * prev_val; - buf[index + 1] = MaybeUninit::new(tmp); - buf[index] = MaybeUninit::new(prev_val - tmp); + buf[index + 1] = tmp; + buf[index] = prev_val - tmp; }); } } @@ -454,7 +454,7 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { build_eq_x_r_vec_sequential(r) } else { let eq_ts = build_eq_x_r_vec_sequential(&r[(r.len() - nbits)..]); - let mut ret = create_uninit_vec(1 << r.len()); + let mut ret = vec![E::ZERO; (1 << r.len())]; // eq(x, r) = eq(x_lo, r_lo) * eq(x_hi, r_hi) // where rlen = r.len(), x_lo = x[0..rlen-nbits], x_hi = x[rlen-nbits..] @@ -468,7 +468,7 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { build_eq_x_r_helper_sequential(&r[..(r.len() - nbits)], chunks, eq_t); }); - unsafe { std::mem::transmute::>, Vec>(ret) } + ret } } From 196142d94cef0d12e84203f3d1bd20bb3b2209ba Mon Sep 17 00:00:00 2001 From: Mihai Date: Thu, 21 Nov 2024 10:26:03 +0200 Subject: [PATCH 02/18] fix inference --- ceno_zkvm/src/instructions/riscv/branch/bltu.rs | 2 +- ceno_zkvm/src/scheme/mock_prover.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ceno_zkvm/src/instructions/riscv/branch/bltu.rs b/ceno_zkvm/src/instructions/riscv/branch/bltu.rs index 5e4ab4065..713158fdc 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/bltu.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/bltu.rs @@ -75,7 +75,7 @@ impl Instruction for BltuCircuit fn assign_instance( config: &Self::InstructionConfig, - instance: &mut [std::mem::E::BaseField], + instance: &mut [E::BaseField], lk_multiplicity: &mut LkMultiplicity, step: &ceno_emul::StepRecord, ) -> Result<(), ZKVMError> { diff --git a/ceno_zkvm/src/scheme/mock_prover.rs b/ceno_zkvm/src/scheme/mock_prover.rs index 5cf7941a9..8f820e044 100644 --- a/ceno_zkvm/src/scheme/mock_prover.rs +++ b/ceno_zkvm/src/scheme/mock_prover.rs @@ -654,7 +654,7 @@ impl<'a, E: ExtensionField + Hash> MockProver { for table_expr in &cs.lk_table_expressions { for row in fixed.iter_rows() { // TODO: Find a better way to obtain the row content. - // let row = row.iter().map(|v| *v.into()).collect::>(); + let row = row.iter().map(|v| (*v).into()).collect::>(); let rlc_record = eval_by_expr_with_fixed(&row, &[], &challenge, &table_expr.values); t_vec.push(rlc_record.to_canonical_u64_vec()); } From d5f3357fde5b98083cd8ce20a8a4485bcaef6d84 Mon Sep 17 00:00:00 2001 From: Mihai Date: Thu, 21 Nov 2024 10:48:31 +0200 Subject: [PATCH 03/18] cargo fix --- ceno_zkvm/src/expression.rs | 1 - ceno_zkvm/src/gadgets/div.rs | 2 +- ceno_zkvm/src/gadgets/is_lt.rs | 2 +- ceno_zkvm/src/gadgets/is_zero.rs | 1 - ceno_zkvm/src/gadgets/signed_ext.rs | 2 +- ceno_zkvm/src/instructions.rs | 1 - ceno_zkvm/src/instructions/riscv/arith.rs | 1 - ceno_zkvm/src/instructions/riscv/arith_imm.rs | 2 +- ceno_zkvm/src/instructions/riscv/b_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs | 2 +- ceno_zkvm/src/instructions/riscv/branch/blt.rs | 2 +- ceno_zkvm/src/instructions/riscv/config.rs | 1 - ceno_zkvm/src/instructions/riscv/divu.rs | 1 - ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs | 1 - ceno_zkvm/src/instructions/riscv/ecall/halt.rs | 2 +- ceno_zkvm/src/instructions/riscv/ecall_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/i_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/im_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/insn_base.rs | 1 - ceno_zkvm/src/instructions/riscv/j_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/jump/auipc.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/jal.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/jalr.rs | 2 +- ceno_zkvm/src/instructions/riscv/jump/lui.rs | 2 +- ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs | 1 - .../src/instructions/riscv/logic_imm/logic_imm_circuit.rs | 1 - ceno_zkvm/src/instructions/riscv/memory/gadget.rs | 1 - ceno_zkvm/src/instructions/riscv/memory/load.rs | 2 +- ceno_zkvm/src/instructions/riscv/memory/store.rs | 2 +- ceno_zkvm/src/instructions/riscv/mulh.rs | 1 - ceno_zkvm/src/instructions/riscv/r_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/s_insn.rs | 1 - ceno_zkvm/src/instructions/riscv/shift.rs | 2 +- ceno_zkvm/src/instructions/riscv/shift_imm.rs | 2 +- ceno_zkvm/src/instructions/riscv/slt.rs | 1 - ceno_zkvm/src/instructions/riscv/slti.rs | 1 - ceno_zkvm/src/instructions/riscv/sltu.rs | 1 - ceno_zkvm/src/instructions/riscv/u_insn.rs | 1 - ceno_zkvm/src/scheme/mock_prover.rs | 2 +- ceno_zkvm/src/scheme/tests.rs | 2 +- ceno_zkvm/src/tables/mod.rs | 2 +- ceno_zkvm/src/tables/ops/ops_impl.rs | 2 +- ceno_zkvm/src/tables/program.rs | 2 +- ceno_zkvm/src/tables/ram/ram_impl.rs | 2 +- ceno_zkvm/src/tables/range/range_impl.rs | 2 +- ceno_zkvm/src/uint.rs | 2 +- ceno_zkvm/src/witness.rs | 2 +- multilinear_extensions/src/util.rs | 1 - multilinear_extensions/src/virtual_poly.rs | 6 +++--- 49 files changed, 27 insertions(+), 51 deletions(-) diff --git a/ceno_zkvm/src/expression.rs b/ceno_zkvm/src/expression.rs index 8429142fc..bad41b1b4 100644 --- a/ceno_zkvm/src/expression.rs +++ b/ceno_zkvm/src/expression.rs @@ -4,7 +4,6 @@ use std::{ cmp::max, fmt::Display, iter::{Product, Sum}, - mem::MaybeUninit, ops::{Add, AddAssign, Deref, Mul, MulAssign, Neg, Shl, ShlAssign, Sub, SubAssign}, }; diff --git a/ceno_zkvm/src/gadgets/div.rs b/ceno_zkvm/src/gadgets/div.rs index f24b9f5e1..95b7cc49f 100644 --- a/ceno_zkvm/src/gadgets/div.rs +++ b/ceno_zkvm/src/gadgets/div.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, mem::MaybeUninit}; +use std::fmt::Display; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/gadgets/is_lt.rs b/ceno_zkvm/src/gadgets/is_lt.rs index e5afe48bf..967efc868 100644 --- a/ceno_zkvm/src/gadgets/is_lt.rs +++ b/ceno_zkvm/src/gadgets/is_lt.rs @@ -1,4 +1,4 @@ -use std::{fmt::Display, mem::MaybeUninit}; +use std::fmt::Display; use ceno_emul::{SWord, Word}; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/gadgets/is_zero.rs b/ceno_zkvm/src/gadgets/is_zero.rs index 48174d823..e156cb9ce 100644 --- a/ceno_zkvm/src/gadgets/is_zero.rs +++ b/ceno_zkvm/src/gadgets/is_zero.rs @@ -1,4 +1,3 @@ -use std::mem::MaybeUninit; use ff_ext::ExtensionField; use goldilocks::SmallField; diff --git a/ceno_zkvm/src/gadgets/signed_ext.rs b/ceno_zkvm/src/gadgets/signed_ext.rs index de50a1f94..3b6757bc3 100644 --- a/ceno_zkvm/src/gadgets/signed_ext.rs +++ b/ceno_zkvm/src/gadgets/signed_ext.rs @@ -7,7 +7,7 @@ use crate::{ witness::LkMultiplicity, }; use ff_ext::ExtensionField; -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; #[derive(Debug)] pub struct SignedExtendConfig { diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 5d36ab5b8..43de84eb9 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -1,4 +1,3 @@ -use std::mem::MaybeUninit; use ceno_emul::StepRecord; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/arith.rs b/ceno_zkvm/src/instructions/riscv/arith.rs index 3afb0ff39..2ea302844 100644 --- a/ceno_zkvm/src/instructions/riscv/arith.rs +++ b/ceno_zkvm/src/instructions/riscv/arith.rs @@ -11,7 +11,6 @@ use crate::{ uint::{Value, ValueMul}, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles R-Instructions that represent registers values as 2 * u16. #[derive(Debug)] diff --git a/ceno_zkvm/src/instructions/riscv/arith_imm.rs b/ceno_zkvm/src/instructions/riscv/arith_imm.rs index 18a1121f8..c4c09629b 100644 --- a/ceno_zkvm/src/instructions/riscv/arith_imm.rs +++ b/ceno_zkvm/src/instructions/riscv/arith_imm.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::StepRecord; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/b_insn.rs b/ceno_zkvm/src/instructions/riscv/b_insn.rs index fb7d49ebc..646e9bd05 100644 --- a/ceno_zkvm/src/instructions/riscv/b_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/b_insn.rs @@ -13,7 +13,6 @@ use crate::{ utils::i64_to_base, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; // Opcode: 1100011 // Funct3: diff --git a/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs b/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs index a7b5ce4bb..b8c96fbd1 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/beq_circuit.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::{InsnKind, StepRecord}; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/branch/blt.rs b/ceno_zkvm/src/instructions/riscv/branch/blt.rs index 8ccc6b64f..dfe5995e1 100644 --- a/ceno_zkvm/src/instructions/riscv/branch/blt.rs +++ b/ceno_zkvm/src/instructions/riscv/branch/blt.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/config.rs b/ceno_zkvm/src/instructions/riscv/config.rs index 8c91f180e..e35465add 100644 --- a/ceno_zkvm/src/instructions/riscv/config.rs +++ b/ceno_zkvm/src/instructions/riscv/config.rs @@ -1,4 +1,3 @@ -use std::mem::MaybeUninit; use crate::{expression::WitIn, set_val, utils::i64_to_base, witness::LkMultiplicity}; use goldilocks::SmallField; diff --git a/ceno_zkvm/src/instructions/riscv/divu.rs b/ceno_zkvm/src/instructions/riscv/divu.rs index d6b69996c..33ff68fae 100644 --- a/ceno_zkvm/src/instructions/riscv/divu.rs +++ b/ceno_zkvm/src/instructions/riscv/divu.rs @@ -16,7 +16,6 @@ use crate::{ uint::Value, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; use std::marker::PhantomData; pub struct ArithConfig { diff --git a/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs b/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs index b1d0c2bec..1d98d1271 100644 --- a/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs @@ -19,7 +19,6 @@ use crate::{ utils::i64_to_base, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// DummyInstruction can handle any instruction and produce its side-effects. pub struct DummyInstruction(PhantomData<(E, I)>); diff --git a/ceno_zkvm/src/instructions/riscv/ecall/halt.rs b/ceno_zkvm/src/instructions/riscv/ecall/halt.rs index 17a4f5f99..ddad991a9 100644 --- a/ceno_zkvm/src/instructions/riscv/ecall/halt.rs +++ b/ceno_zkvm/src/instructions/riscv/ecall/halt.rs @@ -16,7 +16,7 @@ use crate::{ }; use ceno_emul::{StepRecord, Tracer}; use ff_ext::ExtensionField; -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; pub struct HaltConfig { ecall_cfg: EcallInstructionConfig, diff --git a/ceno_zkvm/src/instructions/riscv/ecall_insn.rs b/ceno_zkvm/src/instructions/riscv/ecall_insn.rs index 64e6fada5..577ad2ff3 100644 --- a/ceno_zkvm/src/instructions/riscv/ecall_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/ecall_insn.rs @@ -12,7 +12,6 @@ use crate::{ }; use ceno_emul::{CENO_PLATFORM, InsnKind::EANY, PC_STEP_SIZE, StepRecord, Tracer}; use ff_ext::ExtensionField; -use std::mem::MaybeUninit; pub struct EcallInstructionConfig { pub pc: WitIn, diff --git a/ceno_zkvm/src/instructions/riscv/i_insn.rs b/ceno_zkvm/src/instructions/riscv/i_insn.rs index 1b6b5d17c..52147cdec 100644 --- a/ceno_zkvm/src/instructions/riscv/i_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/i_insn.rs @@ -10,7 +10,6 @@ use crate::{ tables::InsnRecord, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles the common part of I-type instructions: /// - PC, cycle, fetch. diff --git a/ceno_zkvm/src/instructions/riscv/im_insn.rs b/ceno_zkvm/src/instructions/riscv/im_insn.rs index 4733eb02a..36ad1a8a1 100644 --- a/ceno_zkvm/src/instructions/riscv/im_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/im_insn.rs @@ -9,7 +9,6 @@ use crate::{ }; use ceno_emul::{InsnKind, StepRecord}; use ff_ext::ExtensionField; -use std::mem::MaybeUninit; /// This config handle the common part of I-type Instruction (memory variant) /// - PC, cycle, fetch diff --git a/ceno_zkvm/src/instructions/riscv/insn_base.rs b/ceno_zkvm/src/instructions/riscv/insn_base.rs index 5c9698966..c8dad1070 100644 --- a/ceno_zkvm/src/instructions/riscv/insn_base.rs +++ b/ceno_zkvm/src/instructions/riscv/insn_base.rs @@ -18,7 +18,6 @@ use crate::{ witness::LkMultiplicity, }; use ceno_emul::Tracer; -use core::mem::MaybeUninit; use std::{iter, marker::PhantomData}; #[derive(Debug)] diff --git a/ceno_zkvm/src/instructions/riscv/j_insn.rs b/ceno_zkvm/src/instructions/riscv/j_insn.rs index 562cbd8db..81cc8c493 100644 --- a/ceno_zkvm/src/instructions/riscv/j_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/j_insn.rs @@ -10,7 +10,6 @@ use crate::{ tables::InsnRecord, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; // Opcode: 1101111 diff --git a/ceno_zkvm/src/instructions/riscv/jump/auipc.rs b/ceno_zkvm/src/instructions/riscv/jump/auipc.rs index 9ad183784..ed1f07d1f 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/auipc.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/auipc.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::InsnKind; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/jump/jal.rs b/ceno_zkvm/src/instructions/riscv/jump/jal.rs index 464a812ef..24bcad463 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/jal.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/jal.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/jump/jalr.rs b/ceno_zkvm/src/instructions/riscv/jump/jalr.rs index 53cafda80..1d9c2c025 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/jalr.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/jalr.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ff::Field; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/jump/lui.rs b/ceno_zkvm/src/instructions/riscv/jump/lui.rs index 5c078c544..8ad9d497e 100644 --- a/ceno_zkvm/src/instructions/riscv/jump/lui.rs +++ b/ceno_zkvm/src/instructions/riscv/jump/lui.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::InsnKind; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs b/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs index f5610ae21..30fe370e0 100644 --- a/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/logic/logic_circuit.rs @@ -1,6 +1,5 @@ //! The circuit implementation of logic instructions. -use core::mem::MaybeUninit; use ff_ext::ExtensionField; use std::marker::PhantomData; diff --git a/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs b/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs index 1b3070660..9328e27a7 100644 --- a/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs +++ b/ceno_zkvm/src/instructions/riscv/logic_imm/logic_imm_circuit.rs @@ -1,6 +1,5 @@ //! The circuit implementation of logic instructions. -use core::mem::MaybeUninit; use ff_ext::ExtensionField; use std::marker::PhantomData; diff --git a/ceno_zkvm/src/instructions/riscv/memory/gadget.rs b/ceno_zkvm/src/instructions/riscv/memory/gadget.rs index 327b147be..e43d862fe 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/gadget.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/gadget.rs @@ -11,7 +11,6 @@ use ceno_emul::StepRecord; use ff::Field; use ff_ext::ExtensionField; use itertools::izip; -use std::mem::MaybeUninit; pub struct MemWordChange { prev_limb_bytes: Vec, diff --git a/ceno_zkvm/src/instructions/riscv/memory/load.rs b/ceno_zkvm/src/instructions/riscv/memory/load.rs index 5df4869cd..6ed258804 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/load.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/load.rs @@ -19,7 +19,7 @@ use crate::{ use ceno_emul::{ByteAddr, InsnKind, StepRecord}; use ff_ext::ExtensionField; use itertools::izip; -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; pub struct LoadConfig { im_insn: IMInstructionConfig, diff --git a/ceno_zkvm/src/instructions/riscv/memory/store.rs b/ceno_zkvm/src/instructions/riscv/memory/store.rs index dd5512fa8..0c233738d 100644 --- a/ceno_zkvm/src/instructions/riscv/memory/store.rs +++ b/ceno_zkvm/src/instructions/riscv/memory/store.rs @@ -17,7 +17,7 @@ use crate::{ }; use ceno_emul::{ByteAddr, CENO_PLATFORM, InsnKind, StepRecord}; use ff_ext::ExtensionField; -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; pub struct StoreConfig { s_insn: SInstructionConfig, diff --git a/ceno_zkvm/src/instructions/riscv/mulh.rs b/ceno_zkvm/src/instructions/riscv/mulh.rs index 533733c39..7ec706b7b 100644 --- a/ceno_zkvm/src/instructions/riscv/mulh.rs +++ b/ceno_zkvm/src/instructions/riscv/mulh.rs @@ -101,7 +101,6 @@ use crate::{ utils::i64_to_base, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; pub struct MulhInstructionBase(PhantomData<(E, I)>); diff --git a/ceno_zkvm/src/instructions/riscv/r_insn.rs b/ceno_zkvm/src/instructions/riscv/r_insn.rs index 16447b8cf..78c262290 100644 --- a/ceno_zkvm/src/instructions/riscv/r_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/r_insn.rs @@ -10,7 +10,6 @@ use crate::{ tables::InsnRecord, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles the common part of R-type instructions: /// - PC, cycle, fetch. diff --git a/ceno_zkvm/src/instructions/riscv/s_insn.rs b/ceno_zkvm/src/instructions/riscv/s_insn.rs index d1036e78f..efe271337 100644 --- a/ceno_zkvm/src/instructions/riscv/s_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/s_insn.rs @@ -9,7 +9,6 @@ use crate::{ }; use ceno_emul::{InsnKind, StepRecord}; use ff_ext::ExtensionField; -use std::mem::MaybeUninit; /// This config handles the common part of S-type instructions: /// - PC, cycle, fetch. diff --git a/ceno_zkvm/src/instructions/riscv/shift.rs b/ceno_zkvm/src/instructions/riscv/shift.rs index d1f0c46ca..792472c4d 100644 --- a/ceno_zkvm/src/instructions/riscv/shift.rs +++ b/ceno_zkvm/src/instructions/riscv/shift.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::InsnKind; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/instructions/riscv/shift_imm.rs b/ceno_zkvm/src/instructions/riscv/shift_imm.rs index 7044954e4..771121f89 100644 --- a/ceno_zkvm/src/instructions/riscv/shift_imm.rs +++ b/ceno_zkvm/src/instructions/riscv/shift_imm.rs @@ -15,7 +15,7 @@ use crate::{ }; use ceno_emul::{InsnKind, StepRecord}; use ff_ext::ExtensionField; -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; pub struct ShiftImmConfig { i_insn: IInstructionConfig, diff --git a/ceno_zkvm/src/instructions/riscv/slt.rs b/ceno_zkvm/src/instructions/riscv/slt.rs index 88a18df54..65cca0169 100644 --- a/ceno_zkvm/src/instructions/riscv/slt.rs +++ b/ceno_zkvm/src/instructions/riscv/slt.rs @@ -8,7 +8,6 @@ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, gadgets::SignedLtConfig, instructions::Instruction, uint::Value, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles R-Instructions that represent registers values as 2 * u16. #[derive(Debug)] diff --git a/ceno_zkvm/src/instructions/riscv/slti.rs b/ceno_zkvm/src/instructions/riscv/slti.rs index aee632438..6b5097eec 100644 --- a/ceno_zkvm/src/instructions/riscv/slti.rs +++ b/ceno_zkvm/src/instructions/riscv/slti.rs @@ -20,7 +20,6 @@ use crate::{ utils::i64_to_base, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; #[derive(Debug)] pub struct SetLessThanImmConfig { diff --git a/ceno_zkvm/src/instructions/riscv/sltu.rs b/ceno_zkvm/src/instructions/riscv/sltu.rs index 46ed77a03..ecab275de 100644 --- a/ceno_zkvm/src/instructions/riscv/sltu.rs +++ b/ceno_zkvm/src/instructions/riscv/sltu.rs @@ -12,7 +12,6 @@ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, gadgets::IsLtConfig, instructions::Instruction, uint::Value, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles R-Instructions that represent registers values as 2 * u16. #[derive(Debug)] diff --git a/ceno_zkvm/src/instructions/riscv/u_insn.rs b/ceno_zkvm/src/instructions/riscv/u_insn.rs index f59618b1d..719ee2df1 100644 --- a/ceno_zkvm/src/instructions/riscv/u_insn.rs +++ b/ceno_zkvm/src/instructions/riscv/u_insn.rs @@ -10,7 +10,6 @@ use crate::{ tables::InsnRecord, witness::LkMultiplicity, }; -use core::mem::MaybeUninit; /// This config handles the common part of the U-type instruction: /// - PC, cycle, fetch diff --git a/ceno_zkvm/src/scheme/mock_prover.rs b/ceno_zkvm/src/scheme/mock_prover.rs index 8f820e044..8b114ef3e 100644 --- a/ceno_zkvm/src/scheme/mock_prover.rs +++ b/ceno_zkvm/src/scheme/mock_prover.rs @@ -1204,7 +1204,7 @@ Hints: #[cfg(test)] mod tests { - use std::mem::MaybeUninit; + use super::*; use crate::{ diff --git a/ceno_zkvm/src/scheme/tests.rs b/ceno_zkvm/src/scheme/tests.rs index 8b87ed2ce..485b81bca 100644 --- a/ceno_zkvm/src/scheme/tests.rs +++ b/ceno_zkvm/src/scheme/tests.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::{ CENO_PLATFORM, diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index 28fd779b0..c3e50e7d1 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -5,7 +5,7 @@ use crate::{ use ff::Field; use ff_ext::ExtensionField; use rayon::iter::{IndexedParallelIterator, ParallelIterator}; -use std::{collections::HashMap, mem::MaybeUninit}; +use std::collections::HashMap; mod range; pub use range::*; diff --git a/ceno_zkvm/src/tables/ops/ops_impl.rs b/ceno_zkvm/src/tables/ops/ops_impl.rs index ddd407078..937b334b3 100644 --- a/ceno_zkvm/src/tables/ops/ops_impl.rs +++ b/ceno_zkvm/src/tables/ops/ops_impl.rs @@ -4,7 +4,7 @@ use ff_ext::ExtensionField; use goldilocks::SmallField; use itertools::Itertools; use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; -use std::{collections::HashMap, mem::MaybeUninit}; +use std::collections::HashMap; use crate::{ circuit_builder::CircuitBuilder, diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 2d9417279..60dc40c8f 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, marker::PhantomData, mem::MaybeUninit}; +use std::{collections::HashMap, marker::PhantomData}; use crate::{ circuit_builder::CircuitBuilder, diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index 4c20bc894..5b8b5b302 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, mem::MaybeUninit}; +use std::marker::PhantomData; use ceno_emul::{Addr, Cycle}; use ff_ext::ExtensionField; diff --git a/ceno_zkvm/src/tables/range/range_impl.rs b/ceno_zkvm/src/tables/range/range_impl.rs index 505ef4b4f..f8d5a96e7 100644 --- a/ceno_zkvm/src/tables/range/range_impl.rs +++ b/ceno_zkvm/src/tables/range/range_impl.rs @@ -3,7 +3,7 @@ use ff_ext::ExtensionField; use goldilocks::SmallField; use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; -use std::{collections::HashMap, mem::MaybeUninit}; +use std::collections::HashMap; use crate::{ circuit_builder::CircuitBuilder, diff --git a/ceno_zkvm/src/uint.rs b/ceno_zkvm/src/uint.rs index 642d8368b..1f540b472 100644 --- a/ceno_zkvm/src/uint.rs +++ b/ceno_zkvm/src/uint.rs @@ -20,7 +20,7 @@ use goldilocks::SmallField; use itertools::Itertools; use std::{ borrow::Cow, - mem::{self, MaybeUninit}, + mem::{self}, ops::Index, }; pub use strum::IntoEnumIterator; diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index d2960a914..6f8a97a54 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -3,7 +3,7 @@ use std::{ array, cell::RefCell, collections::HashMap, - mem::{self, MaybeUninit}, + mem::{self}, ops::Index, slice::{Chunks, ChunksMut}, sync::Arc, diff --git a/multilinear_extensions/src/util.rs b/multilinear_extensions/src/util.rs index 8e41a1368..c8cb6b8a4 100644 --- a/multilinear_extensions/src/util.rs +++ b/multilinear_extensions/src/util.rs @@ -1,4 +1,3 @@ -use std::mem::MaybeUninit; /// Decompose an integer into a binary vector in little endian. pub fn bit_decompose(input: u64, num_var: usize) -> Vec { diff --git a/multilinear_extensions/src/virtual_poly.rs b/multilinear_extensions/src/virtual_poly.rs index f40829a70..556444eb6 100644 --- a/multilinear_extensions/src/virtual_poly.rs +++ b/multilinear_extensions/src/virtual_poly.rs @@ -1,4 +1,4 @@ -use std::{cmp::max, collections::HashMap, marker::PhantomData, mem::MaybeUninit, sync::Arc}; +use std::{cmp::max, collections::HashMap, marker::PhantomData, sync::Arc}; use crate::{ mle::{ArcDenseMultilinearExtension, DenseMultilinearExtension, MultilinearExtension}, @@ -383,7 +383,7 @@ pub fn build_eq_x_r_vec_sequential(r: &[E]) -> Vec { // 1 1 1 1 -> r0 * r1 * r2 * r3 // we will need 2^num_var evaluations - let mut evals = vec![E::ZERO; (1 << r.len())]; + let mut evals = vec![E::ZERO; 1 << r.len()]; build_eq_x_r_helper_sequential(r, &mut evals, E::ONE); evals @@ -454,7 +454,7 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { build_eq_x_r_vec_sequential(r) } else { let eq_ts = build_eq_x_r_vec_sequential(&r[(r.len() - nbits)..]); - let mut ret = vec![E::ZERO; (1 << r.len())]; + let mut ret = vec![E::ZERO; 1 << r.len()]; // eq(x, r) = eq(x_lo, r_lo) * eq(x_hi, r_hi) // where rlen = r.len(), x_lo = x[0..rlen-nbits], x_hi = x[rlen-nbits..] From e40719b90e1143c69b6a97c75cdc033c84fd0cb4 Mon Sep 17 00:00:00 2001 From: Mihai Date: Fri, 22 Nov 2024 09:38:14 +0200 Subject: [PATCH 04/18] suspend padding --- ceno_zkvm/src/tables/mod.rs | 50 ++++++++++----------- ceno_zkvm/src/tables/ops/ops_circuit.rs | 4 +- ceno_zkvm/src/tables/program.rs | 4 +- ceno_zkvm/src/tables/range/range_circuit.rs | 4 +- 4 files changed, 31 insertions(+), 31 deletions(-) diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index c3e50e7d1..fe83c7c92 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -42,29 +42,29 @@ pub trait TableCircuit { input: &Self::WitnessInput, ) -> Result, ZKVMError>; - fn padding_zero( - table: &mut RowMajorMatrix, - num_witin: usize, - ) -> Result<(), ZKVMError> { - // Fill the padding with zeros, if any. - let num_padding_instances = table.num_padding_instances(); - if num_padding_instances > 0 { - let nthreads = - std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); - let padding_instance = vec![E::BaseField::ZERO; num_witin]; - let num_padding_instance_per_batch = if num_padding_instances > 256 { - num_padding_instances.div_ceil(nthreads) - } else { - num_padding_instances - }; - table - .par_batch_iter_padding_mut(num_padding_instance_per_batch) - .with_min_len(MIN_PAR_SIZE) - .for_each(|row| { - row.chunks_mut(num_witin) - .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); - }); - } - Ok(()) - } + // fn padding_zero( + // table: &mut RowMajorMatrix, + // num_witin: usize, + // ) -> Result<(), ZKVMError> { + // // Fill the padding with zeros, if any. + // let num_padding_instances = table.num_padding_instances(); + // if num_padding_instances > 0 { + // let nthreads = + // std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); + // let padding_instance = vec![E::BaseField::ZERO; num_witin]; + // let num_padding_instance_per_batch = if num_padding_instances > 256 { + // num_padding_instances.div_ceil(nthreads) + // } else { + // num_padding_instances + // }; + // table + // .par_batch_iter_padding_mut(num_padding_instance_per_batch) + // .with_min_len(MIN_PAR_SIZE) + // .for_each(|row| { + // row.chunks_mut(num_witin) + // .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); + // }); + // } + // Ok(()) + // } } diff --git a/ceno_zkvm/src/tables/ops/ops_circuit.rs b/ceno_zkvm/src/tables/ops/ops_circuit.rs index cd48ebe19..8a946b6ba 100644 --- a/ceno_zkvm/src/tables/ops/ops_circuit.rs +++ b/ceno_zkvm/src/tables/ops/ops_circuit.rs @@ -52,7 +52,7 @@ impl TableCircuit for OpsTableCircuit _input: &(), ) -> RowMajorMatrix { let mut table = config.generate_fixed_traces(num_fixed, OP::content()); - Self::padding_zero(&mut table, num_fixed).expect("padding error"); + // Self::padding_zero(&mut table, num_fixed).expect("padding error"); table } @@ -64,7 +64,7 @@ impl TableCircuit for OpsTableCircuit ) -> Result, ZKVMError> { let multiplicity = &multiplicity[OP::ROM_TYPE as usize]; let mut table = config.assign_instances(num_witin, multiplicity, OP::len())?; - Self::padding_zero(&mut table, num_witin)?; + // Self::padding_zero(&mut table, num_witin)?; Ok(table) } } diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 60dc40c8f..3d9c24e2c 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -172,7 +172,7 @@ impl TableCircuit } }); - Self::padding_zero(&mut fixed, num_fixed).expect("padding error"); + // Self::padding_zero(&mut fixed, num_fixed).expect("padding error"); fixed } @@ -199,7 +199,7 @@ impl TableCircuit set_val!(row, config.mlt, E::BaseField::from(mlt as u64)); }); - Self::padding_zero(&mut witness, num_witin).expect("padding error"); + // Self::padding_zero(&mut witness, num_witin).expect("padding error"); Ok(witness) } } diff --git a/ceno_zkvm/src/tables/range/range_circuit.rs b/ceno_zkvm/src/tables/range/range_circuit.rs index bb7c83448..81d9ac7ac 100644 --- a/ceno_zkvm/src/tables/range/range_circuit.rs +++ b/ceno_zkvm/src/tables/range/range_circuit.rs @@ -45,7 +45,7 @@ impl TableCircuit for RangeTableCircuit _input: &(), ) -> RowMajorMatrix { let mut table = config.generate_fixed_traces(num_fixed, RANGE::content()); - Self::padding_zero(&mut table, num_fixed).expect("padding error"); + // Self::padding_zero(&mut table, num_fixed).expect("padding error"); table } @@ -57,7 +57,7 @@ impl TableCircuit for RangeTableCircuit ) -> Result, ZKVMError> { let multiplicity = &multiplicity[RANGE::ROM_TYPE as usize]; let mut table = config.assign_instances(num_witin, multiplicity, RANGE::len())?; - Self::padding_zero(&mut table, num_witin).expect("padding error"); + // Self::padding_zero(&mut table, num_witin).expect("padding error"); Ok(table) } } From d2055bc5e6732348bfbb613f8c3fb51eabbd5792 Mon Sep 17 00:00:00 2001 From: Mihai Date: Fri, 22 Nov 2024 12:57:20 +0200 Subject: [PATCH 05/18] experiments --- ceno_zkvm/src/instructions.rs | 35 ++++++++----- ceno_zkvm/src/instructions/riscv/divu.rs | 1 - ceno_zkvm/src/instructions/riscv/insn_base.rs | 1 - ceno_zkvm/src/scheme/mock_prover.rs | 2 - ceno_zkvm/src/tables/mod.rs | 52 +++++++++---------- ceno_zkvm/src/tables/program.rs | 2 +- ceno_zkvm/src/witness.rs | 43 +++++++++++++-- multilinear_extensions/src/mle.rs | 5 +- multilinear_extensions/src/util.rs | 1 - 9 files changed, 91 insertions(+), 51 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 61cdb780b..0d4231950 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -1,4 +1,3 @@ - use ceno_emul::StepRecord; use ff_ext::ExtensionField; use rayon::{ @@ -87,18 +86,28 @@ pub trait Instruction { InstancePaddingStrategy::RepeatLast => raw_witin[steps.len() - 1].to_vec(), }; - let num_padding_instance_per_batch = if num_padding_instances > 256 { - num_padding_instances.div_ceil(nthreads) - } else { - num_padding_instances - }; - raw_witin - .par_batch_iter_padding_mut(None, num_padding_instance_per_batch) - .with_min_len(MIN_PAR_SIZE) - .for_each(|row| { - row.chunks_mut(num_witin) - .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); - }); + if let InstancePaddingStrategy::RepeatLast = Self::padding_strategy() { + let num_padding_instance_per_batch = if num_padding_instances > 256 { + num_padding_instances.div_ceil(nthreads) + } else { + num_padding_instances + }; + + // for row in 0..raw_witin.num_padding_instances() { + // let start = raw_witin.len() - (row + 1) * num_witin; + // let end = raw_witin.len() - row * num_witin; + // let slice = &mut raw_witin.values()[start..end]; + // slice.copy_from_slice(padding_instance.as_slice()); + // } + raw_witin + .par_batch_iter_padding_mut(None, num_padding_instance_per_batch) + .with_min_len(MIN_PAR_SIZE) + .for_each(|row| { + row.chunks_mut(num_witin).for_each(|instance| { + instance.copy_from_slice(padding_instance.as_slice()) + }); + }); + } } Ok((raw_witin, lk_multiplicity)) diff --git a/ceno_zkvm/src/instructions/riscv/divu.rs b/ceno_zkvm/src/instructions/riscv/divu.rs index 33ff68fae..2e1b0d8a8 100644 --- a/ceno_zkvm/src/instructions/riscv/divu.rs +++ b/ceno_zkvm/src/instructions/riscv/divu.rs @@ -237,7 +237,6 @@ mod test { MockProver::assert_with_expected_errors( &cb, &raw_witin - .de_interleaving() .into_mles() .into_iter() .map(|v| v.into()) diff --git a/ceno_zkvm/src/instructions/riscv/insn_base.rs b/ceno_zkvm/src/instructions/riscv/insn_base.rs index c8dad1070..f9a3bd9b8 100644 --- a/ceno_zkvm/src/instructions/riscv/insn_base.rs +++ b/ceno_zkvm/src/instructions/riscv/insn_base.rs @@ -541,7 +541,6 @@ mod test { MockProver::assert_with_expected_errors( &cb, &raw_witin - .de_interleaving() .into_mles() .into_iter() .map(|v| v.into()) diff --git a/ceno_zkvm/src/scheme/mock_prover.rs b/ceno_zkvm/src/scheme/mock_prover.rs index 5ff9cb25b..5120a8a74 100644 --- a/ceno_zkvm/src/scheme/mock_prover.rs +++ b/ceno_zkvm/src/scheme/mock_prover.rs @@ -724,7 +724,6 @@ Hints: lkm: Option, ) { let wits_in = raw_witin - .de_interleaving() .into_mles() .into_iter() .map(|v| v.into()) @@ -1203,7 +1202,6 @@ Hints: #[cfg(test)] mod tests { - use super::*; use crate::{ diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index db3bf2552..57355e97e 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -69,29 +69,29 @@ pub trait TableCircuit { // } } -/// Fill the padding with zeros. Start after the given `num_instances`, or detect it from the table. -pub fn padding_zero( - table: &mut RowMajorMatrix, - num_cols: usize, - num_instances: Option, -) { - // Fill the padding with zeros, if any. - let num_padding_instances = table.num_padding_instances(); - if num_padding_instances > 0 { - let nthreads = - std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); - let padding_instance = vec![F::ZERO; num_cols]; - let num_padding_instance_per_batch = if num_padding_instances > 256 { - num_padding_instances.div_ceil(nthreads) - } else { - num_padding_instances - }; - table - .par_batch_iter_padding_mut(num_instances, num_padding_instance_per_batch) - .with_min_len(MIN_PAR_SIZE) - .for_each(|row| { - row.chunks_mut(num_cols) - .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); - }); - } -} +// /// Fill the padding with zeros. Start after the given `num_instances`, or detect it from the table. +// pub fn padding_zero( +// table: &mut RowMajorMatrix, +// num_cols: usize, +// num_instances: Option, +// ) { +// // Fill the padding with zeros, if any. +// let num_padding_instances = table.num_padding_instances(); +// if num_padding_instances > 0 { +// let nthreads = +// std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); +// let padding_instance = vec![F::ZERO; num_cols]; +// let num_padding_instance_per_batch = if num_padding_instances > 256 { +// num_padding_instances.div_ceil(nthreads) +// } else { +// num_padding_instances +// }; +// table +// .par_batch_iter_padding_mut(num_instances, num_padding_instance_per_batch) +// .with_min_len(MIN_PAR_SIZE) +// .for_each(|row| { +// row.chunks_mut(num_cols) +// .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); +// }); +// } +// } diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index b04e5ef5c..e3f246de3 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -7,7 +7,7 @@ use crate::{ scheme::constants::MIN_PAR_SIZE, set_fixed_val, set_val, structs::ROMType, - tables::{TableCircuit, padding_zero}, + tables::TableCircuit, utils::i64_to_base, witness::RowMajorMatrix, }; diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 9dcb7424e..85c1c7fc3 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -1,4 +1,5 @@ use ff::Field; +use itertools::Itertools; use std::{ array, cell::RefCell, @@ -7,12 +8,16 @@ use std::{ ops::Index, slice::{Chunks, ChunksMut}, sync::Arc, + time::Instant, }; -use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLEs}; +use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE, IntoMLEs}; use rayon::{ - iter::{IndexedParallelIterator, IntoParallelRefMutIterator, ParallelIterator}, - slice::ParallelSliceMut, + iter::{ + IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, + IntoParallelRefMutIterator, ParallelIterator, + }, + slice::{ParallelSlice, ParallelSliceMut}, }; use thread_local::ThreadLocal; @@ -48,6 +53,7 @@ impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize) -> Self { let num_total_rows = next_pow2_instance_padding(num_rows); let num_padding_rows = num_total_rows - num_rows; + println!("{}, {}", num_total_rows, num_col); RowMajorMatrix { values: vec![T::default(); num_total_rows * num_col], num_padding_rows, @@ -55,6 +61,14 @@ impl RowMajorMatrix { } } + pub fn len(&self) -> usize { + self.values.len() + } + + pub fn values(&mut self) -> &mut Vec { + &mut self.values + } + pub fn num_instances(&self) -> usize { self.values.len() / self.num_col - self.num_padding_rows } @@ -90,7 +104,7 @@ impl RowMajorMatrix { .par_chunks_mut(batch_size * self.num_col) } - pub fn de_interleaving(mut self) -> Vec> { + fn de_interleaving(mut self) -> Vec> { (0..self.num_col) .map(|i| { self.values @@ -109,7 +123,26 @@ impl RowMajorMatrix { pub fn into_mles>( self, ) -> Vec> { - self.de_interleaving().into_mles() + let start = Instant::now(); + let result = (0..self.num_col) + .collect_vec() + .par_iter() + .map(|i| { + self.values + .iter() + .skip(*i) + .step_by(self.num_col) + .map(|val| *val) + .collect::>() + .into_mle() + }) + .collect(); + let size = self.num_col * self.len(); + if size > 1000 * 1000 { + let duration = start.elapsed().as_secs_f64(); + println!("Time taken: {:?}, size: {:?}", duration, size); + } + result } } diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index 8a182e645..ba16568e8 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -105,7 +105,8 @@ pub trait IntoMLE: Sized { impl IntoMLE> for Vec { fn into_mle(mut self) -> DenseMultilinearExtension { let next_pow2 = self.len().next_power_of_two(); - self.resize(next_pow2, F::ZERO); + // self.resize(next_pow2, F::ZERO); + assert!(self.len().is_power_of_two()); DenseMultilinearExtension::from_evaluation_vec_smart::(ceil_log2(next_pow2), self) } } @@ -118,6 +119,8 @@ impl> IntoMLEs> { fn into_mles(self) -> Vec> { + // TODO: figure out correct place to test + // assert!(self.len().is_power_of_two(), "{}", self.len()); self.into_iter().map(|v| v.into_mle()).collect() } } diff --git a/multilinear_extensions/src/util.rs b/multilinear_extensions/src/util.rs index c8cb6b8a4..611ed4fb8 100644 --- a/multilinear_extensions/src/util.rs +++ b/multilinear_extensions/src/util.rs @@ -1,4 +1,3 @@ - /// Decompose an integer into a binary vector in little endian. pub fn bit_decompose(input: u64, num_var: usize) -> Vec { let mut res = Vec::with_capacity(num_var); From 717f7fba884d5355b7e4644e3df47c9ffc2ba091 Mon Sep 17 00:00:00 2001 From: Mihai Date: Fri, 22 Nov 2024 16:05:27 +0200 Subject: [PATCH 06/18] remove ad-hoc paddings --- ceno_zkvm/src/instructions.rs | 43 +--------- ceno_zkvm/src/instructions/riscv/insn_base.rs | 7 +- ceno_zkvm/src/scheme/mock_prover.rs | 13 ++- ceno_zkvm/src/tables/ops/ops_impl.rs | 7 +- ceno_zkvm/src/tables/program.rs | 13 ++- ceno_zkvm/src/tables/ram/ram_circuit.rs | 3 +- ceno_zkvm/src/tables/ram/ram_impl.rs | 53 ++++++------ ceno_zkvm/src/tables/range/range_impl.rs | 7 +- ceno_zkvm/src/witness.rs | 81 +++++++++++-------- multilinear_extensions/src/mle.rs | 2 +- 10 files changed, 115 insertions(+), 114 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 0d4231950..1ae0a2f45 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -15,6 +15,7 @@ use ff::Field; pub mod riscv; +#[derive(Clone)] pub enum InstancePaddingStrategy { Zero, RepeatLast, @@ -54,7 +55,8 @@ pub trait Instruction { } .max(1); let lk_multiplicity = LkMultiplicity::default(); - let mut raw_witin = RowMajorMatrix::::new(steps.len(), num_witin); + let mut raw_witin = + RowMajorMatrix::::new(steps.len(), num_witin, Self::padding_strategy()); let raw_witin_iter = raw_witin.par_batch_iter_mut(num_instance_per_batch); raw_witin_iter @@ -71,45 +73,6 @@ pub trait Instruction { }) .collect::>()?; - let num_padding_instances = raw_witin.num_padding_instances(); - if num_padding_instances > 0 { - // Fill the padding based on strategy - - let padding_instance = match Self::padding_strategy() { - InstancePaddingStrategy::Zero => { - vec![E::BaseField::ZERO; num_witin] - } - InstancePaddingStrategy::RepeatLast if steps.is_empty() => { - tracing::debug!("No {} steps to repeat, using zero padding", Self::name()); - vec![E::BaseField::ZERO; num_witin] - } - InstancePaddingStrategy::RepeatLast => raw_witin[steps.len() - 1].to_vec(), - }; - - if let InstancePaddingStrategy::RepeatLast = Self::padding_strategy() { - let num_padding_instance_per_batch = if num_padding_instances > 256 { - num_padding_instances.div_ceil(nthreads) - } else { - num_padding_instances - }; - - // for row in 0..raw_witin.num_padding_instances() { - // let start = raw_witin.len() - (row + 1) * num_witin; - // let end = raw_witin.len() - row * num_witin; - // let slice = &mut raw_witin.values()[start..end]; - // slice.copy_from_slice(padding_instance.as_slice()); - // } - raw_witin - .par_batch_iter_padding_mut(None, num_padding_instance_per_batch) - .with_min_len(MIN_PAR_SIZE) - .for_each(|row| { - row.chunks_mut(num_witin).for_each(|instance| { - instance.copy_from_slice(padding_instance.as_slice()) - }); - }); - } - } - Ok((raw_witin, lk_multiplicity)) } } diff --git a/ceno_zkvm/src/instructions/riscv/insn_base.rs b/ceno_zkvm/src/instructions/riscv/insn_base.rs index f9a3bd9b8..abe810c26 100644 --- a/ceno_zkvm/src/instructions/riscv/insn_base.rs +++ b/ceno_zkvm/src/instructions/riscv/insn_base.rs @@ -476,6 +476,7 @@ mod test { ROMType, circuit_builder::{CircuitBuilder, ConstraintSystem}, error::ZKVMError, + instructions::InstancePaddingStrategy, scheme::mock_prover::MockProver, witness::{LkMultiplicity, RowMajorMatrix}, }; @@ -515,7 +516,11 @@ mod test { let mut lkm = LkMultiplicity::default(); let num_rows = 2; - let mut raw_witin = RowMajorMatrix::::new(num_rows, cb.cs.num_witin as usize); + let mut raw_witin = RowMajorMatrix::::new( + num_rows, + cb.cs.num_witin as usize, + InstancePaddingStrategy::Zero, + ); for instance in raw_witin.iter_mut() { mem_addr.assign_instance(instance, &mut lkm, addr)?; } diff --git a/ceno_zkvm/src/scheme/mock_prover.rs b/ceno_zkvm/src/scheme/mock_prover.rs index 5120a8a74..a7a081f29 100644 --- a/ceno_zkvm/src/scheme/mock_prover.rs +++ b/ceno_zkvm/src/scheme/mock_prover.rs @@ -1209,6 +1209,7 @@ mod tests { error::ZKVMError, expression::{ToExpr, WitIn}, gadgets::{AssertLTConfig, IsLtConfig}, + instructions::InstancePaddingStrategy, set_val, witness::{LkMultiplicity, RowMajorMatrix}, }; @@ -1390,7 +1391,11 @@ mod tests { instances: Vec, lk_multiplicity: &mut LkMultiplicity, ) -> Result, ZKVMError> { - let mut raw_witin = RowMajorMatrix::::new(instances.len(), num_witin); + let mut raw_witin = RowMajorMatrix::::new( + instances.len(), + num_witin, + InstancePaddingStrategy::Zero, + ); let raw_witin_iter = raw_witin.iter_mut(); raw_witin_iter @@ -1504,7 +1509,11 @@ mod tests { instances: Vec, lk_multiplicity: &mut LkMultiplicity, ) -> Result, ZKVMError> { - let mut raw_witin = RowMajorMatrix::::new(instances.len(), num_witin); + let mut raw_witin = RowMajorMatrix::::new( + instances.len(), + num_witin, + InstancePaddingStrategy::Zero, + ); let raw_witin_iter = raw_witin.iter_mut(); raw_witin_iter diff --git a/ceno_zkvm/src/tables/ops/ops_impl.rs b/ceno_zkvm/src/tables/ops/ops_impl.rs index 937b334b3..4c34c5fec 100644 --- a/ceno_zkvm/src/tables/ops/ops_impl.rs +++ b/ceno_zkvm/src/tables/ops/ops_impl.rs @@ -10,6 +10,7 @@ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, expression::{Expression, Fixed, ToExpr, WitIn}, + instructions::InstancePaddingStrategy, scheme::constants::MIN_PAR_SIZE, set_fixed_val, set_val, structs::ROMType, @@ -47,7 +48,8 @@ impl OpTableConfig { num_fixed: usize, content: Vec<[u64; 3]>, ) -> RowMajorMatrix { - let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed); + let mut fixed = + RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); fixed .par_iter_mut() @@ -68,7 +70,8 @@ impl OpTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - let mut witness = RowMajorMatrix::::new(length, num_witin); + let mut witness = + RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); let mut mlts = vec![0; length]; for (idx, mlt) in multiplicity { diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index e3f246de3..10bd4701d 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -4,6 +4,7 @@ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, expression::{Expression, Fixed, ToExpr, WitIn}, + instructions::InstancePaddingStrategy, scheme::constants::MIN_PAR_SIZE, set_fixed_val, set_val, structs::ROMType, @@ -158,7 +159,11 @@ impl TableCircuit for ProgramTableCircuit { let pc_base = program.base_address; assert!(num_instructions <= config.program_size); - let mut fixed = RowMajorMatrix::::new(config.program_size, num_fixed); + let mut fixed = RowMajorMatrix::::new( + config.program_size, + num_fixed, + InstancePaddingStrategy::Zero, + ); fixed .par_iter_mut() @@ -193,7 +198,11 @@ impl TableCircuit for ProgramTableCircuit { prog_mlt[i] = *mlt; } - let mut witness = RowMajorMatrix::::new(config.program_size, num_witin); + let mut witness = RowMajorMatrix::::new( + config.program_size, + num_witin, + InstancePaddingStrategy::Zero, + ); witness .par_iter_mut() .with_min_len(MIN_PAR_SIZE) diff --git a/ceno_zkvm/src/tables/ram/ram_circuit.rs b/ceno_zkvm/src/tables/ram/ram_circuit.rs index 81f0fcfaa..9ed7f9067 100644 --- a/ceno_zkvm/src/tables/ram/ram_circuit.rs +++ b/ceno_zkvm/src/tables/ram/ram_circuit.rs @@ -6,6 +6,7 @@ use ff_ext::ExtensionField; use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, + instructions::InstancePaddingStrategy, structs::{ProgramParams, RAMType}, tables::TableCircuit, witness::RowMajorMatrix, @@ -192,7 +193,7 @@ impl TableC _num_fixed: usize, _init_v: &Self::FixedInput, ) -> RowMajorMatrix { - RowMajorMatrix::::new(0, 0) + RowMajorMatrix::::new(0, 0, InstancePaddingStrategy::Zero) } fn assign_instances( diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index 0cfcd461e..04af49df0 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -10,7 +10,10 @@ use crate::{ circuit_builder::{CircuitBuilder, DynamicAddr, SetTableAddrType, SetTableSpec}, error::ZKVMError, expression::{Expression, Fixed, ToExpr, WitIn}, - instructions::riscv::constants::{LIMB_BITS, LIMB_MASK}, + instructions::{ + InstancePaddingStrategy, + riscv::constants::{LIMB_BITS, LIMB_MASK}, + }, scheme::constants::MIN_PAR_SIZE, set_fixed_val, set_val, structs::ProgramParams, @@ -116,7 +119,11 @@ impl NonVolatileTableConfig::new(NVRAM::len(&self.params), num_fixed); + let mut init_table = RowMajorMatrix::::new( + NVRAM::len(&self.params), + num_fixed, + InstancePaddingStrategy::Zero, + ); assert_eq!(init_table.num_padding_instances(), 0); init_table @@ -146,7 +153,11 @@ impl NonVolatileTableConfig Result, ZKVMError> { - let mut final_table = RowMajorMatrix::::new(NVRAM::len(&self.params), num_witness); + let mut final_table = RowMajorMatrix::::new( + NVRAM::len(&self.params), + num_witness, + InstancePaddingStrategy::Zero, + ); final_table .par_iter_mut() @@ -246,7 +257,11 @@ impl PubIOTableConfig { ) -> RowMajorMatrix { assert!(NVRAM::len(&self.params).is_power_of_two()); - let mut init_table = RowMajorMatrix::::new(NVRAM::len(&self.params), num_fixed); + let mut init_table = RowMajorMatrix::::new( + NVRAM::len(&self.params), + num_fixed, + InstancePaddingStrategy::Zero, + ); assert_eq!(init_table.num_padding_instances(), 0); init_table @@ -265,7 +280,11 @@ impl PubIOTableConfig { num_witness: usize, final_cycles: &[Cycle], ) -> Result, ZKVMError> { - let mut final_table = RowMajorMatrix::::new(NVRAM::len(&self.params), num_witness); + let mut final_table = RowMajorMatrix::::new( + NVRAM::len(&self.params), + num_witness, + InstancePaddingStrategy::Zero, + ); final_table .par_iter_mut() @@ -370,7 +389,7 @@ impl DynVolatileRamTableConfig assert!(final_mem.len() <= DVRAM::max_len(&self.params)); assert!(DVRAM::max_len(&self.params).is_power_of_two()); let mut final_table = - RowMajorMatrix::::new(final_mem.len().next_power_of_two(), num_witness); + RowMajorMatrix::::new(final_mem.len(), num_witness, InstancePaddingStrategy::Zero); final_table .par_iter_mut() @@ -391,28 +410,6 @@ impl DynVolatileRamTableConfig set_val!(row, self.final_cycle, rec.cycle); }); - // set padding with well-form address - if final_mem.len().next_power_of_two() - final_mem.len() > 0 { - let paddin_entry_start = final_mem.len(); - final_table - .par_iter_mut() - .skip(final_mem.len()) - .enumerate() - .with_min_len(MIN_PAR_SIZE) - .for_each(|(i, row)| { - // Assign value limbs. - self.final_v.iter().for_each(|limb| { - set_val!(row, limb, 0u64); - }); - set_val!( - row, - self.addr, - DVRAM::addr(&self.params, paddin_entry_start + i) as u64 - ); - set_val!(row, self.final_cycle, 0_u64); - }); - } - Ok(final_table) } } diff --git a/ceno_zkvm/src/tables/range/range_impl.rs b/ceno_zkvm/src/tables/range/range_impl.rs index f8d5a96e7..11191e22f 100644 --- a/ceno_zkvm/src/tables/range/range_impl.rs +++ b/ceno_zkvm/src/tables/range/range_impl.rs @@ -9,6 +9,7 @@ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, expression::{Expression, Fixed, ToExpr, WitIn}, + instructions::InstancePaddingStrategy, scheme::constants::MIN_PAR_SIZE, set_fixed_val, set_val, structs::ROMType, @@ -42,7 +43,8 @@ impl RangeTableConfig { num_fixed: usize, content: Vec, ) -> RowMajorMatrix { - let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed); + let mut fixed = + RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); fixed .par_iter_mut() @@ -61,7 +63,8 @@ impl RangeTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - let mut witness = RowMajorMatrix::::new(length, num_witin); + let mut witness = + RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); let mut mlts = vec![0; length]; for (idx, mlt) in multiplicity { diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 85c1c7fc3..c9f947fa3 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -1,9 +1,11 @@ +use ark_std::iterable::Iterable; use ff::Field; use itertools::Itertools; use std::{ array, cell::RefCell, collections::HashMap, + iter, mem::{self}, ops::Index, slice::{Chunks, ChunksMut}, @@ -22,6 +24,7 @@ use rayon::{ use thread_local::ThreadLocal; use crate::{ + instructions::InstancePaddingStrategy, structs::ROMType, tables::{AndTable, LtuTable, OpsTable, OrTable, PowTable, XorTable}, utils::next_pow2_instance_padding, @@ -45,19 +48,19 @@ macro_rules! set_fixed_val { pub struct RowMajorMatrix { // represent 2D in 1D linear memory and avoid double indirection by Vec> to improve performance values: Vec, - num_padding_rows: usize, num_col: usize, + padding_strategy: InstancePaddingStrategy, } impl RowMajorMatrix { - pub fn new(num_rows: usize, num_col: usize) -> Self { - let num_total_rows = next_pow2_instance_padding(num_rows); - let num_padding_rows = num_total_rows - num_rows; - println!("{}, {}", num_total_rows, num_col); + pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { + // let num_total_rows = next_pow2_instance_padding(num_rows); + // let num_padding_rows = num_total_rows - num_rows; + // println!("{}, {}", num_total_rows, num_col); RowMajorMatrix { - values: vec![T::default(); num_total_rows * num_col], - num_padding_rows, + values: vec![T::default(); num_rows * num_col], num_col, + padding_strategy, } } @@ -69,12 +72,12 @@ impl RowMajorMatrix { &mut self.values } - pub fn num_instances(&self) -> usize { - self.values.len() / self.num_col - self.num_padding_rows + pub fn num_padding_instances(&self) -> usize { + return next_pow2_instance_padding(self.num_instances()) - self.num_instances(); } - pub fn num_padding_instances(&self) -> usize { - self.num_padding_rows + pub fn num_instances(&self) -> usize { + self.values.len() / self.num_col } pub fn iter_rows(&self) -> Chunks { @@ -93,30 +96,30 @@ impl RowMajorMatrix { self.values.par_chunks_mut(num_rows * self.num_col) } - pub fn par_batch_iter_padding_mut( - &mut self, - num_instances: Option, - batch_size: usize, - ) -> rayon::slice::ChunksMut<'_, T> { - let num_instances = num_instances.unwrap_or(self.num_instances()); - self.values[num_instances * self.num_col..] - .as_mut() - .par_chunks_mut(batch_size * self.num_col) - } - - fn de_interleaving(mut self) -> Vec> { - (0..self.num_col) - .map(|i| { - self.values - .par_iter_mut() - .skip(i) - .step_by(self.num_col) - //.map(|v| unsafe { mem::replace(v, mem::MaybeUninit::uninit()).assume_init() }) - .map(|v| *v) - .collect::>() - }) - .collect() - } + // pub fn par_batch_iter_padding_mut( + // &mut self, + // num_instances: Option, + // batch_size: usize, + // ) -> rayon::slice::ChunksMut<'_, T> { + // let num_instances = num_instances.unwrap_or(self.num_instances()); + // self.values[num_instances * self.num_col..] + // .as_mut() + // .par_chunks_mut(batch_size * self.num_col) + // } + + // fn de_interleaving(mut self) -> Vec> { + // (0..self.num_col) + // .map(|i| { + // self.values + // .par_iter_mut() + // .skip(i) + // .step_by(self.num_col) + // //.map(|v| unsafe { mem::replace(v, mem::MaybeUninit::uninit()).assume_init() }) + // .map(|v| *v) + // .collect::>() + // }) + // .collect() + //} } impl RowMajorMatrix { @@ -124,6 +127,13 @@ impl RowMajorMatrix { self, ) -> Vec> { let start = Instant::now(); + let padding_row = match self.padding_strategy { + InstancePaddingStrategy::RepeatLast => { + self.values[self.values.len() - self.num_col..].to_vec() + } + InstancePaddingStrategy::Zero => vec![F::ZERO; self.num_col], + }; + let num_padding = self.num_padding_instances(); let result = (0..self.num_col) .collect_vec() .par_iter() @@ -132,6 +142,7 @@ impl RowMajorMatrix { .iter() .skip(*i) .step_by(self.num_col) + .chain(&mut iter::repeat(&padding_row[*i]).take(num_padding)) .map(|val| *val) .collect::>() .into_mle() diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index ba16568e8..f05efc98f 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -106,7 +106,7 @@ impl IntoMLE> for Vec< fn into_mle(mut self) -> DenseMultilinearExtension { let next_pow2 = self.len().next_power_of_two(); // self.resize(next_pow2, F::ZERO); - assert!(self.len().is_power_of_two()); + assert!(self.len().is_power_of_two(), "{}", self.len()); DenseMultilinearExtension::from_evaluation_vec_smart::(ceil_log2(next_pow2), self) } } From 95d066d993ced0c197628c5755dffeaffe1b65e4 Mon Sep 17 00:00:00 2001 From: Mihai Date: Fri, 22 Nov 2024 16:10:37 +0200 Subject: [PATCH 07/18] some clean-up --- ceno_zkvm/src/tables/mod.rs | 53 ------------------------------------- ceno_zkvm/src/witness.rs | 28 -------------------- 2 files changed, 81 deletions(-) diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index 57355e97e..9a794fe08 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -41,57 +41,4 @@ pub trait TableCircuit { multiplicity: &[HashMap], input: &Self::WitnessInput, ) -> Result, ZKVMError>; - - // fn padding_zero( - // table: &mut RowMajorMatrix, - // num_witin: usize, - // ) -> Result<(), ZKVMError> { - // // Fill the padding with zeros, if any. - // let num_padding_instances = table.num_padding_instances(); - // if num_padding_instances > 0 { - // let nthreads = - // std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); - // let padding_instance = vec![E::BaseField::ZERO; num_witin]; - // let num_padding_instance_per_batch = if num_padding_instances > 256 { - // num_padding_instances.div_ceil(nthreads) - // } else { - // num_padding_instances - // }; - // table - // .par_batch_iter_padding_mut(num_padding_instance_per_batch) - // .with_min_len(MIN_PAR_SIZE) - // .for_each(|row| { - // row.chunks_mut(num_witin) - // .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); - // }); - // } - // Ok(()) - // } } - -// /// Fill the padding with zeros. Start after the given `num_instances`, or detect it from the table. -// pub fn padding_zero( -// table: &mut RowMajorMatrix, -// num_cols: usize, -// num_instances: Option, -// ) { -// // Fill the padding with zeros, if any. -// let num_padding_instances = table.num_padding_instances(); -// if num_padding_instances > 0 { -// let nthreads = -// std::env::var("RAYON_NUM_THREADS").map_or(8, |s| s.parse::().unwrap_or(8)); -// let padding_instance = vec![F::ZERO; num_cols]; -// let num_padding_instance_per_batch = if num_padding_instances > 256 { -// num_padding_instances.div_ceil(nthreads) -// } else { -// num_padding_instances -// }; -// table -// .par_batch_iter_padding_mut(num_instances, num_padding_instance_per_batch) -// .with_min_len(MIN_PAR_SIZE) -// .for_each(|row| { -// row.chunks_mut(num_cols) -// .for_each(|instance| instance.copy_from_slice(padding_instance.as_slice())); -// }); -// } -// } diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index c9f947fa3..574928daa 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -54,9 +54,6 @@ pub struct RowMajorMatrix { impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { - // let num_total_rows = next_pow2_instance_padding(num_rows); - // let num_padding_rows = num_total_rows - num_rows; - // println!("{}, {}", num_total_rows, num_col); RowMajorMatrix { values: vec![T::default(); num_rows * num_col], num_col, @@ -95,31 +92,6 @@ impl RowMajorMatrix { pub fn par_batch_iter_mut(&mut self, num_rows: usize) -> rayon::slice::ChunksMut { self.values.par_chunks_mut(num_rows * self.num_col) } - - // pub fn par_batch_iter_padding_mut( - // &mut self, - // num_instances: Option, - // batch_size: usize, - // ) -> rayon::slice::ChunksMut<'_, T> { - // let num_instances = num_instances.unwrap_or(self.num_instances()); - // self.values[num_instances * self.num_col..] - // .as_mut() - // .par_chunks_mut(batch_size * self.num_col) - // } - - // fn de_interleaving(mut self) -> Vec> { - // (0..self.num_col) - // .map(|i| { - // self.values - // .par_iter_mut() - // .skip(i) - // .step_by(self.num_col) - // //.map(|v| unsafe { mem::replace(v, mem::MaybeUninit::uninit()).assume_init() }) - // .map(|v| *v) - // .collect::>() - // }) - // .collect() - //} } impl RowMajorMatrix { From e5f0b6c627bd01f73fa00fc57f3cc4523032a238 Mon Sep 17 00:00:00 2001 From: Mihai Date: Fri, 22 Nov 2024 16:12:16 +0200 Subject: [PATCH 08/18] cargo fix --- ceno_zkvm/src/instructions.rs | 2 -- ceno_zkvm/src/instructions/riscv/divu.rs | 1 - ceno_zkvm/src/tables/mod.rs | 4 +--- ceno_zkvm/src/tables/ops/ops_circuit.rs | 4 ++-- ceno_zkvm/src/tables/range/range_circuit.rs | 4 ++-- ceno_zkvm/src/witness.rs | 10 +++------- multilinear_extensions/src/mle.rs | 2 +- 7 files changed, 9 insertions(+), 18 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 1ae0a2f45..375e3aed6 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -8,10 +8,8 @@ use rayon::{ use crate::{ circuit_builder::CircuitBuilder, error::ZKVMError, - scheme::constants::MIN_PAR_SIZE, witness::{LkMultiplicity, RowMajorMatrix}, }; -use ff::Field; pub mod riscv; diff --git a/ceno_zkvm/src/instructions/riscv/divu.rs b/ceno_zkvm/src/instructions/riscv/divu.rs index 2e1b0d8a8..0b44a85a0 100644 --- a/ceno_zkvm/src/instructions/riscv/divu.rs +++ b/ceno_zkvm/src/instructions/riscv/divu.rs @@ -172,7 +172,6 @@ mod test { use ceno_emul::{Change, InsnKind, StepRecord, Word, encode_rv32}; use goldilocks::GoldilocksExt2; use itertools::Itertools; - use multilinear_extensions::mle::IntoMLEs; use rand::Rng; use crate::{ diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index 9a794fe08..88a5834de 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -1,10 +1,8 @@ use crate::{ - circuit_builder::CircuitBuilder, error::ZKVMError, scheme::constants::MIN_PAR_SIZE, + circuit_builder::CircuitBuilder, error::ZKVMError, witness::RowMajorMatrix, }; use ff_ext::ExtensionField; -use goldilocks::SmallField; -use rayon::iter::{IndexedParallelIterator, ParallelIterator}; use std::collections::HashMap; mod range; pub use range::*; diff --git a/ceno_zkvm/src/tables/ops/ops_circuit.rs b/ceno_zkvm/src/tables/ops/ops_circuit.rs index 8a946b6ba..988c9eba6 100644 --- a/ceno_zkvm/src/tables/ops/ops_circuit.rs +++ b/ceno_zkvm/src/tables/ops/ops_circuit.rs @@ -51,7 +51,7 @@ impl TableCircuit for OpsTableCircuit num_fixed: usize, _input: &(), ) -> RowMajorMatrix { - let mut table = config.generate_fixed_traces(num_fixed, OP::content()); + let table = config.generate_fixed_traces(num_fixed, OP::content()); // Self::padding_zero(&mut table, num_fixed).expect("padding error"); table } @@ -63,7 +63,7 @@ impl TableCircuit for OpsTableCircuit _input: &(), ) -> Result, ZKVMError> { let multiplicity = &multiplicity[OP::ROM_TYPE as usize]; - let mut table = config.assign_instances(num_witin, multiplicity, OP::len())?; + let table = config.assign_instances(num_witin, multiplicity, OP::len())?; // Self::padding_zero(&mut table, num_witin)?; Ok(table) } diff --git a/ceno_zkvm/src/tables/range/range_circuit.rs b/ceno_zkvm/src/tables/range/range_circuit.rs index 81d9ac7ac..7146270ce 100644 --- a/ceno_zkvm/src/tables/range/range_circuit.rs +++ b/ceno_zkvm/src/tables/range/range_circuit.rs @@ -44,7 +44,7 @@ impl TableCircuit for RangeTableCircuit num_fixed: usize, _input: &(), ) -> RowMajorMatrix { - let mut table = config.generate_fixed_traces(num_fixed, RANGE::content()); + let table = config.generate_fixed_traces(num_fixed, RANGE::content()); // Self::padding_zero(&mut table, num_fixed).expect("padding error"); table } @@ -56,7 +56,7 @@ impl TableCircuit for RangeTableCircuit _input: &(), ) -> Result, ZKVMError> { let multiplicity = &multiplicity[RANGE::ROM_TYPE as usize]; - let mut table = config.assign_instances(num_witin, multiplicity, RANGE::len())?; + let table = config.assign_instances(num_witin, multiplicity, RANGE::len())?; // Self::padding_zero(&mut table, num_witin).expect("padding error"); Ok(table) } diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 574928daa..d24538c28 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -1,4 +1,3 @@ -use ark_std::iterable::Iterable; use ff::Field; use itertools::Itertools; use std::{ @@ -13,13 +12,10 @@ use std::{ time::Instant, }; -use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE, IntoMLEs}; +use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE}; use rayon::{ - iter::{ - IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, - IntoParallelRefMutIterator, ParallelIterator, - }, - slice::{ParallelSlice, ParallelSliceMut}, + iter::{IntoParallelRefIterator, ParallelIterator}, + slice::ParallelSliceMut, }; use thread_local::ThreadLocal; diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index f05efc98f..dc7b38d9c 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -103,7 +103,7 @@ pub trait IntoMLE: Sized { } impl IntoMLE> for Vec { - fn into_mle(mut self) -> DenseMultilinearExtension { + fn into_mle(self) -> DenseMultilinearExtension { let next_pow2 = self.len().next_power_of_two(); // self.resize(next_pow2, F::ZERO); assert!(self.len().is_power_of_two(), "{}", self.len()); From edec7894ed8cdc74227cab04b2960d23470f8740 Mon Sep 17 00:00:00 2001 From: Mihai Date: Mon, 25 Nov 2024 17:44:44 +0200 Subject: [PATCH 09/18] revert changes in virtual_poly --- ceno_zkvm/src/instructions.rs | 1 + ceno_zkvm/src/tables/ops/ops_impl.rs | 2 ++ ceno_zkvm/src/tables/program.rs | 2 ++ ceno_zkvm/src/tables/ram/ram_impl.rs | 2 ++ ceno_zkvm/src/tables/range/range_impl.rs | 2 ++ ceno_zkvm/src/witness.rs | 1 + multilinear_extensions/src/util.rs | 8 ++++++++ multilinear_extensions/src/virtual_poly.rs | 22 +++++++++++----------- 8 files changed, 29 insertions(+), 11 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 375e3aed6..a663c6f77 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -53,6 +53,7 @@ pub trait Instruction { } .max(1); let lk_multiplicity = LkMultiplicity::default(); + dbg!(steps.len(), num_witin); let mut raw_witin = RowMajorMatrix::::new(steps.len(), num_witin, Self::padding_strategy()); let raw_witin_iter = raw_witin.par_batch_iter_mut(num_instance_per_batch); diff --git a/ceno_zkvm/src/tables/ops/ops_impl.rs b/ceno_zkvm/src/tables/ops/ops_impl.rs index 4c34c5fec..f233ad8c3 100644 --- a/ceno_zkvm/src/tables/ops/ops_impl.rs +++ b/ceno_zkvm/src/tables/ops/ops_impl.rs @@ -48,6 +48,7 @@ impl OpTableConfig { num_fixed: usize, content: Vec<[u64; 3]>, ) -> RowMajorMatrix { + dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -70,6 +71,7 @@ impl OpTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { + dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 10bd4701d..0f1be6558 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -159,6 +159,7 @@ impl TableCircuit for ProgramTableCircuit { let pc_base = program.base_address; assert!(num_instructions <= config.program_size); + dbg!(config.program_size, num_fixed); let mut fixed = RowMajorMatrix::::new( config.program_size, num_fixed, @@ -198,6 +199,7 @@ impl TableCircuit for ProgramTableCircuit { prog_mlt[i] = *mlt; } + dbg!(config.program_size, num_witin); let mut witness = RowMajorMatrix::::new( config.program_size, num_witin, diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index 04af49df0..98aa77ad7 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -119,6 +119,7 @@ impl NonVolatileTableConfig::new( NVRAM::len(&self.params), num_fixed, @@ -388,6 +389,7 @@ impl DynVolatileRamTableConfig ) -> Result, ZKVMError> { assert!(final_mem.len() <= DVRAM::max_len(&self.params)); assert!(DVRAM::max_len(&self.params).is_power_of_two()); + dbg!(final_mem.len(), num_witness); let mut final_table = RowMajorMatrix::::new(final_mem.len(), num_witness, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/range/range_impl.rs b/ceno_zkvm/src/tables/range/range_impl.rs index 11191e22f..aefbef232 100644 --- a/ceno_zkvm/src/tables/range/range_impl.rs +++ b/ceno_zkvm/src/tables/range/range_impl.rs @@ -43,6 +43,7 @@ impl RangeTableConfig { num_fixed: usize, content: Vec, ) -> RowMajorMatrix { + dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -63,6 +64,7 @@ impl RangeTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { + dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index d24538c28..ac2dd2677 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -50,6 +50,7 @@ pub struct RowMajorMatrix { impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { + // assert!(false); RowMajorMatrix { values: vec![T::default(); num_rows * num_col], num_col, diff --git a/multilinear_extensions/src/util.rs b/multilinear_extensions/src/util.rs index 611ed4fb8..a0a8e56a2 100644 --- a/multilinear_extensions/src/util.rs +++ b/multilinear_extensions/src/util.rs @@ -1,3 +1,5 @@ +use std::mem::MaybeUninit; + /// Decompose an integer into a binary vector in little endian. pub fn bit_decompose(input: u64, num_var: usize) -> Vec { let mut res = Vec::with_capacity(num_var); @@ -18,6 +20,12 @@ pub fn ceil_log2(x: usize) -> usize { usize_bits - (x - 1).leading_zeros() as usize } +pub fn create_uninit_vec(len: usize) -> Vec> { + let mut vec: Vec> = Vec::with_capacity(len); + unsafe { vec.set_len(len) }; + vec +} + #[inline(always)] pub fn largest_even_below(n: usize) -> usize { if n % 2 == 0 { n } else { n.saturating_sub(1) } diff --git a/multilinear_extensions/src/virtual_poly.rs b/multilinear_extensions/src/virtual_poly.rs index 556444eb6..094d640db 100644 --- a/multilinear_extensions/src/virtual_poly.rs +++ b/multilinear_extensions/src/virtual_poly.rs @@ -1,8 +1,8 @@ -use std::{cmp::max, collections::HashMap, marker::PhantomData, sync::Arc}; +use std::{cmp::max, collections::HashMap, marker::PhantomData, mem::MaybeUninit, sync::Arc}; use crate::{ mle::{ArcDenseMultilinearExtension, DenseMultilinearExtension, MultilinearExtension}, - util::{bit_decompose, max_usable_threads}, + util::{bit_decompose, create_uninit_vec, max_usable_threads}, }; use ark_std::{end_timer, iterable::Iterable, rand::Rng, start_timer}; use ff::{Field, PrimeField}; @@ -383,16 +383,16 @@ pub fn build_eq_x_r_vec_sequential(r: &[E]) -> Vec { // 1 1 1 1 -> r0 * r1 * r2 * r3 // we will need 2^num_var evaluations - let mut evals = vec![E::ZERO; 1 << r.len()]; + let mut evals = create_uninit_vec(1 << r.len()); build_eq_x_r_helper_sequential(r, &mut evals, E::ONE); - evals + unsafe { std::mem::transmute(evals) } } /// A helper function to build eq(x, r)*init via dynamic programing tricks. /// This function takes 2^num_var iterations, and per iteration with 1 multiplication. -fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [E], init: E) { - buf[0] = init; +fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [MaybeUninit], init: E) { + buf[0] = MaybeUninit::new(init); for (i, r) in r.iter().rev().enumerate() { let next_size = 1 << (i + 1); @@ -402,10 +402,10 @@ fn build_eq_x_r_helper_sequential(r: &[E], buf: &mut [E], ini // buf[2*j + 1] = r * buf[j] // buf[2*j] = (1 - r) * buf[j] (0..next_size).step_by(2).rev().for_each(|index| { - let prev_val = buf[index >> 1]; + let prev_val = unsafe { buf[index >> 1].assume_init() }; let tmp = *r * prev_val; - buf[index + 1] = tmp; - buf[index] = prev_val - tmp; + buf[index + 1] = MaybeUninit::new(tmp); + buf[index] = MaybeUninit::new(prev_val - tmp); }); } } @@ -454,7 +454,7 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { build_eq_x_r_vec_sequential(r) } else { let eq_ts = build_eq_x_r_vec_sequential(&r[(r.len() - nbits)..]); - let mut ret = vec![E::ZERO; 1 << r.len()]; + let mut ret = create_uninit_vec(1 << r.len()); // eq(x, r) = eq(x_lo, r_lo) * eq(x_hi, r_hi) // where rlen = r.len(), x_lo = x[0..rlen-nbits], x_hi = x[rlen-nbits..] @@ -468,7 +468,7 @@ pub fn build_eq_x_r_vec(r: &[E]) -> Vec { build_eq_x_r_helper_sequential(&r[..(r.len() - nbits)], chunks, eq_t); }); - ret + unsafe { std::mem::transmute::>, Vec>(ret) } } } From bb388a21bb6c9282147fa862b7c4ce9f4936ce25 Mon Sep 17 00:00:00 2001 From: Mihai Date: Tue, 26 Nov 2024 14:13:46 +0200 Subject: [PATCH 10/18] handle repeat on empty --- ceno_zkvm/src/witness.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index ac2dd2677..3e39fbd90 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -97,10 +97,12 @@ impl RowMajorMatrix { ) -> Vec> { let start = Instant::now(); let padding_row = match self.padding_strategy { - InstancePaddingStrategy::RepeatLast => { + // If asked to repeat and actually have content to repeat + InstancePaddingStrategy::RepeatLast if self.values.len() > 0 => { self.values[self.values.len() - self.num_col..].to_vec() } - InstancePaddingStrategy::Zero => vec![F::ZERO; self.num_col], + // Otherwise zeros + _ => vec![F::ZERO; self.num_col], }; let num_padding = self.num_padding_instances(); let result = (0..self.num_col) From 5d1e4dfc5bc0164ac75b7800eb02ab7a4ebc5d27 Mon Sep 17 00:00:00 2001 From: Mihai Date: Thu, 5 Dec 2024 12:11:18 +0200 Subject: [PATCH 11/18] stash --- ceno_zkvm/src/instructions.rs | 2 +- ceno_zkvm/src/tables/ops/ops_impl.rs | 4 ++-- ceno_zkvm/src/tables/program.rs | 4 ++-- ceno_zkvm/src/tables/ram/ram_impl.rs | 4 ++-- ceno_zkvm/src/tables/range/range_impl.rs | 4 ++-- ceno_zkvm/src/witness.rs | 7 +++++-- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index d76adb948..7174318ea 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -53,7 +53,7 @@ pub trait Instruction { } .max(1); let lk_multiplicity = LkMultiplicity::default(); - dbg!(steps.len(), num_witin); + // dbg!(steps.len(), num_witin); let mut raw_witin = RowMajorMatrix::::new(steps.len(), num_witin, Self::padding_strategy()); let raw_witin_iter = raw_witin.par_batch_iter_mut(num_instance_per_batch); diff --git a/ceno_zkvm/src/tables/ops/ops_impl.rs b/ceno_zkvm/src/tables/ops/ops_impl.rs index f233ad8c3..54bc3f4f7 100644 --- a/ceno_zkvm/src/tables/ops/ops_impl.rs +++ b/ceno_zkvm/src/tables/ops/ops_impl.rs @@ -48,7 +48,7 @@ impl OpTableConfig { num_fixed: usize, content: Vec<[u64; 3]>, ) -> RowMajorMatrix { - dbg!(content.len(), num_fixed); + //dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -71,7 +71,7 @@ impl OpTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - dbg!(length, num_witin); + //dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 4d72d6ee6..3b4bb4537 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -159,7 +159,7 @@ impl TableCircuit for ProgramTableCircuit { let pc_base = program.base_address; assert!(num_instructions <= config.program_size); - dbg!(config.program_size, num_fixed); + // dbg!(config.program_size, num_fixed); let mut fixed = RowMajorMatrix::::new( config.program_size, num_fixed, @@ -199,7 +199,7 @@ impl TableCircuit for ProgramTableCircuit { prog_mlt[i] = *mlt; } - dbg!(config.program_size, num_witin); + // dbg!(config.program_size, num_witin); let mut witness = RowMajorMatrix::::new( config.program_size, num_witin, diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index aa3d0023b..616b933ba 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -119,7 +119,7 @@ impl NonVolatileTableConfig::new( NVRAM::len(&self.params), num_fixed, @@ -389,7 +389,7 @@ impl DynVolatileRamTableConfig ) -> Result, ZKVMError> { assert!(final_mem.len() <= DVRAM::max_len(&self.params)); assert!(DVRAM::max_len(&self.params).is_power_of_two()); - dbg!(final_mem.len(), num_witness); + //dbg!(final_mem.len(), num_witness); let mut final_table = RowMajorMatrix::::new(final_mem.len(), num_witness, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/range/range_impl.rs b/ceno_zkvm/src/tables/range/range_impl.rs index aefbef232..e9637e4bc 100644 --- a/ceno_zkvm/src/tables/range/range_impl.rs +++ b/ceno_zkvm/src/tables/range/range_impl.rs @@ -43,7 +43,7 @@ impl RangeTableConfig { num_fixed: usize, content: Vec, ) -> RowMajorMatrix { - dbg!(content.len(), num_fixed); + //dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -64,7 +64,7 @@ impl RangeTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - dbg!(length, num_witin); + //dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 3e39fbd90..23c387552 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -14,7 +14,7 @@ use std::{ use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE}; use rayon::{ - iter::{IntoParallelRefIterator, ParallelIterator}, + iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}, slice::ParallelSliceMut, }; use thread_local::ThreadLocal; @@ -52,7 +52,10 @@ impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { // assert!(false); RowMajorMatrix { - values: vec![T::default(); num_rows * num_col], + values: (0..num_rows * num_col) + .into_par_iter() + .map(|_| T::default()) + .collect(), num_col, padding_strategy, } From 50d54268d787f67d798cc8bf10eaa6d4ae67f51d Mon Sep 17 00:00:00 2001 From: Mihai Date: Mon, 9 Dec 2024 13:03:05 +0200 Subject: [PATCH 12/18] clean up --- ceno_zkvm/src/witness.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 23c387552..7820894c6 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -61,9 +61,9 @@ impl RowMajorMatrix { } } - pub fn len(&self) -> usize { - self.values.len() - } + // pub fn len(&self) -> usize { + // self.values.len() + // } pub fn values(&mut self) -> &mut Vec { &mut self.values @@ -98,7 +98,6 @@ impl RowMajorMatrix { pub fn into_mles>( self, ) -> Vec> { - let start = Instant::now(); let padding_row = match self.padding_strategy { // If asked to repeat and actually have content to repeat InstancePaddingStrategy::RepeatLast if self.values.len() > 0 => { @@ -122,11 +121,6 @@ impl RowMajorMatrix { .into_mle() }) .collect(); - let size = self.num_col * self.len(); - if size > 1000 * 1000 { - let duration = start.elapsed().as_secs_f64(); - println!("Time taken: {:?}, size: {:?}", duration, size); - } result } } From 239eee224a651caa4db2584afa1a90cc88595b60 Mon Sep 17 00:00:00 2001 From: Mihai Date: Mon, 9 Dec 2024 13:12:57 +0200 Subject: [PATCH 13/18] restore tests --- ceno_zkvm/src/tables/ops.rs | 37 ++++++++-------- ceno_zkvm/src/tables/program.rs | 76 ++++++++++++++++----------------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/ceno_zkvm/src/tables/ops.rs b/ceno_zkvm/src/tables/ops.rs index a5af18328..d6da1de33 100644 --- a/ceno_zkvm/src/tables/ops.rs +++ b/ceno_zkvm/src/tables/ops.rs @@ -112,24 +112,21 @@ mod tests { }; use goldilocks::{GoldilocksExt2 as E, SmallField}; - // #[test] - // fn test_ops_pow_table_assign() { - // let mut cs = ConstraintSystem::::new(|| "riscv"); - // let mut cb = CircuitBuilder::new(&mut cs); - - // let config = PowTableCircuit::::construct_circuit(&mut cb).unwrap(); - - // let fixed = PowTableCircuit::::generate_fixed_traces(&config, cb.cs.num_fixed, &()); - - // for (i, row) in fixed.iter_rows().enumerate() { - // let (base, exp) = PowTable::unpack(i as u64); - // assert_eq!(PowTable::pack(base, exp), i as u64); - // assert_eq!(base, unsafe { row[0].assume_init() }.to_canonical_u64()); - // assert_eq!(exp, unsafe { row[1].assume_init() }.to_canonical_u64()); - // assert_eq!( - // base.pow(exp.try_into().unwrap()), - // unsafe { row[2].assume_init() }.to_canonical_u64() - // ); - // } - // } + #[test] + fn test_ops_pow_table_assign() { + let mut cs = ConstraintSystem::::new(|| "riscv"); + let mut cb = CircuitBuilder::new(&mut cs); + + let config = PowTableCircuit::::construct_circuit(&mut cb).unwrap(); + + let fixed = PowTableCircuit::::generate_fixed_traces(&config, cb.cs.num_fixed, &()); + + for (i, row) in fixed.iter_rows().enumerate() { + let (base, exp) = PowTable::unpack(i as u64); + assert_eq!(PowTable::pack(base, exp), i as u64); + assert_eq!(base, row[0].to_canonical_u64()); + assert_eq!(exp, row[1].to_canonical_u64()); + assert_eq!(base.pow(exp.try_into().unwrap()), row[2].to_canonical_u64()); + } + } } diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 3b4bb4537..288f27548 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -249,42 +249,42 @@ mod tests { } } - // #[test] - // fn test_program_padding() { - // let mut cs = ConstraintSystem::::new(|| "riscv"); - // let mut cb = CircuitBuilder::new(&mut cs); - - // let actual_len = 3; - // let instructions = vec![encode_rv32(ADD, 1, 2, 3, 0); actual_len]; - // let program = Program::new(0x2000_0000, 0x2000_0000, instructions, Default::default()); - - // let config = ProgramTableCircuit::construct_circuit(&mut cb).unwrap(); - - // let check = |matrix: &RowMajorMatrix| { - // assert_eq!( - // matrix.num_instances() + matrix.num_padding_instances(), - // cb.params.program_size - // ); - // for row in matrix.iter_rows().skip(actual_len) { - // for col in row.iter() { - // assert_eq!(unsafe { col.assume_init() }, F::ZERO); - // } - // } - // }; - - // let fixed = - // ProgramTableCircuit::::generate_fixed_traces(&config, cb.cs.num_fixed, &program); - // check(&fixed); - - // let lkm = LkMultiplicity::default().into_finalize_result(); - - // let witness = ProgramTableCircuit::::assign_instances( - // &config, - // cb.cs.num_witin as usize, - // &lkm, - // &program, - // ) - // .unwrap(); - // check(&witness); - // } + #[test] + fn test_program_padding() { + let mut cs = ConstraintSystem::::new(|| "riscv"); + let mut cb = CircuitBuilder::new(&mut cs); + + let actual_len = 3; + let instructions = vec![encode_rv32(ADD, 1, 2, 3, 0); actual_len]; + let program = Program::new(0x2000_0000, 0x2000_0000, instructions, Default::default()); + + let config = ProgramTableCircuit::construct_circuit(&mut cb).unwrap(); + + let check = |matrix: &RowMajorMatrix| { + assert_eq!( + matrix.num_instances() + matrix.num_padding_instances(), + cb.params.program_size + ); + for row in matrix.iter_rows().skip(actual_len) { + for col in row.iter() { + assert_eq!(*col, F::ZERO); + } + } + }; + + let fixed = + ProgramTableCircuit::::generate_fixed_traces(&config, cb.cs.num_fixed, &program); + check(&fixed); + + let lkm = LkMultiplicity::default().into_finalize_result(); + + let witness = ProgramTableCircuit::::assign_instances( + &config, + cb.cs.num_witin as usize, + &lkm, + &program, + ) + .unwrap(); + check(&witness); + } } From d93b3906f6c6f3244096c24c1cdd92b800c43900 Mon Sep 17 00:00:00 2001 From: Mihai Date: Mon, 9 Dec 2024 13:22:28 +0200 Subject: [PATCH 14/18] clean-up --- ceno_zkvm/src/instructions/riscv/insn_base.rs | 1 - ceno_zkvm/src/tables/mod.rs | 5 +---- ceno_zkvm/src/tables/ops/ops_circuit.rs | 4 +--- ceno_zkvm/src/tables/range/range_circuit.rs | 8 ++------ ceno_zkvm/src/witness.rs | 15 +++------------ multilinear_extensions/src/mle.rs | 3 --- multilinear_extensions/src/virtual_poly.rs | 2 +- 7 files changed, 8 insertions(+), 30 deletions(-) diff --git a/ceno_zkvm/src/instructions/riscv/insn_base.rs b/ceno_zkvm/src/instructions/riscv/insn_base.rs index abe810c26..10cb4c1b7 100644 --- a/ceno_zkvm/src/instructions/riscv/insn_base.rs +++ b/ceno_zkvm/src/instructions/riscv/insn_base.rs @@ -470,7 +470,6 @@ impl MemAddr { mod test { use goldilocks::{Goldilocks as F, GoldilocksExt2 as E}; use itertools::Itertools; - use multilinear_extensions::mle::IntoMLEs; use crate::{ ROMType, diff --git a/ceno_zkvm/src/tables/mod.rs b/ceno_zkvm/src/tables/mod.rs index 1801910a4..ef1a4fd68 100644 --- a/ceno_zkvm/src/tables/mod.rs +++ b/ceno_zkvm/src/tables/mod.rs @@ -1,9 +1,6 @@ use crate::{circuit_builder::CircuitBuilder, error::ZKVMError, witness::RowMajorMatrix}; -use ff::Field; use ff_ext::ExtensionField; -use multilinear_extensions::util::max_usable_threads; -use rayon::iter::{IndexedParallelIterator, ParallelIterator}; -use std::{collections::HashMap, mem::MaybeUninit}; +use std::collections::HashMap; mod range; pub use range::*; diff --git a/ceno_zkvm/src/tables/ops/ops_circuit.rs b/ceno_zkvm/src/tables/ops/ops_circuit.rs index 988c9eba6..5813f3894 100644 --- a/ceno_zkvm/src/tables/ops/ops_circuit.rs +++ b/ceno_zkvm/src/tables/ops/ops_circuit.rs @@ -51,9 +51,7 @@ impl TableCircuit for OpsTableCircuit num_fixed: usize, _input: &(), ) -> RowMajorMatrix { - let table = config.generate_fixed_traces(num_fixed, OP::content()); - // Self::padding_zero(&mut table, num_fixed).expect("padding error"); - table + config.generate_fixed_traces(num_fixed, OP::content()) } fn assign_instances( diff --git a/ceno_zkvm/src/tables/range/range_circuit.rs b/ceno_zkvm/src/tables/range/range_circuit.rs index 7146270ce..d5fdf7363 100644 --- a/ceno_zkvm/src/tables/range/range_circuit.rs +++ b/ceno_zkvm/src/tables/range/range_circuit.rs @@ -44,9 +44,7 @@ impl TableCircuit for RangeTableCircuit num_fixed: usize, _input: &(), ) -> RowMajorMatrix { - let table = config.generate_fixed_traces(num_fixed, RANGE::content()); - // Self::padding_zero(&mut table, num_fixed).expect("padding error"); - table + config.generate_fixed_traces(num_fixed, RANGE::content()) } fn assign_instances( @@ -56,8 +54,6 @@ impl TableCircuit for RangeTableCircuit _input: &(), ) -> Result, ZKVMError> { let multiplicity = &multiplicity[RANGE::ROM_TYPE as usize]; - let table = config.assign_instances(num_witin, multiplicity, RANGE::len())?; - // Self::padding_zero(&mut table, num_witin).expect("padding error"); - Ok(table) + config.assign_instances(num_witin, multiplicity, RANGE::len()) } } diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 7820894c6..94e9cda6b 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -9,7 +9,6 @@ use std::{ ops::Index, slice::{Chunks, ChunksMut}, sync::Arc, - time::Instant, }; use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE}; @@ -61,16 +60,8 @@ impl RowMajorMatrix { } } - // pub fn len(&self) -> usize { - // self.values.len() - // } - - pub fn values(&mut self) -> &mut Vec { - &mut self.values - } - pub fn num_padding_instances(&self) -> usize { - return next_pow2_instance_padding(self.num_instances()) - self.num_instances(); + next_pow2_instance_padding(self.num_instances()) - self.num_instances() } pub fn num_instances(&self) -> usize { @@ -100,7 +91,7 @@ impl RowMajorMatrix { ) -> Vec> { let padding_row = match self.padding_strategy { // If asked to repeat and actually have content to repeat - InstancePaddingStrategy::RepeatLast if self.values.len() > 0 => { + InstancePaddingStrategy::RepeatLast if !self.values.is_empty() => { self.values[self.values.len() - self.num_col..].to_vec() } // Otherwise zeros @@ -116,7 +107,7 @@ impl RowMajorMatrix { .skip(*i) .step_by(self.num_col) .chain(&mut iter::repeat(&padding_row[*i]).take(num_padding)) - .map(|val| *val) + .copied() .collect::>() .into_mle() }) diff --git a/multilinear_extensions/src/mle.rs b/multilinear_extensions/src/mle.rs index dc7b38d9c..b4e8df983 100644 --- a/multilinear_extensions/src/mle.rs +++ b/multilinear_extensions/src/mle.rs @@ -105,7 +105,6 @@ pub trait IntoMLE: Sized { impl IntoMLE> for Vec { fn into_mle(self) -> DenseMultilinearExtension { let next_pow2 = self.len().next_power_of_two(); - // self.resize(next_pow2, F::ZERO); assert!(self.len().is_power_of_two(), "{}", self.len()); DenseMultilinearExtension::from_evaluation_vec_smart::(ceil_log2(next_pow2), self) } @@ -119,8 +118,6 @@ impl> IntoMLEs> { fn into_mles(self) -> Vec> { - // TODO: figure out correct place to test - // assert!(self.len().is_power_of_two(), "{}", self.len()); self.into_iter().map(|v| v.into_mle()).collect() } } diff --git a/multilinear_extensions/src/virtual_poly.rs b/multilinear_extensions/src/virtual_poly.rs index 094d640db..72c5b2649 100644 --- a/multilinear_extensions/src/virtual_poly.rs +++ b/multilinear_extensions/src/virtual_poly.rs @@ -313,7 +313,7 @@ impl VirtualPolynomial { let mut flattened_ml_extensions = vec![]; let mut hm = HashMap::new(); for mle in self.flattened_ml_extensions.iter() { - let mle_ptr = Arc::as_ptr(&mle) as usize; + let mle_ptr = Arc::as_ptr(mle) as usize; let index = self.raw_pointers_lookup_table.get(&mle_ptr).unwrap(); let mle_ext_field = mle.as_ref().to_ext_field(); From 3af1ef10779832e7e0183aee3ffbc910af857ba9 Mon Sep 17 00:00:00 2001 From: Mihai Date: Tue, 10 Dec 2024 09:35:30 +0200 Subject: [PATCH 15/18] remove sltu.rs (present due to imprudent merge) --- ceno_zkvm/src/instructions/riscv/sltu.rs | 180 ----------------------- 1 file changed, 180 deletions(-) delete mode 100644 ceno_zkvm/src/instructions/riscv/sltu.rs diff --git a/ceno_zkvm/src/instructions/riscv/sltu.rs b/ceno_zkvm/src/instructions/riscv/sltu.rs deleted file mode 100644 index ecab275de..000000000 --- a/ceno_zkvm/src/instructions/riscv/sltu.rs +++ /dev/null @@ -1,180 +0,0 @@ -use std::marker::PhantomData; - -use ceno_emul::{InsnKind, StepRecord}; -use ff_ext::ExtensionField; - -use super::{ - RIVInstruction, - constants::{UINT_LIMBS, UInt}, - r_insn::RInstructionConfig, -}; -use crate::{ - circuit_builder::CircuitBuilder, error::ZKVMError, gadgets::IsLtConfig, - instructions::Instruction, uint::Value, witness::LkMultiplicity, -}; - -/// This config handles R-Instructions that represent registers values as 2 * u16. -#[derive(Debug)] -pub struct ArithConfig { - r_insn: RInstructionConfig, - - rs1_read: UInt, - rs2_read: UInt, - #[cfg_attr(not(test), allow(dead_code))] - rd_written: UInt, - - is_lt: IsLtConfig, -} - -pub struct ArithInstruction(PhantomData<(E, I)>); - -pub struct SLTUOp; -impl RIVInstruction for SLTUOp { - const INST_KIND: InsnKind = InsnKind::SLTU; -} -pub type SltuInstruction = ArithInstruction; - -// TODO combine with SLT -impl Instruction for ArithInstruction { - type InstructionConfig = ArithConfig; - - fn name() -> String { - format!("{:?}", I::INST_KIND) - } - - fn construct_circuit( - circuit_builder: &mut CircuitBuilder, - ) -> Result { - // If rs1_read < rs2_read, rd_written = 1. Otherwise rd_written = 0 - let rs1_read = UInt::new_unchecked(|| "rs1_read", circuit_builder)?; - let rs2_read = UInt::new_unchecked(|| "rs2_read", circuit_builder)?; - - let lt = IsLtConfig::construct_circuit( - circuit_builder, - || "rs1 < rs2", - rs1_read.value(), - rs2_read.value(), - UINT_LIMBS, - )?; - let rd_written = UInt::from_exprs_unchecked(vec![lt.expr()]); - - let r_insn = RInstructionConfig::::construct_circuit( - circuit_builder, - I::INST_KIND, - rs1_read.register_expr(), - rs2_read.register_expr(), - rd_written.register_expr(), - )?; - - Ok(ArithConfig { - r_insn, - rs1_read, - rs2_read, - rd_written, - is_lt: lt, - }) - } - - fn assign_instance( - config: &Self::InstructionConfig, - instance: &mut [::BaseField], - lkm: &mut LkMultiplicity, - step: &StepRecord, - ) -> Result<(), ZKVMError> { - config.r_insn.assign_instance(instance, lkm, step)?; - - let rs1 = step.rs1().unwrap().value; - let rs2 = step.rs2().unwrap().value; - - let rs1_read = Value::new_unchecked(rs1); - let rs2_read = Value::new_unchecked(rs2); - config - .rs1_read - .assign_limbs(instance, rs1_read.as_u16_limbs()); - config - .rs2_read - .assign_limbs(instance, rs2_read.as_u16_limbs()); - config - .is_lt - .assign_instance(instance, lkm, rs1.into(), rs2.into())?; - - Ok(()) - } -} - -#[cfg(test)] -mod test { - use ceno_emul::{Change, StepRecord, Word, encode_rv32}; - use goldilocks::GoldilocksExt2; - use rand::Rng; - - use super::*; - use crate::{ - circuit_builder::{CircuitBuilder, ConstraintSystem}, - instructions::Instruction, - scheme::mock_prover::{MOCK_PC_START, MockProver}, - }; - - fn verify(name: &'static str, rs1: Word, rs2: Word, rd: Word) { - let mut cs = ConstraintSystem::::new(|| "riscv"); - let mut cb = CircuitBuilder::new(&mut cs); - let config = cb - .namespace( - || format!("SLTU/{name}"), - |cb| { - let config = SltuInstruction::construct_circuit(cb); - Ok(config) - }, - ) - .unwrap() - .unwrap(); - - let insn_code = encode_rv32(InsnKind::SLTU, 2, 3, 4, 0); - let (raw_witin, lkm) = - SltuInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![ - StepRecord::new_r_instruction( - 3, - MOCK_PC_START, - insn_code, - rs1, - rs2, - Change::new(0, rd), - 0, - ), - ]) - .unwrap(); - - let expected_rd_written = - UInt::from_const_unchecked(Value::new_unchecked(rd).as_u16_limbs().to_vec()); - - config - .rd_written - .require_equal(|| "assert_rd_written", &mut cb, &expected_rd_written) - .unwrap(); - - MockProver::assert_satisfied_raw(&cb, raw_witin, &[insn_code], None, Some(lkm)); - } - - #[test] - fn test_sltu_simple() { - verify("lt = true, 0 < 1", 0, 1, 1); - verify("lt = true, 1 < 2", 1, 2, 1); - verify("lt = true, 0 < u32::MAX", 0, u32::MAX, 1); - verify("lt = true, u32::MAX - 1", u32::MAX - 1, u32::MAX, 1); - verify("lt = false, u32::MAX", u32::MAX, u32::MAX, 0); - verify("lt = false, u32::MAX - 1", u32::MAX, u32::MAX - 1, 0); - verify("lt = false, u32::MAX > 0", u32::MAX, 0, 0); - verify("lt = false, 2 > 1", 2, 1, 0); - } - - #[test] - fn test_sltu_random() { - // TODO(Matthias): use property pased testing. - // Like eg https://docs.rs/proptest/latest/proptest/ - let mut rng = rand::thread_rng(); - let a: u32 = rng.gen(); - let b: u32 = rng.gen(); - verify("random 1", a, b, (a < b) as u32); - verify("random 2", b, a, (a >= b) as u32); - } -} From e13ef15bbbdfc8a02c24623645f00efa60e8f848 Mon Sep 17 00:00:00 2001 From: Mihai Date: Tue, 10 Dec 2024 09:42:07 +0200 Subject: [PATCH 16/18] slight clean-up --- ceno_zkvm/src/instructions.rs | 1 - ceno_zkvm/src/tables/ops/ops_impl.rs | 2 -- ceno_zkvm/src/tables/program.rs | 4 ---- ceno_zkvm/src/tables/ram/ram_impl.rs | 2 -- ceno_zkvm/src/tables/range/range_impl.rs | 2 -- ceno_zkvm/src/witness.rs | 15 ++++++--------- 6 files changed, 6 insertions(+), 20 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index 7174318ea..bb3109d82 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -53,7 +53,6 @@ pub trait Instruction { } .max(1); let lk_multiplicity = LkMultiplicity::default(); - // dbg!(steps.len(), num_witin); let mut raw_witin = RowMajorMatrix::::new(steps.len(), num_witin, Self::padding_strategy()); let raw_witin_iter = raw_witin.par_batch_iter_mut(num_instance_per_batch); diff --git a/ceno_zkvm/src/tables/ops/ops_impl.rs b/ceno_zkvm/src/tables/ops/ops_impl.rs index 54bc3f4f7..4c34c5fec 100644 --- a/ceno_zkvm/src/tables/ops/ops_impl.rs +++ b/ceno_zkvm/src/tables/ops/ops_impl.rs @@ -48,7 +48,6 @@ impl OpTableConfig { num_fixed: usize, content: Vec<[u64; 3]>, ) -> RowMajorMatrix { - //dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -71,7 +70,6 @@ impl OpTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - //dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/program.rs b/ceno_zkvm/src/tables/program.rs index 288f27548..2ff4dd5d5 100644 --- a/ceno_zkvm/src/tables/program.rs +++ b/ceno_zkvm/src/tables/program.rs @@ -159,7 +159,6 @@ impl TableCircuit for ProgramTableCircuit { let pc_base = program.base_address; assert!(num_instructions <= config.program_size); - // dbg!(config.program_size, num_fixed); let mut fixed = RowMajorMatrix::::new( config.program_size, num_fixed, @@ -181,7 +180,6 @@ impl TableCircuit for ProgramTableCircuit { } }); - // Self::padding_zero(&mut fixed, num_fixed).expect("padding error"); fixed } @@ -199,7 +197,6 @@ impl TableCircuit for ProgramTableCircuit { prog_mlt[i] = *mlt; } - // dbg!(config.program_size, num_witin); let mut witness = RowMajorMatrix::::new( config.program_size, num_witin, @@ -213,7 +210,6 @@ impl TableCircuit for ProgramTableCircuit { set_val!(row, config.mlt, E::BaseField::from(mlt as u64)); }); - // Self::padding_zero(&mut witness, num_witin).expect("padding error"); Ok(witness) } } diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index 616b933ba..b824c2646 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -119,7 +119,6 @@ impl NonVolatileTableConfig::new( NVRAM::len(&self.params), num_fixed, @@ -389,7 +388,6 @@ impl DynVolatileRamTableConfig ) -> Result, ZKVMError> { assert!(final_mem.len() <= DVRAM::max_len(&self.params)); assert!(DVRAM::max_len(&self.params).is_power_of_two()); - //dbg!(final_mem.len(), num_witness); let mut final_table = RowMajorMatrix::::new(final_mem.len(), num_witness, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/tables/range/range_impl.rs b/ceno_zkvm/src/tables/range/range_impl.rs index e9637e4bc..11191e22f 100644 --- a/ceno_zkvm/src/tables/range/range_impl.rs +++ b/ceno_zkvm/src/tables/range/range_impl.rs @@ -43,7 +43,6 @@ impl RangeTableConfig { num_fixed: usize, content: Vec, ) -> RowMajorMatrix { - //dbg!(content.len(), num_fixed); let mut fixed = RowMajorMatrix::::new(content.len(), num_fixed, InstancePaddingStrategy::Zero); @@ -64,7 +63,6 @@ impl RangeTableConfig { multiplicity: &HashMap, length: usize, ) -> Result, ZKVMError> { - //dbg!(length, num_witin); let mut witness = RowMajorMatrix::::new(length, num_witin, InstancePaddingStrategy::Zero); diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 94e9cda6b..6185b318b 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -1,5 +1,4 @@ use ff::Field; -use itertools::Itertools; use std::{ array, cell::RefCell, @@ -13,7 +12,7 @@ use std::{ use multilinear_extensions::mle::{DenseMultilinearExtension, IntoMLE}; use rayon::{ - iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}, + iter::{IntoParallelIterator, ParallelIterator}, slice::ParallelSliceMut, }; use thread_local::ThreadLocal; @@ -49,7 +48,6 @@ pub struct RowMajorMatrix { impl RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { - // assert!(false); RowMajorMatrix { values: (0..num_rows * num_col) .into_par_iter() @@ -90,23 +88,22 @@ impl RowMajorMatrix { self, ) -> Vec> { let padding_row = match self.padding_strategy { - // If asked to repeat and actually have content to repeat + // Repeat last row if it exists InstancePaddingStrategy::RepeatLast if !self.values.is_empty() => { self.values[self.values.len() - self.num_col..].to_vec() } - // Otherwise zeros + // Otherwise use zeroes _ => vec![F::ZERO; self.num_col], }; let num_padding = self.num_padding_instances(); let result = (0..self.num_col) - .collect_vec() - .par_iter() + .into_par_iter() .map(|i| { self.values .iter() - .skip(*i) + .skip(i) .step_by(self.num_col) - .chain(&mut iter::repeat(&padding_row[*i]).take(num_padding)) + .chain(&mut iter::repeat(&padding_row[i]).take(num_padding)) .copied() .collect::>() .into_mle() From 0953c7e24974521ff50047a0be4f9b4addc65531 Mon Sep 17 00:00:00 2001 From: Mihai Date: Tue, 10 Dec 2024 15:04:18 +0200 Subject: [PATCH 17/18] address some comments --- ceno_zkvm/src/tables/ops/ops_circuit.rs | 4 +--- ceno_zkvm/src/witness.rs | 5 ++--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ceno_zkvm/src/tables/ops/ops_circuit.rs b/ceno_zkvm/src/tables/ops/ops_circuit.rs index 5813f3894..852702ea7 100644 --- a/ceno_zkvm/src/tables/ops/ops_circuit.rs +++ b/ceno_zkvm/src/tables/ops/ops_circuit.rs @@ -61,8 +61,6 @@ impl TableCircuit for OpsTableCircuit _input: &(), ) -> Result, ZKVMError> { let multiplicity = &multiplicity[OP::ROM_TYPE as usize]; - let table = config.assign_instances(num_witin, multiplicity, OP::len())?; - // Self::padding_zero(&mut table, num_witin)?; - Ok(table) + config.assign_instances(num_witin, multiplicity, OP::len()) } } diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 6185b318b..79cb6a9ba 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -96,7 +96,7 @@ impl RowMajorMatrix { _ => vec![F::ZERO; self.num_col], }; let num_padding = self.num_padding_instances(); - let result = (0..self.num_col) + (0..self.num_col) .into_par_iter() .map(|i| { self.values @@ -108,8 +108,7 @@ impl RowMajorMatrix { .collect::>() .into_mle() }) - .collect(); - result + .collect() } } From 94001c969b8a52f2160df1213c2f3de79e40f853 Mon Sep 17 00:00:00 2001 From: Mihai Date: Wed, 11 Dec 2024 08:01:52 +0200 Subject: [PATCH 18/18] incorporate well-formed address padding and add unit test --- ceno_zkvm/src/instructions.rs | 5 ++ ceno_zkvm/src/tables/ram/ram_impl.rs | 78 ++++++++++++++++++++++++++-- ceno_zkvm/src/witness.rs | 52 +++++++++++-------- 3 files changed, 110 insertions(+), 25 deletions(-) diff --git a/ceno_zkvm/src/instructions.rs b/ceno_zkvm/src/instructions.rs index bb3109d82..a6533ad22 100644 --- a/ceno_zkvm/src/instructions.rs +++ b/ceno_zkvm/src/instructions.rs @@ -5,6 +5,7 @@ use rayon::{ iter::{IndexedParallelIterator, ParallelIterator}, slice::ParallelSlice, }; +use std::sync::Arc; use crate::{ circuit_builder::CircuitBuilder, @@ -18,6 +19,10 @@ pub mod riscv; pub enum InstancePaddingStrategy { Zero, RepeatLast, + // Custom strategy consists of a closure + // `pad(i, j) = padding value for cell at row i, column j` + // pad should be able to cross thread boundaries + Custom(Arc u64 + Send + Sync>), } pub trait Instruction { diff --git a/ceno_zkvm/src/tables/ram/ram_impl.rs b/ceno_zkvm/src/tables/ram/ram_impl.rs index b824c2646..943baeb69 100644 --- a/ceno_zkvm/src/tables/ram/ram_impl.rs +++ b/ceno_zkvm/src/tables/ram/ram_impl.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use std::{marker::PhantomData, sync::Arc}; use ceno_emul::{Addr, Cycle}; use ff_ext::ExtensionField; @@ -388,8 +388,23 @@ impl DynVolatileRamTableConfig ) -> Result, ZKVMError> { assert!(final_mem.len() <= DVRAM::max_len(&self.params)); assert!(DVRAM::max_len(&self.params).is_power_of_two()); - let mut final_table = - RowMajorMatrix::::new(final_mem.len(), num_witness, InstancePaddingStrategy::Zero); + + let params = self.params.clone(); + // TODO: make this more robust + let addr_column = 0; + let padding_fn = move |row: u64, col: u64| { + if col == addr_column { + DVRAM::addr(¶ms, row as usize) as u64 + } else { + 0u64 + } + }; + + let mut final_table = RowMajorMatrix::::new( + final_mem.len(), + num_witness, + InstancePaddingStrategy::Custom(Arc::new(padding_fn)), + ); final_table .par_iter_mut() @@ -416,3 +431,60 @@ impl DynVolatileRamTableConfig Ok(final_table) } } + +#[cfg(test)] +mod tests { + use std::iter::successors; + + use crate::{ + circuit_builder::{CircuitBuilder, ConstraintSystem}, + structs::ProgramParams, + tables::{DynVolatileRamTable, HintsCircuit, HintsTable, MemFinalRecord, TableCircuit}, + utils::next_pow2_instance_padding, + witness::LkMultiplicity, + }; + + use ceno_emul::WORD_SIZE; + use goldilocks::{Goldilocks as F, GoldilocksExt2 as E}; + use itertools::Itertools; + + #[test] + fn test_well_formed_address_padding() { + let mut cs = ConstraintSystem::::new(|| "riscv"); + let mut cb = CircuitBuilder::new(&mut cs); + let config = HintsCircuit::construct_circuit(&mut cb).unwrap(); + + let def_params = ProgramParams::default(); + let lkm = LkMultiplicity::default().into_finalize_result(); + + // ensure non-empty padding is required + let some_non_2_pow = 26; + let input = (0..some_non_2_pow) + .map(|i| MemFinalRecord { + addr: HintsTable::addr(&def_params, i), + cycle: 0, + value: 0, + }) + .collect_vec(); + let wit = + HintsCircuit::::assign_instances(&config, cb.cs.num_witin as usize, &lkm, &input) + .unwrap(); + + let addr_column = cb + .cs + .witin_namespace_map + .iter() + .position(|name| name == "riscv/RAM_Memory_HintsTable/addr") + .unwrap(); + + let addr_padded_view = wit.column_padded(addr_column); + // Expect addresses to proceed consecutively inside the padding as well + let expected = successors(Some(addr_padded_view[0]), |idx| { + Some(*idx + F::from(WORD_SIZE as u64)) + }) + .take(next_pow2_instance_padding(wit.num_instances())) + .collect::>(); + + assert_eq!(addr_padded_view, expected) + } +} diff --git a/ceno_zkvm/src/witness.rs b/ceno_zkvm/src/witness.rs index 79cb6a9ba..134a66689 100644 --- a/ceno_zkvm/src/witness.rs +++ b/ceno_zkvm/src/witness.rs @@ -3,7 +3,6 @@ use std::{ array, cell::RefCell, collections::HashMap, - iter, mem::{self}, ops::Index, slice::{Chunks, ChunksMut}, @@ -46,7 +45,7 @@ pub struct RowMajorMatrix { padding_strategy: InstancePaddingStrategy, } -impl RowMajorMatrix { +impl> RowMajorMatrix { pub fn new(num_rows: usize, num_col: usize, padding_strategy: InstancePaddingStrategy) -> Self { RowMajorMatrix { values: (0..num_rows * num_col) @@ -66,6 +65,10 @@ impl RowMajorMatrix { self.values.len() / self.num_col } + pub fn row(&self, row: usize) -> &[T] { + &self.values[row * self.num_col..(row + 1) * self.num_col] + } + pub fn iter_rows(&self) -> Chunks { self.values.chunks(self.num_col) } @@ -83,31 +86,36 @@ impl RowMajorMatrix { } } -impl RowMajorMatrix { +impl> RowMajorMatrix { + // Returns column number `column`, padded appropriately according to the stored strategy + pub fn column_padded(&self, column: usize) -> Vec { + let num_instances = self.num_instances(); + let num_padding_instances = self.num_padding_instances(); + let last_element = self.row(num_instances - 1)[column]; + + let padding_iter = (num_instances..num_instances + num_padding_instances).map(|i| { + match &self.padding_strategy { + InstancePaddingStrategy::Custom(fun) => F::from(fun(i as u64, column as u64)), + InstancePaddingStrategy::RepeatLast => last_element, + _ => F::ZERO, + } + }); + + self.values + .iter() + .skip(column) + .step_by(self.num_col) + .copied() + .chain(padding_iter) + .collect::>() + } + pub fn into_mles>( self, ) -> Vec> { - let padding_row = match self.padding_strategy { - // Repeat last row if it exists - InstancePaddingStrategy::RepeatLast if !self.values.is_empty() => { - self.values[self.values.len() - self.num_col..].to_vec() - } - // Otherwise use zeroes - _ => vec![F::ZERO; self.num_col], - }; - let num_padding = self.num_padding_instances(); (0..self.num_col) .into_par_iter() - .map(|i| { - self.values - .iter() - .skip(i) - .step_by(self.num_col) - .chain(&mut iter::repeat(&padding_row[i]).take(num_padding)) - .copied() - .collect::>() - .into_mle() - }) + .map(|i| self.column_padded(i).into_mle()) .collect() } }