Skip to content
Merged
Changes from 1 commit
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
20 changes: 11 additions & 9 deletions ff/src/fields/models/fp/montgomery_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,14 @@ pub trait MontConfig<const N: usize>: 'static + Sync + Send + Sized {
}
(a.0).0 = r;
}
a.subtract_modulus();
} else {
// Alternative implementation
// Implements CIOS.
*a = a.mul_without_cond_subtract(b);
let (carry, res) = a.mul_without_cond_subtract(b);
*a = res;
a.subtract_modulus_with_carry(carry);
}
a.subtract_modulus();
}

#[inline(always)]
Expand Down Expand Up @@ -734,7 +736,7 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
}
}

const fn mul_without_cond_subtract(mut self, other: &Self) -> Self {
const fn mul_without_cond_subtract(mut self, other: &Self) -> (bool, Self) {
let (mut lo, mut hi) = ([0u64; N], [0u64; N]);
crate::const_for!((i in 0..N) {
let mut carry = 0;
Expand Down Expand Up @@ -768,12 +770,12 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
crate::const_for!((i in 0..N) {
(self.0).0[i] = hi[i];
});
self
(carry2 != 0, self)
}

const fn mul(mut self, other: &Self) -> Self {
self = self.mul_without_cond_subtract(other);
self.const_subtract_modulus()
const fn mul(self, other: &Self) -> Self {
let (carry, res) = self.mul_without_cond_subtract(other);
res.const_subtract_modulus_with_carry(carry)
}

const fn const_is_valid(&self) -> bool {
Expand All @@ -788,8 +790,8 @@ impl<T: MontConfig<N>, const N: usize> Fp<MontBackend<T, N>, N> {
}

#[inline]
const fn const_subtract_modulus(mut self) -> Self {
if !self.const_is_valid() {
const fn const_subtract_modulus_with_carry(mut self, carry: bool) -> Self {
if carry || !self.const_is_valid() {
self.0 = Self::sub_with_borrow(&self.0, &T::MODULUS);
}
self
Expand Down