|
| 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