From 02aa03ca5c416b4dd5945dbe5489fbc64967b3b6 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Sun, 11 Mar 2018 21:36:49 -0700 Subject: [PATCH] Running into similar trait problems, albeit not as bad. --- src/cryptonum/extended_math.rs | 61 ++++++++++++++++++---------------- src/cryptonum/mod.rs | 6 ++-- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/cryptonum/extended_math.rs b/src/cryptonum/extended_math.rs index 186a2aa..1d86d2d 100644 --- a/src/cryptonum/extended_math.rs +++ b/src/cryptonum/extended_math.rs @@ -1,19 +1,18 @@ -use cryptonum::signed::Signed; use cryptonum::traits::*; use std::ops::*; -pub fn modinv<'a,T>(e: &T, phi: &T) -> T +pub fn modinv(e: &U, phi: &U) -> U where - T: Clone + CryptoNumBase + Ord, - T: AddAssign + SubAssign + MulAssign + DivAssign, - T: Add + Sub + Mul + Div, - &'a T: Sub, - T: 'a + S: Clone + CryptoNumBase + CryptoNumSigned, + S: Div + Mul + Neg + Sub, + S: AddAssign, + U: Clone { - let (_, mut x, _) = extended_euclidean(e, phi); - let int_phi = Signed::::new(phi.clone()); + let (_, mut x, _): (S, S, S) = extended_euclidean(e, phi); + let int_phi: S = S::new(phi.clone()); while x.is_negative() { - x += &int_phi; + // FIXME: Unnecessary clone + x += int_phi.clone(); } x.abs() } @@ -23,44 +22,48 @@ pub fn modexp(b: &T, e: &T, m: &T) -> T panic!("modexp") } -pub fn extended_euclidean(a: &T, b: &T) -> (Signed, Signed, Signed) +pub fn extended_euclidean(a: &U, b: &U) -> (S, S, S) where - T: Clone + CryptoNumBase + Div + Mul + Sub + S: Clone + CryptoNumBase + CryptoNumSigned, + S: Div + Mul + Neg + Sub, + U: Clone { - let posinta = Signed::::new(a.clone()); - let posintb = Signed::::new(b.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.negate(); - x.negate(); - y.negate(); + d = -d; + x = -x; + y = -y; } (d, x, y) } -pub fn egcd(a: &Signed, b: &Signed) -> (Signed,Signed,Signed) +pub fn egcd(a: &S, b: &S) -> (S, S, S) where - T: Clone + CryptoNumBase + Div + Mul + Sub + S: Clone + CryptoNumBase, + S: Div + Mul + Sub, { - let mut s = Signed::::zero(); - let mut old_s = Signed::::from_u8(1); - let mut t = Signed::::from_u8(1); - let mut old_t = Signed::::zero(); - let mut r = b.clone(); - let mut old_r = a.clone(); + 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 = old_r.clone() / r.clone(); + let quotient: S = old_r.clone() / r.clone(); let prov_r = r.clone(); let prov_s = s.clone(); let prov_t = t.clone(); - r = old_r - (r * "ient); - s = old_s - (s * "ient); - t = old_t - (t * "ient); + // 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; diff --git a/src/cryptonum/mod.rs b/src/cryptonum/mod.rs index 85664da..3dce0a5 100644 --- a/src/cryptonum/mod.rs +++ b/src/cryptonum/mod.rs @@ -6,13 +6,13 @@ mod core; #[macro_use] mod builder; -//mod extended_math; +mod extended_math; // mod primes; mod signed; mod traits; mod unsigned; -// pub use self::extended_math::{modexp,modinv,extended_euclidean,egcd}; +pub use self::extended_math::{modexp,modinv,extended_euclidean,egcd}; // pub use self::primes::{probably_prime}; -pub use self::signed::{I512}; +pub use self::signed::{I512,I1024,I2048,I3072,I4096,I7680,I8192,I15360}; pub use self::unsigned::{U512,U1024,U2048,U3072,U4096,U7680,U8192,U15360};