Documentation

CslibFrontier.Computability.Circuit.Boolean.Karatsuba

Karatsuba multiplication circuits #

A Boolean circuit over the De Morgan basis computes the full 2w-bit product of two w-bit numbers with O(w ^ log₂ 3) gates. Every gate is charged, including constants and negations.

Let m = ⌈w/2⌉, and split a = a₀ + 2ᵐ a₁ and b = b₀ + 2ᵐ b₁ into m-bit halves. Karatsuba's method forms three half-width products, a₀ b₀, a₁ b₁ and (a₀ + a₁)(b₀ + b₁), and recovers the middle coefficient as (a₀ + a₁)(b₀ + b₁) - a₀ b₀ - a₁ b₁. The middle sums have m + 1 bits. Write a₀ + a₁ = r_a + 2ᵐ c_a with a carry bit c_a, and the same for b. Then

(a₀ + a₁)(b₀ + b₁) = r_a r_b + 2ᵐ (c_a r_b + c_b r_a) + 2²ᵐ c_a c_b.

The circuit multiplies r_a r_b recursively and computes the masked words c_a r_b and c_b r_a with one conjunction per bit, so every recursive product has width exactly m. Additions and subtractions are the ripple-carry Synthesis.word_add and Synthesis.word_sub, at width w + w where the result must not overflow. All shifts and truncations are free rewiring (CslibFrontier.Computability.Circuit.Boolean.Word.Rewire). The correctness of one step is ring arithmetic in BitVec (w + w) plus the absence of overflow.

The gate count is Karatsuba.gates:

gates w = 3 gates ⌈w/2⌉ + 26 ⌈w/2⌉ + 172 w + 34 for w ≥ 2.

The master theorem (Nat.DivideConquerBound, case 1) solves it to gates w ≤ 419 · 3 ^ ⌈log₂ w⌉ ≤ 1257 w ^ log₂ 3.

The existing schoolbook multiplier Cslib.Circuits.Boolean.Modular.multiply has at most 63 (w + 1)² gates (Modular.size_multiply_le). It computes only the product modulo 2 ^ w, that is the low w bits, while mulBits has all 2w bits. The constants here are larger: gates w + 1 < 63 (w + 1)² from w = 525 on (checked numerically, not formalized). The asymptotic saving is a factor w ^ (2 - log₂ 3), even against a circuit that returns half the bits.

A fairer baseline for the full product is Modular.multiply (2w) on zero-extended inputs (that wiring is not formalized here). Its bounds are 63 (2w + 1)² gates and depth 9 + 14 ⌈log₂ (2w + 1)⌉ (Modular.depth_multiply_le). Karatsuba.gates_add_one_lt shows gates w + 1 < 63 (2w + 1)² for every w, so Karatsuba is below that gate bound at all widths. It pays in depth: the ripple-carry additions make its depth linear in w, which is not formalized either. Both comparisons are against an upper bound on the schoolbook circuit, not its exact size.

Callers of Synthesis.word_mul_karatsuba must make the false constant available. The construction uses it for the high bit at width 1 and for the zero bits of shifted and zero-extended words. complexity_mulBits_le charges one gate for it.

Main definitions #

Main results #

References #

One step, as arithmetic in BitVec (w + w) #

The intermediate words of one step, in the order they are built.

The gate count #

@[irreducible]

Gates charged by Synthesis.word_mul_karatsuba on w-bit words. For w ≥ 2 and m = ⌈w/2⌉: three products of width m, two ripple-carry middle sums of width m + 1, two masks of width m and one conjunction of the carries, and five ripple-carry additions and two subtractions of width 2w.

theorem Cslib.Circuits.Boolean.Karatsuba.gates_of_two_le {w : ℕ} (hw : 2 ≤ w) :
gates w = 3 * gates ((w + 1) / 2) + 26 * ((w + 1) / 2) + 172 * w + 34

The recurrence for w ≥ 2.

The synthesis bound #

theorem Cslib.Circuits.Synthesis.word_mul_karatsuba {n w : ℕ} {s : Set (BooleanFunction n)} (a b : BitString n → BitVec w) (ha : ∀ (i : Fin w), (fun (x : BitString n) => (a x).getLsbD ↑i) ∈ s) (hb : ∀ (i : Fin w), (fun (x : BitString n) => (b x).getLsbD ↑i) ∈ s) (hz : (fun (x : BitString n) => false) ∈ s) :

Karatsuba multiplication. From available w-bit words a and b and the false constant, the 2w bits of a * b cost Karatsuba.gates w gates.

The false constant must be in s (hypothesis hz). It supplies the high bit at width 1 and the zero bits of shifted and zero-extended words, and it is not charged here. A client whose sources are only the inputs first adds it with Synthesis.const, one more gate, as complexity_mulBits_le does.

Solving the recurrence #

gates w ≤ 419 · 3 ^ ⌈log₂ w⌉.

gates w ≤ 1257 w ^ log₂ 3.

gates w + 1 < 63 (2w + 1)² for every w. The right side is the bound Modular.size_multiply_le for the schoolbook multiplier at width 2w, which on zero-extended inputs gives all 2w bits of the product. So Karatsuba's full product, including its false constant, is below that bound at every width.

The full product #

The 2w bits, least significant first, of the product of the two w-bit halves of the input, each read least significant bit first.

mulBits is the product of the two halves; it fits in 2w bits.

Karatsuba multiplication circuits. The 2w-bit product of two w-bit numbers has circuit complexity at most Karatsuba.gates w + 1 over the De Morgan basis; the extra gate is the false constant.

The 2w-bit product has circuit complexity at most 1257 w ^ log₂ 3 + 1.