Number Theory for Public Key Cryptography

Chinese Remainder Theorem (CRT)

Let pp, qq be relatively prime.

Let n=pqn = pq be the modulus.

Given integers c1c_1 and c2c_2 there exists a unique integer 0x<n0 \le x \lt n such that:

xc1(modp)xc2(modq) \begin{aligned} x &\equiv c_1 \pmod p \\ x &\equiv c_2 \pmod q \end{aligned}
xnpy1c1+nqy2c2(modn) x \equiv \frac{n}{p}y_1c_1 + \frac{n}{q}y_2c_2 \pmod n

Where:

y1(np)1(modp)q1(modp)y2(nq)1(modq)p1(modq) \begin{aligned} y_1 \equiv \left(\frac{n}{p}\right)^{-1} \pmod p \equiv q^{-1} \pmod p \\ y_2 \equiv \left(\frac{n}{q}\right)^{-1} \pmod q \equiv p^{-1} \pmod q \end{aligned}

Condensed Equation:

xqc1(q1(modp))+pc2(p1(modq))(modpq) x \equiv qc_1(q^{-1} \pmod p) + pc_2(p^{-1} \pmod q) \pmod{pq}

Example

Find xx such that x5(mod6)x \equiv 5 \pmod 6 and x33(mod35)x \equiv 33 \pmod {35}:

For y1y_1:

2106y11(mod6)35y11(mod6)5y11(mod6)y15(mod6) \begin{aligned} \frac{210}{6}y_1 &\equiv 1 \pmod 6 \\ 35y_1 &\equiv 1 \pmod 6 \\ 5y_1 &\equiv 1 \pmod 6 \\ y_1 &\equiv 5 \pmod 6 \end{aligned}

Make sure you replace qq with qmodpq \bmod p (assuming q>pq \gt p): otherwise instead of finding 51(mod6)5^{-1} \pmod 6, you will find 61mod356^{-1} \bmod{35}.

For y2y_2:

21035y11(mod35)6y11(mod35)y16(mod35) \begin{aligned} \frac{210}{35}y_1 &\equiv 1 \pmod {35} \\ 6y_1 &\equiv 1 \pmod {35} \\ y_1 &\equiv 6 \pmod {35} \end{aligned}
xnpy1c1+nqy2c2(modn)(3555)+(6633)(mod210)173(mod210) \begin{aligned} x &\equiv \frac{n}{p}y_1c_1 + \frac{n}{q}y_2c_2 \pmod n \\ &\equiv (35 \cdot 5 \cdot 5) + (6 \cdot 6 \cdot 33) \pmod {210} \\ &\equiv 173 \pmod {210} \end{aligned}

Example 2

Find xx such that x5(mod7)x \equiv 5 \pmod{7} and x7(mod10)x \equiv 7 \pmod{10}

gcd(7,10)=1\mathrm{gcd}(7, 10) = 1; pp and qq are relatively prime. Hence, CRT applies.

Hence n=710=70n = 7 \cdot 10 = 70

x10y15+7y27(mod70)50y1+49y2(mod70) \begin{aligned} x &\equiv 10 \cdot y_1 \cdot 5 + 7 \cdot y_2 \cdot 7 &\pmod{70} \\ &\equiv 50 y_1 + 49 y_2 &\pmod{70} \end{aligned}

Where:

Hence:

x50y1+49y2(mod70)505+493(mod70)250+147(mod70)40+7(mod70)47(mod70) \begin{aligned} x &\equiv 50 y_1 + 49 y_2 &\pmod{70} \\ &\equiv 50 \cdot 5 + 49 \cdot 3 &\pmod{70} \\ &\equiv 250 + 147 &\pmod{70} \\ &\equiv 40 + 7 &\pmod{70} \\ &\equiv 47 &\pmod{70} \end{aligned}

Test:

Euler Function

Given the positive integer nn, the Euler function ϕ(n)\phi(n) denotes the number of positive integers less than nn and relatively prime to nn.

e.g. ϕ(10)=4\phi(10) = 4 as Z10={1,3,7,9}\mathbb{Z}^{*}_{10} = \{ 1, 3, 7, 9 \}.

Properties

ϕ(p)=p1\phi(p) = p - 1 where pp is prime.

ϕ(pq)=(p1)(q1)\phi(pq) = (p - 1)(q - 1) where pp and qq are distinct primes.

if n=p1e1ptetn = p_1^{e_1} \dots p_t^{e_t} where pip_i are distinct primes, then:

