Skip to content

Commit

Permalink
correct naming
Browse files Browse the repository at this point in the history
  • Loading branch information
olesHolem committed Dec 17, 2024
1 parent 4f4dc8e commit d4a361f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 40 deletions.
11 changes: 5 additions & 6 deletions crates/boojum/src/gadgets/mersenne_field/fourth_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use mersenne_field::{Mersenne31Complex, Mersenne31Quartic};

use super::*;
use super::second_ext::*;
use crate::gadgets::impls::limbs_decompose::decompose_into_limbs;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MersenneQuartic<F: SmallField> {
Expand Down Expand Up @@ -51,7 +50,7 @@ impl<F: SmallField> MersenneQuartic<F> {
[self.x.x.into_num(), self.x.y.into_num(), self.y.x.into_num(), self.y.y.into_num()]
}

pub fn into_coeffs(&self) -> [MersenneFiled<F>; 4] {
pub fn into_coeffs(&self) -> [MersenneField<F>; 4] {
[self.x.x, self.x.y, self.y.x, self.y.y]
}

Expand Down Expand Up @@ -87,7 +86,7 @@ impl<F: SmallField> MersenneQuartic<F> {
self.y.enforce_reduced(cs);
}

pub fn from_base<CS: ConstraintSystem<F>>(cs: &mut CS, value: MersenneFiled<F>) -> Self {
pub fn from_base<CS: ConstraintSystem<F>>(cs: &mut CS, value: MersenneField<F>) -> Self {
Self {
x: MersenneComplex::from_base(cs, value),
y: MersenneComplex::zero(cs),
Expand Down Expand Up @@ -432,14 +431,14 @@ impl<F: SmallField> MersenneQuartic<F> {
}
}

pub fn mul_by_base<CS: ConstraintSystem<F>>(&self, cs: &mut CS, other: &MersenneFiled<F>) -> Self {
pub fn mul_by_base<CS: ConstraintSystem<F>>(&self, cs: &mut CS, other: &MersenneField<F>) -> Self {
Self {
x: self.x.mul_by_base(cs, other),
y: self.y.mul_by_base(cs, other),
}
}

pub fn mul_by_base_and_add<CS: ConstraintSystem<F>>(&self, cs: &mut CS, coeff: &MersenneFiled<F>, other: &Self) -> Self {
pub fn mul_by_base_and_add<CS: ConstraintSystem<F>>(&self, cs: &mut CS, coeff: &MersenneField<F>, other: &Self) -> Self {
Self {
x: self.x.mul_by_base_and_add(cs, coeff, &other.x),
y: self.y.mul_by_base_and_add(cs, coeff, &other.y),
Expand Down Expand Up @@ -728,7 +727,7 @@ mod tests {
let cs = &mut owned_cs;

let rand_base_witness = [0; 2].map(|_| Mersenne31Field::new(rand::random::<u32>() % M31_MODULUS as u32));
let rand_base_vars = rand_base_witness.map(|w| MersenneFiled::<F>::allocate_checked(cs, w, false));
let rand_base_vars = rand_base_witness.map(|w| MersenneField::<F>::allocate_checked(cs, w, false));

let rand_witness = [0; 3].map(|_|
Mersenne31Quartic {
Expand Down
18 changes: 9 additions & 9 deletions crates/boojum/src/gadgets/mersenne_field/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const M31_MODULUS: u64 = (1 << 31) - 1;

// #[derive(Derivative, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MersenneFiled<F: SmallField> {
pub struct MersenneField<F: SmallField> {
// the inner witness value is always reduced with the modulus
// if reduced is true, then the reduction is proved and max possible value is 2^31 - 2
// if reduced is false, then only 31-bit check is proved and max possible value is 2^31 - 1
Expand All @@ -40,7 +40,7 @@ pub struct MersenneFiled<F: SmallField> {
pub(crate) _marker: std::marker::PhantomData<F>,
}

impl<F: SmallField> MersenneFiled<F> {
impl<F: SmallField> MersenneField<F> {
pub fn allocated_constant<CS: ConstraintSystem<F>>(cs: &mut CS, value: Mersenne31Field) -> Self {
let variable = cs.allocate_constant(F::from_u64_unchecked(value.to_reduced_u32() as u64));

Expand Down Expand Up @@ -1222,8 +1222,8 @@ pub fn get_15_bits_range_check_table<F: SmallField, CS: ConstraintSystem<F>>(
pub fn reduce_mersenne31<F: SmallField, CS: ConstraintSystem<F>>(
cs: &mut CS,
unreduced_a: Variable,
) -> (MersenneFiled<F>, Variable) {
let a = MersenneFiled::allocate_checked_without_value(cs, false);
) -> (MersenneField<F>, Variable) {
let a = MersenneField::allocate_checked_without_value(cs, false);
let reduce_a = cs.alloc_variable_without_value();

if <CS::Config as CSConfig>::WitnessConfig::EVALUATE_WITNESS {
Expand Down Expand Up @@ -1272,7 +1272,7 @@ pub fn reduce_mersenne31<F: SmallField, CS: ConstraintSystem<F>>(
(a, reduce_a)
}

impl<F: SmallField> CSAllocatable<F> for MersenneFiled<F> {
impl<F: SmallField> CSAllocatable<F> for MersenneField<F> {
type Witness = Mersenne31Field;

fn placeholder_witness() -> Self::Witness {
Expand All @@ -1298,7 +1298,7 @@ impl<F: SmallField> WitnessCastable<F, [F; 1]> for Mersenne31Field {
}
}

impl<F: SmallField> CSWitnessable<F, 1> for MersenneFiled<F> {
impl<F: SmallField> CSWitnessable<F, 1> for MersenneField<F> {
type ConversionFunction = Convertor<F, [F; 1], Mersenne31Field>;

fn witness_from_set_of_values(values: [F; 1]) -> Self::Witness {
Expand All @@ -1310,7 +1310,7 @@ impl<F: SmallField> CSWitnessable<F, 1> for MersenneFiled<F> {
}
}

impl<F: SmallField> WitnessHookable<F> for MersenneFiled<F> {
impl<F: SmallField> WitnessHookable<F> for MersenneField<F> {
fn witness_hook<CS: ConstraintSystem<F>>(
&self,
cs: &CS,
Expand All @@ -1320,7 +1320,7 @@ impl<F: SmallField> WitnessHookable<F> for MersenneFiled<F> {
}
}

impl<F: SmallField> Selectable<F> for MersenneFiled<F> {
impl<F: SmallField> Selectable<F> for MersenneField<F> {
#[must_use]
fn conditionally_select<CS: ConstraintSystem<F>>(
cs: &mut CS,
Expand Down Expand Up @@ -1461,7 +1461,7 @@ mod tests {
let cs = &mut owned_cs;

let rand_witness = [0; 4].map(|_| Mersenne31Field::new(rand::random::<u32>() % M31_MODULUS as u32));
let mut rand_vars = rand_witness.map(|w| MersenneFiled::<F>::allocate_checked(cs, w, false));
let mut rand_vars = rand_witness.map(|w| MersenneField::<F>::allocate_checked(cs, w, false));

// enforce reduced
for var in rand_vars.iter_mut() {
Expand Down
50 changes: 25 additions & 25 deletions crates/boojum/src/gadgets/mersenne_field/second_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ use super::*;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct MersenneComplex<F: SmallField> {
pub x: MersenneFiled<F>,
pub y: MersenneFiled<F>,
pub x: MersenneField<F>,
pub y: MersenneField<F>,
}

impl<F: SmallField> MersenneComplex<F> {
pub fn allocated_constant<CS: ConstraintSystem<F>>(cs: &mut CS, value: Mersenne31Complex) -> Self {
Self {
x: MersenneFiled::allocated_constant(cs, value.c0),
y: MersenneFiled::allocated_constant(cs, value.c1),
x: MersenneField::allocated_constant(cs, value.c0),
y: MersenneField::allocated_constant(cs, value.c1),
}
}

pub fn zero<CS: ConstraintSystem<F>>(cs: &mut CS) -> Self {
Self {
x: MersenneFiled::zero(cs),
y: MersenneFiled::zero(cs),
x: MersenneField::zero(cs),
y: MersenneField::zero(cs),
}
}

pub fn one<CS: ConstraintSystem<F>>(cs: &mut CS) -> Self {
Self {
x: MersenneFiled::one(cs),
y: MersenneFiled::zero(cs),
x: MersenneField::one(cs),
y: MersenneField::zero(cs),
}
}

pub fn minus_one<CS: ConstraintSystem<F>>(cs: &mut CS) -> Self {
Self {
x: MersenneFiled::minus_one(cs),
y: MersenneFiled::zero(cs),
x: MersenneField::minus_one(cs),
y: MersenneField::zero(cs),
}
}

Expand All @@ -58,16 +58,16 @@ impl<F: SmallField> MersenneComplex<F> {
/// The coordinate values should be in range [0, 2^31 - 2]
pub fn from_variables_checked<CS: ConstraintSystem<F>>(cs: &mut CS, variables: [Variable; 2], reduced: bool) -> Self {
Self {
x: MersenneFiled::from_variable_checked(cs, variables[0], reduced),
y: MersenneFiled::from_variable_checked(cs, variables[1], reduced),
x: MersenneField::from_variable_checked(cs, variables[0], reduced),
y: MersenneField::from_variable_checked(cs, variables[1], reduced),
}
}

/// The coordinate values should be in range [0, 2^31 - 2]
pub fn allocate_checked_without_value<CS: ConstraintSystem<F>>(cs: &mut CS, reduced: bool) -> Self {
Self {
x: MersenneFiled::allocate_checked_without_value(cs, reduced),
y: MersenneFiled::allocate_checked_without_value(cs, reduced),
x: MersenneField::allocate_checked_without_value(cs, reduced),
y: MersenneField::allocate_checked_without_value(cs, reduced),
}
}

Expand All @@ -77,8 +77,8 @@ impl<F: SmallField> MersenneComplex<F> {
reduced: bool,
) -> Self {
Self {
x: MersenneFiled::allocate_checked(cs, witness.c0, reduced),
y: MersenneFiled::allocate_checked(cs, witness.c1, reduced),
x: MersenneField::allocate_checked(cs, witness.c0, reduced),
y: MersenneField::allocate_checked(cs, witness.c1, reduced),
}
}

Expand All @@ -87,10 +87,10 @@ impl<F: SmallField> MersenneComplex<F> {
self.y.enforce_reduced(cs);
}

pub fn from_base<CS: ConstraintSystem<F>>(cs: &mut CS, value: MersenneFiled<F>) -> Self {
pub fn from_base<CS: ConstraintSystem<F>>(cs: &mut CS, value: MersenneField<F>) -> Self {
Self {
x: value,
y: MersenneFiled::zero(cs),
y: MersenneField::zero(cs),
}
}

Expand Down Expand Up @@ -146,14 +146,14 @@ impl<F: SmallField> MersenneComplex<F> {
}
}

pub fn mul_by_base<CS: ConstraintSystem<F>>(&self, cs: &mut CS, other: &MersenneFiled<F>) -> Self {
pub fn mul_by_base<CS: ConstraintSystem<F>>(&self, cs: &mut CS, other: &MersenneField<F>) -> Self {
Self {
x: self.x.mul(cs, other),
y: self.y.mul(cs, other),
}
}

pub fn mul_by_base_and_add<CS: ConstraintSystem<F>>(&self, cs: &mut CS, coeff: &MersenneFiled<F>, other: &Self) -> Self {
pub fn mul_by_base_and_add<CS: ConstraintSystem<F>>(&self, cs: &mut CS, coeff: &MersenneField<F>, other: &Self) -> Self {
Self {
x: self.x.mul_and_add(cs, coeff, &other.x),
y: self.y.mul_and_add(cs, coeff, &other.y),
Expand Down Expand Up @@ -185,8 +185,8 @@ impl<F: SmallField> MersenneComplex<F> {
let reduce1 = Num::allocate_without_value(cs);
let reduce2 = Num::allocate_without_value(cs);
crate::gadgets::u8::range_check_u8_pair(cs, &[reduce1.get_variable(), reduce2.get_variable()]); // 6th constraint
let result_x = MersenneFiled::allocate_checked_without_value(cs, false); // 7th constraint
let result_y = MersenneFiled::allocate_checked_without_value(cs, false); // 8th constraint
let result_x = MersenneField::allocate_checked_without_value(cs, false); // 7th constraint
let result_y = MersenneField::allocate_checked_without_value(cs, false); // 8th constraint

if <CS::Config as CSConfig>::WitnessConfig::EVALUATE_WITNESS {
let value_fn = move |inputs: [F; 2]| {
Expand Down Expand Up @@ -415,8 +415,8 @@ impl<F: SmallField> Selectable<F> for MersenneComplex<F> {
b: &Self,
) -> Self {
Self {
x: MersenneFiled::conditionally_select(cs, flag, &a.x, &b.x),
y: MersenneFiled::conditionally_select(cs, flag, &a.y, &b.y),
x: MersenneField::conditionally_select(cs, flag, &a.x, &b.x),
y: MersenneField::conditionally_select(cs, flag, &a.y, &b.y),
}
}
const SUPPORTS_PARALLEL_SELECT: bool = true;
Expand Down Expand Up @@ -545,7 +545,7 @@ mod tests {
let cs = &mut owned_cs;

let rand_base_witness = [0; 2].map(|_| Mersenne31Field::new(rand::random::<u32>() % M31_MODULUS as u32));
let rand_base_vars = rand_base_witness.map(|w| MersenneFiled::<F>::allocate_checked(cs, w, false));
let rand_base_vars = rand_base_witness.map(|w| MersenneField::<F>::allocate_checked(cs, w, false));

let rand_witness = [0; 2].map(|_|
Mersenne31Complex {
Expand Down

0 comments on commit d4a361f

Please sign in to comment.