Skip to content

CBMC: Add proof and spec for poly_uniform_4x and poly_uniform_eta_4x #254

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 2 commits into from
May 21, 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
20 changes: 20 additions & 0 deletions mldsa/poly.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ void poly_uniform_4x(poly *vec,
buflen = STREAM128_BLOCKBYTES;
while (ctr[0] < MLDSA_N || ctr[1] < MLDSA_N || ctr[2] < MLDSA_N ||
ctr[3] < MLDSA_N)
__loop__(
assigns(ctr, state, memory_slice(vec, sizeof(poly) * 4), object_whole(buf))
invariant(ctr[0] <= MLDSA_N && ctr[1] <= MLDSA_N)
invariant(ctr[2] <= MLDSA_N && ctr[3] <= MLDSA_N)
invariant(array_bound(vec[0].coeffs, 0, ctr[0], 0, MLDSA_Q))
invariant(array_bound(vec[1].coeffs, 0, ctr[1], 0, MLDSA_Q))
invariant(array_bound(vec[2].coeffs, 0, ctr[2], 0, MLDSA_Q))
invariant(array_bound(vec[3].coeffs, 0, ctr[3], 0, MLDSA_Q)))
{
mld_xof128_x4_squeezeblocks(buf, 1, &state);
ctr[0] = rej_uniform(vec[0].coeffs, MLDSA_N, ctr[0], buf[0], buflen);
Expand Down Expand Up @@ -537,6 +545,18 @@ void poly_uniform_eta_4x(poly *r0, poly *r1, poly *r2, poly *r3,
buflen = STREAM256_BLOCKBYTES;
while (ctr[0] < MLDSA_N || ctr[1] < MLDSA_N || ctr[2] < MLDSA_N ||
ctr[3] < MLDSA_N)
__loop__(
assigns(ctr, state, memory_slice(r0, sizeof(poly)),
memory_slice(r1, sizeof(poly)), memory_slice(r2, sizeof(poly)),
memory_slice(r3, sizeof(poly)), object_whole(buf[0]),
object_whole(buf[1]), object_whole(buf[2]),
object_whole(buf[3]))
invariant(ctr[0] <= MLDSA_N && ctr[1] <= MLDSA_N)
invariant(ctr[2] <= MLDSA_N && ctr[3] <= MLDSA_N)
invariant(array_abs_bound(r0->coeffs, 0, ctr[0], MLDSA_ETA + 1))
invariant(array_abs_bound(r1->coeffs, 0, ctr[1], MLDSA_ETA + 1))
invariant(array_abs_bound(r2->coeffs, 0, ctr[2], MLDSA_ETA + 1))
invariant(array_abs_bound(r3->coeffs, 0, ctr[3], MLDSA_ETA + 1)))
{
mld_xof256_x4_squeezeblocks(buf, 1, &state);
ctr[0] = rej_eta(r0->coeffs, MLDSA_N, ctr[0], buf[0], buflen);
Expand Down
32 changes: 28 additions & 4 deletions mldsa/poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ __contract__(
assigns(memory_slice(a, sizeof(poly)))
ensures(array_bound(a->coeffs, 0, MLDSA_N, 0, MLDSA_Q))
);
#define poly_rej_uniform_4x MLD_NAMESPACE(poly_rej_uniform_4x)

#define poly_uniform_4x MLD_NAMESPACE(poly_uniform_4x)
/*************************************************
* Name: poly_uniform_x4
*
Expand All @@ -318,8 +319,16 @@ __contract__(
*
**************************************************/
void poly_uniform_4x(poly *vec,
uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)]);

uint8_t seed[4][MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)])
__contract__(
requires(memory_no_alias(vec, 4 * sizeof(poly)))
requires(memory_no_alias(seed, 4 * MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)))
assigns(memory_slice(vec, 4 * sizeof(poly)))
ensures(array_bound(vec[0].coeffs, 0, MLDSA_N, 0, MLDSA_Q))
ensures(array_bound(vec[1].coeffs, 0, MLDSA_N, 0, MLDSA_Q))
ensures(array_bound(vec[2].coeffs, 0, MLDSA_N, 0, MLDSA_Q))
ensures(array_bound(vec[3].coeffs, 0, MLDSA_N, 0, MLDSA_Q))
);

