sntrup761
A pure-Rust implementation of Streamlined NTRU Prime 4591761.
NTRU Prime is a lattice-based cryptosystem aiming to improve the security of lattice schemes at minimal cost. It is thought to be resistant to quantum computing advances, in particular Shor's algorithm. It made it to NIST final round but was not selected for finalization.
Please read the warnings before use.
The algorithm was authored by Daniel J. Bernstein, Chitchanok Chuengsatiansup, Tanja Lange & Christine van Vredendaal. This implementation is aligned with the PQClean reference and verified against the IETF draft KAT vectors.
Parameter set
| Parameter | Value |
|---|---|
| p | 761 |
| q | 4591 |
| w | 286 |
Sizes
Note that Streamlined NTRU Prime 761 sizes are fixed. All keys and ciphertexts use a canonical encoding which is enfored by the code i.e. it should not be feasible to modify the encoding of an existing public key or a ciphertext without changing is mathematical value.
| Type | Bytes |
|---|---|
| Public Key | 1158 |
| Private Key | 1763 |
| Compressed Private Key | 32 |
| Ciphertext | 1039 |
| Shared Key | 32 |
Features
- Pure Rust,
no_std-compatible, dependency-minimal - IND-CCA2 secure with implicit rejection
- Constant-time operations throughout (branchless sort, constant-time comparison and selection)
- Optional
serdesupport via theserdefeature - Deterministic key generation and encapsulation from a 32-byte seed
- Compressed decapsulation key (32-byte seed instead of 1763 bytes)
Installation
Add to your Cargo.toml:
[]
= "0.2"
Feature Flags
All features are opt-in. Enable them in your Cargo.toml:
[]
= { = "0.2", = ["std", "serde"] }
| Feature | Default | Description |
|---|---|---|
alloc |
no | Enables TryFrom<Vec<u8>> and TryFrom<Box<[u8]>> conversions (requires an allocator) |
std |
no | Enables standard library support (implies alloc functionality) |
serde |
no | Enables Serialize/Deserialize for all key and ciphertext types (via serdect for constant-time hex encoding) |
js |
no | Enables WebAssembly support for wasm32-unknown-unknown by configuring getrandom to use JavaScript's crypto.getRandomValues() |
Usage
Basic key exchange
use *;
// Key generation
let = generate_key;
// Encapsulation (sender side)
let = public_key.encapsulate;
// Decapsulation (receiver side — implicit rejection: always returns a key)
let shared_secret_receiver = private_key.decapsulate;
assert!;
Deterministic key generation
Useful for deriving the same keypair from stored entropy:
use *;
let seed = ; // must come from a cryptographically secure source
let = generate_key_from_seed;
let = generate_key_from_seed;
assert_eq!;
assert!;
Deterministic encapsulation
Produces the same ciphertext and shared secret from a given seed and public key:
use *;
let = generate_key;
let seed = ; // must come from a cryptographically secure source
let = pk.encapsulate_deterministic;
let = pk.encapsulate_deterministic;
assert_eq!;
assert!;
Compressed decapsulation key
Store only 32 bytes instead of the full 1763-byte secret key:
use *;
let compressed = generate;
let = compressed.expand;
// Or decapsulate directly (re-expands the full key each time)
let = pk.encapsulate;
let ss2 = compressed.decapsulate;
assert!;
Serialization with serde
Enable the serde feature:
= { = "0.2", = ["serde"] }
Keys and ciphertexts serialize to hex in human-readable formats (JSON) and raw bytes in binary formats (postcard, bincode):
use *;
let = generate_key;
let json = to_string.unwrap;
let pk2: EncapsulationKey = from_str.unwrap;
assert_eq!;
WebAssembly
To compile for wasm32-unknown-unknown, enable the js feature so that getrandom uses JavaScript's crypto.getRandomValues() for randomness:
[]
= { = "0.2", = ["js"] }
Install the target and build:
For wasm32-wasi (or wasm32-wasip1), the js feature is not needed since WASI provides its own random source.
wasm-bindgen example
use *;
use *;
Benchmarks
Measured on Apple M1 (aarch64). The NEON column uses ARM NEON SIMD intrinsics that are baseline on aarch64; the scalar column uses the force-scalar feature to disable them.
| Operation | Scalar (pure Rust) | NEON (aarch64) | Speedup |
|---|---|---|---|
| Key Gen | 2,858 µs | 1,400 µs | 2.0× |
| Encapsulate | 305 µs | 102 µs | 3.0× |
| Decapsulate | 768 µs | 186 µs | 4.1× |
Measured on AMD Ryzen 9 5900HX (x86_64). The AVX2 column uses 256-bit SIMD intrinsics enabled by target-cpu=native; the scalar column uses the force-scalar feature to disable them.
| Operation | Scalar (pure Rust) | AVX2 (x86_64) | Speedup |
|---|---|---|---|
| Key Gen | 2,514 µs | 756 µs | 3.3× |
| Encapsulate | 390 µs | 56 µs | 6.9× |
| Decapsulate | 1,089 µs | 93 µs | 11.8× |
AVX2 optimizations are enabled automatically on x86_64 when AVX2 is available. NEON optimizations are enabled automatically on aarch64 targets. To force pure-Rust (scalar) code, enable the force-scalar feature.
Security Properties
- IND-CCA2 security via implicit rejection: decapsulation always returns a shared key. On failure, a pseudorandom key is derived from secret randomness (
rho), making it indistinguishable from a valid key to an attacker. - Hash domain separation: all hashes use prefix bytes (following the NTRU Prime specification).
- Constant-time operations: branchless sorting (djbsort), constant-time weight checks, constant-time ciphertext comparison, and constant-time selection in decapsulation.
- Zeroization: secret key material is zeroized on drop.
Warnings
Implementation
This implementation has not undergone any security auditing and while care has been taken no guarantees can be made for either correctness or the constant time running of the underlying functions. Please use at your own risk.
Algorithm
Streamlined NTRU Prime was first published in 2016. The algorithm still requires careful security review. Please see here for further warnings from the authors regarding NTRU Prime and lattice-based encryption schemes.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.