Skip to content

Commit b9efccf

Browse files
committed
CBMC: Add contract and proof for polyveck_sub
Resolves #127 Unrolling resulted in too poor performance. The direct proof works, but it requires to workaround the CBMC limitation described in: #102 diffblue/cbmc#8617 This also moves the correctness post-condition in poly_sub into a assert at the end of the function. This vastly improves performance. Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 426892b commit b9efccf

File tree

6 files changed

+90
-5
lines changed

6 files changed

+90
-5
lines changed

mldsa/poly.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ void poly_sub(poly *c, const poly *a, const poly *b)
5656
__loop__(
5757
invariant(i <= MLDSA_N)
5858
invariant(forall(k1, 0, i, c->coeffs[k1] == a->coeffs[k1] - b->coeffs[k1])))
59-
c->coeffs[i] = a->coeffs[i] - b->coeffs[i];
59+
{
60+
c->coeffs[i] = a->coeffs[i] - b->coeffs[i];
61+
}
62+
cassert(forall(k, 0, MLDSA_N, c->coeffs[k] == a->coeffs[k] - b->coeffs[k]));
6063
}
6164

6265
void poly_shiftl(poly *a)

mldsa/poly.h

-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ __contract__(
9191
requires(memory_no_alias(b, sizeof(poly)))
9292
requires(forall(k0, 0, MLDSA_N, (int64_t) a->coeffs[k0] - b->coeffs[k0] <= INT32_MAX))
9393
requires(forall(k1, 0, MLDSA_N, (int64_t) a->coeffs[k1] - b->coeffs[k1] >= INT32_MIN))
94-
ensures(forall(k, 0, MLDSA_N, c->coeffs[k] == a->coeffs[k] - b->coeffs[k]))
9594
assigns(memory_slice(c, sizeof(poly))));
9695

9796
#define poly_shiftl MLD_NAMESPACE(poly_shiftl)

mldsa/polyvec.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,16 @@ void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v)
206206
unsigned int i;
207207

208208
for (i = 0; i < MLDSA_K; ++i)
209+
__loop__(
210+
assigns(i, object_whole(w))
211+
invariant(i <= MLDSA_K))
209212
{
210-
poly_sub(&w->vec[i], &u->vec[i], &v->vec[i]);
213+
poly t;
214+
poly_sub(&t, &u->vec[i], &v->vec[i]);
215+
/* Full struct assignment from local variables to simplify proof */
216+
/* TODO: eliminate once CBMC resolves
217+
* https://github.com/diffblue/cbmc/issues/8617 */
218+
w->vec[i] = t;
211219
}
212220
}
213221

mldsa/polyvec.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ __contract__(
195195
assigns(memory_slice(w, sizeof(polyveck)))
196196
);
197197

198+
#define polyveck_sub MLD_NAMESPACE(polyveck_sub)
198199
/*************************************************
199200
* Name: polyveck_sub
200201
*
@@ -206,8 +207,18 @@ __contract__(
206207
* - const polyveck *v: pointer to second input vector to be
207208
* subtracted from first input vector
208209
**************************************************/
209-
#define polyveck_sub MLD_NAMESPACE(polyveck_sub)
210-
void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v);
210+
void polyveck_sub(polyveck *w, const polyveck *u, const polyveck *v)
211+
__contract__(
212+
requires(memory_no_alias(w, sizeof(polyveck)))
213+
requires(memory_no_alias(u, sizeof(polyveck)))
214+
requires(memory_no_alias(v, sizeof(polyveck)))
215+
requires(forall(k0, 0, MLDSA_K,
216+
forall(k1, 0, MLDSA_N, (int64_t) u->vec[k0].coeffs[k1] - v->vec[k0].coeffs[k1] <= INT32_MAX)))
217+
requires(forall(k2, 0, MLDSA_K,
218+
forall(k3, 0, MLDSA_N, (int64_t) u->vec[k2].coeffs[k3] - v->vec[k2].coeffs[k3] >= INT32_MIN)))
219+
assigns(memory_slice(w, sizeof(polyveck)))
220+
);
221+
211222
#define polyveck_shiftl MLD_NAMESPACE(polyveck_shiftl)
212223
/*************************************************
213224
* Name: polyveck_shiftl

proofs/cbmc/polyveck_sub/Makefile

+54
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_sub_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_sub
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_sub
22+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_sub
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_sub
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
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_sub(a, b, c);
10+
}

0 commit comments

Comments
 (0)