Skip to content

CBMC: Add contract and proof for polyvecl_invntt_tomont #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion mldsa/polyvec.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,17 @@ void polyvecl_invntt_tomont(polyvecl *v)
unsigned int i;

for (i = 0; i < MLDSA_L; ++i)
__loop__(
invariant(i <= MLDSA_L)
invariant(forall(k0, i, MLDSA_L, forall(k1, 0, MLDSA_N, v->vec[k0].coeffs[k1] == loop_entry(*v).vec[k0].coeffs[k1])))
invariant(forall(k1, 0, i, array_abs_bound(v->vec[k1].coeffs, 0, MLDSA_N, MLDSA_Q))))
{
poly_invntt_tomont(&v->vec[i]);
poly t = v->vec[i];
poly_invntt_tomont(&t);
/* Full struct assignment from local variables to simplify proof */
/* TODO: eliminate once CBMC resolves
* https://github.com/diffblue/cbmc/issues/8617 */
v->vec[i] = t;
}
}

Expand Down
17 changes: 16 additions & 1 deletion mldsa/polyvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,22 @@ __contract__(
);

#define polyvecl_invntt_tomont MLD_NAMESPACE(polyvecl_invntt_tomont)
void polyvecl_invntt_tomont(polyvecl *v);
/*************************************************
* Name: polyvecl_invntt_tomont
*
* Description: Inplace inverse NTT and multiplication by 2^{32}.
* Input coefficients need to be less than MLDSA_Q in absolute
* value and output coefficients are again bounded by MLDSA_Q.
*
* Arguments: - polyvecl *v: pointer to input/output vector
**************************************************/
void polyvecl_invntt_tomont(polyvecl *v)
__contract__(
requires(memory_no_alias(v, sizeof(polyvecl)))
requires(forall(k0, 0, MLDSA_L, array_abs_bound(v->vec[k0].coeffs, 0, MLDSA_N, MLDSA_Q)))
assigns(memory_slice(v, sizeof(polyvecl)))
ensures(forall(k1, 0, MLDSA_L, array_abs_bound(v->vec[k1].coeffs, 0 , MLDSA_N, MLDSA_Q)))
);

#define polyvecl_pointwise_poly_montgomery \
MLD_NAMESPACE(polyvecl_pointwise_poly_montgomery)
Expand Down
54 changes: 54 additions & 0 deletions proofs/cbmc/polyvecl_invntt_tomont/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# SPDX-License-Identifier: Apache-2.0

include ../Makefile_params.common

HARNESS_ENTRY = harness
HARNESS_FILE = polyvecl_invntt_tomont_harness

# This should be a unique identifier for this proof, and will appear on the
# Litani dashboard. It can be human-readable and contain spaces if you wish.
PROOF_UID = polyvecl_invntt_tomont

DEFINES +=
INCLUDES +=

REMOVE_FUNCTION_BODY +=
UNWINDSET +=

PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
PROJECT_SOURCES += $(SRCDIR)/mldsa/polyvec.c

CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)polyvecl_invntt_tomont
USE_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_invntt_tomont
APPLY_LOOP_CONTRACTS=on
USE_DYNAMIC_FRAMES=1

# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
EXTERNAL_SAT_SOLVER=
CBMCFLAGS=--smt2

FUNCTION_NAME = polyvecl_invntt_tomont

# If this proof is found to consume huge amounts of RAM, you can set the
# EXPENSIVE variable. With new enough versions of the proof tools, this will
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
# documentation in Makefile.common under the "Job Pools" heading for details.
# EXPENSIVE = true

# This function is large enough to need...
CBMC_OBJECT_BITS = 8

# If you require access to a file-local ("static") function or object to conduct
# your proof, set the following (and do not include the original source file
# ("mldsa/poly.c") in PROJECT_SOURCES).
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
# include ../Makefile.common
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/poly.c
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
# be set before including Makefile.common, but any use of variables on the
# left-hand side requires those variables to be defined. Hence, _SOURCE,
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.

include ../Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2025 The mldsa-native project authors
// SPDX-License-Identifier: Apache-2.0

#include <stdint.h>
#include "params.h"
#include "polyvec.h"

void harness(void)
{
polyvecl *v;
polyvecl_invntt_tomont(v);
}
Loading