Science  People  Locations  Timeline
Index: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Home > Exponentiation by squaring


 Contents
Exponentiating by squaring is an algorithm used for the fast computation of large powers of a number x. It is also known as the square-and-multiply algorithm or binary exponentation. It implicitly uses the binary expansion of the exponent. It is of quite general use, for example in modular arithmetic.

1 Squaring algorithm

The following recursive algorithm computes xn for a positive integer n:

Compared to the ordinary method of multiplying x with itself n − 1 times, this algorithm uses only O(log n) multiplications and therefore speeds up the computation of xn tremendously, in much the same way that the "long multiplication" algorithm speeds up multiplication over the slower method of repeated addition.

2 Further applications

The same idea allows fast computation of large exponents modulo a number. Especially in cryptography, it is useful to compute powers in a ring of integers modulo q. It can also be used to compute integer powers in a group, using the rule

Power(x, -n) = (Power(x, n))-1.

The method works in every semigroup and is often used to compute powers of matrices,

For example, the evaluation of

13789722341 (mod 2345)

would take a very long time and lots of storage space if the naļve method is used: compute 13789722341 then take the remainder when divided by 2345. 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. This will take 722340 modular multiplications. The square-and-multiply algorithm is based on the observation that 13789722341 = 13789(137892)361170. So, if we computed 137892, then the full computation would only take 361170 modular multiplications. This is a gain of a factor of two. But since the new problem is of the same type, we can apply the same observation again, once more approximately halving the size.

The repeated application of this algorithm is equivalent to decomposing the exponent (by a base conversion to binary) into a sequence of squares and products: for example

x7
= x4*x2*x1
= (x2)2*x2*x
= (x2*x)2*x       ---> algorithm needs only 4 multiplications instead of 7 - 1 = 6

where 7 = (111)2 = 22+21+20

Some more examples:



Read more »

Non User