Lecture Notes on RSA Cryptosystem and Chinese Remainder Theorem

Assignment Updates

  • Assignment 3 grades are posted with comments available for review.
  • Assignment 4 is due today.
  • Assignment 5, focusing on the RSA cryptosystem's programming aspect, will be released today and will have undergraduate and graduate versions.

RSA Cryptosystem Recap

  • Lectures covered:
    • Introduction to public-key cryptosystems.
    • Mathematical preliminaries.
    • RSA cryptosystem description and correctness proof.
  • The correctness proof had a minor issue that needs correction.

RSA Implementation Guidelines

  • Use integer arithmetic exclusively. Avoid floating-point or real number arithmetic.
  • Implement algorithms taught in the course yourself without using library functions.

RSA Key Generation Algorithm

  • Bob selects two large prime numbers, pp and qq.
  • Bob computes n=p×qn = p \times q.
  • Bob computes ϕ(n)=(p1)(q1)\phi(n) = (p-1)(q-1).
  • Bob picks an integer bb in Z<em>ϕ(n)Z<em>{\phi(n)} such that gcd(b,ϕ(n))=1gcd(b, \phi(n)) = 1. This means bb has an inverse in Z</em>ϕ(n)Z</em>{\phi(n)}.
  • Bob computes the inverse of bb mod ϕ(n)\phi(n).
  • Public Key: (n,b)(n, b)
  • Private Key: Factorization of nn into (p,q)(p, q) and the decryption exponent aa.

Encryption and Decryption Functions

  • Encryption: E(x)=xbmodnE(x) = x^b \mod n, where xx is the plaintext.
  • Decryption: D(y)=yamodnD(y) = y^a \mod n, where yy is the ciphertext.
  • Both encryption and decryption involve modular exponentiation.

Toy Example of RSA

  • p=7p = 7, q=11q = 11
  • n=7×11=77n = 7 \times 11 = 77
  • ϕ(n)=(71)(111)=6×10=60\phi(n) = (7-1)(11-1) = 6 \times 10 = 60
  • Pick b=13b = 13 in Z60Z_{60}
  • Check if 13 has an inverse in Z60Z_{60} by computing gcd(13,60)gcd(13, 60).
    • gcd(13,60)=1gcd(13, 60) = 1
  • Compute aa, the inverse of bb (13) mod 60, using the extended Euclidean algorithm. a=37a = 37

Public and Private Keys (Toy Example)

  • Public Key: (77,13)(77, 13)
  • Private Key: (7,11,37)(7, 11, 37)

Plaintext and Ciphertext Space

  • Z77Z_{77}

Encryption Example with Small Numbers

  • Plaintext x=15x = 15
  • Encryption: 1513mod7715^{13} \mod 77

Modular Exponentiation Problem

  • Direct computation of x13x^{13} requires 12 multiplications, which is inefficient.

Square and Multiply Algorithm (Russian Peasant Algorithm)

  • Efficient method for modular exponentiation.
  • Write the exponent in binary.

Example: Computing 1513mod7715^{13} \mod 77

  • Binary representation of 13: 1101 (i.e., 13=8+4+113 = 8 + 4 + 1)
  • Compute x2,x4,x8x^2, x^4, x^8 through repeated squaring.
  • x=15x = 15
  • x2=152=22571mod77x^2 = 15^2 = 225 \equiv 71 \mod 77
  • x4=712=504136mod77x^4 = 71^2 = 5041 \equiv 36 \mod 77
  • x8=362=129664mod77x^8 = 36^2 = 1296 \equiv 64 \mod 77
  • x13=x8×x4×x1=64×36×1564mod77x^{13} = x^8 \times x^4 \times x^1 = 64 \times 36 \times 15 \equiv 64 \mod 77

Decryption Example

  • Ciphertext y=64y = 64
  • Compute 6437mod7764^{37} \mod 77.
  • Binary representation of 37: 100101 (i.e., 37=32+4+137 = 32 + 4 + 1)
  • Compute successive powers of yy.
  • y=64y = 64
  • y2=64215mod77y^2 = 64^2 \equiv 15 \mod 77
  • y4=15271mod77y^4 = 15^2 \equiv 71 \mod 77
  • y8=71236mod77y^8 = 71^2 \equiv 36 \mod 77
  • y16=36264mod77y^{16} = 36^2 \equiv 64 \mod 77
  • y32=64215mod77y^{32} = 64^2 \equiv 15 \mod 77
  • y37=y32×y4×y1=15×71×6415mod77y^{37} = y^{32} \times y^4 \times y^1 = 15 \times 71 \times 64 \equiv 15 \mod 77

