oxinum-int 0.1.0

Arbitrary-precision integers for OxiNum (UBig/IBig via dashu-int)
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! Native `BigUint` — little-endian `Vec<u64>`-limb arbitrary-precision
//! unsigned integer.
//!
//! Invariants (enforced by every public constructor and after every operation
//! that mutates `limbs` in place):
//!
//! - `limbs` is little-endian: `limbs[0]` is the least-significant 64-bit limb.
//! - No trailing-zero limbs: `limbs.last() != Some(&0)`.
//! - Canonical zero: `limbs.is_empty()`.

use core::cmp::Ordering;
use core::ops::{BitAnd, BitOr, BitXor, Shl, ShlAssign, Shr, ShrAssign};

/// Threshold (in limbs) above which Karatsuba multiplication is used.
///
/// Below this threshold, schoolbook multiplication is used. Empirically
/// ~32 limbs is a good crossover on 64-bit hardware for OxiNum's working set.
pub const KARATSUBA_THRESHOLD: usize = 32;

/// Threshold (in limbs) at or above which Toom-Cook-3 multiplication is used.
///
/// Between [`KARATSUBA_THRESHOLD`] and this value, Karatsuba is used. At or
/// above this value the asymptotically faster Toom-3 (O(n^1.465)) takes over.
/// The dispatch gates on `min(a.len, b.len)` so both operands must be large.
/// Empirically ~100 limbs is a reasonable crossover on 64-bit hardware.
pub(crate) const TOOM3_THRESHOLD: usize = 100;

/// Native arbitrary-precision unsigned integer.
///
/// Stored as a little-endian `Vec<u64>` with no trailing-zero limbs.
///
/// # Examples
///
/// ```
/// use oxinum_int::native::BigUint;
///
/// let a = BigUint::from_u64(42);
/// let b = BigUint::from_u64(58);
/// let sum = &a + &b;
/// assert_eq!(sum, BigUint::from_u64(100));
/// ```
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BigUint {
    /// Little-endian limbs. No trailing zeros. Empty `Vec` = zero.
    pub(crate) limbs: Vec<u64>,
}

impl BigUint {
    /// The canonical zero value.
    pub const ZERO: BigUint = BigUint { limbs: Vec::new() };

    /// Construct a zero `BigUint`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert!(BigUint::zero().is_zero());
    /// ```
    #[inline]
    pub fn zero() -> Self {
        Self { limbs: Vec::new() }
    }

    /// Construct a `BigUint` equal to `1`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert!(BigUint::one().is_one());
    /// ```
    #[inline]
    pub fn one() -> Self {
        Self { limbs: vec![1] }
    }

    /// Construct from a primitive `u64`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_u64(42);
    /// assert_eq!(n.to_u64(), Some(42));
    /// ```
    #[inline]
    pub fn from_u64(value: u64) -> Self {
        if value == 0 {
            Self::zero()
        } else {
            Self { limbs: vec![value] }
        }
    }

    /// Construct from a primitive `u128`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let huge = BigUint::from_u128(u128::MAX);
    /// assert_eq!(huge.bit_length(), 128);
    /// ```
    #[inline]
    pub fn from_u128(value: u128) -> Self {
        let lo = value as u64;
        let hi = (value >> 64) as u64;
        if hi == 0 {
            Self::from_u64(lo)
        } else {
            Self {
                limbs: vec![lo, hi],
            }
        }
    }

    /// Construct from a slice of little-endian limbs (normalizing input).
    ///
    /// The input is little-endian: `limbs[0]` is the least-significant limb.
    /// Trailing zeros are stripped.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_le_limbs(&[5, 0, 0]);
    /// assert_eq!(n, BigUint::from_u64(5));
    /// ```
    pub fn from_le_limbs(limbs: &[u64]) -> Self {
        let mut v = limbs.to_vec();
        normalize(&mut v);
        Self { limbs: v }
    }

