Skip to main content

crypto_bigint/uint/
add.rs

1//! [`Uint`] addition operations.
2
3use crate::{
4    Add, AddAssign, Checked, CheckedAdd, CtOption, Limb, Uint, Wrapping, WrappingAdd, word,
5};
6
7impl<const LIMBS: usize> Uint<LIMBS> {
8    /// Computes `self + rhs + carry`, returning the result along with the new carry.
9    #[deprecated(since = "0.7.0", note = "please use `carrying_add` instead")]
10    #[must_use]
11    pub const fn adc(&self, rhs: &Self, carry: Limb) -> (Self, Limb) {
12        self.carrying_add(rhs, carry)
13    }
14
15    /// Computes `self + rhs + carry`, returning the result along with the new carry.
16    #[inline(always)]
17    #[must_use]
18    pub const fn carrying_add(&self, rhs: &Self, mut carry: Limb) -> (Self, Limb) {
19        let mut limbs = [Limb::ZERO; LIMBS];
20        let mut i = 0;
21
22        while i < LIMBS {
23            let (w, c) = self.limbs[i].carrying_add(rhs.limbs[i], carry);
24            limbs[i] = w;
25            carry = c;
26            i += 1;
27        }
28
29        (Self { limbs }, carry)
30    }
31
32    /// Perform saturating addition, returning `MAX` on overflow.
33    #[must_use]
34    pub const fn saturating_add(&self, rhs: &Self) -> Self {
35        let (res, overflow) = self.carrying_add(rhs, Limb::ZERO);
36        Self::select(&res, &Self::MAX, word::choice_from_lsb(overflow.0))
37    }
38
39    /// Perform wrapping addition, discarding overflow.
40    #[inline]
41    #[must_use]
42    pub const fn wrapping_add(&self, rhs: &Self) -> Self {
43        self.carrying_add(rhs, Limb::ZERO).0
44    }
45
46    /// Computes `self + rhs + carry`, returning the result along with the new carry.
47    #[inline]
48    pub(crate) const fn overflowing_add_limb(&self, mut carry: Limb) -> (Self, Limb) {
49        let mut limbs = [Limb::ZERO; LIMBS];
50        let mut i = 0;
51
52        while i < LIMBS {
53            (limbs[i], carry) = self.limbs[i].overflowing_add(carry);
54            i += 1;
55        }
56
57        (Self { limbs }, carry)
58    }
59
60    /// Computes `self + rhs`, discarding overflow.
61    #[inline]
62    pub(crate) const fn wrapping_add_limb(&self, rhs: Limb) -> Self {
63        self.overflowing_add_limb(rhs).0
64    }
65}
66
67impl<const LIMBS: usize> Add for Uint<LIMBS> {
68    type Output = Self;
69
70    fn add(self, rhs: Self) -> Self {
71        self.add(&rhs)
72    }
73}
74
75impl<const LIMBS: usize> Add<&Uint<LIMBS>> for Uint<LIMBS> {
76    type Output = Self;
77
78    fn add(self, rhs: &Self) -> Self {
79        self.checked_add(rhs)
80            .expect("attempted to add with overflow")
81    }
82}
83
84impl<const LIMBS: usize> AddAssign for Uint<LIMBS> {
85    fn add_assign(&mut self, other: Self) {
86        *self += &other;
87    }
88}
89
90impl<const LIMBS: usize> AddAssign<&Uint<LIMBS>> for Uint<LIMBS> {
91    fn add_assign(&mut self, other: &Self) {
92        *self = *self + other;
93    }
94}
95
96impl<const LIMBS: usize> AddAssign for Wrapping<Uint<LIMBS>> {
97    fn add_assign(&mut self, other: Self) {
98        *self = *self + other;
99    }
100}
101
102impl<const LIMBS: usize> AddAssign<&Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
103    fn add_assign(&mut self, other: &Self) {
104        *self = *self + other;
105    }
106}
107
108impl<const LIMBS: usize> AddAssign for Checked<Uint<LIMBS>> {
109    fn add_assign(&mut self, other: Self) {
110        *self = *self + other;
111    }
112}
113
114impl<const LIMBS: usize> AddAssign<&Checked<Uint<LIMBS>>> for Checked<Uint<LIMBS>> {
115    fn add_assign(&mut self, other: &Self) {
116        *self = *self + other;
117    }
118}
119
120impl<const LIMBS: usize> CheckedAdd for Uint<LIMBS> {
121    fn checked_add(&self, rhs: &Self) -> CtOption<Self> {
122        let (result, carry) = self.carrying_add(rhs, Limb::ZERO);
123        CtOption::new(result, carry.is_zero())
124    }
125}
126
127impl<const LIMBS: usize> WrappingAdd for Uint<LIMBS> {
128    fn wrapping_add(&self, v: &Self) -> Self {
129        self.wrapping_add(v)
130    }
131}
132
133#[cfg(test)]
134mod tests {
135    use crate::{CheckedAdd, Limb, U128};
136
137    #[test]
138    fn carrying_add_no_carry() {
139        let (res, carry) = U128::ZERO.carrying_add(&U128::ONE, Limb::ZERO);
140        assert_eq!(res, U128::ONE);
141        assert_eq!(carry, Limb::ZERO);
142    }
143
144    #[test]
145    fn carrying_add_with_carry() {
146        let (res, carry) = U128::MAX.carrying_add(&U128::ONE, Limb::ZERO);
147        assert_eq!(res, U128::ZERO);
148        assert_eq!(carry, Limb::ONE);
149    }
150
151    #[test]
152    fn saturating_add_no_carry() {
153        assert_eq!(U128::ZERO.saturating_add(&U128::ONE), U128::ONE);
154    }
155
156    #[test]
157    fn saturating_add_with_carry() {
158        assert_eq!(U128::MAX.saturating_add(&U128::ONE), U128::MAX);
159    }
160
161    #[test]
162    fn wrapping_add_no_carry() {
163        assert_eq!(U128::ZERO.wrapping_add(&U128::ONE), U128::ONE);
164    }
165
166    #[test]
167    fn wrapping_add_with_carry() {
168        assert_eq!(U128::MAX.wrapping_add(&U128::ONE), U128::ZERO);
169    }
170
171    #[test]
172    fn checked_add_ok() {
173        let result = U128::ZERO.checked_add(&U128::ONE);
174        assert_eq!(result.unwrap(), U128::ONE);
175    }
176
177    #[test]
178    fn checked_add_overflow() {
179        let result = U128::MAX.checked_add(&U128::ONE);
180        assert!(!bool::from(result.is_some()));
181    }
182
183    #[test]
184    fn overflowing_add_limb() {
185        let result = U128::ZERO.overflowing_add_limb(Limb::ZERO);
186        assert_eq!(result, (U128::ZERO, Limb::ZERO));
187        let result = U128::ZERO.overflowing_add_limb(Limb::ONE);
188        assert_eq!(result, (U128::ONE, Limb::ZERO));
189        let result = U128::MAX.overflowing_add_limb(Limb::ZERO);
190        assert_eq!(result, (U128::MAX, Limb::ZERO));
191        let result = U128::MAX.overflowing_add_limb(Limb::ONE);
192        assert_eq!(result, (U128::ZERO, Limb::ONE));
193
194        assert_eq!(U128::ZERO.wrapping_add_limb(Limb::ZERO), U128::ZERO);
195        assert_eq!(U128::ZERO.wrapping_add_limb(Limb::ONE), U128::ONE);
196        assert_eq!(U128::MAX.wrapping_add_limb(Limb::ZERO), U128::MAX);
197        assert_eq!(U128::MAX.wrapping_add_limb(Limb::ONE), U128::ZERO);
198    }
199}