-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `impl Gcd for Int` * `impl Gcd<Int> for Uint`
- Loading branch information
1 parent
91f184f
commit 7fbf363
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ mod div; | |
mod div_uint; | ||
mod encoding; | ||
mod from; | ||
mod gcd; | ||
mod inv_mod; | ||
mod mul; | ||
mod mul_uint; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! Support for computing the greatest common divisor of `Int`s. | ||
use crate::modular::SafeGcdInverter; | ||
use crate::{Gcd, Int, Odd, PrecomputeInverter, Uint}; | ||
|
||
/// Gcd of two [Int]s | ||
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Gcd for Int<SAT_LIMBS> | ||
where | ||
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>>, | ||
{ | ||
type Output = Uint<SAT_LIMBS>; | ||
|
||
fn gcd(&self, rhs: &Self) -> Self::Output { | ||
self.abs().gcd(&rhs.abs()) | ||
} | ||
|
||
fn gcd_vartime(&self, rhs: &Self) -> Self::Output { | ||
self.abs().gcd_vartime(&rhs.abs()) | ||
} | ||
} | ||
|
||
/// Gcd of an [Int] and a [Uint]. | ||
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Gcd<Uint<SAT_LIMBS>> for Int<SAT_LIMBS> | ||
where | ||
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>>, | ||
{ | ||
type Output = Uint<SAT_LIMBS>; | ||
|
||
fn gcd(&self, rhs: &Uint<SAT_LIMBS>) -> Self::Output { | ||
self.abs().gcd(rhs) | ||
} | ||
|
||
fn gcd_vartime(&self, rhs: &Uint<SAT_LIMBS>) -> Self::Output { | ||
self.abs().gcd_vartime(rhs) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::{Gcd, I256, U256}; | ||
|
||
#[test] | ||
fn gcd_always_positive() { | ||
// Two numbers with a shared factor of 61 | ||
let f = I256::from(59i32 * 61); | ||
let g = I256::from(61i32 * 71); | ||
|
||
assert_eq!(U256::from(61u32), f.gcd(&g)); | ||
assert_eq!(U256::from(61u32), f.wrapping_neg().gcd(&g)); | ||
assert_eq!(U256::from(61u32), f.gcd(&g.wrapping_neg())); | ||
assert_eq!(U256::from(61u32), f.wrapping_neg().gcd(&g.wrapping_neg())); | ||
} | ||
|
||
#[test] | ||
fn gcd_int_uint() { | ||
// Two numbers with a shared factor of 61 | ||
let f = I256::from(59i32 * 61); | ||
let g = U256::from(61u32 * 71); | ||
|
||
assert_eq!(U256::from(61u32), f.gcd(&g)); | ||
assert_eq!(U256::from(61u32), f.wrapping_neg().gcd(&g)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters