Elliptic Arithmetic 06: Scalar Multiplication
Elliptic arithmetic notes / 06
Scalar multiplication
Scalar multiplication is where algebra meets leakage: the loop invariant is mathematical, but the branch pattern is cryptographic.
Given a nonnegative integer \(k\) and \(P\in E(\mathbb F_p)\), compute \([k]P\). The bit-scanning routines require \(k<2^{\texttt{scalar\_bits}}\), with that public width no larger than the allocated scalar storage. The book treats this as the dominant operation in ECC schemes and presents binary, NAF, window, fixed-base, and multiple-point methods.
Binary double-and-add
Left-to-right binary multiplication maintains the invariant:
after processing a bit prefix \(u\) of \(k\), the accumulator is \(Q=[u]P\).
For the next bit \(b\),
\[Q\leftarrow [2u]P,\qquad Q\leftarrow [2u+b]P.\]The update preserves the prefix invariant. A branch on \(b\) makes the operation sequence depend on the scalar bit.
A public test routine may branch to select the doubled or added state. The secret-scalar API uses mask selection and point operations whose control flow and memory addresses are independent of the scalar.
Ladder-style constant-time shape
Initialize \(R_0=\mathcal O\) and \(R_1=P\). The ladder maintains
\[R_0=[u]P,\qquad R_1=[u+1]P.\]For the next bit \(b\), the new pair is \(([2u+b]P,[2u+b+1]P)\). The following schedule computes three candidates and selects that pair. Here p256_jac_select(dst, a, b, bit) selects a for bit 0 and b for bit 1:
for (uint32_t i = scalar_bits; i-- > 0;) {
p256_jac_t d0, d1, s;
uint32_t bit = p256_scalar_get_bit(k, i);
p256_jac_double(&d0, &r0); /* [2u]P */
p256_jac_add_complete(&s, &r0, &r1); /* [2u+1]P, complete or proved safe */
p256_jac_double(&d1, &r1); /* [2u+2]P */
p256_jac_select(&r0, &d0, &s, bit);
p256_jac_select(&r1, &s, &d1, bit);
}
Both doubling calls must handle infinity and order-2 points, and the addition primitive must handle every pair reached by the invariant. These requirements apply to all computed candidates, including the unselected ones. With constant-time field primitives and fixed public scalar_bits, the schedule has two doublings, one addition, and two selections per bit. A conditional-swap ladder can reduce this to one doubling and one addition. Montgomery-curve x-coordinate ladders use different point formulas.
Fixed-window multiplication
For an unsigned window of width \(w\) and fixed public base \(G\), precompute \([j]G\) for \(0\le j<2^w\). Initialize the accumulator to \(\mathcal O\) and pad the scalar with leading zero bits to a public width divisible by \(w\). Each window performs \(w\) doublings and adds the selected multiple, including \(\mathcal O\) for a zero window. Secret windows require a fixed table-access pattern:
static uint32_t ct_eq_u32(uint32_t x, uint32_t y) {
uint32_t z = x ^ y;
z |= 0u - z;
return (z >> 31) ^ 1u;
}
static void p256_select_table(p256_jac_t *r, const p256_jac_t table[], uint32_t len, uint32_t idx) {
p256_jac_set_infinity(r);
for (uint32_t i = 0; i < len; i++) {
uint32_t take = ct_eq_u32(i, idx);
p256_jac_cmov(r, &table[i], take);
}
}
Preconditions: idx < len, len is public, the table has len valid points, and the output does not overlap the table. Extracting exactly \(w\) bits for a table of \(2^w\) rows establishes the index bound without a secret-dependent range check. The output must be disjoint because the helper initializes it before reading the table entries.
Signed digits and NAF
A non-adjacent form writes a scalar as
\[k=\sum_i u_i2^i,\qquad u_i\in\{0,\pm 1\}\]with no two adjacent nonzero digits in the width-2 case. Wider signed-window variants use odd digits such as +/-1, +/-3, …, +/-(2^{w-1}-1). The nonzero-digit density decreases, so a variable-time implementation performs fewer additions. For secret scalars, skipping zero digits would reveal their positions; a regular signed-digit implementation must also fix the operation schedule. Selecting P versus -P cannot branch on a secret digit, and negation must be represented as a constant-time conditional replacement of Y by -Y.
For example, 13 has ordinary NAF digits 1,0,-1,0,1 from low to high, namely 13 = 1 - 4 + 16. For a width-4 signed window, a table of odd positive multiples can be reused for negative digits only if sign handling and table selection are constant-time.
Two examples
For \(k=13=(1101)_2\), binary left-to-right performs:
\[\mathcal O \to P \to 2P+P=3P \to 6P \to 12P+P=13P.\]For width-4 fixed-window multiplication, a 256-bit scalar uses 64 windows. The loop count is public. The selected table entry per window is secret, so the selection must be a full scan or use another constant-time mechanism.
SageMath scalar checks
p = 17
E = EllipticCurve(GF(p), [2, 2])
P = E(5, 1)
for k in [0, 1, 2, 13, 37]:
print(k, k*P)
print(13*P == sum([P for _ in range(13)], E(0)))
