cmpa 0.1.6

Multiprecision arithmetic primitives commonly needed for asymmetric cryptography
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// SPDX-License-Identifier: Apache-2.0
// Copyright 2023, 2026 SUSE LLC
// Author: Nicolai Stange <nstange@suse.de>

//! Implementation of multiprecision integer comparison primitives.

use super::limb::{LimbChoice, LimbType, ct_is_zero_l, ct_leq_l_l, ct_lt_or_eq_l_l};
use super::limbs::MpUIntCommon;
#[cfg(test)]
use super::limbs::{MpMutUInt, MpMutUIntSlice};

/// Compare two multiprecision integers for `==`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is equal to `op1`
/// in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned. See [`ct_is_zero_mp()`] and [`ct_is_one_mp()`]
/// for comparing against zero or one.
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_eq_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    let op0_nlimbs = op0.nlimbs();
    let op1_nlimbs = op1.nlimbs();
    let common_nlimbs = op0_nlimbs.min(op1_nlimbs);

    let mut is_neq = 0;
    for i in 0..common_nlimbs {
        let op0_val = op0.load_l(i);
        let op1_val = op1.load_l(i);
        is_neq |= op0_val ^ op1_val;
    }

    for i in common_nlimbs..op0_nlimbs {
        let op0_val = op0.load_l(i);
        is_neq |= op0_val;
    }

    for i in common_nlimbs..op1_nlimbs {
        let op1_val = op1.load_l(i);
        is_neq |= op1_val;
    }

    LimbChoice::from(ct_is_zero_l(is_neq))
}

#[cfg(test)]
fn test_ct_eq_mp_mp<T0: MpMutUIntSlice, T1: MpMutUIntSlice>() {
    use super::limb::LIMB_BYTES;

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [1]
    op1.store_l(0, 1);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [2 1]
    op0.store_l(0, 1);
    op0.store_l(1, 2);
    // [2 1]
    op1.store_l(0, 1);
    op1.store_l(1, 2);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [2 1]
    op1.store_l(0, 1);
    op1.store_l(1, 2);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [2 1]
    op0.store_l(0, 1);
    op0.store_l(1, 2);
    // [1]
    op1.store_l(0, 1);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [0 2]
    op1.store_l(0, 2);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [0 2]
    op1.store_l(0, 2);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [2]
    op1.store_l(0, 2);
    assert_eq!(ct_eq_mp_mp(&op0, &op1).unwrap(), 0);
}

#[test]
fn test_ct_eq_be_be() {
    use super::limbs::MpMutBigEndianUIntByteSlice;
    test_ct_eq_mp_mp::<MpMutBigEndianUIntByteSlice, MpMutBigEndianUIntByteSlice>()
}

#[test]
fn test_ct_eq_le_le() {
    use super::limbs::MpMutLittleEndianUIntByteSlice;
    test_ct_eq_mp_mp::<MpMutLittleEndianUIntByteSlice, MpMutLittleEndianUIntByteSlice>()
}

#[test]
fn test_ct_eq_ne_ne() {
    use super::limbs::MpMutNativeEndianUIntLimbsSlice;
    test_ct_eq_mp_mp::<MpMutNativeEndianUIntLimbsSlice, MpMutNativeEndianUIntLimbsSlice>()
}

/// Compare two multiprecision integers for `!=`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is not equal to `op1`
/// in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned.
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_neq_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    !ct_eq_mp_mp(op0, op1)
}

/// Incremental, limb-wise `<=` comparison of two multiprecision integers.
///
/// Used for comparing two multiprecision integers' values while their limbs are
/// being produced or consumed one at a time, e.g. by some arithmetic primitive,
/// without a separate pass over them. The limbs are to be fed to
/// [`update()`](Self::update) in pairs, from least to most significant.
/// [`finish()`](Self::finish) then returns the comparison's result.
///
/// Each step runs in constant time.
struct CtLeqMpMpKernel {
    /// Whether the multiprecision integers' parts inspected so far, i.e. their
    /// least significant limbs up to and including the most recently
    /// [`update()`](Self::update)d ones, compare as `<=`. Either `0` or `1`.
    tail_is_leq: LimbType,
}

impl CtLeqMpMpKernel {
    /// Start a new comparison.
    ///
    /// Before any limbs have been fed, i.e. for two empty multiprecision
    /// integers, the result is `true`.
    pub fn new() -> Self {
        Self { tail_is_leq: 1 }
    }

    /// Feed the next, more significant pair of limbs.
    ///
    /// # Arguments
    ///
    /// * `v0_val` - The next limb of the first operand, `op0`.
    /// * `v1_val` - The next limb of the second operand, `op1`.
    pub fn update(&mut self, v0_val: LimbType, v1_val: LimbType) {
        let (is_lt, is_eq) = ct_lt_or_eq_l_l(v0_val, v1_val);
        // Consider the result from the tail comparisons only if the currently
        // inspected limbs are equal.
        self.tail_is_leq &= is_eq;
        // If the current v0_val is < v1_val, the tail does not matter.
        self.tail_is_leq |= is_lt;
    }

