Skip to content

Commit cbcd8d5

Browse files
committed
CBMC: Add contract and proof for polyvecl_reduce
Resolves #116 Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 82b328d commit cbcd8d5

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

mldsa/poly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct
2929
void poly_reduce(poly *a)
3030
__contract__(
3131
requires(memory_no_alias(a, sizeof(poly)))
32-
requires(forall(k0, 0, MLDSA_N, a->coeffs[k0] <= REDUCE_DOMAIN_MAX))
32+
requires(array_bound(a->coeffs, 0, MLDSA_N, INT32_MIN, REDUCE_DOMAIN_MAX))
3333
assigns(memory_slice(a, sizeof(poly)))
3434
ensures(array_bound(a->coeffs, 0, MLDSA_N, -REDUCE_RANGE_MAX, REDUCE_RANGE_MAX))
3535
);

mldsa/polyvec.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,18 @@ void polyvecl_reduce(polyvecl *v)
6565
unsigned int i;
6666

6767
for (i = 0; i < MLDSA_L; ++i)
68+
__loop__(
69+
invariant(i <= MLDSA_L)
70+
invariant(forall(k0, i, MLDSA_L, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
71+
invariant(forall(k2, 0, i,
72+
array_bound(v->vec[k2].coeffs, 0, MLDSA_N, -REDUCE_RANGE_MAX, REDUCE_RANGE_MAX))))
6873
{
69-
poly_reduce(&v->vec[i]);
74+
poly t = v->vec[i];
75+
poly_reduce(&t);
76+
/* Full struct assignment from local variables to simplify proof */
77+
/* TODO: eliminate once CBMC resolves
78+
* https://github.com/diffblue/cbmc/issues/8617 */
79+
v->vec[i] = t;
7080
}
7181
}
7282

mldsa/polyvec.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@ void polyvecl_uniform_gamma1(polyvecl *v, const uint8_t seed[MLDSA_CRHBYTES],
2525
uint16_t nonce);
2626

2727
#define polyvecl_reduce MLD_NAMESPACE(polyvecl_reduce)
28-
void polyvecl_reduce(polyvecl *v);
28+
/*************************************************
29+
* Name: polyvecl_reduce
30+
*
31+
* Description: Inplace reduction of all coefficients of all polynomial in a
32+
* vector of length MLDSA_L to
33+
* representative in [-6283008,6283008].
34+
*
35+
* Arguments: - poly *v: pointer to input/output vector
36+
**************************************************/
37+
void polyvecl_reduce(polyvecl *v)
38+
__contract__(
39+
requires(memory_no_alias(v, sizeof(polyvecl)))
40+
requires(forall(k0, 0, MLDSA_L,
41+
array_bound(v->vec[k0].coeffs, 0, MLDSA_N, INT32_MIN, REDUCE_DOMAIN_MAX)))
42+
assigns(memory_slice(v, sizeof(polyvecl)))
43+
ensures(forall(k1, 0, MLDSA_L,
44+
array_bound(v->vec[k1].coeffs, 0, MLDSA_N, -REDUCE_RANGE_MAX, REDUCE_RANGE_MAX)))
45+
);
2946

3047
#define polyvecl_add MLD_NAMESPACE(polyvecl_add)
3148
/*************************************************

proofs/cbmc/polyvecl_reduce/Makefile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
include ../Makefile_params.common
4+
5+
HARNESS_ENTRY = harness
6+
HARNESS_FILE = polyvecl_reduce_harness
7+
8+
# This should be a unique identifier for this proof, and will appear on the
9+
# Litani dashboard. It can be human-readable and contain spaces if you wish.
10+
PROOF_UID = polyvecl_reduce
11+
12+
DEFINES +=
13+
INCLUDES +=
14+
15+
REMOVE_FUNCTION_BODY +=
16+
UNWINDSET +=
17+
18+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
19+
PROJECT_SOURCES += $(SRCDIR)/mldsa/polyvec.c
20+
21+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyvecl_reduce
22+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_reduce
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+
FUNCTION_NAME = polyvecl_reduce
31+
32+
# If this proof is found to consume huge amounts of RAM, you can set the
33+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
34+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
35+
# documentation in Makefile.common under the "Job Pools" heading for details.
36+
# EXPENSIVE = true
37+
38+
# This function is large enough to need...
39+
CBMC_OBJECT_BITS = 8
40+
41+
# If you require access to a file-local ("static") function or object to conduct
42+
# your proof, set the following (and do not include the original source file
43+
# ("mldsa/poly.c") in PROJECT_SOURCES).
44+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
45+
# include ../Makefile.common
46+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/poly.c
47+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
48+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
49+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
50+
# be set before including Makefile.common, but any use of variables on the
51+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
52+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
53+
54+
include ../Makefile.common
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2025 The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "polyvec.h"
5+
6+
void harness(void)
7+
{
8+
polyvecl *a;
9+
polyvecl_reduce(a);
10+
}

0 commit comments

Comments
 (0)