@@ -74,6 +74,24 @@ impl Inverter for BoxedBernsteinYangInverter {
7474 }
7575}
7676
77+ /// Returns the greatest common divisor (GCD) of the two given numbers.
78+ pub ( crate ) fn gcd ( f : & BoxedUint , g : & BoxedUint ) -> BoxedUint {
79+ let bits_precision = f. bits_precision ( ) ;
80+ let inverse = inv_mod2_62 ( f. as_words ( ) ) ;
81+ let f = BoxedInt62L :: from ( f) ;
82+ let mut g = BoxedInt62L :: from ( g) ;
83+ let mut d = BoxedInt62L :: zero ( f. 0 . len ( ) ) ;
84+ let e = BoxedInt62L :: one ( f. 0 . len ( ) ) ;
85+
86+ let mut f = divsteps ( & mut d, & e, & f, & mut g, inverse) ;
87+
88+ if f. is_negative ( ) {
89+ f = f. neg ( ) ;
90+ }
91+
92+ f. to_uint ( bits_precision)
93+ }
94+
7795/// Algorithm `divsteps2` to compute (δₙ, fₙ, gₙ) = divstepⁿ(δ, f, g) as described in Figure 10.1
7896/// of <https://eprint.iacr.org/2019/266.pdf>.
7997fn divsteps (
@@ -322,6 +340,13 @@ impl BoxedInt62L {
322340 Self ( vec ! [ 0 ; nlimbs] . into ( ) )
323341 }
324342
343+ /// Get the value zero for the given number of limbs.
344+ pub fn one ( nlimbs : usize ) -> Self {
345+ let mut ret = Self :: zero ( nlimbs) ;
346+ ret. 0 [ 0 ] = 0 ;
347+ ret
348+ }
349+
325350 /// Widen self to the given number of limbs.
326351 pub fn widen ( self , nlimbs : usize ) -> Self {
327352 let mut limbs = Vec :: from ( self . 0 ) ;
0 commit comments