Formal Description of Square and Multiply Algorithm

  • Goal: Compute xbmodnx^b \mod n
  • Write bb in binary as b=b<em>l1b</em>l2b0b = b<em>{l-1}b</em>{l-2}…b_0 (L bits).
  • Initialize accumulator z=1z = 1.
  • For i=l1i = l-1 down to 0:
    • z=z2modnz = z^2 \mod n
    • If bi=1b_i = 1, then z=z×xmodnz = z \times x \mod n
  • Output zz.

Complexity Analysis

  • Naive method requires b1b-1 modular multiplications.
  • Square and multiply requires O(l)O(l) multiplications.
  • Significant improvement as ll is proportional to log2(b)\log_2(b).

Bit-Level Complexity

  • Let k=logn+1k = \lfloor \log n \rfloor + 1 be the number of bits needed to represent any number in ZnZ_n.
  • Addition/Subtraction: O(k)O(k)
  • Multiplication: O(k2)O(k^2)
  • Exponentiation: O(k3)O(k^3)
  • RSA encryption and decryption can be performed in cubic time.

Practical Considerations

  • RSA is slower than private key cryptosystems like DES.
  • RSA may be 1,700 times slower than DES.
  • Hybrid encryption is used in practice.

Hybrid Encryption

  • Use both public key and private key cryptosystems.
  • Alice wants to send message X to Bob.
    • Bob's public key: kBobk_{Bob}
    • Bob's private key: kBobk'_{Bob}
  • Alice encrypts X using DES with key K: Y=DESK(X)Y = DES_K(X).
  • Alice encrypts DES key K using Bob's public key: Z=E<em>k</em>Bob(KDES)Z = E<em>{k</em>{Bob}}(K_{DES}).
  • Alice sends Y and Z to Bob.
  • Bob decrypts Z using his private key to obtain DES key K: K<em>DES=D</em>kBob(Z)K<em>{DES} = D</em>{k'_{Bob}}(Z).
  • Bob decrypts Y using DES with key K to obtain X: X=DESK1(Y)X = DES_K^{-1}(Y).

Correctness of RSA Cryptosystem and Chinese Remainder Theorem (CRT)

  • Need to show that encryption followed by decryption yields the original message.
  • RSA's plaintext space is Z<em>nZ<em>n, not just Z</em>nZ</em>n^*.
  • Chinese Remainder Theorem is used to prove the correctness of RSA.

Chinese Remainder Theorem (CRT)

  • Let m<em>1,m</em>2,,mrm<em>1, m</em>2, …, m_r be pairwise relatively prime numbers.
  • Let M=m<em>1×m</em>2××mrM = m<em>1 \times m</em>2 \times … \times m_r.
  • CRT states that there is a unique solution to the following system of congruences modulo M:
    • xa<em>1modm</em>1x \equiv a<em>1 \mod m</em>1
    • xa<em>2modm</em>2x \equiv a<em>2 \mod m</em>2
    • \vdots
    • xa<em>rmodm</em>rx \equiv a<em>r \mod m</em>r

Proof of Chinese Remainder Theorem (by Construction)

  • Define M<em>i=M/m</em>iM<em>i = M / m</em>i (product of all mm's except mim_i).
  • gcd(M<em>i,m</em>i)=1gcd(M<em>i, m</em>i) = 1.
  • Therefore, M<em>iM<em>i has an inverse y</em>iy</em>i in Z<em>m</em>iZ<em>{m</em>i}.
  • x=<em>i=1ra</em>iM<em>iy</em>ix = \sum<em>{i=1}^{r} a</em>i M<em>i y</em>i is a solution to the system of congruences.
  • To show this, consider the equation modulo mjm_j:
    • All terms except a<em>jM</em>jy<em>ja<em>j M</em>j y<em>j vanish because they have m</em>jm</em>j as a factor.
    • Since M<em>jy</em>j1modm<em>jM<em>j y</em>j \equiv 1 \mod m<em>j, we have xa</em>jmodmjx \equiv a</em>j \mod m_j.

Uniqueness of Solution

  • Suppose X and Y are both solutions.
  • Then xa<em>imodm</em>ix \equiv a<em>i \mod m</em>i and ya<em>imodm</em>iy \equiv a<em>i \mod m</em>i for all i.
  • Therefore, xymodmix \equiv y \mod m_i for all i.
  • This implies xy0modmix - y \equiv 0 \mod m_i for all i.
  • Thus, mim_i divides xyx - y for all i.
  • Since the m<em>im<em>i are pairwise relatively prime, M=m</em>1×m<em>2××m</em>rM = m</em>1 \times m<em>2 \times … \times m</em>r divides xyx - y.
  • This means xymodMx \equiv y \mod M, so the solution is unique up to modulo M.