|
| 1 | +[//]: # (SPDX-License-Identifier: CC-BY-4.0) |
| 2 | + |
| 3 | +# Multi-level mldsa-native in a single compilation unit, with native code |
| 4 | + |
| 5 | +This directory contains a minimal example for how to build multiple instances of mldsa-native in a single compilation |
| 6 | +unit, while additionally linking assembly sources from native code. |
| 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. For each inclusion, it sets `MLD_CONFIG_FILE` |
| 16 | +appropriately first, and then includes the monobuild: |
| 17 | +```C |
| 18 | +/* Three instances of mldsa-native for all security levels */ |
| 19 | + |
| 20 | +#define MLD_CONFIG_FILE "multilevel_config.h" |
| 21 | + |
| 22 | +/* Include level-independent code */ |
| 23 | +#define MLD_CONFIG_MULTILEVEL_WITH_SHARED 1 |
| 24 | +/* Keep level-independent headers at the end of monobuild file */ |
| 25 | +#define MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 26 | +#define MLD_CONFIG_PARAMETER_SET 44 |
| 27 | +#include "mldsa_native.c" |
| 28 | +#undef MLD_CONFIG_MULTILEVEL_WITH_SHARED |
| 29 | +#undef MLD_CONFIG_PARAMETER_SET |
| 30 | + |
| 31 | +/* Exclude level-independent code */ |
| 32 | +#define MLD_CONFIG_MULTILEVEL_NO_SHARED |
| 33 | +#define MLD_CONFIG_PARAMETER_SET 65 |
| 34 | +#include "mldsa_native.c" |
| 35 | +/* `#undef` all headers at the and of the monobuild file */ |
| 36 | +#undef MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 37 | +#undef MLD_CONFIG_PARAMETER_SET |
| 38 | + |
| 39 | +#define MLD_CONFIG_PARAMETER_SET 87 |
| 40 | +#include "mldsa_native.c" |
| 41 | +#undef MLD_CONFIG_PARAMETER_SET |
| 42 | +``` |
| 43 | +
|
| 44 | +Note the setting `MLD_CONFIG_MULTILEVEL_WITH_SHARED` which forces the inclusion of all level-independent |
| 45 | +code in the ML_DSA-44 build, and the setting `MLD_CONFIG_MULTILEVEL_NO_SHARED`, which drops all |
| 46 | +level-independent code in the subsequent builds. Finally, `MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS` entails that |
| 47 | +[mldsa_native.c](mldsa/mldsa_native.c) does not `#undefine` the `#define` clauses from level-independent files. |
| 48 | +
|
| 49 | +Since we embed [mldsa_native_all.c](mldsa_native_all.c) directly into the application source [main.c](main.c), we don't |
| 50 | +need a header for function declarations. However, we still import [mldsa_native.h](../../mldsa/mldsa_native.h) once |
| 51 | +with `MLD_CONFIG_API_CONSTANTS_ONLY`, for definitions of the sizes of the key material and signatures. |
| 52 | +Excerpt from [main.c](main.c): |
| 53 | +
|
| 54 | +```c |
| 55 | +#include "mldsa_native_all.c" |
| 56 | +
|
| 57 | +#define MLD_CONFIG_API_CONSTANTS_ONLY |
| 58 | +#include <mldsa_native.h> |
| 59 | +``` |
| 60 | + |
| 61 | +## Usage |
| 62 | + |
| 63 | +Build this example with `make build`, run with `make run`. |
| 64 | + |
| 65 | +**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation |
| 66 | +outside of testing. |
0 commit comments