Exponentiation by squaring
In mathematics and computer programming, exponentiating by squaring is a general method for fast computation of large positive integer powers of a number, or more generally of an element of a semigroup, like a polynomial or a square matrix. Some variants are commonly referred to as square-and-multiply algorithms or binary exponentiation. These can be of quite general use, for example in modular arithmetic or powering of matrices. For semigroups for which additive notation is commonly used, like elliptic curves used in cryptography, this method is also referred to as double-and-add.
Basic method
Recursive version
The method is based on the observation that, for any integer, one hasIf the exponent is zero, then the answer is 1. If the exponent is negative then we can reuse the previous formula by rewriting the value using a positive exponent. That is,
Together, these may be implemented directly as the following recursive algorithm:
Inputs: a real number x; an integer n
Output: xn
function exp_by_squaring is
if n < 0 then
return exp_by_squaring
else if n = 0 then
return 1
else if n is even then
return exp_by_squaring
else if n is odd then
return x × exp_by_squaring
end function
In each recursive call, the least-significant digit of the binary representation of is removed. It follows that the number of recursive calls is the number of bits of the binary representation of. So this algorithm computes this number of squares and a lower number of multiplication, which is equal to the number of 1s in the binary representation of. This logarithmic number of operations is to be compared with the trivial algorithm which requires multiplications.
This algorithm is not tail-recursive. This implies that it requires an amount of auxiliary memory that is roughly proportional to the number of recursive calls, or perhaps higher if the amount of data per iteration is increasing.
The algorithms of the next section use a different approach, and the resulting algorithms needs the same number of operations, but use an auxiliary memory that is roughly the same as the memory required to store the result.
With constant auxiliary memory
The variants described in this section are based on the formulaIf one applies recursively this formula, by starting with, one gets eventually an exponent equal to, and the desired result is then the left factor.
This may be implemented as a tail-recursive function:
Function exp_by_squaring
return exp_by_squaring2
Function exp_by_squaring2
if n < 0 then return exp_by_squaring2;
else if n = 0 then return y;
else if n is even then return exp_by_squaring2;
else if n is odd then return exp_by_squaring2.
The iterative version of the algorithm also uses a bounded auxiliary space, and is given by
Function exp_by_squaring_iterative
if n < 0 then
x := 1 / x;
n := -n;
if n = 0 then return 1
y := 1;
while n > 1 do
if n is odd then
y := x * y;
n := n - 1;
x := x * x;
n := n / 2;
return x * y
The correctness of the algorithm results from the fact that is invariant during the computation; it is at the beginning; and it is at the end.
These algorithms use exactly the same number of operations as the algorithm of the preceding section, but the multiplications are done in a different order.
Computational complexity
A brief analysis shows that such an algorithm uses squarings and at most multiplications, where denotes the floor function. More precisely, the number of multiplications is one less than the number of ones present in the binary expansion of n. For n greater than about 4 this is computationally more efficient than naively multiplying the base with itself repeatedly.Each squaring results in approximately double the number of digits of the previous, and so, if multiplication of two d-digit numbers is implemented in O operations for some fixed k, then the complexity of computing xn is given by
2''k''-ary method
This algorithm calculates the value of xn after expanding the exponent in base 2k. It was first proposed by Brauer in 1939. In the algorithm below we make use of the following function f = and f =, where m = u·2s with u odd.Algorithm:
;Input: An element x of G, a parameter k > 0, a non-negative integer and the precomputed values.
;Output: The element xn in G
y := 1; i := l - 1
while i ≥ 0 do
:= f
for j := 1 to k - s do
y := y2
y := y * xu
for j := 1 to s do
y := y2
i := i - 1
return y
For optimal efficiency, k should be the smallest integer satisfying
Sliding-window method
This method is an efficient variant of the 2k-ary method. For example, to calculate the exponent 398, which has binary expansion 2, we take a window of length 3 using the 2k-ary method algorithm and calculate 1, x3, x6, x12, x24, x48, x49, x98, x99, x198, x199, x398.But, we can also compute 1, x3, x6, x12, x24, x48, x96, x192, x199, x398, which saves one multiplication and amounts to evaluating 2
Here is the general algorithm:
Algorithm:
;Input: An element x of G, a non negative integer, a parameter k > 0 and the pre-computed values.
;Output: The element xn ∈ G.
Algorithm:
y := 1; i := l - 1
while i > -1 do
if ni = 0 then
y := y2
i := i - 1
else
s := max
while ns = 0 do
s := s + 1
for h := 1 to i - s + 1 do
y := y2
u := 2
y := y * xu
i := s - 1
return y
Montgomery's ladder technique
Many algorithms for exponentiation do not provide defence against side-channel attacks. Namely, an attacker observing the sequence of squarings and multiplications can recover the exponent involved in the computation. This is a problem if the exponent should remain secret, as with many public-key cryptosystems. A technique called "Montgomery's ladder" addresses this concern.Given the binary expansion of a positive, non-zero integer n = 2 with nk−1 = 1, we can compute xn as follows:
x1 = x; x2 = x2
for i = k - 2 to 0 do
if ni = 0 then
x2 = x1 * x2; x1 = x12
else
x1 = x1 * x2; x2 = x22
return x1
The algorithm performs a fixed sequence of operations : a multiplication and squaring takes place for each bit in the exponent, regardless of the bit's specific value. A similar algorithm for multiplication by doubling exists.
This specific implementation of Montgomery's ladder is not yet protected against cache timing attacks: memory access latencies might still be observable to an attacker, as different variables are accessed depending on the value of bits of the secret exponent. Modern cryptographic implementations use a "scatter" technique to make sure the processor always misses the faster cache.
Fixed-base exponent
There are several methods which can be employed to calculate xn when the base is fixed and the exponent varies. As one can see, precomputations play a key role in these algorithms.Yao's method
Yao's method is orthogonal to the -ary method where the exponent is expanded in radix and the computation is as performed in the algorithm above. Let,,, and be integers.Let the exponent be written as
where for all.
Let.
Then the algorithm uses the equality
Given the element of, and the exponent written in the above form, along with the precomputed values, the element is calculated using the algorithm below:
y = 1, u = 1, j = h - 1
while j > 0 do
for i = 0 to w - 1 do
if ni = j then
u = u × xbi
y = y × u
j = j - 1
return y
If we set and, then the values are simply the digits of in base. Yao's method collects in u first those that appear to the highest power ; in the next round those with power are collected in as well etc. The variable y is multiplied times with the initial, times with the next highest powers, and so on.
The algorithm uses multiplications, and elements must be stored to compute.
Euclidean method
The Euclidean method was first introduced in Efficient exponentiation using precomputation and vector addition chains by P.D Rooij.This method for computing in group, where is a natural integer, whose algorithm is given below, is using the following equality recursively:
where.
In other words, a Euclidean division of the exponent by is used to return a quotient and a rest.
Given the base element in group, and the exponent written as in Yao's method, the element is calculated using precomputed values and then the algorithm below.
Begin loop
Break loop
End loop;
The algorithm first finds the largest value among the and then the supremum within the set of.
Then it raises to the power, multiplies this value with, and then assigns the result of this computation and the value modulo.