use tc_bigint::BigUint;
use crate::{ModMul, NonZero};
impl ModMul for BigUint {
type Output = Self;
fn mod_mul(&self, rhs: &Self, modulus: &NonZero<Self>) -> Self {
let modulus: &Self = modulus;
((self % modulus) * (rhs % modulus)) % modulus
}
}
#[cfg(test)]
mod tests {
use tc_bigint::BigUint;
use crate::{ModMul, NonZero};
const VALUES: [u128; 8] = [
0,
1,
255,
u64::MAX as u128,
u64::MAX as u128 + 1,
i128::MAX as u128,
u128::MAX - 1,
u128::MAX,
];
fn expected(a: u128, b: u128, m: u128) -> u128 {
let add = |x: u128, y: u128| if x >= m - y { x - (m - y) } else { x + y };
let (a, b) = (a % m, b % m);
(0..u128::BITS).rev().fold(0, |product, index| {
let doubled = add(product, product);
if b >> index & 1 == 1 {
add(doubled, a)
} else {
doubled
}
})
}
#[test]
fn the_product_is_the_primitive_product_reduced_by_the_modulus() {
for m in &VALUES[1..] {
let modulus = NonZero::new(BigUint::from(*m)).unwrap();
for a in VALUES {
for b in VALUES {
let (x, y) = (BigUint::from(a), BigUint::from(b));
let expected = BigUint::from(expected(a, b, *m));
assert_eq!(x.mod_mul(&y, &modulus), expected, "{a} {b} {m}");
}
}
}
}
#[test]
fn a_product_wider_than_the_operands_is_reduced() {
let power = BigUint::from(u64::MAX as u128 + 1);
let modulus = NonZero::new(BigUint::from(i128::MAX as u128)).unwrap();
assert_eq!(power.mod_mul(&power, &modulus), BigUint::from(2u8));
let below = BigUint::from(u128::MAX - 1);
let modulus = NonZero::new(BigUint::from(u128::MAX)).unwrap();
assert_eq!(below.mod_mul(&below, &modulus), BigUint::from(1u8));
}
}