Skip to content

Commit 42dddd1

Browse files
authored
BoxedMontyForm: additional explanatory comments for modpow (#749)
Better note why we have to subtract the modulus twice
1 parent 0761be3 commit 42dddd1

File tree

1 file changed

+7
-3
lines changed
  • src/modular/boxed_monty_form

1 file changed

+7
-3
lines changed

src/modular/boxed_monty_form/pow.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ impl PowBoundedExp<BoxedUint> for BoxedMontyForm {
4141
}
4242

4343
/// Performs modular exponentiation using Montgomery's ladder.
44-
/// `exponent_bits` represents the number of bits to take into account for the exponent.
4544
///
46-
/// NOTE: this value is leaked in the time pattern.
45+
/// `exponent_bits` represents the length of the exponent in bits.
46+
///
47+
/// NOTE: `exponent_bits` is leaked in the time pattern.
4748
fn pow_montgomery_form(
4849
x: &BoxedUint,
4950
exponent: &BoxedUint,
@@ -110,9 +111,12 @@ fn pow_montgomery_form(
110111
}
111112
}
112113

113-
// Ensure output is fully reduced (AMM only reduces to the bit length of the modulus)
114+
// Ensure output is properly reduced: AMM only reduces to the bit length of `modulus`
114115
// See RustCrypto/crypto-bigint#441
115116
z.conditional_sbb_assign(modulus, !z.ct_lt(modulus));
117+
118+
// Subtract again to ensure output is fully reduced
119+
// See RustCrypto/crypto-bigint#455 and golang.org/issue/13907
116120
z.conditional_sbb_assign(modulus, !z.ct_lt(modulus));
117121
debug_assert!(&z < modulus);
118122

0 commit comments

Comments
 (0)