Skip to main content

crypto_bigint/uint/
mul.rs

1//! [`UInt`] addition operations.
2
3use super::UInt;
4use crate::{Concat, Limb, Wrapping};
5use core::ops::{Mul, MulAssign};
6use subtle::CtOption;
7
8impl<const LIMBS: usize> UInt<LIMBS> {
9    /// Compute "wide" multiplication, with a product twice the size of the input.
10    // TODO(tarcieri): use `concat` (or similar) when const trait is stable
11    pub const fn mul_wide(&self, rhs: &Self) -> (Self, Self) {
12        let mut i = 0;
13        let mut lo = Self::ZERO;
14        let mut hi = Self::ZERO;
15
16        // Schoolbook multiplication.
17        // TODO(tarcieri): use Karatsuba for better performance?
18        while i < LIMBS {
19            let mut j = 0;
20            let mut carry = Limb::ZERO;
21
22            while j < LIMBS {
23                let k = i + j;
24
25                if k >= LIMBS {
26                    let (n, c) = hi.limbs[k - LIMBS].mac(self.limbs[i], rhs.limbs[j], carry);
27                    hi.limbs[k - LIMBS] = n;
28                    carry = c;
29                } else {
30                    let (n, c) = lo.limbs[k].mac(self.limbs[i], rhs.limbs[j], carry);
31                    lo.limbs[k] = n;
32                    carry = c;
33                }
34
35                j += 1;
36            }
37
38            hi.limbs[i + j - LIMBS] = carry;
39            i += 1;
40        }
41
42        (hi, lo)
43    }
44
45    /// Perform wrapping multiplication, discarding overflow.
46    pub const fn wrapping_mul(&self, rhs: &Self) -> Self {
47        self.mul_wide(rhs).1
48    }
49
50    /// Perform checked multiplication, returning a [`CtOption`] which `is_some`
51    /// only if the operation did not overflow.
52    pub fn checked_mul(&self, rhs: &Self) -> CtOption<Self> {
53        let (hi, lo) = self.mul_wide(rhs);
54        CtOption::new(lo, hi.is_zero())
55    }
56
57    /// Square self, returning a "wide" result.
58    pub fn square(&self) -> <Self as Concat>::Output
59    where
60        Self: Concat,
61    {
62        let (hi, lo) = self.mul_wide(self);
63        hi.concat(&lo)
64    }
65}
66
67impl<const LIMBS: usize> Mul for Wrapping<UInt<LIMBS>> {
68    type Output = Self;
69
70    fn mul(self, rhs: Self) -> Wrapping<UInt<LIMBS>> {
71        Wrapping(self.0.wrapping_mul(&rhs.0))
72    }
73}
74
75impl<const LIMBS: usize> Mul<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
76    type Output = Wrapping<UInt<LIMBS>>;
77
78    fn mul(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
79        Wrapping(self.0.wrapping_mul(&rhs.0))
80    }
81}
82
83impl<const LIMBS: usize> Mul<Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
84    type Output = Wrapping<UInt<LIMBS>>;
85
86    fn mul(self, rhs: Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
87        Wrapping(self.0.wrapping_mul(&rhs.0))
88    }
89}
90
91impl<const LIMBS: usize> Mul<&Wrapping<UInt<LIMBS>>> for &Wrapping<UInt<LIMBS>> {
92    type Output = Wrapping<UInt<LIMBS>>;
93
94    fn mul(self, rhs: &Wrapping<UInt<LIMBS>>) -> Wrapping<UInt<LIMBS>> {
95        Wrapping(self.0.wrapping_mul(&rhs.0))
96    }
97}
98
99impl<const LIMBS: usize> MulAssign for Wrapping<UInt<LIMBS>> {
100    fn mul_assign(&mut self, other: Self) {
101        *self = *self * other;
102    }
103}
104
105impl<const LIMBS: usize> MulAssign<&Wrapping<UInt<LIMBS>>> for Wrapping<UInt<LIMBS>> {
106    fn mul_assign(&mut self, other: &Self) {
107        *self = *self * other;
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use crate::Split;
114    use crate::U64;
115
116    #[test]
117    fn mul_wide_zero_and_one() {
118        assert_eq!(U64::ZERO.mul_wide(&U64::ZERO), (U64::ZERO, U64::ZERO));
119        assert_eq!(U64::ZERO.mul_wide(&U64::ONE), (U64::ZERO, U64::ZERO));
120        assert_eq!(U64::ONE.mul_wide(&U64::ZERO), (U64::ZERO, U64::ZERO));
121        assert_eq!(U64::ONE.mul_wide(&U64::ONE), (U64::ZERO, U64::ONE));
122    }
123
124    // TODO(tarcieri): add proptests for multiplication
125    #[test]
126    fn mul_wide_lo_only() {
127        let primes: &[u32] = &[3, 5, 17, 256, 65537];
128
129        for &a_int in primes {
130            for &b_int in primes {
131                let (hi, lo) = U64::from_u32(a_int).mul_wide(&U64::from_u32(b_int));
132                let expected = U64::from_u64(a_int as u64 * b_int as u64);
133                assert_eq!(lo, expected);
134                assert!(bool::from(hi.is_zero()));
135            }
136        }
137    }
138
139    #[test]
140    fn checked_mul_ok() {
141        let n = U64::from_u32(0xffff_ffff);
142        assert_eq!(
143            n.checked_mul(&n).unwrap(),
144            U64::from_u64(0xffff_fffe_0000_0001)
145        );
146    }
147
148    #[test]
149    fn checked_mul_overflow() {
150        let n = U64::from_u64(0xffff_ffff_ffff_ffff);
151        assert!(bool::from(n.checked_mul(&n).is_none()));
152    }
153
154    #[test]
155    fn square() {
156        let n = U64::from_u64(0xffff_ffff_ffff_ffff);
157        let (hi, lo) = n.square().split();
158        assert_eq!(lo, U64::from_u64(1));
159        assert_eq!(hi, U64::from_u64(0xffff_ffff_ffff_fffe));
160    }
161}