|
2 | 2 |
|
3 | 3 | // TODO(tarcieri): use Karatsuba for better performance |
4 | 4 |
|
5 | | -use crate::{Checked, CheckedMul, Concat, ConcatMixed, Limb, Uint, WideWord, Word, Wrapping, Zero}; |
| 5 | +use crate::{ |
| 6 | + Checked, CheckedMul, Concat, ConcatMixed, Limb, Uint, WideWord, WideningMul, Word, Wrapping, |
| 7 | + Zero, |
| 8 | +}; |
6 | 9 | use core::ops::{Mul, MulAssign}; |
7 | 10 | use subtle::CtOption; |
8 | 11 |
|
@@ -165,15 +168,49 @@ impl<const LIMBS: usize> Uint<LIMBS> { |
165 | 168 | } |
166 | 169 | } |
167 | 170 |
|
| 171 | +impl<const LIMBS: usize, const HLIMBS: usize> CheckedMul<Uint<HLIMBS>> for Uint<LIMBS> { |
| 172 | + type Output = Self; |
| 173 | + |
| 174 | + #[inline] |
| 175 | + fn checked_mul(&self, rhs: Uint<HLIMBS>) -> CtOption<Self> { |
| 176 | + self.checked_mul(&rhs) |
| 177 | + } |
| 178 | +} |
| 179 | + |
168 | 180 | impl<const LIMBS: usize, const HLIMBS: usize> CheckedMul<&Uint<HLIMBS>> for Uint<LIMBS> { |
169 | 181 | type Output = Self; |
170 | 182 |
|
| 183 | + #[inline] |
171 | 184 | fn checked_mul(&self, rhs: &Uint<HLIMBS>) -> CtOption<Self> { |
172 | 185 | let (lo, hi) = self.mul_wide(rhs); |
173 | 186 | CtOption::new(lo, hi.is_zero()) |
174 | 187 | } |
175 | 188 | } |
176 | 189 |
|
| 190 | +impl<const LIMBS: usize, const HLIMBS: usize> WideningMul<Uint<HLIMBS>> for Uint<LIMBS> |
| 191 | +where |
| 192 | + Uint<HLIMBS>: ConcatMixed<Self>, |
| 193 | +{ |
| 194 | + type Output = <Uint<HLIMBS> as ConcatMixed<Self>>::MixedOutput; |
| 195 | + |
| 196 | + #[inline] |
| 197 | + fn widening_mul(&self, rhs: Uint<HLIMBS>) -> Self::Output { |
| 198 | + self.mul(&rhs) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +impl<const LIMBS: usize, const HLIMBS: usize> WideningMul<&Uint<HLIMBS>> for Uint<LIMBS> |
| 203 | +where |
| 204 | + Uint<HLIMBS>: ConcatMixed<Self>, |
| 205 | +{ |
| 206 | + type Output = <Uint<HLIMBS> as ConcatMixed<Self>>::MixedOutput; |
| 207 | + |
| 208 | + #[inline] |
| 209 | + fn widening_mul(&self, rhs: &Uint<HLIMBS>) -> Self::Output { |
| 210 | + self.mul(rhs) |
| 211 | + } |
| 212 | +} |
| 213 | + |
177 | 214 | impl<const LIMBS: usize, const HLIMBS: usize> Mul<Wrapping<Uint<HLIMBS>>> |
178 | 215 | for Wrapping<Uint<LIMBS>> |
179 | 216 | { |
|
0 commit comments