Project

NTRU

Tanish Makadia, Alex Khosrowshahi

Overview.  The classic NTRUEncrypt post-quantum cryptosystem, implemented in Rust.

Source
Plaintext m(x)
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100020000000000020001000200000000000000020002000100010000000000010000000100020000000000020002000200020000000100000000000000020000000100000000000200020000000000010000000100020000000000020002000100000000000000020002000200010000000000020002000200000000000000020001000200020000000100000000000000010000000000020002000200010000000000020002000200010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Decoded
····EL PSY KONGROO····························································································
NTRUDocumentation
All projects

Project

NTRU

Introduction

In the post-quantum era, most of the cryptographic algorithms that are in widespread use today will be rendered obsolete. Polynomial-time quantum algorithms such as Shor's Algorithm have already been discovered, and threaten the security of cryptosystems relying on integer factorization, the Discrete Logarithm Problem (DLP), and Elliptic Curve Discrete Logarithm Problem (ECDLP). For example, the ECDLP with 256-bit modulus (corresponding to a 128-bit security level) will become feasible to solve once a quantum computer with ≈2330\approx 2330 qubits and ≈126\approx 126 billion Toffoli gates is created, assuming it doesn't succumb to quantum noise [Roetteler et al. 2017].

Emerging from this chaos, lattice-based cryptosystems have moved to the forefront of post-quantum cryptography, bringing forth new "hard" problems like the Shortest Vector Problem (SVP) and the Closest Vector Problem (CVP). One of the earliest lattice-based cryptosystems to be devised is NTRU. As of the time this document is written, no polynomial-time quantum algorithm has been discovered that can break the NTRU cryptosystem, even after 30 years of its existence.

This writeup will provide a brief overview of the NTRU cryptosystem, including the necessary background in convolution polynomial rings and the core encryption and decryption algorithms.

Notice.

This code is not intended for use in security-critical systems. It was conceived as an interpretable (and therefore unoptimized) implementation of the legacy NTRUEncrypt cryptosystem. For production applications, use modern key encapsulation mechanisms such as Streamlined NTRU Prime (sntrup761) or ML-KEM (FIPS 203).

Note.

I revised the implementation of this project in 2026. The following expository writeup was originally submitted in 2023 for my MATH 1580 (Cryptography) final project at Brown University. In 2024, Alex Khosrowshahi and I implemented this cryptosystem for use in our CSCI 1680 (Computer Networks) final project.


Installation

The crate requires only the Rust toolchain. Add it to an existing Rust project as a Git dependency.

Cargo.toml
[dependencies]
ntru-encrypt = { git = "https://github.com/starboi-63/ntru-encrypt" }

Usage

main.rs
use ntru_encrypt::{NtruKeyPair, NtruParams};

fn main() {
    let message = b"Les sanglots longs des violons de l'automne".to_vec();

    let keys       = NtruKeyPair::new(NtruParams::default());     // Keygen
    let ciphertext = keys.public.encrypt_bytes(message.clone());  // Encrypt
    let recovered  = keys.private.decrypt_to_bytes(ciphertext);   // Decrypt

    assert_eq!(recovered, message);
}

Background

We assume the reader is familiar with the basics of ring theory and a little bit of algebraic number theory, and proceed accordingly. For a more thorough introduction to these topics, a reader is encouraged to reference [Gallian 2010] and [Hoffstein et al. 2008].

Definition 4.1 (Ring of Convolution Polynomials)

Choose a positive integer NN. We define the ring of convolution polynomials of rank NN as the quotient ring

R=Z[x]⟨xN−1⟩.R = \frac{\Z[x]}{\langle x^N-1 \rangle}.

Likewise, we define the ring of convolution polynomials modulo qq as the quotient ring

Rq=(Z/qZ)[x]⟨xN−1⟩.R_q = \frac{(\Z/q\Z)[x]}{\langle x^N-1 \rangle}.

