A safe pure-rust implementation of the NTRU post-quantum scheme.
tiny-keccak
as SHA-3 implementation and aes
as AES block cipher (used as RNG) implementationntruhps2048509
) and 45 milliseconds (ntruhps4096821
) to run on a modern computerntrust-native
.Anyone, how wants to use the NTRU scheme to negotiate a key between two parties.
Add this to your Cargo.toml
:
toml
[dependencies]
ntrust-native = "1.0"
To use a specific NTRU variant, you need to import it with the corresponding feature flag:
toml
[dependencies]
ntrust-native = { version = "1.0", features = ["ntruhrss701"] }
The simple
example illustrates the API:
```rust
use ntrustnative::AesState;
use ntrustnative::{cryptokemdec, cryptokemenc, cryptokemkeypair};
use ntrustnative::{CRYPTOBYTES, CRYPTOCIPHERTEXTBYTES, CRYPTOPUBLICKEYBYTES, CRYPTO_SECRETKEYBYTES};
use std::error;
fn main() -> Result<(), Box
cryptokemkeypair(&mut pk, &mut sk, &mut rng)?; cryptokemenc(&mut ct, &mut ssbob, &pk, &mut rng)?; cryptokemdec(&mut ssalice, &ct, &sk)?;
asserteq!(ssbob, ss_alice);
Ok(()) } ```
This library comes with two examples:
bash
$ cargo run --example simple
The output annotates messages with Alice/Bob to illustrate which data is processed by which party.
The katkem
example implements the classic request/response file structure which is part of the NIST PQC framework.
bash
$ cargo run --example katkem PQCkemKAT_935.req PQCkemKAT_935.rsp
$ cargo run --example katkem PQCkemKAT_935.rsp
The different variants (ntruhps2048509
, ntruhps2048677
, ntruhps4096821
, ntruhrss701
) can be enabled through feature flags:
bash
$ cargo run --example katkem --features ntruhrss701 -- PQCkemKAT_1450.req PQCkemKAT_1450.rsp
ntruhps2048509
is the default variant. You cannot enable two variants simultaneously.
All data uses clock cycles as unit. The rust implementation has the following clock-cycle count characteristics (the smaller the better):
complete KEM | keypair | enc | dec | |
ntruhps2048509 | 19,980,855 | 14,105,680 | 472,909 | 1,122,414 |
ntruhps2048677 | 27,478,939 | 24,077,519 | 895,930 | 2,333,079 |
ntruhps4096821 | 42,083,125 | 36,882,783 | 1,487,401 | 3,367,818 |
ntruhrss701 | 32,433,993 | 28,506,984 | 828,162 | 2,919,074 |
The C reference implementation has the following clock-cycle count characteristics (the smaller the better):
complete KEM | keypair | enc | dec | |
ntruhps2048509 | 15,912,900 | 12,139,200 | 811,651 | 1,812,650 |
ntruhps2048677 | 28,911,500 | 22,233,600 | 1,520,640 | 3,668,860 |
ntruhps4096821 | 41,914,800 | 32,138,300 | 2,089,350 | 5,908,570 |
ntruhrss701 | 28,966,600 | 23,134,700 | 1,368,270 | 3,462,640 |
The tests were done on a Lenovo Thinkpad x260 (Intel Core i5-6200U CPU @ 2.30GHz). In the case of rust, criterion 0.3.5 has been used as given in benches/
and in case of C, Google's benchmark with PFM support and disabled CPU frequency scaling. Our summary is that both implementations have comparable runtime. rust is a little bit slower (but uses many copy operations for type safety you could replace with unsafe {}
code). You can run the benchmark suite yourself with the bench
subcommand and optionally some variant feature flag:
bash
$ cargo bench --features ntruhrss701
On github.
On github.