fixed-bigint 0.6.1

Fixed-size big integer implementation for Rust
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
//! The shift-family traits for `HeaplessBigInt`: overflowing / wrapping /
//! checked / unbounded / exact / funnel shifts, plus the `num_traits`
//! wrapping/checked wrappers.
//!
//! All build on the inherent `<<` / `>>`, so they inherit those width
//! contracts: the left-shift family is width-preserving (`out_len =
//! self.len`, bits past the width discarded), the right-shift family follows
//! `Shr`'s whole-word narrowing (`out_len = self.len - bits/word_bits`). The
//! shift amount is a public `u32`, so every arm is personality-generic.
//!
//! The "overflow" flag / `None` / wrap is purely about the *amount* (`bits >=
//! value_width`), exactly like the primitive `overflowing_shl` — it is not a
//! value predicate, so it stays uniform across `Nct`/`Ct`.

use super::HeaplessBigInt;
use crate::MachineWord;
use const_num_traits::{
    CheckedShl, CheckedShr, FunnelShl, FunnelShr, OverflowingShl, OverflowingShr, Personality,
    PersonalityTag, PrimBits, ShlExact, ShrExact, UnboundedShl, UnboundedShr, WrappingShl,
    WrappingShr,
};

impl<T: MachineWord, const CAP: usize, P: Personality> HeaplessBigInt<T, CAP, P> {
    /// Operating width in bits (`len · word_bits`). Fits `u32` for any
    /// representable `len` (≤ `u16::MAX`) and word size (≤ 64).
    #[inline]
    fn value_bits(&self) -> u32 {
        self.len as u32 * (core::mem::size_of::<T>() as u32 * 8)
    }
}