    /// Finish the comparison.
    ///
    /// Returns a [`LimbChoice`] representing `true` if the first operand,
    /// composed of all `v0_val` limbs fed to [`update()`](Self::update), is
    /// less than or equal to the second one, composed of all `v1_val` limbs,
    /// and `false` otherwise.
    pub fn finish(self) -> LimbChoice {
        LimbChoice::from(self.tail_is_leq)
    }
}

/// Incremental, limb-wise `>=` comparison of two multiprecision integers.
///
/// The `>=` counterpart of [`CtLeqMpMpKernel`], see there for the usage. The
/// limbs are to be fed to [`update()`](Self::update) in pairs, from least to
/// most significant. [`finish()`](Self::finish) then returns the comparison's
/// result.
///
/// Each step runs in constant time.
pub(super) struct CtGeqMpMpKernel {
    /// The underlying `<=` comparison, fed with the operands swapped.
    leq_kernel: CtLeqMpMpKernel,
}

impl CtGeqMpMpKernel {
    /// Start a new comparison.
    ///
    /// Before any limbs have been fed, i.e. for two empty multiprecision
    /// integers, the result is `true`.
    pub fn new() -> Self {
        Self {
            leq_kernel: CtLeqMpMpKernel::new(),
        }
    }

    /// Feed the next, more significant pair of limbs.
    ///
    /// # Arguments
    ///
    /// * `v0_val` - The next limb of the first operand, `op0`.
    /// * `v1_val` - The next limb of the second operand, `op1`.
    pub fn update(&mut self, v0_val: LimbType, v1_val: LimbType) {
        self.leq_kernel.update(v1_val, v0_val)
    }

    /// Finish the comparison.
    ///
    /// Returns a [`LimbChoice`] representing `true` if the first operand,
    /// composed of all `v0_val` limbs fed to [`update()`](Self::update), is
    /// greater than or equal to the second one, composed of all `v1_val`
    /// limbs, and `false` otherwise.
    pub fn finish(self) -> LimbChoice {
        self.leq_kernel.finish()
    }
}

/// Compare two multiprecision integers for `<=`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is less than or
/// equal to `op1` in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned. See [`ct_leq_mp_l()`] for comparing against a single
/// [`LimbType`].
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_leq_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    let op0_nlimbs = op0.nlimbs();
    let op1_nlimbs = op1.nlimbs();
    let common_nlimbs = op0_nlimbs.min(op1_nlimbs);

    let mut leq_kernel = CtLeqMpMpKernel::new();
    for i in 0..common_nlimbs {
        leq_kernel.update(op0.load_l(i), op1.load_l(i));
    }
    for i in common_nlimbs..op0_nlimbs {
        leq_kernel.update(op0.load_l(i), 0);
    }
    for i in common_nlimbs..op1_nlimbs {
        leq_kernel.update(0, op1.load_l(i));
    }

    leq_kernel.finish()
}

#[cfg(test)]
fn test_ct_leq_mp_mp<T0: MpMutUIntSlice, T1: MpMutUIntSlice>() {
    use super::limb::LIMB_BYTES;

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let op1 = T1::from_slice(&mut op1).unwrap();
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let op1 = T1::from_slice(&mut op1).unwrap();
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [0 2]
    op1.store_l(0, 2);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1]
    op0.store_l(0, 1);
    // [1 0]
    op1.store_l(1, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [2]
    op1.store_l(0, 2);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [0 2]
    op1.store_l(0, 2);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [0 1]
    op0.store_l(0, 1);
    // [1 0]
    op1.store_l(1, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let op1 = T1::from_slice(&mut op1).unwrap();
    // [1 0]
    op0.store_l(1, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 0]
    op0.store_l(1, 1);
    // [1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 0]
    op0.store_l(1, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 1]
    op0.store_l(0, 1);
    op0.store_l(1, 1);
    // [1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 1]
    op0.store_l(0, 1);
    op0.store_l(1, 1);
    // [0 1]
    op1.store_l(0, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 1]
    op0.store_l(0, 1);
    op0.store_l(1, 1);
    // [1 0]
    op1.store_l(1, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 0);

    let mut op0 = tst_mk_mp_backing_vec!(T0, 2 * LIMB_BYTES);
    let mut op0 = T0::from_slice(&mut op0).unwrap();
    let mut op1 = tst_mk_mp_backing_vec!(T1, 2 * LIMB_BYTES);
    let mut op1 = T1::from_slice(&mut op1).unwrap();
    // [1 1]
    op0.store_l(0, 1);
    op0.store_l(1, 1);
    // [1 1]
    op1.store_l(0, 1);
    op1.store_l(1, 1);
    assert_eq!(ct_leq_mp_mp(&op0, &op1).unwrap(), 1);
}

#[test]
fn test_ct_leq_be_be() {
    use super::limbs::MpMutBigEndianUIntByteSlice;
    test_ct_leq_mp_mp::<MpMutBigEndianUIntByteSlice, MpMutBigEndianUIntByteSlice>()
}

#[test]
fn test_ct_leq_le_le() {
    use super::limbs::MpMutLittleEndianUIntByteSlice;
    test_ct_leq_mp_mp::<MpMutLittleEndianUIntByteSlice, MpMutLittleEndianUIntByteSlice>()
}

#[test]
fn test_ct_leq_ne_ne() {
    use super::limbs::MpMutNativeEndianUIntLimbsSlice;
    test_ct_leq_mp_mp::<MpMutNativeEndianUIntLimbsSlice, MpMutNativeEndianUIntLimbsSlice>()
}

/// Compare two multiprecision integers for `<`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is less than `op1`
/// in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned.
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_lt_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    !ct_geq_mp_mp(op0, op1)
}

