-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CBMC: Add contracts to native backend
So far, the CBMC proofs covered the combination of C frontend and C backend. While most native backend functions are drop-in replacements of functions in the default C backend and should thus inherit their specification, there are minor differences in signature (in particular since #675) or functionality (e.g. the native implementation of rej_uniform may reject certain inputs, and the frontend uses fallback logic which is not present with the C backend) that merit explicit contract annotations for all functions in the arithmetic and FIPS202 backend, and proofs against those specs. This commit takes a step in this direction by adding CBMC contracts for all functions in the native arithmetic backend, except for `rej_uniform_native`, and proving that their call-sites still uphold their spec when the respective MLKEM_USE_NATIVE_XXX option is set. This is largely trivial, but it is still worth doing for (a) documentation purposes, and since (b) the native backends operate on raw arrays, while their call-sites operate in terms of the `poly_xxx` structs. The case of `rej_uniform` is more complicated and will be handled separately. Signed-off-by: Hanno Becker <[email protected]>
- Loading branch information
1 parent
68a82c6
commit 9ebf93e
Showing
31 changed files
with
753 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2024 The mlkem-native project authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifdef MLKEM_NATIVE_ARITH_PROFILE_H | ||
#error Only one MLKEM_ARITH assembly profile can be defined -- did you include multiple profiles? | ||
#else | ||
#define MLKEM_NATIVE_ARITH_PROFILE_H | ||
|
||
#define MLKEM_NATIVE_ARITH_BACKEND_NAME DUMMY_BACKEND | ||
|
||
/* Filename of the C backend implementation. | ||
* This is not inlined here because this header is included in assembly | ||
* files as well. */ | ||
#define MLKEM_NATIVE_ARITH_BACKEND_IMPL "dummy_backend_impl.h" | ||
|
||
#endif /* MLKEM_NATIVE_ARITH_PROFILE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2024 The mlkem-native project authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* ML-KEM arithmetic native profile for clean assembly */ | ||
|
||
#ifdef MLKEM_NATIVE_ARITH_PROFILE_IMPL_H | ||
#error Only one MLKEM_ARITH assembly profile can be defined -- did you include multiple profiles? | ||
#else | ||
#define MLKEM_NATIVE_ARITH_PROFILE_IMPL_H | ||
|
||
#define MLKEM_USE_NATIVE_REJ_UNIFORM | ||
#define MLKEM_USE_NATIVE_NTT | ||
#define MLKEM_USE_NATIVE_INTT | ||
#define MLKEM_USE_NATIVE_POLY_REDUCE | ||
#define MLKEM_USE_NATIVE_POLY_TOMONT | ||
#define MLKEM_USE_NATIVE_POLYVEC_BASEMUL_ACC_MONTGOMERY_CACHED | ||
#define MLKEM_USE_NATIVE_POLY_MULCACHE_COMPUTE | ||
#define MLKEM_USE_NATIVE_POLY_TOBYTES | ||
#define MLKEM_USE_NATIVE_POLY_FROMBYTES | ||
#define MLKEM_USE_NATIVE_NTT_CUSTOM_ORDER | ||
|
||
#include "../mlkem/native/api.h" | ||
|
||
#endif /* MLKEM_NATIVE_ARITH_PROFILE_IMPL_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = gen_matrix_native_order_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 = gen_matrix_native_order | ||
|
||
DEFINES += -DMLKEM_USE_NATIVE_BACKEND_ARITH -DMLKEM_NATIVE_ARITH_BACKEND_FILE="\"dummy_backend.h\"" | ||
INCLUDES += | ||
|
||
REMOVE_FUNCTION_BODY += | ||
UNWINDSET += $(MLKEM_NAMESPACE)gen_matrix.0:4 $(MLKEM_NAMESPACE)gen_matrix.1:4 $(MLKEM_NAMESPACE)gen_matrix.2:4 $(MLKEM_NAMESPACE)gen_matrix.3:4 $(MLKEM_NAMESPACE)gen_matrix.4:4 | ||
|
||
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c | ||
PROJECT_SOURCES += $(SRCDIR)/mlkem/indcpa.c | ||
|
||
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)gen_matrix | ||
USE_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_rej_uniform $(MLKEM_NAMESPACE)poly_rej_uniform_x4 poly_permute_bitrev_to_custom | ||
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 = $(MLKEM_NAMESPACE)gen_matrix_native_order | ||
|
||
# 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 = 10 | ||
|
||
# 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 | ||
# ("mlkem/poly.c") in PROJECT_SOURCES). | ||
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i | ||
# include ../Makefile.common | ||
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mlkem/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This file marks this directory as containing a CBMC proof. |
14 changes: 14 additions & 0 deletions
14
proofs/cbmc/gen_matrix_native_order/gen_matrix_native_order_harness.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2024 The mlkem-native project authors | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT-0 | ||
|
||
#include <stdint.h> | ||
#include "indcpa.h" | ||
|
||
void harness(void) | ||
{ | ||
polyvec *a; | ||
uint8_t *seed; | ||
int transposed; | ||
gen_matrix(a, seed, transposed); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = poly_frombytes_native_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_frombytes_native | ||
|
||
DEFINES += -DMLKEM_USE_NATIVE_BACKEND_ARITH -DMLKEM_NATIVE_ARITH_BACKEND_FILE="\"dummy_backend.h\"" | ||
INCLUDES += | ||
|
||
REMOVE_FUNCTION_BODY += | ||
UNWINDSET += | ||
|
||
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c | ||
PROJECT_SOURCES += $(SRCDIR)/mlkem/compress.c | ||
|
||
CHECK_FUNCTION_CONTRACTS=$(MLKEM_NAMESPACE)poly_frombytes | ||
USE_FUNCTION_CONTRACTS=poly_frombytes_native | ||
APPLY_LOOP_CONTRACTS=on | ||
USE_DYNAMIC_FRAMES=1 | ||
|
||
# Disable any setting of EXTERNAL_SAT_SOLVER, and choose SMT backend instead | ||
EXTERNAL_SAT_SOLVER= | ||
CBMCFLAGS=--bitwuzla | ||
|
||
FUNCTION_NAME = poly_frombytes_native | ||
|
||
# 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 | ||
# ("mlkem/poly.c") in PROJECT_SOURCES). | ||
# REWRITTEN_SOURCES = $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i | ||
# include ../Makefile.common | ||
# $(PROOFDIR)/<__SOURCE_FILE_BASENAME__>.i_SOURCE = $(SRCDIR)/mlkem/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This file marks this directory as containing a CBMC proof. |
14 changes: 14 additions & 0 deletions
14
proofs/cbmc/poly_frombytes_native/poly_frombytes_native_harness.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2024 The mlkem-native project authors | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT-0 | ||
|
||
#include "compress.h" | ||
|
||
void harness(void) | ||
{ | ||
poly *a; | ||
uint8_t *r; | ||
|
||
/* Contracts for this function are in poly.h */ | ||
poly_frombytes(a, r); | ||
} |
Oops, something went wrong.