Skip to content

Commit b1b71c1

Browse files
manastasovamkannwischer
authored andcommitted
Add spec and proof for rej_eta
Signed-off-by: manastasova <[email protected]>
1 parent 93297f8 commit b1b71c1

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

mldsa/poly.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,37 @@ void poly_uniform(poly *a, const uint8_t seed[MLDSA_SEEDBYTES], uint16_t nonce)
435435
* Returns number of sampled coefficients. Can be smaller than len if not enough
436436
* random bytes were given.
437437
**************************************************/
438+
#if MLDSA_ETA == 2
439+
#define POLY_UNIFORM_ETA_NBLOCKS \
440+
((136 + STREAM256_BLOCKBYTES - 1) / STREAM256_BLOCKBYTES)
441+
#elif MLDSA_ETA == 4
442+
#define POLY_UNIFORM_ETA_NBLOCKS \
443+
((227 + STREAM256_BLOCKBYTES - 1) / STREAM256_BLOCKBYTES)
444+
#else
445+
#error "Invalid value of MLDSA_ETA"
446+
#endif
438447
static unsigned int rej_eta(int32_t *a, unsigned int len, const uint8_t *buf,
439448
unsigned int buflen)
449+
__contract__(
450+
requires(len <= buflen && len <= MLDSA_N && \
451+
buflen <= (POLY_UNIFORM_ETA_NBLOCKS * STREAM256_BLOCKBYTES))
452+
requires(memory_no_alias(a, sizeof(int32_t) * len))
453+
requires(memory_no_alias(buf, buflen))
454+
assigns(memory_slice(a, sizeof(int32_t) * len))
455+
ensures(return_value <= len)
456+
ensures(array_abs_bound(a, 0, return_value, MLDSA_ETA + 1))
457+
)
440458
{
441459
unsigned int ctr, pos;
442-
uint32_t t0, t1;
460+
int32_t t0, t1;
443461
DBENCH_START();
444462

445463
ctr = pos = 0;
446464
while (ctr < len && pos < buflen)
465+
__loop__(
466+
invariant(0 <= ctr && ctr <= len && pos <= buflen)
467+
invariant(ctr > 0 ==> array_abs_bound(a, 0, ctr, MLDSA_ETA + 1))
468+
)
447469
{
448470
t0 = buf[pos] & 0x0F;
449471
t1 = buf[pos++] >> 4;
@@ -489,15 +511,6 @@ static unsigned int rej_eta(int32_t *a, unsigned int len, const uint8_t *buf,
489511
*MLDSA_CRHBYTES
490512
* - uint16_t nonce: 2-byte nonce
491513
**************************************************/
492-
#if MLDSA_ETA == 2
493-
#define POLY_UNIFORM_ETA_NBLOCKS \
494-
((136 + STREAM256_BLOCKBYTES - 1) / STREAM256_BLOCKBYTES)
495-
#elif MLDSA_ETA == 4
496-
#define POLY_UNIFORM_ETA_NBLOCKS \
497-
((227 + STREAM256_BLOCKBYTES - 1) / STREAM256_BLOCKBYTES)
498-
#else
499-
#error "Invalid value of MLDSA_ETA"
500-
#endif
501514
void poly_uniform_eta(poly *a, const uint8_t seed[MLDSA_CRHBYTES],
502515
uint16_t nonce)
503516
{

proofs/cbmc/rej_eta/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 = rej_eta_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 = rej_eta
11+
12+
DEFINES +=
13+
INCLUDES +=
14+
15+
REMOVE_FUNCTION_BODY +=
16+
UNWINDSET +=
17+
18+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
19+
PROJECT_SOURCES += $(SRCDIR)/mldsa/poly.c
20+
21+
CHECK_FUNCTION_CONTRACTS=rej_eta
22+
USE_FUNCTION_CONTRACTS=
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 = rej_eta
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2025 The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "poly.h"
5+
6+
unsigned rej_eta(int32_t *a, unsigned int len, const uint8_t *buf,
7+
unsigned int buflen);
8+
9+
void harness(void)
10+
{
11+
int32_t *a;
12+
unsigned int len;
13+
const uint8_t *buf;
14+
unsigned int buflen;
15+
16+
rej_eta(a, len, buf, buflen);
17+
}

0 commit comments

Comments
 (0)