Binary Exponentiation by Factoring
Consider a problem of computing , given integers , , and , where is odd.
The algorithm below allows to solve this problem with additions and binary operations and a single multiplication by .
Due to the structure of the multiplicative group modulo , any number such that can be represented as
where . Without loss of generality we assume that , as we can reduce to by substituting and . In this notion, is represented as
The core idea of the algorithm is to simplify the computation of and using the fact that we're working modulo . For reasons that will be apparent later on, we'll be working with rather than , but taken modulo instead of .
In this article, we will cover the implementation for -bit integers. Let
mbin_log_32(r, x)be a function that computes ;mbin_exp_32(r, x)be a function that computes ;mbin_power_odd_32(a, x, y)be a function that computes .
Then mbin_power_odd_32 is implemented as follows:
uint32_t mbin_power_odd_32(uint32_t rem, uint32_t base, uint32_t exp) {
if (base & 2) {
/* divider is considered negative */
base = -base;
/* check if result should be negative */
if (exp & 1) {
rem = -rem;
}
}
return (mbin_exp_32(rem, mbin_log_32(0, base) * exp));
}Computing 4L(x) from x
Let be an odd number such that . It can be represented as
where . Here is well-defined for each multiplier, as they're equal to modulo . Hence,
So, if we precompute for all , we will be able to compute for any number .
For 32-bit integers, we can use the following table:
const uint32_t mbin_log_32_table[32] = {
0x00000000, 0x00000000, 0xd3cfd984, 0x9ee62e18,
0xe83d9070, 0xb59e81e0, 0xa17407c0, 0xce601f80,
0xf4807f00, 0xe701fe00, 0xbe07fc00, 0xfc1ff800,
0xf87ff000, 0xf1ffe000, 0xe7ffc000, 0xdfff8000,
0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
0xfff00000, 0xffe00000, 0xffc00000, 0xff800000,
0xff000000, 0xfe000000, 0xfc000000, 0xf8000000,
0xf0000000, 0xe0000000, 0xc0000000, 0x80000000,
};On practice, a slightly different approach is used than described above. Rather than finding the factorization for , we will consequently multiply with until we turn it into modulo . In this way, we will find the representation of , that is
To do this, we iterate over such that . If the current has -th bit set, we multiply with , which is conveniently done in C++ as x = x + (x << n). This won't change bits lower than , but will turn the -th bit to zero, because is odd.
With all this in mind, the function mbin_log_32(r, x) is implemented as follows:
uint32_t mbin_log_32(uint32_t r, uint32_t x) {
uint8_t n;
for (n = 2; n < 32; n++) {
if (x & (1 << n)) {
x = x + (x << n);
r -= mbin_log_32_table[n];
}
}
return r;
}Note that , so instead of adding , we subtract it from , which initially equates to .
Computing x from 4L(x)
Note that for it holds that
from which (by repeated squaring) we can deduce that
Applying this result to and we deduce that the multiplicative order of is a divisor of .
This, in turn, means that must be divisible by , as the order of is and the order of is , where is the highest power of that divides , so we need
thus must be greater or equal than . This is a bit ugly and to mitigate this we said in the beginning that we multiply by . Now if we know , we can uniquely decomposing it into a sum of by consequentially checking bits in . If the -th bit is set to , we will multiply the result with and reduce the current by .
Thus, mbin_exp_32 is implemented as follows:
uint32_t mbin_exp_32(uint32_t r, uint32_t x) {
uint8_t n;
for (n = 2; n < 32; n++) {
if (x & (1 << n)) {
r = r + (r << n);
x -= mbin_log_32_table[n];
}
}
return r;
}Further optimizations
It is possible to halve the number of iterations if you note that and that for it holds that
which allows to deduce that for . So, you could simplify the algorithm by only going up to and then use the fact above to compute the remaining part with bitwise operations:
uint32_t mbin_log_32(uint32_t r, uint32_t x) {
uint8_t n;
for (n = 2; n != 16; n++) {
if (x & (1 << n)) {
x = x + (x << n);
r -= mbin_log_32_table[n];
}
}
r -= (x & 0xFFFF0000);
return r;
}
uint32_t mbin_exp_32(uint32_t r, uint32_t x) {
uint8_t n;
for (n = 2; n != 16; n++) {
if (x & (1 << n)) {
r = r + (r << n);
x -= mbin_log_32_table[n];
}
}
r *= 1 - (x & 0xFFFF0000);
return r;
}Computing logarithm table
To compute log-table, one could modify the Pohlig–Hellman algorithm for the case when modulo is a power of .
Our main task here is to compute such that , where and is a number of kind .
Squaring both parts times we arrive to
Note that the order of is not greater than (in fact, than , but we will stick to for convenience), hence using we will have either or on the left hand side which allows us to determine the smallest bit of by comparing to . Now assume that , where is a known part and is not yet known. Then
Multiplying both parts with , we get
Now, squaring both sides times we can obtain the next bit of , eventually recovering all its bits.