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 cryptonum::traits::*;
|
||||||
use std::ops::*;
|
use std::ops::*;
|
||||||
|
|
||||||
pub fn modinv<'a,T>(e: &T, phi: &T) -> T
|
pub fn modinv<S,U>(e: &U, phi: &U) -> U
|
||||||
where
|
where
|
||||||
T: Clone + CryptoNumBase + Ord,
|
S: Clone + CryptoNumBase + CryptoNumSigned<Unsigned=U>,
|
||||||
T: AddAssign + SubAssign + MulAssign + DivAssign,
|
S: Div<Output=S> + Mul<Output=S> + Neg<Output=S> + Sub<Output=S>,
|
||||||
T: Add<Output=T> + Sub<Output=T> + Mul<Output=T> + Div<Output=T>,
|
S: AddAssign,
|
||||||
&'a T: Sub<Output=T>,
|
U: Clone
|
||||||
T: 'a
|
|
||||||
{
|
{
|
||||||
let (_, mut x, _) = extended_euclidean(e, phi);
|
let (_, mut x, _): (S, S, S) = extended_euclidean(e, phi);
|
||||||
let int_phi = Signed::<T>::new(phi.clone());
|
let int_phi: S = S::new(phi.clone());
|
||||||
while x.is_negative() {
|
while x.is_negative() {
|
||||||
x += &int_phi;
|
// FIXME: Unnecessary clone
|
||||||
|
x += int_phi.clone();
|
||||||
}
|
}
|
||||||
x.abs()
|
x.abs()
|
||||||
}
|
}
|
||||||
@@ -23,44 +22,48 @@ pub fn modexp<T>(b: &T, e: &T, m: &T) -> T
|
|||||||
panic!("modexp")
|
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
|
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 posinta = S::new(a.clone());
|
||||||
let posintb = Signed::<T>::new(b.clone());
|
let posintb = S::new(b.clone());
|
||||||
let (mut d, mut x, mut y) = egcd(&posinta, &posintb);
|
let (mut d, mut x, mut y) = egcd(&posinta, &posintb);
|
||||||
|
|
||||||
if d.is_negative() {
|
if d.is_negative() {
|
||||||
d.negate();
|
d = -d;
|
||||||
x.negate();
|
x = -x;
|
||||||
y.negate();
|
y = -y;
|
||||||
}
|
}
|
||||||
|
|
||||||
(d, x, 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
|
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 s: S = S::zero();
|
||||||
let mut old_s = Signed::<T>::from_u8(1);
|
let mut old_s: S = S::from_u8(1);
|
||||||
let mut t = Signed::<T>::from_u8(1);
|
let mut t: S = S::from_u8(1);
|
||||||
let mut old_t = Signed::<T>::zero();
|
let mut old_t: S = S::zero();
|
||||||
let mut r = b.clone();
|
let mut r: S = b.clone();
|
||||||
let mut old_r = a.clone();
|
let mut old_r: S = a.clone();
|
||||||
|
|
||||||
while !r.is_zero() {
|
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_r = r.clone();
|
||||||
let prov_s = s.clone();
|
let prov_s = s.clone();
|
||||||
let prov_t = t.clone();
|
let prov_t = t.clone();
|
||||||
|
|
||||||
r = old_r - (r * "ient);
|
// FIXME: Unnecessary clones
|
||||||
s = old_s - (s * "ient);
|
r = old_r - (r * quotient.clone());
|
||||||
t = old_t - (t * "ient);
|
s = old_s - (s * quotient.clone());
|
||||||
|
t = old_t - (t * quotient.clone());
|
||||||
|
|
||||||
old_r = prov_r;
|
old_r = prov_r;
|
||||||
old_s = prov_s;
|
old_s = prov_s;
|
||||||
|
|||||||
@@ -6,13 +6,13 @@
|
|||||||
mod core;
|
mod core;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod builder;
|
mod builder;
|
||||||
//mod extended_math;
|
mod extended_math;
|
||||||
// mod primes;
|
// mod primes;
|
||||||
mod signed;
|
mod signed;
|
||||||
mod traits;
|
mod traits;
|
||||||
mod unsigned;
|
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::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};
|
pub use self::unsigned::{U512,U1024,U2048,U3072,U4096,U7680,U8192,U15360};
|
||||||
|
|||||||
Reference in New Issue
Block a user