Post · 6 min read· pp. 169–173

Binary Exponentiation by Factoring

Consider a problem of computing axy(mod2d)ax^y \pmod{2^d}, given integers aa, xx, yy and d≥3d \geq 3, where xx is odd.

The algorithm below allows to solve this problem with O(d)O(d) additions and binary operations and a single multiplication by yy.

Due to the structure of the multiplicative group modulo 2d2^d, any number xx such that x≡1(mod4)x \equiv 1 \pmod 4 can be represented as

x≡bL(x)(mod2d),x \equiv b^{L(x)} \pmod{2^d},

where b≡5(mod8)b \equiv 5 \pmod 8. Without loss of generality we assume that x≡1(mod4)x \equiv 1 \pmod 4, as we can reduce x≡3(mod4)x \equiv 3 \pmod 4 to x≡1(mod4)x \equiv 1 \pmod 4 by substituting x↦−xx \mapsto -x and a↦(−1)yaa \mapsto (-1)^{y} a. In this notion, axyax^y is represented as

axy≡abyL(x)(mod2d).a x^y \equiv a b^{yL(x)} \pmod{2^d}.

The core idea of the algorithm is to simplify the computation of L(x)L(x) and byL(x)b^{y L(x)} using the fact that we're working modulo 2d2^d. For reasons that will be apparent later on, we'll be working with 4L(x)4L(x) rather than L(x)L(x), but taken modulo 2d2^d instead of 2d−22^{d-2}.

In this article, we will cover the implementation for 3232-bit integers. Let

  • mbin_log_32(r, x) be a function that computes r+4L(x)(mod2d)r+4L(x) \pmod{2^d};
  • mbin_exp_32(r, x) be a function that computes rbx4(mod2d)r b^{\frac{x}{4}} \pmod{2^d};
  • mbin_power_odd_32(a, x, y) be a function that computes axy(mod2d)ax^y \pmod{2^d}.

Then mbin_power_odd_32 is implemented as follows:

text
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 xx be an odd number such that x≡1(mod4)x \equiv 1 \pmod 4. It can be represented as

x≡(2a1+1)…(2ak+1)(mod2d),x \equiv (2^{a_1}+1)\dots(2^{a_k}+1) \pmod{2^d},

where 1<a1<⋯<ak<d1 < a_1 < \dots < a_k < d. Here L(⋅)L(\cdot) is well-defined for each multiplier, as they're equal to 11 modulo 44. Hence,

4L(x)≡4L(2a1+1)+⋯+4L(2ak+1)(mod2d).4L(x) \equiv 4L(2^{a_1}+1)+\dots+4L(2^{a_k}+1) \pmod{2^{d}}.

So, if we precompute tk=4L(2n+1)t_k = 4L(2^n+1) for all 1<k<d1 < k < d, we will be able to compute 4L(x)4L(x) for any number xx.

For 32-bit integers, we can use the following table:

text
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 xx, we will consequently multiply xx with 2n+12^n+1 until we turn it into 11 modulo 2d2^d. In this way, we will find the representation of x−1x^{-1}, that is

x(2a1+1)…(2ak+1)≡1(mod2d).x (2^{a_1}+1)\dots(2^{a_k}+1) \equiv 1 \pmod {2^d}.

To do this, we iterate over nn such that 1<n<d1 < n < d. If the current xx has nn-th bit set, we multiply xx with 2n+12^n+1, which is conveniently done in C++ as x = x + (x << n). This won't change bits lower than nn, but will turn the nn-th bit to zero, because xx is odd.

With all this in mind, the function mbin_log_32(r, x) is implemented as follows:

text
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 4L(x)=−4L(x−1)4L(x) = -4L(x^{-1}), so instead of adding 4L(2n+1)4L(2^n+1), we subtract it from rr, which initially equates to 00.

Computing x from 4L(x)

Note that for k≥1k \geq 1 it holds that

(a2k+1)2=a222k+a2k+1+1=b2k+1+1,(a 2^{k}+1)^2 = a^2 2^{2k} +a 2^{k+1}+1 = b2^{k+1}+1,

from which (by repeated squaring) we can deduce that

(2a+1)2b≡1(mod2a+b).(2^a+1)^{2^b} \equiv 1 \pmod{2^{a+b}}.

Applying this result to a=2n+1a=2^n+1 and b=d−kb=d-k we deduce that the multiplicative order of 2n+12^n+1 is a divisor of 2d−n2^{d-n}.

This, in turn, means that L(2n+1)L(2^n+1) must be divisible by 2n2^{n}, as the order of bb is 2d−22^{d-2} and the order of byb^y is 2d−2−v2^{d-2-v}, where 2v2^v is the highest power of 22 that divides yy, so we need

2d−k≡0(mod2d−2−v),2^{d-k} \equiv 0 \pmod{2^{d-2-v}},

thus vv must be greater or equal than k−2k-2. This is a bit ugly and to mitigate this we said in the beginning that we multiply L(x)L(x) by 44. Now if we know 4L(x)4L(x), we can uniquely decomposing it into a sum of 4L(2n+1)4L(2^n+1) by consequentially checking bits in 4L(x)4L(x). If the nn-th bit is set to 11, we will multiply the result with 2n+12^n+1 and reduce the current 4L(x)4L(x) by 4L(2n+1)4L(2^n+1).

Thus, mbin_exp_32 is implemented as follows:

text
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 4L(2d−1+1)=2d−14L(2^{d-1}+1)=2^{d-1} and that for 2k≥d2k \geq d it holds that

(2n+1)2≡22n+2n+1+1≡2n+1+1(mod2d),(2^n+1)^2 \equiv 2^{2n} + 2^{n+1}+1 \equiv 2^{n+1}+1 \pmod{2^d},

which allows to deduce that 4L(2n+1)=2n4L(2^n+1)=2^n for 2n≥d2n \geq d. So, you could simplify the algorithm by only going up to d2\frac{d}{2} and then use the fact above to compute the remaining part with bitwise operations:

text
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 22.

Our main task here is to compute xx such that gx≡y(mod2d)g^x \equiv y \pmod{2^d}, where g=5g=5 and yy is a number of kind 2n+12^n+1.

Squaring both parts kk times we arrive to

g2kx≡y2k(mod2d).g^{2^k x} \equiv y^{2^k} \pmod{2^d}.

Note that the order of gg is not greater than 2d2^{d} (in fact, than 2d−22^{d-2}, but we will stick to 2d2^d for convenience), hence using k=d−1k=d-1 we will have either g1g^1 or g0g^0 on the left hand side which allows us to determine the smallest bit of xx by comparing y2ky^{2^k} to gg. Now assume that x=x0+2kx1x=x_0 + 2^k x_1, where x0x_0 is a known part and x1x_1 is not yet known. Then

gx0+2kx1≡y(mod2d).g^{x_0+2^k x_1} \equiv y \pmod{2^d}.

Multiplying both parts with g−x0g^{-x_0}, we get

g2kx1≡(g−x0y)(mod2d).g^{2^k x_1} \equiv (g^{-x_0} y) \pmod{2^d}.

Now, squaring both sides d−k−1d-k-1 times we can obtain the next bit of xx, eventually recovering all its bits.

References