Skip to main content

crypto_bigint/
traits.rs

1//! Traits provided by this crate
2
3pub use core::ops::{
4    Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign,
5    Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
6};
7pub use ctutils::{CtAssign, CtEq, CtGt, CtLt, CtNeg, CtSelect};
8pub use num_traits::{
9    ConstOne, ConstZero, WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr,
10    WrappingSub,
11};
12
13use crate::{
14    Choice, CtOption, Limb, NonZero, Odd, Reciprocal, UintRef,
15    modular::{MontyParams, Retrieve},
16    primitives::u32_bits,
17};
18use core::fmt::{self, Debug};
19
20#[cfg(feature = "rand_core")]
21use rand_core::{Rng, TryRng};
22
23/// Integers whose representation takes a bounded amount of space.
24pub trait Bounded {
25    /// Size of this integer in bits.
26    const BITS: u32;
27
28    /// Size of this integer in bytes.
29    const BYTES: usize;
30}
31
32/// Integer trait: represents common functionality of integer types provided by this crate.
33pub trait Integer:
34    'static
35    + sealed::Sealed
36    + Add<Output = Self>
37    + for<'a> Add<&'a Self, Output = Self>
38    + AddAssign<Self>
39    + for<'a> AddAssign<&'a Self>
40    + AsRef<[Limb]>
41    + AsMut<[Limb]>
42    + BitAnd<Output = Self>
43    + for<'a> BitAnd<&'a Self, Output = Self>
44    + BitAndAssign
45    + for<'a> BitAndAssign<&'a Self>
46    + BitOr<Output = Self>
47    + for<'a> BitOr<&'a Self, Output = Self>
48    + BitOrAssign
49    + for<'a> BitOrAssign<&'a Self>
50    + BitXor<Output = Self>
51    + for<'a> BitXor<&'a Self, Output = Self>
52    + BitXorAssign
53    + for<'a> BitXorAssign<&'a Self>
54    + CheckedAdd
55    + CheckedSub
56    + CheckedMul
57    + CheckedDiv
58    + CheckedSquareRoot<Output = Self>
59    + Clone
60    + CtAssign
61    + CtEq
62    + CtGt
63    + CtLt
64    + CtSelect
65    + Debug
66    + Default
67    + DivAssign<NonZero<Self>>
68    + for<'a> DivAssign<&'a NonZero<Self>>
69    + Eq
70    + fmt::LowerHex
71    + fmt::UpperHex
72    + fmt::Binary
73    + Mul<Output = Self>
74    + for<'a> Mul<&'a Self, Output = Self>
75    + MulAssign<Self>
76    + for<'a> MulAssign<&'a Self>
77    + Not<Output = Self>
78    + One
79    + Ord
80    + Rem<NonZero<Self>, Output = Self>
81    + for<'a> Rem<&'a NonZero<Self>, Output = Self>
82    + RemAssign<NonZero<Self>>
83    + for<'a> RemAssign<&'a NonZero<Self>>
84    + Send
85    + Sized
86    + Shl<u32, Output = Self>
87    + ShlAssign<u32>
88    + ShlVartime
89    + Shr<u32, Output = Self>
90    + ShrAssign<u32>
91    + ShrVartime
92    + Sub<Output = Self>
93    + for<'a> Sub<&'a Self, Output = Self>
94    + SubAssign<Self>
95    + for<'a> SubAssign<&'a Self>
96    + Sync
97    + WrappingAdd
98    + WrappingSub
99    + WrappingMul
100    + WrappingNeg
101    + WrappingShl
102    + WrappingShr
103    + Zero
104{
105    /// Borrow the raw limbs used to represent this integer.
106    #[must_use]
107    fn as_limbs(&self) -> &[Limb];
108
109    /// Mutably borrow the raw limbs used to represent this integer.
110    #[must_use]
111    fn as_mut_limbs(&mut self) -> &mut [Limb];
112
113    /// Number of limbs in this integer.
114    #[must_use]
115    fn nlimbs(&self) -> usize;
116
117    /// Is this integer value an odd number?
118    ///
119    /// # Returns
120    ///
121    /// If odd, returns `Choice::FALSE`. Otherwise, returns `Choice::TRUE`.
122    #[must_use]
123    fn is_odd(&self) -> Choice {
124        self.as_ref()
125            .first()
126            .copied()
127            .map_or(Choice::FALSE, Limb::is_odd)
128    }
129
130    /// Is this integer value an even number?
131    ///
132    /// # Returns
133    ///
134    /// If even, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
135    #[must_use]
136    fn is_even(&self) -> Choice {
137        !self.is_odd()
138    }
139}
140
141/// Signed [`Integer`]s.
142pub trait Signed:
143    Div<NonZero<Self>, Output = CtOption<Self>>
144    + for<'a> Div<&'a NonZero<Self>, Output = CtOption<Self>>
145    + From<i8>
146    + From<i16>
147    + From<i32>
148    + From<i64>
149    + Gcd<Output = Self::Unsigned>
150    + Integer // + CtNeg TODO(tarcieri)
151{
152    /// Corresponding unsigned integer type.
153    type Unsigned: Unsigned;
154
155    /// The sign and magnitude of this [`Signed`].
156    #[must_use]
157    fn abs_sign(&self) -> (Self::Unsigned, Choice);
158
159    /// The magnitude of this [`Signed`].
160    #[must_use]
161    fn abs(&self) -> Self::Unsigned {
162        self.abs_sign().0
163    }
164
165    /// Whether this [`Signed`] is negative (and non-zero), as a [`Choice`].
166    #[must_use]
167    fn is_negative(&self) -> Choice;
168
169    /// Whether this [`Signed`] is positive (and non-zero), as a [`Choice`].
170    #[must_use]
171    fn is_positive(&self) -> Choice;
172}
173
174/// Unsigned [`Integer`]s.
175pub trait Unsigned:
176    AsRef<UintRef>
177    + AsMut<UintRef>
178    + AddMod<Output = Self>
179    + BitOps
180    + Div<NonZero<Self>, Output = Self>
181    + for<'a> Div<&'a NonZero<Self>, Output = Self>
182    + DivRemLimb
183    + FloorSquareRoot<Output = Self>
184    + From<u8>
185    + From<u16>
186    + From<u32>
187    + From<u64>
188    + From<Limb>
189    + Gcd<Output = Self>
190    + Integer
191    + MulMod<Output = Self>
192    + NegMod<Output = Self>
193    + RemLimb
194    + SquareMod<Output = Self>
195    + SubMod<Output = Self>
196{
197    /// Borrow the limbs of this unsigned integer as a [`UintRef`].
198    #[must_use]
199    fn as_uint_ref(&self) -> &UintRef;
200
201    /// Mutably borrow the limbs of this unsigned integer as a [`UintRef`].
202    fn as_mut_uint_ref(&mut self) -> &mut UintRef;
203
204    /// Returns an integer with the first limb set to `limb`, and the same precision as `other`.
205    #[must_use]
206    fn from_limb_like(limb: Limb, other: &Self) -> Self;
207}
208
209/// [`Unsigned`] integers with an associated [`MontyForm`].
210pub trait UnsignedWithMontyForm: Unsigned {
211    /// The corresponding Montgomery representation,
212    /// optimized for the performance of modular operations at the price of a conversion overhead.
213    type MontyForm: MontyForm<Integer = Self>;
214}
215
216/// Support for upgrading `UintRef`-compatible references into `Unsigned`.
217pub trait ToUnsigned: AsRef<UintRef> + AsMut<UintRef> {
218    /// The corresponding owned `Unsigned` type.
219    type Unsigned: Unsigned;
220
221    /// Convert from a reference into an owned instance.
222    fn to_unsigned(&self) -> Self::Unsigned;
223
224    /// Convert from a reference into an owned instance representing zero.
225    fn to_unsigned_zero(&self) -> Self::Unsigned {
226        let mut res = self.to_unsigned();
227        res.set_zero();
228        res
229    }
230}
231
232impl<T: Unsigned> ToUnsigned for T {
233    type Unsigned = T;
234
235    fn to_unsigned(&self) -> Self::Unsigned {
236        self.clone()
237    }
238
239    fn to_unsigned_zero(&self) -> Self::Unsigned {
240        T::zero_like(self)
241    }
242}
243
244/// Zero values: additive identity element for `Self`.
245pub trait Zero: CtEq + Sized {
246    /// Returns the additive identity element of `Self`, `0`.
247    #[must_use]
248    fn zero() -> Self;
249
250    /// Determine if this value is equal to `0`.
251    ///
252    /// # Returns
253    ///
254    /// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
255    #[inline]
256    #[must_use]
257    fn is_zero(&self) -> Choice {
258        self.ct_eq(&Self::zero())
259    }
260
261    /// Set `self` to its additive identity, i.e. `Self::zero`.
262    #[inline]
263    fn set_zero(&mut self) {
264        *self = Zero::zero();
265    }
266
267    /// Return the value `0` with the same precision as `other`.
268    #[must_use]
269    fn zero_like(other: &Self) -> Self
270    where
271        Self: Clone,
272    {
273        let mut ret = other.clone();
274        ret.set_zero();
275        ret
276    }
277}
278
279/// One values: multiplicative identity element for `Self`.
280pub trait One: CtEq + Sized {
281    /// Returns the multiplicative identity element of `Self`, `1`.
282    #[must_use]
283    fn one() -> Self;
284
285    /// Determine if this value is equal to `1`.
286    ///
287    /// # Returns
288    ///
289    /// If one, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
290    #[inline]
291    #[must_use]
292    fn is_one(&self) -> Choice {
293        self.ct_eq(&Self::one())
294    }
295
296    /// Set `self` to its multiplicative identity, i.e. `Self::one`.
297    #[inline]
298    fn set_one(&mut self) {
299        *self = One::one();
300    }
301
302    /// Return the value `0` with the same precision as `other`.
303    #[must_use]
304    fn one_like(_other: &Self) -> Self {
305        One::one()
306    }
307}
308
309/// Trait for associating constant values with a type.
310pub trait Constants: ConstZero + ConstOne {
311    /// Maximum value this integer can express.
312    const MAX: Self;
313}
314
315/// Fixed-width [`Integer`]s.
316pub trait FixedInteger: Bounded + Constants + Copy + Integer {
317    /// The number of limbs used on this platform.
318    const LIMBS: usize;
319}
320
321/// Compute the greatest common divisor of two integers.
322pub trait Gcd<Rhs = Self>: Sized {
323    /// Output type.
324    type Output;
325
326    /// Compute the greatest common divisor of `self` and `rhs`.
327    #[must_use]
328    fn gcd(&self, rhs: &Rhs) -> Self::Output;
329
330    /// Compute the greatest common divisor of `self` and `rhs` in variable time.
331    #[must_use]
332    fn gcd_vartime(&self, rhs: &Rhs) -> Self::Output;
333}
334
335/// Compute the extended greatest common divisor of two integers.
336pub trait Xgcd<Rhs = Self>: Sized {
337    /// Output type.
338    type Output;
339
340    /// Compute the extended greatest common divisor of `self` and `rhs`.
341    #[must_use]
342    fn xgcd(&self, rhs: &Rhs) -> Self::Output;
343
344    /// Compute the extended greatest common divisor of `self` and `rhs` in variable time.
345    #[must_use]
346    fn xgcd_vartime(&self, rhs: &Rhs) -> Self::Output;
347}
348
349/// Random number generation support.
350#[cfg(feature = "rand_core")]
351pub trait Random: Sized {
352    /// Generate a random value.
353    ///
354    /// If `rng` is a CSRNG, the generation is cryptographically secure as well.
355    ///
356    /// # Errors
357    /// - Returns `R::Error` in the event the RNG experienced an internal failure.
358    fn try_random_from_rng<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error>;
359
360    /// Generate a random value.
361    ///
362    /// If `rng` is a CSRNG, the generation is cryptographically secure as well.
363    #[must_use]
364    fn random_from_rng<R: Rng + ?Sized>(rng: &mut R) -> Self {
365        let Ok(out) = Self::try_random_from_rng(rng);
366        out
367    }
368
369    /// Randomly generate a value of this type using the system's ambient cryptographically secure
370    /// random number generator.
371    ///
372    /// # Errors
373    /// Returns [`getrandom::Error`] in the event the system's ambient RNG experiences an internal
374    /// failure.
375    #[cfg(feature = "getrandom")]
376    fn try_random() -> Result<Self, getrandom::Error> {
377        Self::try_random_from_rng(&mut getrandom::SysRng)
378    }
379
380    /// Randomly generate a value of this type using the system's ambient cryptographically secure
381    /// random number generator.
382    ///
383    /// # Panics
384    /// This method will panic in the event the system's ambient RNG experiences an internal
385    /// failure.
386    ///
387    /// This shouldn't happen on most modern operating systems.
388    #[cfg(feature = "getrandom")]
389    #[must_use]
390    fn random() -> Self {
391        Self::try_random().expect("RNG failure")
392    }
393}
394
395/// Possible errors of the methods in [`RandomBits`] trait.
396#[cfg(feature = "rand_core")]
397#[derive(Debug)]
398pub enum RandomBitsError<T> {
399    /// An error of the internal RNG library.
400    RandCore(T),
401    /// The requested `bits_precision` does not match the size of the integer
402    /// corresponding to the type (in the cases where this is set in compile time).
403    BitsPrecisionMismatch {
404        /// The requested precision.
405        bits_precision: u32,
406        /// The compile-time size of the integer.
407        integer_bits: u32,
408    },
409    /// The requested `bit_length` is larger than `bits_precision`.
410    BitLengthTooLarge {
411        /// The requested bit length of the random number.
412        bit_length: u32,
413        /// The requested precision.
414        bits_precision: u32,
415    },
416}
417
418#[cfg(feature = "rand_core")]
419impl<T> fmt::Display for RandomBitsError<T>
420where
421    T: fmt::Display,
422{
423    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
424        match self {
425            Self::RandCore(err) => write!(f, "{err}"),
426            Self::BitsPrecisionMismatch {
427                bits_precision,
428                integer_bits,
429            } => write!(
430                f,
431                concat![
432                    "The requested `bits_precision` ({}) does not match ",
433                    "the size of the integer corresponding to the type ({})"
434                ],
435                bits_precision, integer_bits
436            ),
437            Self::BitLengthTooLarge {
438                bit_length,
439                bits_precision,
440            } => write!(
441                f,
442                "The requested `bit_length` ({bit_length}) is larger than `bits_precision` ({bits_precision}).",
443            ),
444        }
445    }
446}
447
448#[cfg(feature = "rand_core")]
449impl<T> core::error::Error for RandomBitsError<T> where T: Debug + fmt::Display {}
450
451/// Random bits generation support.
452#[cfg(feature = "rand_core")]
453pub trait RandomBits: Sized {
454    /// Generate a random value in range `[0, 2^bit_length)`.
455    ///
456    /// A wrapper for [`RandomBits::try_random_bits`] that panics on error.
457    #[must_use]
458    fn random_bits<R: TryRng + ?Sized>(rng: &mut R, bit_length: u32) -> Self {
459        Self::try_random_bits(rng, bit_length).expect("try_random_bits() failed")
460    }
461
462    /// Generate a random value in range `[0, 2^bit_length)`.
463    ///
464    /// This method is variable time wrt `bit_length`.
465    ///
466    /// If `rng` is a CSRNG, the generation is cryptographically secure as well.
467    ///
468    /// # Errors
469    /// - Returns `R::Error` in the event the RNG experienced an internal failure.
470    fn try_random_bits<R: TryRng + ?Sized>(
471        rng: &mut R,
472        bit_length: u32,
473    ) -> Result<Self, RandomBitsError<R::Error>>;
474
475    /// Generate a random value in range `[0, 2^bit_length)`,
476    /// returning an integer with the closest available size to `bits_precision`
477    /// (if the implementing type supports runtime sizing).
478    ///
479    /// A wrapper for [`RandomBits::try_random_bits_with_precision`] that panics on error.
480    #[must_use]
481    #[track_caller]
482    fn random_bits_with_precision<R: TryRng + ?Sized>(
483        rng: &mut R,
484        bit_length: u32,
485        bits_precision: u32,
486    ) -> Self {
487        Self::try_random_bits_with_precision(rng, bit_length, bits_precision)
488            .expect("try_random_bits_with_precision() failed")
489    }
490
491    /// Generate a random value in range `[0, 2^bit_length)`,
492    /// returning an integer with the closest available size to `bits_precision`
493    /// (if the implementing type supports runtime sizing).
494    ///
495    /// This method is variable time wrt `bit_length`.
496    ///
497    /// If `rng` is a CSRNG, the generation is cryptographically secure as well.
498    ///
499    /// # Errors
500    /// - Returns `R::Error` in the event the RNG experienced an internal failure.
501    fn try_random_bits_with_precision<R: TryRng + ?Sized>(
502        rng: &mut R,
503        bit_length: u32,
504        bits_precision: u32,
505    ) -> Result<Self, RandomBitsError<R::Error>>;
506}
507
508/// Modular random number generation support.
509#[cfg(feature = "rand_core")]
510pub trait RandomMod: Sized + Zero {
511    /// Generate a random number which is less than a given `modulus`.
512    ///
513    /// This uses rejection sampling.
514    ///
515    /// As a result, it runs in variable time that depends in part on
516    /// `modulus`. If the generator `rng` is cryptographically secure (for
517    /// example, it implements `CryptoRng`), then this is guaranteed not to
518    /// leak anything about the output value aside from it being less than
519    /// `modulus`.
520    #[must_use]
521    fn random_mod_vartime<R: Rng + ?Sized>(rng: &mut R, modulus: &NonZero<Self>) -> Self {
522        let Ok(out) = Self::try_random_mod_vartime(rng, modulus);
523        out
524    }
525
526    /// Generate a random number which is less than a given `modulus`.
527    ///
528    /// This uses rejection sampling.
529    ///
530    /// As a result, it runs in variable time that depends in part on
531    /// `modulus`. If the generator `rng` is cryptographically secure (for
532    /// example, it implements `CryptoRng`), then this is guaranteed not to
533    /// leak anything about the output value aside from it being less than
534    /// `modulus`.
535    ///
536    /// # Errors
537    /// - Returns `R::Error` in the event the RNG experienced an internal failure.
538    fn try_random_mod_vartime<R: TryRng + ?Sized>(
539        rng: &mut R,
540        modulus: &NonZero<Self>,
541    ) -> Result<Self, R::Error>;
542
543    /// Generate a random number which is less than a given `modulus`.
544    ///
545    /// This uses rejection sampling.
546    ///
547    /// As a result, it runs in variable time that depends in part on
548    /// `modulus`. If the generator `rng` is cryptographically secure (for
549    /// example, it implements `CryptoRng`), then this is guaranteed not to
550    /// leak anything about the output value aside from it being less than
551    /// `modulus`.
552    #[deprecated(since = "0.7.0", note = "please use `random_mod_vartime` instead")]
553    #[must_use]
554    fn random_mod<R: Rng + ?Sized>(rng: &mut R, modulus: &NonZero<Self>) -> Self {
555        Self::random_mod_vartime(rng, modulus)
556    }
557
558    /// Generate a random number which is less than a given `modulus`.
559    ///
560    /// This uses rejection sampling.
561    ///
562    /// As a result, it runs in variable time that depends in part on
563    /// `modulus`. If the generator `rng` is cryptographically secure (for
564    /// example, it implements `CryptoRng`), then this is guaranteed not to
565    /// leak anything about the output value aside from it being less than
566    /// `modulus`.
567    ///
568    /// # Errors
569    /// - Returns `R::Error` in the event the RNG experienced an internal failure.
570    #[deprecated(since = "0.7.0", note = "please use `try_random_mod_vartime` instead")]
571    fn try_random_mod<R: TryRng + ?Sized>(
572        rng: &mut R,
573        modulus: &NonZero<Self>,
574    ) -> Result<Self, R::Error> {
575        Self::try_random_mod_vartime(rng, modulus)
576    }
577}
578
579/// Compute `self + rhs mod p`.
580pub trait AddMod<Rhs = Self, Mod = NonZero<Self>> {
581    /// Output type.
582    type Output;
583
584    /// Compute `self + rhs mod p`.
585    ///
586    /// Assumes `self` and `rhs` are `< p`.
587    #[must_use]
588    fn add_mod(&self, rhs: &Rhs, p: &Mod) -> Self::Output;
589}
590
591/// Compute `self - rhs mod p`.
592pub trait SubMod<Rhs = Self, Mod = NonZero<Self>> {
593    /// Output type.
594    type Output;
595
596    /// Compute `self - rhs mod p`.
597    ///
598    /// Assumes `self` and `rhs` are `< p`.
599    #[must_use]
600    fn sub_mod(&self, rhs: &Rhs, p: &Mod) -> Self::Output;
601}
602
603/// Compute `-self mod p`.
604pub trait NegMod<Mod = NonZero<Self>> {
605    /// Output type.
606    type Output;
607
608    /// Compute `-self mod p`.
609    #[must_use]
610    fn neg_mod(&self, p: &Mod) -> Self::Output;
611}
612
613/// Compute `self * rhs mod p`.
614pub trait MulMod<Rhs = Self, Mod = NonZero<Self>> {
615    /// Output type.
616    type Output;
617
618    /// Compute `self * rhs mod p`.
619    #[must_use]
620    fn mul_mod(&self, rhs: &Rhs, p: &Mod) -> Self::Output;
621}
622
623/// Compute `self * self mod p`.
624pub trait SquareMod<Mod = NonZero<Self>> {
625    /// Output type.
626    type Output;
627
628    /// Compute `self * self mod p`.
629    #[must_use]
630    fn square_mod(&self, p: &Mod) -> Self::Output;
631}
632
633/// Compute `1 / self mod p`.
634#[deprecated(since = "0.7.0", note = "please use `InvertMod` instead")]
635pub trait InvMod<Rhs = Self>: Sized {
636    /// Output type.
637    type Output;
638
639    /// Compute `1 / self mod p`.
640    #[must_use]
641    fn inv_mod(&self, p: &Rhs) -> CtOption<Self::Output>;
642}
643
644#[allow(deprecated)]
645impl<T, Rhs> InvMod<Rhs> for T
646where
647    T: InvertMod<Rhs>,
648{
649    type Output = <T as InvertMod<Rhs>>::Output;
650
651    fn inv_mod(&self, p: &Rhs) -> CtOption<Self::Output> {
652        self.invert_mod(p)
653    }
654}
655
656/// Compute `1 / self mod p`.
657pub trait InvertMod<Mod = NonZero<Self>>: Sized {
658    /// Output type.
659    type Output;
660
661    /// Compute `1 / self mod p`.
662    #[must_use]
663    fn invert_mod(&self, p: &Mod) -> CtOption<Self::Output>;
664}
665
666/// Checked addition.
667pub trait CheckedAdd<Rhs = Self>: Sized {
668    /// Perform checked addition, returning a [`CtOption`] which `is_some` only if the operation
669    /// did not overflow.
670    #[must_use]
671    fn checked_add(&self, rhs: &Rhs) -> CtOption<Self>;
672}
673
674/// Checked division.
675pub trait CheckedDiv<Rhs = Self>: Sized {
676    /// Perform checked division, returning a [`CtOption`] which `is_some` only if the divisor is
677    /// non-zero.
678    #[must_use]
679    fn checked_div(&self, rhs: &Rhs) -> CtOption<Self>;
680}
681
682/// Checked multiplication.
683pub trait CheckedMul<Rhs = Self>: Sized {
684    /// Perform checked multiplication, returning a [`CtOption`] which `is_some`
685    /// only if the operation did not overflow.
686    #[must_use]
687    fn checked_mul(&self, rhs: &Rhs) -> CtOption<Self>;
688}
689
690/// Checked subtraction.
691pub trait CheckedSub<Rhs = Self>: Sized {
692    /// Perform checked subtraction, returning a [`CtOption`] which `is_some`
693    /// only if the operation did not underflow.
694    #[must_use]
695    fn checked_sub(&self, rhs: &Rhs) -> CtOption<Self>;
696}
697
698/// Define the result of concatenating two numbers into a "wide" value.
699pub trait Concat<const HI: usize> {
700    /// Concatenated output: having the width of `Self` plus `HI`.
701    type Output: Integer;
702}
703
704/// Define the result of splitting a number into two parts, with the
705/// first part having the width `LO`.
706pub trait Split<const LO: usize> {
707    /// High limbs of output: having the width of `Self` minus `LO`.
708    type Output: Integer;
709}
710
711/// Define the result of splitting a number into two parts, with
712/// each part having an equal width.
713pub trait SplitEven {
714    /// Split output: each component having half the width of `Self`.
715    type Output: Integer;
716}
717
718/// Encoding support.
719pub trait Encoding: Sized {
720    /// Byte array representation.
721    type Repr: AsRef<[u8]>
722        + AsMut<[u8]>
723        + Clone
724        + Sized
725        + for<'a> TryFrom<&'a [u8], Error: core::error::Error>;
726
727    /// Decode from big endian bytes.
728    #[must_use]
729    fn from_be_bytes(bytes: Self::Repr) -> Self;
730
731    /// Decode from little endian bytes.
732    #[must_use]
733    fn from_le_bytes(bytes: Self::Repr) -> Self;
734
735    /// Encode to big endian bytes.
736    #[must_use]
737    fn to_be_bytes(&self) -> Self::Repr;
738
739    /// Encode to little endian bytes.
740    #[must_use]
741    fn to_le_bytes(&self) -> Self::Repr;
742}
743
744/// A trait mapping between encoded representations of integers.
745pub trait EncodedSize {
746    /// The equivalent encoded representation.
747    type Target;
748}
749
750/// Possible errors in variable-time integer decoding methods.
751#[derive(Clone, Copy, Debug, Eq, PartialEq)]
752pub enum DecodeError {
753    /// The input value was empty.
754    Empty,
755
756    /// The input was not consistent with the format restrictions.
757    InvalidDigit,
758
759    /// Input size is too small to fit in the given precision.
760    InputSize,
761
762    /// The deserialized number is larger than the given precision.
763    Precision,
764}
765
766impl fmt::Display for DecodeError {
767    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
768        match self {
769            Self::Empty => write!(f, "empty value provided"),
770            Self::InvalidDigit => {
771                write!(f, "invalid digit character")
772            }
773            Self::InputSize => write!(f, "input size is too small to fit in the given precision"),
774            Self::Precision => write!(
775                f,
776                "the deserialized number is larger than the given precision"
777            ),
778        }
779    }
780}
781
782impl core::error::Error for DecodeError {}
783
784/// Support for optimized squaring
785pub trait Square {
786    /// Computes the same as `self * self`, but may be more efficient.
787    #[must_use]
788    fn square(&self) -> Self;
789}
790
791/// Support for optimized squaring in-place
792pub trait SquareAssign {
793    /// Computes the same as `self * self`, but may be more efficient.
794    /// Writes the result in `self`.
795    fn square_assign(&mut self);
796}
797
798/// Support for calculating checked square roots.
799pub trait CheckedSquareRoot: Sized {
800    /// Output of the square root operation.
801    type Output;
802
803    /// Computes `sqrt(self)`, returning `none` if no root exists.
804    #[must_use]
805    fn checked_sqrt(&self) -> CtOption<Self::Output>;
806
807    /// Computes `sqrt(self)`, returning `none` if no root exists.
808    ///
809    /// Variable time with respect to `self`.
810    #[must_use]
811    fn checked_sqrt_vartime(&self) -> Option<Self::Output>;
812}
813
814/// Support for calculating floored square roots.
815pub trait FloorSquareRoot: CheckedSquareRoot {
816    /// Computes `floor(sqrt(self))`.
817    #[must_use]
818    fn floor_sqrt(&self) -> Self::Output;
819
820    /// Computes `floor(sqrt(self))`.
821    ///
822    /// Variable time with respect to `self`.
823    #[must_use]
824    fn floor_sqrt_vartime(&self) -> Self::Output;
825}
826
827/// Support for optimized division by a single limb.
828pub trait DivRemLimb: Sized {
829    /// Computes `self / rhs` using a pre-made reciprocal,
830    /// returns the quotient (q) and remainder (r).
831    #[must_use]
832    fn div_rem_limb(&self, rhs: NonZero<Limb>) -> (Self, Limb) {
833        self.div_rem_limb_with_reciprocal(&Reciprocal::new(rhs))
834    }
835
836    /// Computes `self / rhs`, returns the quotient (q) and remainder (r).
837    #[must_use]
838    fn div_rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> (Self, Limb);
839}
840
841/// Support for calculating the remainder of two differently sized integers.
842pub trait RemMixed<Reductor>: Sized {
843    /// Calculate the remainder of `self` by the `reductor`.
844    #[must_use]
845    fn rem_mixed(&self, reductor: &NonZero<Reductor>) -> Reductor;
846}
847
848/// Modular reduction from a larger value `T`.
849///
850/// This can be seen as fixed modular reduction, where the modulus is fixed at compile time
851/// by `Self`.
852///
853/// For modular reduction with a variable modulus, use [`Rem`].
854pub trait Reduce<T>: Sized {
855    /// Reduces `self` modulo `Modulus`.
856    #[must_use]
857    fn reduce(value: &T) -> Self;
858}
859
860/// Division in variable time.
861pub trait DivVartime: Sized {
862    /// Computes `self / rhs` in variable time.
863    #[must_use]
864    fn div_vartime(&self, rhs: &NonZero<Self>) -> Self;
865}
866
867/// Support for optimized division by a single limb.
868pub trait RemLimb: Sized {
869    /// Computes `self % rhs` using a pre-made reciprocal.
870    #[must_use]
871    fn rem_limb(&self, rhs: NonZero<Limb>) -> Limb {
872        self.rem_limb_with_reciprocal(&Reciprocal::new(rhs))
873    }
874
875    /// Computes `self % rhs`.
876    #[must_use]
877    fn rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> Limb;
878}
879
880/// Bit counting and bit operations.
881pub trait BitOps {
882    /// Precision of this integer in bits.
883    #[must_use]
884    fn bits_precision(&self) -> u32;
885
886    /// `floor(log2(self.bits_precision()))`.
887    #[must_use]
888    fn log2_bits(&self) -> u32 {
889        u32_bits(self.bits_precision()) - 1
890    }
891
892    /// Precision of this integer in bytes.
893    #[must_use]
894    fn bytes_precision(&self) -> usize;
895
896    /// Get the value of the bit at position `index`, as a truthy or falsy `Choice`.
897    /// Returns the falsy value for indices out of range.
898    #[must_use]
899    fn bit(&self, index: u32) -> Choice;
900
901    /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`.
902    fn set_bit(&mut self, index: u32, bit_value: Choice);
903
904    /// Calculate the number of bits required to represent a given number.
905    #[must_use]
906    fn bits(&self) -> u32 {
907        self.bits_precision() - self.leading_zeros()
908    }
909
910    /// Calculate the number of trailing zeros in the binary representation of this number.
911    #[must_use]
912    fn trailing_zeros(&self) -> u32;
913
914    /// Calculate the number of trailing ones in the binary representation of this number.
915    #[must_use]
916    fn trailing_ones(&self) -> u32;
917
918    /// Calculate the number of leading zeros in the binary representation of this number.
919    #[must_use]
920    fn leading_zeros(&self) -> u32;
921
922    /// Returns `true` if the bit at position `index` is set, `false` otherwise.
923    ///
924    /// # Remarks
925    /// This operation is variable time with respect to `index` only.
926    #[must_use]
927    fn bit_vartime(&self, index: u32) -> bool;
928
929    /// Calculate the number of bits required to represent a given number in variable-time with
930    /// respect to `self`.
931    #[must_use]
932    fn bits_vartime(&self) -> u32 {
933        self.bits()
934    }
935
936    /// Sets the bit at `index` to 0 or 1 depending on the value of `bit_value`,
937    /// variable time in `self`.
938    fn set_bit_vartime(&mut self, index: u32, bit_value: bool);
939
940    /// Calculate the number of leading zeros in the binary representation of this number.
941    #[must_use]
942    fn leading_zeros_vartime(&self) -> u32 {
943        self.bits_precision() - self.bits_vartime()
944    }
945
946    /// Calculate the number of trailing zeros in the binary representation of this number in
947    /// variable-time with respect to `self`.
948    #[must_use]
949    fn trailing_zeros_vartime(&self) -> u32 {
950        self.trailing_zeros()
951    }
952
953    /// Calculate the number of trailing ones in the binary representation of this number,
954    /// variable time in `self`.
955    #[must_use]
956    fn trailing_ones_vartime(&self) -> u32 {
957        self.trailing_ones()
958    }
959}
960
961/// Constant-time exponentiation.
962pub trait Pow<Exponent> {
963    /// Raises to the `exponent` power.
964    #[must_use]
965    fn pow(&self, exponent: &Exponent) -> Self;
966}
967
968impl<T: PowBoundedExp<Exponent>, Exponent: Unsigned> Pow<Exponent> for T {
969    fn pow(&self, exponent: &Exponent) -> Self {
970        self.pow_bounded_exp(exponent, exponent.bits_precision())
971    }
972}
973
974/// Constant-time exponentiation with exponent of a bounded bit size.
975pub trait PowBoundedExp<Exponent> {
976    /// Raises to the `exponent` power,
977    /// with `exponent_bits` representing the number of (least significant) bits
978    /// to take into account for the exponent.
979    ///
980    /// NOTE: `exponent_bits` may be leaked in the time pattern.
981    #[must_use]
982    fn pow_bounded_exp(&self, exponent: &Exponent, exponent_bits: u32) -> Self;
983}
984
985/// Performs modular multi-exponentiation using Montgomery's ladder.
986///
987/// See: Straus, E. G. Problems and solutions: Addition chains of vectors. American Mathematical Monthly 71 (1964), 806–808.
988pub trait MultiExponentiate<Exponent, BasesAndExponents>: Pow<Exponent> + Sized
989where
990    BasesAndExponents: AsRef<[(Self, Exponent)]> + ?Sized,
991{
992    /// Calculates `x1 ^ k1 * ... * xn ^ kn`.
993    #[must_use]
994    fn multi_exponentiate(bases_and_exponents: &BasesAndExponents) -> Self;
995}
996
997impl<T, Exponent, BasesAndExponents> MultiExponentiate<Exponent, BasesAndExponents> for T
998where
999    T: MultiExponentiateBoundedExp<Exponent, BasesAndExponents>,
1000    Exponent: Bounded,
1001    BasesAndExponents: AsRef<[(Self, Exponent)]> + ?Sized,
1002{
1003    fn multi_exponentiate(bases_and_exponents: &BasesAndExponents) -> Self {
1004        Self::multi_exponentiate_bounded_exp(bases_and_exponents, Exponent::BITS)
1005    }
1006}
1007
1008/// Performs modular multi-exponentiation using Montgomery's ladder.
1009/// `exponent_bits` represents the number of bits to take into account for the exponent.
1010///
1011/// See: Straus, E. G. Problems and solutions: Addition chains of vectors. American Mathematical Monthly 71 (1964), 806–808.
1012///
1013/// NOTE: this value is leaked in the time pattern.
1014pub trait MultiExponentiateBoundedExp<Exponent, BasesAndExponents>: Pow<Exponent> + Sized
1015where
1016    BasesAndExponents: AsRef<[(Self, Exponent)]> + ?Sized,
1017{
1018    /// Calculates `x1 ^ k1 * ... * xn ^ kn`.
1019    #[must_use]
1020    fn multi_exponentiate_bounded_exp(
1021        bases_and_exponents: &BasesAndExponents,
1022        exponent_bits: u32,
1023    ) -> Self;
1024}
1025
1026/// Constant-time inversion.
1027pub trait Invert {
1028    /// Output of the inversion.
1029    type Output;
1030
1031    /// Computes the inverse.
1032    fn invert(&self) -> Self::Output;
1033
1034    /// Computes the inverse in variable-time.
1035    fn invert_vartime(&self) -> Self::Output {
1036        self.invert()
1037    }
1038}
1039
1040/// Widening multiply: returns a value with a number of limbs equal to the sum of the inputs.
1041pub trait ConcatenatingMul<Rhs = Self>: Sized {
1042    /// Output of the widening multiplication.
1043    type Output: Integer;
1044
1045    /// Perform widening multiplication.
1046    #[must_use]
1047    fn concatenating_mul(&self, rhs: Rhs) -> Self::Output;
1048}
1049
1050/// Widening square: returns a value with a number of limbs equal to double the sum of the input.
1051pub trait ConcatenatingSquare: Sized {
1052    /// Output of the widening multiplication.
1053    type Output: Integer;
1054
1055    /// Perform widening squaring.
1056    #[must_use]
1057    fn concatenating_square(&self) -> Self::Output;
1058}
1059
1060/// Widening multiply: returns a value with a number of limbs equal to the sum of the inputs.
1061#[deprecated(since = "0.7.0", note = "please use `ConcatenatingMul` instead")]
1062pub trait WideningMul<Rhs = Self>: Sized {
1063    /// Output of the widening multiplication.
1064    type Output: Integer;
1065
1066    /// Perform widening multiplication.
1067    #[must_use]
1068    fn widening_mul(&self, rhs: Rhs) -> Self::Output;
1069}
1070
1071#[allow(deprecated)]
1072impl<T, Rhs> WideningMul<Rhs> for T
1073where
1074    T: ConcatenatingMul<Rhs>,
1075{
1076    type Output = <T as ConcatenatingMul<Rhs>>::Output;
1077
1078    fn widening_mul(&self, rhs: Rhs) -> Self::Output {
1079        self.concatenating_mul(rhs)
1080    }
1081}
1082
1083/// Left shifts, variable time in `shift`.
1084pub trait ShlVartime: Sized {
1085    /// Computes `self << shift`.
1086    ///
1087    /// Returns `None` if `shift >= self.bits_precision()`.
1088    fn overflowing_shl_vartime(&self, shift: u32) -> Option<Self>;
1089
1090    /// Computes `self << shift`.
1091    ///
1092    /// Returns zero if `shift >= self.bits_precision()`.
1093    #[must_use]
1094    fn unbounded_shl_vartime(&self, shift: u32) -> Self;
1095
1096    /// Computes `self << shift` in a panic-free manner, masking off bits of `shift`
1097    /// which would cause the shift to exceed the type's width.
1098    #[must_use]
1099    fn wrapping_shl_vartime(&self, shift: u32) -> Self;
1100}
1101
1102/// Right shifts, variable time in `shift`.
1103pub trait ShrVartime: Sized {
1104    /// Computes `self >> shift`.
1105    ///
1106    /// Returns `None` if `shift >= self.bits_precision()`.
1107    fn overflowing_shr_vartime(&self, shift: u32) -> Option<Self>;
1108
1109    /// Computes `self >> shift`.
1110    ///
1111    /// Returns zero if `shift >= self.bits_precision()`.
1112    #[must_use]
1113    fn unbounded_shr_vartime(&self, shift: u32) -> Self;
1114
1115    /// Computes `self >> shift` in a panic-free manner, masking off bits of `shift`
1116    /// which would cause the shift to exceed the type's width.
1117    #[must_use]
1118    fn wrapping_shr_vartime(&self, shift: u32) -> Self;
1119}
1120
1121/// Methods for resizing the allocated storage.
1122pub trait Resize: Sized {
1123    /// The result of the resizing.
1124    type Output;
1125
1126    /// Resizes to the minimum storage that fits `at_least_bits_precision`
1127    /// without checking if the bit size of `self` is larger than `at_least_bits_precision`.
1128    ///
1129    /// Variable-time w.r.t. `at_least_bits_precision`.
1130    #[must_use]
1131    fn resize_unchecked(self, at_least_bits_precision: u32) -> Self::Output;
1132
1133    /// Resizes to the minimum storage that fits `at_least_bits_precision`
1134    /// returning `None` if the bit size of `self` is larger than `at_least_bits_precision`.
1135    ///
1136    /// Variable-time w.r.t. `at_least_bits_precision`.
1137    fn try_resize(self, at_least_bits_precision: u32) -> Option<Self::Output>;
1138
1139    /// Resizes to the minimum storage that fits `at_least_bits_precision`
1140    /// panicking if the bit size of `self` is larger than `at_least_bits_precision`.
1141    ///
1142    /// Variable-time w.r.t. `at_least_bits_precision`.
1143    #[must_use]
1144    #[track_caller]
1145    fn resize(self, at_least_bits_precision: u32) -> Self::Output {
1146        self.try_resize(at_least_bits_precision).unwrap_or_else(|| {
1147            panic!("The bit size of `self` is larger than `at_least_bits_precision`")
1148        })
1149    }
1150}
1151
1152/// A representation of an integer optimized for the performance of modular operations.
1153pub trait MontyForm:
1154    'static
1155    + sealed::Sealed
1156    + Clone
1157    + CtEq
1158    + CtSelect
1159    + Debug
1160    + Eq
1161    + Invert<Output = CtOption<Self>>
1162    + Sized
1163    + Send
1164    + Sync
1165    + Add<Output = Self>
1166    + for<'a> Add<&'a Self, Output = Self>
1167    + AddAssign
1168    + for<'a> AddAssign<&'a Self>
1169    + Sub<Output = Self>
1170    + for<'a> Sub<&'a Self, Output = Self>
1171    + SubAssign
1172    + for<'a> SubAssign<&'a Self>
1173    + Mul<Output = Self>
1174    + for<'a> Mul<&'a Self, Output = Self>
1175    + MulAssign
1176    + for<'a> MulAssign<&'a Self>
1177    + Neg<Output = Self>
1178    + PowBoundedExp<Self::Integer>
1179    + Retrieve<Output = Self::Integer>
1180    + Square
1181    + SquareAssign
1182{
1183    /// The original integer type.
1184    type Integer: UnsignedWithMontyForm<MontyForm = Self>;
1185
1186    /// Prepared Montgomery multiplier for tight loops.
1187    type Multiplier<'a>: Debug + Clone + MontyMultiplier<'a, Monty = Self>;
1188
1189    /// The precomputed data needed for this representation.
1190    type Params: 'static
1191        + AsRef<MontyParams<Self::Integer>>
1192        + From<MontyParams<Self::Integer>>
1193        + Clone
1194        + Debug
1195        + Eq
1196        + Sized
1197        + Send
1198        + Sync;
1199
1200    /// Create the precomputed data for Montgomery representation of integers modulo `modulus`,
1201    /// variable time in `modulus`.
1202    #[must_use]
1203    fn new_params_vartime(modulus: Odd<Self::Integer>) -> Self::Params;
1204
1205    /// Convert the value into the representation using precomputed data.
1206    #[must_use]
1207    fn new(value: Self::Integer, params: &Self::Params) -> Self;
1208
1209    /// Returns zero in this representation.
1210    #[must_use]
1211    fn zero(params: &Self::Params) -> Self;
1212
1213    /// Determine if this value is equal to zero.
1214    ///
1215    /// # Returns
1216    ///
1217    /// If zero, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
1218    #[must_use]
1219    fn is_zero(&self) -> Choice {
1220        self.as_montgomery().is_zero()
1221    }
1222
1223    /// Returns one in this representation.
1224    #[must_use]
1225    fn one(params: &Self::Params) -> Self;
1226
1227    /// Determine if this value is equal to one.
1228    ///
1229    /// # Returns
1230    ///
1231    /// If one, returns `Choice(1)`. Otherwise, returns `Choice(0)`.
1232    #[must_use]
1233    fn is_one(&self) -> Choice {
1234        self.as_montgomery().ct_eq(self.params().as_ref().one())
1235    }
1236
1237    /// Returns the parameter struct used to initialize this object.
1238    #[must_use]
1239    fn params(&self) -> &Self::Params;
1240
1241    /// Access the value in Montgomery form.
1242    #[must_use]
1243    fn as_montgomery(&self) -> &Self::Integer;
1244
1245    /// Copy the Montgomery representation from `other` into `self`.
1246    /// NOTE: the parameters remain unchanged.
1247    fn copy_montgomery_from(&mut self, other: &Self);
1248
1249    /// Create a new Montgomery representation from an integer in Montgomery form.
1250    #[must_use]
1251    fn from_montgomery(integer: Self::Integer, params: &Self::Params) -> Self;
1252
1253    /// Move the Montgomery form result out of `self` and return it.
1254    #[must_use]
1255    fn into_montgomery(self) -> Self::Integer;
1256
1257    /// Performs doubling, returning `self + self`.
1258    #[must_use]
1259    fn double(&self) -> Self;
1260
1261    /// Performs division by 2, that is returns `x` such that `x + x = self`.
1262    #[must_use]
1263    fn div_by_2(&self) -> Self;
1264
1265    /// Performs division by 2 inplace, that is finds `x` such that `x + x = self`
1266    /// and writes it into `self`.
1267    fn div_by_2_assign(&mut self) {
1268        *self = self.div_by_2();
1269    }
1270
1271    /// Calculate the sum of products of pairs `(a, b)` in `products`.
1272    ///
1273    /// This method is variable time only with the value of the modulus.
1274    /// For a modulus with leading zeros, this method is more efficient than a naive sum of products.
1275    ///
1276    /// This method will panic if `products` is empty. All terms must be associated with equivalent
1277    /// Montgomery parameters.
1278    #[must_use]
1279    fn lincomb_vartime(products: &[(&Self, &Self)]) -> Self;
1280}
1281
1282/// Prepared Montgomery multiplier for tight loops.
1283///
1284/// Allows one to perform inplace multiplication without allocations
1285/// (important for the `BoxedUint` case).
1286///
1287/// NOTE: You will be operating with Montgomery representations directly,
1288/// make sure they all correspond to the same set of parameters.
1289pub trait MontyMultiplier<'a>: From<&'a <Self::Monty as MontyForm>::Params> {
1290    /// The associated Montgomery-representation integer.
1291    type Monty: MontyForm;
1292
1293    /// Performs a Montgomery multiplication, assigning a fully reduced result to `lhs`.
1294    fn mul_assign(&mut self, lhs: &mut Self::Monty, rhs: &Self::Monty);
1295
1296    /// Performs a Montgomery squaring, assigning a fully reduced result to `lhs`.
1297    fn square_assign(&mut self, lhs: &mut Self::Monty);
1298}
1299
1300/// Prepared Montgomery multiplier for tight loops, performing "Almost Montgomery Multiplication".
1301///
1302/// NOTE: the resulting output of any of these functions will be reduced to the *bit length* of the
1303/// modulus, but not fully reduced and may exceed the modulus. A final reduction is required to
1304/// ensure AMM results are fully reduced, and should not be exposed outside the internals of this
1305/// crate.
1306pub(crate) trait AmmMultiplier<'a>: MontyMultiplier<'a> {
1307    /// Perform an "Almost Montgomery Multiplication", assigning the product to `a`.
1308    fn mul_amm_assign(
1309        &mut self,
1310        a: &mut <Self::Monty as MontyForm>::Integer,
1311        b: &<Self::Monty as MontyForm>::Integer,
1312    );
1313
1314    /// Perform a squaring using "Almost Montgomery Multiplication", assigning the result to `a`.
1315    fn square_amm_assign(&mut self, a: &mut <Self::Monty as MontyForm>::Integer);
1316}
1317
1318/// Support for sealing traits defined in this module.
1319pub(crate) mod sealed {
1320    /// Sealed trait.
1321    pub trait Sealed {}
1322}
1323
1324#[cfg(test)]
1325pub(crate) mod tests {
1326    use super::{
1327        Integer, Invert, MontyForm, Retrieve, Signed, Square, SquareAssign, ToUnsigned, Unsigned,
1328        UnsignedWithMontyForm,
1329    };
1330    use crate::{Choice, CtEq, CtSelect, Limb, NonZero, Odd, One, Reciprocal, Zero};
1331
1332    /// Apply a suite of tests against a type implementing Integer.
1333    pub fn test_integer<T: Integer>(min: T, max: T) {
1334        let zero = T::zero_like(&min);
1335        let one = T::one_like(&min);
1336        let two = one.clone() + &one;
1337        let inputs = &[zero.clone(), one.clone(), max.clone()];
1338        let nz_one = NonZero::new(one.clone()).expect("must be non-zero");
1339        let nz_two = NonZero::new(two.clone()).expect("must be non-zero");
1340
1341        // FIXME Could use itertools combinations or an equivalent iterator
1342        let pairs = &[
1343            (zero.clone(), zero.clone()),
1344            (zero.clone(), one.clone()),
1345            (zero.clone(), max.clone()),
1346            (one.clone(), zero.clone()),
1347            (one.clone(), one.clone()),
1348            (one.clone(), max.clone()),
1349            (max.clone(), zero.clone()),
1350            (max.clone(), one.clone()),
1351            (max.clone(), max.clone()),
1352        ];
1353
1354        // Test formatting
1355        #[cfg(feature = "alloc")]
1356        for a in inputs {
1357            let _ = format!("{a:?}");
1358            let _ = format!("{a:#?}");
1359            let _ = format!("{a:b}");
1360            let _ = format!("{a:x}");
1361            let _ = format!("{a:X}");
1362        }
1363
1364        // Default must be zero
1365        assert_eq!(T::default(), zero);
1366
1367        // Sanity checks
1368        assert_eq!(zero, T::zero());
1369        assert_eq!(one, T::one());
1370        assert_ne!(zero, one);
1371        assert_ne!(zero, max);
1372        assert_ne!(min, max);
1373
1374        // Even/odd trait methods
1375        assert!(zero.is_even().to_bool());
1376        assert!(!zero.is_odd().to_bool());
1377        assert!(!one.is_even().to_bool());
1378        assert!(one.is_odd().to_bool());
1379        assert!(two.is_even().to_bool());
1380        assert!(!two.is_odd().to_bool());
1381
1382        // AsRef<[Limb]>, Eq, Ord, CtEq, CtGt, CtLt, CtNe
1383        for (a, b) in pairs {
1384            assert_eq!(a.nlimbs(), a.as_limbs().len());
1385            assert_eq!(a.as_limbs(), a.as_ref());
1386            assert_eq!(a.clone().as_mut_limbs(), a.clone().as_mut());
1387            assert_eq!(&*a.clone().as_mut_limbs(), a.as_limbs());
1388            assert_eq!(
1389                a.ct_eq(b).to_bool(),
1390                a.as_limbs().ct_eq(b.as_limbs()).to_bool()
1391            );
1392            assert_ne!(a.ct_eq(b).to_bool(), a.ct_ne(b).to_bool());
1393            if a == b {
1394                assert!(!a.ct_lt(b).to_bool());
1395                assert!(!a.ct_gt(b).to_bool());
1396            } else {
1397                assert_ne!(a.ct_lt(b).to_bool(), a.ct_gt(b).to_bool());
1398            }
1399            assert_eq!(a.ct_lt(b).to_bool(), a < b);
1400            assert_eq!(a.ct_gt(b).to_bool(), a > b);
1401        }
1402
1403        // CtAssign, CtSelect
1404        for (a, b) in pairs {
1405            let mut c = a.clone();
1406            c.ct_assign(b, Choice::FALSE);
1407            assert_eq!(&c, a);
1408            c.ct_assign(b, Choice::TRUE);
1409            assert_eq!(&c, b);
1410
1411            assert_eq!(&CtSelect::ct_select(a, b, Choice::FALSE), a);
1412            assert_eq!(&CtSelect::ct_select(a, b, Choice::TRUE), b);
1413            let (mut c, mut d) = (a.clone(), b.clone());
1414            CtSelect::ct_swap(&mut c, &mut d, Choice::FALSE);
1415            assert_eq!((&c, &d), (a, b));
1416            CtSelect::ct_swap(&mut c, &mut d, Choice::TRUE);
1417            assert_eq!((&c, &d), (b, a));
1418        }
1419
1420        // BitAnd
1421        for a in inputs {
1422            assert_eq!(a.clone().bitand(zero.clone()), zero);
1423            assert_eq!(a.clone().bitand(&zero), zero);
1424            assert_eq!(&a.clone().bitand(a), a);
1425            assert_eq!(zero.clone().bitand(a), zero);
1426            assert_eq!(a.clone().not().bitand(a), zero);
1427            // BitAndAssign ref
1428            let mut b = a.clone();
1429            b &= a.clone();
1430            assert_eq!(a, &b);
1431            // BitAndAssign owned
1432            let mut b = a.clone();
1433            b &= a;
1434            assert_eq!(a, &b);
1435        }
1436
1437        // BitOr
1438        for a in inputs {
1439            assert_eq!(&a.clone().bitor(zero.clone()), a);
1440            assert_eq!(&a.clone().bitor(&zero), a);
1441            assert_eq!(&a.clone().bitor(a), a);
1442            assert_eq!(&zero.clone().bitor(a), a);
1443            // BitOrAssign ref
1444            let mut b = a.clone();
1445            b |= a;
1446            assert_eq!(a, &b);
1447            // BitOrAssign owned
1448            let mut b = a.clone();
1449            b |= a.clone();
1450            assert_eq!(a, &b);
1451        }
1452
1453        // BitXor
1454        for a in inputs {
1455            assert_eq!(&a.clone().bitxor(zero.clone()), a);
1456            assert_eq!(&a.clone().bitor(&zero), a);
1457            assert_eq!(a.clone().bitxor(a), zero);
1458            assert_eq!(&zero.clone().bitxor(a), a);
1459            // BitXorAssign ref
1460            let mut b = a.clone();
1461            b ^= a;
1462            assert_eq!(T::zero(), b);
1463            // BitXorAssign owned
1464            let mut b = a.clone();
1465            b ^= a.clone();
1466            assert_eq!(T::zero(), b);
1467        }
1468
1469        // Shr/Shl
1470        assert_eq!(zero.clone().shr(1u32), zero);
1471        assert_eq!(one.clone().shr(1u32), zero);
1472        assert_eq!(zero.clone().shl(1u32), zero);
1473        assert_eq!(one.clone().shl(1u32), two);
1474        assert_eq!(two.clone().shr(1u32), one);
1475        assert_ne!(max.clone().shr(1u32), max);
1476        let mut expect = one.clone();
1477        expect.shl_assign(1);
1478        assert_eq!(expect, two);
1479        expect.shr_assign(1);
1480        assert_eq!(expect, one);
1481
1482        // Non-carrying Add
1483        for a in inputs {
1484            assert_eq!(&a.clone().add(zero.clone()), a);
1485            assert_eq!(&a.clone().add(&zero), a);
1486            assert_eq!(&zero.clone().add(a), a);
1487            // AddAssign ref
1488            let mut b = a.clone();
1489            b += &zero;
1490            assert_eq!(a, &b);
1491            // AddAssign owned
1492            let mut b = a.clone();
1493            b += zero.clone();
1494            assert_eq!(a, &b);
1495        }
1496
1497        // Non-borrowing Sub
1498        for a in inputs {
1499            assert_eq!(&a.clone().sub(zero.clone()), a);
1500            assert_eq!(&a.clone().sub(&zero), a);
1501            assert_eq!(a.clone().sub(a), zero);
1502            // SubAssign ref
1503            let mut b = a.clone();
1504            b -= a;
1505            assert_eq!(zero, b);
1506            // SubAssign owned
1507            let mut b = a.clone();
1508            b -= a.clone();
1509            assert_eq!(zero, b);
1510        }
1511
1512        // Non-carrying Mul
1513        for a in inputs {
1514            assert_eq!(a.clone().mul(zero.clone()), zero);
1515            assert_eq!(a.clone().mul(&zero), zero);
1516            assert_eq!(&a.clone().mul(&one), a);
1517            assert_eq!(zero.clone().mul(a), zero);
1518            assert_eq!(&one.clone().mul(a), a);
1519            // MulAssign ref
1520            let mut b = a.clone();
1521            b *= &one;
1522            assert_eq!(a, &b);
1523            // MulAssign owned
1524            let mut b = a.clone();
1525            b *= one.clone();
1526            assert_eq!(a, &b);
1527        }
1528
1529        // DivAssign ref
1530        let mut a = one.clone();
1531        a /= &nz_one;
1532        assert_eq!(a, one);
1533        // DivAssign owned
1534        let mut a = one.clone();
1535        a /= nz_one.clone();
1536        assert_eq!(a, one);
1537
1538        // Rem
1539        assert_eq!(zero.clone().rem(&nz_one), zero);
1540        assert_eq!(zero.clone().rem(nz_one.clone()), zero);
1541        assert_eq!(one.clone().rem(&nz_one), zero);
1542        assert_eq!(one.clone().rem(&nz_two), one);
1543        // RemAssign ref
1544        let mut a = one.clone();
1545        a %= &nz_one;
1546        assert_eq!(a, zero);
1547        // RemAssign owned
1548        let mut a = one.clone();
1549        a %= nz_one.clone();
1550        assert_eq!(a, zero);
1551
1552        // CheckedAdd
1553        assert_eq!(
1554            zero.clone().checked_add(&zero).into_option(),
1555            Some(zero.clone())
1556        );
1557        assert_eq!(
1558            zero.clone().checked_add(&one).into_option(),
1559            Some(one.clone())
1560        );
1561        assert_eq!(
1562            zero.clone().checked_add(&max).into_option(),
1563            Some(max.clone())
1564        );
1565        assert_eq!(max.checked_add(&one).into_option(), None);
1566        assert_eq!(max.checked_add(&max).into_option(), None);
1567
1568        // CheckedSub
1569        assert_eq!(
1570            zero.clone().checked_sub(&zero).into_option(),
1571            Some(zero.clone())
1572        );
1573        assert_eq!(min.checked_sub(&zero).into_option(), Some(min.clone()));
1574        assert_eq!(min.checked_sub(&one).into_option(), None);
1575        assert_eq!(min.checked_sub(&max).into_option(), None);
1576        assert_eq!(max.checked_sub(&zero).into_option(), Some(max.clone()));
1577        assert_eq!(max.checked_sub(&max).into_option(), Some(zero.clone()));
1578
1579        // CheckedMul
1580        assert_eq!(
1581            zero.clone().checked_mul(&zero).into_option(),
1582            Some(zero.clone())
1583        );
1584        assert_eq!(
1585            zero.clone().checked_mul(&one).into_option(),
1586            Some(zero.clone())
1587        );
1588        assert_eq!(
1589            one.clone().checked_mul(&max).into_option(),
1590            Some(max.clone())
1591        );
1592        assert_eq!(max.checked_mul(&max).into_option(), None);
1593
1594        // CheckedDiv
1595        assert_eq!(zero.clone().checked_div(&zero).into_option(), None);
1596        assert_eq!(one.clone().checked_div(&zero).into_option(), None);
1597        assert_eq!(
1598            one.clone().checked_div(&one).into_option(),
1599            Some(one.clone())
1600        );
1601        assert_eq!(max.checked_div(&max).into_option(), Some(one.clone()));
1602
1603        // CheckedSquareRoot
1604        assert_eq!(zero.checked_sqrt().into_option(), Some(zero.clone()));
1605        assert_eq!(zero.checked_sqrt_vartime(), Some(zero.clone()));
1606        assert_eq!(one.checked_sqrt().into_option(), Some(one.clone()));
1607        assert_eq!(one.checked_sqrt_vartime(), Some(one.clone()));
1608        assert_eq!(two.checked_sqrt().into_option(), None);
1609        assert_eq!(two.checked_sqrt_vartime(), None);
1610
1611        // WrappingAdd
1612        assert_eq!(zero.clone().wrapping_add(&zero), zero);
1613        assert_eq!(zero.clone().wrapping_add(&one), one);
1614        assert_eq!(one.clone().wrapping_add(&zero), one);
1615        assert_eq!(one.clone().wrapping_add(&one), two);
1616        assert_eq!(one.clone().wrapping_add(&max), min);
1617        assert_eq!(max.wrapping_add(&one), min);
1618
1619        // WrappingSub
1620        assert_eq!(zero.clone().wrapping_sub(&zero), zero);
1621        assert_eq!(one.clone().wrapping_sub(&zero), one);
1622        assert_eq!(two.wrapping_sub(&one), one);
1623        assert_eq!(min.wrapping_sub(&one), max);
1624        assert_eq!(min.wrapping_sub(&min), zero);
1625
1626        // WrappingMul
1627        assert_eq!(zero.clone().wrapping_mul(&zero), zero);
1628        assert_eq!(zero.clone().wrapping_mul(&one), zero);
1629        assert_eq!(one.clone().wrapping_mul(&zero), zero);
1630        assert_eq!(one.clone().wrapping_mul(&one), one);
1631        assert_eq!(one.clone().wrapping_mul(&max), max);
1632        assert_eq!(max.wrapping_mul(&zero), zero);
1633        assert_eq!(max.wrapping_mul(&one), max);
1634        assert_eq!(max.wrapping_mul(&two), max.clone().shl(1u32));
1635
1636        // WrappingNeg
1637        assert_eq!(zero.clone().wrapping_neg(), zero);
1638        for a in inputs {
1639            assert_eq!(a.wrapping_add(&a.wrapping_neg()), zero);
1640            assert_eq!(zero.wrapping_sub(a), a.wrapping_neg());
1641        }
1642
1643        // WrappingShr/WrappingShl
1644        assert_eq!(zero.clone().wrapping_shr(1u32), zero);
1645        assert_eq!(one.clone().wrapping_shr(1u32), zero);
1646        assert_eq!(zero.clone().wrapping_shl(1u32), zero);
1647        assert_eq!(one.clone().wrapping_shl(1u32), two);
1648        assert_eq!(two.clone().wrapping_shr(1u32), one);
1649        assert_ne!(max.clone().wrapping_shr(1u32), max);
1650    }
1651
1652    /// Apply a suite of tests against a type implementing Unsigned.
1653    pub fn test_unsigned<T: Unsigned>(min: T, max: T) {
1654        let zero = T::zero_like(&min);
1655        let one = T::one_like(&min);
1656        let two = one.clone() + &one;
1657        let nz_one = NonZero::new(one.clone()).expect("must be non-zero");
1658        let nz_two = NonZero::new(two.clone()).expect("must be non-zero");
1659        let nz_limb_one = NonZero::new(Limb::ONE).expect("must be non-zero");
1660        let nz_limb_two = NonZero::new(Limb::from(2u8)).expect("must be non-zero");
1661        let inputs = &[zero.clone(), one.clone(), max.clone()];
1662
1663        // Test Integer trait
1664        test_integer(min.clone(), max.clone());
1665
1666        // Trait methods
1667        for a in inputs {
1668            assert_eq!(a.as_uint_ref(), a.as_ref());
1669            assert_eq!(a.clone().as_mut_uint_ref(), a.clone().as_mut());
1670            assert_eq!(T::from_limb_like(Limb::ZERO, &one), zero);
1671            assert_eq!(T::from_limb_like(Limb::ONE, &one), one);
1672        }
1673
1674        // Zero trait
1675        assert!(zero.is_zero().to_bool());
1676        assert!(!one.is_zero().to_bool());
1677        let mut a = one.clone();
1678        a.set_zero();
1679        assert_eq!(a, zero);
1680
1681        // One trait
1682        assert!(!zero.is_one().to_bool());
1683        assert!(one.is_one().to_bool());
1684        let mut a = zero.clone();
1685        a.set_one();
1686        assert_eq!(a, one);
1687
1688        // From implementations
1689        assert_eq!(T::from(0u8), T::zero());
1690        assert_eq!(T::from(1u8), T::one());
1691        assert_eq!(T::from(1u16), T::one());
1692        assert_eq!(T::from(1u32), T::one());
1693        assert_eq!(T::from(1u64), T::one());
1694        assert_eq!(T::from(Limb::ONE), T::one());
1695
1696        // ToUnsigned
1697        assert_eq!(one.to_unsigned(), one);
1698        assert_eq!(one.to_unsigned_zero(), zero);
1699
1700        // FloorSquareRoot
1701        assert_eq!(zero.floor_sqrt(), zero);
1702        assert_eq!(zero.floor_sqrt_vartime(), zero);
1703        assert_eq!(one.floor_sqrt(), one);
1704        assert_eq!(one.floor_sqrt_vartime(), one);
1705        assert_eq!(two.floor_sqrt(), one);
1706        assert_eq!(two.floor_sqrt_vartime(), one);
1707
1708        // Gcd
1709        assert_eq!(zero.gcd(&zero), zero);
1710        assert_eq!(zero.gcd_vartime(&zero), zero);
1711        assert_eq!(one.gcd(&one), one);
1712        assert_eq!(one.gcd_vartime(&one), one);
1713        assert_eq!(two.gcd(&one), one);
1714        assert_eq!(two.gcd_vartime(&one), one);
1715
1716        // Div by ref
1717        assert_eq!(zero.clone().div(&nz_one), zero);
1718        assert_eq!(zero.clone().div(&nz_two), zero);
1719        assert_eq!(one.clone().div(&nz_one), one);
1720        assert_eq!(one.clone().div(&nz_two), zero);
1721        // Div by owned
1722        assert_eq!(zero.clone().div(nz_one.clone()), zero);
1723        assert_eq!(zero.clone().div(nz_two.clone()), zero);
1724        assert_eq!(one.clone().div(nz_one.clone()), one);
1725        assert_eq!(one.clone().div(nz_two.clone()), zero);
1726
1727        // DivRemLimb
1728        assert_eq!(zero.div_rem_limb(nz_limb_one), (zero.clone(), Limb::ZERO));
1729        assert_eq!(zero.div_rem_limb(nz_limb_two), (zero.clone(), Limb::ZERO));
1730        assert_eq!(one.div_rem_limb(nz_limb_one), (one.clone(), Limb::ZERO));
1731        assert_eq!(one.div_rem_limb(nz_limb_two), (zero.clone(), Limb::ONE));
1732        assert_eq!(
1733            zero.div_rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_one)),
1734            (zero.clone(), Limb::ZERO)
1735        );
1736        assert_eq!(
1737            one.div_rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_one)),
1738            (one.clone(), Limb::ZERO)
1739        );
1740        assert_eq!(
1741            one.div_rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_two)),
1742            (zero.clone(), Limb::ONE)
1743        );
1744
1745        // RemLimb
1746        assert_eq!(zero.rem_limb(nz_limb_one), Limb::ZERO);
1747        assert_eq!(zero.rem_limb(nz_limb_two), Limb::ZERO);
1748        assert_eq!(one.rem_limb(nz_limb_one), Limb::ZERO);
1749        assert_eq!(one.rem_limb(nz_limb_two), Limb::ONE);
1750        assert_eq!(
1751            zero.rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_one)),
1752            Limb::ZERO
1753        );
1754        assert_eq!(
1755            one.rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_one)),
1756            Limb::ZERO
1757        );
1758        assert_eq!(
1759            one.rem_limb_with_reciprocal(&Reciprocal::new(nz_limb_two)),
1760            Limb::ONE
1761        );
1762
1763        // BitOps
1764        assert_eq!(zero.bits(), 0);
1765        assert_eq!(one.bits(), 1);
1766        assert_eq!(one.bits_vartime(), 1);
1767        assert_eq!(one.bits_precision() as usize, one.bytes_precision() * 8);
1768        assert_eq!(one.bits_precision(), 1 << one.log2_bits());
1769        // Leading/trailing zeros and ones
1770        assert_eq!(zero.leading_zeros(), zero.bits_precision());
1771        assert_eq!(zero.leading_zeros_vartime(), zero.bits_precision());
1772        assert_eq!(zero.trailing_zeros(), zero.bits_precision());
1773        assert_eq!(zero.trailing_zeros_vartime(), zero.bits_precision());
1774        assert_eq!(zero.trailing_ones(), 0);
1775        assert_eq!(zero.trailing_ones_vartime(), 0);
1776        assert_eq!(one.leading_zeros(), one.bits_precision() - 1);
1777        assert_eq!(one.leading_zeros_vartime(), one.bits_precision() - 1);
1778        assert_eq!(one.trailing_zeros(), 0);
1779        assert_eq!(one.trailing_zeros_vartime(), 0);
1780        assert_eq!(one.trailing_ones(), 1);
1781        assert_eq!(one.trailing_ones_vartime(), 1);
1782        // Bit access
1783        assert!(!zero.bit(0).to_bool());
1784        assert!(!zero.bit_vartime(0));
1785        assert!(one.bit(0).to_bool());
1786        assert!(one.bit_vartime(0));
1787        assert!(!one.bit(1).to_bool());
1788        assert!(!one.bit_vartime(1));
1789        // Bit assignment
1790        let mut a = zero.clone();
1791        a.set_bit(0, Choice::TRUE);
1792        assert_eq!(a, one);
1793        let mut a = zero.clone();
1794        a.set_bit_vartime(0, true);
1795        assert_eq!(a, one);
1796        let mut a = one.clone();
1797        a.set_bit(0, Choice::FALSE);
1798        assert_eq!(a, zero);
1799        let mut a = one.clone();
1800        a.set_bit_vartime(0, false);
1801        assert_eq!(a, zero);
1802
1803        // AddMod
1804        assert_eq!(zero.add_mod(&zero, &nz_two), zero);
1805        assert_eq!(zero.add_mod(&one, &nz_two), one);
1806        assert_eq!(one.add_mod(&zero, &nz_two), one);
1807        assert_eq!(one.add_mod(&one, &nz_two), zero);
1808
1809        // SubMod
1810        assert_eq!(zero.sub_mod(&zero, &nz_two), zero);
1811        assert_eq!(zero.sub_mod(&one, &nz_two), one);
1812        assert_eq!(one.sub_mod(&zero, &nz_two), one);
1813        assert_eq!(one.sub_mod(&one, &nz_two), zero);
1814
1815        // MulMod
1816        assert_eq!(zero.mul_mod(&zero, &nz_two), zero);
1817        assert_eq!(zero.mul_mod(&one, &nz_two), zero);
1818        assert_eq!(one.mul_mod(&zero, &nz_two), zero);
1819        assert_eq!(one.mul_mod(&one, &nz_two), one);
1820
1821        // SquareMod
1822        assert_eq!(zero.square_mod(&nz_two), zero);
1823        assert_eq!(one.square_mod(&nz_two), one);
1824
1825        // NegMod
1826        assert_eq!(zero.neg_mod(&nz_two), zero);
1827        assert_eq!(one.neg_mod(&nz_two), one);
1828    }
1829
1830    pub fn test_signed<T: Signed>(min: T, max: T) {
1831        let zero = T::zero_like(&min);
1832        let one = T::one_like(&min);
1833        let two = one.clone() + &one;
1834        let zero_unsigned = T::Unsigned::zero();
1835        let one_unsigned = T::Unsigned::one();
1836        let minus_one = T::from(-1i8);
1837        let nz_one = NonZero::new(one.clone()).expect("must be non-zero");
1838        let nz_minus_one = NonZero::new(minus_one.clone()).expect("must be non-zero");
1839        let nz_two = NonZero::new(two.clone()).expect("must be non-zero");
1840
1841        // Test Integer trait
1842        test_integer(min.clone(), max.clone());
1843
1844        // Trait sign methods
1845        assert!(!zero.is_positive().to_bool());
1846        assert!(!zero.is_negative().to_bool());
1847        assert!(one.is_positive().to_bool());
1848        assert!(!one.is_negative().to_bool());
1849        assert!(!minus_one.is_positive().to_bool());
1850        assert!(minus_one.is_negative().to_bool());
1851        // Trait abs methods
1852        assert_eq!(zero.abs(), zero_unsigned);
1853        assert_eq!(one.abs(), one_unsigned);
1854        assert_eq!(minus_one.abs(), one_unsigned);
1855        let (check, check_sign) = zero.abs_sign();
1856        assert_eq!(
1857            (check, check_sign.to_bool()),
1858            (zero_unsigned.clone(), false)
1859        );
1860        let (check, check_sign) = one.abs_sign();
1861        assert_eq!((check, check_sign.to_bool()), (one_unsigned.clone(), false));
1862        let (check, check_sign) = minus_one.abs_sign();
1863        assert_eq!((check, check_sign.to_bool()), (one_unsigned.clone(), true));
1864
1865        // From implementations
1866        assert_eq!(T::from(0i8), T::zero());
1867        assert_eq!(T::from(1i8), T::one());
1868        assert_eq!(T::from(1i16), T::one());
1869        assert_eq!(T::from(1i32), T::one());
1870        assert_eq!(T::from(1i64), T::one());
1871        assert_eq!(T::from(-1i64), T::zero() - T::one());
1872
1873        // Gcd
1874        assert_eq!(zero.gcd(&zero), T::Unsigned::zero());
1875        assert_eq!(zero.gcd_vartime(&zero), T::Unsigned::zero());
1876        assert_eq!(one.gcd(&one), T::Unsigned::one());
1877        assert_eq!(one.gcd_vartime(&one), T::Unsigned::one());
1878        assert_eq!(two.gcd(&one), T::Unsigned::one());
1879        assert_eq!(two.gcd_vartime(&one), T::Unsigned::one());
1880        assert_eq!(minus_one.gcd(&one), T::Unsigned::one());
1881        assert_eq!(minus_one.gcd_vartime(&one), T::Unsigned::one());
1882
1883        // Div by ref
1884        assert_eq!(zero.clone().div(&nz_one).into_option(), Some(zero.clone()));
1885        assert_eq!(
1886            zero.clone().div(&nz_minus_one).into_option(),
1887            Some(zero.clone())
1888        );
1889        assert_eq!(zero.clone().div(&nz_two).into_option(), Some(zero.clone()));
1890        assert_eq!(one.clone().div(&nz_one).into_option(), Some(one.clone()));
1891        assert_eq!(
1892            one.clone().div(&nz_minus_one).into_option(),
1893            Some(minus_one.clone())
1894        );
1895        assert_eq!(one.clone().div(&nz_two).into_option(), Some(zero.clone()));
1896        // Div by owned
1897        assert_eq!(
1898            zero.clone().div(nz_one.clone()).into_option(),
1899            Some(zero.clone())
1900        );
1901        assert_eq!(
1902            zero.clone().div(nz_minus_one.clone()).into_option(),
1903            Some(zero.clone())
1904        );
1905        assert_eq!(
1906            zero.clone().div(nz_two.clone()).into_option(),
1907            Some(zero.clone())
1908        );
1909        assert_eq!(
1910            one.clone().div(nz_one.clone()).into_option(),
1911            Some(one.clone())
1912        );
1913        assert_eq!(
1914            one.clone().div(nz_minus_one.clone()).into_option(),
1915            Some(minus_one.clone())
1916        );
1917        assert_eq!(
1918            one.clone().div(nz_two.clone()).into_option(),
1919            Some(zero.clone())
1920        );
1921    }
1922
1923    pub fn test_unsigned_monty_form<T: UnsignedWithMontyForm>() {
1924        let modulus = Odd::new(T::from(17863u32)).unwrap();
1925        let params = T::MontyForm::new_params_vartime(modulus);
1926
1927        // test AsRef, From for Params
1928        assert_eq!(
1929            <T::MontyForm as MontyForm>::Params::from(params.as_ref().clone()),
1930            params,
1931        );
1932        // test Clone for Params
1933        assert_eq!(params, params.clone());
1934
1935        // test zero
1936        let zero = T::MontyForm::zero(&params);
1937        assert!(zero.is_zero().to_bool_vartime());
1938        assert!(!zero.is_one().to_bool_vartime());
1939
1940        // test one
1941        let one = T::MontyForm::one(&params);
1942        assert!(!one.is_zero().to_bool_vartime());
1943        assert!(one.is_one().to_bool_vartime());
1944
1945        // test as_montgomery()
1946        assert_eq!(zero.as_montgomery(), &T::zero());
1947        assert_eq!(one.as_montgomery(), params.as_ref().one());
1948
1949        // test copy_montgomery_from()
1950        let mut z2 = one.clone();
1951        z2.copy_montgomery_from(&zero);
1952        assert_eq!(z2, zero);
1953
1954        // test from_montgomery()
1955        let one2 =
1956            <T::MontyForm as MontyForm>::from_montgomery(params.as_ref().one().clone(), &params);
1957        assert_eq!(one2, one);
1958
1959        // test into_montgomery()
1960        assert_eq!(&one2.into_montgomery(), params.as_ref().one());
1961
1962        // test double(), div_by_2()
1963        assert_eq!(zero.double(), zero);
1964        assert_ne!(one.double(), one);
1965        assert_eq!(one.double().div_by_2(), one);
1966        let mut half = one.clone();
1967        half.div_by_2_assign();
1968        assert_ne!(half, one);
1969        assert_eq!(half.double(), one);
1970
1971        // test lincomb_vartime()
1972        assert_eq!(
1973            <T::MontyForm as MontyForm>::lincomb_vartime(&[(&zero, &zero)]),
1974            zero
1975        );
1976        assert_eq!(
1977            <T::MontyForm as MontyForm>::lincomb_vartime(&[(&one, &one)]),
1978            one
1979        );
1980        assert_eq!(
1981            <T::MontyForm as MontyForm>::lincomb_vartime(&[(&one, &one), (&one, &one)]),
1982            one.double()
1983        );
1984
1985        // test CtSelect
1986        assert_eq!(zero.ct_select(&one, Choice::FALSE), zero);
1987        assert_eq!(zero.ct_select(&one, Choice::TRUE), one);
1988
1989        // test Invert
1990        assert!(zero.invert().is_none().to_bool_vartime());
1991        assert!(zero.invert_vartime().is_none().to_bool_vartime());
1992        assert!(
1993            one.invert()
1994                .expect("inversion error")
1995                .is_one()
1996                .to_bool_vartime()
1997        );
1998        assert!(
1999            one.invert_vartime()
2000                .expect("inversion error")
2001                .is_one()
2002                .to_bool_vartime()
2003        );
2004
2005        // test Add, AddAssign
2006        assert_eq!(one.clone() + one.clone(), one.double());
2007        assert_eq!(one.clone() + &one, one.double());
2008        let mut two = one.clone();
2009        two += one.clone();
2010        assert_eq!(two, one.double());
2011        let mut two = one.clone();
2012        two += &one;
2013        assert_eq!(two, one.double());
2014
2015        // test Sub, SubAssign
2016        assert_eq!(one.clone() - one.clone(), zero);
2017        assert_eq!(one.clone() - &one, zero);
2018        let mut check = one.double();
2019        check -= one.clone();
2020        assert_eq!(check, one);
2021        let mut check = one.double();
2022        check -= &one;
2023        assert_eq!(check, one);
2024
2025        // test Mul, MulAssign
2026        assert_eq!(zero.clone() * &zero, zero);
2027        assert_eq!(zero.clone() * &one, zero);
2028        assert_eq!(one.clone() * one.clone(), one);
2029        let mut check = one.clone();
2030        check *= one.clone();
2031        assert_eq!(check, one);
2032        let mut check = one.clone();
2033        check *= &zero;
2034        assert_eq!(check, zero);
2035
2036        // test Neg
2037        assert_eq!(-zero.clone(), zero);
2038        assert_ne!(-one.clone(), one);
2039        assert_eq!(-(-one.clone()), one);
2040
2041        // test Retrieve
2042        assert_eq!(zero.retrieve(), T::zero());
2043        assert_eq!(one.retrieve(), T::one());
2044
2045        // test Square, SquareAssign
2046        assert_eq!(zero.square(), zero);
2047        assert_eq!(one.square(), one);
2048        let mut check = one.double();
2049        check.square_assign();
2050        assert_eq!(check, one.double().double());
2051    }
2052}