Next, we show that in both RR and RqR_q, quotient ring elements of the form a(x)+⟨xN−1⟩\poly{a}+\langle x^N-1\rangle can be uniquely expressed by coset representatives r(x)\poly{r} with deg⁡(r(x))<N\deg(\poly{r})< N. We prove the following proposition with RR, but some slight modifications allow similar results to be proven for RqR_q.

Proposition 4.2 (Unique Coset Representatives)

Every congruence class a(x)‾\overline{\poly{a}} of RR (or RqR_q) can be uniquely represented as some polynomial,

r(x)=a0+a1x+⋯+aN−1xN−1,r(x) = a_0 + a_1x + \cdots + a_{N-1}x^{N-1},

of degree less than NN with coefficients in Z\Z (or Z/qZ\Z/q\Z). Additionally, a(x)≡r(x)(modxN−1)\poly{a}\equiv \poly{r}\pmod{x^N-1}.

Let a(x)+⟨xN−1⟩∈R\poly{a} + \langle x^N-1\rangle \in R. We can use high-school polynomial long division to write

k(x))xN−1) a(x))‾−⋮(‾r(x).)\begin{array}{r} \poly{k}\quad\phantom{)} \\ x^N-1{\overline{\smash{\big)}\,\quad\poly{a}\quad\phantom{)}}} \\ \underline{-\quad\vdots\qquad\phantom{(}} \\ \poly{r}\rlap{.}\quad\phantom{)} \\ \end{array}

Here, k(x)∈Z[x]\poly{k}\in\Z[x] is the quotient, and r(x)∈Z[x]\poly{r}\in\Z[x] is the remainder satisfying deg⁡(r(x))<N\deg(\poly{r})<N, which exist because the polynomial xN−1x^N-1 is monic. Thus,

a(x)=k(x)⋅(xN−1)+r(x).\poly{a} = \poly{k}\cdot (x^N-1) + \poly{r}.

