crypto-bigint 0.7.3

Pure Rust implementation of a big integer library which has been designed from the ground-up for use in cryptographic applications. Provides constant-time, no_std-friendly implementations of modern formulas using const generics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! [`Int`] division operations.

use crate::{CheckedDiv, Choice, CtOption, DivVartime, Int, NonZero, Uint, Wrapping};
use core::ops::{Div, DivAssign, Rem, RemAssign};

/// Checked division operations.
impl<const LIMBS: usize> Int<LIMBS> {
    #[inline]
    /// Base `div_rem` operation on dividing [`Int`]s.
    ///
    /// Computes the quotient and remainder of `self / rhs`.
    /// Furthermore, returns the signs of `self` and `rhs`.
    const fn div_rem_base<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (Uint<LIMBS>, Uint<RHS_LIMBS>, Choice, Choice) {
        // Step 1: split operands into signs and magnitudes.
        let (lhs_mag, lhs_sgn) = self.abs_sign();
        let (rhs_mag, rhs_sgn) = rhs.abs_sign();

        // Step 2. Divide magnitudes
        // safe to unwrap since rhs is NonZero.
        let (quotient, remainder) = lhs_mag.div_rem(&rhs_mag);

        (quotient, remainder, lhs_sgn, rhs_sgn)
    }

    /// Compute the quotient and remainder of `self / rhs`.
    ///
    /// Returns `none` for the quotient when [`Int::MIN`] / [`Int::MINUS_ONE`]; that quotient cannot
    /// be captured in an [`Int`].
    ///
    /// Example:
    /// ```
    /// use crypto_bigint::{I128, NonZero};
    /// let (quotient, remainder) = I128::from(8).checked_div_rem(&I128::from(3).to_nz().unwrap());
    /// assert_eq!(quotient.unwrap(), I128::from(2));
    /// assert_eq!(remainder, I128::from(2));
    ///
    /// let (quotient, remainder) = I128::from(-8).checked_div_rem(&I128::from(3).to_nz().unwrap());
    /// assert_eq!(quotient.unwrap(), I128::from(-2));
    /// assert_eq!(remainder, I128::from(-2));
    ///
    /// let (quotient, remainder) = I128::from(8).checked_div_rem(&I128::from(-3).to_nz().unwrap());
    /// assert_eq!(quotient.unwrap(), I128::from(-2));
    /// assert_eq!(remainder, I128::from(2));
    ///
    /// let (quotient, remainder) = I128::from(-8).checked_div_rem(&I128::from(-3).to_nz().unwrap());
    /// assert_eq!(quotient.unwrap(), I128::from(2));
    /// assert_eq!(remainder, I128::from(-2));
    /// ```
    #[must_use]
    pub const fn checked_div_rem<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (CtOption<Self>, Int<RHS_LIMBS>) {
        let (quotient, remainder, lhs_sgn, rhs_sgn) = self.div_rem_base(rhs);
        let opposing_signs = lhs_sgn.ne(rhs_sgn);
        (
            Self::new_from_abs_sign(quotient, opposing_signs),
            remainder.as_int().wrapping_neg_if(lhs_sgn), // as_int mapping is safe; remainder < 2^{k-1} by construction.
        )
    }

    /// Perform checked division, returning a [`CtOption`] which `is_some` if
    /// - the `rhs != 0`, and
    /// - `self != MIN` or `rhs != MINUS_ONE`.
    ///
    /// Note: this operation rounds towards zero, truncating any fractional part of the exact result.
    #[must_use]
    pub fn checked_div<const RHS_LIMBS: usize>(&self, rhs: &Int<RHS_LIMBS>) -> CtOption<Self> {
        NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem(&rhs).0)
    }

    /// Computes `self` % `rhs`, returns the remainder.
    #[must_use]
    pub const fn rem<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> Int<RHS_LIMBS> {
        self.checked_div_rem(rhs).1
    }
}

/// Vartime checked division operations.
impl<const LIMBS: usize> Int<LIMBS> {
    #[inline]
    /// Variable time equivalent of [`Self::div_rem_base`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    const fn div_rem_base_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (Uint<LIMBS>, Uint<RHS_LIMBS>, Choice, Choice) {
        // Step 1: split operands into signs and magnitudes.
        let (lhs_mag, lhs_sgn) = self.abs_sign();
        let (rhs_mag, rhs_sgn) = rhs.abs_sign();

