[This page is auto-generated; please prefer to read the article’s PDF Form.]
In the above algorithm, we iteratively read the bits of b from MSB to LSB, thus building-up x from 1 to include successively more prefix bits of b. Alternatively, we can start with x as the single-bit suffix of b (the LSB), and iteratively build-up x to include successively more suffix bits of b. Thus, bits of b will be read from LSB to MSB (right to left). For this reason, this algorithm is often called Right-to-Left Binary Exponentiation.
In the common-structure shown earlier, every iteration increases the number of suffix bits in x by 1. So, if the next bit of b to prepend to x is bp (from position p), the iteration will do it by updating x to x + bp(2p). To maintain invariant P, r will be updated to (modulo n):
|
A separate variable y will be maintaining the value of a2p in it as each iteration increments p by 1, starting from p = 0. The following method implements this algorithm:
This algorithm performs m − 1 squarings of y, and a multiplication r ⋅ y for every 1 bit seen during the loop, which are maximum m − 1. Thus, it performs total up to 2(m − 1) multiplications, where m = ⌊log 2b⌋ + 1.
Copyright © 2021 Nitin Verma. All rights reserved.