Bignum arithmetic notes / 09

Modular exponentiation

Exponentiation is where arithmetic meets side channels: the multiplication routine may be constant-time while the exponent loop leaks the key.

For integers \(m>1\) and \(e\ge0\) and a residue \(0\le x<m\), the mathematical task is

\[y=x^e\bmod m.\]

The Montgomery implementations below assume that \(m\) is odd. When the exponent is public, the exponentiation schedule may depend on its bits. The modular arithmetic routines still need to protect any secret base or intermediate values. When the exponent is secret, the operation sequence and memory addresses must also be independent of its bits.

Square-and-multiply

Initialize acc to \(R\bmod m\), the Montgomery representation of 1, and base to \(xR\bmod m\). The left-to-right algorithm scans exponent bits and multiplies only when a bit is 1. Its operation sequence therefore reveals secret exponent bits.

/* Variable-time: only for public e. */
for (uint32_t i = bits; i-- > 0;) {
    mont_sqr(acc, acc, m);
    if (bit(e, i)) mont_mul(acc, acc, base, m);
}

The mont_mul and mont_sqr interfaces used here permit exact output/input aliasing. An implementation meets this aliasing contract by forming the product and performing REDC in separate scratch buffers before writing the result; the lower-level mont_redc retains its disjoint-buffer contract.

Always multiply

A constant-operation-count variant computes both candidates and selects by mask.

for (uint32_t i = bits; i-- > 0;) {
    mont_sqr(sq, acc, m);
    mont_mul(prod, sq, base, m);
    bn_cmov(acc, prod, sq, n, bit(e, i));
}

Here bits is a public fixed width satisfying \(0\le e<2^{\texttt{bits}}\) and fitting the allocated exponent array. The loop includes leading zero bits, and every bit call reads a position determined by the loop index. The multiplication, squaring, and selection routines must meet the target’s leakage contract for all intermediate values.

Fixed-window exponentiation

An unsigned fixed window of width \(w_0\) uses a table of \(x^j\) for \(0\le j<2^{w_0}\), all in the chosen residue representation. Starting from 1, scan a public fixed number of windows, padding the exponent with leading zero bits to a multiple of \(w_0\). Each window performs \(w_0\) squarings and one multiplication, including multiplication by 1 for a zero window. An odd-power table belongs to a recoded or sliding-window variant and needs the corresponding digit algorithm. Constant-time lookup scans the whole table and mask-selects one row.

Table selection contract

For secret index \(j\), do not read table[j] directly. Read every table entry and mask-select the desired one. The memory trace must be independent of \(j\).

Montgomery ladder

The ladder starts from \(R_0=1\) and \(R_1=x\) and maintains \(R_0=x^u\), \(R_1=x^{u+1}\) for the processed prefix \(u\), modulo \(m\). For bit 0, replace the pair by \((R_0^2,R_0R_1)\); for bit 1, use \((R_0R_1,R_1^2)\). Conditional swaps arrange these updates using one multiplication and one squaring per bit. In Montgomery form, both initial values and every update use the encoded residues.

Example: RSA public exponent 65537

For encryption or signature verification with \(e=65537=2^{16}+1\), initialize the accumulator to the base and perform 16 squarings followed by one multiplication by that base. The schedule is fixed by the public exponent. During encryption the message representative can be secret, so multiplication and reduction must still protect their operands and intermediates.

Example: Diffie-Hellman secret exponent

For finite-field Diffie-Hellman, the exponent is secret. A sliding-window method with direct table indexing leaks. Use a ladder or fixed-window with constant-time table selection.

Proof sketch

For left-to-right binary exponentiation, let \(u\) be the integer represented by the processed bit prefix. The decoded accumulator is \(x^u\bmod m\); in Montgomery form its stored integer is \(x^uR\bmod m\). Montgomery squaring produces the encoding of \(x^{2u}\), and multiplication by the encoded base produces the encoding of \(x^{2u+1}\). Selecting according to the next bit \(b\) therefore preserves the invariant for the new prefix \(2u+b\).

SageMath vectors

m = 2^127 - 1
for x, e in [(5, 65537), (1234567, 0xdeadbeef), (m-2, 12345)]:
    print(power_mod(x, e, m) == pow(x, e, m))