Skip to content

Commit 87209d3

Browse files
committed
Examples: Add monolithic_build_multilevel example
This ports the monolithic_build_multilevel from mlkem-native. This examples demonstrated how to build multiple instances of mldsa-native in a single compilation unit Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent a4de303 commit 87209d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1966
-0
lines changed

BIBLIOGRAPHY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ source code and documentation.
1717
- [examples/monolithic_build/config_44.h](examples/monolithic_build/config_44.h)
1818
- [examples/monolithic_build/config_65.h](examples/monolithic_build/config_65.h)
1919
- [examples/monolithic_build/config_87.h](examples/monolithic_build/config_87.h)
20+
- [examples/monolithic_build_multilevel/multilevel_config.h](examples/monolithic_build_multilevel/multilevel_config.h)
2021
- [integration/liboqs/config_aarch64.h](integration/liboqs/config_aarch64.h)
2122
- [integration/liboqs/config_c.h](integration/liboqs/config_c.h)
2223
- [integration/liboqs/config_x86_64.h](integration/liboqs/config_x86_64.h)
@@ -51,6 +52,7 @@ source code and documentation.
5152
- [examples/monolithic_build/config_44.h](examples/monolithic_build/config_44.h)
5253
- [examples/monolithic_build/config_65.h](examples/monolithic_build/config_65.h)
5354
- [examples/monolithic_build/config_87.h](examples/monolithic_build/config_87.h)
55+
- [examples/monolithic_build_multilevel/multilevel_config.h](examples/monolithic_build_multilevel/multilevel_config.h)
5456
- [mldsa/mldsa_native.h](mldsa/mldsa_native.h)
5557
- [mldsa/src/config.h](mldsa/src/config.h)
5658
- [mldsa/src/ct.h](mldsa/src/ct.h)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
2+
3+
build/
4+
*.d
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright (c) The mlkem-native project authors
2+
# Copyright (c) The mldsa-native project authors
3+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4+
5+
.PHONY: build run clean
6+
.DEFAULT_GOAL := all
7+
8+
CC ?= gcc
9+
AR ?= ar
10+
11+
# Adjust CFLAGS if needed
12+
CFLAGS := \
13+
-Wall \
14+
-Wextra \
15+
-Werror=unused-result \
16+
-Wpedantic \
17+
-Werror \
18+
-Wmissing-prototypes \
19+
-Wshadow \
20+
-Wpointer-arith \
21+
-Wredundant-decls \
22+
-Wconversion \
23+
-Wsign-conversion \
24+
-Wno-long-long \
25+
-Wno-unknown-pragmas \
26+
-Wno-unused-command-line-argument \
27+
-O3 \
28+
-fomit-frame-pointer \
29+
-std=c99 \
30+
-pedantic \
31+
-MMD \
32+
$(CFLAGS)
33+
34+
35+
# The following only concerns the cross-compilation tests.
36+
# You can likely ignore the following for your application.
37+
#
38+
# Append cross-prefix for cross compilation
39+
# When called from the root Makefile, CROSS_PREFIX has already been added here
40+
ifeq (,$(findstring $(CROSS_PREFIX),$(CC)))
41+
CC := $(CROSS_PREFIX)$(CC)
42+
endif
43+
44+
ifeq (,$(findstring $(CROSS_PREFIX),$(AR)))
45+
AR := $(CROSS_PREFIX)$(AR)
46+
endif
47+
48+
# Part A:
49+
#
50+
# mldsa-native source and header files
51+
#
52+
# Here, we use just a single monolithic compilation unit to include
53+
# multiple instances of mldsa-native.
54+
55+
MLD_SOURCE=mldsa_native_all.c
56+
57+
INC=-Imldsa/ -I./
58+
59+
# Part B:
60+
#
61+
# Random number generator
62+
#
63+
# !!! WARNING !!!
64+
#
65+
# The randombytes() implementation used here is for TESTING ONLY.
66+
# You MUST NOT use this implementation outside of testing.
67+
#
68+
# !!! WARNING !!!
69+
RNG_SOURCE=$(wildcard test_only_rng/*.c)
70+
71+
# Part C:
72+
#
73+
# Your application source code
74+
APP_SOURCE=$(RNG_SOURCE) main.c
75+
76+
BUILD_DIR=build
77+
BIN=test_binary
78+
LIB=libmldsa.a
79+
80+
BINARY_NAME_FULL=$(BUILD_DIR)/$(BIN)
81+
LIB_NAME_FULL=$(BUILD_DIR)/$(LIB)
82+
83+
$(LIB_NAME_FULL): $(MLD_SOURCE)
84+
echo "$@"
85+
mkdir -p $(BUILD_DIR)
86+
$(CC) -c $(CFLAGS) $(INC) $^ -o $(BUILD_DIR)/mldsa_native.o
87+
$(AR) rcs $@ $(BUILD_DIR)/mldsa_native.o
88+
strip -S $@
89+
90+
$(BINARY_NAME_FULL): $(APP_SOURCE) $(LIB_NAME_FULL)
91+
echo "$@"
92+
mkdir -p $(BUILD_DIR)
93+
$(CC) $(CFLAGS) $(INC) $^ -o $@
94+
95+
all: build
96+
97+
build: $(BINARY_NAME_FULL)
98+
99+
run: $(BINARY_NAME_FULL)
100+
$(EXEC_WRAPPER) ./$(BINARY_NAME_FULL)
101+
102+
clean:
103+
rm -rf $(BUILD_DIR)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
2+
3+
# Multi-level mldsa-native in a single compilation unit
4+
5+
This directory contains a minimal example for how to build multiple instances of mldsa-native in a single compilation
6+
unit. Only the C-backend is exercised.
7+
8+
The auto-generated source file [mldsa_native.c](mldsa/mldsa_native.c) includes all mldsa-native C source
9+
files. Moreover, it clears all `#define`s clauses set by mldsa-native at the end, and is hence amenable to multiple
10+
inclusion in another compilation unit.
11+
12+
The manually written source file [mldsa_native_all.c](mldsa_native_all.c) includes
13+
[mldsa_native.c](mldsa/mldsa_native.c) three times, each time using the fixed config
14+
[multilevel_config.h](multilevel_config.h), but changing the security level (specified
15+
by `MLD_CONFIG_PARAMETER_SET`) every time.
16+
```C
17+
#define MLD_CONFIG_FILE "multilevel_config.h"
18+
19+
/* Three instances of mldsa-native for all security levels */
20+
21+
/* Include level-independent code */
22+
#define MLD_CONFIG_MULTILEVEL_WITH_SHARED
23+
/* Keep level-independent headers at the end of monobuild file */
24+
#define MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS
25+
#define MLD_CONFIG_PARAMETER_SET 44
26+
#include "mldsa_native.c"
27+
#undef MLD_CONFIG_PARAMETER_SET
28+
#undef MLD_CONFIG_MULTILEVEL_WITH_SHARED
29+
30+
/* Exclude level-independent code */
31+
#define MLD_CONFIG_MULTILEVEL_NO_SHARED
32+
#define MLD_CONFIG_PARAMETER_SET 65
33+
#include "mldsa_native.c"
34+
#undef MLD_CONFIG_PARAMETER_SET
35+
/* `#undef` all headers at the and of the monobuild file */
36+
#undef MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS
37+
38+
#define MLD_CONFIG_PARAMETER_SET 87
39+
#include "mldsa_native.c"
40+
#undef MLD_CONFIG_PARAMETER_SET
41+
```
42+
43+
Note the setting `MLD_CONFIG_MULTILEVEL_WITH_SHARED` which forces the inclusion of all level-independent
44+
code in the MLDSA-44 build, and the setting `MLD_CONFIG_MULTILEVEL_NO_SHARED`, which drops all
45+
level-independent code in the subsequent builds. Finally, `MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS` entails that
46+
`mldsa_native.c` does not `#undefine` the `#define` clauses from level-independent files.
47+
48+
To make the monolithic multi-level build accessible from the application source [main.c](main.c), we provide
49+
[mldsa_native_all.h](mldsa_native_all.h), which includes [mldsa_native.h](../../mldsa/mldsa_native.h) once per
50+
configuration. Note that we don't refer to the configuration using `MLD_CONFIG_FILE`, but by setting
51+
`MLD_CONFIG_API_XXX` explicitly. Otherwise, [mldsa_native.h](../../mldsa/mldsa_native.h) would include the confg, which
52+
would lead to name-clashes upon multiple use.
53+
54+
```C
55+
#define MLD_CONFIG_API_NO_SUPERCOP
56+
57+
/* API for MLDSA-44 */
58+
#define MLD_CONFIG_API_PARAMETER_SET 44
59+
#define MLD_CONFIG_API_NAMESPACE_PREFIX mldsa44
60+
#include <mldsa_native.h>
61+
#undef MLD_CONFIG_API_PARAMETER_SET
62+
#undef MLD_CONFIG_API_NAMESPACE_PREFIX
63+
#undef MLD_H
64+
65+
/* API for MLDSA-65*/
66+
#define MLD_CONFIG_API_PARAMETER_SET 65
67+
#define MLD_CONFIG_API_NAMESPACE_PREFIX mldsa65
68+
#include <mldsa_native.h>
69+
#undef MLD_CONFIG_API_PARAMETER_SET
70+
#undef MLD_CONFIG_API_NAMESPACE_PREFIX
71+
#undef MLD_H
72+
73+
/* API for MLDSA_87 */
74+
#define MLD_CONFIG_API_PARAMETER_SET 87
75+
#define MLD_CONFIG_API_NAMESPACE_PREFIX mldsa87
76+
#include <mldsa_native.h>
77+
#undef MLD_CONFIG_API_PARAMETER_SET
78+
#undef MLD_CONFIG_API_NAMESPACE_PREFIX
79+
#undef MLD_H
80+
```
81+
82+
## Usage
83+
84+
Build this example with `make build`, run with `make run`.
85+
86+
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
87+
outside of testing.

0 commit comments

Comments
 (0)