Skip to content

Commit 66aa9c5

Browse files
committed
Revert "optimize gf multiplication"
This reverts commit c563649.
1 parent 0bf6199 commit 66aa9c5

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

kem/mceliece/mceliece348864/mceliece_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,30 @@ func TestPolyMul(t *testing.T) {
262262
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
263263
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
264264
})
265+
266+
fill(&arg1, 8191)
267+
fill(&arg2, 1)
268+
arg1[0] = 1
269+
arg2[0] = 1
270+
polyMul(&res, &arg1, &arg2)
271+
assertEq(t, &res, []gf{
272+
4068, 4068, 18, 4087, 18, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087,
273+
4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087,
274+
4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087,
275+
4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087,
276+
4068, 4087, 4068, 4087, 4068, 4087, 4068, 4087,
277+
})
278+
279+
fill(&arg1, 1)
280+
fill(&arg2, 8191)
281+
arg1[0] = 1
282+
arg2[0] = 1
283+
polyMul(&res, &arg1, &arg2)
284+
assertEq(t, &res, []gf{
285+
4086, 4086, 9, 4094, 9, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
286+
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
287+
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
288+
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
289+
4086, 4094, 4086, 4094, 4086, 4094, 4086, 4094,
290+
})
265291
}

math/gf4096/gf4096.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,16 @@ func Add(a, b Gf) Gf {
1717

1818
// Mul calculate the product of two Gf elements.
1919
func Mul(a, b Gf) Gf {
20-
// carry-less multiplication by adding "holes"
21-
// see https://www.bearssl.org/constanttime.html#ghash-for-gcm
22-
x := uint64(a)
23-
y := uint64(b)
24-
x0 := x & 0x111111111111
25-
x1 := x & 0x222222222222
26-
x2 := x & 0x444444444444
27-
x3 := x & 0x888888888888
28-
y0 := y & 0x111111111111
29-
y1 := y & 0x222222222222
30-
y2 := y & 0x444444444444
31-
y3 := y & 0x888888888888
32-
z0 := (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1)
33-
z1 := (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2)
34-
z2 := (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3)
35-
z3 := (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0)
36-
z0 &= 0x111111111111
37-
z1 &= 0x222222222222
38-
z2 &= 0x444444444444
39-
z3 &= 0x888888888888
40-
tmp := z0 | z1 | z2 | z3
20+
a64 := uint64(a)
21+
b64 := uint64(b)
22+
23+
// if the LSB of b is 1, set tmp to a64, and 0 otherwise
24+
tmp := a64 & -(b64 & 1)
25+
26+
// check if i-th bit of b64 is set, add a64 shifted by i bits if so
27+
for i := 1; i < GfBits; i++ {
28+
tmp ^= a64 * (b64 & (1 << i))
29+
}
4130

4231
// polynomial reduction
4332
t := tmp & 0x7FC000

math/gf4096/gf_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ func TestGeneric(t *testing.T) {
2929
}
3030

3131
func testDiv(t *testing.T, div tdiv) {
32+
assertEq(t, div(6733, 1), 2637)
3233
assertEq(t, div(0, 2), 0)
3334
assertEq(t, div(4, 2), 2)
35+
assertEq(t, div(4096, 2), 0)
3436
assertEq(t, div(9, 3), 7)
37+
assertEq(t, div(4591, 5), 99)
3538
assertEq(t, div(10, 550), 3344)
39+
assertEq(t, div(3, 5501), 1763)
3640
}
3741

3842
func testInv(t *testing.T, inv tinv) {
@@ -41,6 +45,11 @@ func testInv(t *testing.T, inv tinv) {
4145
assertEq(t, inv(2), 2052)
4246
assertEq(t, inv(3), 4088)
4347
assertEq(t, inv(4), 1026)
48+
assertEq(t, inv(4095), 1539)
49+
assertEq(t, inv(4096), 0)
50+
assertEq(t, inv(8191), 1539)
51+
assertEq(t, inv(8192), 0)
52+
assertEq(t, inv(0xFFFF), 1539)
4453
}
4554

4655
func testSqr(t *testing.T, sqr tsqr) {
@@ -73,6 +82,9 @@ func testMul(t *testing.T, mul tmul) {
7382
assertEq(t, mul(125, 37), 3625)
7483
assertEq(t, mul(37, 125), 3625)
7584
assertEq(t, mul(4095, 1), 4095)
85+
assertEq(t, mul(1, 4095), 4095)
86+
assertEq(t, mul(8191, 1), 4086)
87+
assertEq(t, mul(1, 8191), 4095)
7688
assertEq(t, mul(550, 3344), 10)
7789
assertEq(t, mul(3344, 550), 10)
7890
}

math/gf8192/gf8192.go

+10-21
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,16 @@ func Add(a, b Gf) Gf {
1717

1818
// Mul calculate the product of two Gf elements.
1919
func Mul(a, b Gf) Gf {
20-
// carry-less multiplication by adding "holes"
21-
// see https://www.bearssl.org/constanttime.html#ghash-for-gcm
22-
x := uint64(a)
23-
y := uint64(b)
24-
x0 := x & 0x1111111111111
25-
x1 := x & 0x2222222222222
26-
x2 := x & 0x4444444444444
27-
x3 := x & 0x8888888888888
28-
y0 := y & 0x1111111111111
29-
y1 := y & 0x2222222222222
30-
y2 := y & 0x4444444444444
31-
y3 := y & 0x8888888888888
32-
z0 := (x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1)
33-
z1 := (x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2)
34-
z2 := (x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3)
35-
z3 := (x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0)
36-
z0 &= 0x1111111111111
37-
z1 &= 0x2222222222222
38-
z2 &= 0x4444444444444
39-
z3 &= 0x8888888888888
40-
tmp := z0 | z1 | z2 | z3
20+
a64 := uint64(a)
21+
b64 := uint64(b)
22+
23+
// if the LSB of b is 1, set tmp to a64, and 0 otherwise
24+
tmp := a64 & -(b64 & 1)
25+
26+
// check if i-th bit of b64 is set, add a64 shifted by i bits if so
27+
for i := 1; i < GfBits; i++ {
28+
tmp ^= a64 * (b64 & (1 << i))
29+
}
4130

4231
// polynomial reduction
4332
t := tmp & 0x1FF0000

0 commit comments

Comments
 (0)