crypto-bigint 0.7.11

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`/`no_alloc`-friendly implementations of modern formulas using const generics. Also includes optional support for heap-backed integers.
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
//! Integer primitives for cryptographic arithmetic.

use crate::{Choice, WideWord, Word};
use core::hint::black_box;

/// Computes `lhs + rhs + carry`, returning the result along with the new carry (0, 1, or 2).
#[inline(always)]
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn carrying_add(lhs: Word, rhs: Word, carry: Word) -> (Word, Word) {
    if cfg!(all(target_arch = "arm", not(target_feature = "thumb2"))) {
        // Widened addition can introduce secret-dependent branches when extracting carries
        // on Thumb-1. Use the hardened word-sized primitive for both additions instead.
        let (sum, carry1) = overflowing_add(lhs, rhs);
        let (sum, carry2) = overflowing_add(sum, carry);

        // The incoming carry is a full Word, so both additions can overflow.
        (sum, carry1.wrapping_add(carry2))
    } else {
        // We could use `Word::overflowing_add()` here analogous to `overflowing_add()`,
        // but this version seems to produce a slightly better assembly.
        let a = lhs as WideWord;
        let b = rhs as WideWord;
        let carry = carry as WideWord;
        let ret = a + b + carry;
        (ret as Word, (ret >> Word::BITS) as Word)
    }
}

/// Computes `lhs + rhs`, returning the result along with the carry (0 or 1).
#[inline(always)]
pub(crate) const fn overflowing_add(lhs: Word, rhs: Word) -> (Word, Word) {
    if cfg!(all(target_arch = "arm", not(target_feature = "thumb2"))) {
        let sum = lhs.wrapping_add(rhs);

        // See "Hacker's Delight", 2nd edition, section 2-13 (Overflow detection).
        // Avoid a boolean overflow result, which can become a branch on Thumb-1.
        // TODO(tarcieri): export `ctutils::bitborrow!` macro and use that
        let carry = ((lhs & rhs) | ((lhs | rhs) & !sum)) >> (Word::BITS - 1);
        (sum, black_box(carry))
    } else {
        let (res, carry) = lhs.overflowing_add(rhs);
        (res, carry as Word)
    }
}

/// Computes `lhs - rhs - borrow`, using word-sized borrow masks.
///
/// The incoming borrow is the most significant bit of `borrow`; all other bits are ignored.
/// The outgoing borrow is `Word::MAX` if the subtraction underflows, or zero otherwise.
#[inline]
pub(crate) const fn borrowing_sub(lhs: Word, rhs: Word, borrow: Word) -> (Word, Word) {
    // Keep all arithmetic in Word: widening can trigger incorrect optimization on older compilers:
    // https://github.com/rust-lang/rust/issues/149522
    let difference = lhs
        .wrapping_sub(rhs)
        .wrapping_sub(borrow >> (Word::BITS - 1));

    // See "Hacker's Delight", 2nd edition, section 2-13 (Overflow detection).
    let borrow = ((!lhs & rhs) | ((!lhs | rhs) & difference)) >> (Word::BITS - 1);

    // Keep the full-width bit opaque before forming a mask, so a caller's masked addend can't be
    // folded into a secret-dependent branch.
    (difference, black_box(borrow).wrapping_neg())
}

/// Computes `lhs * rhs`, returning the low and the high words of the result.
#[inline(always)]
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn widening_mul(lhs: Word, rhs: Word) -> (Word, Word) {
    let a = lhs as WideWord;
    let b = rhs as WideWord;
    let ret = a * b;
    (ret as Word, (ret >> Word::BITS) as Word)
}

/// Computes `(lhs * rhs) + addend + carry`, returning the result along with the new carry.
#[inline(always)]
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn carrying_mul_add(
    lhs: Word,
    rhs: Word,
    addend: Word,
    carry: Word,
) -> (Word, Word) {
    let lhs = lhs as WideWord;
    let rhs = rhs as WideWord;
    let addend = addend as WideWord;
    let carry = carry as WideWord;

    // Cannot overflow:
    // lhs      * rhs      + addend   + carry
    // (2^64-1) * (2^64-1) + (2^64-1) + (2^64-1) =
    // 2^128 - 2^65 + 1 + 2^64 - 1 + 2^64 - 1 =
    // 2^128 - 2^65 + 2*2^64 - 1 =
    // 2^128 - 1 = u128::MAX
    let ret = ((lhs * rhs) + addend) + carry;
    (ret as Word, (ret >> Word::BITS) as Word)
}

/// Count leading zero bits, returning `Word::BITS` if `value` is zero.
#[inline]
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn leading_zeros(value: Word) -> u32 {
    if cfg!(any(target_arch = "aarch64", target_arch = "x86_64")) {
        value.leading_zeros()
    } else {
        cpubits::cpubits! {
            32 => { leading_zeros_u32(value) }
            64 => {
                let hi = (value >> 32) as u32;
                let lo = value as u32;
                let mask = Choice::from_u32_nz(hi).not().to_u32_mask();
                leading_zeros_u32(hi).wrapping_add(mask & leading_zeros_u32(lo))
            }
        }
    }
}

/// Count leading zero bits, using native instructions where supported.
#[inline]
const fn leading_zeros_u32(value: u32) -> u32 {
    if cfg!(any(
        target_arch = "aarch64",
        target_arch = "x86",
        target_arch = "x86_64",
        target_feature = "thumb2",
        target_feature = "zbb"
    )) {
        value.leading_zeros()
    } else {
        leading_zeros_u32_soft(value)
    }
}

/// Fixed-stage binary search for the highest set bit, including the zero case.
#[inline]
const fn leading_zeros_u32_soft(mut value: u32) -> u32 {
    let mut count = 0u32;
    let mut shift = 16;

    while shift != 0 {
        let mask = Choice::from_u32_nz(value >> (u32::BITS - shift))
            .not()
            .to_u32_mask();
        value ^= mask & (value ^ (value << shift));
        count = count.wrapping_add(mask & shift);
        shift >>= 1;
    }

    count.wrapping_add(Choice::from_u32_nz(value).not().to_u32_mask() & 1)
}

/// Count trailing zero bits, returning `Word::BITS` if `value` is zero.
#[inline]
#[allow(clippy::cast_possible_truncation)]
pub(crate) const fn trailing_zeros(value: Word) -> u32 {
    if cfg!(any(target_arch = "aarch64", target_arch = "x86_64")) {
        value.trailing_zeros()
    } else {
        cpubits::cpubits! {
            32 => { trailing_zeros_u32(value) }
            64 => {
                let hi = (value >> 32) as u32;
                let lo = value as u32;
                let mask = Choice::from_u32_nz(lo).not().to_u32_mask();
                trailing_zeros_u32(lo).wrapping_add(mask & trailing_zeros_u32(hi))
            }
        }
    }
}

/// Count trailing zero bits, using native instructions where supported.
#[inline]
const fn trailing_zeros_u32(value: u32) -> u32 {
    if cfg!(any(
        target_arch = "aarch64",
        target_arch = "x86",
        target_arch = "x86_64",
        target_feature = "thumb2",
        target_feature = "zbb"
    )) {
        value.trailing_zeros()
    } else {
        // Reversing bits uses fixed shifts and masks when RBIT is unavailable.
        leading_zeros_u32_soft(value.reverse_bits())
    }
}

/// `const fn` equivalent of `u32::max(a, b)`.
#[inline]
pub(crate) const fn u32_max(a: u32, b: u32) -> u32 {
    Choice::from_u32_lt(a, b).select_u32(a, b)
}

/// `const` equivalent of `u32::min(a, b)`.
#[inline]
pub(crate) const fn u32_min(a: u32, b: u32) -> u32 {
    Choice::from_u32_lt(a, b).select_u32(b, a)
}

/// Remainder calculation, constant time for a given divisor `d`.
/// Based on "Faster Remainder by Direct Computation: Applications to Compilers and Software Libraries"
/// by Daniel Lemire, Owen Kaser, and Nathan Kurz., Fig. 1.
#[inline(never)]
#[allow(clippy::cast_possible_truncation, reason = "needs triage")]
#[allow(clippy::integer_division_remainder_used, reason = "needs triage")]
pub(crate) const fn u32_rem(n: u32, d: u32) -> u32 {
    assert!(d > 0, "divisor must be nonzero");
    let c = u64::MAX / (d as u64) + 1;
    (((c.wrapping_mul(n as u64) as u128) * d as u128) >> 64) as u32
}

/// Compute the number of bits needed to represent `n`.
#[inline(always)]
pub(crate) const fn u32_bits(n: u32) -> u32 {
    u32::BITS - n.leading_zeros()
}

/// Return a `Choice` representing whether `a < b`.
#[allow(clippy::cast_possible_truncation)]
#[cfg(target_pointer_width = "32")]
#[inline]
pub(crate) const fn usize_lt(a: usize, b: usize) -> Choice {
    Choice::from_u32_lt(a as u32, b as u32)
}

/// Return a `Choice` representing whether `a < b`.
#[allow(clippy::cast_possible_truncation)]
#[cfg(target_pointer_width = "64")]
#[inline]
pub(crate) const fn usize_lt(a: usize, b: usize) -> Choice {
    Choice::from_u64_lt(a as u64, b as u64)
}

cpubits::cpubits! {
    32 => {
        /// Returns the multiplicative inverse of the argument modulo 2^32.
        ///
        /// For correct results, the input `value` must be odd.
        #[must_use]
        pub(crate) const fn u32_invert_odd(value: u32) -> u32 {
            debug_assert!(value & 1 == 1, "value must be odd");
            let x = value.wrapping_mul(3) ^ 2;
            let y = 1u32.wrapping_sub(x.wrapping_mul(value));
            let (x, y) = (x.wrapping_mul(y.wrapping_add(1)), y.wrapping_mul(y));
            let (x, y) = (x.wrapping_mul(y.wrapping_add(1)), y.wrapping_mul(y));
            x.wrapping_mul(y.wrapping_add(1))
        }
    }
}

/// Returns the multiplicative inverse of the argument modulo 2^64. The implementation is based
/// on Hurchalla's method for computing the multiplicative inverse modulo a power of two, and
/// is essentially an optimized Newton iteration.
///
/// For correct results, the input `value` must be odd.
///
/// For better understanding the implementation, the following paper is recommended:
/// J. Hurchalla, "An Improved Integer Multiplicative Inverse (modulo 2^w)",
/// <https://arxiv.org/abs/2204.04342>
#[must_use]
pub(crate) const fn u64_invert_odd(value: u64) -> u64 {
    debug_assert!(value & 1 == 1, "value must be odd");
    let x = value.wrapping_mul(3) ^ 2;
    let y = 1u64.wrapping_sub(x.wrapping_mul(value));
    let (x, y) = (x.wrapping_mul(y.wrapping_add(1)), y.wrapping_mul(y));
    let (x, y) = (x.wrapping_mul(y.wrapping_add(1)), y.wrapping_mul(y));
    let (x, y) = (x.wrapping_mul(y.wrapping_add(1)), y.wrapping_mul(y));
    x.wrapping_mul(y.wrapping_add(1))
}

#[cfg(test)]
mod tests {
    use super::{u32_max, u32_min, u32_rem, usize_lt};
    use crate::{WideWord, Word};

    // These primitives must remain usable by const callers.
    const _: () = {
        assert!(super::carrying_add(Word::MAX, Word::MAX, Word::MAX).0 == Word::MAX - 2);
        assert!(super::carrying_add(Word::MAX, Word::MAX, Word::MAX).1 == 2);
        assert!(super::overflowing_add(Word::MAX, 1).0 == 0);
        assert!(super::overflowing_add(Word::MAX, 1).1 == 1);
        assert!(super::borrowing_sub(0, 0, Word::MAX).0 == Word::MAX);
        assert!(super::borrowing_sub(0, 0, Word::MAX).1 == Word::MAX);
        assert!(super::borrowing_sub(1, 0, Word::MAX).1 == 0);
        assert!(super::leading_zeros(0) == Word::BITS);
        assert!(super::trailing_zeros(0) == Word::BITS);
        assert!(super::leading_zeros_u32_soft(0) == u32::BITS);
    };

    #[test]
    #[allow(clippy::cast_possible_truncation, reason = "tests")]
    fn addition_boundary_values() {
        let high_bit = 1 << (Word::BITS - 1);
        let values = [
            0,
            1,
            2,
            high_bit - 1,
            high_bit,
            high_bit + 1,
            Word::MAX - 1,
            Word::MAX,
        ];

        for lhs in values {
            for rhs in values {
                let expected = WideWord::from(lhs) + WideWord::from(rhs);
                assert_eq!(
                    super::overflowing_add(lhs, rhs),
                    (expected as Word, (expected >> Word::BITS) as Word)
                );

                // The incoming carry is not restricted to a bit, and the outgoing carry can be 2.
                // In particular, combining the two overflows with OR is wrong.
                for carry in values {
                    let expected = expected + WideWord::from(carry);
                    assert_eq!(
                        super::carrying_add(lhs, rhs, carry),
                        (expected as Word, (expected >> Word::BITS) as Word)
                    );
                }
            }
        }
    }

    #[test]
    fn zero_counts() {
        for bit in 0..Word::BITS {
            for value in [0, 1 << bit, !(1 << bit), Word::MAX >> bit] {
                assert_eq!(super::leading_zeros(value), value.leading_zeros());
                assert_eq!(super::trailing_zeros(value), value.trailing_zeros());
            }
        }
    }

    #[test]
    fn zero_counts_u32() {
        for bit in 0..u32::BITS {
            for value in [0, 1 << bit, !(1 << bit), u32::MAX >> bit] {
                assert_eq!(super::leading_zeros_u32(value), value.leading_zeros());
                assert_eq!(super::trailing_zeros_u32(value), value.trailing_zeros());
                // Exercise the software fallback even on hosts with native instructions.
                assert_eq!(super::leading_zeros_u32_soft(value), value.leading_zeros());
                assert_eq!(
                    super::leading_zeros_u32_soft(value.reverse_bits()),
                    value.trailing_zeros()
                );
            }
        }
    }

    #[test]
    fn carrying_mul_add_cannot_overflow() {
        let lhs = Word::MAX;
        let rhs = Word::MAX;
        let addend = Word::MAX;
        let carry_in = Word::MAX;
        let (result, carry_out) = super::carrying_mul_add(lhs, rhs, addend, carry_in);
        assert_eq!(result, Word::MAX);
        assert_eq!(carry_out, Word::MAX);
    }

    #[test]
    fn test_u32_const_min() {
        assert_eq!(u32_min(0, 5), 0);
        assert_eq!(u32_min(7, 0), 0);
        assert_eq!(u32_min(7, 5), 5);
        assert_eq!(u32_min(7, 7), 7);
    }

    #[test]
    fn test_u32_const_max() {
        assert_eq!(u32_max(0, 5), 5);
        assert_eq!(u32_max(7, 0), 7);
        assert_eq!(u32_max(7, 5), 7);
        assert_eq!(u32_max(7, 7), 7);
    }

    #[test]
    fn test_u32_const_rem() {
        assert_eq!(u32_rem(0, 5), 0);
        assert_eq!(u32_rem(4, 5), 4);
        assert_eq!(u32_rem(7, 5), 2);
        assert_eq!(u32_rem(101, 5), 1);
    }

    #[test]
    fn test_usize_const_lt() {
        assert!(usize_lt(0, 5).to_bool());
        assert!(!usize_lt(7, 0).to_bool());
        assert!(!usize_lt(7, 5).to_bool());
        assert!(!usize_lt(7, 7).to_bool());
    }

    cpubits::cpubits! {
        32 => {
            #[test]
            fn test_u32_invert_odd() {
                use super::u32_invert_odd;

                assert_eq!(u32_invert_odd(1), 1);
                assert_eq!(u32_invert_odd(5).wrapping_mul(5), 1);
                assert_eq!(u32_invert_odd(u32::MAX).wrapping_mul(u32::MAX), 1);
            }
        }
    }
}