Open sollyucko opened 4 years ago
It might also be possible to use references instead of clone
, similarly to this:
trait HasModInv {
fn modinv(&self, m: &Self) -> Option<Self> where Self : Sized;
}
impl<T : Integer + Clone> HasModInv for T where for<'a> &'a T : Add<Output = T> + Rem<Output = T> {
fn modinv(&self, m: &T) -> Option<T> {
//modinverse(self, m)
let ExtendedGcd { gcd, x, .. } = self.extended_gcd(m);
if !gcd.is_one() {
None
} else {
Some(&(&(&x % m) + m) % m) // Deal with negative values properly
}
}
}
Let me know if you want me to separate out any of these changes. The change from Copy to Clone is the main goal, since it allows using types like
BigInt
andBigUint
.