pub(crate) mod pow_schoolbook;
pub(crate) mod pow_square_and_multiply;
#[cfg(test)]
mod tests {
use super::pow_schoolbook::pow_schoolbook;
use super::pow_square_and_multiply::pow_square_and_multiply;
use crate::int::types::Uint;
#[test]
fn pow_schoolbook_matches_square_and_multiply_uint2() {
let bases: &[Uint<2>] = &[
Uint::<2>::from_limbs([0, 0]),
Uint::<2>::from_limbs([1, 0]),
Uint::<2>::from_limbs([2, 0]),
Uint::<2>::from_limbs([3, 0]),
Uint::<2>::from_limbs([0xDEAD_BEEF_CAFE_F00D, 0x1234_5678_9ABC_DEF0]),
];
let exps: &[u32] = &[0, 1, 2, 3, 5, 7, 10];
for &base in bases {
for &exp in exps {
let got = pow_schoolbook(base, exp);
let want = pow_square_and_multiply(base, exp);
assert_eq!(
got, want,
"pow_schoolbook Uint<2> mismatch: base={:?} exp={}",
base.as_limbs(), exp
);
}
}
}
#[test]
fn pow_schoolbook_uint1_known_values() {
let cases: &[(u64, u32, u64)] = &[
(2, 0, 1),
(2, 1, 2),
(2, 2, 4),
(2, 3, 8),
(2, 10, 1024),
(3, 0, 1),
(3, 1, 3),
(3, 2, 9),
(3, 3, 27),
(3, 5, 243),
(0, 1, 0),
(0, 5, 0),
(1, 0, 1),
(1, 100, 1),
(u64::MAX, 2, 1),
(u64::MAX, 3, u64::MAX),
];
for &(base_raw, exp, expected) in cases {
let base = Uint::<1>::from_limbs([base_raw]);
let got = pow_schoolbook(base, exp);
assert_eq!(
got.as_limbs()[0], expected,
"pow_schoolbook Uint<1> mismatch: {}^{} expected {}",
base_raw, exp, expected
);
}
}
#[test]
fn pow_schoolbook_matches_square_and_multiply_uint4() {
let bases: &[Uint<4>] = &[
Uint::<4>::from_limbs([0, 0, 0, 0]),
Uint::<4>::from_limbs([1, 0, 0, 0]),
Uint::<4>::from_limbs([7, 0, 0, 0]),
Uint::<4>::from_limbs([0xFFFF_FFFF, 0, 0, 0]),
Uint::<4>::from_limbs([0xAAAA_AAAA_AAAA_AAAA, 0x5555_5555_5555_5555, 0, 0]),
];
let exps: &[u32] = &[0, 1, 2, 3, 4];
for &base in bases {
for &exp in exps {
let got = pow_schoolbook(base, exp);
let want = pow_square_and_multiply(base, exp);
assert_eq!(
got, want,
"pow_schoolbook Uint<4> mismatch: base={:?} exp={}",
base.as_limbs(), exp
);
}
}
}
}