Skip to content

Commit 61a2040

Browse files
mkannwischerhanno-becker
authored andcommitted
CBMC: Add contract and proof for polyveck_use_hint
Resolves #135 Unrolling resulted in too poor performance. The direct proof works, but it requires to workaround the CBMC limitation described in: #102 diffblue/cbmc#8617 Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 13c50f5 commit 61a2040

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

mldsa/polyvec.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,15 @@ void polyveck_use_hint(polyveck *w, const polyveck *u, const polyveck *h)
354354
unsigned int i;
355355

356356
for (i = 0; i < MLDSA_K; ++i)
357+
__loop__(
358+
invariant(i <= MLDSA_K))
357359
{
358-
poly_use_hint(&w->vec[i], &u->vec[i], &h->vec[i]);
360+
poly t;
361+
poly_use_hint(&t, &u->vec[i], &h->vec[i]);
362+
/* Full struct assignment from local variables to simplify proof */
363+
/* TODO: eliminate once CBMC resolves
364+
* https://github.com/diffblue/cbmc/issues/8617 */
365+
w->vec[i] = t;
359366
}
360367
}
361368

mldsa/polyvec.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,19 @@ __contract__(
336336
* - const polyveck *u: pointer to input vector
337337
* - const polyveck *h: pointer to input hint vector
338338
**************************************************/
339-
void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h);
339+
void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h)
340+
__contract__(
341+
requires(memory_no_alias(w, sizeof(polyveck)))
342+
requires(memory_no_alias(v, sizeof(polyveck)))
343+
requires(memory_no_alias(h, sizeof(polyveck)))
344+
requires(forall(k0, 0, MLDSA_K,
345+
array_bound(v->vec[k0].coeffs, 0, MLDSA_N, 0, MLDSA_Q)))
346+
requires(forall(k1, 0, MLDSA_K,
347+
array_bound(h->vec[k1].coeffs, 0, MLDSA_N, 0, 2)))
348+
assigns(memory_slice(w, sizeof(polyveck)))
349+
requires(forall(k2, 0, MLDSA_K,
350+
array_bound(w->vec[k2].coeffs, 0, MLDSA_N, 0, (MLDSA_Q-1)/(2*MLDSA_GAMMA2))))
351+
);
340352

341353
#define polyveck_pack_w1 MLD_NAMESPACE(polyveck_pack_w1)
342354
/*************************************************
+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_use_hint_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_use_hint
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_use_hint
22+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_use_hint
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_use_hint
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_use_hint(a, b, c);
10+
}

0 commit comments

Comments
 (0)