Skip to content

Commit b0f47c9

Browse files
committed
CBMC: Add contract and proof for polyveck_pack_w1
Resolves #136 Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent f45ad10 commit b0f47c9

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

mldsa/poly.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,13 @@ __contract__(
523523
requires(memory_no_alias(r, MLDSA_POLYW1_PACKEDBYTES))
524524
requires(memory_no_alias(a, sizeof(poly)))
525525
requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 16))
526-
assigns(object_whole(r)));
526+
assigns(memory_slice(r, MLDSA_POLYW1_PACKEDBYTES)));
527527
#elif MLDSA_GAMMA2 == (MLDSA_Q - 1) / 88
528528
__contract__(
529529
requires(memory_no_alias(r, MLDSA_POLYW1_PACKEDBYTES))
530530
requires(memory_no_alias(a, sizeof(poly)))
531531
requires(array_bound(a->coeffs, 0, MLDSA_N, 0, 44))
532-
assigns(object_whole(r)));
532+
assigns(memory_slice(r, MLDSA_POLYW1_PACKEDBYTES)));
533533
#else
534534
#error "Invalid value of MLDSA_GAMMA2"
535535
#endif

mldsa/polyvec.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,35 @@ __contract__(
372372
void polyveck_use_hint(polyveck *w, const polyveck *v, const polyveck *h);
373373

374374
#define polyveck_pack_w1 MLD_NAMESPACE(polyveck_pack_w1)
375+
/*************************************************
376+
* Name: polyveck_pack_w1
377+
*
378+
* Description: Bit-pack polynomial vector w1 with coefficients in [0,15] or
379+
* [0,43].
380+
* Input coefficients are assumed to be standard representatives.
381+
*
382+
* Arguments: - uint8_t *r: pointer to output byte array with at least
383+
* MLDSA_K* MLDSA_POLYW1_PACKEDBYTES bytes
384+
* - const polyveck *a: pointer to input polynomial vector
385+
**************************************************/
375386
void polyveck_pack_w1(uint8_t r[MLDSA_K * MLDSA_POLYW1_PACKEDBYTES],
376-
const polyveck *w1);
387+
const polyveck *w1)
388+
#if MLDSA_MODE == 2
389+
__contract__(
390+
requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
391+
requires(memory_no_alias(w1, sizeof(polyveck)))
392+
requires(forall(k1, 0, MLDSA_K,
393+
array_bound(w1->vec[k1].coeffs, 0, MLDSA_N, 0, 44)))
394+
assigns(object_whole(r)));
395+
#else
396+
__contract__(
397+
requires(memory_no_alias(r, MLDSA_K * MLDSA_POLYW1_PACKEDBYTES))
398+
requires(memory_no_alias(w1, sizeof(polyveck)))
399+
requires(forall(k1, 0, MLDSA_K,
400+
array_bound(w1->vec[k1].coeffs, 0, MLDSA_N, 0, 16)))
401+
assigns(object_whole(r)));
402+
#endif
403+
377404

378405
#define polyveck_pack_eta MLD_NAMESPACE(polyveck_pack_eta)
379406
void polyveck_pack_eta(uint8_t r[MLDSA_K * MLDSA_POLYETA_PACKEDBYTES],

proofs/cbmc/polyveck_pack_w1/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 = polyveck_pack_w1_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_pack_w1
11+
12+
DEFINES +=
13+
INCLUDES +=
14+
15+
REMOVE_FUNCTION_BODY +=
16+
UNWINDSET += $(MLD_NAMESPACE)polyveck_pack_w1.0:8 # Largest value of MLDSA_K
17+
18+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
19+
PROJECT_SOURCES += $(SRCDIR)/mldsa/polyvec.c
20+
21+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyveck_pack_w1
22+
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyw1_pack
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_pack_w1
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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;
9+
uint8_t *b;
10+
polyveck_pack_w1(b, a);
11+
}

0 commit comments

Comments
 (0)