RSA for the Impatient

A deep dive into the mathematics of RSA encryption

(except you’ve been pushed rather than diving on your own volition.)

What is RSA?

Mathematical Definition

Encryption:

\[ C = M^e\ \text{mod}\ n \]

Decryption:

\[ M = C^d\ \text{mod}\ n \]

Why it works

Encryption is like a lock, easy with the key, very hard without it.

The bolt in this case is that factoring large numbers is hard.

Full Operation

I. Key generation

Full Operation

I. Key generation

Example:

\[ \begin{align} p &= 7 \\ q &= 13 \\ n &= 91 \\ \phi(n) &= (p - 1)(q - 1) \\ \end{align} \]

\[ \begin{align} \phi(n) &= 72 \\ e &= 17 \\ e \times d &= 1\ \text{mod}\ 72 \\ d &= 17 \end{align} \]

Full Operation

II. Encryption

To encrypt a message \(M\), represent it as a number smaller than \(n\) and compute \(C = M^e\ \text{mod}\ n\).

Full Operation

II. Encryption

To encrypt a message \(M\), represent it as a number smaller than \(n\) and compute \(C = M^e\ \text{mod}\ n\).

Example

Sending the “message” 213, a six-bit number represented as 3 2-bit couplets. We can represent this in base 10 as \(39\).

\[ \begin{align} C &= 39^{17}\ \text{mod}\ 91 \\ C &= 65 \end{align} \]

Full Operation

III. Decryption

Simply undo encryption with \(d\)!

\[ M = C^d\ \text{mod}\ n \]

Full Operation

III. Decryption

Simply undo encryption with \(d\)!

\[ M = C^d\ \text{mod}\ n \]

Example

\[ 65^{17}\ \text{mod}\ 91=39 \]

Which, we can de-numerical-ize as 213, left to right, via its couplets, 10 01 11.

Cool, we did it. We’re cryptographers now.

Optimizations

I. Chinese Remainder Theorem (CRT)

The Chinese Remainder Theorem states that, given a set of congruences such that all \(m\)s are coprime:

\[ x \equiv a_1\ \text{mod}\ m_1 \\ x \equiv a_2\ \text{mod}\ m_2 \\ \vdots \\ x \equiv a_n\ \text{mod}\ m_n \\ \]

Optimizations

I. Chinese Remainder Theorem

we can solve for \(x\) with the congruence:

\[ x \equiv (\sum_{i=1}^n a_i \cdot M_i \cdot M_{i}^{-1})\ \text{mod}\ N \]

\[ \prod^n_{i=1} m_i \]

Optimizations

I. Chinese Remainder Theorem

The CRT lets us break \(C^d\ \text{mod}\ n\) into two congruences, each with prime moduli.

\[ M = C^d\ \text{mod}\ p \\ M = C^d\ \text{mod}\ q \]

Once we do this, we get a smaller exponent through Euler’s Theorem, which tells us that:

\[ a^{x\ \text{mod}\ \phi(p)} \equiv a^x\ (\text{mod}\ p) \]

When \(x\) and \(p\) are coprime.

Optimizations

I. Chinese Remainder Theorem

Which gives us

\[ M \equiv C^{d\ \text{mod}\ \phi(p)}\ (\text{mod}\ p) \\ M \equiv C^{d\ \text{mod}\ \phi(q)}\ (\text{mod}\ q) \]

Which we can then use the CRT to recombine.

E2E Example

The straight-forward way

When

\[ \begin{align} p &= 13 \\ q &= 17 \\ M &= 39 \\ e &= 19 \\ \end{align} \]

\[ C = 39^{19}\ (\text{mod}\ 221) \]

Which gives us \(91\).

E2E Example

The optimized way

When

\[ \begin{align} p &= 13 \\ q &= 17 \\ M &= 39 \\ e &= 19 \\ \end{align} \]

We have the two congruences through Euler’s Theorem and the CRT:

\[ C \equiv 39^7\ \text{mod}\ 13 \\ C \equiv 39^3\ \text{mod}\ 17 \]

E2E Example

The optimized way

Which we recombine:

\[ C \equiv (39^7 \cdot 17 \cdot 10) + (39^3 \cdot 13 \cdot 4)\ (\text{mod}\ 221) \]

Also giving us \(91\).

We did it! We’re optimizing cryptographers now.

E2E Example

Who cares?

In the optimized example, we’re multiplying 10 (7+3) 39’s, while in the simple example, we’re doing it 19 times.

In reality, \(e\), \(p\), and \(q\) are all very large numbers.

Advanced Optimization

Montgomery Multiplication

Advanced Optimization

Montgomery Multiplication: Initialization

  1. Choose a power of two that’s greater than \(m\), this is \(r\).

    r = 2 ^ ceiling (logBase 2 (fromIntegral m))
  2. Square \(r\), modulo \(m\). This we’ll call \(r_2\).

    r2 = r * r `mod` m
  3. Compute the multiplicative inverse of \(-m\ \text{mod}\ r\), this is \(m'\)

    m' = inv (negate m) r

Advanced Optimization

Montgomery Multiplication: Montgomery Form

  1. Put \(a\) into “montgomery form” by multiplying \(a * r_2\), this is \(a_m\).

    am = a * r2
  2. Do a montgomery reduction of \(a\), this is \(a'\). This routine we’ll call \(\text{reduce}\).

    1. Multiply \(a_m\) by \(m'\ \text{mod}\ r\), I’ll call this \(a_m'\)

      am' = am * m' `mod` r
    2. Do \(\cfrac{a_m + a_m' * m}{r}\). This temp value we’ll call \(t\).

      t = am + am' * m `div` r
    3. The result is \(t\) if \(t < m\), otherwise it’s \(t - m\).

       if t < m
         then t
         else t - m

Advanced Optimization

Montgomery Multiplication

Compute multiplications in montgomery form.

  1. Put \(b\) into Montgomery form and then reduce it as well. This is \(b'\).

    bm = b * r2
    b' = reduce bm
  2. compute \(c'\), which is a reduction as well, but this time of \(a' * b'\) instead of \(a\) or \(b\) times \(r_2\).,

    c' = reduce (a' * b')
  3. Finally, the final result is one last reduction of \(c'\).

    reduce c'

Who cares?