@@ -36,6 +36,19 @@ impl BoxedUint {
3636 Limb :: BITS * ( i as u32 + 1 ) - limb. leading_zeros ( )
3737 }
3838
39+ /// Returns `true` if the bit at position `index` is set, `false` otherwise.
40+ ///
41+ /// # Remarks
42+ /// This operation is variable time with respect to `index` only.
43+ #[ inline( always) ]
44+ pub fn bit_vartime ( & self , index : u32 ) -> bool {
45+ if index >= self . bits_precision ( ) {
46+ false
47+ } else {
48+ ( self . limbs [ ( index / Limb :: BITS ) as usize ] . 0 >> ( index % Limb :: BITS ) ) & 1 == 1
49+ }
50+ }
51+
3952 /// Get the precision of this [`BoxedUint`] in bits.
4053 pub fn bits_precision ( & self ) -> u32 {
4154 self . limbs . len ( ) as u32 * Limb :: BITS
@@ -119,4 +132,16 @@ mod tests {
119132 u. set_bit ( 150 , Choice :: from ( 0 ) ) ;
120133 assert_eq ! ( u, uint_with_bits_at( & [ 16 , 79 ] ) ) ;
121134 }
135+
136+ #[ test]
137+ fn bit_vartime ( ) {
138+ let u = uint_with_bits_at ( & [ 16 , 48 , 112 , 127 , 255 ] ) ;
139+ assert ! ( !u. bit_vartime( 0 ) ) ;
140+ assert ! ( !u. bit_vartime( 1 ) ) ;
141+ assert ! ( u. bit_vartime( 16 ) ) ;
142+ assert ! ( u. bit_vartime( 127 ) ) ;
143+ assert ! ( u. bit_vartime( 255 ) ) ;
144+ assert ! ( !u. bit_vartime( 256 ) ) ;
145+ assert ! ( !u. bit_vartime( 260 ) ) ;
146+ }
122147}
0 commit comments