Skip to content

Clamp Montgomery exponent bit bounds#1273

Open
tob-joe wants to merge 1 commit into
RustCrypto:masterfrom
tob-joe:fix-montgomery-exponent-bit-bounds
Open

Clamp Montgomery exponent bit bounds#1273
tob-joe wants to merge 1 commit into
RustCrypto:masterfrom
tob-joe:fix-montgomery-exponent-bit-bounds

Conversation

@tob-joe
Copy link
Copy Markdown

@tob-joe tob-joe commented Jun 4, 2026

Summary

Bounded Montgomery exponentiation APIs define exponent_bits as the number of least-significant exponent bits to take into account:

/// Raises to the `exponent` power,
/// with `exponent_bits` representing the number of (least significant) bits
/// to take into account for the exponent.
///
/// NOTE: `exponent_bits` may be leaked in the time pattern.
#[must_use]
pub const fn pow_bounded_exp<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<RHS_LIMBS>,
exponent_bits: u32,

/// Raises to the `exponent` power,
/// with `exponent_bits` representing the number of (least significant) bits
/// to take into account for the exponent.
///
/// NOTE: `exponent_bits` may be leaked in the time pattern.
#[must_use]
pub const fn pow_bounded_exp<const RHS_LIMBS: usize>(
&self,
exponent: &Uint<RHS_LIMBS>,
exponent_bits: u32,

/// Raises to the `exponent` power,
/// with `exponent_bits` representing the number of (least significant) bits
/// to take into account for the exponent.
///
/// NOTE: `exponent_bits` may be leaked in the time pattern.
#[must_use]
pub fn pow_bounded_exp(&self, exponent: &BoxedUint, exponent_bits: u32) -> Self {
Self {
montgomery_form: pow_montgomery_form_amm(
&self.montgomery_form,
exponent,
exponent_bits,

When exponent_bits exceeded the supplied exponent precision, the Montgomery helpers could derive an out-of-range limb index. Since exponent limbs are little-endian, those missing high exponent bits should be treated as zero.

Fix

Clamp exponent_bits to the supplied exponent precision at the shared Montgomery exponentiation entry points. This makes oversized bounded exponent lengths behave like full-precision exponentiation instead of indexing past the exponent limbs.

Tests

Added regression tests covering oversized exponent_bits for:

  • standard Montgomery exponentiation
  • AMM Montgomery exponentiation
  • array multi-exponentiation
  • alloc slice multi-exponentiation

Verified with:

cargo test --all-features modular::pow::tests -- --nocapture
cargo test --all-features --test uint monty_form_pow_bounded_exp -- --exact

This work was completed by Trail of Bits as part of the Patch The Planet project in collaboration with OpenAI. The issue was identified primarily by the Codex coding agent, and manually reviewed before submission.

Treat oversized bounded Montgomery exponent lengths as requests for all available exponent bits instead of indexing past the exponent limbs.

Clamp exponent_bits at the shared Montgomery exponentiation entry points so missing high exponent bits are handled as zero.

Add regression tests for the standard, AMM, array multi-exponentiation, and alloc slice multi-exponentiation helpers.

Co-authored-by: GPT 5.5 <gpt-5.5@openai.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.02%. Comparing base (4c6f87d) to head (1b1b52e).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1273      +/-   ##
==========================================
+ Coverage   90.99%   91.02%   +0.02%     
==========================================
  Files         189      189              
  Lines       22139    22195      +56     
==========================================
+ Hits        20146    20202      +56     
  Misses       1993     1993              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant