Skip to content

Commit

Permalink
add trait FieldFrom & FieldInto
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Feb 18, 2025
1 parent f1fc090 commit 6c2f148
Show file tree
Hide file tree
Showing 42 changed files with 164 additions and 114 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions ceno_zkvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ version.workspace = true

[dependencies]
ark-std.workspace = true
ff.workspace = true
goldilocks.workspace = true
rand_chacha.workspace = true
rayon.workspace = true
serde.workspace = true
Expand All @@ -27,6 +25,7 @@ sumcheck = { version = "0", path = "../sumcheck" }
transcript = { path = "../transcript" }

p3-field.workspace = true
p3-goldilocks.workspace = true
itertools.workspace = true
num-traits.workspace = true
paste.workspace = true
Expand All @@ -51,7 +50,6 @@ cfg-if.workspace = true
criterion.workspace = true
pprof2.workspace = true
proptest.workspace = true
p3-goldilocks.workspace = true

[build-dependencies]
glob = "0.3"
Expand Down
9 changes: 6 additions & 3 deletions ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
witness::RowMajorMatrix,
};
use p3_field::FieldAlgebra;

/// namespace used for annotation, preserve meta info during circuit construction
#[derive(Clone, Debug, Default, serde::Serialize)]
Expand Down Expand Up @@ -275,9 +276,11 @@ 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(rom_type as u64)))
.chain(record.clone())
.collect(),
std::iter::once(Expression::Constant(E::BaseField::from_canonical_u64(
rom_type as u64,
)))
.chain(record.clone())
.collect(),
);
assert_eq!(
rlc_record.degree(),
Expand Down
3 changes: 2 additions & 1 deletion ceno_zkvm/src/expression/monomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use itertools::{Itertools, chain, iproduct};

use super::Expression;
use Expression::*;
use ff_ext::FieldInto;
use std::iter::Sum;

impl<E: ExtensionField> Expression<E> {
Expand Down Expand Up @@ -98,7 +99,7 @@ mod tests {
let x = || WitIn(0);
let y = || WitIn(1);
let z = || WitIn(2);
let n = || Constant(104.into());
let n = || Constant(104u64.into_f());
let m = || Constant(-F::from_canonical_u64(599));
let r = || Challenge(0, 1, E::ONE, E::ZERO);

Expand Down
3 changes: 2 additions & 1 deletion ceno_zkvm/src/gadgets/is_lt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
utils::i64_to_base,
witness::LkMultiplicity,
};
use ff_ext::FieldInto;

use super::SignedExtendConfig;

Expand Down Expand Up @@ -276,7 +277,7 @@ impl InnerLtConfig {
is_lt: bool,
) -> Result<(), ZKVMError> {
let range_offset: F = if is_lt {
Self::range(self.max_num_u16_limbs).into()
Self::range(self.max_num_u16_limbs).into_f()
} else {
F::ZERO
};
Expand Down
7 changes: 3 additions & 4 deletions ceno_zkvm/src/gadgets/is_zero.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ff_ext::ExtensionField;
use goldilocks::SmallField;
use ff_ext::{ExtensionField, SmallField};

use crate::{
circuit_builder::CircuitBuilder,
Expand Down Expand Up @@ -65,10 +64,10 @@ impl IsZeroConfig {
instance: &mut [F],
x: F,
) -> Result<(), ZKVMError> {
let (is_zero, inverse) = if x.is_zero_vartime() {
let (is_zero, inverse) = if x.is_zero() {
(F::ONE, F::ZERO)
} else {
(F::ZERO, x.invert().expect("not zero"))
(F::ZERO, x.try_inverse().expect("not zero"))
};

if let Some(wit) = self.is_zero {
Expand Down
5 changes: 3 additions & 2 deletions ceno_zkvm/src/gadgets/signed_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
set_val,
witness::LkMultiplicity,
};
use ff_ext::ExtensionField;
use ff_ext::{ExtensionField, FieldInto};
use p3_field::FieldAlgebra;
use std::marker::PhantomData;

/// Extract the most significant bit from an expression previously constrained
Expand Down Expand Up @@ -102,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(msb));
set_val!(instance, self.msb, E::BaseField::from_canonical_u64(msb));

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E
#[cfg(test)]
mod test {
use ceno_emul::{Change, StepRecord, encode_rv32};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use super::*;
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/arith_imm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<E: ExtensionField> Instruction<E> for AddiInstruction<E> {
#[cfg(test)]
mod test {
use ceno_emul::{Change, InsnKind, PC_STEP_SIZE, StepRecord, encode_rv32};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use crate::{
circuit_builder::{CircuitBuilder, ConstraintSystem},
Expand Down
1 change: 1 addition & 0 deletions ceno_zkvm/src/instructions/riscv/b_insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
utils::i64_to_base,
witness::LkMultiplicity,
};
use ff_ext::FieldInto;

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

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

Expand Down Expand Up @@ -150,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(rs2.as_u64()),
E::BaseField::from(rs1.as_u64()),
E::BaseField::from_canonical_u64(rs2.as_u64()),
E::BaseField::from_canonical_u64(rs1.as_u64()),
)?;
}

Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/branch/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ceno_emul::{ByteAddr, Change, PC_STEP_SIZE, StepRecord, Word, encode_rv32};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use super::*;
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/instructions/riscv/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{expression::WitIn, set_val, utils::i64_to_base, witness::LkMultiplicity};
use goldilocks::SmallField;
use ff_ext::{FieldInto, SmallField};
use itertools::Itertools;

#[derive(Clone)]
Expand Down Expand Up @@ -106,7 +106,7 @@ impl UIntLtuInput<'_> {
set_val!(instance, config.rhs_ne_byte, rhs_ne_byte);
set_val!(instance, config.byte_diff_inv, {
if flag {
(lhs_ne_byte - rhs_ne_byte).invert().unwrap()
(lhs_ne_byte - rhs_ne_byte).inverse()
} else {
F::ONE
}
Expand Down
19 changes: 9 additions & 10 deletions ceno_zkvm/src/instructions/riscv/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
//! booleans is equal to 1.
use ceno_emul::{InsnKind, StepRecord};
use ff_ext::ExtensionField;
use goldilocks::SmallField;
use ff_ext::{ExtensionField, FieldInto, SmallField};
use p3_goldilocks::Goldilocks;

use super::{
RIVInstruction,
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E
// 32-bit registers represented over the Goldilocks field, so verify
// these parameters
assert_eq!(UInt::<E>::TOTAL_BITS, u32::BITS as usize);
assert_eq!(E::BaseField::MODULUS_U64, goldilocks::MODULUS);
assert_eq!(E::BaseField::MODULUS_U64, Goldilocks::MODULUS_U64);

// 32-bit value from rs1
let dividend = UInt::new_unchecked(|| "dividend", cb)?;
Expand Down Expand Up @@ -388,13 +388,13 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E

is_dividend_signed_min.assign_instance(
instance,
(dividend as u32 as u64).into(),
(i32::MIN as u32 as u64).into(),
(dividend as u32 as u64).into_f(),
(i32::MIN as u32 as u64).into_f(),
)?;
is_divisor_neg_one.assign_instance(
instance,
(divisor as u32 as u64).into(),
(-1i32 as u32 as u64).into(),
(divisor as u32 as u64).into_f(),
(-1i32 as u32 as u64).into_f(),
)?;

let signed_div_overflow_b = dividend == i32::MIN && divisor == -1i32;
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E

config
.is_divisor_zero
.assign_instance(instance, (divisor as u64).into())?;
.assign_instance(instance, (divisor as u64).into_f())?;

config.is_remainder_lt_divisor.assign_instance(
instance,
Expand Down Expand Up @@ -461,8 +461,7 @@ mod test {
scheme::mock_prover::{MOCK_PC_START, MockProver},
};
use ceno_emul::{Change, InsnKind, StepRecord, encode_rv32};
use ff_ext::ExtensionField;
use goldilocks::GoldilocksExt2 as GE;
use ff_ext::{ExtensionField, GoldilocksExt2 as GE};
use itertools::Itertools;
use rand::Rng;

Expand Down
1 change: 1 addition & 0 deletions ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
utils::i64_to_base,
witness::LkMultiplicity,
};
use ff_ext::FieldInto;

/// DummyInstruction can handle any instruction and produce its side-effects.
pub struct DummyInstruction<E, I>(PhantomData<(E, I)>);
Expand Down
1 change: 1 addition & 0 deletions ceno_zkvm/src/instructions/riscv/dummy/dummy_ecall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
set_val,
witness::LkMultiplicity,
};
use ff_ext::FieldInto;

trait EcallSpec {
const NAME: &'static str;
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/dummy/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ceno_emul::{Change, InsnKind, StepRecord, encode_rv32};
use dummy_ecall::KeccakSpec;
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

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

pub struct HaltConfig {
Expand Down Expand Up @@ -50,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(ceno_emul::Platform::reg_arg0() as u64),
E::BaseField::from_canonical_u64(ceno_emul::Platform::reg_arg0() as u64),
prev_x10_ts.expr(),
ecall_cfg.ts.expr() + Tracer::SUBCYCLE_RS2,
exit_code,
Expand Down
5 changes: 3 additions & 2 deletions ceno_zkvm/src/instructions/riscv/ecall_insn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::{
witness::LkMultiplicity,
};
use ceno_emul::{InsnKind::ECALL, PC_STEP_SIZE, Platform, StepRecord, Tracer};
use ff_ext::ExtensionField;
use ff_ext::{ExtensionField, FieldInto};
use p3_field::FieldAlgebra;

pub struct EcallInstructionConfig {
pub pc: WitIn,
Expand Down Expand Up @@ -50,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(Platform::reg_ecall() as u64),
E::BaseField::from_canonical_u64(Platform::reg_ecall() as u64),
prev_x5_ts.expr(),
ts.expr() + Tracer::SUBCYCLE_RS1,
syscall_id.clone(),
Expand Down
13 changes: 6 additions & 7 deletions ceno_zkvm/src/instructions/riscv/insn_base.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use ceno_emul::{Cycle, StepRecord, Word, WriteOp};
use ff::Field;
use ff_ext::ExtensionField;
use goldilocks::SmallField;
use ff_ext::{ExtensionField, FieldInto, SmallField};
use itertools::Itertools;
use p3_field::{Field, FieldAlgebra};

use super::constants::{PC_STEP_SIZE, UINT_LIMBS, UInt};
use crate::{
Expand Down Expand Up @@ -437,9 +436,8 @@ 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(1 << Self::N_LOW_BITS)
.invert()
.unwrap()
let shift_right = E::BaseField::from_canonical_u64(1 << Self::N_LOW_BITS)
.inverse()
.expr();
let mid_u14 = (&limbs[0] - low_sum) * shift_right;
cb.assert_ux::<_, _, 14>(|| "mid_u14", mid_u14)?;
Expand Down Expand Up @@ -486,8 +484,9 @@ impl<E: ExtensionField> MemAddr<E> {

#[cfg(test)]
mod test {
use goldilocks::{Goldilocks as F, GoldilocksExt2 as E};
use ff_ext::GoldilocksExt2 as E;
use itertools::Itertools;
use p3_goldilocks::Goldilocks as F;

use crate::{
ROMType,
Expand Down
3 changes: 2 additions & 1 deletion ceno_zkvm/src/instructions/riscv/jump/jalr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::marker::PhantomData;

use ff::Field;
use ff_ext::ExtensionField;

use crate::{
Expand All @@ -18,6 +17,8 @@ use crate::{
witness::LkMultiplicity,
};
use ceno_emul::{InsnKind, PC_STEP_SIZE};
use ff_ext::FieldInto;
use p3_field::FieldAlgebra;

pub struct JalrConfig<E: ExtensionField> {
pub i_insn: IInstructionConfig<E>,
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/jump/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ceno_emul::{ByteAddr, Change, InsnKind, PC_STEP_SIZE, StepRecord, Word, encode_rv32};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use crate::{
circuit_builder::{CircuitBuilder, ConstraintSystem},
Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/logic/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ceno_emul::{Change, StepRecord, Word, encode_rv32};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use crate::{
circuit_builder::{CircuitBuilder, ConstraintSystem},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl<E: ExtensionField> LogicConfig<E> {
#[cfg(test)]
mod test {
use ceno_emul::{Change, InsnKind, PC_STEP_SIZE, StepRecord, encode_rv32u};
use goldilocks::GoldilocksExt2;
use ff_ext::GoldilocksExt2;

use crate::{
chip_handler::test::DebugIndex,
Expand Down
Loading

0 comments on commit 6c2f148

Please sign in to comment.