Skip to content

Commit

Permalink
Skip first iteration in euclidean algorithm if inputs are out of order
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-lj committed Aug 28, 2023
1 parent 1afca41 commit 0425d00
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions fastcrypto/src/groups/class_group/bigint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ pub struct EuclideanAlgorithmOutput {
pub b_divided_by_gcd: BigInt,
}

impl EuclideanAlgorithmOutput {
fn flip(self) -> Self {
Self {
gcd: self.gcd,
x: self.y,
y: self.x,
a_divided_by_gcd: self.b_divided_by_gcd,
b_divided_by_gcd: self.a_divided_by_gcd,
}
}
}

/// Compute the greatest common divisor gcd of a and b. The output also returns the Bezout coefficients
/// x and y such that ax + by = gcd and also the quotients a / gcd and b / gcd.
pub fn extended_euclidean_algorithm(a: &BigInt, b: &BigInt) -> EuclideanAlgorithmOutput {
if b < a {
return extended_euclidean_algorithm(b, a).flip();
}

let mut s = (BigInt::zero(), BigInt::one());
let mut t = (BigInt::one(), BigInt::zero());
let mut r = (a.clone(), b.clone());
Expand All @@ -27,10 +43,10 @@ pub fn extended_euclidean_algorithm(a: &BigInt, b: &BigInt) -> EuclideanAlgorith
r.1 = r.0;
r.0 = r_prime;

let f = |mut r: (BigInt, BigInt)| {
mem::swap(&mut r.0, &mut r.1);
r.0 -= &q * &r.1;
r
let f = |mut x: (BigInt, BigInt)| {
mem::swap(&mut x.0, &mut x.1);
x.0 -= &q * &x.1;
x
};
s = f(s);
t = f(t);
Expand Down

0 comments on commit 0425d00

Please sign in to comment.