Skip to content

Commit 78014be

Browse files
CBMC: Add proofs on top of native functions (polymat_permute_bitrev_to_custom_native)
- This commit adds CBMC proofs for the native function `mld_poly_permute_bitrev_to_custom`, called `polymat_permute_bitrev_to_custom_native` - This following changes are include: - Apply the same harness file and construct function contract, reference from `polymat_permute_bitrev_to_custom`. - Use the following function contracts: - `mld_poly_permute_bitrev_to_custom` - Add `MLD_USE_NATIVE_NTT_CUSTOM_ORDER` in dummy_backend.h - Also, for makeing this CBMC proof work, this commit refactor `mld_polymat_permute_bitrev_to_custom` from double loop to single loop. Co-authored-by: Matthias J. Kannwischer <matthias@kannwischer.eu> Signed-off-by: willieyz <willie.zhao@chelpis.com>
1 parent f9a6605 commit 78014be

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

mldsa/src/native/api.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,17 @@ set if there are native implementations for NTT and INTT."
116116
* Arguments: - int32_t p[MLDSA_N]: pointer to in/output polynomial
117117
*
118118
**************************************************/
119-
static MLD_INLINE void mld_poly_permute_bitrev_to_custom(int32_t p[MLDSA_N]);
119+
static MLD_INLINE void mld_poly_permute_bitrev_to_custom(int32_t p[MLDSA_N])
120+
__contract__(
121+
/* We don't specify that this should be a permutation, but only
122+
* that it does not change the bound established at the end of
123+
* mld_polyvec_matrix_expand.
124+
*/
125+
requires(memory_no_alias(p, sizeof(int32_t) * MLDSA_N))
126+
requires(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q))
127+
assigns(memory_slice(p, sizeof(int32_t) * MLDSA_N))
128+
ensures(array_bound(p, 0, MLDSA_N, 0, MLDSA_Q))
129+
);
120130
#endif /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
121131

122132

mldsa/src/polyvec.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ __contract__(
4949
{
5050
#if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
5151
/* TODO: proof */
52-
unsigned int i, j;
53-
for (i = 0; i < MLDSA_K; i++)
52+
unsigned int i;
53+
for (i = 0; i < MLDSA_K * MLDSA_L; i++)
54+
__loop__(
55+
assigns(i, memory_slice(mat, sizeof(mld_polymat)))
56+
invariant(i <= MLDSA_K * MLDSA_L)
57+
invariant(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
58+
array_bound(mat->vec[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
59+
)
5460
{
55-
for (j = 0; j < MLDSA_L; j++)
56-
{
57-
mld_poly_permute_bitrev_to_custom(mat->vec[i].vec[j].coeffs);
58-
}
61+
mld_poly_permute_bitrev_to_custom(
62+
mat->vec[i / MLDSA_L].vec[i % MLDSA_L].coeffs);
5963
}
6064

6165
#else /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) The mldsa-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 = polymat_permute_bitrev_to_custom_native
12+
13+
DEFINES += -DMLD_CONFIG_USE_NATIVE_BACKEND_ARITH -DMLD_CONFIG_ARITH_BACKEND_FILE="\"dummy_backend.h\""
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mldsa/src/polyvec.c
21+
22+
CHECK_FUNCTION_CONTRACTS=mld_polymat_permute_bitrev_to_custom
23+
USE_FUNCTION_CONTRACTS=mld_poly_permute_bitrev_to_custom
24+
APPLY_LOOP_CONTRACTS=on
25+
USE_DYNAMIC_FRAMES=1
26+
27+
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
28+
EXTERNAL_SAT_SOLVER=
29+
CBMCFLAGS=--smt2 --slice-formula --no-array-field-sensitivity
30+
31+
FUNCTION_NAME = polymat_permute_bitrev_to_custom_native
32+
33+
# If this proof is found to consume huge amounts of RAM, you can set the
34+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
35+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
36+
# documentation in Makefile.common under the "Job Pools" heading for details.
37+
# EXPENSIVE = true
38+
39+
# This function is large enough to need...
40+
CBMC_OBJECT_BITS = 12
41+
42+
# If you require access to a file-local ("static") function or object to conduct
43+
# your proof, set the following (and do not include the original source file
44+
# ("mldsa/poly.c") in PROJECT_SOURCES).
45+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
46+
# include ../Makefile.common
47+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/poly.c
48+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
49+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
50+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
51+
# be set before including Makefile.common, but any use of variables on the
52+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
53+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
54+
55+
include ../Makefile.common
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include "polyvec.h"
5+
6+
void mld_polymat_permute_bitrev_to_custom(mld_polymat *mat);
7+
8+
void harness(void)
9+
{
10+
mld_polymat *mat;
11+
mld_polymat_permute_bitrev_to_custom(mat);
12+
}

scripts/check-contracts

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

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

0 commit comments

Comments
 (0)