Skip to content

Commit 93297f8

Browse files
authored
Merge pull request #89 from pq-code-package/cbmc-power2round
CBMC: Add contract and proof for power2round
2 parents 1943707 + 1397c5a commit 93297f8

File tree

5 files changed

+98
-20
lines changed

5 files changed

+98
-20
lines changed

mldsa/poly.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void poly_power2round(poly *a1, poly *a0, const poly *a)
212212

213213
for (i = 0; i < MLDSA_N; ++i)
214214
{
215-
a1->coeffs[i] = power2round(&a0->coeffs[i], a->coeffs[i]);
215+
power2round(&a0->coeffs[i], &a1->coeffs[i], a->coeffs[i]);
216216
}
217217

218218
DBENCH_STOP(*tround);

mldsa/rounding.c

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,11 @@
66
#include <stdint.h>
77
#include "params.h"
88

9-
/*************************************************
10-
* Name: power2round
11-
*
12-
* Description: For finite field element a, compute a0, a1 such that
13-
* a mod^+ MLDSA_Q = a1*2^MLDSA_D + a0 with -2^{MLDSA_D-1} < a0 <=
14-
*2^{MLDSA_D-1}. Assumes a to be standard representative.
15-
*
16-
* Arguments: - int32_t a: input element
17-
* - int32_t *a0: pointer to output element a0
18-
*
19-
* Returns a1.
20-
**************************************************/
21-
int32_t power2round(int32_t *a0, int32_t a)
22-
{
23-
int32_t a1;
249

25-
a1 = (a + (1 << (MLDSA_D - 1)) - 1) >> MLDSA_D;
26-
*a0 = a - (a1 << MLDSA_D);
27-
return a1;
10+
void power2round(int32_t *a0, int32_t *a1, const int32_t a)
11+
{
12+
*a1 = (a + (1 << (MLDSA_D - 1)) - 1) >> MLDSA_D;
13+
*a0 = a - (*a1 << MLDSA_D);
2814
}
2915

3016
/*************************************************

mldsa/rounding.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,34 @@
99
#include "cbmc.h"
1010
#include "params.h"
1111

12+
#define MLD_2_POW_D (1 << MLDSA_D)
13+
1214
#define power2round MLD_NAMESPACE(power2round)
13-
int32_t power2round(int32_t *a0, int32_t a);
15+
/*************************************************
16+
* Name: power2round
17+
*
18+
* Description: For finite field element a, compute a0, a1 such that
19+
* a mod^+ MLDSA_Q = a1*2^MLDSA_D + a0 with -2^{MLDSA_D-1} < a0 <=
20+
* 2^{MLDSA_D-1}. Assumes a to be standard representative.
21+
*
22+
* Arguments: - int32_t a: input element
23+
* - int32_t *a0: pointer to output element a0
24+
* - int32_t *a1: pointer to output element a1
25+
*
26+
* Reference: a1 is passed as a return value instead
27+
**************************************************/
28+
void power2round(int32_t *a0, int32_t *a1, int32_t a)
29+
__contract__(
30+
requires(memory_no_alias(a0, sizeof(int32_t)))
31+
requires(memory_no_alias(a1, sizeof(int32_t)))
32+
requires(a >= 0 && a < MLDSA_Q)
33+
assigns(memory_slice(a0, sizeof(int32_t)))
34+
assigns(memory_slice(a1, sizeof(int32_t)))
35+
ensures(*a0 > -(MLD_2_POW_D/2) && *a0 <= (MLD_2_POW_D/2))
36+
ensures(*a1 >= 0 && *a1 <= (MLDSA_Q - 1) / MLD_2_POW_D)
37+
ensures((*a1 * MLD_2_POW_D + *a0 - a) % MLDSA_Q == 0)
38+
);
39+
1440

1541
#define decompose MLD_NAMESPACE(decompose)
1642
int32_t decompose(int32_t *a0, int32_t a);

proofs/cbmc/power2round/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 = power2round_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 = power2round
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/rounding.c
20+
21+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)power2round
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 = power2round
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2025 The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "rounding.h"
5+
6+
7+
void harness(void)
8+
{
9+
int32_t *a0, *a1;
10+
int32_t a;
11+
power2round(a0, a1, a);
12+
}

0 commit comments

Comments
 (0)