Several powers with the same nonnegative integer exponent can share one exponentiation by squaring loop. Initialize , and . The loop invariants are .
At each step, multiply every by if is an odd integer, then replace every by and by . Indeed,so all invariants are preserved. The loop variant strictly decreases whenever it is positive. At , the accumulators are exactly the requested powers. For , no iteration is needed and every result is one.
For bases, the method uses multiplications and stored arithmetic values. These are arithmetic-operation counts; exact integer products can grow in bit length. For modular exponentiation, reducing each update modulo its intended integer modulus keeps the stored residues bounded. Sharing the exponent loop does not require mixing the bases' arithmetic types.
Articles by others on the same topic
There are currently no matching articles.