Reversible controlled addition — the quantum×quantum primitive (ECDLP Phase 2, Stage S2) #
Category: 1-Mathlib (CSD-free; staged as a Mathlib-upstream candidate).
The Tranche-3 multiplier (ModMul.lean) is quantum×classical: it multiplies a classical constant
a = ∑ 2^sh (fixed shifts) by the quantum register Y. Squaring Y² and, more importantly, the field
multiplications inside an elliptic-curve point operation are quantum×quantum (both factors are
registers): bit i of one factor must control whether the other, shifted, is added. That needs a
controlled adder, the primitive built here.
The obstacle: a control-wire-ctrled full adder needs ctrl ANDed with the adder's two inputs — i.e.
3-control gates — while the DSL tops out at CCX (2 controls). We use the standard clean-ancilla
decomposition CCCX ctrl x y z = [CCX ctrl x anc, CCX anc y z, CCX ctrl x anc] (anc init false,
restored to false), so one shared ancilla suffices and the controlled full adder stays inside the
2-control DSL.
What is proved here (Stage S2.1 — the gadget) #
cfullAdder— the controlled full adder on(ctrl, a, b, cin, cout, anc)(withcout,ancinitialisedfalse), eightCCXgates: each gate offullAddercontrolled onctrlvia the ancilla decomposition.cfullAdder_correct— full all-inputs correctness (decideoverState 6): whenctrlis set it computes the full-adder sum/carry (b ← a⊕b⊕cin,cout ← majority,a/cinpreserved); whenctrlis clear it is the identity; either wayancis restored tofalse.cfullAdder_cost— derivedtoffoli = 8, everything else0: a controlled add is4×the uncontrolled full adder's two Toffolis, plus one ancilla. The honest quantum×quantum overhead.
The controlled full adder on wires ctrl a b cin cout anc (with cout, anc initialised false):
when ctrl is set it acts as fullAdder a b cin cout; when ctrl is clear it is the identity. Each
of fullAdder's four gates is controlled on ctrl: the two CCXs become 3-control gates realised by
the clean-ancilla decomposition CCX ctrl · anc ; CCX anc · cout ; CCX ctrl · anc, and the two CXs
become CCX ctrl · ·. Eight CCXs; anc is borrowed clean and returned clean.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Controlled-full-adder correctness — genuine all-inputs coverage. On the concrete State 6
layout (wires 0..5 = ctrl, a, b, cin, cout, anc), with cout and anc initialised false: if
ctrl (wire 0) is set the gadget computes the sum bit on b (wire 2), the carry-out on cout
(wire 4), and preserves a, cin; if ctrl is clear it preserves b and leaves cout false;
the ancilla (wire 5) is restored to false in both cases. Proved by decide over State 6 (the
2^6 inputs with cout, anc fixed false).
Derived Toffoli cost of the controlled full adder (from the gate list, via circuitCost):
eight Toffolis. Read off the eight CCXs. The uncontrolled fullAdder is 2 Toffolis + 2 CNOTs; the
controlled gadget is 8 Toffolis + 0 CNOTs (cfullAdder_cnot) — the two CXs are promoted to
CCX ctrl · · (absorbing the CNOTs into Toffolis) and the two CCXs expand to ancilla-mediated CCCX.
Plus one designated clean ancilla wire (not billed by gateCost). The honest quantum×quantum overhead.
The controlled full adder uses no CNOTs (cnot = 0): every gate is a CCX, the two CXs of
the uncontrolled fullAdder having been promoted to controlled CCXs. Derived from the gate list.
Frame lemma for the controlled gadget. A wire distinct from all six of ctrl, a, b, cin, cout, anc is untouched by cfullAdder (every gate's wires lie in that set). Lets the controlled carry-chain
lift the gadget over a register.
Controlled-full-adder correctness, general Fin n wires. For pairwise-distinct wires
ctrl, a, b, cin, cout, anc with cout and anc initialised false: when ctrl is set the gadget
writes the sum bit to b, the carry-out to cout, and preserves a, cin; when ctrl is clear it
preserves b and leaves cout false; the ancilla anc is restored to false in both cases. This
is the slice the controlled ripple carry-chain iterates.
The controlled ripple adder (general n): correctness #
A CRippleLayout is a RippleLayout (registers A, B, carry chain C) plus a control wire ctrl
and a shared clean ancilla anc, both disjoint from the registers. The controlled ripple adder is one
cfullAdder per slice, all sharing ctrl and anc. The headline: it leaves register B holding
(A + B) mod 2^n when ctrl is set, and B unchanged when ctrl is clear.
A controlled ripple-adder layout: a RippleLayout plus a control wire and a shared ancilla,
both disjoint from registers A, B and the carry chain C.
- ctrl : Fin m
The control wire (set ⇒ add, clear ⇒ identity).
- anc : Fin m
The shared clean ancilla for the
CCCXdecomposition (borrowed and returnedfalse).
Instances For
One controlled ripple slice: a controlled full adder on (A i, B i, C i, C (i+1)) with the shared
control and ancilla.
Equations
- Reversible.cRippleSlice L i = Reversible.cfullAdder L.ctrl (L.A i) (L.B i) (L.C i) (L.C (i + 1)) L.anc
Instances For
The circuit of the first k controlled slices.
Equations
Instances For
The full controlled ripple adder: all n slices.
Equations
Instances For
The controlled carry-chain invariant. After the first k slices: B's low k bits plus the
carry into bit k equal (if ctrl then A else 0) + B over the low k bits (P1 — unified: ctrl clear
⇒ added value 0, carry stays false, B unchanged; ctrl set ⇒ the ripple sum); A untouched (P2);
high B (P4) and high carries (P5) preserved; ancilla restored to false (P6); the control bit
preserved (P0c); and — the clause that closes the ctrl-clear case — the working carry stays false
when ctrl is clear (P7). By induction on k, lifting cfullAdder_correct_general through
cfullAdder_apply_of_ne.
Controlled ripple-adder correctness (the S2 headline). For a disjoint-wire layout with all
carries and the ancilla initialised false, the controlled ripple adder leaves register B holding
(A + B) mod 2^n when the control wire ctrl is set, and B unchanged when ctrl is clear. The
quantum×quantum-ready conditional add, derived from the exhibited circuit cRippleCirc.
Derived cost of the controlled ripple adder: 8n Toffolis (eight per slice, cfullAdder_cost),
composed through the Tranche-1 cost_comp_toffoli_count — 4× the uncontrolled ripple adder's 2n,
the honest quantum×quantum overhead.
The shared ancilla is returned clean. After the whole controlled ripple adder the ancilla is
false again (it is borrowed and restored within each slice). The hygiene fact a multi-step consumer
needs to reuse the ancilla between successive controlled adds.
The control wire is preserved by the controlled ripple adder (it is read, never written). The consumer needs this to keep each partial-product's control bit equal to the original register bit across the accumulation loop.
The controlled ripple adder preserves any external wire (distinct from ctrl, anc, and every
register/carry wire A k, B k, C k). The frame lemma at circuit granularity: every gate of
cRippleCirc L has wires among {ctrl, anc} ∪ {A k, B k, C k}.
Non-vacuity witness #
A concrete 2-bit controlled ripple layout on Fin 9 (registers A → {0,1}, B → {2,3}, carry chain
C → {4,5,6}, control 7, ancilla 8), exhibiting that CRippleLayout is inhabited and the headline
applies.
A concrete 2-bit controlled adder layout on Fin 9.
Equations
- One or more equations did not get rendered due to their size.