Skip to content

Commit be7e184

Browse files
hanno-beckermkannwischer
authored andcommitted
Adjust CBMC proof to modified gen_matrix
Signed-off-by: Hanno Becker <[email protected]>
1 parent 9e23df6 commit be7e184

File tree

9 files changed

+129
-31
lines changed

9 files changed

+129
-31
lines changed

mlkem/src/indcpa.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
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_polymat_permute_bitrev_to_custom \
43+
MLK_ADD_PARAM_SET(mlk_polymat_permute_bitrev_to_custom)
4244
/* End of parameter set namespacing */
4345

4446
/*************************************************
@@ -175,21 +177,40 @@ static void mlk_unpack_ciphertext(mlk_polyvec b, mlk_poly *v,
175177
mlk_poly_decompress_dv(v, c + MLKEM_POLYVECCOMPRESSEDBYTES_DU);
176178
}
177179

178-
#if !defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
179-
/* This namespacing is not done at the top to avoid a naming conflict
180-
* with native backends, which are currently not yet namespaced. */
181-
#define mlk_poly_permute_bitrev_to_custom \
182-
MLK_ADD_PARAM_SET(mlk_poly_permute_bitrev_to_custom)
183-
184-
static MLK_INLINE void mlk_poly_permute_bitrev_to_custom(int16_t data[MLKEM_N])
180+
/* Helper function to ensure that the polynomial entries in the output
181+
* of gen_matrix use the standard (bitreversed) ordering of coefficients.
182+
* No-op unless a native backend with a custom ordering is used.
183+
*
184+
* We don't inline this into gen_matrix to avoid having to split the CBMC
185+
* proof for gen_matrix based on MLK_USE_NATIVE_NTT_CUSTOM_ORDER. */
186+
static void mlk_polymat_permute_bitrev_to_custom(mlk_polymat a)
185187
__contract__(
186188
/* We don't specify that this should be a permutation, but only
187189
* that it does not change the bound established at the end of mlk_gen_matrix. */
188-
requires(memory_no_alias(data, sizeof(int16_t) * MLKEM_N))
189-
requires(array_bound(data, 0, MLKEM_N, 0, MLKEM_Q))
190-
assigns(memory_slice(data, sizeof(mlk_poly)))
191-
ensures(array_bound(data, 0, MLKEM_N, 0, MLKEM_Q))) { ((void)data); }
190+
requires(memory_no_alias(a, sizeof(mlk_polymat)))
191+
requires(forall(x, 0, MLKEM_K * MLKEM_K,
192+
array_bound(a[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))
193+
assigns(object_whole(a))
194+
ensures(forall(x, 0, MLKEM_K * MLKEM_K,
195+
array_bound(a[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q))))
196+
{
197+
#if defined(MLK_USE_NATIVE_NTT_CUSTOM_ORDER)
198+
unsigned i;
199+
for (i = 0; i < MLKEM_K * MLKEM_K; i++)
200+
__loop__(
201+
assigns(i, object_whole(a))
202+
invariant(i <= MLKEM_K * MLKEM_K)
203+
invariant(forall(x, 0, MLKEM_K * MLKEM_K,
204+
array_bound(a[x].coeffs, 0, MLKEM_N, 0, MLKEM_Q)))
205+
)
206+
{
207+
mlk_poly_permute_bitrev_to_custom(a[i].coeffs);
208+
}
209+
#else /* MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
210+
/* Nothing to do */
211+
(void)a;
192212
#endif /* !MLK_USE_NATIVE_NTT_CUSTOM_ORDER */
213+
}
193214

194215
/* Reference: `gen_matrix()` in the reference implementation @[REF].
195216
* - We use a special subroutine to generate 4 polynomials
@@ -269,10 +290,7 @@ void mlk_gen_matrix(mlk_polymat a, const uint8_t seed[MLKEM_SYMBYTES],
269290
* The public matrix is generated in NTT domain. If the native backend
270291
* uses a custom order in NTT domain, permute A accordingly.
271292
*/
272-
for (i = 0; i < MLKEM_K * MLKEM_K; i++)
273-
{
274-
mlk_poly_permute_bitrev_to_custom(a[i].coeffs);
275-
}
293+
mlk_polymat_permute_bitrev_to_custom(a);
276294

277295
/* Specification: Partially implements
278296
* @[FIPS203, Section 3.3, Destruction of intermediate values] */
@@ -524,4 +542,4 @@ void mlk_indcpa_dec(uint8_t m[MLKEM_INDCPA_MSGBYTES],
524542
#undef mlk_pack_ciphertext
525543
#undef mlk_unpack_ciphertext
526544
#undef mlk_matvec_mul
527-
#undef mlk_poly_permute_bitrev_to_custom
545+
#undef mlk_polymat_permute_bitrev_to_custom

proofs/cbmc/gen_matrix/Makefile

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

2222
CHECK_FUNCTION_CONTRACTS=mlk_gen_matrix
23-
USE_FUNCTION_CONTRACTS=mlk_poly_rej_uniform mlk_poly_rej_uniform_x4 mlk_poly_permute_bitrev_to_custom mlk_zeroize
23+
USE_FUNCTION_CONTRACTS=mlk_poly_rej_uniform mlk_poly_rej_uniform_x4 mlk_polymat_permute_bitrev_to_custom mlk_zeroize
2424
APPLY_LOOP_CONTRACTS=on
2525
USE_DYNAMIC_FRAMES=1
2626

proofs/cbmc/gen_matrix_native/Makefile renamed to proofs/cbmc/gen_matrix_serial/Makefile

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

66
HARNESS_ENTRY = harness
7-
HARNESS_FILE = gen_matrix_native_harness
7+
HARNESS_FILE = gen_matrix_serial_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 = gen_matrix_native
11+
PROOF_UID = mlk_gen_matrix_serial
1212

13-
DEFINES += -DMLK_CONFIG_USE_NATIVE_BACKEND_ARITH -DMLK_CONFIG_ARITH_BACKEND_FILE="\"dummy_backend.h\""
13+
DEFINES += -DMLK_CONFIG_SERIAL_FIPS202_ONLY
1414
INCLUDES +=
1515

1616
REMOVE_FUNCTION_BODY +=
17-
UNWINDSET += mlk_gen_matrix.0:4 mlk_gen_matrix.1:4 mlk_gen_matrix.2:4 mlk_gen_matrix.3:16
17+
UNWINDSET += mlk_gen_matrix.0:4 mlk_gen_matrix.1:16
1818

1919
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
2020
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/indcpa.c
2121

2222
CHECK_FUNCTION_CONTRACTS=mlk_gen_matrix
23-
USE_FUNCTION_CONTRACTS=mlk_poly_rej_uniform mlk_poly_rej_uniform_x4 mlk_poly_permute_bitrev_to_custom
23+
USE_FUNCTION_CONTRACTS=mlk_poly_rej_uniform mlk_polymat_permute_bitrev_to_custom mlk_zeroize
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_gen_matrix_native
31+
FUNCTION_NAME = mlk_gen_matrix_serial
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/poly_permute_bitrev_to_custom/Makefile renamed to proofs/cbmc/polymat_permute_bitrev_to_custom/Makefile

Lines changed: 4 additions & 4 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 = poly_permute_bitrev_to_custom_harness
7+
HARNESS_FILE = polymat_permute_bitrev_to_custom_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_poly_permute_bitrev_to_custom
11+
PROOF_UID = mlk_polymat_permute_bitrev_to_custom
1212

1313
DEFINES +=
1414
INCLUDES +=
@@ -18,7 +18,7 @@ REMOVE_FUNCTION_BODY +=
1818
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/indcpa.c
2020

21-
CHECK_FUNCTION_CONTRACTS=mlk_poly_permute_bitrev_to_custom
21+
CHECK_FUNCTION_CONTRACTS=mlk_polymat_permute_bitrev_to_custom
2222
USE_FUNCTION_CONTRACTS=
2323
APPLY_LOOP_CONTRACTS=on
2424
USE_DYNAMIC_FRAMES=1
@@ -38,7 +38,7 @@ CBMCFLAGS=--smt2
3838
CBMCFLAGS += --no-array-field-sensitivity
3939
CBMCFLAGS += --slice-formula
4040

41-
FUNCTION_NAME = mlk_poly_permute_bitrev_to_custom
41+
FUNCTION_NAME = mlk_polymat_permute_bitrev_to_custom
4242

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

proofs/cbmc/poly_permute_bitrev_to_custom/poly_permute_bitrev_to_custom_harness.c renamed to proofs/cbmc/polymat_permute_bitrev_to_custom/polymat_permute_bitrev_to_custom_harness.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
// SPDX-License-Identifier: MIT-0
44

55
#include <stdint.h>
6-
#include "params.h"
6+
#include "poly_k.h"
77

8-
void mlk_poly_permute_bitrev_to_custom(int16_t data[MLKEM_N]);
8+
void mlk_polymat_permute_bitrev_to_custom(mlk_polymat a);
99

1010
void harness(void)
1111
{
12-
int16_t *data;
13-
mlk_poly_permute_bitrev_to_custom(data);
12+
mlk_poly *a;
13+
mlk_polymat_permute_bitrev_to_custom(a);
1414
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright (c) The mlkem-native project authors
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
include ../Makefile_params.common
5+
6+
HARNESS_ENTRY = harness
7+
HARNESS_FILE = polymat_permute_bitrev_to_custom_native_harness
8+
9+
# This should be a unique identifier for this proof, and will appear on the
10+
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11+
PROOF_UID = mlk_polymat_permute_bitrev_to_custom_native
12+
13+
DEFINES += -DMLK_CONFIG_USE_NATIVE_BACKEND_ARITH -DMLK_CONFIG_ARITH_BACKEND_FILE="\"dummy_backend.h\""
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
18+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
19+
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/indcpa.c
20+
21+
CHECK_FUNCTION_CONTRACTS=mlk_polymat_permute_bitrev_to_custom
22+
USE_FUNCTION_CONTRACTS= mlk_poly_permute_bitrev_to_custom
23+
APPLY_LOOP_CONTRACTS=on
24+
USE_DYNAMIC_FRAMES=1
25+
26+
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
27+
EXTERNAL_SAT_SOLVER=
28+
CBMCFLAGS=--smt2
29+
30+
# For this proof we tell CBMC to
31+
# - not decompose arrays into their individual cells
32+
# - to slice constraints that are not in the cone of influence of the proof obligations
33+
# These options simplify them modelling of arrays and produce much more compact
34+
# SMT files, leaving all array-type reasoning to the SMT solver.
35+
#
36+
# For functions that use large and multi-dimensional arrays, this yields
37+
# a substantial improvement in proof performance.
38+
# CBMCFLAGS += --no-array-field-sensitivity
39+
# CBMCFLAGS += --slice-formula
40+
41+
FUNCTION_NAME = mlk_polymat_permute_bitrev_to_custom_native
42+
43+
# If this proof is found to consume huge amounts of RAM, you can set the
44+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
45+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
46+
# documentation in Makefile.common under the "Job Pools" heading for details.
47+
# EXPENSIVE = true
48+
CBMC_OBJECT_BITS = 11
49+
50+
# If you require access to a file-local ("static") function or object to conduct
51+
# your proof, set the following (and do not include the original source file
52+
# ("mlkem/src/poly.c") in PROJECT_SOURCES).
53+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
54+
# include ../Makefile.common
55+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mlkem/src/poly.c
56+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
57+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
58+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
59+
# be set before including Makefile.common, but any use of variables on the
60+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
61+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
62+
63+
include ../Makefile.common
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) The mlkem-native project authors
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: MIT-0
4+
5+
#include <stdint.h>
6+
#include "poly_k.h"
7+
8+
void mlk_polymat_permute_bitrev_to_custom(mlk_polymat a);
9+
10+
void harness(void)
11+
{
12+
mlk_poly *a;
13+
mlk_polymat_permute_bitrev_to_custom(a);
14+
}

scripts/check-contracts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def gen_contracts():
4242
def is_exception(funcname):
4343
# The functions passing this filter are known not to have a proof
4444

45+
if funcname == 'poly_permute_bitrev_to_custom':
46+
return True
47+
4548
if funcname.endswith("_native") or funcname.endswith("_asm"):
4649
# CBMC proofs are axiomatized against contracts of the backends
4750
return True

0 commit comments

Comments
 (0)