Skip to content

Commit a4de303

Browse files
committed
Examples: Add monolithic build (single level) example
This commit adds a new example to mldsa-native ported from mlkem-native. It demonstrated how to build a single instance of mldsa-native in a single compilation unit. Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 7190db9 commit a4de303

Some content is hidden

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

43 files changed

+1659
-5
lines changed

BIBLIOGRAPHY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ source code and documentation.
1414
- National Institute of Standards and Technology
1515
* URL: https://csrc.nist.gov/projects/cryptographic-module-validation-program/fips-140-3-ig-announcements
1616
* Referenced from:
17+
- [examples/monolithic_build/config_44.h](examples/monolithic_build/config_44.h)
18+
- [examples/monolithic_build/config_65.h](examples/monolithic_build/config_65.h)
19+
- [examples/monolithic_build/config_87.h](examples/monolithic_build/config_87.h)
1720
- [integration/liboqs/config_aarch64.h](integration/liboqs/config_aarch64.h)
1821
- [integration/liboqs/config_c.h](integration/liboqs/config_c.h)
1922
- [integration/liboqs/config_x86_64.h](integration/liboqs/config_x86_64.h)
@@ -45,6 +48,9 @@ source code and documentation.
4548
* URL: https://csrc.nist.gov/pubs/fips/204/final
4649
* Referenced from:
4750
- [README.md](README.md)
51+
- [examples/monolithic_build/config_44.h](examples/monolithic_build/config_44.h)
52+
- [examples/monolithic_build/config_65.h](examples/monolithic_build/config_65.h)
53+
- [examples/monolithic_build/config_87.h](examples/monolithic_build/config_87.h)
4854
- [mldsa/mldsa_native.h](mldsa/mldsa_native.h)
4955
- [mldsa/src/config.h](mldsa/src/config.h)
5056
- [mldsa/src/ct.h](mldsa/src/ct.h)

