goldilocks-controlled-reduce / lean /GoldilocksStep.lean
SNAPKITTYWEST's picture
October 2026 main drop: mirror from GitHub
4f2752b verified
Raw History Blame Contribute Delete
4.6 kB
-- Lean 4 Proof Sketch: Goldilocks Reduction Step Correctness
-- File: GoldilocksStep.lean
namespace Goldilocks
-- 1. State Definition
def Bit := Fin 2
def State := Bit × Bit × Bit -- (x, c_in, c_out)
-- 2. Gate Semantics (Classical Action on Basis States)
def X (s : State) : State :=
(s.1 + 1, s.2.1, s.2.2)
def CNOT (control_target : Bit × Bit) (s : State) : State :=
-- This is a helper; actual gates act on specific wires.
-- We define specific wire actions below.
s
-- Wire-specific Gates
-- Wire indices: 0 = x, 1 = c_in, 2 = c_out
def X_x (s : State) : State := ⟨(s.1 + 1 : Bit), s.2.1, s.2.2⟩
def CNOT_cin_x (s : State) : State := ⟨s.1 + s.2.1, s.2.1, s.2.2⟩
def CNOT_cin_cout (s : State) : State := ⟨s.1, s.2.1, s.2.2 + s.2.1⟩
def CCX_x_cin_cout (s : State) : State :=
⟨s.1, s.2.1, s.2.2 + (s.1 * s.2.1 : Bit)⟩
-- 3. Circuit Definitions
def lower_step (s : State) : State :=
(X_x ∘ CNOT_cin_x ∘ X_x ∘ CCX_x_cin_cout ∘ X_x) s
def upper_step (s : State) : State :=
(CNOT_cin_x ∘ CCX_x_cin_cout) s
-- 4. Specifications
def spec_lower (s : State) : State :=
⟨s.1 + 1 + s.2.1, s.2.1, s.1 * s.2.1⟩
def spec_upper (s : State) : State :=
⟨s.1 + s.2.1, s.2.1, s.1 * s.2.1⟩
-- 5. Correctness Theorems
theorem lower_step_correct : ∀ (s : State), s.2.2 = 0 → lower_step s = spec_lower s := by
intro s h_zero
-- Proof by exhaustive case analysis on 4 relevant states (c_out=0 fixed)
-- x ∈ {0,1}, c_in ∈ {0,1}
fin_cases s <;> simp_all [lower_step, spec_lower, X_x, CNOT_cin_x, CCX_x_cin_cout, Bit, Fin.ext_iff, Fin.val_zero, Fin.val_add, Fin.val_one]
<;> norm_num at * <;> rfl
theorem upper_step_correct : ∀ (s : State), s.2.2 = 0 → upper_step s = spec_upper s := by
intro s h_zero
fin_cases s <;> simp_all [upper_step, spec_upper, CNOT_cin_x, CCX_x_cin_cout, Bit, Fin.ext_iff, Fin.val_zero, Fin.val_add, Fin.val_one]
<;> norm_num at * <;> rfl
-- 6. Inverse Correctness (Uncomputation)
def lower_step_dag (s : State) : State :=
(X_x ∘ CNOT_cin_x ∘ X_x ∘ CCX_x_cin_cout ∘ X_x) s -- Self-inverse check? No, reverse order.
-- Actual Dagger:
-- (X_x)⁻¹ ∘ (CCX)⁻¹ ∘ (X_x)⁻¹ ∘ (CNOT)⁻¹ ∘ (X_x)⁻¹
-- X⁻¹ = X, CNOT⁻¹ = CNOT, CCX⁻¹ = CCX
-- So: X_x ∘ CCX_x_cin_cout ∘ X_x ∘ CNOT_cin_x ∘ X_x
def lower_step_dag_correct (s : State) : State :=
(X_x ∘ CCX_x_cin_cout ∘ X_x ∘ CNOT_cin_x ∘ X_x) s
theorem lower_step_dag_is_inverse : ∀ (s : State), lower_step_dag_correct (lower_step s) = s := by
intro s
fin_cases s <;> simp [lower_step, lower_step_dag_correct, X_x, CNOT_cin_x, CCX_x_cin_cout, Bit, Fin.ext_iff, Fin.val_zero, Fin.val_add, Fin.val_one]
<;> norm_num <;> rfl
-- 7. Full 64-bit Composition Correctness (Sketch)
-- We model the Ping-Pong carry chain as a state machine.
-- State: (x_vec : Vector Bit 64, carry_in : Bit, carry_out : Bit, phase : Phase)
-- Phase: Lower (0..31), Upper (32..63), ReverseUpper, ReverseLower
-- Invariant: carry_out = 0 at step start.
-- Transition: Apply lower_step/upper_step, swap carry_in/carry_out.
-- Theorem: Final x_vec = x_init + (C * carry_init) mod 2^64, carry_in = carry_init, carry_out = 0.
def CarryState := Bit × Bit -- (active_carry, passive_carry)
-- Ping-Pong Step for Lower Bits
def pp_lower_step (x_bit : Bit) (carries : CarryState) : (Bit × CarryState) :=
let s : State := ⟨x_bit, carries.1, carries.2⟩
let s' := lower_step s
⟨s'.1, ⟨s'.2.2, s'.2.1⟩⟩ -- Output x', Swap carries (new_active = old_c_out, new_passive = old_c_in)
-- Ping-Pong Step for Upper Bits
def pp_upper_step (x_bit : Bit) (carries : CarryState) : (Bit × CarryState) :=
let s : State := ⟨x_bit, carries.1, carries.2⟩
let s' := upper_step s
⟨s'.1, ⟨s'.2.2, s'.2.1⟩⟩
-- Reverse Steps
def pp_lower_step_dag (x_bit : Bit) (carries : CarryState) : (Bit × CarryState) :=
let s : State := ⟨x_bit, carries.1, carries.2⟩
let s' := lower_step_dag_correct s
⟨s'.1, ⟨s'.2.2, s'.2.1⟩⟩
def pp_upper_step_dag (x_bit : Bit) (carries : CarryState) : (Bit × CarryState) :=
let s : State := ⟨x_bit, carries.1, carries.2⟩
let s' := (CNOT_cin_x ∘ CCX_x_cin_cout)⁻¹ s -- Defined explicitly as upper_step_dg
⟨s'.1, ⟨s'.2.2, s'.2.1⟩⟩
-- Main Theorem Statement (Informal)
-- theorem goldilocks_reduce_correct (x_init : Vector Bit 64) (ctrl : Bit) :
-- let final_state := run_full_circuit x_init ctrl
-- final_state.x = (x_init + ctrl * (2^32 - 1)) % 2^64 ∧
-- final_state.carry_in = ctrl ∧
-- final_state.carry_out = 0 := by sorry
end Goldilocks