Skip to content

Commit 53f9e2c

Browse files
authored
polyval: DRY out bmul* mask calculation (#271)
It can be calculated from a single value
1 parent c629521 commit 53f9e2c

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

polyval/src/backend/soft.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{BLOCK_SIZE, Block, Key, Tag};
1616
use core::{
1717
fmt::{self, Debug},
1818
num::Wrapping,
19-
ops::{Add, BitAnd, BitOr, BitXor, Mul},
19+
ops::{Add, BitAnd, BitOr, BitXor, Mul, Shl},
2020
};
2121
use soft_impl::{karatsuba, mont_reduce};
2222
use universal_hash::{
@@ -229,21 +229,22 @@ impl Zeroize for FieldElement {
229229

230230
/// Multiplication in GF(2)[X], implemented generically and wrapped as `bmul32` and `bmul64`.
231231
///
232-
/// Uses "holes" (sequences of zeroes) to avoid carry spilling, as specified in the four masking
233-
/// operands (`m0`-`m4`), which should have full-width values with the following bit patterns:
232+
/// Uses "holes" (sequences of zeroes) to avoid carry spilling, as specified in the mask operand
233+
/// `m0` which should have a full-width value with the following bit pattern:
234234
///
235-
/// - `m0`: `0b100010001...0001` (e.g. `0x1111_1111u32`)
236-
/// - `m1`: `0b100010001...00010` (e.g. `0x2222_2222u32`)
237-
/// - `m2`: `0b100010001...000100` (e.g. `0x4444_4444u32`)
238-
/// - `m3`: `0b100010001...0001000` (e.g. `0x8888_8888u32`)
235+
/// `0b100010001...0001` (e.g. `0x1111_1111u32`)
239236
///
240237
/// When carries do occur, they wind up in a "hole" and are subsequently masked out of the result.
241238
#[inline]
242-
fn bmul<T>(x: T, y: T, m0: T, m1: T, m2: T, m3: T) -> T
239+
fn bmul<T>(x: T, y: T, m0: T) -> T
243240
where
244-
T: BitAnd<Output = T> + BitOr<Output = T> + Copy,
241+
T: BitAnd<Output = T> + BitOr<Output = T> + Copy + Shl<u32, Output = T>,
245242
Wrapping<T>: BitXor<Output = Wrapping<T>> + Mul<Output = Wrapping<T>>,
246243
{
244+
let m1 = m0 << 1;
245+
let m2 = m1 << 1;
246+
let m3 = m2 << 1;
247+
247248
let x0 = Wrapping(x & m0);
248249
let x1 = Wrapping(x & m1);
249250
let x2 = Wrapping(x & m2);

polyval/src/backend/soft/soft32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub(super) fn karatsuba(h: U32x4, y: U32x4) -> U32x8 {
144144
/// Carryless multiplication in GF(2)[X], truncated to the low 32-bits.
145145
#[inline]
146146
fn bmul32(x: u32, y: u32) -> u32 {
147-
super::bmul(x, y, 0x1111_1111, 0x2222_2222, 0x4444_4444, 0x8888_8888)
147+
super::bmul(x, y, 0x1111_1111)
148148
}
149149

150150
/// Reduce the 256-bit carryless product of Karatsuba modulo the POLYVAL polynomial.

polyval/src/backend/soft/soft64.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,7 @@ pub(super) fn karatsuba(h: U64x2, y: U64x2) -> U64x4 {
7373
/// Carryless multiplication in GF(2)[X], truncated to the low 64-bits.
7474
#[inline]
7575
fn bmul64(x: u64, y: u64) -> u64 {
76-
super::bmul(
77-
x,
78-
y,
79-
0x1111_1111_1111_1111,
80-
0x2222_2222_2222_2222,
81-
0x4444_4444_4444_4444,
82-
0x8888_8888_8888_8888,
83-
)
76+
super::bmul(x, y, 0x1111_1111_1111_1111)
8477
}
8578

8679
/// Reduce the 256-bit carryless product of Karatsuba modulo the POLYVAL polynomial.

0 commit comments

Comments
 (0)