Skip to content

Commit 2227d0b

Browse files
mkannwischerhanno-becker
authored andcommitted
Expand and unify example READMEs
Signed-off-by: Matthias J. Kannwischer <matthias@kannwischer.eu>
1 parent f2fb27d commit 2227d0b

File tree

12 files changed

+508
-228
lines changed

12 files changed

+508
-228
lines changed

examples/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
2+
3+
# Usage examples
4+
5+
This directory contains minimal examples demonstrating how you can use mldsa-native.
6+
7+
## Basic
8+
9+
See [basic](basic) for a basic example of how to build a single instance of mldsa-native.
10+
11+
## Basic_deterministic
12+
13+
See [basic_deterministic](basic_deterministic) for a basic example of how to build a single instance of mldsa-native without `randombytes()` implementation. This allows users to build mldsa-native using only the deterministic API when randomized functions are not required.
14+
## Multi-level build (C only)
15+
16+
See [multilevel_build](multilevel_build) for an example of how to build one instance of mldsa-native per security level,
17+
in such a way that level-independent code is shared.
18+
19+
## Multi-level build (with native code)
20+
21+
See [multilevel_build_native](multilevel_build_native) for an example of how to build one instance of mldsa-native per
22+
security level, in such a way that level-independent code is shared, and leveraging the native backends.
23+
24+
## Custom FIPS202 implementation
25+
26+
See [bring_your_own_fips202](bring_your_own_fips202) for an example of how to use mldsa-native with your own FIPS-202
27+
implementation.
28+
29+
## Custom FIPS202 implementation (static state variant)
30+
31+
See [bring_your_own_fips202_static](bring_your_own_fips202_static) for an example of how to use mldsa-native with a
32+
custom FIPS-202 implementation using a static state. This variant demonstrates the serial-only FIPS-202 configuration
33+
(`MLD_CONFIG_SERIAL_FIPS202_ONLY`).
34+
35+
## Custom config + custom FIPS-202 backend
36+
37+
See [custom_backend](custom_backend) for an example of how to use mldsa-native with a custom configuration file and a
38+
custom FIPS-202 backend.
39+
40+
## Monobuild (C only)
41+
42+
See [monolithic_build](monolithic_build) for an example of how to build mldsa-native (with C backend) from a single
43+
auto-generated compilation unit.
44+
45+
## Multi-level monobuild (C only)
46+
47+
See [monolithic_build_multilevel](monolithic_build_multilevel) for an example of how to build all security levels of
48+
mldsa-native (with C backend) inside a single compilation unit, sharing the level-independent code.
49+
50+
## Multi-level monobuild (with native code)
51+
52+
See [monolithic_build_multilevel_native](monolithic_build_multilevel_native) for an example of how to build all security
53+
levels of mldsa-native inside a single compilation unit, sharing the level-independent code, while also linking in assembly
54+
from the native backends.

examples/basic/README.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Building mldsa-native
3+
# Basic build
44

5-
This directory contains a minimal example for how to build mldsa-native.
5+
This directory contains a minimal example for how to build mldsa-native for a single security level.
66

7-
## Components
8-
9-
An application using mldsa-native as-is needs to include the following components:
10-
11-
1. mldsa-native source tree, including [`mldsa/src/`](../../mldsa/src) and [`mldsa/src/fips202/`](../../mldsa/src/fips202).
12-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mldsa/src/randombytes.h).
13-
3. The application source code
7+
## Use Case
148

15-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
16-
outside of testing.
9+
Use this approach when:
10+
- You need only one ML-DSA parameter set (44, 65, or 87)
11+
- You want to build the mldsa-native C files separately, not as a single compilation unit.
12+
- You're using C only, no native backends.
1713

18-
## Usage
14+
## Components
1915

20-
Build this example with `make build`, run with `make run`.
16+
1. mldsa-native source tree: [`mldsa/src/`](../../mldsa/src) and [`mldsa/src/fips202/`](../../mldsa/src/fips202)
17+
2. A secure random number generator implementing [`randombytes.h`](../../mldsa/src/randombytes.h)
18+
3. Your application source code
2119