        // Step 2. Divide magnitudes
        // safe to unwrap since rhs is NonZero.
        let (quotient, remainder) = lhs_mag.div_rem_vartime(&rhs_mag);

        (quotient, remainder, lhs_sgn, rhs_sgn)
    }

    /// Variable time equivalent of [`Self::checked_div_rem`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    #[must_use]
    pub const fn checked_div_rem_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (CtOption<Self>, Int<RHS_LIMBS>) {
        let (quotient, remainder, lhs_sgn, rhs_sgn) = self.div_rem_base_vartime(rhs);
        let opposing_signs = lhs_sgn.ne(rhs_sgn);
        (
            Self::new_from_abs_sign(quotient, opposing_signs),
            remainder.as_int().wrapping_neg_if(lhs_sgn), // as_int mapping is safe; remainder < 2^{k-1} by construction.
        )
    }

    /// Variable time equivalent of [`Self::checked_div`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    #[must_use]
    pub fn checked_div_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &Int<RHS_LIMBS>,
    ) -> CtOption<Self> {
        NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem_vartime(&rhs).0)
    }

    /// Variable time equivalent of [`Self::rem`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    #[must_use]
    pub const fn rem_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> Int<RHS_LIMBS> {
        self.checked_div_rem_vartime(rhs).1
    }
}

/// Vartime checked div-floor operations.
impl<const LIMBS: usize> Int<LIMBS> {
    /// Variable time equivalent of [`Self::checked_div_rem_floor`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    #[must_use]
    pub const fn checked_div_rem_floor_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (CtOption<Self>, Int<RHS_LIMBS>) {
        let (lhs_mag, lhs_sgn) = self.abs_sign();
        let (rhs_mag, rhs_sgn) = rhs.abs_sign();
        let (quotient, remainder) = lhs_mag.div_rem_vartime(&rhs_mag);

        // Modify quotient and remainder when lhs and rhs have opposing signs and the remainder is
        // non-zero.
        let opposing_signs = lhs_sgn.xor(rhs_sgn);
        let modify = remainder.is_nonzero().and(opposing_signs);

        // Increase the quotient by one.
        let quotient_plus_one = quotient.wrapping_add(&Uint::ONE); // cannot wrap.
        let quotient = Uint::select(&quotient, &quotient_plus_one, modify);

        // Invert the remainder.
        let inv_remainder = rhs_mag.0.wrapping_sub(&remainder);
        let remainder = Uint::select(&remainder, &inv_remainder, modify);

        // Negate output when lhs and rhs have opposing signs.
        let quotient = Int::new_from_abs_sign(quotient, opposing_signs);
        let remainder = remainder.as_int().wrapping_neg_if(opposing_signs); // rem always small enough for safe as_int conversion

        (quotient, remainder)
    }

    /// Variable time equivalent of [`Self::checked_div_floor`]
    ///
    /// This is variable only with respect to `rhs`.
    ///
    /// When used with a fixed `rhs`, this function is constant-time with respect
    /// to `self`.
    #[must_use]
    pub fn checked_div_floor_vartime<const RHS_LIMBS: usize>(
        &self,
        rhs: &Int<RHS_LIMBS>,
    ) -> CtOption<Self> {
        NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem_floor_vartime(&rhs).0)
    }
}

/// Checked div-floor operations.
impl<const LIMBS: usize> Int<LIMBS> {
    /// Perform checked floored division, returning a [`CtOption`] which `is_some` only if
    /// - the `rhs != 0`, and
    /// - `self != MIN` or `rhs != MINUS_ONE`.
    ///
    /// Note: this operation rounds down.
    ///
    /// Example:
    /// ```
    /// use crypto_bigint::I128;
    /// assert_eq!(
    ///     I128::from(8).checked_div_floor(&I128::from(3)).unwrap(),
    ///     I128::from(2)
    /// );
    /// assert_eq!(
    ///     I128::from(-8).checked_div_floor(&I128::from(3)).unwrap(),
    ///     I128::from(-3)
    /// );
    /// assert_eq!(
    ///     I128::from(8).checked_div_floor(&I128::from(-3)).unwrap(),
    ///     I128::from(-3)
    /// );
    /// assert_eq!(
    ///     I128::from(-8).checked_div_floor(&I128::from(-3)).unwrap(),
    ///     I128::from(2)
    /// )
    /// ```
    #[must_use]
    pub fn checked_div_floor<const RHS_LIMBS: usize>(
        &self,
        rhs: &Int<RHS_LIMBS>,
    ) -> CtOption<Self> {
        NonZero::new(*rhs).and_then(|rhs| self.checked_div_rem_floor(&rhs).0)
    }