If r(x)=0\poly{r}=0, then a(x)∈⟨xN−1⟩\poly{a}\in\langle x^N-1\rangle so that a(x)‾=0\overline{\poly{a}}=0. Otherwise, we can take both sides modulo xN−1x^N-1 to get a(x)≡r(x)(modxN−1)\poly{a}\equiv \poly{r}\pmod{x^N-1}. This proves the existence of a coset representative r(x)\poly{r} for a(x)‾\overline{\poly{a}} with degree less than NN. To show uniqueness, suppose that r(x)\poly{r} and r′(x)\poly{r'} are two such representatives. Then,

r(x)−r′(x)≡a(x)−a(x)≡0(modxN−1),\poly{r}-\poly{r'}\equiv \poly{a}-\poly{a}\equiv 0\pmod{x^N-1},

so that r(x)−r′(x)∈⟨xN−1⟩\poly{r}-\poly{r'}\in\langle x^N-1\rangle. But since deg⁡(r(x)−r′(x))<N\deg(\poly{r}-\poly{r'})<N, we must have r(x)−r′(x)=0\poly{r}-\poly{r'}=0, which implies that r(x)=r′(x)\poly{r}=\poly{r'} as desired.

Proposition 4.2 reveals that we only need to consider polynomials of degree less than NN when working in RR and RqR_q. This is because the two rings are formed by taking a quotient with the ideal ⟨xN−1⟩\langle x^N-1\rangle, which effectively sets xN−1x^N-1 to zero. Equivalently, you can think of this quotient as replacing any occurrence of xNx^N with the multiplicative identity 11, so that we may reduce exponents modulo NN.

A direct consequence is that polynomials in RR or RqR_q can be efficiently represented as vectors based on their coefficients in Z\Z or Z/qZ\Z/q\Z, thanks to the existence of the bijection

r0+r1x+⋯+rN−1xN−1∈R or Rq ⟷bij (r0,…,rN−1)∈ZN or (Z/qZ)N.r_0 + r_1x + \cdots + r_{N-1}x^{N-1}\in R\text{ or }R_q\ \overset{\text{bij}}{\longleftrightarrow}\ (r_0,\ldots, r_{N-1})\in\Z^N\text{ or } (\Z/q\Z)^N.

Hence, from this point forward we will view polynomials in RR as lattice elements in ZN\Z^{N}. Now, we are ready to define addition and multiplication in RR and RqR_q.

Definition 4.3 (Addition of Convolution Polynomials)

Let a(x),b(x)∈R\poly{a}, \poly{b}\in R or RqR_q. We define the sum of a(x)\poly{a} and b(x)\poly{b} as:

a(x)+b(x) ⟷ (a0+b0,a1+b1,…,aN−1+bN−1).\poly{a} + \poly{b}\ \longleftrightarrow\ (a_0+b_0, a_1+b_1, \ldots, a_{N-1}+b_{N-1}).

Definition 4.4 (Multiplication of Convolution Polynomials)

Let a(x),b(x)∈R\poly{a}, \poly{b}\in R or RqR_q. We define the product of a(x)\poly{a} and b(x)\poly{b} as:

a(x)⋆b(x) ⟷ (c0,c1,…,cN−1),\poly{a}\star\poly{b}\ \longleftrightarrow\ (c_0, c_1, \ldots, c_{N-1}),

where

ck=∑i=0N−1ai b(k−i) mod N.c_k = \sum_{i=0}^{N-1} a_i\, b_{(k-i)\bmod{N}}.

This definition for multiplication can be obtained by taking the product of both polynomials in the standard sense and substituting xN=1x^N=1 as mentioned earlier. We note that this "⋆\star" operator can also be used between two vectors in ZN\Z^N or (Z/qZ)N(\Z/q\Z)^N, and the resulting vector simply has entries matching the coefficients of the product of the corresponding polynomials.

There is a natural ring homomorphism from R→RqR\to R_q that reduces the coefficients of a polynomial a(x)∈R\poly{a}\in R modulo qq. The map for the reverse direction from Rq→RR_q\to R is sufficiently complicated to warrant a separate definition.

Definition 4.5 (Center Lift from RqR_q to RR)

Let a(x)∈Rq\poly{a}\in R_q. The center lift of a(x)\poly{a} is the unique polynomial a∗(x)∈R\poly{a^*}\in R that satisfies

a∗(x)≡a(x)(modq),\poly{a^*}\equiv \poly{a}\pmod{q},

where the coefficients of a∗(x)\poly{a^*} are in the range (−12q,12q](-\frac{1}{2}q,\frac{1}{2}q].

Finally, for polynomials lifted out of the ring R3R_3 (ternary polynomials), we use the following notation.

Notation (Ternary Polynomial Sets)

Let d1d_1 and d2d_2 be positive integers. Define

T(d1,d2)={a(x)∈R:a(x) has d1 coefficients equal to 1,a(x) has d2 coefficients equal to −1,a(x) has all other coefficients equal to 0}.T(d_1, d_2) = \left\{ \poly{a} \in R : \begin{array}{l} \poly{a} \text{ has } d_1 \text{ coefficients equal to } 1, \\ \poly{a} \text{ has } d_2 \text{ coefficients equal to } -1, \\ \poly{a} \text{ has all other coefficients equal to } 0 \end{array} \right\}.

The NTRU Cryptosystem

At last, we are ready to cover the NTRU cryptosystem itself. This cryptosystem is best explained using convolution polynomials, but it is actually closely tied to lattices and the SVP and CVP mentioned in the introduction. To get started, suppose Alice wants to receive a secret message from Bob. Alice (or some other trusted third party) chooses the following public parameters:

Definition 5.1 (NTRU Parameters)

(N,p,q,d)(N,p,q,d) with NN and pp prime, and gcd⁡(N,q)=gcd⁡(p,q)=1\gcd(N,q)=\gcd(p,q)=1. Additionally, q>(6d+1)pq>(6d+1)p and 2d+1≤N2d+1\le N (so that T(d+1,d)T(d+1,d) is nonempty).

For simplicity, we assume qq is prime, so that Z/qZ\Z/q\Z is a field and inverses in RqR_q can be computed by the Extended Euclidean Algorithm. Hoffstein et al. [2008] do not explicitly require this (standard parameter sets take q=2kq=2^k and obtain inverses via Hensel's Lifting Lemma). The exact reasoning for why the parameters are chosen as such will become apparent in the proof of Proposition 6.1. Using these public parameters, we can define the structures within which NTRUEncrypt operates. Given choices of NN and two moduli pp and qq, the NTRU cryptosystem utilizes three different convolution polynomial rings.

Definition 5.2 (NTRU Rings)

R=Z[x]⟨xN−1⟩,Rp=(Z/pZ)[x]⟨xN−1⟩,Rq=(Z/qZ)[x]⟨xN−1⟩R = \frac{\Z[x]}{\langle x^N-1\rangle},\qquad R_p = \frac{(\Z/p\Z)[x]}{\langle x^N-1\rangle},\qquad R_q = \frac{(\Z/q\Z)[x]}{\langle x^N-1\rangle}

Alice can then generate a private key composed of two randomly chosen ternary polynomials.

Definition 5.3 (NTRU Private Key)

f(x)∈T(d+1,d)andg(x)∈T(d,d)\poly{f}\in T(d+1,d)\qquad\text{and}\qquad\poly{g}\in T(d,d)

Here, it's important to note that f(x)\poly{f} must have a multiplicative inverse in both RpR_p and RqR_q. If this is not the case, then Alice must choose a new f(x)\poly{f} and try again until she finds . Assuming Alice has gone through this process, let

Fp(x)=f(x)−1∈RpandFq(x)=f(x)−1∈Rq.\poly{F_p} = \poly{f}^{-1}\in R_p\qquad\text{and}\qquad\poly{F_q} = \poly{f}^{-1}\in R_q.
Existence of polynomial multiplicative inverse

A polynomial f(x)∈Rq\poly{f}\in R_q is a unit   ⟺  \iff gcd⁡(f(x),xN−1)=1\gcd(\poly{f}, x^N-1)=1 in (Z/qZ)[x](\Z/q\Z)[x]. This is because Bézout's Identity ensures that there exist polynomials s(x),t(x)∈(Z/qZ)[x]\poly{s}, \poly{t}\in (\Z/q\Z)[x] such that s(x)f(x)+t(x)(xN−1)=1\poly{s}\poly{f}+\poly{t}(x^N-1)=1. Reducing both sides modulo xN−1x^N-1 reveals that s(x)⋆f(x)=1\poly{s}\star\poly{f}=1, so s(x)=f(x)−1\poly{s}=\poly{f}^{-1} in RqR_q.

At this point, Alice can compute her public key.

Definition 5.4 (NTRU Public Key)

h(x)=Fq(x)⋆g(x) in Rq\poly{h} = \poly{F_q}\star\poly{g}\text{ in }R_q

Now, we are almost ready to encrypt a message that Bob would like to send to Alice, but first, we must define what constitutes valid plaintext in the NTRU cryptosystem.

Definition 5.5 (NTRU Plaintext)

A plaintext m(x)∈R\poly{m}\in R is a polynomial with coefficients mim_i in the range (−12p,12p](-\frac{1}{2}p, \frac{1}{2}p].

Notice that we've constructed plaintext as the center lift of a polynomial in RpR_p (equivalently, we could have defined plaintext as a polynomial in RpR_p to begin with). Concretely, m(x)\poly{m} can be constructed by representing each byte of a message in base-pp and shifting the digits from [0,p)[0,p) to (−12p,12p](-\frac{1}{2}p, \frac{1}{2}p] to obtain the polynomial's coefficients.

Figure 5.1 (Message Encoding)

LChar→76Byte→002211Base 3→00-1-111Center lift
m(x)m(x)Coefficients
For p=3p=3, each UTF-8 byte becomes six coefficients in {−1,0,1}\{-1,0,1\}.

And now, we turn to the much-anticipated encryption algorithm.

Definition 5.6 (NTRU Encryption Algorithm)

Let r(x)∈T(d,d)\poly{r}\in T(d,d) be random. The ciphertext e(x)∈Rq\poly{e}\in R_q is given by

e(x)≡p h(x)⋆r(x)+m(x)(modq).\poly{e} \equiv p\,\poly{h}\star\poly{r}+\poly{m}\pmod{q}.

To decrypt this ciphertext, Alice must go through the two steps outlined below.

Definition 5.7 (NTRU Decryption Algorithm)

First, compute

a(x)≡f(x)⋆e(x)(modq).\poly{a}\equiv\poly{f}\star\poly{e}\pmod{q}.

Take the center lift of this value to obtain a∗(x)\poly{a^*}. The plaintext can be recovered by taking

m(x)≡Fp(x)⋆a∗(x)(modp).\poly{m}\equiv \poly{F_p}\star\poly{a^*}\pmod{p}.

The following diagram encrypts the message “Les sanglots longs des violons de l'automne”. Select values of pp and NN to update dd, qq, and the resulting ciphertext.

Figure 5.2 (Encryption and Decryption)

pp
NN
d=147d = 147q=2657q = 2657
m(x)m(x)282 center-lifted base-3 coefficients
Plaintext and ciphertext coefficients for the selected parameters.

Correctness

We quickly verify that this approach to encryption and decryption is indeed correct.

Proposition 6.1 (Decryption Correctness)

If the NTRU encryption parameters (N,p,q,d)(N,p,q,d) are chosen to satisfy the criteria in Definition 5.1, then the encryption/decryption algorithm is correct and yields the plaintext m(x)\poly{m}.

Notice that

a(x)≡f(x)⋆e(x)(Definition 5.7)≡f(x)⋆[p h(x)⋆r(x)+m(x)](Definition 5.6)≡f(x)⋆[p[Fq(x)⋆g(x)]⋆r(x)+m(x)](Definition 5.4)≡p f(x)⋆Fq(x)⋆g(x)⋆r(x)+f(x)⋆m(x)(Distributive Law)≡p g(x)⋆r(x)+f(x)⋆m(x)(modq).(Multiplicative Inverse)\begin{align*} &\poly{a} \\ &\equiv \poly{f}\star\poly{e} & \text{(}\href{#def:decryption}{\text{Definition 5.7}}\text{)} \\ &\equiv \poly{f}\star\big[p\,\poly{h}\star\poly{r}+\poly{m}\big] & \text{(}\href{#def:encryption}{\text{Definition 5.6}}\text{)} \\ &\equiv \poly{f}\star\big[p\big[\poly{F_q}\star\poly{g}\big]\star\poly{r}+\poly{m}\big] & \text{(}\href{#def:publickey}{\text{Definition 5.4}}\text{)} \\ &\equiv p\,\poly{f}\star\poly{F_q}\star\poly{g}\star\poly{r}+\poly{f}\star\poly{m} & \text{(Distributive Law)} \\ &\equiv p\,\poly{g}\star\poly{r}+\poly{f}\star\poly{m}\pmod{q}. & \text{(Multiplicative Inverse)} \end{align*}

For a moment, consider the last line, p g(x)⋆r(x)+f(x)⋆m(x)p\,\poly{g}\star\poly{r}+\poly{f}\star\poly{m}, as a convolution polynomial in RR rather than RqR_q. Recall that we have chosen g(x)\poly{g} and r(x)\poly{r} to be ternary polynomials in T(d,d)T(d,d). Hence, even if all of their 11 or −1-1 values line up, the maximum coefficient in g(x)⋆r(x)\poly{g}\star\poly{r} is 2d2d. Additionally, recall that f(x)\poly{f} is in T(d+1,d)T(d+1,d) and the coefficients of m(x)\poly{m} are in the range −12p<mi≤12p-\frac{1}{2}p< m_i\leq \frac{1}{2}p. Hence, the largest possible coefficient in f(x)⋆m(x)\poly{f}\star\poly{m} is (2d+1)12p(2d+1)\frac{1}{2}p. Thus, the sum of these two convolution polynomials (with pp multiplied onto the left convolution) will have coefficients no larger than

2dp+(2d+1)12p=2dp+dp+12p=(3d+12)p.2dp + (2d+1)\frac{1}{2}p=2dp+dp+\frac{1}{2}p=\left(3d+\frac{1}{2}\right)p.

So, we were correct in choosing q>(6d+1)pq>(6d+1)p in Definition 5.1, as this ensures that the coefficients of pg(x)⋆r(x)+f(x)⋆m(x)p\poly{g}\star\poly{r}+\poly{f}\star\poly{m} are constrained to the range −12q<ai≤12q-\frac{1}{2}q< a_i\leq \frac{1}{2}q. Therefore, when a(x)∈Rq\poly{a}\in R_q is lifted to a∗(x)∈R\poly{a^*}\in R, we get an equality rather than a congruence:

a∗(x)=pg(x)⋆r(x)+f(x)⋆m(x).\poly{a^*}=p\poly{g}\star\poly{r}+\poly{f}\star\poly{m}.

Finally, we can compute

Fp(x)⋆a∗(x)≡Fp(x)⋆(pg(x)⋆r(x)+f(x)⋆m(x))(Definition 5.7)≡Fp(x)⋆f(x)⋆m(x)(Modular Arithmetic)≡m(x)(modp).(Modular Arithmetic)\begin{align*} &\poly{F_p}\star\poly{a^*} \\ &\equiv \poly{F_p}\star(p\poly{g}\star\poly{r}+\poly{f}\star\poly{m}) & \text{(}\href{#def:decryption}{\text{Definition 5.7}}\text{)} \\ &\equiv \poly{F_p}\star\poly{f}\star\poly{m} & \text{(Modular Arithmetic)} \\ &\equiv \poly{m}\pmod{p}. & \text{(Modular Arithmetic)} \end{align*}

as desired.


Summary

To reiterate, NTRUEncrypt is a public-key cryptosystem (which means two parties can securely send messages to each other while only revealing their public keys). The following diagram shows how a full exchange between Alice and Bob proceeds, including which mathematical data is sent over a public channel.

Figure 6.1 (Secret Exchange)

Alice
f∈T(d+1, d),    g∈T(d, d)f \in T(d+1,\,d),\;\; g \in T(d,\,d)
h=Fq⋆g(modq)h = F_q \star g \pmod q
m=m = blessent mon coeur d'une langueur monotone
hheePublic
channel
Bob
m=m = blessent mon coeur d'une langueur monotone
e≡p h⋆r+m(modq)e \equiv p\,h \star r + m \pmod q
A complete NTRUEncrypt exchange from key generation through decryption.

References

  • Martin Roetteler, Michael Naehrig, Krysta M. Svore, and Kristin Lauter. 2017. Quantum resource estimates for computing elliptic curve discrete logarithms. arxiv.org
  • Joseph A. Gallian. 2010. Contemporary Abstract Algebra. Brooks/Cole, Cengage Learning. books.google.com
  • Jeffrey Hoffstein, Jill Pipher, and Joseph H. Silverman. 2008. An Introduction to Mathematical Cryptography. Springer. doi:10.1007/978-0-387-77993-5