NTRUP Rust

This repository presents an implementation of high-security prime-degree large-Galois-group inert-modulus ideal-lattice-based cryptography on rust programing langudge. “Prime degree” etc. are defenses against potential attacks; see official website.

This implementation uses: Fields of the form (Z/q)[x]/(xp −x−1), where p is prime, are used in “NTRU Prime”, introduced in this paper, and have all of our recommended defenses.

Notation and Parameters for NTRU Prime

In the context of NTRU Prime, several parameters and notations play a crucial role in defining the cryptographic system.

Parameter Set

A parameter set for NTRU Prime is represented as a triple (p, q, w), which forms the foundation of the primary algebraic structures in the system. Let's break down these parameters:

Extra parameter Set

Valid Parameter Set Conditions

To ensure the validity of a parameter set, it must meet the following conditions:

Notational Abbreviations

For brevity and clarity, the following notational abbreviations are used:

R3: Denotes the ring (Z/3)[x]/P, which is a specific variant related to the ring R. Rq: Represents the field (Z/q)[x]/P, another critical element in the cryptographic system.

Rust Features

You can select parameters through features, you must select parameters!

```

Cargo.toml

ntrulp = { version = "0.1.7", features = ["ntrup653"] } ntrulp = { version = "0.1.7", features = ["ntrup761"] } ntrulp = { version = "0.1.7", features = ["ntrup857"] } ntrulp = { version = "0.1.7", features = ["ntrup953"] } ntrulp = { version = "0.1.7", features = ["ntrup1013"] } ntrulp = { version = "0.1.7", features = ["ntrup1277"] } ```

install

bash cargo add ntrulp

Testing

```bash git clone https://github.com/zebra-sh/ntrulp.git cd ntrulp cargo test --features ntrup1277

```

bash git clone https://github.com/zebra-sh/ntrulp.git cd ntrulp cargo bench --features ntrup1277

Keys Generation:

```rust let mut rng = NTRURandom::new(); let f: Rq = Rq::from(rng.shortrandom().unwrap()); let mut g: R3; let sk = loop { g = R3::from(rng.randomsmall().unwrap());

match PrivKey::compute(&f, &g) {
    Ok(s) => break s,
    Err(_) => continue,
};

};

let pk = PubKey::compute(&f, &g).unwrap(); let importedpk = PubKey::fromsk(&sk).unwrap(); let pkbytes = importedpk.asbytes(); let frombytes = PubKey::import(&pk_bytes).unwrap();

asserteq!(frombytes.coeffs, pk.coeffs); ```

Encrypt/Decrypt bytes example

```rust use std::sync::Arc;

use ntrulp::key::privkey::PrivKey; use ntrulp::key::pubkey::PubKey; use ntrulp::ntru::cipher::{ bytesdecrypt, parallelbytesdecrypt, parallelbytes_encrypt, }; use ntrulp::ntru::errors::NTRUErrors; use ntrulp::random::{CommonRandom, NTRURandom};

fn genkeys<'a>() -> Result<(Arc, Arc), NTRUErrors<'a>> { let mut rng = NTRURandom::new(); let mut g: R3; let f: Rq = Rq::from(rng.shortrandom().unwrap()); let sk = loop { g = R3::from(rng.random_small().unwrap());

    match PrivKey::compute(&f, &g) {
        Ok(s) => break s,
        Err(_) => continue,
    };
};
let pk = PubKey::compute(&f, &g).unwrap();

Ok((Arc::new(sk), Arc::new(pk)))

}

let mut rng = NTRURandom::new(); let bytes = Arc::new(rng.randombytes::<1024>().tovec()); let (sk, pk) = genkeys().unwrap();

let numthreads = 4; let encrypted1 = Arc::new(parallelbytesencrypt(&mut rng, &bytes, &pk, numthreads).unwrap()); let decrypted0 = parallelbytesdecrypt(&encrypted1, &sk, numthreads).unwrap(); let decrypted1 = bytesdecrypt(&encrypted1, &sk).unwrap();

assert_eq!(decrypted0, decrypted1); ```

TODO

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.