Skip to content

Commit

Permalink
BoxedMontyForm: additional explanatory comments for modpow (#749)
Browse files Browse the repository at this point in the history
Better note why we have to subtract the modulus twice
  • Loading branch information
tarcieri authored Jan 21, 2025
1 parent 0761be3 commit 42dddd1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/modular/boxed_monty_form/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ impl PowBoundedExp<BoxedUint> for BoxedMontyForm {
}

/// Performs modular exponentiation using Montgomery's ladder.
/// `exponent_bits` represents the number of bits to take into account for the exponent.
///
/// NOTE: this value is leaked in the time pattern.
/// `exponent_bits` represents the length of the exponent in bits.
///
/// NOTE: `exponent_bits` is leaked in the time pattern.
fn pow_montgomery_form(
x: &BoxedUint,
exponent: &BoxedUint,
Expand Down Expand Up @@ -110,9 +111,12 @@ fn pow_montgomery_form(
}
}

// Ensure output is fully reduced (AMM only reduces to the bit length of the modulus)
// Ensure output is properly reduced: AMM only reduces to the bit length of `modulus`
// See RustCrypto/crypto-bigint#441
z.conditional_sbb_assign(modulus, !z.ct_lt(modulus));

// Subtract again to ensure output is fully reduced
// See RustCrypto/crypto-bigint#455 and golang.org/issue/13907
z.conditional_sbb_assign(modulus, !z.ct_lt(modulus));
debug_assert!(&z < modulus);

Expand Down

0 comments on commit 42dddd1

Please sign in to comment.