ϕ(n)=i=1tpiei1(pi1) \phi(n) = \prod_{i = 1}^{t}{p_i^{e_i - 1}(p_i - 1)}

e.g. for 24=233124 = 2^3 \cdot 3^1, ϕ(24)=22(21)30(31)=42=8\phi(24) = 2^2(2 - 1) \cdot 3^0(3 - 1) = 4 \cdot 2 = 8

Fermat’s Theorem

Let pp be a prime; for any integer aa such that 1<ap11 \lt a \le p - 1:

ap1modp=1 a^{p - 1} \mod p = 1

Euler’s Theorem

More general case of Fermat’s theorem.

If gcd(a,n)=1\mathrm{gcd}(a, n) = 1 (i.e. aa and nn coprime) then:

aϕ(n)modn=1 a^{\phi(n)} \mod n = 1

Primality Tests

Testing for primality by trial division not practical for large numbers.

There are many probabilistic methods, although these may fail in exceptional circumstances.

Agrawal, Saxena, Kayal 2002: polynomial time deterministic primality test, although still impractical.

Fermat Primality Test

Fermat’s little theorem: if pp is prime, ap1modp=1a^{p - 1} \mod p = 1 for all aa such that gcd(a,p)=1\mathrm{gcd}(a, p) = 1.

If an1modn1a^{n - 1} \mod n \neq 1, nn is NOT a prime.

The probability of failure can be reduced by repeating the test with different base values of aa.

Given the number nn to test for primality and kk, the number of times to run the test:

Some composite numbers such as 561561 and 11051105 are called Carmichael numbers; the Fermat primality test always returns these numbers as being probable primes.

Example

Check if 517517 is prime, running the test at most four times with values a=2,3,11,17a = 2, 3, 11, 17.

n1=5171=516=43322n - 1 = 517 - 1 = 516 = 43 \cdot 3 \cdot 2^2

Using a=2a = 2:

2516mod517=(243mod517)12mod517=(382)12mod517=((382)3mod517)4mod517=(28)4mod517=4601 \begin{aligned} 2^{516} \bmod{517} &= \left(2^{43} \bmod{517}\right)^{12} \bmod{517} \\ &= (382)^{12} \bmod{517} \\ &= \left((382)^3 \bmod{517}\right)^4 \bmod{517} \\ &= (28)^{4} \bmod{517} \\ &= 460 \neq 1 \end{aligned}

Hence it is composite, so we do not need to make further checks.

Miller-Rabin Test

Most widely used test for generating large prime numbers. It is guaranteed to detect composite numbers of the test is run sufficiently many times.

Modular square root of 11: a number xx such that x2modn=1x^2 \mod n = 1. In other words, the root of the equation x21(x1)(x+1)0(modn)x^2 - 1 \equiv (x - 1)(x + 1) \equiv 0 \pmod n.

There are four square roots of 11 when n=pqn = pq (i.e. composite):

If xx is a non-trivial square root of 11 then gcd(x1,n)\mathrm{gcd}(x - 1, n) is a non-trivial factor of nn. Hence, nn must be composite.

Miller-Rabin Algorithm

Let nn and uu be odd and find vv such that n1=2vun - 1 = 2^v u. Then:

  1. Pick aa at random such that 1<a<n11 \lt a \lt n - 1
  2. Set b=aumodnb = a^u \mod n
  3. If b=1b = 1, return probable prime
  4. For j=0j = 0 to v1v - 1:
    • If b=1b = -1, return probable prime
    • Else, set b=b2modnb = b^2 \mod n
  5. Return composite

If the test returns probable prime, nn may be composite.

If nn is composite then then test returns probable prime with at most a probability of 0.250.25.

As the algorithm is run kk times, it outputs probable prime when nn is composite with a probability of no more than 0.25k0.25^k.

In practice, the error probability is smaller: when aa is given the first seven primes, the smallest composite the algorithm returns as being a probable prime is 341,550,071,728,321341,550,071,728,321

Why The Miller-Rabin Test Works

Given a random aa such that 0<a<n10 \lt a \lt n - 1, with n1n - 1 being equal to 2vu2^v u.

Hence, bb can be given the values {aumodn,a2umodn,,a2vumodn}\{ a^u \mod n, a^{2u} \mod n, \dots, a^{2^{v}u} \mod n \}.

Each number on the sequence is the square of the previous number (modulo nn).

If nn is prime, a2vumodn=an1=1a^{2^v u} \mod n = a^{n - 1} = 1 (Fermat’s theorem). Hence:

