ArticleslgStudy

computer science

Exponentiation by squaring

Exponentiation by squaring is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Exponentiation by squaring rather than just read about it. In short: 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.

Key takeaways

  • Exponentiation by squaring belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Exponentiation by squaring to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Exponentiation by squaring from memory before moving on to harder problems.

Reference excerpt

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 n>0, one has xn={x(x2)(n−1)/2if n is odd,(x2)n/2if n is even. If the exponent n 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, xn=(1x)−n. 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 n is removed. It follows that the number of recursive calls is ⌊log2n⌋+1, the number of bits of the binary representation of n. 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 n. This logarithmic number of operations is to be compared with the trivial algorithm which requires n − 1 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 formula yxn={yx(x2)(n−1)/2if n is odd,y(x2)n/2if n is even. If one applies recursively this formula, by starting with ⁠y=1⁠, one gets eventually an exponent equal to 0, 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

The correctness of the algorithm results from the fact that yxn is invariant during the computation; it is 1⋅xn=xn at the beginning; and it is yx1=xy 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 ⌊log2n⌋ squarings and at most ⌊log2n⌋ 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(dk) operations for some fixed k, then the complexity of computing xn is given by

∑i=0O(log⁡n)(2iO(log⁡x))k=O((nlog⁡x)k).

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:

Input An element x of G, a parameter k > 0, a non-negative integer n = (nl−1, nl−2, ..., n0)2k and the precomputed values x3,x5,...,x2k−1. 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

lg⁡n<k(k+1)⋅22k2k+1−k−2+1.

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:

Input An element x of G, a non negative integer n=(nl−1, nl−2, ..., n0)2, a parameter k > 0 and the pre-computed values x3,x5,...,x2k−1. 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 + 1 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

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Exponentiation by squaring

Start with the simplest possible case. Write down what Exponentiation by squaring claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Exponentiation by squaring before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Exponentiation by squaring ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Exponentiation by squaring

In research
Exponentiation by squaring appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Exponentiation by squaring in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Exponentiation by squaring is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Computer arithmetic algorithms, Exponentials, so understanding it makes those chapters shorter.
In everyday life
Look for Exponentiation by squaring outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Exponentiation by squaring” →

How to study Exponentiation by squaring in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Exponentiation by squaring means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Exponentiation by squaring out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Exponentiation by squaring in simple terms?

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-multi…

Why does Exponentiation by squaring matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Exponentiation by squaring?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Exponentiation by squaring.

Tags

  • Computer arithmetic
  • Computer arithmetic algorithms
  • Exponentials

Keep exploring