Skip to content

Commit 4f7ba63

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. Also updates CBMC to trial new build (CBMC PR[#8705](https://github.com/pq-code-package/mlkem-native/issues/8705)) that solves performance problem for this proof. Simplify polymat_permite_bitrev_to_customer() (native version) The code structure now mimics the data structure to make proof tractable. Also updates Makefile for this proof in line with the similar function in mldsa-native. Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu> Signed-off-by: Rod Chapman <rodchap@amazon.com> Signed-off-by: willieyz <willie.zhao@chelpis.com>
1 parent 5a3fc59 commit 4f7ba63

6 files changed

Lines changed: 32 additions & 131 deletions

File tree

mlkem/src/indcpa.c

Lines changed: 21 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
#define mlk_pack_ciphertext MLK_ADD_PARAM_SET(mlk_pack_ciphertext)
4040
#define mlk_unpack_ciphertext MLK_ADD_PARAM_SET(mlk_unpack_ciphertext)
4141
#define mlk_matvec_mul MLK_ADD_PARAM_SET(mlk_matvec_mul)
42-
#define mlk_polyvec_permute_bitrev_to_custom \
43-
MLK_ADD_PARAM_SET(mlk_polyvec_permute_bitrev_to_custom)
4442
#define mlk_polymat_permute_bitrev_to_custom \
4543
MLK_ADD_PARAM_SET(mlk_polymat_permute_bitrev_to_custom)
4644
/* End of parameter set namespacing */
@@ -181,40 +179,6 @@ static void mlk_unpack_ciphertext(mlk_polyvec *b, mlk_poly *v,
181179
mlk_poly_decompress_dv(v, c + MLKEM_POLYVECCOMPRESSEDBYTES_DU);
182180
}
183181

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-
218182
static void mlk_polymat_permute_bitrev_to_custom(mlk_polymat *a)
219183
__contract__(
220184
/* We don't specify that this should be a permutation, but only
@@ -226,16 +190,31 @@ __contract__(
226190
ensures(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
227191
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
228192
{
229-
unsigned i;
193+
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
194+
unsigned i, j;
230195
for (i = 0; i < MLKEM_K; i++)
231196
__loop__(
232-
assigns(i, memory_slice(a, sizeof(mlk_polymat)))
233-
invariant(i <= MLKEM_K)
234-
invariant(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
235-
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
197+
assigns(i, j, memory_slice(a, sizeof(mlk_polymat)))
198+
invariant(i <= MLKEM_K)
199+
invariant(forall(x2, 0, MLKEM_K, forall(y2, 0, MLKEM_K,
200+
array_bound(a->vec[x2].vec[y2].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
236201
{
237-
mlk_polyvec_permute_bitrev_to_custom(&a->vec[i]);
202+
for (j = 0; j < MLKEM_K; j++)
203+
__loop__(
204+
assigns(j, memory_slice(a, sizeof(mlk_polymat)))
205+
invariant(i <= MLKEM_K)
206+
invariant(j <= MLKEM_K)
207+
invariant(forall(x3, 0, MLKEM_K, forall(y3, 0, MLKEM_K,
208+
array_bound(a->vec[x3].vec[y3].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
209+
)
210+
{
211+
mlk_poly_permute_bitrev_to_custom(a->vec[i].vec[j].coeffs);
212+
}
238213
}
214+
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
215+
/* Nothing to do */
216+
(void)a;
217+
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
239218
}
240219

241220
/* Reference: `gen_matrix()` in the reference implementation @[REF].
@@ -617,5 +596,4 @@ int mlk_indcpa_dec(uint8_t m[MLKEM_INDCPA_MSGBYTES],
617596
#undef mlk_pack_ciphertext
618597
#undef mlk_unpack_ciphertext
619598
#undef mlk_matvec_mul
620-
#undef mlk_polyvec_permute_bitrev_to_custom
621599
#undef mlk_polymat_permute_bitrev_to_custom

proofs/cbmc/polymat_permute_bitrev_to_custom/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/indcpa.c
2020

2121
CHECK_FUNCTION_CONTRACTS=mlk_polymat_permute_bitrev_to_custom
22-
USE_FUNCTION_CONTRACTS=mlk_polyvec_permute_bitrev_to_custom
22+
USE_FUNCTION_CONTRACTS=
2323
APPLY_LOOP_CONTRACTS=on
2424
USE_DYNAMIC_FRAMES=1
2525

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

Lines changed: 7 additions & 7 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,23 +19,23 @@ 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=
29-
CBMCFLAGS=--smt2
29+
CBMCFLAGS=--smt2 --slice-formula --no-array-field-sensitivity
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
3535
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
3636
# documentation in Makefile.common under the "Job Pools" heading for details.
3737
# EXPENSIVE = true
38-
CBMC_OBJECT_BITS = 10
38+
CBMC_OBJECT_BITS = 12
3939

4040
# If you require access to a file-local ("static") function or object to conduct
4141
# your proof, set the following (and do not include the original source file

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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +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-
{
13-
/* Dummy use of `free` to work around CBMC issue #8814. */
14-
free(NULL);
15-
}
16-
17-
mlk_polyvec *v;
18-
mlk_polyvec_permute_bitrev_to_custom(v);
12+
mlk_polymat *a;
13+
mlk_polymat_permute_bitrev_to_custom(a);
1914
}

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 & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)