Number Theory: Exponentiation with Fast Powering and Montgomery Ladder

17 Dec 2021


§ Motivation

We often use exponentiation over finite fields in cryptography, as solving for aa given g,h,ng, h, n in the discrete log problem ganhg^a \equiv_n h is very difficult. However, our exponents are often quite large and computing them through the naïve method of repeated multiplication is not the fastest method asymptotically.

Suppose we wanted to compute 3387(mod100)3^{387} \pmod{100}, in this case we must compute 387387 multiplications of the base 33 in the naïve method. With fast-powering we can get it down to log2(387)9\log_2(387) \approx 9 steps which have at most 22 multiplications each.

This reduction in the number of steps is doubly important, because naïve integer multiplication in binary is O(n2)\mathcal{O}\left(n^2\right). In cryptography we work with very large bases and exponents, so these time complexities are very important in terms of practicality. So, using both naïve exponentiation and multiplication, we can expect O(n3)\mathcal{O}\left(n^3\right) time complexity. We can get better asymptotic times for multiplication using Karatsuba, Toom-k, or fast-fourier transforms for increasingly larger numbers; thusly reducing the asymptotic run-time of the fast-powering approach.

Additionally, we can use a result from Euler's theorem in certain cases to reduce the exponent before attempting to compute the exponent value.

§ Fast Powering

Most texts refer to this method as Exponentiation by squaring instead of fast-powering.

Fast Powering

Suppose for a large AA we want to compute gA(modN)g^A \pmod{N}, we first want to find the binary expansion of AA:

A=i=0mAi2i=A0+A12+A222++Am2mA = \sum_{i=0}^{m}A_i2^i = A_0 + A_1\cdot2 + A_2\cdot2^2 + \dots + A_m\cdot2^m

Then, for each power of two we compute

g20a0(modN)g21(a0)2a1(modN)g22(a1)2a2(modN)g2m(am1)2am(modN).\begin{align*} g^{2^0} &\equiv a_0\pmod{N}\\ g^{2^1} &\equiv (a_0)^2 \equiv a_1\pmod{N}\\ g^{2^2} &\equiv (a_1)^2 \equiv a_2\pmod{N}\\ &\mkern.5em\vdots\\ g^{2^m} &\equiv (a_{m-1})^2 \equiv a_m\pmod{N}. \end{align*}

Finally, we compute

gAi=0mgAi2igA0(g2)A1(g2m)Am(modN)g^A \equiv \prod_{i=0}^m g^{A_i\cdot 2^i} \equiv g^{A_0} \left(g^2\right)^{A_1}\dots\left(g^{2m}\right)^{A_m} \pmod{N}

§ Montgomery Ladder

Side Channel Attacks

Suppose we performed fast powering, as described previously, for an application like RSA.

In this case Mallory, a malicious actor, wants to recover the our private exponent and has the means to profile the fast-powering algorithm.

Mallory knows the public-key information: the modulus NN and the public exponent ee.

Mallory sends an encrypted message CNmeC \equiv_N m^e to us, and we try to decrypt it with our private key mNmdm \equiv_N m^d.

Because Mallory has profiling capabilities, she can tell when the program is running relatively slower or faster.

By counting the slow (squaring) and faster (multiplication) frames, it is possible for her to recover some information about dd.

The Montgomery ladder method attempts to mitigate the viability of side-channel attacks by performing a squaring and multiplication at each step.

Montgomery Ladder

  • # Inputs: base gg, binary expansion A={Ai}i=0mA = \{A_i\}_{i=0}^{m} of gg
  • function MontgomeryLadder(gg, AA)
    • x1x_1 \leftarrow gg
    • x2x_2 \leftarrow g2g^2
    •  
    • for ii in {m2,,0}\{m - 2, \dots, 0\} do
      • if Ai0A_i \equiv 0 then
        • x2x_2 \leftarrow x1x2x_1 * x_2
        • x1x_1 \leftarrow x12x_1^2
      • else
        • x1x_1 \leftarrow x1x2x_1 * x_2
        • x2x_2 \leftarrow x22x_2^2
    •  
    • return x1x_1