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
27 changes: 19 additions & 8 deletions src/field/crypto_bigint_const_monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::{
str::FromStr,
};
use crypto_bigint::{
Limb,
Limb, NonZeroUint, Uint as CBUint,
modular::{ConstMontyForm, ConstMontyParams as Params, Retrieve},
subtle::{Choice, ConditionallySelectable, ConstantTimeEq},
};
Expand Down Expand Up @@ -505,25 +505,28 @@ impl<Mod: Params<LIMBS>, const LIMBS: usize> Semiring for ConstMontyField<Mod, L
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 = ConstMontyForm<Mod, LIMBS>;
type Inner = Uint<LIMBS>;

#[inline(always)]
fn inner(&self) -> &Self::Inner {
&self.0
Uint::new_ref(self.0.as_montgomery())
}
}

impl<Mod: Params<LIMBS>, const LIMBS: usize> ConstPrimeField for ConstMontyField<Mod, LIMBS> {
const MODULUS: Self::Inner = ConstMontyForm::<Mod, LIMBS>::new(Mod::PARAMS.modulus().as_ref());
const MODULUS: Self::Inner = *Uint::new_ref(Mod::PARAMS.modulus().as_ref());
const MODULUS_MINUS_ONE_DIV_TWO: Self::Inner = {
let m_minus_one = ConstMontyForm::sub(&Self::MODULUS, &ConstMontyForm::ONE);
m_minus_one.div_by_2()
let m_minus_one = CBUint::wrapping_sub(Self::MODULUS.inner(), &CBUint::ONE);
let two = CBUint::<LIMBS>::wrapping_add(&CBUint::ONE, &CBUint::ONE);
Uint::new(CBUint::wrapping_div(
&m_minus_one,
&NonZeroUint::new_unwrap(two),
))
};

#[inline(always)]
fn new_unchecked(inner: Self::Inner) -> Self {
// Inner value is a ConstMontyForm so it's guaranteed to be valid
Self(inner)
Self(ConstMontyForm::from_montgomery(inner.into_inner()))
}
}

Expand Down Expand Up @@ -1295,6 +1298,14 @@ mod prop_tests {
);
type F = ConstMontyField<ModP, { U256::LIMBS }>;

#[test]
fn modulus_minus_one_div_two_correct() {
assert_eq!(
F::MODULUS_MINUS_ONE_DIV_TWO,
Uint::from_be_hex("006E54A6C50F6671DB743AAEC4CCBC3E82926C650F53AAF3D7C27DB237D18F93")
)
}

fn any_f() -> impl Strategy<Value = F> {
any::<u64>().prop_map(F::from)
}
Expand Down
20 changes: 19 additions & 1 deletion src/semiring/crypto_bigint_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{
hash::{Hash, Hasher},
iter::{Product, Sum},
ops::{
Add, AddAssign, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
Add, AddAssign, Div, Mul, MulAssign, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
SubAssign,
},
str::FromStr,
Expand Down Expand Up @@ -269,6 +269,24 @@ impl_basic_op!(Add, add);
impl_basic_op!(Sub, sub);
impl_basic_op!(Mul, mul);

impl<const LIMBS: usize> Div for Uint<LIMBS> {
type Output = Self;

#[inline(always)]
fn div(self, rhs: Self) -> Self::Output {
self.div(&rhs)
}
}

impl<'a, const LIMBS: usize> Div<&'a Self> for Uint<LIMBS> {
type Output = Self;

fn div(self, rhs: &'a Self) -> Self::Output {
let non_zero = crypto_bigint::NonZero::new(rhs.0).expect("division by zero");
Self(self.0.div(&non_zero))
}
}

impl<const LIMBS: usize> Rem for Uint<LIMBS> {
type Output = Self;

Expand Down
Loading