    /// Returns the raw little-endian limbs (no trailing zeros).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_u64(0xDEAD_BEEF);
    /// assert_eq!(n.as_limbs(), &[0xDEAD_BEEFu64]);
    /// ```
    #[inline]
    pub fn as_limbs(&self) -> &[u64] {
        &self.limbs
    }

    /// Returns `true` if this value is zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert!(BigUint::zero().is_zero());
    /// assert!(!BigUint::one().is_zero());
    /// ```
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.limbs.is_empty()
    }

    /// Returns `true` if this value is one.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert!(BigUint::one().is_one());
    /// ```
    #[inline]
    pub fn is_one(&self) -> bool {
        self.limbs.as_slice() == [1u64]
    }

    /// Try to convert to a `u64`, returning `None` on overflow.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(42).to_u64(), Some(42));
    /// ```
    #[inline]
    pub fn to_u64(&self) -> Option<u64> {
        match self.limbs.len() {
            0 => Some(0),
            1 => Some(self.limbs[0]),
            _ => None,
        }
    }

    /// Returns the number of significant bits (`0` for zero).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::zero().bit_length(), 0);
    /// assert_eq!(BigUint::from_u64(1).bit_length(), 1);
    /// assert_eq!(BigUint::from_u64(0xFF).bit_length(), 8);
    /// ```
    pub fn bit_length(&self) -> u64 {
        match self.limbs.last() {
            None => 0,
            Some(&top) => {
                let n_limbs = self.limbs.len() as u64;
                (n_limbs - 1) * 64 + (64 - top.leading_zeros() as u64)
            }
        }
    }

    /// Returns the number of trailing zero bits.
    ///
    /// By convention, returns `0` for zero (no bits to count).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(0b1000).trailing_zeros(), 3);
    /// assert_eq!(BigUint::zero().trailing_zeros(), 0);
    /// ```
    pub fn trailing_zeros(&self) -> u64 {
        for (i, &limb) in self.limbs.iter().enumerate() {
            if limb != 0 {
                return (i as u64) * 64 + (limb.trailing_zeros() as u64);
            }
        }
        0
    }

    /// Returns the total count of set bits (population count).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(0b1011).count_ones(), 3);
    /// ```
    pub fn count_ones(&self) -> u64 {
        self.limbs.iter().map(|l| l.count_ones() as u64).sum()
    }

    /// Returns the value of bit `index` (LSB-indexed).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_u64(0b1010);
    /// assert!(!n.test_bit(0));
    /// assert!(n.test_bit(1));
    /// assert!(!n.test_bit(2));
    /// assert!(n.test_bit(3));
    /// ```
    pub fn test_bit(&self, index: u64) -> bool {
        let limb_idx = (index / 64) as usize;
        let bit_idx = index % 64;
        if limb_idx >= self.limbs.len() {
            return false;
        }
        (self.limbs[limb_idx] >> bit_idx) & 1 == 1
    }

    /// Sets bit `index` (LSB-indexed) to 1.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let mut n = BigUint::zero();
    /// n.set_bit(5);
    /// assert_eq!(n, BigUint::from_u64(32));
    /// ```
    pub fn set_bit(&mut self, index: u64) {
        let limb_idx = (index / 64) as usize;
        let bit_idx = index % 64;
        if limb_idx >= self.limbs.len() {
            self.limbs.resize(limb_idx + 1, 0);
        }
        self.limbs[limb_idx] |= 1u64 << bit_idx;
        // Setting a bit can never create a trailing zero, but resize might have
        // introduced trailing zeros if the new limb itself ended up zero (which
        // is impossible here because we just OR'd a non-zero bit into it).
        normalize(&mut self.limbs);
    }

    /// Clears bit `index` (LSB-indexed).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let mut n = BigUint::from_u64(0b1010);
    /// n.clear_bit(1);
    /// assert_eq!(n, BigUint::from_u64(0b1000));
    /// ```
    pub fn clear_bit(&mut self, index: u64) {
        let limb_idx = (index / 64) as usize;
        let bit_idx = index % 64;
        if limb_idx >= self.limbs.len() {
            return;
        }
        self.limbs[limb_idx] &= !(1u64 << bit_idx);
        normalize(&mut self.limbs);
    }

    /// Construct from a big-endian byte sequence (most-significant byte first).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_bytes_be(&[1, 2, 3]);
    /// assert_eq!(n, BigUint::from_u64(0x010203));
    /// ```
    pub fn from_bytes_be(bytes: &[u8]) -> Self {
        // Reverse and delegate
        let mut le = bytes.to_vec();
        le.reverse();
        Self::from_bytes_le(&le)
    }

    /// Construct from a little-endian byte sequence (least-significant byte first).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let n = BigUint::from_bytes_le(&[3, 2, 1]);
    /// assert_eq!(n, BigUint::from_u64(0x010203));
    /// ```
    pub fn from_bytes_le(bytes: &[u8]) -> Self {
        let n_limbs = bytes.len().div_ceil(8);
        let mut limbs = Vec::with_capacity(n_limbs);
        for chunk in bytes.chunks(8) {
            let mut buf = [0u8; 8];
            buf[..chunk.len()].copy_from_slice(chunk);
            limbs.push(u64::from_le_bytes(buf));
        }
        normalize(&mut limbs);
        Self { limbs }
    }

    /// Convert to a big-endian byte sequence. Returns an empty vec for zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(0x010203).to_bytes_be(), vec![1u8, 2, 3]);
    /// assert!(BigUint::zero().to_bytes_be().is_empty());
    /// ```
    pub fn to_bytes_be(&self) -> Vec<u8> {
        let mut le = self.to_bytes_le();
        le.reverse();
        le
    }

    /// Convert to a little-endian byte sequence. Returns an empty vec for zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(0x010203).to_bytes_le(), vec![3u8, 2, 1]);
    /// ```
    pub fn to_bytes_le(&self) -> Vec<u8> {
        if self.limbs.is_empty() {
            return Vec::new();
        }
        let mut out: Vec<u8> = Vec::with_capacity(self.limbs.len() * 8);
        for &limb in &self.limbs {
            out.extend_from_slice(&limb.to_le_bytes());
        }
        // Strip trailing zero bytes from the top.
        while out.last() == Some(&0) {
            out.pop();
        }
        out
    }

    // -----------------------------------------------------------------------
    // Internal helpers (crate-visible) — addition, subtraction, shifts
    // -----------------------------------------------------------------------

    /// Add two `BigUint`s. Always succeeds.
    pub(crate) fn add_ref(a: &BigUint, b: &BigUint) -> BigUint {
        // Ensure `a` is the longer (without cloning if possible).
        let (long, short) = if a.limbs.len() >= b.limbs.len() {
            (a, b)
        } else {
            (b, a)
        };
        let mut out: Vec<u64> = Vec::with_capacity(long.limbs.len() + 1);
        let mut carry: u64 = 0;
        for i in 0..long.limbs.len() {
            let lv = long.limbs[i];
            let sv = if i < short.limbs.len() {
                short.limbs[i]
            } else {
                0
            };
            let (s1, c1) = lv.overflowing_add(sv);
            let (s2, c2) = s1.overflowing_add(carry);
            out.push(s2);
            carry = (c1 as u64) | (c2 as u64);
        }
        if carry != 0 {
            out.push(carry);
        }
        // No trailing zero can appear unless input was already malformed.
        normalize(&mut out);
        BigUint { limbs: out }
    }

    /// Compute `self - other` if `self >= other`; otherwise `None` (no underflow).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let a = BigUint::from_u64(100);
    /// let b = BigUint::from_u64(40);
    /// assert_eq!(a.checked_sub(&b), Some(BigUint::from_u64(60)));
    /// assert_eq!(b.checked_sub(&a), None);
    /// ```
    pub fn checked_sub(&self, other: &BigUint) -> Option<BigUint> {
        if self.cmp(other) == Ordering::Less {
            return None;
        }
        // self >= other => no underflow.
        let mut out: Vec<u64> = Vec::with_capacity(self.limbs.len());
        let mut borrow: u64 = 0;
        for i in 0..self.limbs.len() {
            let av = self.limbs[i];
            let bv = if i < other.limbs.len() {
                other.limbs[i]
            } else {
                0
            };
            let (d1, b1) = av.overflowing_sub(bv);
            let (d2, b2) = d1.overflowing_sub(borrow);
            out.push(d2);
            borrow = (b1 as u64) | (b2 as u64);
        }
        debug_assert_eq!(borrow, 0, "checked_sub underflow despite cmp guard");
        normalize(&mut out);
        Some(BigUint { limbs: out })
    }

    /// Logical shift-left by `n` bits, returning a new `BigUint`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let a = BigUint::from_u64(1);
    /// let shifted = a.shl_bits(65);
    /// assert_eq!(shifted.bit_length(), 66);
    /// ```
    pub fn shl_bits(&self, n: u64) -> BigUint {
        if self.is_zero() || n == 0 {
            return self.clone();
        }
        let limb_offset = (n / 64) as usize;
        let bit_offset = (n % 64) as u32;
        let mut out: Vec<u64> = vec![0u64; limb_offset];
        if bit_offset == 0 {
            out.extend_from_slice(&self.limbs);
        } else {
            let mut carry: u64 = 0;
            for &limb in &self.limbs {
                let lo = (limb << bit_offset) | carry;
                carry = limb >> (64 - bit_offset);
                out.push(lo);
            }
            if carry != 0 {
                out.push(carry);
            }
        }
        normalize(&mut out);
        BigUint { limbs: out }
    }

    /// Raise this value to the `exp` power via binary (square-and-multiply)
    /// exponentiation.
    ///
    /// `self.pow(0) == 1` for every `self` (including zero, matching Rust's
    /// `u64::pow` and the mathematical convention used by `dashu_int::UBig`).
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// assert_eq!(BigUint::from_u64(2).pow(10), BigUint::from_u64(1024));
    /// assert_eq!(BigUint::from_u64(3).pow(0), BigUint::from_u64(1));
    /// assert_eq!(BigUint::zero().pow(0), BigUint::from_u64(1));
    /// ```
    pub fn pow(&self, exp: u32) -> BigUint {
        if exp == 0 {
            return BigUint::one();
        }
        if self.is_zero() {
            return BigUint::zero();
        }
        if self.is_one() {
            return BigUint::one();
        }
        let mut base = self.clone();
        let mut result = BigUint::one();
        let mut e = exp;
        while e > 0 {
            if e & 1 == 1 {
                result = &result * &base;
            }
            e >>= 1;
            if e > 0 {
                base = &base * &base;
            }
        }
        result
    }

    /// Logical shift-right by `n` bits, returning a new `BigUint`.
    ///
    /// # Examples
    ///
    /// ```
    /// use oxinum_int::native::BigUint;
    /// let a = BigUint::from_u64(0b1100);
    /// let shifted = a.shr_bits(2);
    /// assert_eq!(shifted, BigUint::from_u64(0b11));
    /// ```
    pub fn shr_bits(&self, n: u64) -> BigUint {
        if self.is_zero() || n == 0 {
            return self.clone();
        }
        let limb_offset = (n / 64) as usize;
        let bit_offset = (n % 64) as u32;
        if limb_offset >= self.limbs.len() {
            return BigUint::zero();
        }
        let remaining = &self.limbs[limb_offset..];
        let mut out: Vec<u64> = Vec::with_capacity(remaining.len());
        if bit_offset == 0 {
            out.extend_from_slice(remaining);
        } else {
            let mut prev_high: u64 = 0;
            // Process from most-significant to least-significant.
            for i in (0..remaining.len()).rev() {
                let cur = remaining[i];
                let lo = (cur >> bit_offset) | (prev_high << (64 - bit_offset));
                prev_high = cur & ((1u64 << bit_offset) - 1);
                out.push(lo);
            }
            out.reverse();
        }
        normalize(&mut out);
        BigUint { limbs: out }
    }
}