22-
## What this example demonstrates
20+
## Configuration
2321

24-
This basic example shows how to use the ML-DSA (Module-Lattice-Based Digital Signature Algorithm) for:
22+
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets:
23+
- `MLD_CONFIG_PARAMETER_SET`: Security level (44, 65, or 87). Default is 65.
24+
- `MLD_CONFIG_NAMESPACE_PREFIX`: Symbol prefix for the API. Set to `mldsa` in this example.
2525

26-
1. **Key Generation**: Generate a public/private key pair
27-
2. **Signing**: Sign a message with a private key and optional context
28-
3. **Signature Verification**: Verify a signature using the public key
29-
4. **Signed Messages**: Create and open signed messages (signature + message combined)
26+
To change the security level, modify `MLD_CONFIG_PARAMETER_SET` in the config file or pass it via CFLAGS.
3027

31-
The example demonstrates both the detached signature API (`crypto_sign_signature`/`crypto_sign_verify`) and the combined signature API (`crypto_sign`/`crypto_sign_open`).
28+
## Usage
3229

33-
## Parameter Sets
30+
```bash
31+
make build # Build the example
32+
make run # Run the example
33+
```
3434

35-
ML-DSA supports three parameter sets:
36-
- **ML-DSA-44**
37-
- **ML-DSA-65**
38-
- **ML-DSA-87**
35+
## Warning
3936

40-
The example builds and runs all three parameter sets to demonstrate the different security levels and their corresponding key/signature sizes.
37+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
38+
You MUST provide a cryptographically secure RNG for production use.
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Building mldsa-native
43

5-
This directory contains a minimal example showing how to build **mldsa-native** for use cases only requiring the deterministic key generation and signing APIs (`crypto_sign_keypair_internal` and `crypto_sign_signature_internal`). In that case, no implementation of `randombytes()` has to be provided.
4+
This directory contains a minimal example for building mldsa-native using only the deterministic API,
5+
without requiring a `randombytes()` implementation.
66

7-
## Components
7+
## Use Case
88

9-
An application using mldsa-native as-is needs to include the following components:
9+
Use this approach when:
10+
- Your application manages its own entropy/randomness externally
11+
- You only need `crypto_sign_keypair_internal` and `crypto_sign_signature_internal` (deterministic variants)
1012

11-
1. mldsa-native source tree, including [`mldsa/src/`](../../mldsa/src) and [`mldsa/src/fips202/`](../../mldsa/src/fips202).
12-
2. The application source code
13+
## Components
1314

14-
## Usage
15+
1. mldsa-native source tree: [`mldsa/src/`](../../mldsa/src) and [`mldsa/src/fips202/`](../../mldsa/src/fips202)
16+
2. Your application source code
1517

16-
Build this example with `make build`, run with `make run`.
18+
No `randombytes()` implementation is required.
1719

18-
## What this example demonstrates
20+
## Configuration
1921

20-
This basic_deterministic example shows how to use the ML-DSA (Module-Lattice-Based Digital Signature Algorithm) for:
22+
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets:
23+
- `MLD_CONFIG_NO_RANDOMIZED_API`: Disables `crypto_sign_keypair`, `crypto_sign_signature`, etc.
24+
- `MLD_CONFIG_PARAMETER_SET`: Security level (default 65)
25+
- `MLD_CONFIG_NAMESPACE_PREFIX`: Symbol prefix (set to `mldsa`)
2126

22-
1. **Key Generation**: Generate a public/private key pair
23-
2. **Signing**: Sign a message with a private key and optional context
24-
3. **Signature Verification**: Verify a signature using the public key
27+
## Notes
2528

26-
## Parameter Sets
29+
- This is incompatible with `MLD_CONFIG_KEYGEN_PCT` (pairwise consistency test)
2730

