use happy_cracking::crypto::mathtools;
#[test]
fn test_gcd_basic() {
assert_eq!(mathtools::gcd(12, 8), 4);
assert_eq!(mathtools::gcd(54, 24), 6);
}
#[test]
fn test_gcd_coprime() {
assert_eq!(mathtools::gcd(17, 13), 1);
}
#[test]
fn test_gcd_with_zero() {
assert_eq!(mathtools::gcd(0, 5), 5);
assert_eq!(mathtools::gcd(5, 0), 5);
assert_eq!(mathtools::gcd(0, 0), 0);
}
#[test]
fn test_gcd_same() {
assert_eq!(mathtools::gcd(7, 7), 7);
}
#[test]
fn test_lcm_basic() {
assert_eq!(mathtools::lcm(4, 6).unwrap(), 12);
assert_eq!(mathtools::lcm(3, 5).unwrap(), 15);
}
#[test]
fn test_lcm_with_zero() {
assert_eq!(mathtools::lcm(0, 5).unwrap(), 0);
assert_eq!(mathtools::lcm(5, 0).unwrap(), 0);
}
#[test]
fn test_lcm_same() {
assert_eq!(mathtools::lcm(7, 7).unwrap(), 7);
}
#[test]
fn test_modinv_basic() {
assert_eq!(mathtools::modinv(3, 26).unwrap(), 9);
}
#[test]
fn test_modinv_known_values() {
assert_eq!(mathtools::modinv(7, 11).unwrap(), 8);
}
#[test]
fn test_modinv_not_exist() {
assert!(mathtools::modinv(2, 4).is_err());
}
#[test]
fn test_modinv_mod_one() {
assert_eq!(mathtools::modinv(5, 1).unwrap(), 0);
}
#[test]
fn test_modpow_basic() {
assert_eq!(mathtools::modpow(2, 10, 1000).unwrap(), 24);
}
#[test]
fn test_modpow_large() {
assert_eq!(mathtools::modpow(3, 13, 7).unwrap(), 3);
}
#[test]
fn test_modpow_zero_exp() {
assert_eq!(mathtools::modpow(5, 0, 13).unwrap(), 1);
}
#[test]
fn test_modpow_mod_one() {
assert_eq!(mathtools::modpow(100, 100, 1).unwrap(), 0);
}
#[test]
fn test_modpow_rsa_style() {
let n = 3233_u128;
let e = 17_u128;
let d = 2753_u128;
let message = 65_u128;
let encrypted = mathtools::modpow(message, e, n).unwrap();
let decrypted = mathtools::modpow(encrypted, d, n).unwrap();
assert_eq!(decrypted, message);
}
#[test]
fn test_modpow_overflow() {
let m: u128 = 1u128 << 65;
let base: u128 = 3;
let exp: u128 = 100;
let result = mathtools::modpow(base, exp, m).unwrap();
assert_eq!(result, 33908865301881557969);
}
#[test]
fn test_modinv_large_inputs() {
let m = u128::MAX;
let a = u128::MAX - 2;
let res = mathtools::modinv(a, m).unwrap();
assert_eq!(res, 170141183460469231731687303715884105727);
}