| 1 |
[//]: # (SPDX-License-Identifier: CC-BY-4.0) |
| 2 |
|
| 3 |
# Monolithic Multi-Level Build (C Backend) |
| 4 |
|
| 5 |
This directory contains a minimal example for building all three ML-DSA security levels in a single |
| 6 |
compilation unit, 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 the simplest possible multi-level integration (one `.c` file) |
| 13 |
- You're using only C (no native backend) |
| 14 |
|
| 15 |
## Components |
| 16 |
|
| 17 |
An application using mldsa-native as a monolithic multi-level build needs: |
| 18 |
|
| 19 |
1. Source tree [mldsa_native/*](mldsa_native), including top-level compilation unit |
| 20 |
[mldsa_native.c](mldsa_native/mldsa_native.c) (gathering all C sources) |
| 21 |
and the mldsa-native API [mldsa_native.h](mldsa_native/mldsa_native.h). |
| 22 |
2. Manually provided wrapper file [mldsa_native_all.c](mldsa_native_all.c), |
| 23 |
including `mldsa_native.c` three times. |
| 24 |
3. Manually provided header file [mldsa_native_all.h](mldsa_native_all.h), |
| 25 |
including `mldsa_native.h` three times) |
| 26 |
4. A secure random number generator implementing [`randombytes.h`](../../mldsa/src/randombytes.h) |
| 27 |
5. Your application source code |
| 28 |
|
| 29 |
## Configuration |
| 30 |
|
| 31 |
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets: |
| 32 |
- `MLD_CONFIG_MULTILEVEL_BUILD`: Enables multi-level mode |
| 33 |
- `MLD_CONFIG_NAMESPACE_PREFIX=mldsa`: Base prefix |
| 34 |
- `MLD_CONFIG_INTERNAL_API_QUALIFIER=static`: Makes internal functions static |
| 35 |
- `MLD_CONFIG_NO_SUPERCOP`: Disables the SUPERCOP API (crypto_sign*) |
| 36 |
|
| 37 |
The wrapper [mldsa_native_all.c](mldsa_native_all.c) includes `mldsa_native.c` three times: |
| 38 |
```c |
| 39 |
#define MLD_CONFIG_FILE "multilevel_config.h" |
| 40 |
|
| 41 |
/* Include level-independent code with first level */ |
| 42 |
#define MLD_CONFIG_MULTILEVEL_WITH_SHARED |
| 43 |
#define MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 44 |
#define MLD_CONFIG_PARAMETER_SET 44 |
| 45 |
#include "mldsa_native.c" |
| 46 |
#undef MLD_CONFIG_PARAMETER_SET |
| 47 |
#undef MLD_CONFIG_MULTILEVEL_WITH_SHARED |
| 48 |
|
| 49 |
/* Exclude level-independent code for subsequent levels */ |
| 50 |
#define MLD_CONFIG_MULTILEVEL_NO_SHARED |
| 51 |
#define MLD_CONFIG_PARAMETER_SET 65 |
| 52 |
#include "mldsa_native.c" |
| 53 |
#undef MLD_CONFIG_PARAMETER_SET |
| 54 |
#undef MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS |
| 55 |
|
| 56 |
#define MLD_CONFIG_PARAMETER_SET 87 |
| 57 |
#include "mldsa_native.c" |
| 58 |
#undef MLD_CONFIG_PARAMETER_SET |
| 59 |
``` |
| 60 |
|
| 61 |
The header [mldsa_native_all.h](mldsa_native_all.h) exposes all APIs: |
| 62 |
```c |
| 63 |
#define MLD_CONFIG_PARAMETER_SET 44 |
| 64 |
#include <mldsa_native.h> |
| 65 |
#undef MLD_CONFIG_PARAMETER_SET |
| 66 |
#undef MLD_H |
| 67 |
|
| 68 |
#define MLD_CONFIG_PARAMETER_SET 65 |
| 69 |
#include <mldsa_native.h> |
| 70 |
#undef MLD_CONFIG_PARAMETER_SET |
| 71 |
#undef MLD_H |
| 72 |
|
| 73 |
#define MLD_CONFIG_PARAMETER_SET 87 |
| 74 |
#include <mldsa_native.h> |
| 75 |
#undef MLD_CONFIG_PARAMETER_SET |
| 76 |
#undef MLD_H |
| 77 |
``` |
| 78 |
|
| 79 |
## Notes |
| 80 |
|
| 81 |
- `MLD_CONFIG_MULTILEVEL_WITH_SHARED` must be set for exactly ONE level |
| 82 |
- `MLD_CONFIG_MONOBUILD_KEEP_SHARED_HEADERS` prevents cleanup of shared headers between inclusions |
| 83 |
- `MLD_CONFIG_NO_SUPERCOP` is required to avoid conflicting `CRYPTO_*` macro definitions |
| 84 |
|
| 85 |
## Usage |
| 86 |
|
| 87 |
```bash |
| 88 |
make build # Build the example |
| 89 |
make run # Run the example |
| 90 |
``` |
| 91 |
|
| 92 |
## Warning |
| 93 |
|
| 94 |
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY. |
| 95 |
You MUST provide a cryptographically secure RNG for production use. |