| 1 |
[//]: # (SPDX-License-Identifier: CC-BY-4.0) |
| 2 |
|
| 3 |
# Monolithic Multi-Level Build (Native Backend) |
| 4 |
|
| 5 |
This directory contains a minimal example for building all three ML-DSA security levels in a single |
| 6 |
compilation unit with native assembly backends, with shared code deduplicated. |
| 7 |
|
| 8 |
## Use Case |
| 9 |
|
| 10 |
Use this approach when: |
| 11 |
- You need all ML-DSA security levels in one application |
| 12 |
- You want optimal performance via native assembly |
| 13 |
- You want the simplest possible multi-level native integration |
| 14 |
|
| 15 |
## Components |
| 16 |
|
| 17 |
1. Source tree [mldsa_native/*](mldsa_native), including top-level compilation unit |
| 18 |
[mldsa_native.c](mldsa_native/mldsa_native.c) (gathering all C sources), |
| 19 |
[mldsa_native.S](mldsa_native/mldsa_native.S) (gathering all assembly sources), |
| 20 |
and the mldsa-native API [mldsa_native.h](mldsa_native/mldsa_native.h). |
| 21 |
2. Manually provided wrapper file [mldsa_native_all.c](mldsa_native_all.c), |
| 22 |
including `mldsa_native.c` three times (in this example, we don't use a |
| 23 |
wrapper header since we directly include `mldsa_native_all.c` into `main.c`). |
| 24 |
3. A secure random number generator implementing [`randombytes.h`](../../mldsa/src/randombytes.h) |
| 25 |
4. Your application source code |
| 26 |
|
| 27 |
## Configuration |
| 28 |
|
| 29 |
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets: |
| 30 |
- `MLD_CONFIG_MULTILEVEL_BUILD`: Enables multi-level mode |
| 31 |
- `MLD_CONFIG_NAMESPACE_PREFIX=mldsa`: Base prefix |
| 32 |
- `MLD_CONFIG_USE_NATIVE_BACKEND_ARITH`: Enables native arithmetic backend |
| 33 |
- `MLD_CONFIG_USE_NATIVE_BACKEND_FIPS202`: Enables native FIPS-202 backend |
| 34 |
- `MLD_CONFIG_NO_SUPERCOP`: Disables the SUPERCOP API (crypto_sign*) |
| 35 |
|
| 36 |
The wrapper [mldsa_native_all.c](mldsa_native_all.c) includes `mldsa_native.c` three times: |
| 37 |
```c |
| 38 |
#define MLD_CONFIG_FILE "multilevel_config.h" |
| 39 |
|
| 40 |
/* Include level-independent code with first level */ |
| 41 |
#define MLD_CONFIG_MULTILEVEL_WITH_SHARED 1 |
| 42 |
#define MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 43 |
#define MLD_CONFIG_PARAMETER_SET 44 |
| 44 |
#include "mldsa_native.c" |
| 45 |
#undef MLD_CONFIG_MULTILEVEL_WITH_SHARED |
| 46 |
#undef MLD_CONFIG_PARAMETER_SET |
| 47 |
|
| 48 |
/* Exclude level-independent code for subsequent levels */ |
| 49 |
#define MLD_CONFIG_MULTILEVEL_NO_SHARED |
| 50 |
#define MLD_CONFIG_PARAMETER_SET 65 |
| 51 |
#include "mldsa_native.c" |
| 52 |
#undef MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 53 |
#undef MLD_CONFIG_PARAMETER_SET |
| 54 |
|
| 55 |
#define MLD_CONFIG_PARAMETER_SET 87 |
| 56 |
#include "mldsa_native.c" |
| 57 |
#undef MLD_CONFIG_PARAMETER_SET |
| 58 |
``` |
| 59 |
|
| 60 |
The application [main.c](main.c) embeds the wrapper and imports constants: |
| 61 |
```c |
| 62 |
#include "mldsa_native_all.c" |
| 63 |
|
| 64 |
#define MLD_CONFIG_CONSTANTS_ONLY |
| 65 |
#include <mldsa_native.h> |
| 66 |
``` |
| 67 |
|
| 68 |
## Notes |
| 69 |
|
| 70 |
- Both `mldsa_native_all.c` and `mldsa_native.S` must be compiled and linked |
| 71 |
- `MLD_CONFIG_MULTILEVEL_WITH_SHARED` must be set for exactly ONE level |
| 72 |
- `MLD_CONFIG_CONSTANTS_ONLY` imports size constants without function declarations |
| 73 |
- Native backends are auto-selected based on target architecture |
| 74 |
|
| 75 |
## Usage |
| 76 |
|
| 77 |
```bash |
| 78 |
make build # Build the example |
| 79 |
make run # Run the example |
| 80 |
``` |
| 81 |
|
| 82 |
## Warning |
| 83 |
|
| 84 |
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY. |
| 85 |
You MUST provide a cryptographically secure RNG for production use. |