Skip to content

Commit

Permalink
upgrade to latest plonky3
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 20, 2025
1 parent 81e67c2 commit 5c4e093
Show file tree
Hide file tree
Showing 57 changed files with 419 additions and 424 deletions.
214 changes: 154 additions & 60 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ crossbeam-channel = "0.5"
itertools = "0.13"
num-derive = "0.4"
num-traits = "0.2"
p3-challenger = { git = "https://github.com/plonky3/plonky3", rev = "b0591e9b" }
p3-field = { git = "https://github.com/plonky3/plonky3", rev = "b0591e9b" }
p3-goldilocks = { git = "https://github.com/plonky3/plonky3", rev = "b0591e9b" }
p3-mds = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b0591e9b" }
p3-poseidon = { git = "https://github.com/plonky3/plonky3", rev = "b0591e9b" }
p3-poseidon2 = { git = "https://github.com/plonky3/plonky3", rev = "b0591e9b" }
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3.git", rev = "b0591e9b" }
p3-challenger = { git = "https://github.com/plonky3/plonky3", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-field = { git = "https://github.com/plonky3/plonky3", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-goldilocks = { git = "https://github.com/plonky3/plonky3", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-mds = { git = "https://github.com/Plonky3/Plonky3.git", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-poseidon = { git = "https://github.com/plonky3/plonky3", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-poseidon2 = { git = "https://github.com/plonky3/plonky3", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
p3-symmetric = { git = "https://github.com/Plonky3/Plonky3.git", rev = "949cce916f6cfdba9c5ed4b9e41de22cc684f7b5" }
paste = "1"
plonky2 = "0.2"
poseidon = { path = "./poseidon" }
Expand Down
1 change: 1 addition & 0 deletions ceno_zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ cfg-if.workspace = true
criterion.workspace = true
pprof2.workspace = true
proptest.workspace = true
tiny-keccak.workspace = true

[build-dependencies]
glob = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/examples/riscv_opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ceno_zkvm::{
with_panic_hook,
};
use clap::Parser;
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

use ceno_emul::{
CENO_PLATFORM, EmuContext,
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/bin/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use clap::Parser;
use ff_ext::GoldilocksExt2;
use itertools::Itertools;
use mpcs::{Basefold, BasefoldRSParams};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;
use p3_goldilocks::Goldilocks;
use std::{fs, panic};
use tracing::level_filters::LevelFilter;
Expand Down
10 changes: 3 additions & 7 deletions ceno_zkvm/src/chip_handler/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use super::GlobalStateRegisterMachineChipOperations;
use crate::{
circuit_builder::CircuitBuilder, error::ZKVMError, expression::Expression, structs::RAMType,
};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

impl<E: ExtensionField> GlobalStateRegisterMachineChipOperations<E> for CircuitBuilder<'_, E> {
fn state_in(&mut self, pc: Expression<E>, ts: Expression<E>) -> Result<(), ZKVMError> {
let record: Vec<Expression<E>> = vec![
Expression::Constant(E::BaseField::from_canonical_u64(
RAMType::GlobalState as u64,
)),
Expression::Constant(E::BaseField::from_u64(RAMType::GlobalState as u64)),
pc,
ts,
];
Expand All @@ -21,9 +19,7 @@ impl<E: ExtensionField> GlobalStateRegisterMachineChipOperations<E> for CircuitB

fn state_out(&mut self, pc: Expression<E>, ts: Expression<E>) -> Result<(), ZKVMError> {
let record: Vec<Expression<E>> = vec![
Expression::Constant(E::BaseField::from_canonical_u64(
RAMType::GlobalState as u64,
)),
Expression::Constant(E::BaseField::from_u64(RAMType::GlobalState as u64)),
pc,
ts,
];
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/chip_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::successors;
use crate::expression::Expression;
use ff_ext::ExtensionField;
use itertools::izip;
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

pub fn rlc_chip_record<E: ExtensionField>(
records: Vec<Expression<E>>,
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
witness::RowMajorMatrix,
};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

/// namespace used for annotation, preserve meta info during circuit construction
#[derive(Clone, Debug, Default, serde::Serialize)]
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<E: ExtensionField> ConstraintSystem<E> {
record: Vec<Expression<E>>,
) -> Result<(), ZKVMError> {
let rlc_record = self.rlc_chip_record(
std::iter::once(Expression::Constant(E::BaseField::from_canonical_u64(
std::iter::once(Expression::Constant(E::BaseField::from_u64(
rom_type as u64,
)))
.chain(record.clone())
Expand Down
8 changes: 4 additions & 4 deletions ceno_zkvm/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use ceno_emul::InsnKind;
use ff_ext::{ExtensionField, SmallField};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

use multilinear_extensions::virtual_poly::ArcMultilinearExtension;

Expand Down Expand Up @@ -913,7 +913,7 @@ macro_rules! impl_from_unsigned {
$(
impl<F: SmallField, E: ExtensionField<BaseField = F>> From<$t> for Expression<E> {
fn from(value: $t) -> Self {
Expression::Constant(F::from_canonical_u64(value as u64))
Expression::Constant(F::from_u64(value as u64))
}
}
)*
Expand All @@ -928,7 +928,7 @@ macro_rules! impl_from_signed {
impl<F: SmallField, E: ExtensionField<BaseField = F>> From<$t> for Expression<E> {
fn from(value: $t) -> Self {
let reduced = (value as i128).rem_euclid(F::MODULUS_U64 as i128) as u64;
Expression::Constant(F::from_canonical_u64(reduced))
Expression::Constant(F::from_u64(reduced))
}
}
)*
Expand Down Expand Up @@ -1088,7 +1088,7 @@ mod tests {
use super::{Expression, ToExpr, fmt};
use crate::circuit_builder::{CircuitBuilder, ConstraintSystem};
use ff_ext::{FieldInto, GoldilocksExt2};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

#[test]
fn test_expression_arithmetics() {
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/expression/monomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {

use super::*;
use ff_ext::{FieldInto, FromUniformBytes, GoldilocksExt2 as E};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;
use p3_goldilocks::Goldilocks as F;
use rand_chacha::{ChaChaRng, rand_core::SeedableRng};

Expand All @@ -99,7 +99,7 @@ mod tests {
let y = || WitIn(1);
let z = || WitIn(2);
let n = || Constant(104u64.into_f());
let m = || Constant(-F::from_canonical_u64(599));
let m = || Constant(-F::from_u64(599));
let r = || Challenge(0, 1, E::ONE, E::ZERO);

let test_exprs: &[Expression<E>] = &[
Expand Down
8 changes: 1 addition & 7 deletions ceno_zkvm/src/gadgets/is_lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,7 @@ impl InnerLtConfig {
lhs: u64,
rhs: u64,
) -> Result<(), ZKVMError> {
self.assign_instance_field(
instance,
lkm,
F::from_canonical_u64(lhs),
F::from_canonical_u64(rhs),
lhs < rhs,
)
self.assign_instance_field(instance, lkm, F::from_u64(lhs), F::from_u64(rhs), lhs < rhs)
}

/// Assign instance values to this configuration where the ordering is
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/gadgets/signed_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
witness::LkMultiplicity,
};
use ff_ext::{ExtensionField, FieldInto};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;
use std::marker::PhantomData;

/// Extract the most significant bit from an expression previously constrained
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<E: ExtensionField> SignedExtendConfig<E> {
};

assert_ux(lk_multiplicity, 2 * val - (msb << self.n_bits));
set_val!(instance, self.msb, E::BaseField::from_canonical_u64(msb));
set_val!(instance, self.msb, E::BaseField::from_u64(msb));

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions ceno_zkvm/src/instructions/riscv/branch/branch_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
},
witness::LkMultiplicity,
};
pub use p3_field::FieldAlgebra;
pub use p3_field::PrimeCharacteristicRing;

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

Expand Down Expand Up @@ -151,8 +151,8 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for BranchCircuit<E, I
if let Some(equal) = &config.is_equal {
equal.assign_instance(
instance,
E::BaseField::from_canonical_u64(rs2.as_u64()),
E::BaseField::from_canonical_u64(rs1.as_u64()),
E::BaseField::from_u64(rs2.as_u64()),
E::BaseField::from_u64(rs1.as_u64()),
)?;
}

Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/instructions/riscv/ecall/halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
};
use ceno_emul::{StepRecord, Tracer};
use ff_ext::{ExtensionField, FieldInto};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;
use std::marker::PhantomData;

pub struct HaltConfig {
Expand Down Expand Up @@ -51,7 +51,7 @@ impl<E: ExtensionField> Instruction<E> for HaltInstruction<E> {
// read exit_code from arg0 (X10 register)
let (_, lt_x10_cfg) = cb.register_read(
|| "read x10",
E::BaseField::from_canonical_u64(ceno_emul::Platform::reg_arg0() as u64),
E::BaseField::from_u64(ceno_emul::Platform::reg_arg0() as u64),
prev_x10_ts.expr(),
ecall_cfg.ts.expr() + Tracer::SUBCYCLE_RS2,
exit_code,
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/instructions/riscv/ecall_insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};
use ceno_emul::{InsnKind::ECALL, PC_STEP_SIZE, Platform, StepRecord, Tracer};
use ff_ext::{ExtensionField, FieldInto};
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

pub struct EcallInstructionConfig {
pub pc: WitIn,
Expand Down Expand Up @@ -51,7 +51,7 @@ impl EcallInstructionConfig {
// read syscall_id from x5 and write return value to x5
let (_, lt_x5_cfg) = cb.register_write(
|| "write x5",
E::BaseField::from_canonical_u64(Platform::reg_ecall() as u64),
E::BaseField::from_u64(Platform::reg_ecall() as u64),
prev_x5_ts.expr(),
ts.expr() + Tracer::SUBCYCLE_RS1,
syscall_id.clone(),
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/instructions/riscv/insn_base.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ceno_emul::{Cycle, StepRecord, Word, WriteOp};
use ff_ext::{ExtensionField, FieldInto, SmallField};
use itertools::Itertools;
use p3_field::{Field, FieldAlgebra};
use p3_field::{Field, PrimeCharacteristicRing};

use super::constants::{PC_STEP_SIZE, UINT_LIMBS, UInt};
use crate::{
Expand Down Expand Up @@ -436,7 +436,7 @@ impl<E: ExtensionField> MemAddr<E> {
.sum();

// Range check the middle bits, that is the low limb excluding the low bits.
let shift_right = E::BaseField::from_canonical_u64(1 << Self::N_LOW_BITS)
let shift_right = E::BaseField::from_u64(1 << Self::N_LOW_BITS)
.inverse()
.expr();
let mid_u14 = (&limbs[0] - low_sum) * shift_right;
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/jump/jalr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
};
use ceno_emul::{InsnKind, PC_STEP_SIZE};
use ff_ext::FieldInto;
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;

pub struct JalrConfig<E: ExtensionField> {
pub i_insn: IInstructionConfig<E>,
Expand Down
25 changes: 12 additions & 13 deletions ceno_zkvm/src/instructions/riscv/memory/gadget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
use ceno_emul::StepRecord;
use ff_ext::{ExtensionField, FieldInto};
use itertools::izip;
use p3_field::{Field, FieldAlgebra};
use p3_field::{Field, PrimeCharacteristicRing};

pub struct MemWordChange<const N_ZEROS: usize> {
prev_limb_bytes: Vec<WitIn>,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<const N_ZEROS: usize> MemWordChange<N_ZEROS> {

// extract the least significant byte from u16 limb
let rs2_limb_bytes = alloc_bytes(cb, "rs2_limb[0]", 1)?;
let u8_base_inv = E::BaseField::from_canonical_u64(1 << 8).inverse();
let u8_base_inv = E::BaseField::from_u64(1 << 8).inverse();
cb.assert_ux::<_, _, 8>(
|| "rs2_limb[0].le_bytes[1]",
u8_base_inv.expr() * (&rs2_limbs[0] - rs2_limb_bytes[0].expr()),
Expand Down Expand Up @@ -162,41 +162,40 @@ impl<const N_ZEROS: usize> MemWordChange<N_ZEROS> {
match N_ZEROS {
0 => {
for (&col, byte) in izip!(&self.prev_limb_bytes, prev_limb.to_le_bytes()) {
set_val!(instance, col, E::BaseField::from_canonical_u8(byte));
set_val!(instance, col, E::BaseField::from_u8(byte));
lk_multiplicity.assert_ux::<8>(byte as u64);
}

set_val!(
instance,
self.rs2_limb_bytes[0],
E::BaseField::from_canonical_u8(rs2_limb.to_le_bytes()[0])
E::BaseField::from_u8(rs2_limb.to_le_bytes()[0])
);

rs2_limb.to_le_bytes().into_iter().for_each(|byte| {
lk_multiplicity.assert_ux::<8>(byte as u64);
});
let change = if low_bits[0] == 0 {
E::BaseField::from_canonical_u8(rs2_limb.to_le_bytes()[0])
- E::BaseField::from_canonical_u8(prev_limb.to_le_bytes()[0])
E::BaseField::from_u8(rs2_limb.to_le_bytes()[0])
- E::BaseField::from_u8(prev_limb.to_le_bytes()[0])
} else {
E::BaseField::from_canonical_u64((rs2_limb.to_le_bytes()[0] as u64) << 8)
- E::BaseField::from_canonical_u64((prev_limb.to_le_bytes()[1] as u64) << 8)
E::BaseField::from_u64((rs2_limb.to_le_bytes()[0] as u64) << 8)
- E::BaseField::from_u64((prev_limb.to_le_bytes()[1] as u64) << 8)
};
let final_change = if low_bits[1] == 0 {
change
} else {
E::BaseField::from_canonical_u64(1u64 << 16) * change
E::BaseField::from_u64(1u64 << 16) * change
};
set_val!(instance, self.expected_changes[0], change);
set_val!(instance, self.expected_changes[1], final_change);
}
1 => {
let final_change = if low_bits[1] == 0 {
E::BaseField::from_canonical_u16(rs2_limb)
- E::BaseField::from_canonical_u16(prev_limb)
E::BaseField::from_u16(rs2_limb) - E::BaseField::from_u16(prev_limb)
} else {
E::BaseField::from_canonical_u64((rs2_limb as u64) << 16)
- E::BaseField::from_canonical_u64((prev_limb as u64) << 16)
E::BaseField::from_u64((rs2_limb as u64) << 16)
- E::BaseField::from_u64((prev_limb as u64) << 16)
};
set_val!(instance, self.expected_changes[0], final_change);
}
Expand Down
10 changes: 3 additions & 7 deletions ceno_zkvm/src/instructions/riscv/memory/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
use ceno_emul::{ByteAddr, InsnKind, StepRecord};
use ff_ext::{ExtensionField, FieldInto};
use itertools::izip;
use p3_field::FieldAlgebra;
use p3_field::PrimeCharacteristicRing;
use std::marker::PhantomData;

pub struct LoadConfig<E: ExtensionField> {
Expand Down Expand Up @@ -226,11 +226,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for LoadInstruction<E,
.memory_addr
.assign_instance(instance, lk_multiplicity, unaligned_addr.into())?;
if let Some(&limb) = config.target_limb.as_ref() {
set_val!(
instance,
limb,
E::BaseField::from_canonical_u16(target_limb)
);
set_val!(instance, limb, E::BaseField::from_u16(target_limb));
}
if let Some(limb_bytes) = config.target_limb_bytes.as_ref() {
if addr_low_bits[0] == 1 {
Expand All @@ -240,7 +236,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for LoadInstruction<E,
}
for (&col, byte) in izip!(limb_bytes.iter(), target_limb_bytes.into_iter()) {
lk_multiplicity.assert_ux::<8>(byte as u64);
set_val!(instance, col, E::BaseField::from_canonical_u8(byte));
set_val!(instance, col, E::BaseField::from_u8(byte));
}
}
let val = match I::INST_KIND {
Expand Down
Loading

0 comments on commit 5c4e093

Please sign in to comment.