If a non-trivial square root of 11 is found, nn must be composite.

Example

Let n=1729n = 1729 (a Charmichael number)

n1=1728=64×27=26×27n - 1 = 1728 = 64 \times 27 = 2^6 \times 27. Hence, v=6v = 6, u=27u = 27.

  1. Choose a=2a = 2
  2. b=227mod1729=645b = 2^{27} \bmod{1729} = 645
  3. b1b \neq 1 so:
    • b=6452modn=1065b = 645^2 \bmod n = 1065
    • b=10652modn=1b = 1065^2 \bmod n = 1
    • b=12modn=1b = 1^2 \bmod n = 1
    • \dots
    • Hence, b=1=n1b = -1 = n - 1 will never occur
    • Hence, nn must be composite

NB: 10641064 is a non-trivial square root of 11 modulo 17291729: gcd(1729,1064)=133\mathrm{gcd}(1729, 1064) = 133 is a factor of 17291729.

Example 2

Let n=17n = 17.

n1=16=2vun - 1 = 16 = 2^vu.

16=2516 = 2^5 and uu must be odd. Hence, the only valid values are u=1u = 1 and v=5v = 5.

Let 1<a<n11 < a < n - 1 be a prime. Pick a=3a = 3:

Hence, 1717 is a probable prime. Repeat for other values of aa until satisfied.

Generation of Large Primes

  1. Choose a random odd integer nn of the same number of bits as the required prime
  2. Test if rr is divisible by any of a small list of primes
  3. If not:
    • Apply the Miller-Rabin test five random or fixed based values of aa
    • If rr fails any test, set r=r+2r = r + 2 and return to step 2

This incremental method does not produce completely random primes. If this is an issue start from step 1 if rr is found to be composite.

Basic Complexity Theory

Two aspects:

Express this complexity using ‘big O’ notation; in terms of the space and time required to solve the problem for a given size.

Hard Problems

Integer factorization: given an integer of mm bits, find its prime factors.

Discrete logarithm problem (base 2): given a prime pp of mm bits and an integer 0<y<p0 \lt y \lt p, find xx such that y=2xmodpy = 2^x \mod p

There are no known polynomial algorithms to solve the problems; the best known algorithms are sub-exponential.

Factorization Problem

Trial by division: exponential time algorithm

Some fast methods exist, although they only apply to integers with particular properties.

Bets known general method: number field sieve, a sub-exponential algorithm.

As n=pqn = pq, nn is large even with small keys; brute force search of 128-bit AES keys takes roughly the same computational effort as factorization of a 3072-bit number with two factors of roughly equal size.

Discrete Logarithm Problem

Given a prime pp and generator gg of Zp\mathbb{Z}_p^*, the discrete logarithm is:

Given yZpy \in \mathbb{Z}_p^*, find xx such that y=gxmodpy = g^x \bmod p.

(i.e. find the power given the remainder)

This can be written as x=logg(y)(modp)x = log_g(y) \pmod p.

If pp is large enough, the problem is hard. Given the same length, the security level is equal RSA (and hence should be at least 2048 bits).

Example

Find xx such that 2x=3mod52^x = 3 \bmod 5:

1:21mod5=22:22mod5=4mod5=43:23mod5=8mod5=3 \begin{aligned} 1&: 2^1 \bmod 5 = 2 \\ 2&: 2^{2} \bmod 5 = 4 \bmod 5 = 4 \\ 3&: 2^{3} \bmod 5 = 8 \bmod 5 = 3 \\ \end{aligned}

Hence x=3x = 3.

Example 2

Find the discrete logarithm of the number 44 with regard to base 22 for the modulus p=7p = 7.

i.e. solve 2x=4mod72^x = 4 \bmod 7.

1:212(mod7)2:22224(mod7)3:234281(mod7)4:24122(mod7)5:24224(mod7) \begin{aligned} 1&: 2^1 &\equiv 2 &\pmod 7 \\ 2&: 2^2 \equiv 2 \cdot 2 &\equiv 4 &\pmod 7 \\ 3&: 2^3 \equiv 4 \cdot 2 \equiv 8 &\equiv 1 &\pmod 7 \\ 4&: 2^4 \equiv 1 \cdot 2 &\equiv 2 &\pmod 7 \\ 5&: 2^4 \equiv 2 \cdot 2 &\equiv 4 &\pmod 7 \end{aligned}

There is a cycle with powers of 22 modulo 77 taking on the values 2,4,12, 4, 1. Hence, x=2x = 2.