@@ -13,7 +13,7 @@ use elliptic_curve::{
1313 Array , ArraySize ,
1414 typenum:: { Prod , Unsigned } ,
1515 } ,
16- bigint:: { Limb , NonZero , U448 , U896 , Word , Zero } ,
16+ bigint:: { Integer , Limb , NonZero , U448 , U896 , Word , Zero } ,
1717 consts:: U2 ,
1818 ff:: { Field , helpers} ,
1919 ops:: { Invert , Reduce , ReduceNonZero } ,
@@ -662,6 +662,18 @@ impl<C: CurveWithScalar> Scalar<C> {
662662 /// This is used in the 2-isogeny when mapping points from Ed448-Goldilocks
663663 /// to Twisted-Goldilocks
664664 pub ( crate ) fn div_by_four ( & mut self ) {
665+ let s_mod_4 = self [ 0 ] & 3 ;
666+
667+ let s_plus_l = self . scalar + ORDER ;
668+ let s_plus_2l = s_plus_l + ORDER ;
669+ let s_plus_3l = s_plus_2l + ORDER ;
670+
671+ self . scalar . conditional_assign ( & s_plus_l, s_mod_4. ct_eq ( & 1 ) ) ;
672+ self . scalar
673+ . conditional_assign ( & s_plus_2l, s_mod_4. ct_eq ( & 2 ) ) ;
674+ self . scalar
675+ . conditional_assign ( & s_plus_3l, s_mod_4. ct_eq ( & 3 ) ) ;
676+
665677 self . scalar >>= 2 ;
666678 }
667679
@@ -778,8 +790,12 @@ impl<C: CurveWithScalar> Scalar<C> {
778790 }
779791
780792 /// Halves a Scalar modulo the prime
781- pub const fn halve ( & self ) -> Self {
782- Self :: new ( self . scalar . shr_vartime ( 1 ) )
793+ pub fn halve ( & self ) -> Self {
794+ let is_odd = self . scalar . is_odd ( ) ;
795+ let if_odd = self . scalar + * ORDER ;
796+ let scalar = U448 :: conditional_select ( & self . scalar , & if_odd, is_odd) ;
797+
798+ Self :: new ( scalar >> 1 )
783799 }
784800
785801 /// Attempt to construct a `Scalar` from a canonical byte representation.
0 commit comments