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 {
135
135
/// # Returns
136
136
///
137
137
/// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
138
+ #[ inline]
138
139
fn is_zero ( & self ) -> Choice {
139
140
self . ct_eq ( & Self :: zero ( ) )
140
141
}
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
+ }
141
148
}
142
149
143
150
/// Trait for associating a constant representing zero.
@@ -149,6 +156,7 @@ pub trait ZeroConstant: Zero {
149
156
}
150
157
151
158
impl < T : ZeroConstant > Zero for T {
159
+ #[ inline( always) ]
152
160
fn zero ( ) -> T {
153
161
Self :: ZERO
154
162
}
Original file line number Diff line number Diff line change @@ -231,13 +231,8 @@ impl BoxedUint {
231
231
limbs. into ( )
232
232
}
233
233
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
-
239
234
/// 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 ) {
241
236
let nlimbs = self . nlimbs ( ) ;
242
237
let limbs = self . limbs . as_mut ( ) ;
243
238
for i in 0 ..nlimbs {
@@ -402,6 +397,10 @@ impl Zero for BoxedUint {
402
397
fn is_zero ( & self ) -> Choice {
403
398
self . is_zero ( )
404
399
}
400
+
401
+ fn set_zero ( & mut self ) {
402
+ self . limbs . as_mut ( ) . fill ( Limb :: ZERO )
403
+ }
405
404
}
406
405
407
406
#[ cfg( feature = "zeroize" ) ]
Original file line number Diff line number Diff line change 1
1
//! [`BoxedUint`] bitwise left shift operations.
2
2
3
- use crate :: { BoxedUint , Limb } ;
3
+ use crate :: { BoxedUint , Limb , Zero } ;
4
4
use core:: ops:: { Shl , ShlAssign } ;
5
5
use subtle:: { Choice , ConstantTimeLess } ;
6
6
@@ -20,15 +20,15 @@ impl BoxedUint {
20
20
21
21
for i in 0 ..shift_bits {
22
22
let bit = Choice :: from ( ( ( shift >> i) & 1 ) as u8 ) ;
23
- temp. set_to_zero ( ) ;
23
+ temp. set_zero ( ) ;
24
24
// Will not overflow by construction
25
25
result
26
26
. shl_vartime_into ( & mut temp, 1 << i)
27
27
. expect ( "shift within range" ) ;
28
28
result. conditional_assign ( & temp, bit) ;
29
29
}
30
30
31
- result. conditional_set_to_zero ( overflow) ;
31
+ result. conditional_set_zero ( overflow) ;
32
32
33
33
( result, overflow)
34
34
}
Original file line number Diff line number Diff line change 1
1
//! [`BoxedUint`] bitwise right shift operations.
2
2
3
- use crate :: { BoxedUint , Limb } ;
3
+ use crate :: { BoxedUint , Limb , Zero } ;
4
4
use core:: ops:: { Shr , ShrAssign } ;
5
5
use subtle:: { Choice , ConstantTimeLess } ;
6
6
@@ -20,15 +20,15 @@ impl BoxedUint {
20
20
21
21
for i in 0 ..shift_bits {
22
22
let bit = Choice :: from ( ( ( shift >> i) & 1 ) as u8 ) ;
23
- temp. set_to_zero ( ) ;
23
+ temp. set_zero ( ) ;
24
24
// Will not overflow by construction
25
25
result
26
26
. shr_vartime_into ( & mut temp, 1 << i)
27
27
. expect ( "shift within range" ) ;
28
28
result. conditional_assign ( & temp, bit) ;
29
29
}
30
30
31
- result. conditional_set_to_zero ( overflow) ;
31
+ result. conditional_set_zero ( overflow) ;
32
32
33
33
( result, overflow)
34
34
}
You can’t perform that action at this time.
0 commit comments