Skip to content

Commit e8ad25b

Browse files
rod-chapmanmkannwischer
authored andcommitted
Introduce local mld_matrix_permute_bitrev_to_custom() function.
to simplify polyvec_maxtrix_expand(), stabilize proof and avoid need to ignore code for proof. Signed-off-by: Rod Chapman <[email protected]>
1 parent 9324e95 commit e8ad25b

File tree

4 files changed

+109
-20
lines changed

4 files changed

+109
-20
lines changed

mldsa/src/polyvec.c

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,47 @@
2121
#include "poly_kl.h"
2222
#include "polyvec.h"
2323

24-
#if !defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
2524
/* This namespacing is not done at the top to avoid a naming conflict
2625
* with native backends, which are currently not yet namespaced. */
27-
#define mld_poly_permute_bitrev_to_custom \
28-
MLD_NAMESPACE_KL(mld_poly_permute_bitrev_to_custom)
26+
#define mld_matrix_permute_bitrev_to_custom \
27+
MLD_NAMESPACE_KL(mld_matrix_permute_bitrev_to_custom)
28+
29+
/* Helper function to ensure that the polynomial entries in the output
30+
* of mld_polyvec_matrix_expand use the standard (bitreversed) ordering
31+
* of coefficients.
32+
* No-op unless a native backend with a custom ordering is used.
33+
*/
34+
static void mld_matrix_permute_bitrev_to_custom(mld_polyvecl mat[MLDSA_K])
35+
__contract__(
36+
/* We don't specify that this should be a permutation, but only
37+
* that it does not change the bound established at the end of
38+
* mld_polyvec_matrix_expand.
39+
*/
40+
requires(memory_no_alias(mat, MLDSA_K * sizeof(mld_polyvecl)))
41+
requires(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
42+
array_bound(mat[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
43+
assigns(object_whole(mat))
44+
ensures(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
45+
array_bound(mat[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
46+
)
47+
{
48+
#if defined(MLD_USE_NATIVE_NTT_CUSTOM_ORDER)
49+
unsigned int i, j;
50+
for (i = 0; i < MLDSA_K; i++)
51+
{
52+
for (j = 0; j < MLDSA_L; j++)
53+
{
54+
mld_poly_permute_bitrev_to_custom(mat[i].vec[j].coeffs);
55+
}
56+
}
57+
58+
#else /* MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
59+
60+
/* Nothing to do */
61+
((void)mat);
2962

30-
static MLD_INLINE void mld_poly_permute_bitrev_to_custom(int32_t data[MLDSA_N])
31-
{
32-
((void)data);
33-
}
3463
#endif /* !MLD_USE_NATIVE_NTT_CUSTOM_ORDER */
64+
}
3565

3666

3767
MLD_INTERNAL_API
@@ -112,17 +142,7 @@ void mld_polyvec_matrix_expand(mld_polyvecl mat[MLDSA_K],
112142
i++;
113143
}
114144

115-
/*
116-
* The public matrix is generated in NTT domain. If the native backend
117-
* uses a custom order in NTT domain, permute A accordingly.
118-
*/
119-
for (i = 0; i < MLDSA_K; i++)
120-
{
121-
for (j = 0; j < MLDSA_L; j++)
122-
{
123-
mld_poly_permute_bitrev_to_custom(mat[i].vec[j].coeffs);
124-
}
125-
}
145+
mld_matrix_permute_bitrev_to_custom(mat);
126146

127147
/* @[FIPS204, Section 3.6.3] Destruction of intermediate values. */
128148
mld_zeroize(seed_ext, sizeof(seed_ext));
@@ -161,7 +181,7 @@ void mld_polyvecl_uniform_gamma1(mld_polyvecl *v,
161181
/* Safety: nonce is at most ((UINT16_MAX - MLDSA_L) / MLDSA_L), and, hence,
162182
* this cast is safe. See NONCE_UB comment in sign.c. */
163183
nonce = (uint16_t)(MLDSA_L * nonce);
164-
/* Now, nonce <= UINT16_MAX - (MLDSA_L - 1), so the casts below are safe. */
184+
/* Now, nonce <= UINT16_MAX - (MLDSA_L - 1), so the casts below are safe. */
165185
#if MLDSA_L == 4
166186
mld_poly_uniform_gamma1_4x(&v->vec[0], &v->vec[1], &v->vec[2], &v->vec[3],
167187
seed, nonce, (uint16_t)(nonce + 1),
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 = matrix_permute_bitrev_to_custom_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 = matrix_permute_bitrev_to_custom
12+
13+
DEFINES +=
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_NAMESPACE)mld_matrix_permute_bitrev_to_custom
23+
USE_FUNCTION_CONTRACTS=
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 = mld_matrix_permute_bitrev_to_custom
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
#define mld_matrix_permute_bitrev_to_custom \
7+
MLD_NAMESPACE_KL(mld_matrix_permute_bitrev_to_custom)
8+
void mld_matrix_permute_bitrev_to_custom(mld_polyvecl mat[MLDSA_K]);
9+
10+
void harness(void)
11+
{
12+
mld_polyvecl *mat;
13+
mld_matrix_permute_bitrev_to_custom(mat);
14+
}

proofs/cbmc/polyvec_matrix_expand/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)/mldsa/src/polyvec.c
2121

2222
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyvec_matrix_expand
23-
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform_4x $(MLD_NAMESPACE)poly_uniform
23+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform_4x $(MLD_NAMESPACE)poly_uniform $(MLD_NAMESPACE)mld_matrix_permute_bitrev_to_custom
2424
APPLY_LOOP_CONTRACTS=on
2525
USE_DYNAMIC_FRAMES=1
2626

0 commit comments

Comments
 (0)