#define poly_uniform_eta_4x MLD_NAMESPACE(poly_uniform_eta_4x)
/*************************************************
Expand All @@ -342,7 +351,22 @@ void poly_uniform_4x(poly *vec,
**************************************************/
void poly_uniform_eta_4x(poly *r0, poly *r1, poly *r2, poly *r3,
const uint8_t seed[MLDSA_CRHBYTES], uint8_t nonce0,
uint8_t nonce1, uint8_t nonce2, uint8_t nonce3);
uint8_t nonce1, uint8_t nonce2, uint8_t nonce3)
__contract__(
requires(memory_no_alias(r0, sizeof(poly)))
requires(memory_no_alias(r1, sizeof(poly)))
requires(memory_no_alias(r2, sizeof(poly)))
requires(memory_no_alias(r3, sizeof(poly)))
requires(memory_no_alias(seed, MLDSA_CRHBYTES))
assigns(memory_slice(r0, sizeof(poly)))
assigns(memory_slice(r1, sizeof(poly)))
assigns(memory_slice(r2, sizeof(poly)))
assigns(memory_slice(r3, sizeof(poly)))
ensures(array_abs_bound(r0->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
ensures(array_abs_bound(r1->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
ensures(array_abs_bound(r2->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
ensures(array_abs_bound(r3->coeffs, 0, MLDSA_N, MLDSA_ETA + 1))
);

#define poly_uniform_gamma1 MLD_NAMESPACE(poly_uniform_gamma1)
/*************************************************
Expand Down
55 changes: 55 additions & 0 deletions proofs/cbmc/poly_uniform_4x/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) The mldsa-native project authors
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT

include ../Makefile_params.common

HARNESS_ENTRY = harness
HARNESS_FILE = poly_uniform_4x_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 = poly_uniform_4x

DEFINES +=
INCLUDES +=

REMOVE_FUNCTION_BODY +=
UNWINDSET +=

PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
PROJECT_SOURCES += $(SRCDIR)/mldsa/poly.c $(SRCDIR)/mldsa/fips202/fips202x4.c

CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform_4x
USE_FUNCTION_CONTRACTS=$(FIPS202_NAMESPACE)shake128x4_absorb_once $(FIPS202_NAMESPACE)shake128x4_squeezeblocks rej_uniform
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 = poly_uniform_4x

# 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 = 9

# 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
12 changes: 12 additions & 0 deletions proofs/cbmc/poly_uniform_4x/poly_uniform_4x_harness.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) The mldsa-native project authors
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT

#include "poly.h"

void harness(void)
{
poly *r0;
uint8_t(*seed)[MLD_ALIGN_UP(MLDSA_SEEDBYTES + 2)];

poly_uniform_4x(r0, seed);
}
55 changes: 55 additions & 0 deletions proofs/cbmc/poly_uniform_eta_4x/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) The mldsa-native project authors
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT

include ../Makefile_params.common

HARNESS_ENTRY = harness
HARNESS_FILE = poly_uniform_eta_4x_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 = poly_uniform_eta_4x

DEFINES +=
INCLUDES +=

REMOVE_FUNCTION_BODY +=
UNWINDSET +=

PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
PROJECT_SOURCES += $(SRCDIR)/mldsa/poly.c $(SRCDIR)/mldsa/fips202/fips202x4.c

CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_uniform_eta_4x
USE_FUNCTION_CONTRACTS=$(FIPS202_NAMESPACE)shake256x4_absorb_once $(FIPS202_NAMESPACE)shake256x4_squeezeblocks rej_eta
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 = poly_uniform_eta_4x

# 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 = 9

# 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
13 changes: 13 additions & 0 deletions proofs/cbmc/poly_uniform_eta_4x/poly_uniform_eta_4x_harness.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) The mldsa-native project authors
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT

#include "poly.h"

void harness(void)
{
poly *r0, *r1, *r2, *r3;
const uint8_t *seed;
uint8_t n0, n1, n2, n3;

poly_uniform_eta_4x(r0, r1, r2, r3, seed, n0, n1, n2, n3);
}