Skip to content

Commit 089052e

Browse files
mkannwischerhanno-becker
authored andcommitted
CBMC: Add contract and proof for polyveck_add
Resolves #126 Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 4daf5de commit 089052e

File tree

6 files changed

+86
-3
lines changed

6 files changed

+86
-3
lines changed

mldsa/poly.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void poly_add(poly *c, const poly *a, const poly *b)
5050
{
5151
c->coeffs[i] = a->coeffs[i] + b->coeffs[i];
5252
}
53+
54+
cassert(forall(k, 0, MLDSA_N, c->coeffs[k] == a->coeffs[k] + b->coeffs[k]));
5355
}
5456

5557
void poly_sub(poly *c, const poly *a, const poly *b)

mldsa/poly.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ __contract__(
6868
requires(memory_no_alias(b, sizeof(poly)))
6969
requires(forall(k0, 0, MLDSA_N, (int64_t) a->coeffs[k0] + b->coeffs[k0] <= INT32_MAX))
7070
requires(forall(k1, 0, MLDSA_N, (int64_t) a->coeffs[k1] + b->coeffs[k1] >= INT32_MIN))
71-
ensures(forall(k, 0, MLDSA_N, c->coeffs[k] == a->coeffs[k] + b->coeffs[k]))
7271
assigns(memory_slice(c, sizeof(poly)))
7372
);
7473

mldsa/polyvec.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,15 @@ void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v)
209209
unsigned int i;
210210

211211
for (i = 0; i < MLDSA_K; ++i)
212+
__loop__(
213+
invariant(i <= MLDSA_K))
212214
{
213-
poly_add(&w->vec[i], &u->vec[i], &v->vec[i]);
215+
poly t;
216+
poly_add(&t, &u->vec[i], &v->vec[i]);
217+
/* Full struct assignment from local variables to simplify proof */
218+
/* TODO: eliminate once CBMC resolves
219+
* https://github.com/diffblue/cbmc/issues/8617 */
220+
w->vec[i] = t;
214221
}
215222
}
216223

mldsa/polyvec.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,18 @@ void polyveck_caddq(polyveck *v);
168168
* - const polyveck *u: pointer to first summand
169169
* - const polyveck *v: pointer to second summand
170170
**************************************************/
171-
void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v);
171+
void polyveck_add(polyveck *w, const polyveck *u, const polyveck *v)
172+
__contract__(
173+
requires(memory_no_alias(w, sizeof(polyveck)))
174+
requires(memory_no_alias(u, sizeof(polyveck)))
175+
requires(memory_no_alias(v, sizeof(polyveck)))
176+
requires(forall(k0, 0, MLDSA_K,
177+
forall(k1, 0, MLDSA_N, (int64_t) u->vec[k0].coeffs[k1] + v->vec[k0].coeffs[k1] <= INT32_MAX)))
178+
requires(forall(k2, 0, MLDSA_K,
179+
forall(k3, 0, MLDSA_N, (int64_t) u->vec[k2].coeffs[k3] + v->vec[k2].coeffs[k3] >= INT32_MIN)))
180+
assigns(memory_slice(w, sizeof(polyveck)))
181+
);
182+
172183
/*************************************************
173184
* Name: polyveck_sub
174185
*

proofs/cbmc/polyveck_add/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 = polyveck_add_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 = polyveck_add
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)polyveck_add
22+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_add
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 = polyveck_add
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+
polyveck *a, *b, *c;
9+
polyveck_add(a, b, c);
10+
}

0 commit comments

Comments
 (0)