28-
ML-DSA supports three parameter sets:
29-
- **ML-DSA-44**
30-
- **ML-DSA-65**
31-
- **ML-DSA-87**
31+
## Usage
3232

33-
The example builds and runs all three parameter sets to demonstrate the different security levels and their corresponding key/signature sizes.
33+
```bash
34+
make build # Build the example
35+
make run # Run the example
36+
```
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Bring your own FIPS-202
3+
# Bring Your Own FIPS-202
44

5-
This directory contains a minimal example for how to use mldsa-native as a code package, with a custom FIPS-202
5+
This directory contains a minimal example for using mldsa-native with a custom FIPS-202 (SHA-3/SHAKE)
66
implementation. We use tiny_sha3[^tiny_sha3] as an example.
77

8+
## Use Case
9+
10+
Use this approach when:
11+
- You need only one ML-DSA parameter set (44, 65, or 87)
12+
- Your application already has a FIPS-202 software/hardware implementation you want to reuse
13+
814
## Components
915

10-
An application using mldsa-native with a custom FIPS-202 implementation needs the following:
16+
1. Arithmetic part of mldsa-native: [`mldsa/src/`](../../mldsa/src) (excluding `fips202/`)
17+
2. A secure random number generator implementing [`randombytes.h`](../../mldsa/src/randombytes.h)
18+
3. Custom FIPS-202 implementation with headers compatible with:
19+
- [`fips202.h`](../../mldsa/src/fips202/fips202.h)
20+
- [`fips202x4.h`](../../mldsa/src/fips202/fips202x4.h)
21+
4. Your application source code
1122

12-
1. Arithmetic part of the mldsa-native source tree: [`mldsa/src/`](../../mldsa/src)
13-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mldsa/src/randombytes.h).
14-
3. A custom FIPS-202 with `fips202.h` and `fips202x4.h` headers compatible with
15-
[`mldsa/src/fips202/fips202.h`](../../mldsa/src/fips202/fips202.h) and [`mldsa/src/fips202/fips202x4.h`](../../mldsa/src/fips202/fips202x4.h).
16-
4. The application source code
23+
## Configuration
1724

18-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
19-
outside of testing.
25+
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets:
26+
- `MLD_CONFIG_FIPS202_CUSTOM_HEADER`: Path to your custom `fips202.h`
27+
- `MLD_CONFIG_FIPS202X4_CUSTOM_HEADER`: Path to your custom `fips202x4.h`
2028

21-
## Usage
29+
Your custom FIPS-202 implementation must impelement the API specified in [FIPS202.md](../../FIPS202.md).
2230

23-
Build this example with `make build`, run with `make run`.
31+
## Notes
2432

25-
## Custom FIPS-202 Implementation
33+
- The 4x batched functions (`x4`) can fall back to 4 sequential calls if batching isn't available
34+
- Structure definitions may differ from mldsa-native's defaults (e.g., for incremental hashing)
2635

27-
This example uses tiny_sha3 as the underlying Keccak/SHA3 implementation. The wrapper headers in `custom_fips202/`
28-
adapt the tiny_sha3 API to match the API expected by mldsa-native.
36+
## Usage
2937

30-
Note that the `fips202x4.h` implementation provided here is a simple serial implementation that does not provide
31-
any performance benefits from parallelization. For production use, consider using an optimized parallel implementation.
38+
```bash
39+
make build # Build the example
40+
make run # Run the example
41+
```
3242

33-
## Verification
43+
## Warning
3444

35-
This example uses the same test vectors as the basic example (via a symlink to `expected_signatures.h`) and verifies
36-
that the custom FIPS-202 implementation produces identical results to the default implementation. This ensures that
37-
the wrapper is correctly implementing the required API.
45+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
46+
You MUST provide a cryptographically secure RNG for production use.
3847

3948
<!--- bibliography --->
4049
[^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3)
Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Bring your own FIPS-202 (Static State Variant)
3+
# Bring Your Own FIPS-202 (Static State Variant)
44

