Skip to content

Commit 782e470

Browse files
mkannwischerrod-chapman
authored andcommitted
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) that solves performance problem for this proof. Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu> Update to try Tautschnig's new CBMC build Signed-off-by: Rod Chapman <rodchap@amazon.com> 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: Rod Chapman <rodchap@amazon.com>
1 parent eceae93 commit 782e470

6 files changed

Lines changed: 34 additions & 120 deletions

File tree

mlkem/src/indcpa.c

Lines changed: 21 additions & 40 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
@@ -226,16 +192,31 @@ __contract__(
226192
ensures(forall(x, 0, MLKEM_K, forall(y, 0, MLKEM_K,
227193
array_bound(a->vec[x].vec[y].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
228194
{
229-
unsigned i;
195+
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
196+
unsigned i, j;
230197
for (i = 0; i < MLKEM_K; i++)
231198
__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)))))
199+
assigns(i, j, memory_slice(a, sizeof(mlk_polymat)))
200+
invariant(i <= MLKEM_K)
201+
invariant(forall(x2, 0, MLKEM_K, forall(y2, 0, MLKEM_K,
202+
array_bound(a->vec[x2].vec[y2].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))))
236203
{
237-
mlk_polyvec_permute_bitrev_to_custom(&a->vec[i]);
204+
for (j = 0; j < MLKEM_K; j++)
205+
__loop__(
206+
assigns(j, memory_slice(a, sizeof(mlk_polymat)))
207+
invariant(i <= MLKEM_K)
208+
invariant(j <= MLKEM_K)
209+
invariant(forall(x3, 0, MLKEM_K, forall(y3, 0, MLKEM_K,
210+
array_bound(a->vec[x3].vec[y3].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
211+
)
212+
{
213+
mlk_poly_permute_bitrev_to_custom(a->vec[i].vec[j].coeffs);
214+
}
238215
}
216+
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
217+
/* Nothing to do */
218+
(void)a;
219+
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
239220
}
240221

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

nix/cbmc/default.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ buildEnv {
2121
cbmc = cbmc.overrideAttrs (old: rec {
2222
version = "6.8.0";
2323
src = fetchFromGitHub {
24-
owner = "diffblue";
24+
owner = "tautschnig";
2525
repo = "cbmc";
26-
hash = "sha256-PT6AYiwkplCeyMREZnGZA0BKl4ZESRC02/9ibKg7mYU=";
27-
tag = "cbmc-6.8.0";
26+
hash = "sha256-ng1zjICpmoHUWkG1PuLRmLtaUBmEALpRgNEpbsrnMV8=";
27+
rev = "4f514dbd70c89e3bae03a59f1dc9837acf25885c";
2828
};
2929
});
3030
litani = callPackage ./litani.nix { }; # 1.29.0

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 & 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)