Skip to content

Commit bf1df5b

Browse files
committed
Add proofs of ML-KEM-Braid supporting functions.
Adds proof Makefiles and harnesses for serialize_epp deserialize_epp serialize_polyvec_16le deserialize_polyvec_16le Adds and correct contracts for those functions in kem.c Signed-off-by: Rod Chapman <rodchap@amazon.com>
1 parent aad4f6f commit bf1df5b

9 files changed

Lines changed: 254 additions & 3 deletions

File tree

mlkem/src/kem.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ int mlk_kem_enc(uint8_t ct[MLKEM_INDCCA_CIPHERTEXTBYTES],
380380
* Coefficients in [-ETA2, ETA2] are stored as (ETA2 - x), fitting in 4 bits.
381381
* Two coefficients per byte (low nibble first). */
382382
static void mlk_serialize_epp(uint8_t out[MLKEM_EPP_BYTES], const mlk_poly *p)
383+
__contract__(
384+
requires(memory_no_alias(out, MLKEM_EPP_BYTES))
385+
requires(memory_no_alias(p, sizeof(mlk_poly)))
386+
requires(array_abs_bound(p->coeffs, 0, MLKEM_N, MLKEM_ETA2 + 1))
387+
assigns(memory_slice(out, MLKEM_EPP_BYTES))
388+
)
383389
{
384390
unsigned j;
385391
for (j = 0; j < MLKEM_N / 2; j++)
@@ -395,6 +401,11 @@ static void mlk_serialize_epp(uint8_t out[MLKEM_EPP_BYTES], const mlk_poly *p)
395401
}
396402

397403
static void mlk_deserialize_epp(mlk_poly *p, const uint8_t in[MLKEM_EPP_BYTES])
404+
__contract__(
405+
requires(memory_no_alias(in, MLKEM_EPP_BYTES))
406+
requires(memory_no_alias(p, sizeof(mlk_poly)))
407+
assigns(memory_slice(p, sizeof(mlk_poly)))
408+
)
398409
{
399410
unsigned j;
400411
for (j = 0; j < MLKEM_N / 2; j++)
@@ -415,6 +426,11 @@ static void mlk_deserialize_epp(mlk_poly *p, const uint8_t in[MLKEM_EPP_BYTES])
415426
* Coefficients must be non-negative (e.g., after reduce). */
416427
static void mlk_serialize_polyvec_16le(uint8_t out[MLKEM_POLYVEC16_BYTES],
417428
const mlk_polyvec *v)
429+
__contract__(
430+
requires(memory_no_alias(out, MLKEM_POLYVEC16_BYTES))
431+
requires(memory_no_alias(v, sizeof(mlk_polyvec)))
432+
assigns(memory_slice(out, MLKEM_POLYVEC16_BYTES))
433+
)
418434
{
419435
unsigned i, j;
420436
for (i = 0; i < MLKEM_K; i++)
@@ -427,9 +443,9 @@ static void mlk_serialize_polyvec_16le(uint8_t out[MLKEM_POLYVEC16_BYTES],
427443
__loop__(
428444
assigns(j, memory_slice(out, MLKEM_POLYVEC16_BYTES))
429445
invariant(j <= MLKEM_N)
430-
decreases(MLKEM_K - j))
446+
decreases(MLKEM_N - j))
431447
{
432-
uint16_t c = (uint16_t)v->vec[i].coeffs[j];
448+
uint16_t c = mlk_cast_int16_to_uint16(v->vec[i].coeffs[j]);
433449
out[i * MLKEM_POLY16_BYTES + 2 * j] = (uint8_t)(c & 0xFF);
434450
out[i * MLKEM_POLY16_BYTES + 2 * j + 1] = (uint8_t)(c >> 8);
435451
}
@@ -438,6 +454,11 @@ static void mlk_serialize_polyvec_16le(uint8_t out[MLKEM_POLYVEC16_BYTES],
438454

439455
static void mlk_deserialize_polyvec_16le(
440456
mlk_polyvec *v, const uint8_t in[MLKEM_POLYVEC16_BYTES])
457+
__contract__(
458+
requires(memory_no_alias(in, MLKEM_POLYVEC16_BYTES))
459+
requires(memory_no_alias(v, sizeof(mlk_polyvec)))
460+
assigns(memory_slice(v, sizeof(mlk_polyvec)))
461+
)
441462
{
442463
unsigned i, j;
443464
for (i = 0; i < MLKEM_K; i++)
@@ -450,7 +471,7 @@ static void mlk_deserialize_polyvec_16le(
450471
__loop__(
451472
assigns(j, memory_slice(v, sizeof(mlk_polyvec)))
452473
invariant(j <= MLKEM_N)
453-
decreases(MLKEM_K - j))
474+
decreases(MLKEM_N - j))
454475
{
455476
v->vec[i].coeffs[j] = mlk_cast_uint16_to_int16(
456477
(uint16_t)((unsigned)in[i * MLKEM_POLY16_BYTES + 2 * j] |
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) The mlkem-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 = deserialize_epp_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 = mlk_deserialize_epp
12+
13+
DEFINES += -DMLK_CONFIG_ENABLE_MLKEM_BRAID
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/kem.c
21+
22+
CHECK_FUNCTION_CONTRACTS=mlk_deserialize_epp
23+
USE_FUNCTION_CONTRACTS =
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 = mlk_deserialize_epp
32+
33+
# This function is large enough to need...
34+
CBMC_OBJECT_BITS = 12
35+
36+
include ../Makefile.common
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) The mlkem-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include <kem.h>
5+
#include <poly.h>
6+
7+
#define mlk_deserialize_epp MLK_NAMESPACE(deserialize_epp)
8+
void mlk_deserialize_epp(mlk_poly *p, const uint8_t in[MLKEM_EPP_BYTES]);
9+
10+
void harness(void)
11+
{
12+
{
13+
/* Dummy use of `free` to work around CBMC issue #8814. */
14+
free(NULL);
15+
}
16+
17+
uint8_t *in;
18+
mlk_poly *p;
19+
20+
mlk_deserialize_epp(p, in);
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) The mlkem-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 = deserialize_polyvec_16le_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 = mlk_deserialize_polyvec_16le
12+
13+
DEFINES += -DMLK_CONFIG_ENABLE_MLKEM_BRAID
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/kem.c
21+
22+
CHECK_FUNCTION_CONTRACTS=mlk_deserialize_polyvec_16le
23+
USE_FUNCTION_CONTRACTS =
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 = mlk_deserialize_polyvec_16le
32+
33+
# This function is large enough to need...
34+
CBMC_OBJECT_BITS = 12
35+
36+
include ../Makefile.common
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) The mlkem-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include <kem.h>
5+
#include <poly_k.h>
6+
7+
#define mlk_deserialize_polyvec_16le MLK_NAMESPACE(deserialize_polyvec_16le)
8+
void mlk_deserialize_polyvec_16le(mlk_polyvec *v,
9+
const uint8_t in[MLKEM_POLYVEC16_BYTES]);
10+
11+
void harness(void)
12+
{
13+
{
14+
/* Dummy use of `free` to work around CBMC issue #8814. */
15+
free(NULL);
16+
}
17+
18+
uint8_t *in;
19+
mlk_polyvec *v;
20+
21+
mlk_deserialize_polyvec_16le(v, in);
22+
}

proofs/cbmc/serialize_epp/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) The mlkem-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 = serialize_epp_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 = mlk_serialize_epp
12+
13+
DEFINES += -DMLK_CONFIG_ENABLE_MLKEM_BRAID
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/kem.c
21+
22+
CHECK_FUNCTION_CONTRACTS=mlk_serialize_epp
23+
USE_FUNCTION_CONTRACTS =
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 = mlk_serialize_epp
32+
33+
# This function is large enough to need...
34+
CBMC_OBJECT_BITS = 12
35+
36+
include ../Makefile.common
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) The mlkem-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include <kem.h>
5+
#include <poly.h>
6+
7+
#define mlk_serialize_epp MLK_NAMESPACE(serialize_epp)
8+
void mlk_serialize_epp(uint8_t out[MLKEM_EPP_BYTES], const mlk_poly *p);
9+
10+
void harness(void)
11+
{
12+
{
13+
/* Dummy use of `free` to work around CBMC issue #8814. */
14+
free(NULL);
15+
}
16+
17+
uint8_t *out;
18+
mlk_poly *p;
19+
20+
mlk_serialize_epp(out, p);
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) The mlkem-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 = serialize_polyvec_16le_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 = mlk_serialize_polyvec_16le
12+
13+
DEFINES += -DMLK_CONFIG_ENABLE_MLKEM_BRAID
14+
INCLUDES +=
15+
16+
REMOVE_FUNCTION_BODY +=
17+
UNWINDSET +=
18+
19+
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
20+
PROJECT_SOURCES += $(SRCDIR)/mlkem/src/kem.c
21+
22+
CHECK_FUNCTION_CONTRACTS=mlk_serialize_polyvec_16le
23+
USE_FUNCTION_CONTRACTS =
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 = mlk_serialize_polyvec_16le
32+
33+
# This function is large enough to need...
34+
CBMC_OBJECT_BITS = 12
35+
36+
include ../Makefile.common
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) The mlkem-native project authors
2+
// SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
3+
4+
#include <kem.h>
5+
#include <poly_k.h>
6+
7+
#define mlk_serialize_polyvec_16le MLK_NAMESPACE(serialize_polyvec_16le)
8+
void mlk_serialize_polyvec_16le(uint8_t out[MLKEM_POLYVEC16_BYTES],
9+
const mlk_polyvec *v);
10+
11+
void harness(void)
12+
{
13+
{
14+
/* Dummy use of `free` to work around CBMC issue #8814. */
15+
free(NULL);
16+
}
17+
18+
uint8_t *out;
19+
mlk_polyvec *v;
20+
21+
mlk_serialize_polyvec_16le(out, v);
22+
}

0 commit comments

Comments
 (0)