use cryptonum::traits::*; use std::ops::*; pub fn modinv(e: &U, phi: &U) -> U where S: Clone + CryptoNumBase + CryptoNumSigned, S: Div + Mul + Neg + Sub, S: AddAssign, U: Clone { let (_, mut x, _): (S, S, S) = extended_euclidean(e, phi); let int_phi: S = S::new(phi.clone()); while x.is_negative() { // FIXME: Unnecessary clone x += int_phi.clone(); } x.abs() } pub fn modexp(b: &T, e: &T, m: &T) -> T { panic!("modexp") } pub fn extended_euclidean(a: &U, b: &U) -> (S, S, S) where S: Clone + CryptoNumBase + CryptoNumSigned, S: Div + Mul + Neg + Sub, U: Clone { let posinta = S::new(a.clone()); let posintb = S::new(b.clone()); let (mut d, mut x, mut y) = egcd(&posinta, &posintb); if d.is_negative() { d = -d; x = -x; y = -y; } (d, x, y) } pub fn egcd(a: &S, b: &S) -> (S, S, S) where S: Clone + CryptoNumBase, S: Div + Mul + Sub, { let mut s: S = S::zero(); let mut old_s: S = S::from_u8(1); let mut t: S = S::from_u8(1); let mut old_t: S = S::zero(); let mut r: S = b.clone(); let mut old_r: S = a.clone(); while !r.is_zero() { let quotient: S = old_r.clone() / r.clone(); let prov_r = r.clone(); let prov_s = s.clone(); let prov_t = t.clone(); // FIXME: Unnecessary clones r = old_r - (r * quotient.clone()); s = old_s - (s * quotient.clone()); t = old_t - (t * quotient.clone()); old_r = prov_r; old_s = prov_s; old_t = prov_t; } (old_r, old_s, old_t) }