Skip to content

Commit 7cd3fa0

Browse files
committed
Correct loop contracts to add decreases clauses
Signed-off-by: Rod Chapman <rodchap@amazon.com>
1 parent 1602ef8 commit 7cd3fa0

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

mlkem/src/poly_k.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,27 @@ __contract__(
173173
for (i = 0; i < MLKEM_N / 2; i++)
174174
__loop__(
175175
invariant(i <= MLKEM_N / 2)
176-
invariant(array_abs_bound(r->coeffs, 0, 2 * i, INT16_MAX/2)))
177-
decreases(MLKEM_N / 2 - i))
178-
{
179-
unsigned k;
180-
int32_t t[2] = {0};
181-
for (k = 0; k < MLKEM_K; k++)
182-
__loop__(
176+
invariant(array_abs_bound(r->coeffs, 0, 2 * i, INT16_MAX/2))
177+
decreases(MLKEM_N / 2 - i)
178+
)
179+
{
180+
unsigned k;
181+
int32_t t[2] = {0};
182+
for (k = 0; k < MLKEM_K; k++)
183+
__loop__(
183184
invariant(k <= MLKEM_K && i <= MLKEM_N / 2)
184-
invariant(array_abs_bound(t, 0, 2, k * 2 * MLKEM_UINT12_LIMIT * MLK_NTT_BOUND + 1)))
185-
decreases(MLKEM_K - k))
186-
{
187-
t[0] +=
188-
(int32_t)a->vec[k].coeffs[2 * i + 1] * b_cache->vec[k].coeffs[i];
189-
t[0] += (int32_t)a->vec[k].coeffs[2 * i] * b->vec[k].coeffs[2 * i];
190-
t[1] += (int32_t)a->vec[k].coeffs[2 * i] * b->vec[k].coeffs[2 * i + 1];
191-
t[1] += (int32_t)a->vec[k].coeffs[2 * i + 1] * b->vec[k].coeffs[2 * i];
192-
}
193-
r->coeffs[2 * i + 0] = mlk_montgomery_reduce(t[0]);
194-
r->coeffs[2 * i + 1] = mlk_montgomery_reduce(t[1]);
185+
invariant(array_abs_bound(t, 0, 2, k * 2 * MLKEM_UINT12_LIMIT * MLK_NTT_BOUND + 1))
186+
decreases(MLKEM_K - k)
187+
)
188+
{
189+
t[0] += (int32_t)a->vec[k].coeffs[2 * i + 1] * b_cache->vec[k].coeffs[i];
190+
t[0] += (int32_t)a->vec[k].coeffs[2 * i] * b->vec[k].coeffs[2 * i];
191+
t[1] += (int32_t)a->vec[k].coeffs[2 * i] * b->vec[k].coeffs[2 * i + 1];
192+
t[1] += (int32_t)a->vec[k].coeffs[2 * i + 1] * b->vec[k].coeffs[2 * i];
195193
}
194+
r->coeffs[2 * i + 0] = mlk_montgomery_reduce(t[0]);
195+
r->coeffs[2 * i + 1] = mlk_montgomery_reduce(t[1]);
196+
}
196197
}
197198

198199
MLK_INTERNAL_API

0 commit comments

Comments
 (0)