Skip to content

Commit 4608890

Browse files
willieyzmkannwischer
authored andcommitted
CBMC: Add proofs on top of native functions (poly_chknorm_native)
- This commit adds CBMC proofs for following the native function: `mld_poly_chknorm_native`, called `poly_chknorm_native`. - This following changes are include: - Apply the same harness file and construct function contract reference from `poly_chknorm_native`. - Use the following function contracts: - `mld_poly_chknorm_c` - `mld_poly_chknorm_native` - Add `MLD_USE_NATIVE_POLY_CHKNORM` in dummy_backend.h Signed-off-by: willieyz <willie.zhao@chelpis.com>
1 parent 015e66d commit 4608890

File tree

7 files changed

+85
-2
lines changed

7 files changed

+85
-2
lines changed

mldsa/mldsa_native.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@
552552
#undef MLD_NATIVE_FUNC_FALLBACK
553553
#undef MLD_NATIVE_FUNC_SUCCESS
554554
#undef MLD_NTT_BOUND
555+
#undef REDUCE32_RANGE_MAX
555556
/* mldsa/src/native/meta.h */
556557
#undef MLD_NATIVE_META_H
557558
#if defined(MLD_SYS_AARCH64)

mldsa/mldsa_native.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@
549549
#undef MLD_NATIVE_FUNC_FALLBACK
550550
#undef MLD_NATIVE_FUNC_SUCCESS
551551
#undef MLD_NTT_BOUND
552+
#undef REDUCE32_RANGE_MAX
552553
/* mldsa/src/native/meta.h */
553554
#undef MLD_NATIVE_META_H
554555
#if defined(MLD_SYS_AARCH64)

mldsa/src/native/api.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
* in sync. */
5151
#define MLD_INTT_BOUND MLDSA_Q
5252

53+
/* Absolute bound for range of mld_reduce32()
54+
*
55+
* NOTE: This is the same bound as in reduce.h and has to be kept
56+
* in sync. */
57+
/* check-magic: 6283009 == (REDUCE32_DOMAIN_MAX - 255 * MLDSA_Q + 1) */
58+
#define REDUCE32_RANGE_MAX 6283009
5359
/*
5460
* This is the C<->native interface allowing for the drop-in of
5561
* native code for performance critical arithmetic components of ML-DSA.
@@ -396,7 +402,14 @@ __contract__(
396402
* Returns 0 if the infinity norm is strictly smaller than B, and 1
397403
* otherwise. B must not be larger than MLDSA_Q - REDUCE32_RANGE_MAX.
398404
**************************************************/
399-
static MLD_INLINE int mld_poly_chknorm_native(const int32_t *a, int32_t B);
405+
static MLD_INLINE int mld_poly_chknorm_native(const int32_t *a, int32_t B)
406+
__contract__(
407+
requires(memory_no_alias(a, sizeof(int32_t) * MLDSA_N))
408+
requires(0 <= B && B <= MLDSA_Q - REDUCE32_RANGE_MAX)
409+
requires(array_bound(a, 0, MLDSA_N, -REDUCE32_RANGE_MAX, REDUCE32_RANGE_MAX))
410+
ensures(return_value == MLD_NATIVE_FUNC_FALLBACK || return_value == MLD_NATIVE_FUNC_SUCCESS)
411+
ensures((return_value == 0) == array_abs_bound(a, 0, MLDSA_N, B))
412+
);
400413
#endif /* MLD_USE_NATIVE_POLY_CHKNORM */
401414

402415
#if defined(MLD_USE_NATIVE_POLYZ_UNPACK_17)

mldsa/src/poly.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,6 @@ MLD_INTERNAL_API
703703
uint32_t mld_poly_chknorm(const mld_poly *a, int32_t B)
704704
{
705705
#if defined(MLD_USE_NATIVE_POLY_CHKNORM)
706-
/* TODO: proof */
707706
int ret;
708707
int success;
709708
mld_assert_bound(a->coeffs, MLDSA_N, -REDUCE32_RANGE_MAX, REDUCE32_RANGE_MAX);

proofs/cbmc/dummy_backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define MLD_USE_NATIVE_POLY_CADDQ
1818
#define MLD_USE_NATIVE_POLY_USE_HINT_32
1919
#define MLD_USE_NATIVE_POLY_USE_HINT_88
20+
#define MLD_USE_NATIVE_POLY_CHKNORM
2021

2122
#include "../../mldsa/src/native/api.h"
2223

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) The mldsa-native project authors
2+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
include ../Makefile_params.common
5+
6+
HARNESS_ENTRY = harness
7+
HARNESS_FILE =poly_chknorm_native_harness
8+
9+
# This should be a unique identifier for this proof, and will appear on the
10+
# Litani dashboard. It can be human-readable and contain spaces if you wish.
11+
PROOF_UID = poly_chknorm_native
12+
13+
DEFINES += -DMLD_CONFIG_USE_NATIVE_BACKEND_ARITH -DMLD_CONFIG_ARITH_BACKEND_FILE="\"dummy_backend.h\""
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mldsa/src/poly.c
21+
22+
CHECK_FUNCTION_CONTRACTS=$(MLD_NAMESPACE)poly_chknorm
23+
USE_FUNCTION_CONTRACTS=mld_poly_chknorm_native mld_poly_chknorm_c
24+
APPLY_LOOP_CONTRACTS=on
25+
USE_DYNAMIC_FRAMES=1
26+
27+
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead
28+
EXTERNAL_SAT_SOLVER=
29+
CBMCFLAGS=--smt2
30+
31+
FUNCTION_NAME = poly_chknorm_native
32+
33+
# If this proof is found to consume huge amounts of RAM, you can set the
34+
# EXPENSIVE variable. With new enough versions of the proof tools, this will
35+
# restrict the number of EXPENSIVE CBMC jobs running at once. See the
36+
# documentation in Makefile.common under the "Job Pools" heading for details.
37+
# EXPENSIVE = true
38+
39+
# This function is large enough to need...
40+
CBMC_OBJECT_BITS = 8
41+
42+
# If you require access to a file-local ("static") function or object to conduct
43+
# your proof, set the following (and do not include the original source file
44+
# ("mldsa/poly.c") in PROJECT_SOURCES).
45+
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i
46+
# include ../Makefile.common
47+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mldsa/src/poly.c
48+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_FUNCTIONS = foo bar
49+
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_OBJECTS = baz
50+
# Care is required with variables on the left-hand side: REWRITTEN_SOURCES must
51+
# be set before including Makefile.common, but any use of variables on the
52+
# left-hand side requires those variables to be defined. Hence, _SOURCE,
53+
# _FUNCTIONS, _OBJECTS is set after including Makefile.common.
54+
55+
include ../Makefile.common
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) The mldsa-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include "poly_kl.h"
5+
6+
7+
void harness(void)
8+
{
9+
mld_poly *a;
10+
uint32_t r;
11+
int32_t B;
12+
r = mld_poly_chknorm(a, B);
13+
}

0 commit comments

Comments
 (0)