    /// Perform checked division and mod, returning the quotient and remainder.
    ///
    /// The quotient is a [`CtOption`] which `is_some` only if
    /// - the `rhs != 0`, and
    /// - `self != MIN` or `rhs != MINUS_ONE`.
    ///
    /// Note: this operation rounds down.
    ///
    /// Example:
    /// ```
    /// use crypto_bigint::I128;
    ///
    /// let three = I128::from(3).to_nz().unwrap();
    /// let (quotient, remainder) = I128::from(8).checked_div_rem_floor(&three);
    /// assert_eq!(quotient.unwrap(), I128::from(2));
    /// assert_eq!(remainder, I128::from(2));
    ///
    /// let (quotient, remainder) = I128::from(-8).checked_div_rem_floor(&three);
    /// assert_eq!(quotient.unwrap(), I128::from(-3));
    /// assert_eq!(remainder, I128::from(-1));
    ///
    /// let minus_three = I128::from(-3).to_nz().unwrap();
    /// let (quotient, remainder) = I128::from(8).checked_div_rem_floor(&minus_three);
    /// assert_eq!(quotient.unwrap(), I128::from(-3));
    /// assert_eq!(remainder, I128::from(-1));
    ///
    /// let (quotient, remainder) = I128::from(-8).checked_div_rem_floor(&minus_three);
    /// assert_eq!(quotient.unwrap(), I128::from(2));
    /// assert_eq!(remainder, I128::from(2));
    /// ```
    #[must_use]
    pub const fn checked_div_rem_floor<const RHS_LIMBS: usize>(
        &self,
        rhs: &NonZero<Int<RHS_LIMBS>>,
    ) -> (CtOption<Self>, Int<RHS_LIMBS>) {
        let (lhs_mag, lhs_sgn) = self.abs_sign();
        let (rhs_mag, rhs_sgn) = rhs.abs_sign();
        let (quotient, remainder) = lhs_mag.div_rem(&rhs_mag);

        // Modify quotient and remainder when lhs and rhs have opposing signs and the remainder is
        // non-zero.
        let opposing_signs = lhs_sgn.xor(rhs_sgn);
        let modify = remainder.is_nonzero().and(opposing_signs);

        // Increase the quotient by one.
        let quotient_plus_one = quotient.wrapping_add(&Uint::ONE); // cannot wrap.
        let quotient = Uint::select(&quotient, &quotient_plus_one, modify);

        // Invert the remainder.
        let inv_remainder = rhs_mag.0.wrapping_sub(&remainder);
        let remainder = Uint::select(&remainder, &inv_remainder, modify);

        // Negate output when lhs and rhs have opposing signs.
        let quotient = Int::new_from_abs_sign(quotient, opposing_signs);
        let remainder = remainder.as_int().wrapping_neg_if(opposing_signs); // rem always small enough for safe as_int conversion

        (quotient, remainder)
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> CheckedDiv<Int<RHS_LIMBS>> for Int<LIMBS> {
    fn checked_div(&self, rhs: &Int<RHS_LIMBS>) -> CtOption<Self> {
        self.checked_div(rhs)
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
    type Output = CtOption<Int<LIMBS>>;

    fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self / *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
    type Output = CtOption<Int<LIMBS>>;

    fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        self / *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
    type Output = CtOption<Int<LIMBS>>;

    fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self / rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
    type Output = CtOption<Int<LIMBS>>;

    fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        self.checked_div(&rhs)
    }
}

impl<const LIMBS: usize> DivAssign<&NonZero<Int<LIMBS>>> for Int<LIMBS> {
    fn div_assign(&mut self, rhs: &NonZero<Int<LIMBS>>) {
        *self /= *rhs;
    }
}

impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
    fn div_assign(&mut self, rhs: NonZero<Int<LIMBS>>) {
        *self = (*self / rhs).expect("cannot represent positive equivalent of Int::MIN as int");
    }
}

impl<const LIMBS: usize> DivVartime for Int<LIMBS> {
    fn div_vartime(&self, rhs: &NonZero<Int<LIMBS>>) -> Self {
        let (q, _r, lhs_sign, rhs_sign) = self.div_rem_base_vartime(rhs);
        let opposing_signs = lhs_sign.xor(rhs_sign);
        let q = Int::new_from_abs_sign(q, opposing_signs);
        q.expect("int divided by int fits in uint by construction")
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>>
    for Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<LIMBS>>;

    fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        Wrapping((self.0 / rhs).expect("cannot represent positive equivalent of Int::MIN as int"))
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<NonZero<Int<RHS_LIMBS>>>
    for &Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<LIMBS>>;

    fn div(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self / rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>>
    for &Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<LIMBS>>;

    fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self / *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Div<&NonZero<Int<RHS_LIMBS>>>
    for Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<LIMBS>>;

    fn div(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        self / *rhs
    }
}

impl<const LIMBS: usize> DivAssign<&NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
    fn div_assign(&mut self, rhs: &NonZero<Int<LIMBS>>) {
        *self = Wrapping(
            (self.0 / rhs).expect("cannot represent positive equivalent of Int::MIN as int"),
        );
    }
}

impl<const LIMBS: usize> DivAssign<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
    fn div_assign(&mut self, rhs: NonZero<Int<LIMBS>>) {
        *self /= &rhs;
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
    type Output = Int<RHS_LIMBS>;

    fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self % *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
    type Output = Int<RHS_LIMBS>;

    fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        self % *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>> for &Int<LIMBS> {
    type Output = Int<RHS_LIMBS>;

    fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self % rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>> for Int<LIMBS> {
    type Output = Int<RHS_LIMBS>;

    fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        Self::rem(&self, &rhs)
    }
}

impl<const LIMBS: usize> RemAssign<&NonZero<Int<LIMBS>>> for Int<LIMBS> {
    fn rem_assign(&mut self, rhs: &NonZero<Int<LIMBS>>) {
        *self %= *rhs;
    }
}

impl<const LIMBS: usize> RemAssign<NonZero<Int<LIMBS>>> for Int<LIMBS> {
    fn rem_assign(&mut self, rhs: NonZero<Int<LIMBS>>) {
        *self = *self % rhs;
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>>
    for Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<RHS_LIMBS>>;

    fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        Wrapping(self.0 % rhs)
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<NonZero<Int<RHS_LIMBS>>>
    for &Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<RHS_LIMBS>>;

    fn rem(self, rhs: NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self % rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>>
    for &Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<RHS_LIMBS>>;

    fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        *self % *rhs
    }
}

impl<const LIMBS: usize, const RHS_LIMBS: usize> Rem<&NonZero<Int<RHS_LIMBS>>>
    for Wrapping<Int<LIMBS>>
{
    type Output = Wrapping<Int<RHS_LIMBS>>;

    fn rem(self, rhs: &NonZero<Int<RHS_LIMBS>>) -> Self::Output {
        self % *rhs
    }
}

impl<const LIMBS: usize> RemAssign<NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
    fn rem_assign(&mut self, rhs: NonZero<Int<LIMBS>>) {
        *self %= &rhs;
    }
}

impl<const LIMBS: usize> RemAssign<&NonZero<Int<LIMBS>>> for Wrapping<Int<LIMBS>> {
    fn rem_assign(&mut self, rhs: &NonZero<Int<LIMBS>>) {
        *self = Wrapping(self.0 % rhs);
    }
}

#[cfg(test)]
mod tests {
    use crate::{CtAssign, DivVartime, I128, Int, NonZero, One, Zero};

    #[test]
    #[allow(clippy::init_numbered_fields)]
    fn test_checked_div() {
        let min_plus_one = Int {
            0: I128::MIN.0.wrapping_add(&I128::ONE.0),
        };

        // lhs = min

        let result = I128::MIN.checked_div(&I128::MIN);
        assert_eq!(result.unwrap(), I128::ONE);

        let result = I128::MIN.checked_div(&I128::MINUS_ONE);
        assert!(bool::from(result.is_none()));

        let result = I128::MIN.checked_div(&I128::ZERO);
        assert!(bool::from(result.is_none()));

        let result = I128::MIN.checked_div(&I128::ONE);
        assert_eq!(result.unwrap(), I128::MIN);

        let result = I128::MIN.checked_div(&I128::MAX);
        assert_eq!(result.unwrap(), I128::MINUS_ONE);

        // lhs = -1

        let result = I128::MINUS_ONE.checked_div(&I128::MIN);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::MINUS_ONE.checked_div(&I128::MINUS_ONE);
        assert_eq!(result.unwrap(), I128::ONE);

        let result = I128::MINUS_ONE.checked_div(&I128::ZERO);
        assert!(bool::from(result.is_none()));

        let result = I128::MINUS_ONE.checked_div(&I128::ONE);
        assert_eq!(result.unwrap(), I128::MINUS_ONE);

        let result = I128::MINUS_ONE.checked_div(&I128::MAX);
        assert_eq!(result.unwrap(), I128::ZERO);

        // lhs = 0

        let result = I128::ZERO.checked_div(&I128::MIN);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::ZERO.checked_div(&I128::MINUS_ONE);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::ZERO.checked_div(&I128::ZERO);
        assert!(bool::from(result.is_none()));

        let result = I128::ZERO.checked_div(&I128::ONE);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::ZERO.checked_div(&I128::MAX);
        assert_eq!(result.unwrap(), I128::ZERO);

        // lhs = 1

        let result = I128::ONE.checked_div(&I128::MIN);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::ONE.checked_div(&I128::MINUS_ONE);
        assert_eq!(result.unwrap(), I128::MINUS_ONE);

        let result = I128::ONE.checked_div(&I128::ZERO);
        assert!(bool::from(result.is_none()));

        let result = I128::ONE.checked_div(&I128::ONE);
        assert_eq!(result.unwrap(), I128::ONE);

        let result = I128::ONE.checked_div(&I128::MAX);
        assert_eq!(result.unwrap(), I128::ZERO);

        // lhs = max

        let result = I128::MAX.checked_div(&I128::MIN);
        assert_eq!(result.unwrap(), I128::ZERO);

        let result = I128::MAX.checked_div(&I128::MINUS_ONE);
        assert_eq!(result.unwrap(), min_plus_one);

        let result = I128::MAX.checked_div(&I128::ZERO);
        assert!(bool::from(result.is_none()));

        let result = I128::MAX.checked_div(&I128::ONE);
        assert_eq!(result.unwrap(), I128::MAX);

        let result = I128::MAX.checked_div(&I128::MAX);
        assert_eq!(result.unwrap(), I128::ONE);
    }

    #[test]
    fn test_checked_div_floor() {
        assert_eq!(
            I128::from(8).checked_div_floor(&I128::from(3)).unwrap(),
            I128::from(2)
        );
        assert_eq!(
            I128::from(-8).checked_div_floor(&I128::from(3)).unwrap(),
            I128::from(-3)
        );
        assert_eq!(
            I128::from(8).checked_div_floor(&I128::from(-3)).unwrap(),
            I128::from(-3)
        );
        assert_eq!(
            I128::from(-8).checked_div_floor(&I128::from(-3)).unwrap(),
            I128::from(2)
        );
    }

    #[test]
    fn test_checked_div_mod_floor() {
        let (quotient, remainder) =
            I128::MIN.checked_div_rem_floor(&I128::MINUS_ONE.to_nz().unwrap());
        assert!(!quotient.is_some().to_bool());
        assert_eq!(remainder, I128::ZERO);
    }

    #[test]
    fn div_vartime_through_trait() {
        fn myfn<T: DivVartime + Zero + One + CtAssign>(x: T, y: T) -> T {
            x.div_vartime(&NonZero::new(y).unwrap())
        }
        assert_eq!(myfn(I128::from(8), I128::from(3)), I128::from(2));
        assert_eq!(myfn(I128::from(-8), I128::from(3)), I128::from(-2));
        assert_eq!(myfn(I128::from(8), I128::from(-3)), I128::from(-2));
        assert_eq!(myfn(I128::from(-8), I128::from(-3)), I128::from(2));
        assert_eq!(myfn(I128::MAX, I128::from(1)), I128::MAX);
        assert_eq!(myfn(I128::MAX, I128::MAX), I128::from(1));
    }
}