Skip to main content

crypto_bigint/uint/ref_type/
add.rs

1use super::UintRef;
2use crate::{Choice, Limb};
3
4impl UintRef {
5    /// Perform an in-place carrying add of a limb, returning the carried limb value.
6    #[inline]
7    #[track_caller]
8    pub const fn add_assign_limb(&mut self, mut rhs: Limb) -> Limb {
9        let mut i = 0;
10        while i < self.limbs.len() {
11            (self.limbs[i], rhs) = self.limbs[i].overflowing_add(rhs);
12            i += 1;
13        }
14        rhs
15    }
16
17    /// Perform an in-place carrying add of another [`UintRef`], returning the carried limb value.
18    #[inline]
19    #[track_caller]
20    pub const fn carrying_add_assign(&mut self, rhs: &Self, carry: Limb) -> Limb {
21        self.carrying_add_assign_slice(&rhs.limbs, carry)
22    }
23
24    /// Perform an in-place carrying add of another limb slice, returning the carried limb value.
25    ///
26    /// # Panics
27    /// If `self` and `rhs` have different lengths.
28    #[inline]
29    #[track_caller]
30    pub const fn carrying_add_assign_slice(&mut self, rhs: &[Limb], mut carry: Limb) -> Limb {
31        assert!(
32            self.limbs.len() == rhs.len(),
33            "length mismatch in carrying_add_assign_slice"
34        );
35        let mut i = 0;
36        while i < self.limbs.len() {
37            (self.limbs[i], carry) = self.limbs[i].carrying_add(rhs[i], carry);
38            i += 1;
39        }
40        carry
41    }
42
43    /// Perform an in-place carrying add of another limb slice, returning the carried limb value.
44    #[inline]
45    #[track_caller]
46    pub const fn conditional_add_assign(
47        &mut self,
48        rhs: &Self,
49        carry: Limb,
50        choice: Choice,
51    ) -> Limb {
52        self.conditional_add_assign_slice(rhs.as_limbs(), carry, choice)
53    }
54
55    /// Perform an in-place carrying add of another limb slice, returning the carried limb value.
56    ///
57    /// # Panics
58    /// If `self` and `rhs` have different lengths.
59    #[inline]
60    #[track_caller]
61    pub const fn conditional_add_assign_slice(
62        &mut self,
63        rhs: &[Limb],
64        mut carry: Limb,
65        choice: Choice,
66    ) -> Limb {
67        assert!(
68            self.limbs.len() == rhs.len(),
69            "length mismatch in conditional_add_assign_slice"
70        );
71        let mut i = 0;
72        while i < self.limbs.len() {
73            (self.limbs[i], carry) =
74                self.limbs[i].carrying_add(Limb::select(Limb::ZERO, rhs[i], choice), carry);
75            i += 1;
76        }
77        carry
78    }
79}