Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod div;
mod div_uint;
mod encoding;
mod from;
mod gcd;
mod inv_mod;
mod mul;
mod mul_uint;
Expand Down
63 changes: 63 additions & 0 deletions src/int/gcd.rs
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));
}
}
31 changes: 29 additions & 2 deletions src/uint/gcd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Support for computing the greatest common divisor of two `Uint`s.

use crate::{modular::SafeGcdInverter, ConstChoice, Gcd, Odd, PrecomputeInverter, Uint};
use crate::{modular::SafeGcdInverter, ConstChoice, Gcd, Int, Odd, PrecomputeInverter, Uint};

impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Uint<SAT_LIMBS>
where
Expand Down Expand Up @@ -75,9 +75,25 @@ where
}
}

/// Gcd of a [Uint] and an [Int].
impl<const SAT_LIMBS: usize, const UNSAT_LIMBS: usize> Gcd<Int<SAT_LIMBS>> for Uint<SAT_LIMBS>
where
Odd<Uint<SAT_LIMBS>>: PrecomputeInverter<Inverter = SafeGcdInverter<SAT_LIMBS, UNSAT_LIMBS>>,
{
type Output = Uint<SAT_LIMBS>;

fn gcd(&self, rhs: &Int<SAT_LIMBS>) -> Self::Output {
self.gcd(&rhs.abs())
}

fn gcd_vartime(&self, rhs: &Int<SAT_LIMBS>) -> Self::Output {
self.gcd_vartime(&rhs.abs())
}
}

#[cfg(test)]
mod tests {
use crate::U256;
use crate::{Gcd, I256, U256};

#[test]
fn gcd_relatively_prime() {
Expand Down Expand Up @@ -119,4 +135,15 @@ mod tests {
assert_eq!(f, f.gcd(&g));
assert_eq!(f, g.gcd(&f));
}

#[test]
fn gcd_uint_int() {
// Two numbers with a shared factor of 61
let f = U256::from(61u32 * 71);
let g = I256::from(59i32 * 61);

let sixty_one = U256::from(61u32);
assert_eq!(sixty_one, <U256 as Gcd<I256>>::gcd(&f, &g));
assert_eq!(sixty_one, <U256 as Gcd<I256>>::gcd(&f, &g.wrapping_neg()));
}
}
Loading