examples/basic/expected_signatures.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* The PCT modifies the PRNG state, so the KAT tests don't work.
1616
* We run KAT tests only for disabled PCT. */
1717
#if !defined(MLD_CONFIG_KEYGEN_PCT)
18-
#if MLD_CONFIG_PARAMETER_SET == 44
18+
#if MLD_CONFIG_API_PARAMETER_SET == 44
1919
const uint8_t expected_signature[] = {
2020
0x27, 0x5e, 0xbe, 0x2d, 0x23, 0x1a, 0x76, 0xc5, 0xd9, 0x77, 0xcb, 0x62,
2121
0x25, 0x04, 0xdb, 0x23, 0x31, 0xa9, 0xa8, 0xcd, 0xbc, 0xde, 0xf2, 0x20,
@@ -219,7 +219,7 @@ const uint8_t expected_signature[] = {
219219
0x1f, 0x24, 0x38, 0x41, 0x45, 0x54, 0x5f, 0x69, 0x85, 0x8d, 0x93, 0x9e,
220220
0xc5, 0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221221
0x00, 0x00, 0x00, 0x00, 0x16, 0x25, 0x32, 0x42};
222-
#elif MLD_CONFIG_PARAMETER_SET == 65
222+
#elif MLD_CONFIG_API_PARAMETER_SET == 65
223223
const uint8_t expected_signature[] = {
224224
0x8b, 0x6f, 0x79, 0x00, 0xcc, 0x79, 0x57, 0xee, 0x16, 0x86, 0x87, 0xd5,
225225
0xcf, 0xb6, 0x90, 0x2c, 0xc6, 0x30, 0xeb, 0x8d, 0x39, 0xae, 0x9b, 0xf1,
@@ -497,7 +497,7 @@ const uint8_t expected_signature[] = {
497497
0xdf, 0x0e, 0x1a, 0x20, 0x50, 0xf5, 0x02, 0x21, 0x30, 0x35, 0x3c, 0x60,
498498
0x71, 0xad, 0xd0, 0xe3, 0xed, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499499
0x00, 0x00, 0x00, 0x0c, 0x10, 0x17, 0x1d, 0x22, 0x2d};
500-
#elif MLD_CONFIG_PARAMETER_SET == 87
500+
#elif MLD_CONFIG_API_PARAMETER_SET == 87
501501
const uint8_t expected_signature[] = {
502502
0xdf, 0xe6, 0xe1, 0xa5, 0x20, 0xc1, 0xac, 0x98, 0x55, 0x2c, 0xf2, 0x13,
503503
0x65, 0x42, 0xe2, 0xb0, 0x77, 0x5f, 0x15, 0x00, 0x8f, 0x14, 0x48, 0xa1,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
2+
3+
build

examples/monolithic_build/Makefile

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
# The following only concerns the cross-compilation tests.
35+
# You can likely ignore the following for your application.
36+
#
37+
# Append cross-prefix for cross compilation
38+
# When called from the root Makefile, CROSS_PREFIX has already been added here
39+
ifeq (,$(findstring $(CROSS_PREFIX),$(CC)))
40+
CC := $(CROSS_PREFIX)$(CC)
41+
endif
42+
43+
ifeq (,$(findstring $(CROSS_PREFIX),$(AR)))
44+
AR := $(CROSS_PREFIX)$(AR)
45+
endif
46+
47+
Q ?= @
48+
49+
# Part A:
50+
#
51+
# mldsa-native source and header files
52+
#
53+
# Here, we use just a single monolithic compilation unit to include
54+
# multiple instances of mldsa-native.
55+
56+
MLD_SOURCE=mldsa/mldsa_native.c
57+
58+
INC=-Imldsa/ -I./
59+
60+
# Part B:
61+
#
62+
# Random number generator
63+
#
64+
# !!! WARNING !!!
65+
#
66+
# The randombytes() implementation used here is for TESTING ONLY.
67+
# You MUST NOT use this implementation outside of testing.
68+
#
69+
# !!! WARNING !!!
70+
RNG_SOURCE=$(wildcard test_only_rng/*.c)
71+
72+
# Part C:
73+
#
74+
# Your application source code
75+
APP_SOURCE=$(RNG_SOURCE) main.c
76+
77+
BUILD_DIR=build
78+
BIN44=test_binary_mldsa44
79+
BIN65=test_binary_mldsa65
80+
BIN87=test_binary_mldsa87
81+
LIB44=libmldsa44.a
82+
LIB65=libmldsa65.a
83+
LIB87=libmldsa87.a
84+
85+
BIN44_FULL=$(BUILD_DIR)/$(BIN44)
86+
BIN65_FULL=$(BUILD_DIR)/$(BIN65)
87+
BIN87_FULL=$(BUILD_DIR)/$(BIN87)
88+
89+
LIB44_FULL=$(BUILD_DIR)/$(LIB44)
90+
LIB65_FULL=$(BUILD_DIR)/$(LIB65)
91+
LIB87_FULL=$(BUILD_DIR)/$(LIB87)
92+
93+
$(LIB44_FULL): $(MLD_SOURCE)
94+
$(Q)echo "$@"
95+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
96+
$(Q)$(CC) -c $(CFLAGS) -DMLD_CONFIG_FILE="\"config_44.h\"" $(INC) $^ -o $(BUILD_DIR)/mldsa_native44.o
97+
$(Q)$(AR) rcs $@ $(BUILD_DIR)/mldsa_native44.o
98+
$(Q)strip -S $@
99+
100+
$(LIB65_FULL): $(MLD_SOURCE)
101+
$(Q)echo "$@"
102+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
103+
$(Q)$(CC) -c $(CFLAGS) -DMLD_CONFIG_FILE="\"config_65.h\"" $(INC) $^ -o $(BUILD_DIR)/mldsa_native65.o
104+
$(Q)$(AR) rcs $@ $(BUILD_DIR)/mldsa_native65.o
105+
$(Q)strip -S $@
106+
107+
$(LIB87_FULL): $(MLD_SOURCE)
108+
$(Q)echo "$@"
109+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
110+
$(Q)$(CC) -c $(CFLAGS) -DMLD_CONFIG_FILE="\"config_87.h\"" $(INC) $^ -o $(BUILD_DIR)/mldsa_native87.o
111+
$(Q)$(AR) rcs $@ $(BUILD_DIR)/mldsa_native87.o
112+
$(Q)strip -S $@
113+
114+
$(BIN44_FULL): $(APP_SOURCE) $(LIB44_FULL)
115+
$(Q)echo "$@"
116+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
117+
$(Q)$(CC) $(CFLAGS) -DMLD_CONFIG_API_PARAMETER_SET=44 $(INC) $^ -o $@
118+
119+
$(BIN65_FULL): $(APP_SOURCE) $(LIB65_FULL)
120+
$(Q)echo "$@"
121+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
122+
$(Q)$(CC) $(CFLAGS) -DMLD_CONFIG_API_PARAMETER_SET=65 $(INC) $^ -o $@
123+
124+
$(BIN87_FULL): $(APP_SOURCE) $(LIB87_FULL)
125+
$(Q)echo "$@"
126+
$(Q)[ -d $(@) ] || mkdir -p $(@D)
127+
$(Q)$(CC) $(CFLAGS) -DMLD_CONFIG_API_PARAMETER_SET=87 $(INC) $^ -o $@
128+
129+
all: build
130+
131+
build: $(BIN44_FULL) $(BIN65_FULL) $(BIN87_FULL)
132+
133+
run: $(BIN44_FULL) $(BIN65_FULL) $(BIN87_FULL)
134+
$(Q)$(EXEC_WRAPPER) ./$(BIN44_FULL)
135+
$(Q)$(EXEC_WRAPPER) ./$(BIN65_FULL)
136+
$(Q)$(EXEC_WRAPPER) ./$(BIN87_FULL)
137+
138+
clean:
139+
rm -rf $(BUILD_DIR)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
2+
3+
# Single-level mldsa-native in a single compilation unit
4+
5+
This directory contains a minimal example for how to build a single instance 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. It exposes the API [../../mldsa/mldsa_native.h](mldsa/mldsa_native.h).
11+
12+
## Usage
13+
14+
Build this example with `make build`, run with `make run`.
15+
16+
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
17+
outside of testing.

0 commit comments

Comments
 (0)