Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize add to avoid overflow check #520

Merged
merged 2 commits into from
Aug 31, 2023
Merged
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
12 changes: 8 additions & 4 deletions math/src/field/fields/montgomery_backed_prime_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
&M::MODULUS,
&Self::MU,
);
const MODULUS_HAS_ONE_SPARE_BIT: bool = Self::modulus_has_one_spare_bit();

/// Computes `- modulus^{-1} mod 2^{64}`
/// This algorithm is given by Dussé and Kaliski Jr. in
Expand Down Expand Up @@ -97,7 +98,8 @@ where
/// Checks whether the most significant limb of the modulus is at
/// most `0x7FFFFFFFFFFFFFFE`. This check is useful since special
/// optimizations exist for this kind of moduli.
const fn moduli_has_one_spare_bit() -> bool {
#[inline(always)]
const fn modulus_has_one_spare_bit() -> bool {
M::MODULUS.limbs[0] < (1u64 << 63) - 1
}
}
Expand All @@ -111,21 +113,23 @@ where
#[inline(always)]
fn add(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType {
let (sum, overflow) = UnsignedInteger::add(a, b);
if !overflow {
if Self::MODULUS_HAS_ONE_SPARE_BIT {
if sum >= M::MODULUS {
sum - M::MODULUS
} else {
sum
}
} else {
} else if overflow || sum >= M::MODULUS {
let (diff, _) = UnsignedInteger::sub(&sum, &M::MODULUS);
diff
} else {
sum
}
}

#[inline(always)]
fn mul(a: &Self::BaseType, b: &Self::BaseType) -> Self::BaseType {
if Self::moduli_has_one_spare_bit() {
if Self::MODULUS_HAS_ONE_SPARE_BIT {
MontgomeryAlgorithms::cios_optimized_for_moduli_with_one_spare_bit(
a,
b,
Expand Down
Loading