/// Compare two multiprecision integers for `>=`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is greater than or
/// equal to `op1` in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned.
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_geq_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    ct_leq_mp_mp(op1, op0)
}

/// Compare two multiprecision integers for `>`.
///
/// The two multiprecision integers may be of different representations and
/// of arbitrary [lengths in units of bytes](MpUIntCommon::len), independently
/// of each other.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is greater than `op1`
/// in value and `false` otherwise.
///
/// Execution time depends only on the multiprecision integers' [lengths in
/// units of bytes](MpUIntCommon::len) and is otherwise constant as far as
/// branching is concerned.
///
/// # Arguments
///
/// * `op0` - The first operand.
/// * `op1` - The second operand.
pub fn ct_gt_mp_mp<T0: MpUIntCommon, T1: MpUIntCommon>(op0: &T0, op1: &T1) -> LimbChoice {
    !ct_leq_mp_mp(op0, op1)
}

/// Test whether a multiprecision integer is zero.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is zero in value and
/// `false` otherwise. An empty multiprecision integer is zero.
///
/// Execution time depends only on `op0`'s [length in units of
/// bytes](MpUIntCommon::len) and is otherwise constant as far as branching is
/// concerned. This is a specialized variant of [`ct_eq_mp_mp()`] for comparing
/// against zero.
///
/// # Arguments
///
/// * `op0` - The multiprecision integer to test.
pub fn ct_is_zero_mp<T0: MpUIntCommon>(op0: &T0) -> LimbChoice {
    let mut is_nz: LimbType = 0;
    for i in 0..op0.nlimbs() {
        let op0_val = op0.load_l(i);
        is_nz |= op0_val;
    }
    LimbChoice::from(ct_is_zero_l(is_nz))
}

/// Test whether a multiprecision integer is one.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is one in value and
/// `false` otherwise. An empty multiprecision integer is zero, hence not one.
///
/// Execution time depends only on `op0`'s [length in units of
/// bytes](MpUIntCommon::len) and is otherwise constant as far as branching is
/// concerned. This is a specialized variant of [`ct_eq_mp_mp()`] for comparing
/// against one.
///
/// # Arguments
///
/// * `op0` - The multiprecision integer to test.
pub fn ct_is_one_mp<T0: MpUIntCommon>(op0: &T0) -> LimbChoice {
    if op0.is_empty() {
        return LimbChoice::from(0);
    }

    let tail_is_not_one = op0.load_l(0) ^ 1;

    let mut head_is_nz: LimbType = 0;
    for i in 1..op0.nlimbs() {
        let op0_val = op0.load_l(i);
        head_is_nz |= op0_val;
    }

    LimbChoice::from(ct_is_zero_l(tail_is_not_one | head_is_nz))
}

/// Compare a multiprecision integer and a [`LimbType`] for `<=`.
///
/// Returns a [`LimbChoice`] representing `true` if `op0` is less than or equal
/// to `op1` in value and `false` otherwise. An empty multiprecision integer is
/// zero, hence less than or equal to any `op1`.
///
/// Execution time depends only on `op0`'s [length in units of
/// bytes](MpUIntCommon::len) and is otherwise constant as far as branching is
/// concerned. This is a specialized variant of [`ct_leq_mp_mp()`] for a single
/// [`LimbType`] second operand.
///
/// # Arguments
///
/// * `op0` - The multiprecision integer to compare. Its [length in units of
///   bytes](MpUIntCommon::len) isn't restricted.
/// * `op1` - The [`LimbType`] to compare against.
pub fn ct_leq_mp_l<T0: MpUIntCommon>(op0: &T0, op1: LimbType) -> LimbChoice {
    if op0.is_empty() {
        return LimbChoice::from(1);
    }

    let l0_is_leq = ct_leq_l_l(op0.load_l(0), op1);

    let mut head_is_nz: LimbType = 0;
    for i in 1..op0.nlimbs() {
        let op0_val = op0.load_l(i);
        head_is_nz |= op0_val;
    }

    l0_is_leq & LimbChoice::from(ct_is_zero_l(head_is_nz))
}