Skip to main content

crypto_bigint/uint/
add.rs

1//! [`UInt`] addition operations.
2
3use super::UInt;
4use crate::{Limb, Wrapping};
5use core::ops::{Add, AddAssign};
6use subtle::CtOption;
7
8impl<const LIMBS: usize> UInt<LIMBS> {
9    /// Computes `a + b + carry`, returning the result along with the new carry.
10    #[inline(always)]
11    pub const fn adc(&self, rhs: &Self, mut carry: Limb) -> (Self, Limb) {
12        let mut limbs = [Limb::ZERO; LIMBS];
13        let mut i = 0;
14
15        while i < LIMBS {
16            let (w, c) = self.limbs[i].adc(rhs.limbs[i], carry);
17            limbs[i] = w;
18            carry = c;
19            i += 1;
20        }
21
22        (Self { limbs }, carry)
23    }
24
25    /// Perform wrapping addition, discarding overflow.
26    pub const fn wrapping_add(&self, rhs: &Self) -> Self {
27        self.adc(rhs, Limb::ZERO).0
28    }
29
30    /// Perform checked addition, returning a [`CtOption`] which `is_some` only
31    /// if the operation did not overflow.
32    pub fn checked_add(&self, rhs: &Self) -> CtOption<Self> {
33        let (result, carry) = self.adc(rhs, Limb::ZERO);
34        CtOption::new(result, carry.is_zero())
35    }
36}
37
38impl<const LIMBS: usize> Add for Wrapping<UInt<LIMBS>> {
39    type Output = Self;
40
41    fn add(self, rhs: Self) -> Wrapping<UInt<LIMBS>> {
42        Wrapping(self.0.wrapping_add(&rhs.0))
43    }
44}
45
46impl<const LIMBS: usize> Add<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
47    type Output = Wrapping<UInt<LIMBS>>;
48
49    fn add(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
50        Wrapping(self.0.wrapping_add(&rhs.0))
51    }
52}
53
54impl<const LIMBS: usize> Add<Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
55    type Output = Wrapping<UInt<LIMBS>>;
56
57    fn add(self, rhs: Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
58        Wrapping(self.0.wrapping_add(&rhs.0))
59    }
60}
61
62impl<const LIMBS: usize> Add<&Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
63    type Output = Wrapping<UInt<LIMBS>>;
64
65    fn add(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
66        Wrapping(self.0.wrapping_add(&rhs.0))
67    }
68}
69
70impl<const LIMBS: usize> AddAssign for Wrapping<UInt<LIMBS>> {
71    fn add_assign(&mut self, other: Self) {
72        *self = *self + other;
73    }
74}
75
76impl<const LIMBS: usize> AddAssign<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
77    fn add_assign(&mut self, other: &Self) {
78        *self = *self + other;
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use crate::{Limb, U128};
85
86    #[test]
87    fn adc_no_carry() {
88        let (res, carry) = U128::ZERO.adc(&U128::ONE, Limb::ZERO);
89        assert_eq!(res, U128::ONE);
90        assert_eq!(carry, Limb::ZERO);
91    }
92
93    #[test]
94    fn adc_with_carry() {
95        let (res, carry) = U128::MAX.adc(&U128::ONE, Limb::ZERO);
96        assert_eq!(res, U128::ZERO);
97        assert_eq!(carry, Limb::ONE);
98    }
99
100    #[test]
101    fn wrapping_add_no_carry() {
102        assert_eq!(U128::ZERO.wrapping_add(&U128::ONE), U128::ONE);
103    }
104
105    #[test]
106    fn wrapping_add_with_carry() {
107        assert_eq!(U128::MAX.wrapping_add(&U128::ONE), U128::ZERO);
108    }
109
110    #[test]
111    fn checked_add_ok() {
112        let result = U128::ZERO.checked_add(&U128::ONE);
113        assert_eq!(result.unwrap(), U128::ONE);
114    }
115
116    #[test]
117    fn checked_add_overflow() {
118        let result = U128::MAX.checked_add(&U128::ONE);
119        assert!(!bool::from(result.is_some()));
120    }
121}