@@ -95,6 +95,7 @@ pub trait Integer:
95
95
+ for < ' a > BitXor < & ' a Self , Output = Self >
96
96
+ BitXorAssign
97
97
+ for < ' a > BitXorAssign < & ' a Self >
98
+ + BitOps
98
99
+ CheckedAdd
99
100
+ CheckedSub
100
101
+ CheckedMul
@@ -154,24 +155,6 @@ pub trait Integer:
154
155
/// The value `1`.
155
156
fn one ( ) -> Self ;
156
157
157
- /// Calculate the number of bits required to represent a given number.
158
- fn bits ( & self ) -> u32 ;
159
-
160
- /// Calculate the number of bits required to represent a given number in variable-time with
161
- /// respect to `self`.
162
- fn bits_vartime ( & self ) -> u32 ;
163
-
164
- /// Precision of this integer in bits.
165
- fn bits_precision ( & self ) -> u32 ;
166
-
167
- /// Precision of this integer in bytes.
168
- fn bytes_precision ( & self ) -> usize ;
169
-
170
- /// Calculate the number of leading zeros in the binary representation of this number.
171
- fn leading_zeros ( & self ) -> u32 {
172
- self . bits_precision ( ) - self . bits ( )
173
- }
174
-
175
158
/// Number of limbs in this integer.
176
159
fn nlimbs ( & self ) -> usize ;
177
160
@@ -496,6 +479,69 @@ pub trait RemLimb: Sized {
496
479
fn rem_limb_with_reciprocal ( & self , reciprocal : & Reciprocal ) -> Limb ;
497
480
}
498
481
482
+ /// Bit counting and bit operations.
483
+ pub trait BitOps {
484
+ /// Precision of this integer in bits.
485
+ fn bits_precision ( & self ) -> u32 ;
486
+
487
+ /// `floor(log2(self.bits_precision()))`.
488
+ fn log2_bits ( & self ) -> u32 {
489
+ u32:: BITS - self . bits_precision ( ) . leading_zeros ( ) - 1
490
+ }
491
+
492
+ /// Precision of this integer in bytes.
493
+ fn bytes_precision ( & self ) -> usize ;
494
+
495
+ /// Calculate the number of bits needed to represent this number.
496
+ fn bit ( & self , index : u32 ) -> Choice ;
497
+
498
+ /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`.
499
+ fn set_bit ( & mut self , index : u32 , bit_value : Choice ) ;
500
+
501
+ /// Calculate the number of bits required to represent a given number.
502
+ fn bits ( & self ) -> u32 {
503
+ self . bits_precision ( ) - self . leading_zeros ( )
504
+ }
505
+
506
+ /// Calculate the number of trailing zeros in the binary representation of this number.
507
+ fn trailing_zeros ( & self ) -> u32 ;
508
+
509
+ /// Calculate the number of trailing ones in the binary representation of this number.
510
+ fn trailing_ones ( & self ) -> u32 ;
511
+
512
+ /// Calculate the number of leading zeros in the binary representation of this number.
513
+ fn leading_zeros ( & self ) -> u32 ;
514
+
515
+ /// Returns `true` if the bit at position `index` is set, `false` otherwise.
516
+ ///
517
+ /// # Remarks
518
+ /// This operation is variable time with respect to `index` only.
519
+ fn bit_vartime ( & self , index : u32 ) -> bool ;
520
+
521
+ /// Calculate the number of bits required to represent a given number in variable-time with
522
+ /// respect to `self`.
523
+ fn bits_vartime ( & self ) -> u32 {
524
+ self . bits_precision ( ) - self . leading_zeros_vartime ( )
525
+ }
526
+
527
+ /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`,
528
+ /// variable time in `self`.
529
+ fn set_bit_vartime ( & mut self , index : u32 , bit_value : bool ) ;
530
+
531
+ /// Calculate the number of leading zeros in the binary representation of this number.
532
+ fn leading_zeros_vartime ( & self ) -> u32 {
533
+ self . bits_precision ( ) - self . bits_vartime ( )
534
+ }
535
+
536
+ /// Calculate the number of trailing zeros in the binary representation of this number in
537
+ /// variable-time with respect to `self`.
538
+ fn trailing_zeros_vartime ( & self ) -> u32 ;
539
+
540
+ /// Calculate the number of trailing ones in the binary representation of this number,
541
+ /// variable time in `self`.
542
+ fn trailing_ones_vartime ( & self ) -> u32 ;
543
+ }
544
+
499
545
/// Constant-time exponentiation.
500
546
pub trait Pow < Exponent > {
501
547
/// Raises to the `exponent` power.
0 commit comments