↑ arvau

Wikipedia / 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

If the exponent

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(x, n) is
    if n < 0 then
        return exp_by_squaring(1 / x, −n)
    else if n = 0 then
        return 1
    else if n is even then
        return exp_by_squaring(x × x, n / 2)
    else if n is odd then
        return x × exp_by_squaring(x × x, (n − 1) / 2)
end function

In each recursive call, the least-significant digit of the binary representation of 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

If 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:

The iterative version of the algorithm also uses a bounded auxiliary space, and is given by

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

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(dk) operations for some fixed k, then the complexity of computing xn is given by

2k-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(0) = (k, 0) and f(m) = (s, u), where m = u·2s with u odd.

Algorithm:

  1. Input: An element x of G, a parameter k > 0, a non-negative integer .

    Output: The element xn in G

y := 1; i := l - 1
while i ≥ 0 do
    (s, u) := f(ni)
    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 (110 001 110)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 (110 001 110)2

Here is the general algorithm:

Algorithm:

  1. Input: An element x of G, a non negative integer , a parameter k.

    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{i - k + 1, 0}
        while ns = 0 do
            s := s + 1In this line, the loop finds the longest string of length less than or equal to kx^{2^k-1} need be computed, and only those specifically involved in the computation need be considered.
        for h := 1 to i - s + 1 do
            y := y2
        u := (ni, ni-1, ..., ns)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 (partially) 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 = (nk−1...n0)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 (up to log n): 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

.

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.

in group , where is a natural integer, whose algorithm is given below, is using the following equality recursively:

. 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 then the algorithm below.

Begin loop
    M \in [0, l - 1], \forall i \in [0, l - 1], n_M \ge n_i.
    N \in \big([0, l - 1] - M\big), \forall i \in \big([0, l - 1] - M\big), n_N \ge n_i.
    Break loop n_N = 0.
    Letq = \lfloor n_M / n_N \rfloor, and then letn_N = (n_M \bmod n_N).
    x_M^q, and then letx_N = x_N \cdot x_M^q.
End loop;
Returnx^n = x_M^{n_M}.

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 .

Further applications

The approach also works with semigroups that are not of characteristic zero, for example allowing fast computation of large exponents modulo a number. Especially in cryptography, it is useful to compute powers in a ring of integers modulo q. For example, the evaluation of

would take a very long time and much storage space if the naïve method of computing and then taking the remainder when divided by 2345 were used. Even using a more effective method will take a long time: square 13789, take the remainder when divided by 2345, multiply the result by 13789, and so on.

Applying above exp-by-squaring algorithm, with "*" interpreted as (that is, a multiplication followed by a division with remainder) leads to only 27 multiplications and divisions of integers, which may all be stored in a single machine word. Generally, any of these approaches will take fewer than modular multiplications.

The approach can also be used to compute integer powers in a group, using either of the rules

  1. ,

  2. .

The approach also works in non-commutative semigroups and is often used to compute powers of matrices.

More generally, the approach works with positive integer exponents in every magma for which the binary operation is power associative.

Signed-digit recoding

In certain computations it may be more efficient to allow negative coefficients and hence use the inverse of the base, provided inversion in is "fast" or has been precomputed. For example, when computing , the binary method requires multiplications and squarings. However, one could perform squarings to get and then multiply by to obtain .

To this end we define the signed-digit representation of an integer in radix as

Signed binary representation corresponds to the particular choice . There are several methods for computing this representation. The representation is not unique. For example, take is used to denote . Since the binary method computes a multiplication for every non-zero entry in the base-2 representation of , we are interested in finding the signed-binary representation with the smallest number of non-zero entries, that is, the one with minimal Hamming weight. One method of doing this is to compute the representation in non-adjacent form is the following:

c_0=0
for i = 0 to l − 1 do
  c_{i+1} = \left\lfloor\frac{1}{2}(c_i + n_i + n_{i+1})\right\rfloor
  n_i' = c_i + n_i - 2c_{i+1}
(n_{l-1}' \dots n_0')_\text{NAF}

; it still minimizes the Hamming weight.

Alternatives and generalizations

Exponentiation by squaring can be viewed as a suboptimal addition-chain exponentiation algorithm: it computes the exponent by an addition chain consisting of repeated exponent doublings (squarings) and/or incrementing exponents by one (multiplying by x) only. More generally, if one allows any previously computed exponents to be summed (by multiplying those powers of x), one can sometimes perform the exponentiation using fewer multiplications (but typically using more memory). The smallest power where this occurs is for n = 15:

  1. (squaring, 6 multiplies),

  2. (optimal addition chain, 5 multiplies if x3 is re-used).

In general, finding the optimal addition chain for a given exponent is a hard problem, for which no efficient algorithms are known, so optimal chains are typically used for small exponents only (e.g. in compilers where the chains for small powers have been pre-tabulated). However, there are a number of heuristic algorithms that, while not being optimal, have fewer multiplications than exponentiation by squaring at the cost of additional bookkeeping work and memory usage. Regardless, the number of multiplications never grows more slowly than Θ(log n), so these algorithms improve asymptotically upon exponentiation by squaring by only a constant factor at best.

See also

Notes

References