Skip to content

Commit 7adc7e8

Browse files
committed
CBMC: Add spec + proof for polyvec_basemul_acc_montgomery_cached
The proof is less trivial than the ones before since one has to use the output bounds of the lower level functions to show that the accumulation loop does not overflow. Signed-off-by: Hanno Becker <[email protected]>
1 parent a028910 commit 7adc7e8

File tree

5 files changed

+141
-9
lines changed

5 files changed

+141
-9
lines changed
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 = polyvec_basemul_acc_montgomery_cached_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 = polyvec_basemul_acc_montgomery_cached
11+
12+
DEFINES +=
13+
INCLUDES +=
14+
15+
REMOVE_FUNCTION_BODY +=
16+
UNWINDSET += $(MLKEM_NAMESPACE)polyvec_basemul_acc_montgomery_cached.0:4 # Largest value of MLKEM_K
17+
18+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
19+
PROJECT_SOURCES += $(SRCDIR)/mlkem/polyvec.c
20+
21+
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)polyvec_basemul_acc_montgomery_cached
22+
USE_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_basemul_montgomery_cached $(MLKEM_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 = polyvec_basemul_acc_montgomery_cached
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+
# ("mlkem/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)/mlkem/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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# This file marks this directory as containing a CBMC proof.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0 AND Apache-2.0
3+
4+
/*
5+
* Insert copyright notice
6+
*/
7+
8+
/**
9+
* @file polyvec_basemul_acc_montgomery_cached_harness.c
10+
* @brief Implements the proof harness for basemul_cached function.
11+
*/
12+
#include "polyvec.h"
13+
14+
/*
15+
* Insert project header files that
16+
* - include the declaration of the function
17+
* - include the types needed to declare function arguments
18+
*/
19+
20+
/**
21+
* @brief Starting point for formal analysis
22+
*
23+
*/
24+
void harness(void) {
25+
poly r;
26+
polyvec a, b;
27+
polyvec_mulcache b_cached;
28+
29+
polyvec_basemul_acc_montgomery_cached(&r, &a, &b, &b_cached);
30+
}

mlkem/polyvec.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,35 @@ void polyvec_basemul_acc_montgomery_cached(poly *r, const polyvec *a,
226226
POLYVEC_BOUND(b, NTT_BOUND);
227227
POLYVEC_BOUND(b_cache, MLKEM_Q);
228228

229-
unsigned int i;
229+
int i;
230230
poly t;
231231

232232
poly_basemul_montgomery_cached(r, &a->vec[0], &b->vec[0], &b_cache->vec[0]);
233-
for (i = 1; i < MLKEM_K; i++) {
234-
poly_basemul_montgomery_cached(&t, &a->vec[i], &b->vec[i],
235-
&b_cache->vec[i]);
236-
poly_add(r, r, &t);
237-
// abs bounds: < (i+1) * 3/2 * q
238-
}
239233

240-
// abs bounds: < MLKEM_K * 3/2 * q <= 4 * 3/2 * q = 19974
234+
for (i = 1; i < MLKEM_K; i++)
235+
// clang-format off
236+
ASSIGNS(i, t, OBJECT_WHOLE(r))
237+
INVARIANT(i >= 1 && i <= MLKEM_K)
238+
INVARIANT(ARRAY_IN_BOUNDS(int, k, 0, MLKEM_N - 1, r->coeffs, \
239+
i * (-3 * HALF_Q + 1), i * (3 * HALF_Q - 1)))
240+
DECREASES(MLKEM_K - i)
241+
// clang-format on
242+
{
243+
poly_basemul_montgomery_cached(&t, &a->vec[i], &b->vec[i],
244+
&b_cache->vec[i]);
245+
poly_add(r, r, &t);
246+
// abs bounds: < (i+1) * 3/2 * q
247+
}
248+
249+
// Those bounds are true for the C implementation, but not needed
250+
// in the higher level bounds reasoning. It is thus best to omit
251+
// them from the spec to not unnecessarily constraint native implementations.
252+
ASSERT(
253+
ARRAY_IN_BOUNDS(int, k, 0, MLKEM_N - 1, r->coeffs,
254+
MLKEM_K * (-3 * HALF_Q + 1), MLKEM_K * (3 * HALF_Q - 1)),
255+
"polyvec_basemul_acc_montgomery_cached output bounds");
256+
// TODO: Integrate CBMC assertin into POLY_BOUND if CBMC is set
257+
POLY_BOUND(r, MLKEM_K * 3 * HALF_Q);
241258
}
242259
#else /* !MLKEM_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED */
243260
void polyvec_basemul_acc_montgomery_cached(poly *r, const polyvec *a,

mlkem/polyvec.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,37 @@ void polyvec_basemul_acc_montgomery(poly *r, const polyvec *a,
4040
// REF-CHANGE: This function does not exist in the reference implementation
4141
#define polyvec_basemul_acc_montgomery_cached \
4242
MLKEM_NAMESPACE(polyvec_basemul_acc_montgomery_cached)
43+
/*************************************************
44+
* Name: polyvec_basemul_acc_montgomery_cached
45+
*
46+
* Description: Scalar product of two vectors of polynomials in NTT domain,
47+
* using mulcache for second operand.
48+
*
49+
* Bounds:
50+
* - a is assumed to be coefficient-wise < q in absolute value.
51+
* - No bounds guarantees for the coefficients in the result.
52+
*
53+
* Arguments: - poly *r: pointer to output polynomial
54+
* - const polyvec *a: pointer to first input polynomial vector
55+
* - const polyvec *b: pointer to second input polynomial vector
56+
* - const polyvec_mulcache *b_cache: pointer to mulcache
57+
* for second input polynomial vector. Can be computed
58+
* via polyvec_mulcache_compute().
59+
**************************************************/
4360
void polyvec_basemul_acc_montgomery_cached(poly *r, const polyvec *a,
4461
const polyvec *b,
45-
const polyvec_mulcache *b_cache);
62+
const polyvec_mulcache *b_cache)
63+
// clang-format off
64+
REQUIRES(r != NULL && a != NULL && b != NULL && b_cache != NULL)
65+
REQUIRES(IS_FRESH(r, sizeof(poly)))
66+
REQUIRES(IS_FRESH(a, sizeof(polyvec)) && IS_FRESH(b, sizeof(polyvec)))
67+
REQUIRES(IS_FRESH(b_cache, sizeof(polyvec_mulcache)))
68+
// Input is coefficient-wise < q in absolute value
69+
REQUIRES(FORALL(int, k1, 0, MLKEM_K - 1, \
70+
ARRAY_IN_BOUNDS(int, k2, 0, MLKEM_N - 1, \
71+
a->vec[k1].coeffs, -(MLKEM_Q - 1), (MLKEM_Q - 1))))
72+
ASSIGNS(OBJECT_WHOLE(r));
73+
// clang-format on
4674

4775
// REF-CHANGE: This function does not exist in the reference implementation
4876
#define polyvec_mulcache_compute MLKEM_NAMESPACE(polyvec_mulcache_compute)

0 commit comments

Comments
 (0)