Skip to content

Commit bf47bd0

Browse files
mkannwischerrod-chapman
authored andcommitted
CBMC: Add proof and spec for polyvec_matrix_expand
Resolves #137 Signed-off-by: willieyz <[email protected]> Correction to assigns() contracts following review. Also move local variable declaration inside scope of inner loop and remove from that loop's assigns contract. Signed-off-by: Rod Chapman <[email protected]>
1 parent 2f5f24a commit bf47bd0

File tree

4 files changed

+126
-10
lines changed

4 files changed

+126
-10
lines changed

mldsa/polyvec.c

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,36 @@ void polyvec_matrix_expand(polyvecl mat[MLDSA_K],
2323
MLD_ALIGN uint8_t seed_ext[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)];
2424

2525
for (j = 0; j < 4; j++)
26+
__loop__(
27+
assigns(j, object_whole(seed_ext))
28+
invariant(j <= 4)
29+
)
2630
{
2731
memcpy(seed_ext[j], rho, MLDSA_SEEDBYTES);
2832
}
29-
3033
/* Sample 4 matrix entries a time. */
3134
for (i = 0; i < (MLDSA_K * MLDSA_L / 4) * 4; i += 4)
35+
__loop__(
36+
assigns(i, j, object_whole(seed_ext), memory_slice(mat, MLDSA_K * sizeof(polyvecl)))
37+
invariant(i <= (MLDSA_K * MLDSA_L / 4) * 4 && i % 4 == 0)
38+
/* vectors 0 .. i / MLDSA_L are completely sampled */
39+
invariant(forall(k1, 0, i / MLDSA_L, forall(l1, 0, MLDSA_L,
40+
array_bound(mat[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
41+
/* last vector is sampled up to i % MLDSA_L */
42+
invariant(forall(k2, i / MLDSA_L, i / MLDSA_L + 1, forall(l2, 0, i % MLDSA_L,
43+
array_bound(mat[k2].vec[l2].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
44+
)
3245
{
33-
uint8_t x, y;
46+
poly tmpvec[4];
3447

3548
for (j = 0; j < 4; j++)
49+
__loop__(
50+
assigns(j, object_whole(seed_ext))
51+
invariant(j <= 4)
52+
)
3653
{
37-
x = (i + j) / MLDSA_L;
38-
y = (i + j) % MLDSA_L;
54+
uint8_t x = (i + j) / MLDSA_L;
55+
uint8_t y = (i + j) % MLDSA_L;
3956

4057
seed_ext[j][MLDSA_SEEDBYTES + 0] = y;
4158
seed_ext[j][MLDSA_SEEDBYTES + 1] = x;
@@ -48,20 +65,43 @@ void polyvec_matrix_expand(polyvecl mat[MLDSA_K],
4865
* does not introduce any padding here. Need to refactor the data type to
4966
* polymat as in mlkem-native.
5067
*/
51-
poly_uniform_4x(&mat[(i) / MLDSA_L].vec[(i) % MLDSA_L], seed_ext);
68+
69+
/* Full struct assignment from local variables to simplify proof */
70+
/* TODO: eliminate once CBMC resolves
71+
* https://github.com/diffblue/cbmc/issues/8617 */
72+
poly_uniform_4x(tmpvec, seed_ext);
73+
mat[i / MLDSA_L].vec[i % MLDSA_L] = tmpvec[0];
74+
mat[(i + 1) / MLDSA_L].vec[(i + 1) % MLDSA_L] = tmpvec[1];
75+
mat[(i + 2) / MLDSA_L].vec[(i + 2) % MLDSA_L] = tmpvec[2];
76+
mat[(i + 3) / MLDSA_L].vec[(i + 3) % MLDSA_L] = tmpvec[3];
5277
}
5378

5479
/* For MLDSA_K=6, MLDSA_L=5, process the last two entries individually */
5580
while (i < MLDSA_K * MLDSA_L)
81+
__loop__(
82+
assigns(i, object_whole(seed_ext), memory_slice(mat, MLDSA_K * sizeof(polyvecl)))
83+
invariant(i <= MLDSA_K * MLDSA_L)
84+
/* vectors 0 .. i / MLDSA_L are completely sampled */
85+
invariant(forall(k1, 0, i / MLDSA_L, forall(l1, 0, MLDSA_L,
86+
array_bound(mat[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
87+
/* last vector is sampled up to i % MLDSA_L */
88+
invariant(forall(k2, i / MLDSA_L, i / MLDSA_L + 1, forall(l2, 0, i % MLDSA_L,
89+
array_bound(mat[k2].vec[l2].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
90+
)
5691
{
57-
uint8_t x, y;
58-
x = i / MLDSA_L;
59-
y = i % MLDSA_L;
92+
poly tmp;
93+
uint8_t x = i / MLDSA_L;
94+
uint8_t y = i % MLDSA_L;
6095

6196
seed_ext[0][MLDSA_SEEDBYTES + 0] = y;
6297
seed_ext[0][MLDSA_SEEDBYTES + 1] = x;
6398

64-
poly_uniform(&mat[i / MLDSA_L].vec[i % MLDSA_L], seed_ext[0]);
99+
100+
/* Full struct assignment from local variables to simplify proof */
101+
/* TODO: eliminate once CBMC resolves
102+
* https://github.com/diffblue/cbmc/issues/8617 */
103+
poly_uniform(&tmp, seed_ext[0]);
104+
mat[i / MLDSA_L].vec[i % MLDSA_L] = tmp;
65105

66106
i++;
67107
}

mldsa/polyvec.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,16 @@ __contract__(
606606
* - const uint8_t rho[]: byte array containing seed rho
607607
**************************************************/
608608
void polyvec_matrix_expand(polyvecl mat[MLDSA_K],
609-
const uint8_t rho[MLDSA_SEEDBYTES]);
609+
const uint8_t rho[MLDSA_SEEDBYTES])
610+
__contract__(
611+
requires(memory_no_alias(mat, MLDSA_K * sizeof(polyvecl)))
612+
requires(memory_no_alias(rho, MLDSA_SEEDBYTES))
613+
assigns(memory_slice(mat, MLDSA_K * sizeof(polyvecl)))
614+
ensures(forall(k1, 0, MLDSA_K, forall(l1, 0, MLDSA_L,
615+
array_bound(mat[k1].vec[l1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))))
616+
);
617+
618+
610619

611620
#define polyvec_matrix_pointwise_montgomery \
612621
MLD_NAMESPACE(polyvec_matrix_pointwise_montgomery)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) The mldsa-native project authors
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
include ../Makefile_params.common
5+
6+
HARNESS_ENTRY = harness
7+
HARNESS_FILE = polyvec_matrix_expand_harness
8+
9+
# This should be a unique identifier for this proof, and will appear on the
10+
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11+
PROOF_UID = polyvec_matrix_expand
12+
13+
DEFINES +=
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mldsa/polyvec.c
21+
22+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyvec_matrix_expand
23+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform_4x $(MLD_NAMESPACE)poly_uniform
24+
APPLY_LOOP_CONTRACTS=on
25+
USE_DYNAMIC_FRAMES=1
26+
27+
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
28+
EXTERNAL_SAT_SOLVER=
29+
CBMCFLAGS=--smt2
30+
31+
FUNCTION_NAME = polyvec_matrix_expand
32+
33+
# If this proof is found to consume huge amounts of RAM, you can set the
34+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
35+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
36+
# documentation in Makefile.common under the "Job Pools" heading for details.
37+
# EXPENSIVE = true
38+
39+
# This function is large enough to need...
40+
CBMC_OBJECT_BITS = 8
41+
42+
# If you require access to a file-local ("static") function or object to conduct
43+
# your proof, set the following (and do not include the original source file
44+
# ("mldsa/poly.c") in PROJECT_SOURCES).
45+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
46+
# include ../Makefile.common
47+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/poly.c
48+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
49+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
50+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
51+
# be set before including Makefile.common, but any use of variables on the
52+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
53+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
54+
55+
include ../Makefile.common
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include "polyvec.h"
5+
6+
void harness(void)
7+
{
8+
polyvecl *mat;
9+
uint8_t *rho;
10+
11+
polyvec_matrix_expand(mat, rho);
12+
}

0 commit comments

Comments
 (0)