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    #[must_use]
41    pub const fn wrapping_add(&self, rhs: &Self) -> Self {
42        self.carrying_add(rhs, Limb::ZERO).0
43    }
44
45    /// Computes `self + rhs + carry`, returning the result along with the new carry.
46    pub(crate) const fn overflowing_add_limb(&self, mut carry: Limb) -> (Self, Limb) {
47        let mut limbs = [Limb::ZERO; LIMBS];
48        let mut i = 0;
49
50        while i < LIMBS {
51            (limbs[i], carry) = self.limbs[i].overflowing_add(carry);
52            i += 1;
53        }
54
55        (Self { limbs }, carry)
56    }
57
58    /// Computes `self + rhs`, discarding overflow.
59    pub(crate) const fn wrapping_add_limb(&self, rhs: Limb) -> Self {
60        self.overflowing_add_limb(rhs).0
61    }
62}
63
64impl<const LIMBS: usize> Add for Uint<LIMBS> {
65    type Output = Self;
66
67    fn add(self, rhs: Self) -> Self {
68        self.add(&rhs)
69    }
70}
71
72impl<const LIMBS: usize> Add<&Uint<LIMBS>> for Uint<LIMBS> {
73    type Output = Self;
74
75    fn add(self, rhs: &Self) -> Self {
76        self.checked_add(rhs)
77            .expect("attempted to add with overflow")
78    }
79}
80
81impl<const LIMBS: usize> AddAssign for Uint<LIMBS> {
82    fn add_assign(&mut self, other: Self) {
83        *self += &other;
84    }
85}
86
87impl<const LIMBS: usize> AddAssign<&Uint<LIMBS>> for Uint<LIMBS> {
88    fn add_assign(&mut self, other: &Self) {
89        *self = *self + other;
90    }
91}
92
93impl<const LIMBS: usize> AddAssign for Wrapping<Uint<LIMBS>> {
94    fn add_assign(&mut self, other: Self) {
95        *self = *self + other;
96    }
97}
98
99impl<const LIMBS: usize> AddAssign<&Wrapping<Uint<LIMBS>>> for Wrapping<Uint<LIMBS>> {
100    fn add_assign(&mut self, other: &Self) {
101        *self = *self + other;
102    }
103}
104
105impl<const LIMBS: usize> AddAssign for Checked<Uint<LIMBS>> {
106    fn add_assign(&mut self, other: Self) {
107        *self = *self + other;
108    }
109}
110
111impl<const LIMBS: usize> AddAssign<&Checked<Uint<LIMBS>>> for Checked<Uint<LIMBS>> {
112    fn add_assign(&mut self, other: &Self) {
113        *self = *self + other;
114    }
115}
116
117impl<const LIMBS: usize> CheckedAdd for Uint<LIMBS> {
118    fn checked_add(&self, rhs: &Self) -> CtOption<Self> {
119        let (result, carry) = self.carrying_add(rhs, Limb::ZERO);
120        CtOption::new(result, carry.is_zero())
121    }
122}
123
124impl<const LIMBS: usize> WrappingAdd for Uint<LIMBS> {
125    fn wrapping_add(&self, v: &Self) -> Self {
126        self.wrapping_add(v)
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use crate::{CheckedAdd, Limb, U128};
133
134    #[test]
135    fn carrying_add_no_carry() {
136        let (res, carry) = U128::ZERO.carrying_add(&U128::ONE, Limb::ZERO);
137        assert_eq!(res, U128::ONE);
138        assert_eq!(carry, Limb::ZERO);
139    }
140
141    #[test]
142    fn carrying_add_with_carry() {
143        let (res, carry) = U128::MAX.carrying_add(&U128::ONE, Limb::ZERO);
144        assert_eq!(res, U128::ZERO);
145        assert_eq!(carry, Limb::ONE);
146    }
147
148    #[test]
149    fn saturating_add_no_carry() {
150        assert_eq!(U128::ZERO.saturating_add(&U128::ONE), U128::ONE);
151    }
152
153    #[test]
154    fn saturating_add_with_carry() {
155        assert_eq!(U128::MAX.saturating_add(&U128::ONE), U128::MAX);
156    }
157
158    #[test]
159    fn wrapping_add_no_carry() {
160        assert_eq!(U128::ZERO.wrapping_add(&U128::ONE), U128::ONE);
161    }
162
163    #[test]
164    fn wrapping_add_with_carry() {
165        assert_eq!(U128::MAX.wrapping_add(&U128::ONE), U128::ZERO);
166    }
167
168    #[test]
169    fn checked_add_ok() {
170        let result = U128::ZERO.checked_add(&U128::ONE);
171        assert_eq!(result.unwrap(), U128::ONE);
172    }
173
174    #[test]
175    fn checked_add_overflow() {
176        let result = U128::MAX.checked_add(&U128::ONE);
177        assert!(!bool::from(result.is_some()));
178    }
179
180    #[test]
181    fn overflowing_add_limb() {
182        let result = U128::ZERO.overflowing_add_limb(Limb::ZERO);
183        assert_eq!(result, (U128::ZERO, Limb::ZERO));
184        let result = U128::ZERO.overflowing_add_limb(Limb::ONE);
185        assert_eq!(result, (U128::ONE, Limb::ZERO));
186        let result = U128::MAX.overflowing_add_limb(Limb::ZERO);
187        assert_eq!(result, (U128::MAX, Limb::ZERO));
188        let result = U128::MAX.overflowing_add_limb(Limb::ONE);
189        assert_eq!(result, (U128::ZERO, Limb::ONE));
190
191        assert_eq!(U128::ZERO.wrapping_add_limb(Limb::ZERO), U128::ZERO);
192        assert_eq!(U128::ZERO.wrapping_add_limb(Limb::ONE), U128::ONE);
193        assert_eq!(U128::MAX.wrapping_add_limb(Limb::ZERO), U128::MAX);
194        assert_eq!(U128::MAX.wrapping_add_limb(Limb::ONE), U128::ZERO);
195    }
196}