@@ -11,8 +11,8 @@ pub(crate) use sealed::PrecomputeInverterWithAdjuster;
11
11
use crate :: { Limb , NonZero } ;
12
12
use core:: fmt:: Debug ;
13
13
use core:: ops:: {
14
- Add , BitAnd , BitAndAssign , BitOr , BitOrAssign , BitXor , BitXorAssign , Div , DivAssign , Mul , Not ,
15
- Rem , Shl , ShlAssign , Shr , ShrAssign , Sub ,
14
+ Add , AddAssign , BitAnd , BitAndAssign , BitOr , BitOrAssign , BitXor , BitXorAssign , Div , DivAssign ,
15
+ Mul , MulAssign , Neg , Not , Rem , Shl , ShlAssign , Shr , ShrAssign , Sub , SubAssign ,
16
16
} ;
17
17
use subtle:: {
18
18
Choice , ConditionallySelectable , ConstantTimeEq , ConstantTimeGreater , ConstantTimeLess ,
@@ -141,6 +141,10 @@ pub trait Integer:
141
141
+ WrappingShr
142
142
+ Zero
143
143
{
144
+ /// The corresponding Montgomery representation,
145
+ /// optimized for the performance of modular operations at the price of a conversion overhead.
146
+ type Monty : Monty < Integer = Self > ;
147
+
144
148
/// The value `1`.
145
149
fn one ( ) -> Self ;
146
150
@@ -425,14 +429,16 @@ pub trait Encoding: Sized {
425
429
}
426
430
427
431
/// Support for optimized squaring
428
- pub trait Square : Sized
429
- where
430
- for < ' a > & ' a Self : Mul < & ' a Self , Output = Self > ,
431
- {
432
- /// Computes the same as `self.mul(self)`, but may be more efficient.
433
- fn square ( & self ) -> Self {
434
- self * self
435
- }
432
+ pub trait Square {
433
+ /// Computes the same as `self * self`, but may be more efficient.
434
+ fn square ( & self ) -> Self ;
435
+ }
436
+
437
+ /// Support for optimized squaring in-place
438
+ pub trait SquareAssign {
439
+ /// Computes the same as `self * self`, but may be more efficient.
440
+ /// Writes the result in `self`.
441
+ fn square_assign ( & mut self ) ;
436
442
}
437
443
438
444
/// Constant-time exponentiation.
@@ -513,3 +519,50 @@ pub trait WideningMul<Rhs = Self>: Sized {
513
519
/// Perform widening multiplication.
514
520
fn widening_mul ( & self , rhs : Rhs ) -> Self :: Output ;
515
521
}
522
+
523
+ /// A representation of an integer optimized for the performance of modular operations.
524
+ pub trait Monty :
525
+ ' static
526
+ + Clone
527
+ + Debug
528
+ + Eq
529
+ + Sized
530
+ + Send
531
+ + Sync
532
+ + Add < Output = Self >
533
+ + for < ' a > Add < & ' a Self , Output = Self >
534
+ + AddAssign
535
+ + for < ' a > AddAssign < & ' a Self >
536
+ + Sub < Output = Self >
537
+ + for < ' a > Sub < & ' a Self , Output = Self >
538
+ + SubAssign
539
+ + for < ' a > SubAssign < & ' a Self >
540
+ + Mul < Output = Self >
541
+ + for < ' a > Mul < & ' a Self , Output = Self >
542
+ + MulAssign
543
+ + for < ' a > MulAssign < & ' a Self >
544
+ + Neg < Output = Self >
545
+ + PowBoundedExp < Self :: Integer >
546
+ + Square
547
+ + SquareAssign
548
+ {
549
+ /// The original integer type.
550
+ type Integer : Integer < Monty = Self > ;
551
+
552
+ /// The precomputed data needed for this representation.
553
+ type Params : Clone ;
554
+
555
+ /// Create the precomputed data for Montgomery representation of integers modulo `modulus`.
556
+ ///
557
+ /// `modulus` must be odd, otherwise returns `None`.
558
+ fn new_params ( modulus : Self :: Integer ) -> CtOption < Self :: Params > ;
559
+
560
+ /// Convert the value into the representation using precomputed data.
561
+ fn new ( value : Self :: Integer , params : Self :: Params ) -> Self ;
562
+
563
+ /// Returns zero in this representation.
564
+ fn zero ( params : Self :: Params ) -> Self ;
565
+
566
+ /// Returns one in this representation.
567
+ fn one ( params : Self :: Params ) -> Self ;
568
+ }
0 commit comments