File tree Expand file tree Collapse file tree 4 files changed +19
-12
lines changed Expand file tree Collapse file tree 4 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -135,9 +135,16 @@ pub trait Zero: ConstantTimeEq + Sized {
135135 /// # Returns
136136 ///
137137 /// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
138+ #[ inline]
138139 fn is_zero ( & self ) -> Choice {
139140 self . ct_eq ( & Self :: zero ( ) )
140141 }
142+
143+ /// Set `self` to its additive identity, i.e. `Self::zero`.
144+ #[ inline]
145+ fn set_zero ( & mut self ) {
146+ * self = Zero :: zero ( ) ;
147+ }
141148}
142149
143150/// Trait for associating a constant representing zero.
@@ -149,6 +156,7 @@ pub trait ZeroConstant: Zero {
149156}
150157
151158impl < T : ZeroConstant > Zero for T {
159+ #[ inline( always) ]
152160 fn zero ( ) -> T {
153161 Self :: ZERO
154162 }
Original file line number Diff line number Diff line change @@ -231,13 +231,8 @@ impl BoxedUint {
231231 limbs. into ( )
232232 }
233233
234- /// Set the value of `self` to zero in-place.
235- pub ( crate ) fn set_to_zero ( & mut self ) {
236- self . limbs . as_mut ( ) . fill ( Limb :: ZERO )
237- }
238-
239234 /// Set the value of `self` to zero in-place if `choice` is truthy.
240- pub ( crate ) fn conditional_set_to_zero ( & mut self , choice : Choice ) {
235+ pub ( crate ) fn conditional_set_zero ( & mut self , choice : Choice ) {
241236 let nlimbs = self . nlimbs ( ) ;
242237 let limbs = self . limbs . as_mut ( ) ;
243238 for i in 0 ..nlimbs {
@@ -402,6 +397,10 @@ impl Zero for BoxedUint {
402397 fn is_zero ( & self ) -> Choice {
403398 self . is_zero ( )
404399 }
400+
401+ fn set_zero ( & mut self ) {
402+ self . limbs . as_mut ( ) . fill ( Limb :: ZERO )
403+ }
405404}
406405
407406#[ cfg( feature = "zeroize" ) ]
Original file line number Diff line number Diff line change 11//! [`BoxedUint`] bitwise left shift operations.
22
3- use crate :: { BoxedUint , Limb } ;
3+ use crate :: { BoxedUint , Limb , Zero } ;
44use core:: ops:: { Shl , ShlAssign } ;
55use subtle:: { Choice , ConstantTimeLess } ;
66
@@ -20,15 +20,15 @@ impl BoxedUint {
2020
2121 for i in 0 ..shift_bits {
2222 let bit = Choice :: from ( ( ( shift >> i) & 1 ) as u8 ) ;
23- temp. set_to_zero ( ) ;
23+ temp. set_zero ( ) ;
2424 // Will not overflow by construction
2525 result
2626 . shl_vartime_into ( & mut temp, 1 << i)
2727 . expect ( "shift within range" ) ;
2828 result. conditional_assign ( & temp, bit) ;
2929 }
3030
31- result. conditional_set_to_zero ( overflow) ;
31+ result. conditional_set_zero ( overflow) ;
3232
3333 ( result, overflow)
3434 }
Original file line number Diff line number Diff line change 11//! [`BoxedUint`] bitwise right shift operations.
22
3- use crate :: { BoxedUint , Limb } ;
3+ use crate :: { BoxedUint , Limb , Zero } ;
44use core:: ops:: { Shr , ShrAssign } ;
55use subtle:: { Choice , ConstantTimeLess } ;
66
@@ -20,15 +20,15 @@ impl BoxedUint {
2020
2121 for i in 0 ..shift_bits {
2222 let bit = Choice :: from ( ( ( shift >> i) & 1 ) as u8 ) ;
23- temp. set_to_zero ( ) ;
23+ temp. set_zero ( ) ;
2424 // Will not overflow by construction
2525 result
2626 . shr_vartime_into ( & mut temp, 1 << i)
2727 . expect ( "shift within range" ) ;
2828 result. conditional_assign ( & temp, bit) ;
2929 }
3030
31- result. conditional_set_to_zero ( overflow) ;
31+ result. conditional_set_zero ( overflow) ;
3232
3333 ( result, overflow)
3434 }
You can’t perform that action at this time.
0 commit comments