/// `(normalized_shift, overflowed)` — mirrors the primitive masking. A
/// zero-width value (`len == 0`) always overflows.
#[inline]
fn normalize_shift(bits: u32, value_bits: u32) -> (usize, bool) {
    if value_bits == 0 {
        (0, true)
    } else if bits >= value_bits {
        ((bits % value_bits) as usize, true)
    } else {
        (bits as usize, false)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> OverflowingShl
    for HeaplessBigInt<T, CAP, P>
{
    type Output = Self;
    fn overflowing_shl(self, bits: u32) -> (Self, bool) {
        let (shift, overflow) = normalize_shift(bits, self.value_bits());
        (self << shift, overflow)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> OverflowingShr
    for HeaplessBigInt<T, CAP, P>
{
    type Output = Self;
    fn overflowing_shr(self, bits: u32) -> (Self, bool) {
        let (shift, overflow) = normalize_shift(bits, self.value_bits());
        (self >> shift, overflow)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> WrappingShl for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn wrapping_shl(self, bits: u32) -> Self {
        OverflowingShl::overflowing_shl(self, bits).0
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> WrappingShr for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn wrapping_shr(self, bits: u32) -> Self {
        OverflowingShr::overflowing_shr(self, bits).0
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> CheckedShl for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn checked_shl(self, bits: u32) -> Option<Self> {
        let (res, overflow) = OverflowingShl::overflowing_shl(self, bits);
        if overflow { None } else { Some(res) }
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> CheckedShr for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn checked_shr(self, bits: u32) -> Option<Self> {
        let (res, overflow) = OverflowingShr::overflowing_shr(self, bits);
        if overflow { None } else { Some(res) }
    }
}

// Unbounded: shift by any amount, saturating to zero past the width. The
// inherent `<<` / `>>` already collapse to zero once the whole-word shift
// clears every limb, so an over-width amount is handled directly.
impl<T: MachineWord, const CAP: usize, P: Personality> UnboundedShl for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn unbounded_shl(self, rhs: u32) -> Self {
        match P::TAG {
            // Ct: the over-width guard would branch on the (secret) amount;
            // the Ct `<<` barrel already collapses over-width shifts to zero.
            PersonalityTag::Ct => self << (rhs as usize),
            PersonalityTag::Nct => {
                if rhs >= self.value_bits() {
                    Self::new_zero_with_len(self.len())
                } else {
                    self << (rhs as usize)
                }
            }
        }
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> UnboundedShr for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn unbounded_shr(self, rhs: u32) -> Self {
        match P::TAG {
            PersonalityTag::Ct => self >> (rhs as usize),
            PersonalityTag::Nct => {
                if rhs >= self.value_bits() {
                    Self::new_zero_with_len(self.len())
                } else {
                    self >> (rhs as usize)
                }
            }
        }
    }
}

// Exact (lossless) shifts: `None` if any one-bit would be shifted out or the
// amount reaches the value width.
impl<T: MachineWord, const CAP: usize, P: Personality> ShlExact for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn shl_exact(self, rhs: u32) -> Option<Self> {
        if rhs < self.value_bits() && rhs <= PrimBits::leading_zeros(self) {
            Some(self << (rhs as usize))
        } else {
            None
        }
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> ShrExact for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn shr_exact(self, rhs: u32) -> Option<Self> {
        if rhs < self.value_bits() && rhs <= PrimBits::trailing_zeros(self) {
            // A whole-limb `>>` narrows `len`; an *exact* (reversible) shift
            // must keep the operand width so `<<` by the same amount recovers
            // the input. No bits are lost (the trailing-zero check passed), so
            // widening back is value-preserving.
            let width = self.len();
            Some((self >> (rhs as usize)).widened(width))
        } else {
            None
        }
    }
}

// Funnel shifts: the double-width `(self, rhs)` shifted by `n`, one half
// returned. Both halves are taken at `self`'s width — `rhs` must share it, or
// its high limbs (outside the funnel word) would leak into the result — so a
// width mismatch is a caller error, asserted for `n > 0`. `n == 0` is a no-op
// checked first, so it never trips the width/range asserts (a `len == 0`
// operand has `value_bits() == 0`, against which `n < bits` would fail).
impl<T: MachineWord, const CAP: usize, P: Personality> FunnelShl for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn funnel_shl(self, rhs: Self, n: u32) -> Self {
        if n == 0 {
            return self;
        }
        assert!(
            self.len() == rhs.len(),
            "HeaplessBigInt::funnel_shl: operands must share a width"
        );
        let bits = self.value_bits();
        assert!(n < bits, "HeaplessBigInt::funnel_shl: n out of range");
        let lo_shift = bits - n;
        (self << (n as usize)) | (rhs >> (lo_shift as usize))
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> FunnelShr for HeaplessBigInt<T, CAP, P> {
    type Output = Self;
    fn funnel_shr(self, rhs: Self, n: u32) -> Self {
        if n == 0 {
            return rhs;
        }
        assert!(
            self.len() == rhs.len(),
            "HeaplessBigInt::funnel_shr: operands must share a width"
        );
        let bits = self.value_bits();
        assert!(n < bits, "HeaplessBigInt::funnel_shr: n out of range");
        let hi_shift = bits - n;
        (rhs >> (n as usize)) | (self << (hi_shift as usize))
    }
}

// Reference-receiver mirrors (`&HeaplessBigInt`), so `(&h).wrapping_shl(n)` etc.
// resolve. `HeaplessBigInt` is `Copy`; each delegates to the value impl on
// `*self` (and `*rhs` for the funnel operand). `u32` amounts pass through.

impl<T: MachineWord, const CAP: usize, P: Personality> OverflowingShl
    for &HeaplessBigInt<T, CAP, P>
{
    type Output = HeaplessBigInt<T, CAP, P>;
    fn overflowing_shl(self, bits: u32) -> (HeaplessBigInt<T, CAP, P>, bool) {
        <HeaplessBigInt<T, CAP, P> as OverflowingShl>::overflowing_shl(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> OverflowingShr
    for &HeaplessBigInt<T, CAP, P>
{
    type Output = HeaplessBigInt<T, CAP, P>;
    fn overflowing_shr(self, bits: u32) -> (HeaplessBigInt<T, CAP, P>, bool) {
        <HeaplessBigInt<T, CAP, P> as OverflowingShr>::overflowing_shr(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> WrappingShl for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn wrapping_shl(self, bits: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as WrappingShl>::wrapping_shl(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> WrappingShr for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn wrapping_shr(self, bits: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as WrappingShr>::wrapping_shr(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> CheckedShl for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn checked_shl(self, bits: u32) -> Option<HeaplessBigInt<T, CAP, P>> {
        <HeaplessBigInt<T, CAP, P> as CheckedShl>::checked_shl(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> CheckedShr for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn checked_shr(self, bits: u32) -> Option<HeaplessBigInt<T, CAP, P>> {
        <HeaplessBigInt<T, CAP, P> as CheckedShr>::checked_shr(*self, bits)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> UnboundedShl for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn unbounded_shl(self, rhs: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as UnboundedShl>::unbounded_shl(*self, rhs)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> UnboundedShr for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn unbounded_shr(self, rhs: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as UnboundedShr>::unbounded_shr(*self, rhs)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> ShlExact for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn shl_exact(self, rhs: u32) -> Option<HeaplessBigInt<T, CAP, P>> {
        <HeaplessBigInt<T, CAP, P> as ShlExact>::shl_exact(*self, rhs)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> ShrExact for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn shr_exact(self, rhs: u32) -> Option<HeaplessBigInt<T, CAP, P>> {
        <HeaplessBigInt<T, CAP, P> as ShrExact>::shr_exact(*self, rhs)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> FunnelShl for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn funnel_shl(self, rhs: Self, n: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as FunnelShl>::funnel_shl(*self, *rhs, n)
    }
}

impl<T: MachineWord, const CAP: usize, P: Personality> FunnelShr for &HeaplessBigInt<T, CAP, P> {
    type Output = HeaplessBigInt<T, CAP, P>;
    fn funnel_shr(self, rhs: Self, n: u32) -> HeaplessBigInt<T, CAP, P> {
        <HeaplessBigInt<T, CAP, P> as FunnelShr>::funnel_shr(*self, *rhs, n)
    }
}

// num_traits wrappers — delegate to the const-num-traits impls above.
#[cfg(feature = "num-traits")]
impl<T: MachineWord, const CAP: usize, P: Personality> num_traits::WrappingShl
    for HeaplessBigInt<T, CAP, P>
{
    fn wrapping_shl(&self, bits: u32) -> Self {
        <Self as WrappingShl>::wrapping_shl(*self, bits)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const CAP: usize, P: Personality> num_traits::WrappingShr
    for HeaplessBigInt<T, CAP, P>
{
    fn wrapping_shr(&self, bits: u32) -> Self {
        <Self as WrappingShr>::wrapping_shr(*self, bits)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const CAP: usize, P: Personality> num_traits::CheckedShl
    for HeaplessBigInt<T, CAP, P>
{
    fn checked_shl(&self, bits: u32) -> Option<Self> {
        <Self as CheckedShl>::checked_shl(*self, bits)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const CAP: usize, P: Personality> num_traits::CheckedShr
    for HeaplessBigInt<T, CAP, P>
{
    fn checked_shr(&self, bits: u32) -> Option<Self> {
        <Self as CheckedShr>::checked_shr(*self, bits)
    }
}

#[cfg(test)]
mod tests {
    use super::HeaplessBigInt;
    use const_num_traits::{
        CheckedShl, CheckedShr, FunnelShl, FunnelShr, OverflowingShl, ShlExact, ShrExact,
        UnboundedShl, UnboundedShr, WrappingShl,
    };

    type H = HeaplessBigInt<u8, 4>; // 32-bit width at len 4

    #[test]
    fn overflowing_wrapping_checked() {
        let v = H::from(1u8).widened(4);
        // In-range shift, no overflow.
        assert_eq!(
            OverflowingShl::overflowing_shl(v, 4),
            (H::from(16u8), false)
        );
        // Amount == width overflows; masked to 0 → shift by 0.
        assert_eq!(OverflowingShl::overflowing_shl(v, 32), (v, true));
        assert_eq!(WrappingShl::wrapping_shl(v, 32), v);
        assert_eq!(CheckedShl::checked_shl(v, 32), None);
        assert_eq!(CheckedShl::checked_shl(v, 5), Some(H::from(32u8)));
        assert_eq!(CheckedShr::checked_shr(v, 32), None);
    }

    #[test]
    fn unbounded_saturates_to_zero() {
        let v = H::from(0xFFu8).widened(4);
        assert_eq!(UnboundedShl::unbounded_shl(v, 100), H::from(0u8));
        assert_eq!(UnboundedShr::unbounded_shr(v, 100), H::from(0u8));
        // Over-width shift keeps the operand width.
        assert_eq!(UnboundedShl::unbounded_shl(v, 100).len(), 4);
    }

    #[test]
    fn exact_shifts() {
        let v = H::from(0b100u8).widened(4);
        // 0b100 has 2 trailing zeros: shr by 2 is exact, by 3 loses the bit.
        assert_eq!(ShrExact::shr_exact(v, 2), Some(H::from(1u8)));
        assert_eq!(ShrExact::shr_exact(v, 3), None);
        // shl by more than leading_zeros drops the top bit.
        assert!(ShlExact::shl_exact(H::from(1u8).widened(4), 31).is_some());
        assert_eq!(ShlExact::shl_exact(H::from(1u8).widened(4), 32), None);
    }

    // A whole-limb exact shr must keep the operand width so it's reversible.
    #[test]
    fn shr_exact_preserves_width() {
        let v = H::from(256u16).widened(4); // [0, 1, 0, 0], len 4
        let r = ShrExact::shr_exact(v, 8).unwrap();
        assert_eq!(r, H::from(1u8));
        assert_eq!(r.len(), 4, "exact shr must not narrow away the width");
        // Reversible: shifting back left recovers the input.
        assert_eq!(r << 8usize, H::from(256u16));
    }

    // Over-width `Shl<u32>` / `Shr<u32>` zero out without truncating the count
    // (the 16-bit-usize hazard); Shl keeps the width, Shr empties.
    #[test]
    fn u32_operator_shifts_handle_over_width() {
        let v = H::from(0xFFu8).widened(4);
        let sl = core::ops::Shl::<u32>::shl(v, 100);
        assert!(<H as const_num_traits::Zero>::is_zero(&sl));
        assert_eq!(sl.len(), 4);
        let sr = core::ops::Shr::<u32>::shr(v, 100);
        assert!(<H as const_num_traits::Zero>::is_zero(&sr));
    }

    // funnel with n == 0 is a no-op even on a len-0 operand (value_bits() == 0
    // would otherwise trip the `n < bits` assert).
    #[test]
    fn funnel_zero_shift_on_empty_operand() {
        let z0 = H::new_zero_with_len(0);
        assert_eq!(FunnelShl::funnel_shl(z0, z0, 0).len(), 0);
        assert_eq!(FunnelShr::funnel_shr(z0, z0, 0).len(), 0);
    }

    #[test]
    #[should_panic(expected = "must share a width")]
    fn funnel_rejects_width_mismatch() {
        let narrow = H::from(1u8); // len 1
        let wide = H::from(1u8).widened(4); // len 4
        FunnelShl::funnel_shl(narrow, wide, 1);
    }

    #[test]
    fn funnel() {
        // (hi=0x1234, lo=0x5678) as a 32-bit pair, funnel_shl by 8 →
        // top 32 bits of (0x1234_5678 << 8) = 0x3456_78__ >> ... check value.
        let hi = H::from(0x1234_5678u32);
        let lo = H::from(0x9ABC_DEF0u32);
        // funnel_shl by 8: (hi << 8) | (lo >> 24) = 0x3456_7800 | 0x9A = 0x3456_789A
        assert_eq!(FunnelShl::funnel_shl(hi, lo, 8), H::from(0x3456_789Au32));
        // funnel_shr by 8: (lo >> 8) | (hi << 24) = 0x009A_BCDE | 0x7800_0000 = 0x789A_BCDE
        assert_eq!(FunnelShr::funnel_shr(hi, lo, 8), H::from(0x789A_BCDEu32));
        assert_eq!(FunnelShl::funnel_shl(hi, lo, 0), hi);
        assert_eq!(FunnelShr::funnel_shr(hi, lo, 0), lo);
    }

    // The `&Self` mirrors agree with the value impls.
    #[test]
    fn by_ref_matches_value() {
        let v = H::from(1u8).widened(4);
        assert_eq!(
            OverflowingShl::overflowing_shl(&v, 4),
            OverflowingShl::overflowing_shl(v, 4)
        );
        assert_eq!(
            WrappingShl::wrapping_shl(&v, 5),
            WrappingShl::wrapping_shl(v, 5)
        );
        assert_eq!(
            CheckedShl::checked_shl(&v, 5),
            CheckedShl::checked_shl(v, 5)
        );
        assert_eq!(
            UnboundedShl::unbounded_shl(&v, 100),
            UnboundedShl::unbounded_shl(v, 100)
        );
        assert_eq!(ShlExact::shl_exact(&v, 3), ShlExact::shl_exact(v, 3));
        assert_eq!(ShrExact::shr_exact(&v, 0), ShrExact::shr_exact(v, 0));

        let hi = H::from(0x1234_5678u32);
        let lo = H::from(0x9ABC_DEF0u32);
        assert_eq!(
            FunnelShl::funnel_shl(&hi, &lo, 8),
            FunnelShl::funnel_shl(hi, lo, 8)
        );
        assert_eq!(
            FunnelShr::funnel_shr(&hi, &lo, 8),
            FunnelShr::funnel_shr(hi, lo, 8)
        );
    }
}