Skip to main content

crypto_bigint/limb/
mul.rs

1//! Limb multiplication
2
3use super::{Inner, Limb, Wide};
4use crate::{Encoding, Wrapping};
5use core::ops::{Mul, MulAssign};
6use subtle::CtOption;
7
8impl Limb {
9    /// Computes `self + (b * c) + carry`, returning the result along with the new carry.
10    #[inline(always)]
11    pub const fn mac(self, b: Limb, c: Limb, carry: Limb) -> (Limb, Limb) {
12        let a = self.0 as Wide;
13        let b = b.0 as Wide;
14        let c = c.0 as Wide;
15        let carry = carry.0 as Wide;
16        let ret = a + (b * c) + carry;
17        (Limb(ret as Inner), Limb((ret >> Self::BIT_SIZE) as Inner))
18    }
19
20    /// Perform wrapping multiplication, discarding overflow.
21    #[inline(always)]
22    pub const fn wrapping_mul(&self, rhs: Self) -> Self {
23        Limb(self.0.wrapping_mul(rhs.0))
24    }
25
26    /// Perform checked multiplication, returning a [`CtOption`] which `is_some`
27    /// only if the operation did not overflow.
28    #[inline]
29    pub fn checked_mul(&self, rhs: Self) -> CtOption<Self> {
30        let result = self.mul_wide(rhs);
31        let overflow = Limb((result >> Self::BIT_SIZE) as Inner);
32        CtOption::new(Limb(result as Inner), overflow.is_zero())
33    }
34
35    /// Compute "wide" multiplication, with a product twice the size of the input.
36    pub(crate) const fn mul_wide(&self, rhs: Self) -> Wide {
37        (self.0 as Wide) * (rhs.0 as Wide)
38    }
39}
40
41impl Mul for Wrapping<Limb> {
42    type Output = Self;
43
44    fn mul(self, rhs: Self) -> Wrapping<Limb> {
45        Wrapping(self.0.wrapping_mul(rhs.0))
46    }
47}
48
49impl Mul<&Wrapping<Limb>> for Wrapping<Limb> {
50    type Output = Wrapping<Limb>;
51
52    fn mul(self, rhs: &Wrapping<Limb>) -> Wrapping<Limb> {
53        Wrapping(self.0.wrapping_mul(rhs.0))
54    }
55}
56
57impl Mul<Wrapping<Limb>> for &Wrapping<Limb> {
58    type Output = Wrapping<Limb>;
59
60    fn mul(self, rhs: Wrapping<Limb>) -> Wrapping<Limb> {
61        Wrapping(self.0.wrapping_mul(rhs.0))
62    }
63}
64
65impl Mul<&Wrapping<Limb>> for &Wrapping<Limb> {
66    type Output = Wrapping<Limb>;
67
68    fn mul(self, rhs: &Wrapping<Limb>) -> Wrapping<Limb> {
69        Wrapping(self.0.wrapping_mul(rhs.0))
70    }
71}
72
73impl MulAssign for Wrapping<Limb> {
74    fn mul_assign(&mut self, other: Self) {
75        *self = *self * other;
76    }
77}
78
79impl MulAssign<&Wrapping<Limb>> for Wrapping<Limb> {
80    fn mul_assign(&mut self, other: &Self) {
81        *self = *self * other;
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::{Limb, Wide};
88
89    #[test]
90    fn mul_wide_zero_and_one() {
91        assert_eq!(Limb::ZERO.mul_wide(Limb::ZERO), 0);
92        assert_eq!(Limb::ZERO.mul_wide(Limb::ONE), 0);
93        assert_eq!(Limb::ONE.mul_wide(Limb::ZERO), 0);
94        assert_eq!(Limb::ONE.mul_wide(Limb::ONE), 1);
95    }
96
97    // TODO(tarcieri): add proptests for multiplication
98    #[test]
99    fn mul_wide() {
100        let primes: &[u32] = &[3, 5, 17, 256, 65537];
101
102        for &a_int in primes {
103            for &b_int in primes {
104                let actual = Limb::from_u32(a_int).mul_wide(Limb::from_u32(b_int));
105                let expected = a_int as Wide * b_int as Wide;
106                assert_eq!(actual, expected);
107            }
108        }
109    }
110
111    #[test]
112    #[cfg(target_pointer_width = "32")]
113    fn checked_mul_ok() {
114        let n = Limb::from_u16(0xffff);
115        assert_eq!(n.checked_mul(n).unwrap(), Limb::from_u32(0xfffe_0001));
116    }
117
118    #[test]
119    #[cfg(target_pointer_width = "64")]
120    fn checked_mul_ok() {
121        let n = Limb::from_u32(0xffff_ffff);
122        assert_eq!(
123            n.checked_mul(n).unwrap(),
124            Limb::from_u64(0xffff_fffe_0000_0001)
125        );
126    }
127
128    #[test]
129    fn checked_mul_overflow() {
130        let n = Limb::MAX;
131        assert!(bool::from(n.checked_mul(n).is_none()));
132    }
133}