Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod crypto_bigint_const_monty;
pub(crate) mod crypto_bigint_helpers;
#[cfg(feature = "crypto_bigint")]
pub mod crypto_bigint_monty;
pub mod f2;

use crate::{ConstSemiring, ring::Ring};
use core::{
Expand Down Expand Up @@ -40,6 +41,11 @@ pub trait Field:
/// Underlying representation of an element
type Inner: Debug + Eq + Clone + Sync + Send;

/// Type used to represent the modulus. Usually the same as `Self::Inner`,
/// but may differ when the modulus doesn't fit in the element representation
/// (e.g. F2 where elements are `bool` but the modulus is `2: u8`).
type Modulus: Debug + Eq + Clone + Sync + Send;

fn inner(&self) -> &Self::Inner;
fn inner_mut(&mut self) -> &mut Self::Inner;
fn into_inner(self) -> Self::Inner;
Expand All @@ -63,11 +69,11 @@ pub trait PrimeField: Field {
// Note: Not using `&self` to avoid conflicts with `Zero` trait.
fn is_zero(value: &Self) -> bool;

fn modulus(&self) -> Self::Inner;
fn modulus(&self) -> Self::Modulus;

fn modulus_minus_one_div_two(&self) -> Self::Inner;

fn make_cfg(modulus: &Self::Inner) -> Result<Self::Config, FieldError>;
fn make_cfg(modulus: &Self::Modulus) -> Result<Self::Config, FieldError>;

/// Creates a new instance of a prime field element from
/// an arbitrary element of `Self::Inner`. The method
Expand All @@ -92,7 +98,7 @@ pub trait PrimeField: Field {
pub trait ConstPrimeField:
Field + ConstSemiring + Inv<Output = Option<Self>> + From<u64> + From<u128> + From<Self::Inner>
{
const MODULUS: Self::Inner;
const MODULUS: Self::Modulus;
const MODULUS_MINUS_ONE_DIV_TWO: Self::Inner;

/// Creates a new instance of a prime field element from
Expand Down Expand Up @@ -124,7 +130,7 @@ impl<T: ConstPrimeField> PrimeField for T {
}

#[inline(always)]
fn modulus(&self) -> Self::Inner {
fn modulus(&self) -> Self::Modulus {
Self::MODULUS
}

Expand All @@ -133,7 +139,7 @@ impl<T: ConstPrimeField> PrimeField for T {
Self::MODULUS_MINUS_ONE_DIV_TWO
}

fn make_cfg(modulus: &Self::Inner) -> Result<Self::Config, FieldError> {
fn make_cfg(modulus: &Self::Modulus) -> Result<Self::Config, FieldError> {
if *modulus == Self::MODULUS {
Ok(())
} else {
Expand Down
1 change: 1 addition & 0 deletions src/field/ark_ff_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ impl<F: ArkWrappedField> Ring for ArkField<F> {}

impl<F: ArkWrappedField> Field for ArkField<F> {
type Inner = F;
type Modulus = <F::BasePrimeField as ark_ff::PrimeField>::BigInt;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
Expand Down
3 changes: 2 additions & 1 deletion src/field/ark_ff_fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ impl<P: FpConfig<N>, const N: usize> IntRing for Fp<P, N> {

impl<P: FpConfig<N>, const N: usize> Field for Fp<P, N> {
type Inner = BigInt<N>;
type Modulus = Self::Inner;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
Expand All @@ -510,7 +511,7 @@ impl<P: FpConfig<N>, const N: usize> Field for Fp<P, N> {

/// ConstPrimeField is only implemented for MontConfig and MontBackend
impl<M: MontConfig<N>, const N: usize> ConstPrimeField for Fp<MontBackend<M, N>, N> {
const MODULUS: Self::Inner = M::MODULUS;
const MODULUS: Self::Modulus = M::MODULUS;
const MODULUS_MINUS_ONE_DIV_TWO: Self::Inner =
<ArkWrappedFp<MontBackend<M, N>, N> as ArkPrimeField>::MODULUS_MINUS_ONE_DIV_TWO;

Expand Down
5 changes: 3 additions & 2 deletions src/field/crypto_bigint_boxed_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl Ring for BoxedMontyField {}

impl Field for BoxedMontyField {
type Inner = BoxedUint;
type Modulus = Self::Inner;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
Expand Down Expand Up @@ -440,7 +441,7 @@ impl PrimeField for BoxedMontyField {
value.0.is_zero().into()
}

fn modulus(&self) -> Self::Inner {
fn modulus(&self) -> Self::Modulus {
self.0.params().modulus().clone().get()
}

Expand All @@ -450,7 +451,7 @@ impl PrimeField for BoxedMontyField {
(value - BoxedUint::one()) / NonZero::new(BoxedUint::from(2_u8)).unwrap()
}

fn make_cfg(modulus: &Self::Inner) -> Result<Self::Config, FieldError> {
fn make_cfg(modulus: &Self::Modulus) -> Result<Self::Config, FieldError> {
let Some(modulus) = Odd::new(modulus.clone()).into_option() else {
return Err(FieldError::InvalidModulus);
};
Expand Down
3 changes: 2 additions & 1 deletion src/field/crypto_bigint_const_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ impl<Mod: Params<LIMBS>, const LIMBS: usize> Ring for ConstMontyField<Mod, LIMBS

impl<Mod: Params<LIMBS>, const LIMBS: usize> Field for ConstMontyField<Mod, LIMBS> {
type Inner = Uint<LIMBS>;
type Modulus = Self::Inner;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
Expand All @@ -571,7 +572,7 @@ impl<Mod: Params<LIMBS>, const LIMBS: usize> Field for ConstMontyField<Mod, LIMB
}

impl<Mod: Params<LIMBS>, const LIMBS: usize> ConstPrimeField for ConstMontyField<Mod, LIMBS> {
const MODULUS: Self::Inner = *Uint::new_ref(Mod::PARAMS.modulus().as_ref());
const MODULUS: Self::Modulus = *Uint::new_ref(Mod::PARAMS.modulus().as_ref());
const MODULUS_MINUS_ONE_DIV_TWO: Self::Inner = {
let m_minus_one = CBUint::wrapping_sub(Self::MODULUS.inner(), &CBUint::ONE);
let two = CBUint::<LIMBS>::wrapping_add(&CBUint::ONE, &CBUint::ONE);
Expand Down
5 changes: 3 additions & 2 deletions src/field/crypto_bigint_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ impl<const LIMBS: usize> Ring for MontyField<LIMBS> {}

impl<const LIMBS: usize> Field for MontyField<LIMBS> {
type Inner = Uint<LIMBS>;
type Modulus = Self::Inner;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
Expand Down Expand Up @@ -515,7 +516,7 @@ impl<const LIMBS: usize> PrimeField for MontyField<LIMBS> {
value.0.as_montgomery().is_zero()
}

fn modulus(&self) -> Self::Inner {
fn modulus(&self) -> Self::Modulus {
Uint::new(self.0.params().modulus().get())
}

Expand All @@ -528,7 +529,7 @@ impl<const LIMBS: usize> PrimeField for MontyField<LIMBS> {
)
}

fn make_cfg(modulus: &Self::Inner) -> Result<Self::Config, FieldError> {
fn make_cfg(modulus: &Self::Modulus) -> Result<Self::Config, FieldError> {
let Some(modulus) = Odd::new(*modulus.inner()).into_option() else {
return Err(FieldError::InvalidModulus);
};
Expand Down
Loading