Skip to content

Commit 7303bac

Browse files
rod-chapmanmkannwischer
authored andcommitted
Refactor keccak_squeezeblock_x4() to improve proof time.
Removes pointer arithmetic in favour of an integer current_offset value. Simplifies loop invariant. Proof time on M1/macOS falls from 127s to 25s for all values of MLKEM_K. With gcc 14.2.0 on macOS, generated code at -O3 is identical to the original, so no performance impact expected. Signed-off-by: Rod Chapman <rodchap@amazon.com>
1 parent ddbc21d commit 7303bac

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

mlkem/src/fips202/fips202x4.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,24 @@ __contract__(
9595
assigns(memory_slice(out2, nblocks * r))
9696
assigns(memory_slice(out3, nblocks * r)))
9797
{
98+
size_t current_offset = 0;
9899
while (nblocks > 0)
99100
__loop__(
100-
assigns(out0, out1, out2, out3, nblocks,
101+
assigns(nblocks, current_offset,
101102
memory_slice(s, sizeof(uint64_t) * MLK_KECCAK_LANES * MLK_KECCAK_WAY),
102103
memory_slice(out0, nblocks * r),
103104
memory_slice(out1, nblocks * r),
104105
memory_slice(out2, nblocks * r),
105106
memory_slice(out3, nblocks * r))
106-
invariant(nblocks <= loop_entry(nblocks) &&
107-
out0 == loop_entry(out0) + r * (loop_entry(nblocks) - nblocks) &&
108-
out1 == loop_entry(out1) + r * (loop_entry(nblocks) - nblocks) &&
109-
out2 == loop_entry(out2) + r * (loop_entry(nblocks) - nblocks) &&
110-
out3 == loop_entry(out3) + r * (loop_entry(nblocks) - nblocks))
107+
invariant(nblocks <= loop_entry(nblocks))
108+
invariant(current_offset == (loop_entry(nblocks) - nblocks) * r)
111109
decreases(nblocks))
112110
{
113111
mlk_keccakf1600x4_permute(s);
114-
mlk_keccakf1600x4_extract_bytes(s, out0, out1, out2, out3, 0, r);
115-
116-
out0 += r;
117-
out1 += r;
118-
out2 += r;
119-
out3 += r;
112+
mlk_keccakf1600x4_extract_bytes(
113+
s, &out0[current_offset], &out1[current_offset], &out2[current_offset],
114+
&out3[current_offset], 0, r);
115+
current_offset += r;
120116
nblocks--;
121117
}
122118
}

0 commit comments

Comments
 (0)