fixed-bigint 0.4.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
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
use super::{
    FixedUInt, MachineWord, PanicReason, add_impl, const_ct_select, maybe_panic_if, sub_impl,
};
use crate::machineword::ConstMachineWord;
use const_num_traits::ops::ct::{CtCheckedAdd, CtCheckedSub};
use const_num_traits::{
    Bounded, CheckedAdd, CheckedSub, ConstZero, OverflowingAdd, OverflowingSub, Personality,
    PersonalityTag, SaturatingAdd, SaturatingSub, WrappingAdd, WrappingSub,
};

c0nst::c0nst! {
    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> OverflowingAdd for FixedUInt<T, N, P> {
        fn overflowing_add(self, other: Self) -> (Self, bool) {
            let mut ret = self;
            let overflow = add_impl(&mut ret.array, &other.array);
            (ret, overflow)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> OverflowingSub for FixedUInt<T, N, P> {
        fn overflowing_sub(self, other: Self) -> (Self, bool) {
            let mut ret = self;
            let overflow = sub_impl(&mut ret.array, &other.array);
            (ret, overflow)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> WrappingAdd for FixedUInt<T, N, P> {
        fn wrapping_add(self, other: Self) -> Self {
            <Self as OverflowingAdd>::overflowing_add(self, other).0
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> WrappingSub for FixedUInt<T, N, P> {
        fn wrapping_sub(self, other: Self) -> Self {
            <Self as OverflowingSub>::overflowing_sub(self, other).0
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> CheckedAdd for FixedUInt<T, N, P> {
        fn checked_add(self, other: Self) -> Option<Self> {
            let (res, overflow) = <Self as OverflowingAdd>::overflowing_add(self, other);
            if overflow { None } else { Some(res) }
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> CheckedSub for FixedUInt<T, N, P> {
        fn checked_sub(self, other: Self) -> Option<Self> {
            let (res, overflow) = <Self as OverflowingSub>::overflowing_sub(self, other);
            if overflow { None } else { Some(res) }
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> SaturatingAdd for FixedUInt<T, N, P> {
        fn saturating_add(self, other: Self) -> Self {
            let (res, overflow) = <Self as OverflowingAdd>::overflowing_add(self, other);
            match P::TAG {
                PersonalityTag::Nct => if overflow { <Self as Bounded>::max_value() } else { res },
                PersonalityTag::Ct => const_ct_select(res, <Self as Bounded>::max_value(), overflow as u8),
            }
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> SaturatingSub for FixedUInt<T, N, P> {
        fn saturating_sub(self, other: Self) -> Self {
            let (res, overflow) = <Self as OverflowingSub>::overflowing_sub(self, other);
            match P::TAG {
                PersonalityTag::Nct => if overflow { <Self as ConstZero>::ZERO } else { res },
                PersonalityTag::Ct => const_ct_select(res, <Self as ConstZero>::ZERO, overflow as u8),
            }
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Add for FixedUInt<T, N, P> {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let (res, overflow) = <Self as OverflowingAdd>::overflowing_add(self, other);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Sub for FixedUInt<T, N, P> {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let (res, overflow) = <Self as OverflowingSub>::overflowing_sub(self, other);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Add<&'_ Self> for FixedUInt<T, N, P> {
        type Output = Self;
        fn add(self, other: &Self) -> Self {
            let (res, overflow) = <Self as OverflowingAdd>::overflowing_add(self, *other);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Add<FixedUInt<T, N, P>> for &FixedUInt<T, N, P> {
        type Output = FixedUInt<T, N, P>;
        fn add(self, other: FixedUInt<T, N, P>) -> Self::Output {
            let (res, overflow) = <FixedUInt<T, N, P> as OverflowingAdd>::overflowing_add(*self, other);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Add<Self> for &FixedUInt<T, N, P> {
        type Output = FixedUInt<T, N, P>;
        fn add(self, other: Self) -> Self::Output {
            let (res, overflow) = <FixedUInt<T, N, P> as OverflowingAdd>::overflowing_add(*self, *other);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Sub<&'_ Self> for FixedUInt<T, N, P> {
        type Output = Self;
        fn sub(self, other: &Self) -> Self {
            let (res, overflow) = <Self as OverflowingSub>::overflowing_sub(self, *other);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Sub<FixedUInt<T, N, P>> for &FixedUInt<T, N, P> {
        type Output = FixedUInt<T, N, P>;
        fn sub(self, other: FixedUInt<T, N, P>) -> Self::Output {
            let (res, overflow) = <FixedUInt<T, N, P> as OverflowingSub>::overflowing_sub(*self, other);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::Sub<Self> for &FixedUInt<T, N, P> {
        type Output = FixedUInt<T, N, P>;
        fn sub(self, other: Self) -> Self::Output {
            let (res, overflow) = <FixedUInt<T, N, P> as OverflowingSub>::overflowing_sub(*self, *other);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            res
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::AddAssign<Self> for FixedUInt<T, N, P> {
        fn add_assign(&mut self, other: Self) {
            let mut array = self.array;
            let overflow = add_impl(&mut array, &other.array);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            self.array = array;
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::AddAssign<&'_ Self> for FixedUInt<T, N, P> {
        fn add_assign(&mut self, other: &Self) {
            let mut array = self.array;
            let overflow = add_impl(&mut array, &other.array);
            maybe_panic_if::<P>(overflow, PanicReason::Add);
            self.array = array;
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::SubAssign<Self> for FixedUInt<T, N, P> {
        fn sub_assign(&mut self, other: Self) {
            let mut array = self.array;
            let overflow = sub_impl(&mut array, &other.array);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            self.array = array;
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> core::ops::SubAssign<&'_ Self> for FixedUInt<T, N, P> {
        fn sub_assign(&mut self, other: &Self) {
            let mut array = self.array;
            let overflow = sub_impl(&mut array, &other.array);
            maybe_panic_if::<P>(overflow, PanicReason::Sub);
            self.array = array;
        }
    }

    // --- Reference-receiver impls ------------------------------------------
    //
    // Mirror the by-value trait surface for `&Self` via the trait's
    // `type Output` — for Copy carriers like FixedUInt the body just
    // dereferences and delegates. Call sites can write
    // `(&x).checked_add(&y)` (or use a `T: CheckedAdd` bound where T is
    // `&FixedUInt`) without sprinkling derefs. The `Add<&FixedUInt> for
    // &FixedUInt` impls above supply `Output = FixedUInt<T,N,P>`.

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> OverflowingAdd for &FixedUInt<T, N, P> {
        fn overflowing_add(self, other: Self) -> (FixedUInt<T, N, P>, bool) {
            <FixedUInt<T, N, P> as OverflowingAdd>::overflowing_add(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> OverflowingSub for &FixedUInt<T, N, P> {
        fn overflowing_sub(self, other: Self) -> (FixedUInt<T, N, P>, bool) {
            <FixedUInt<T, N, P> as OverflowingSub>::overflowing_sub(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> WrappingAdd for &FixedUInt<T, N, P> {
        fn wrapping_add(self, other: Self) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as WrappingAdd>::wrapping_add(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> WrappingSub for &FixedUInt<T, N, P> {
        fn wrapping_sub(self, other: Self) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as WrappingSub>::wrapping_sub(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> CheckedAdd for &FixedUInt<T, N, P> {
        fn checked_add(self, other: Self) -> Option<FixedUInt<T, N, P>> {
            <FixedUInt<T, N, P> as CheckedAdd>::checked_add(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> CheckedSub for &FixedUInt<T, N, P> {
        fn checked_sub(self, other: Self) -> Option<FixedUInt<T, N, P>> {
            <FixedUInt<T, N, P> as CheckedSub>::checked_sub(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> SaturatingAdd for &FixedUInt<T, N, P> {
        fn saturating_add(self, other: Self) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as SaturatingAdd>::saturating_add(*self, *other)
        }
    }

    c0nst impl<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality> SaturatingSub for &FixedUInt<T, N, P> {
        fn saturating_sub(self, other: Self) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as SaturatingSub>::saturating_sub(*self, *other)
        }
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::ops::overflowing::OverflowingAdd
    for FixedUInt<T, N, P>
{
    fn overflowing_add(&self, other: &Self) -> (Self, bool) {
        <Self as OverflowingAdd>::overflowing_add(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::WrappingAdd
    for FixedUInt<T, N, P>
{
    fn wrapping_add(&self, other: &Self) -> Self {
        <Self as WrappingAdd>::wrapping_add(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::CheckedAdd for FixedUInt<T, N, P> {
    fn checked_add(&self, other: &Self) -> Option<Self> {
        <Self as CheckedAdd>::checked_add(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::ops::saturating::SaturatingAdd
    for FixedUInt<T, N, P>
{
    fn saturating_add(&self, other: &Self) -> Self {
        <Self as SaturatingAdd>::saturating_add(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::ops::overflowing::OverflowingSub
    for FixedUInt<T, N, P>
{
    fn overflowing_sub(&self, other: &Self) -> (Self, bool) {
        <Self as OverflowingSub>::overflowing_sub(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::WrappingSub
    for FixedUInt<T, N, P>
{
    fn wrapping_sub(&self, other: &Self) -> Self {
        <Self as WrappingSub>::wrapping_sub(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::CheckedSub for FixedUInt<T, N, P> {
    fn checked_sub(&self, other: &Self) -> Option<Self> {
        <Self as CheckedSub>::checked_sub(*self, *other)
    }
}

#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::ops::saturating::SaturatingSub
    for FixedUInt<T, N, P>
{
    fn saturating_sub(&self, other: &Self) -> Self {
        <Self as SaturatingSub>::saturating_sub(*self, *other)
    }
}

/// Note: This is marked deprecated, but still used by PrimInt
#[cfg(feature = "num-traits")]
impl<T: MachineWord, const N: usize, P: Personality> num_traits::Saturating for FixedUInt<T, N, P> {
    fn saturating_add(self, other: Self) -> Self {
        <Self as SaturatingAdd>::saturating_add(self, other)
    }

    fn saturating_sub(self, other: Self) -> Self {
        <Self as SaturatingSub>::saturating_sub(self, other)
    }
}

// ── CtCheckedAdd / CtCheckedSub (masked-return checked arithmetic) ───
//
// Delegate to the existing `OverflowingAdd`/`OverflowingSub` impls (which
// already cover both personalities branchlessly via `add_impl`/`sub_impl`),
// then wrap the `(Self, bool)` into a `CtOption<Self>` by converting the
// overflow flag to a `Choice`. Same shape upstream uses for the primitives.
impl<T: MachineWord, const N: usize, P: Personality> CtCheckedAdd for FixedUInt<T, N, P> {
    fn ct_checked_add(&self, v: &Self) -> subtle::CtOption<Self> {
        let (val, overflow) = <Self as OverflowingAdd>::overflowing_add(*self, *v);
        subtle::CtOption::new(val, subtle::Choice::from(!overflow as u8))
    }
}

impl<T: MachineWord, const N: usize, P: Personality> CtCheckedSub for FixedUInt<T, N, P> {
    fn ct_checked_sub(&self, v: &Self) -> subtle::CtOption<Self> {
        let (val, overflow) = <Self as OverflowingSub>::overflowing_sub(*self, *v);
        subtle::CtOption::new(val, subtle::Choice::from(!overflow as u8))
    }
}

#[cfg(test)]
// Coverage tests deliberately exercise every ref/value combination of
// `Add`/`Sub` (see `test_add_combinations`, `test_sub_combinations`).
#[allow(clippy::op_ref)]
mod tests {
    use super::*;
    use crate::machineword::ConstMachineWord;
    use const_num_traits::Bounded;
    use const_num_traits::{
        CheckedAdd, CheckedSub, OverflowingAdd, OverflowingSub, WrappingAdd, WrappingSub,
    };

    c0nst::c0nst! {
        /// Test wrapper for OverflowingAdd
        pub c0nst fn const_overflowing_add<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> (FixedUInt<T, N, P>, bool) {
            <FixedUInt<T, N, P> as OverflowingAdd>::overflowing_add(*a, *b)
        }

        /// Test wrapper for OverflowingSub
        pub c0nst fn const_overflowing_sub<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> (FixedUInt<T, N, P>, bool) {
            <FixedUInt<T, N, P> as OverflowingSub>::overflowing_sub(*a, *b)
        }

        /// Test wrapper for const Add
        pub c0nst fn const_add<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: FixedUInt<T, N, P>,
            b: FixedUInt<T, N, P>
        ) -> FixedUInt<T, N, P> {
            a + b
        }

        /// Test wrapper for const Sub
        pub c0nst fn const_sub<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: FixedUInt<T, N, P>,
            b: FixedUInt<T, N, P>
        ) -> FixedUInt<T, N, P> {
            a - b
        }

        /// Test wrapper for WrappingAdd
        pub c0nst fn const_wrapping_add<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as WrappingAdd>::wrapping_add(*a, *b)
        }

        /// Test wrapper for WrappingSub
        pub c0nst fn const_wrapping_sub<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> FixedUInt<T, N, P> {
            <FixedUInt<T, N, P> as WrappingSub>::wrapping_sub(*a, *b)
        }

        /// Test wrapper for CheckedAdd
        pub c0nst fn const_checked_add<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> Option<FixedUInt<T, N, P>> {
            <FixedUInt<T, N, P> as CheckedAdd>::checked_add(*a, *b)
        }

        /// Test wrapper for CheckedSub
        pub c0nst fn const_checked_sub<T: [c0nst] ConstMachineWord + MachineWord, const N: usize, P: Personality>(
            a: &FixedUInt<T, N, P>,
            b: &FixedUInt<T, N, P>
        ) -> Option<FixedUInt<T, N, P>> {
            <FixedUInt<T, N, P> as CheckedSub>::checked_sub(*a, *b)
        }
    }

    #[test]
    fn test_add_combinations() {
        let a = FixedUInt::<u8, 2>::from(12u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(15u8);

        // value + value
        assert_eq!(a + b, expected);
        // value + ref
        assert_eq!(a + &b, expected);
        // ref + value
        assert_eq!(&a + b, expected);
        // ref + ref
        assert_eq!(&a + &b, expected);
    }

    #[test]
    fn test_sub_combinations() {
        let a = FixedUInt::<u8, 2>::from(15u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let expected = FixedUInt::<u8, 2>::from(12u8);

        // value - value
        assert_eq!(a - b, expected);
        // value - ref
        assert_eq!(a - &b, expected);
        // ref - value
        assert_eq!(&a - b, expected);
        // ref - ref
        assert_eq!(&a - &b, expected);
    }

    #[test]
    fn test_const_overflowing_add() {
        // No overflow
        let a = FixedUInt::<u8, 2>::from(12u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let (result, overflow) = const_overflowing_add(&a, &b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(15u8));
        assert!(!overflow);

        // With overflow: max + max wraps to max-1 with overflow
        let a = <FixedUInt<u8, 2> as Bounded>::max_value();
        let b = <FixedUInt<u8, 2> as Bounded>::max_value();
        let (result, overflow) = const_overflowing_add(&a, &b);
        // 0xFFFF + 0xFFFF = 0x1FFFE, which wraps to 0xFFFE with overflow
        assert_eq!(result, FixedUInt::<u8, 2>::from(u16::MAX - 1));
        assert!(overflow);

        // Max value overflow
        let max = <FixedUInt<u8, 2> as Bounded>::max_value();
        let one = FixedUInt::<u8, 2>::from(1u8);
        let (_, overflow) = const_overflowing_add(&max, &one);
        assert!(overflow);

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt::from_array([12, 0]);
            const B: FixedUInt<u8, 2> = FixedUInt::from_array([3, 0]);
            const RESULT: (FixedUInt<u8, 2>, bool) = const_overflowing_add(&A, &B);
            assert_eq!(RESULT.0.array, [15, 0]);
            assert!(!RESULT.1);
        }
    }

    #[test]
    fn test_const_overflowing_sub() {
        // No overflow
        let a = FixedUInt::<u8, 2>::from(15u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let (result, overflow) = const_overflowing_sub(&a, &b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(12u8));
        assert!(!overflow);

        // With underflow
        let a = FixedUInt::<u8, 2>::from(0u8);
        let b = FixedUInt::<u8, 2>::from(1u8);
        let (_, overflow) = const_overflowing_sub(&a, &b);
        assert!(overflow);

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt::from_array([15, 0]);
            const B: FixedUInt<u8, 2> = FixedUInt::from_array([3, 0]);
            const RESULT: (FixedUInt<u8, 2>, bool) = const_overflowing_sub(&A, &B);
            assert_eq!(RESULT.0.array, [12, 0]);
            assert!(!RESULT.1);
        }
    }

    #[test]
    fn test_const_add_op() {
        let a = FixedUInt::<u8, 2>::from(12u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let result = const_add(a, b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(15u8));

        // Test with u32 word type
        let a = FixedUInt::<u32, 2>::from(100u32);
        let b = FixedUInt::<u32, 2>::from(200u32);
        let result = const_add(a, b);
        assert_eq!(result, FixedUInt::<u32, 2>::from(300u32));

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt::from_array([12, 0]);
            const B: FixedUInt<u8, 2> = FixedUInt::from_array([3, 0]);
            const RESULT: FixedUInt<u8, 2> = const_add(A, B);
            assert_eq!(RESULT.array, [15, 0]);
        }
    }

    #[test]
    fn test_const_sub_op() {
        let a = FixedUInt::<u8, 2>::from(15u8);
        let b = FixedUInt::<u8, 2>::from(3u8);
        let result = const_sub(a, b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(12u8));

        // Test with u32 word type
        let a = FixedUInt::<u32, 2>::from(300u32);
        let b = FixedUInt::<u32, 2>::from(100u32);
        let result = const_sub(a, b);
        assert_eq!(result, FixedUInt::<u32, 2>::from(200u32));

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt::from_array([15, 0]);
            const B: FixedUInt<u8, 2> = FixedUInt::from_array([3, 0]);
            const RESULT: FixedUInt<u8, 2> = const_sub(A, B);
            assert_eq!(RESULT.array, [12, 0]);
        }
    }

    #[test]
    fn test_const_wrapping_checked() {
        // Test wrapping_add without overflow
        let a = FixedUInt::<u8, 2>::from(100u8);
        let b = FixedUInt::<u8, 2>::from(50u8);
        let result = const_wrapping_add(&a, &b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(150u8));

        // Test wrapping_add with overflow
        let max = <FixedUInt<u8, 2> as Bounded>::max_value();
        let one = FixedUInt::<u8, 2>::from(1u8);
        let result = const_wrapping_add(&max, &one);
        assert_eq!(result, FixedUInt::<u8, 2>::from(0u8));

        // Test wrapping_sub without overflow
        let a = FixedUInt::<u8, 2>::from(100u8);
        let b = FixedUInt::<u8, 2>::from(50u8);
        let result = const_wrapping_sub(&a, &b);
        assert_eq!(result, FixedUInt::<u8, 2>::from(50u8));

        // Test wrapping_sub with underflow
        let zero = FixedUInt::<u8, 2>::from(0u8);
        let one = FixedUInt::<u8, 2>::from(1u8);
        let result = const_wrapping_sub(&zero, &one);
        assert_eq!(result, <FixedUInt::<u8, 2> as Bounded>::max_value());

        // Test checked_add without overflow
        let a = FixedUInt::<u8, 2>::from(100u8);
        let b = FixedUInt::<u8, 2>::from(50u8);
        let result = const_checked_add(&a, &b);
        assert_eq!(result, Some(FixedUInt::<u8, 2>::from(150u8)));

        // Test checked_add with overflow
        let max = <FixedUInt<u8, 2> as Bounded>::max_value();
        let one = FixedUInt::<u8, 2>::from(1u8);
        let result = const_checked_add(&max, &one);
        assert_eq!(result, None);

        // Test checked_sub without overflow
        let a = FixedUInt::<u8, 2>::from(100u8);
        let b = FixedUInt::<u8, 2>::from(50u8);
        let result = const_checked_sub(&a, &b);
        assert_eq!(result, Some(FixedUInt::<u8, 2>::from(50u8)));

        // Test checked_sub with underflow
        let zero = FixedUInt::<u8, 2>::from(0u8);
        let one = FixedUInt::<u8, 2>::from(1u8);
        let result = const_checked_sub(&zero, &one);
        assert_eq!(result, None);

        #[cfg(feature = "nightly")]
        {
            const A: FixedUInt<u8, 2> = FixedUInt::from_array([100, 0]);
            const B: FixedUInt<u8, 2> = FixedUInt::from_array([50, 0]);

            const WRAP_ADD: FixedUInt<u8, 2> = const_wrapping_add(&A, &B);
            const WRAP_SUB: FixedUInt<u8, 2> = const_wrapping_sub(&A, &B);
            const CHECK_ADD: Option<FixedUInt<u8, 2>> = const_checked_add(&A, &B);
            const CHECK_SUB: Option<FixedUInt<u8, 2>> = const_checked_sub(&A, &B);

            assert_eq!(WRAP_ADD.array, [150, 0]);
            assert_eq!(WRAP_SUB.array, [50, 0]);
            assert!(CHECK_ADD.is_some());
            assert!(CHECK_SUB.is_some());

            const MAX: FixedUInt<u8, 2> = FixedUInt::from_array([255, 255]);
            const ONE: FixedUInt<u8, 2> = FixedUInt::from_array([1, 0]);
            const CHECK_ADD_OVERFLOW: Option<FixedUInt<u8, 2>> = const_checked_add(&MAX, &ONE);
            assert!(CHECK_ADD_OVERFLOW.is_none());
        }
    }

    #[test]
    fn ct_checked_add_masks_overflow() {
        use CtCheckedAdd;
        type U16 = FixedUInt<u8, 2>;
        let a = U16::from(100u8);
        let b = U16::from(50u8);
        let ok = a.ct_checked_add(&b);
        assert!(bool::from(ok.is_some()));
        assert_eq!(ok.unwrap(), U16::from(150u8));
        // Overflow case: MAX + 1
        let max = <U16 as const_num_traits::Bounded>::max_value();
        let one = U16::from(1u8);
        let bad = max.ct_checked_add(&one);
        assert!(!bool::from(bad.is_some()));
    }

    #[test]
    fn ct_checked_sub_masks_underflow() {
        use CtCheckedSub;
        type U16 = FixedUInt<u8, 2>;
        let ok = U16::from(100u8).ct_checked_sub(&U16::from(50u8));
        assert!(bool::from(ok.is_some()));
        assert_eq!(ok.unwrap(), U16::from(50u8));
        // Underflow: 0 - 1
        let bad = U16::from(0u8).ct_checked_sub(&U16::from(1u8));
        assert!(!bool::from(bad.is_some()));
    }
}