1- //! Implementation of Bernstein-Yang modular inversion and GCD algorithm as described in:
2- //! <https://eprint.iacr.org/2019/266>.
1+ //! Implementation of Bernstein-Yang modular inversion and GCD algorithm (a.k.a. safegcd)
2+ //! as described in: <https://eprint.iacr.org/2019/266>.
33//!
44//! See parent module for more information.
55
@@ -14,9 +14,9 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreate
1414
1515/// Modular multiplicative inverter based on the Bernstein-Yang method.
1616///
17- /// See [`super::BernsteinYangInverter `] for more information.
17+ /// See [`super::SafeGcdInverter `] for more information.
1818#[ derive( Clone , Debug ) ]
19- pub struct BoxedBernsteinYangInverter {
19+ pub struct BoxedSafeGcdInverter {
2020 /// Modulus
2121 pub ( crate ) modulus : BoxedUnsatInt ,
2222
@@ -27,7 +27,7 @@ pub struct BoxedBernsteinYangInverter {
2727 inverse : i64 ,
2828}
2929
30- impl BoxedBernsteinYangInverter {
30+ impl BoxedSafeGcdInverter {
3131 /// Creates the inverter for specified modulus and adjusting parameter.
3232 ///
3333 /// Modulus must be odd. Returns `None` if it is not.
@@ -50,7 +50,7 @@ impl BoxedBernsteinYangInverter {
5050 }
5151}
5252
53- impl Inverter for BoxedBernsteinYangInverter {
53+ impl Inverter for BoxedSafeGcdInverter {
5454 type Output = BoxedUint ;
5555
5656 fn invert ( & self , value : & BoxedUint ) -> CtOption < Self :: Output > {
@@ -78,7 +78,7 @@ fn unsat_nlimbs_for_sat_nlimbs(saturated_nlimbs: usize) -> usize {
7878 saturated_nlimbs
7979 } ;
8080
81- bernstein_yang_nlimbs ! ( saturated_nlimbs * Limb :: BITS as usize )
81+ safegcd_nlimbs ! ( saturated_nlimbs * Limb :: BITS as usize )
8282}
8383
8484/// Returns the greatest common divisor (GCD) of the two given numbers.
@@ -300,10 +300,7 @@ impl BoxedUnsatInt {
300300 bits_precision = 64 ;
301301 }
302302
303- debug_assert_eq ! (
304- self . nlimbs( ) ,
305- bernstein_yang_nlimbs!( bits_precision as usize )
306- ) ;
303+ debug_assert_eq ! ( self . nlimbs( ) , safegcd_nlimbs!( bits_precision as usize ) ) ;
307304 assert ! (
308305 !bool :: from( self . is_negative( ) ) ,
309306 "can't convert negative number to BoxedUint"
@@ -522,7 +519,7 @@ mod tests {
522519 use subtle:: ConstantTimeEq ;
523520
524521 #[ cfg( not( miri) ) ]
525- use crate :: modular:: bernstein_yang :: UnsatInt ;
522+ use crate :: modular:: safegcd :: UnsatInt ;
526523
527524 impl PartialEq for BoxedUnsatInt {
528525 fn eq ( & self , other : & Self ) -> bool {
@@ -669,8 +666,8 @@ mod tests {
669666 #[ test]
670667 #[ cfg( not( miri) ) ]
671668 fn boxed_unsatint_add( x in u256( ) , y in u256( ) ) {
672- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
673- let y_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & y) ;
669+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
670+ let y_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & y) ;
674671 let mut x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
675672 let y_boxed = BoxedUnsatInt :: from( & y. into( ) ) ;
676673
@@ -682,7 +679,7 @@ mod tests {
682679 #[ test]
683680 #[ cfg( not( miri) ) ]
684681 fn boxed_unsatint_mul( x in u256( ) , y in any:: <i64 >( ) ) {
685- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
682+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
686683 let x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
687684
688685 let expected = x_ref. mul( y) ;
@@ -693,7 +690,7 @@ mod tests {
693690 #[ test]
694691 #[ cfg( not( miri) ) ]
695692 fn boxed_unsatint_neg( x in u256( ) ) {
696- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
693+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
697694 let x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
698695
699696 let expected = x_ref. neg( ) ;
@@ -704,7 +701,7 @@ mod tests {
704701 #[ test]
705702 #[ cfg( not( miri) ) ]
706703 fn boxed_unsatint_shr( x in u256( ) ) {
707- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
704+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
708705 let mut x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
709706 x_boxed. shr_assign( ) ;
710707
@@ -716,7 +713,7 @@ mod tests {
716713 #[ cfg( not( miri) ) ]
717714
718715 fn boxed_unsatint_is_negative( x in u256( ) ) {
719- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
716+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
720717 let x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
721718 assert_eq!( x_ref. is_negative( ) . to_bool_vartime( ) , bool :: from( x_boxed. is_negative( ) ) ) ;
722719 }
@@ -725,7 +722,7 @@ mod tests {
725722 #[ cfg( not( miri) ) ]
726723
727724 fn boxed_unsatint_is_minus_one( x in u256( ) ) {
728- let x_ref = UnsatInt :: <{ bernstein_yang_nlimbs !( 256usize ) } >:: from_uint( & x) ;
725+ let x_ref = UnsatInt :: <{ safegcd_nlimbs !( 256usize ) } >:: from_uint( & x) ;
729726 let x_boxed = BoxedUnsatInt :: from( & x. into( ) ) ;
730727 assert!( bool :: from( x_boxed. is_minus_one( ) . ct_eq( & x_ref. eq( & UnsatInt :: MINUS_ONE ) . into( ) ) ) ) ;
731728 }
0 commit comments