You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.9 KiB

  1. extern crate num;
  2. extern crate num_bigint;
  3. extern crate num_traits;
  4. use num_bigint::BigInt;
  5. use num_traits::{One, Zero};
  6. pub fn modulus(a: &BigInt, m: &BigInt) -> BigInt {
  7. ((a % m) + m) % m
  8. }
  9. pub fn mod_inverse0(a: &BigInt, q: &BigInt) -> BigInt {
  10. let mut mn = (q.clone(), a.clone());
  11. let mut xy: (BigInt, BigInt) = (Zero::zero(), One::one());
  12. let big_zero: BigInt = Zero::zero();
  13. while mn.1 != big_zero {
  14. xy = (xy.1.clone(), xy.0 - (mn.0.clone() / mn.1.clone()) * xy.1);
  15. mn = (mn.1.clone(), modulus(&mn.0, &mn.1));
  16. }
  17. while xy.0 < Zero::zero() {
  18. xy.0 = modulus(&xy.0, q);
  19. }
  20. xy.0
  21. }
  22. /*
  23. pub fn mod_inverse1(a0: BigInt, m0: BigInt) -> BigInt {
  24. if m0 == One::one() {
  25. return One::one();
  26. }
  27. let (mut a, mut m, mut x0, mut inv): (BigInt, BigInt, BigInt, BigInt) =
  28. (a0, m0.clone(), Zero::zero(), One::one());
  29. while a > One::one() {
  30. inv = inv - (&a / m.clone()) * x0.clone();
  31. a = a % m.clone();
  32. std::mem::swap(&mut a, &mut m);
  33. std::mem::swap(&mut x0, &mut inv);
  34. }
  35. if inv < Zero::zero() {
  36. inv += m0.clone()
  37. }
  38. inv
  39. }
  40. pub fn mod_inverse2(a: BigInt, q: BigInt) -> BigInt {
  41. let mut aa: BigInt = a;
  42. let mut qq: BigInt = q;
  43. if qq < Zero::zero() {
  44. qq = -qq;
  45. }
  46. if aa < Zero::zero() {
  47. aa = -aa;
  48. }
  49. let d = num::Integer::gcd(&aa, &qq);
  50. if d != One::one() {
  51. println!("ERR no mod_inv");
  52. }
  53. let res: BigInt;
  54. if d < Zero::zero() {
  55. res = d + qq;
  56. } else {
  57. res = d;
  58. }
  59. res
  60. }
  61. */
  62. #[cfg(test)]
  63. mod tests {
  64. use super::*;
  65. #[test]
  66. fn test_mod_inverse() {
  67. let a = BigInt::parse_bytes(b"123456789123456789123456789123456789123456789", 10).unwrap();
  68. let b = BigInt::parse_bytes(b"12345678", 10).unwrap();
  69. assert_eq!(
  70. mod_inverse0(&a, &b),
  71. BigInt::parse_bytes(b"641883", 10).unwrap()
  72. );
  73. }
  74. }