Skip to content

Commit efedf87

Browse files
committed
CBMC: Add contract and proof for poly_chknorm
Resolves #109 The explicit check for the range of B is removed as this is a compile-time constant in all call sites anyway. Instead, we can just add it as pre-condition to the CBMC contract. Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 9c8f5b9 commit efedf87

File tree

5 files changed

+90
-9
lines changed

5 files changed

+90
-9
lines changed

mldsa/cbmc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
((qvar_lb) <= (qvar) && (qvar) < (qvar_ub)) ==> (predicate) \
8585
}
8686

87-
#define EXISTS(qvar, qvar_lb, qvar_ub, predicate) \
87+
#define exists(qvar, qvar_lb, qvar_ub, predicate) \
8888
__CPROVER_exists \
8989
{ \
9090
unsigned qvar; \

mldsa/poly.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,38 @@ void poly_use_hint(poly *b, const poly *a, const poly *h)
156156
}
157157
}
158158

159+
/* Reference: explicitly checks the bound B to be <= (MLDSA_Q - 1) / 8).
160+
* This is unnecessary as it's always a compile-time constant.
161+
* We instead model it as a precondition.
162+
*/
159163
int poly_chknorm(const poly *a, int32_t B)
160164
{
161165
unsigned int i;
166+
int rc = 0;
162167
int32_t t;
163168

164-
if (B > (MLDSA_Q - 1) / 8)
165-
{
166-
return 1;
167-
}
168-
169169
/* It is ok to leak which coefficient violates the bound since
170170
the probability for each coefficient is independent of secret
171171
data but we must not leak the sign of the centralized representative. */
172+
172173
for (i = 0; i < MLDSA_N; ++i)
174+
__loop__(
175+
invariant(i <= MLDSA_N)
176+
invariant(rc == 0 || rc == 1)
177+
invariant((rc == 0) == array_abs_bound(a->coeffs, 0, i, B))
178+
)
173179
{
174180
/* Absolute value */
175181
t = a->coeffs[i] >> 31;
176182
t = a->coeffs[i] - (t & 2 * a->coeffs[i]);
177183

178184
if (t >= B)
179185
{
180-
return 1;
186+
rc = 1;
181187
}
182188
}
183189

184-
return 0;
190+
return rc;
185191
}
186192

187193
/*************************************************

mldsa/poly.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,15 @@ __contract__(
271271
* Returns 0 if norm is strictly smaller than B <= (MLDSA_Q-1)/8 and 1
272272
*otherwise.
273273
**************************************************/
274-
int poly_chknorm(const poly *a, int32_t B);
274+
int poly_chknorm(const poly *a, int32_t B)
275+
__contract__(
276+
requires(memory_no_alias(a, sizeof(poly)))
277+
requires(0 <= B && B <= (MLDSA_Q - 1) / 8)
278+
requires(array_bound(a->coeffs, 0, MLDSA_N, -REDUCE_RANGE_MAX, REDUCE_RANGE_MAX))
279+
ensures(return_value == 0 || return_value == 1)
280+
ensures((return_value == 0) == array_abs_bound(a->coeffs, 0, MLDSA_N, B))
281+
);
282+
275283

276284
#define poly_uniform MLD_NAMESPACE(poly_uniform)
277285
/*************************************************

proofs/cbmc/poly_chknorm/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 =poly_chknorm_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 = poly_chknorm
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/poly.c
20+
21+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_chknorm
22+
USE_FUNCTION_CONTRACTS=
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 = poly_chknorm
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2025 The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "poly.h"
5+
6+
7+
void harness(void)
8+
{
9+
poly *a;
10+
int r;
11+
int32_t B;
12+
r = poly_chknorm(a, B);
13+
}

0 commit comments

Comments
 (0)