Skip to content

Commit a5aeef9

Browse files
committed
mlkem: gen_matrix.jinc update from formosa-crypto/formosa-mlkem#19
1 parent 37c9502 commit a5aeef9

File tree

2 files changed

+64
-80
lines changed

2 files changed

+64
-80
lines changed

src/crypto_kem/mlkem/mlkem768/amd64/avx2/gen_matrix.jinc

+32-40
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,46 @@ fn __rej_uniform(stack u16[MLKEM_N] rp, reg u64 offset, stack u8[SHAKE128_RATE]
99
reg u16 val1 val2;
1010
reg u16 t;
1111
reg u64 pos ctr;
12-
reg u64 cnd0 cnd1 exit;
1312

1413

1514
ctr = offset;
1615
pos = 0;
17-
exit = 0;
1816

19-
while(exit == 0)
20-
{
21-
val1 = (16u)buf[(int)pos];
22-
pos += 1;
23-
t = (16u)buf[(int)pos];
24-
val2 = t;
25-
val2 >>= 4;
26-
t &= 0x0F;
27-
t <<= 8;
28-
val1 |= t;
29-
pos += 1;
30-
31-
t = (16u)buf[(int)pos];
32-
t <<= 4;
33-
val2 |= t;
34-
pos += 1;
35-
36-
if(val1 < MLKEM_Q)
37-
{
38-
rp[(int)ctr] = val1;
39-
ctr += 1;
40-
}
41-
42-
if(val2 < MLKEM_Q)
43-
{
44-
if(ctr < MLKEM_N)
45-
{
46-
rp[(int)ctr] = val2;
17+
while (pos < SHAKE128_RATE - 2) {
18+
if ctr < MLKEM_N {
19+
val1 = (16u)buf[pos];
20+
t = (16u)buf[pos + 1];
21+
val2 = t;
22+
val2 >>= 4;
23+
t &= 0x0F;
24+
t <<= 8;
25+
val1 |= t;
26+
27+
t = (16u)buf[pos + 2];
28+
t <<= 4;
29+
val2 |= t;
30+
pos += 3;
31+
32+
reg bool cond;
33+
#[declassify]
34+
cond = val1 < MLKEM_Q;
35+
if cond {
36+
rp[ctr] = val1;
4737
ctr += 1;
4838
}
49-
}
5039

51-
// Check if we should exit the loop
52-
cnd0 = MLKEM_N;
53-
cnd0 -= ctr;
54-
cnd0 -= 1;
55-
cnd1 = SHAKE128_RATE;
56-
cnd1 -= pos;
57-
cnd1 -= 3; //TODO: (potentially) wasting 2 'good' bytes
58-
exit = cnd0 | cnd1;
59-
exit >>= 63;
40+
#[declassify]
41+
cond = val2 < MLKEM_Q;
42+
if cond {
43+
if(ctr < MLKEM_N)
44+
{
45+
rp[ctr] = val2;
46+
ctr += 1;
47+
}
48+
}
49+
} else {
50+
pos = SHAKE128_RATE;
51+
}
6052
}
6153

6254
return ctr, rp;

src/crypto_kem/mlkem/mlkem768/amd64/ref/gen_matrix.jinc

+32-40
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,46 @@ fn __rej_uniform(stack u16[MLKEM_N] rp, reg u64 offset, stack u8[SHAKE128_RATE]
77
reg u16 val1 val2;
88
reg u16 t;
99
reg u64 pos ctr;
10-
reg u64 cnd0 cnd1 exit;
1110

1211

1312
ctr = offset;
1413
pos = 0;
15-
exit = 0;
1614

17-
while(exit == 0)
18-
{
19-
val1 = (16u)buf[(int)pos];
20-
pos += 1;
21-
t = (16u)buf[(int)pos];
22-
val2 = t;
23-
val2 >>= 4;
24-
t &= 0x0F;
25-
t <<= 8;
26-
val1 |= t;
27-
pos += 1;
28-
29-
t = (16u)buf[(int)pos];
30-
t <<= 4;
31-
val2 |= t;
32-
pos += 1;
33-
34-
if(val1 < MLKEM_Q)
35-
{
36-
rp[(int)ctr] = val1;
37-
ctr += 1;
38-
}
39-
40-
if(val2 < MLKEM_Q)
41-
{
42-
if(ctr < MLKEM_N)
43-
{
44-
rp[(int)ctr] = val2;
15+
while (pos < SHAKE128_RATE - 2) {
16+
if ctr < MLKEM_N {
17+
val1 = (16u)buf[pos];
18+
t = (16u)buf[pos + 1];
19+
val2 = t;
20+
val2 >>= 4;
21+
t &= 0x0F;
22+
t <<= 8;
23+
val1 |= t;
24+
25+
t = (16u)buf[pos + 2];
26+
t <<= 4;
27+
val2 |= t;
28+
pos += 3;
29+
30+
reg bool cond;
31+
#[declassify]
32+
cond = val1 < MLKEM_Q;
33+
if cond {
34+
rp[ctr] = val1;
4535
ctr += 1;
4636
}
47-
}
4837

49-
// Check if we should exit the loop
50-
cnd0 = MLKEM_N;
51-
cnd0 -= ctr;
52-
cnd0 -= 1;
53-
cnd1 = SHAKE128_RATE;
54-
cnd1 -= pos;
55-
cnd1 -= 3; //TODO: (potentially) wasting 2 'good' bytes
56-
exit = cnd0 | cnd1;
57-
exit >>= 63;
38+
#[declassify]
39+
cond = val2 < MLKEM_Q;
40+
if cond {
41+
if(ctr < MLKEM_N)
42+
{
43+
rp[ctr] = val2;
44+
ctr += 1;
45+
}
46+
}
47+
} else {
48+
pos = SHAKE128_RATE;
49+
}
5850
}
5951

6052
return ctr, rp;

0 commit comments

Comments
 (0)