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

Add non-allocating mul_mod #340

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions src/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@
modulus
}

/// Alloc free variant of [`mul_mod`](Self::mul_mod).
///
/// Requires `N` to be set to `nlimbs(2 * BITS)`.
#[inline]
#[must_use]
pub fn mul_mod_na<const N: usize>(self, rhs: Self, mut modulus: Self) -> Self {
assert_eq!(N, crate::nlimbs(2 * BITS));
if modulus == Self::ZERO {
return Self::ZERO;
}
// Compute full product.
let mut product = [0; N];
let overflow = algorithms::addmul(&mut product, self.as_limbs(), rhs.as_limbs());
debug_assert!(!overflow);

Check warning on line 90 in src/modular.rs

View check run for this annotation

Codecov / codecov/patch

src/modular.rs#L82-L90

Added lines #L82 - L90 were not covered by tests

// Compute modulus using `div_rem`.
// This stores the remainder in the divisor, `modulus`.
algorithms::div(&mut product, &mut modulus.limbs);

modulus
}

Check warning on line 97 in src/modular.rs

View check run for this annotation

Codecov / codecov/patch

src/modular.rs#L94-L97

Added lines #L94 - L97 were not covered by tests

/// Compute $\mod{\mathtt{self}^{\mathtt{rhs}}}_{\mathtt{modulus}}$.
///
/// Returns zero if the modulus is zero.
Expand Down
Loading