/// Strip trailing-zero limbs. The canonical zero is an empty `Vec`.
#[inline]
pub(crate) fn normalize(limbs: &mut Vec<u64>) {
    while limbs.last() == Some(&0) {
        limbs.pop();
    }
}

// ---------------------------------------------------------------------------
// Equality & ordering
// ---------------------------------------------------------------------------

impl PartialEq for BigUint {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.limbs == other.limbs
    }
}

impl Eq for BigUint {}

impl std::hash::Hash for BigUint {
    #[inline]
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.limbs.hash(state);
    }
}

impl Ord for BigUint {
    fn cmp(&self, other: &Self) -> Ordering {
        // Compare by length first (normalized invariant): longer is greater.
        match self.limbs.len().cmp(&other.limbs.len()) {
            Ordering::Equal => {
                // Same length: compare MSB-first.
                for (a, b) in self.limbs.iter().rev().zip(other.limbs.iter().rev()) {
                    match a.cmp(b) {
                        Ordering::Equal => continue,
                        non_eq => return non_eq,
                    }
                }
                Ordering::Equal
            }
            non_eq => non_eq,
        }
    }
}

impl PartialOrd for BigUint {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

// ---------------------------------------------------------------------------
// Shl/Shr operators (by u64 bit-count) — owned and borrowed
// ---------------------------------------------------------------------------

impl Shl<u64> for BigUint {
    type Output = BigUint;
    #[inline]
    fn shl(self, n: u64) -> BigUint {
        self.shl_bits(n)
    }
}

impl Shl<u64> for &BigUint {
    type Output = BigUint;
    #[inline]
    fn shl(self, n: u64) -> BigUint {
        self.shl_bits(n)
    }
}

impl Shr<u64> for BigUint {
    type Output = BigUint;
    #[inline]
    fn shr(self, n: u64) -> BigUint {
        self.shr_bits(n)
    }
}

impl Shr<u64> for &BigUint {
    type Output = BigUint;
    #[inline]
    fn shr(self, n: u64) -> BigUint {
        self.shr_bits(n)
    }
}

impl ShlAssign<u64> for BigUint {
    #[inline]
    fn shl_assign(&mut self, n: u64) {
        *self = self.shl_bits(n);
    }
}

impl ShrAssign<u64> for BigUint {
    #[inline]
    fn shr_assign(&mut self, n: u64) {
        *self = self.shr_bits(n);
    }
}

// ---------------------------------------------------------------------------
// Bitwise AND, OR, XOR — limb-wise, padding shorter with implicit zeros.
// ---------------------------------------------------------------------------

#[inline]
fn limbwise<F: Fn(u64, u64) -> u64>(a: &BigUint, b: &BigUint, op: F) -> BigUint {
    let n = a.limbs.len().max(b.limbs.len());
    let mut out: Vec<u64> = Vec::with_capacity(n);
    for i in 0..n {
        let av = a.limbs.get(i).copied().unwrap_or(0);
        let bv = b.limbs.get(i).copied().unwrap_or(0);
        out.push(op(av, bv));
    }
    normalize(&mut out);
    BigUint { limbs: out }
}

impl BitAnd<&BigUint> for &BigUint {
    type Output = BigUint;
    fn bitand(self, rhs: &BigUint) -> BigUint {
        // AND result is bounded by the shorter operand; iterate to min.
        let n = self.limbs.len().min(rhs.limbs.len());
        let mut out: Vec<u64> = Vec::with_capacity(n);
        for i in 0..n {
            out.push(self.limbs[i] & rhs.limbs[i]);
        }
        normalize(&mut out);
        BigUint { limbs: out }
    }
}

impl BitAnd<BigUint> for BigUint {
    type Output = BigUint;
    fn bitand(self, rhs: BigUint) -> BigUint {
        (&self).bitand(&rhs)
    }
}

impl BitAnd<&BigUint> for BigUint {
    type Output = BigUint;
    fn bitand(self, rhs: &BigUint) -> BigUint {
        (&self).bitand(rhs)
    }
}

impl BitAnd<BigUint> for &BigUint {
    type Output = BigUint;
    fn bitand(self, rhs: BigUint) -> BigUint {
        self.bitand(&rhs)
    }
}

impl BitOr<&BigUint> for &BigUint {
    type Output = BigUint;
    fn bitor(self, rhs: &BigUint) -> BigUint {
        limbwise(self, rhs, |a, b| a | b)
    }
}

impl BitOr<BigUint> for BigUint {
    type Output = BigUint;
    fn bitor(self, rhs: BigUint) -> BigUint {
        (&self).bitor(&rhs)
    }
}

impl BitOr<&BigUint> for BigUint {
    type Output = BigUint;
    fn bitor(self, rhs: &BigUint) -> BigUint {
        (&self).bitor(rhs)
    }
}

impl BitOr<BigUint> for &BigUint {
    type Output = BigUint;
    fn bitor(self, rhs: BigUint) -> BigUint {
        self.bitor(&rhs)
    }
}

impl BitXor<&BigUint> for &BigUint {
    type Output = BigUint;
    fn bitxor(self, rhs: &BigUint) -> BigUint {
        limbwise(self, rhs, |a, b| a ^ b)
    }
}

impl BitXor<BigUint> for BigUint {
    type Output = BigUint;
    fn bitxor(self, rhs: BigUint) -> BigUint {
        (&self).bitxor(&rhs)
    }
}

impl BitXor<&BigUint> for BigUint {
    type Output = BigUint;
    fn bitxor(self, rhs: &BigUint) -> BigUint {
        (&self).bitxor(rhs)
    }
}

impl BitXor<BigUint> for &BigUint {
    type Output = BigUint;
    fn bitxor(self, rhs: BigUint) -> BigUint {
        self.bitxor(&rhs)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn zero_is_canonical() {
        let z = BigUint::zero();
        assert!(z.is_zero());
        assert_eq!(z.limbs.len(), 0);
        assert_eq!(z.bit_length(), 0);
    }

    #[test]
    fn normalize_strips_trailing_zeros() {
        let n = BigUint::from_le_limbs(&[5, 0, 0]);
        assert_eq!(n.as_limbs(), &[5u64]);
    }

    #[test]
    fn from_u128_high_limb() {
        let n = BigUint::from_u128(u128::MAX);
        assert_eq!(n.as_limbs(), &[u64::MAX, u64::MAX]);
        assert_eq!(n.bit_length(), 128);
    }

    #[test]
    fn add_with_carry_chain() {
        // 0xFF...F + 1 should produce a new limb.
        let a = BigUint::from_u64(u64::MAX);
        let b = BigUint::from_u64(1);
        let s = &a + &b;
        assert_eq!(s.as_limbs(), &[0u64, 1u64]);
    }

    #[test]
    fn checked_sub_underflow_returns_none() {
        let a = BigUint::from_u64(5);
        let b = BigUint::from_u64(10);
        assert!(a.checked_sub(&b).is_none());
    }

    #[test]
    fn checked_sub_basic() {
        let a = BigUint::from_u64(100);
        let b = BigUint::from_u64(40);
        assert_eq!(a.checked_sub(&b), Some(BigUint::from_u64(60)));
    }

    #[test]
    fn checked_sub_borrow_chain() {
        let a = BigUint::from_le_limbs(&[0, 1]); // 2^64
        let b = BigUint::from_u64(1);
        let d = a.checked_sub(&b).expect("non-underflow");
        assert_eq!(d.as_limbs(), &[u64::MAX]);
    }

    #[test]
    fn shl_within_limb() {
        let n = BigUint::from_u64(1);
        assert_eq!(n.shl_bits(5), BigUint::from_u64(32));
    }

    #[test]
    fn shl_crosses_limb_boundary() {
        let n = BigUint::from_u64(1);
        let s = n.shl_bits(64);
        assert_eq!(s.as_limbs(), &[0u64, 1u64]);
    }

    #[test]
    fn shl_by_zero_is_identity() {
        let n = BigUint::from_u64(42);
        assert_eq!(n.shl_bits(0), n);
    }

    #[test]
    fn shr_within_limb() {
        let n = BigUint::from_u64(0b1010_0000);
        assert_eq!(n.shr_bits(4), BigUint::from_u64(0b1010));
    }

    #[test]
    fn shr_crosses_limb_boundary() {
        let n = BigUint::from_le_limbs(&[0, 1]); // 2^64
        let s = n.shr_bits(64);
        assert_eq!(s, BigUint::from_u64(1));
    }

    #[test]
    fn shl_then_shr_is_identity() {
        let n = BigUint::from_u64(0xDEAD_BEEF_CAFE_BABE);
        for k in [0u64, 1, 32, 63, 64, 65, 100, 256] {
            let r = n.shl_bits(k).shr_bits(k);
            assert_eq!(r, n, "shl/shr identity failed for k={k}");
        }
    }

    #[test]
    fn cmp_by_length() {
        let small = BigUint::from_u64(u64::MAX);
        let big = BigUint::from_le_limbs(&[0, 1]); // 2^64 > 2^64 - 1
        assert!(big > small);
    }

    #[test]
    fn cmp_same_length_msb_first() {
        let a = BigUint::from_le_limbs(&[1, 2]);
        let b = BigUint::from_le_limbs(&[100, 1]);
        // [1, 2] has top limb 2 > 1 = top of [100, 1].
        assert!(a > b);
    }

    #[test]
    fn bit_length_basic() {
        assert_eq!(BigUint::zero().bit_length(), 0);
        assert_eq!(BigUint::from_u64(1).bit_length(), 1);
        assert_eq!(BigUint::from_u64(0xFF).bit_length(), 8);
        assert_eq!(BigUint::from_u64(u64::MAX).bit_length(), 64);
    }

    #[test]
    fn trailing_zeros_basic() {
        assert_eq!(BigUint::from_u64(0b1000).trailing_zeros(), 3);
        assert_eq!(BigUint::from_le_limbs(&[0, 1]).trailing_zeros(), 64);
        assert_eq!(BigUint::zero().trailing_zeros(), 0);
    }

    #[test]
    fn count_ones_basic() {
        assert_eq!(BigUint::zero().count_ones(), 0);
        assert_eq!(BigUint::from_u64(0b1011).count_ones(), 3);
        // Multi-limb: top limb has u64 = 0xF (4 bits), low limb has 1 bit set.
        let n = BigUint::from_le_limbs(&[1, 0xF]);
        assert_eq!(n.count_ones(), 5);
    }

    #[test]
    fn test_set_clear_bit() {
        let mut n = BigUint::zero();
        n.set_bit(100);
        assert!(n.test_bit(100));
        assert_eq!(n.bit_length(), 101);
        n.clear_bit(100);
        assert!(n.is_zero());
    }

    #[test]
    fn bytes_roundtrip_le() {
        let n = BigUint::from_u64(0xDEAD_BEEF_CAFE_BABE);
        let bytes = n.to_bytes_le();
        let m = BigUint::from_bytes_le(&bytes);
        assert_eq!(m, n);
    }

    #[test]
    fn bytes_roundtrip_be() {
        let n = BigUint::from_u64(0xDEAD_BEEF_CAFE_BABE);
        let bytes = n.to_bytes_be();
        let m = BigUint::from_bytes_be(&bytes);
        assert_eq!(m, n);
    }

    #[test]
    fn bitand_or_xor() {
        let a = BigUint::from_u64(0b1100);
        let b = BigUint::from_u64(0b1010);
        assert_eq!(&a & &b, BigUint::from_u64(0b1000));
        assert_eq!(&a | &b, BigUint::from_u64(0b1110));
        assert_eq!(&a ^ &b, BigUint::from_u64(0b0110));
    }
}