5-
This directory contains a minimal example for how to use mldsa-native with external FIPS-202
6-
HW/SW-implementations that use a single global state (for example, some hardware accelerators).
7-
Specifically, this example demonstrates the use of the serial-only FIPS-202 configuration
8-
`MLD_CONFIG_SERIAL_FIPS202_ONLY`.
5+
This directory contains a minimal example for using mldsa-native with a custom FIPS-202 implementation
6+
that uses a single global state. This is common for hardware accelerators that can only hold one
7+
Keccak state at a time.
98

10-
**WARNING:** This example is EXPECTED TO PRODUCE INCORRECT RESULTS because ML-DSA requires
11-
multiple independent FIPS-202 contexts to be active simultaneously. This example demonstrates
12-
what happens when only a single global state is available.
9+
## Use Case
10+
11+
Use this approach when:
12+
- You need only one ML-DSA parameter set (44, 65, or 87)
13+
- Your application already has a FIPS-202 software/hardware implementation you want to reuse
14+
- Your FIPS-202 implementation does not support multiple active SHA3/SHAKE computations.
1315

1416
## Components
1517

16-
An application using mldsa-native with a custom FIPS-202 implementation needs the following:
18+
1. Arithmetic part of mldsa-native: [`mldsa/src/`](../../mldsa/src) (excluding `fips202/`)
19+
2. A secure random number generator implementing [`randombytes.h`](../../mldsa/src/randombytes.h)
20+
3. Custom FIPS-202 implementation with headers compatible with [`fips202.h`](../../mldsa/src/fips202/fips202.h)
21+
4. Your application source code
22+
23+
## Configuration
24+
25+
The configuration file [mldsa_native_config.h](mldsa_native/mldsa_native_config.h) sets:
26+
- `MLD_CONFIG_SERIAL_FIPS202_ONLY`: Disables batched Keccak; matrix entries generated one at a time
27+
- `MLD_CONFIG_FIPS202_CUSTOM_HEADER`: Path to your custom `fips202.h`
1728

18-
1. Arithmetic part of the mldsa-native source tree: [`mldsa/src/`](../../mldsa/src)
19-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mldsa/src/randombytes.h).
20-
3. A custom FIPS-202 with `fips202.h` header compatible with [`mldsa/fips202/fips202.h`](../../mldsa/src/fips202/fips202.h).
21-
With `MLD_CONFIG_SERIAL_FIPS202_ONLY`, the FIPS-202x4 parallel API is not used.
22-
4. The application source code
29+
Your custom FIPS-202 implementation must provide:
30+
- `mld_shake128_init()`, `mld_shake128_absorb()`, `mld_shake128_finalize()`, `mld_shake128_squeeze()`, `mld_shake128_release()`
31+
- `mld_shake256_init()`, `mld_shake256_absorb()`, `mld_shake256_finalize()`, `mld_shake256_squeeze()`, `mld_shake256_release()`
32+
- `mld_shake256`
33+
- Structure definitions for `mld_shake128ctx` and `mld_shake256ctx`
2334

24-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
25-
outside of testing.
35+
## Notes
36+
37+
- `MLD_CONFIG_SERIAL_FIPS202_ONLY` may reduce performance on CPUs with SIMD support
38+
- Matrix and vector generation becomes sequential instead of batched (4 entries at a time)
39+
- Only enable this when your hardware requires it
2640

2741
## Usage
2842

29-
Build this example with `make build`, run with `make run`.
43+
```bash
44+
make build # Build the example
45+
make run # Run the example
46+
```
47+
48+
## Warning
3049

31-
You should see verification failures, which is the expected behavior demonstrating that a single
32-
global FIPS-202 state is insufficient for ML-DSA.
50+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
51+
You MUST provide a cryptographically secure RNG for production use.
3352

3453
<!--- bibliography --->
3554
[^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3)

0 commit comments

Comments
 (0)