Running into similar trait problems, albeit not as bad.
This commit is contained in:
@@ -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<S,U>(e: &U, phi: &U) -> U
|
||||
where
|
||||
T: Clone + CryptoNumBase + Ord,
|
||||
T: AddAssign + SubAssign + MulAssign + DivAssign,
|
||||
T: Add<Output=T> + Sub<Output=T> + Mul<Output=T> + Div<Output=T>,
|
||||
&'a T: Sub<Output=T>,
|
||||
T: 'a
|
||||
S: Clone + CryptoNumBase + CryptoNumSigned<Unsigned=U>,
|
||||
S: Div<Output=S> + Mul<Output=S> + Neg<Output=S> + Sub<Output=S>,
|
||||
S: AddAssign,
|
||||
U: Clone
|
||||
{
|
||||
let (_, mut x, _) = extended_euclidean(e, phi);
|
||||
let int_phi = Signed::<T>::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<T>(b: &T, e: &T, m: &T) -> T
|
||||
panic!("modexp")
|
||||
}
|
||||
|
||||
pub fn extended_euclidean<T>(a: &T, b: &T) -> (Signed<T>, Signed<T>, Signed<T>)
|
||||
pub fn extended_euclidean<U,S>(a: &U, b: &U) -> (S, S, S)
|
||||
where
|
||||
T: Clone + CryptoNumBase + Div + Mul + Sub
|
||||
S: Clone + CryptoNumBase + CryptoNumSigned<Unsigned=U>,
|
||||
S: Div<Output=S> + Mul<Output=S> + Neg<Output=S> + Sub<Output=S>,
|
||||
U: Clone
|
||||
{
|
||||
let posinta = Signed::<T>::new(a.clone());
|
||||
let posintb = Signed::<T>::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<T>(a: &Signed<T>, b: &Signed<T>) -> (Signed<T>,Signed<T>,Signed<T>)
|
||||
pub fn egcd<S>(a: &S, b: &S) -> (S, S, S)
|
||||
where
|
||||
T: Clone + CryptoNumBase + Div + Mul + Sub
|
||||
S: Clone + CryptoNumBase,
|
||||
S: Div<Output=S> + Mul<Output=S> + Sub<Output=S>,
|
||||
{
|
||||
let mut s = Signed::<T>::zero();
|
||||
let mut old_s = Signed::<T>::from_u8(1);
|
||||
let mut t = Signed::<T>::from_u8(1);
|
||||
let mut old_t = Signed::<T>::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;
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user