Skip to content

Commit 18ed177

Browse files
committed
Refactor mlk_polymat_permute_bitrev_to_custom
This commit refactors mlk_polymat_permute_bitrev_to_custom to not require the helper function mlk_polyvec_permute_bitrev_to_custom. The function was only needed due CBMC limitations. Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
1 parent 49caebb commit 18ed177

File tree

5 files changed

+17
-112
lines changed

5 files changed

+17
-112
lines changed

mlkem/src/indcpa.c

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -181,40 +181,6 @@ static void mlk_unpack_ciphertext(mlk_polyvec *b, mlk_poly *v,
181181
mlk_poly_decompress_dv(v, c + MLKEM_POLYVECCOMPRESSEDBYTES_DU);
182182
}
183183

184-
/* Helper function to ensure that the polynomial entries in the output
185-
* of gen_matrix use the standard (bitreversed) ordering of coefficients.
186-
* No-op unless a native backend with a custom ordering is used.
187-
*
188-
* We don't inline this into gen_matrix to avoid having to split the CBMC
189-
* proof for gen_matrix based on MLK_USE_NATIVE_NTT_CUSTOM_ORDER. */
190-
static void mlk_polyvec_permute_bitrev_to_custom(mlk_polyvec *v)
191-
__contract__(
192-
/* We don't specify that this should be a permutation, but only
193-
* that it does not change the bound established at the end of mlk_gen_matrix. */
194-
requires(memory_no_alias(v, sizeof(mlk_polyvec)))
195-
requires(forall(x, 0, MLKEM_K,
196-
array_bound(v->vec[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))
197-
assigns(memory_slice(v, sizeof(mlk_polyvec)))
198-
ensures(forall(x, 0, MLKEM_K,
199-
array_bound(v->vec[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
200-
{
201-
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
202-
unsigned i;
203-
for (i = 0; i < MLKEM_K; i++)
204-
__loop__(
205-
assigns(i, memory_slice(v, sizeof(mlk_polyvec)))
206-
invariant(i <= MLKEM_K)
207-
invariant(forall(x, 0, MLKEM_K,
208-
array_bound(v->vec[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
209-
{
210-
mlk_poly_permute_bitrev_to_custom(v->vec[i].coeffs);
211-
}
212-
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
213-
/* Nothing to do */
214-
(void)v;
215-
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
216-
}
217-
218184
static void mlk_polymat_permute_bitrev_to_custom(mlk_polymat *a)
219185
__contract__(
220186
/* We don't specify that this should be a permutation, but only
@@ -227,15 +193,21 @@ __contract__(
227193
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
228194
{
229195
unsigned i;
230-
for (i = 0; i < MLKEM_K; i++)
196+
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
197+
for (i = 0; i < MLKEM_K * MLKEM_K; i++)
231198
__loop__(
232199
assigns(i, object_whole(a))
233-
invariant(i <= MLKEM_K)
200+
invariant(i <= MLKEM_K * MLKEM_K)
234201
invariant(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
235202
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
236203
{
237-
mlk_polyvec_permute_bitrev_to_custom(&a->vec[i]);
204+
mlk_poly_permute_bitrev_to_custom(
205+
a->vec[i / MLKEM_K].vec[i % MLKEM_K].coeffs);
238206
}
207+
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
208+
/* Nothing to do */
209+
(void)a;
210+
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
239211
}
240212

241213
/* Reference: `gen_matrix()` in the reference implementation @[REF].

proofs/cbmc/polyvec_permute_bitrev_to_custom_native/Makefile renamed to proofs/cbmc/polymat_permute_bitrev_to_custom_native/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
include ../Makefile_params.common
55

66
HARNESS_ENTRY = harness
7-
HARNESS_FILE = polyvec_permute_bitrev_to_custom_native_harness
7+
HARNESS_FILE = polymat_permute_bitrev_to_custom_native_harness
88

99
# This should be a unique identifier for this proof, and will appear on the
1010
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11-
PROOF_UID = mlk_polyvec_permute_bitrev_to_custom_native
11+
PROOF_UID = mlk_polymat_permute_bitrev_to_custom_native
1212

1313
DEFINES += -DMLK_CONFIG_USE_NATIVE_BACKEND_ARITH -DMLK_CONFIG_ARITH_BACKEND_FILE="\"dummy_backend.h\""
1414
INCLUDES +=
@@ -19,16 +19,16 @@ REMOVE_FUNCTION_BODY +=
1919
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
2020
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/indcpa.c
2121

22-
CHECK_FUNCTION_CONTRACTS=mlk_polyvec_permute_bitrev_to_custom
23-
USE_FUNCTION_CONTRACTS= mlk_poly_permute_bitrev_to_custom
22+
CHECK_FUNCTION_CONTRACTS=mlk_polymat_permute_bitrev_to_custom
23+
USE_FUNCTION_CONTRACTS=mlk_poly_permute_bitrev_to_custom
2424
APPLY_LOOP_CONTRACTS=on
2525
USE_DYNAMIC_FRAMES=1
2626

2727
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
2828
EXTERNAL_SAT_SOLVER=
2929
CBMCFLAGS=--smt2
3030

31-
FUNCTION_NAME = mlk_polyvec_permute_bitrev_to_custom_native
31+
FUNCTION_NAME = mlk_polymat_permute_bitrev_to_custom_native
3232

3333
# If this proof is found to consume huge amounts of RAM, you can set the
3434
# EXPENSIVE variable. With new enough versions of the proof tools, this will

proofs/cbmc/polyvec_permute_bitrev_to_custom/polyvec_permute_bitrev_to_custom_harness.c renamed to proofs/cbmc/polymat_permute_bitrev_to_custom_native/polymat_permute_bitrev_to_custom_native_harness.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <stdint.h>
66
#include "poly_k.h"
77

8-
void mlk_polyvec_permute_bitrev_to_custom(mlk_polyvec *v);
8+
void mlk_polymat_permute_bitrev_to_custom(mlk_polymat *a);
99

1010
void harness(void)
1111
{
12-
mlk_polyvec *v;
13-
mlk_polyvec_permute_bitrev_to_custom(v);
12+
mlk_polymat *a;
13+
mlk_polymat_permute_bitrev_to_custom(a);
1414
}

proofs/cbmc/polyvec_permute_bitrev_to_custom/Makefile

Lines changed: 0 additions & 53 deletions
This file was deleted.

proofs/cbmc/polyvec_permute_bitrev_to_custom_native/polyvec_permute_bitrev_to_custom_native_harness.c

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)