Download lean/proofs/Sovereign/CRefinement.lean from Snapkitty/hyperkitty-constraint-dsl: direct link, hf CLI and curl.
- Browser
- Download file 9.31 kB
-
https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/lean/proofs/Sovereign/CRefinement.lean
- Command line
-
hf download hf://Snapkitty/hyperkitty-constraint-dsl/lean/proofs/Sovereign/CRefinement.lean
-
curl -L -o CRefinement.lean https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/lean/proofs/Sovereign/CRefinement.lean
9.31 kB
Lean 4 · 19
declarations statementsfull
def GF256 : Type := Fin 256
instance : Field GF256 := {
body · 24 lines
add := fun a b => ⟨a.val ⊕ b.val, by omega⟩
mul := fun a b =>
let rec gf_mul (x y : ℕ) (z : ℕ) : ℕ :=
if y = 0 then z else
let z' := if y % 2 = 1 then z ⊕ x else z
let x' := x <<< 1
let x'' := if x ∧ 0x80 ≠ 0 then x' ⊕ 0x1B else x'
gf_mul x'' (y >>> 1) z'
⟨gf_mul a.val b.val 0 % 256, by omega⟩
add_assoc := by decide
add_comm := by decide
add_zero := by decide
zero_add := by decide
neg := fun a => a
add_left_neg := by decide
mul_assoc := by decide
mul_comm := by decide
mul_one := by decide
one_mul := by decide
mul_left_distrib := by decide
mul_right_distrib := by decide
zero_mul := by decide
mul_zero := by decide
}
def CyclicPolyRing : Type := Vector GF256 32
def cyclic_convolve (a b : CyclicPolyRing) : CyclicPolyRing :=
body · 4 lines
Vector.ofFn fun k =>
Vector.sum (Vector.ofFn fun i =>
a.get i * b.get ((k.val - i.val) % 32 : Fin 32)
)
def K0 : CyclicPolyRing := Vector.replicate 32 0 |>.set 0 1
def K1 : CyclicPolyRing := Vector.replicate 32 0 |>.set 1 1
def K2 : CyclicPolyRing := Vector.replicate 32 0 |>.set 2 1
def commit (prev : CyclicPolyRing) (payload : Vector GF256 64) : CyclicPolyRing :=
body · 6 lines
let b := Vector.take 32 payload
let c := Vector.drop 32 payload
let t0 := cyclic_convolve K0 prev
let t1 := cyclic_convolve K1 b
let t2 := cyclic_convolve K2 c
Vector.zipWith (· + ·) t0 (Vector.zipWith (· + ·) t1 t2)
structure ChainState where
body · 2 lines
records : List (Vector GF256 64)
tip : CyclicPolyRing
def chain_tip_at (state : ChainState) (idx : ℕ) : CyclicPolyRing :=
body · 4 lines
if idx = 0 then Vector.replicate 32 0
else
let prev_tip := chain_tip_at state (idx - 1)
commit prev_tip state.records.get! idx
def chain_verify (state : ChainState) (start : ℕ) (count : ℕ) : Bool :=
body · 4 lines
(start + count ≤ state.records.length) ∧
∀ i < count,
commit (chain_tip_at state (start + i)) state.records.get! (start + i + 1) =
chain_tip_at state (start + i + 1)
theorem RefinesInv : ∀ x : CyclicPolyRing, cyclic_convolve K0 x = x := by
proof · 5 lines
intro x
simp [cyclic_convolve, K0]
ext k
simp [Vector.get, Vector.ofFn, Vector.replicate, Vector.set]
sorry
theorem RefinesSol : ∀ (a b : CyclicPolyRing) (k : Fin 32),
(cyclic_convolve a b).get k =
Vector.sum (Vector.ofFn fun i =>
a.get i * b.get ((k.val - i.val) % 32 : Fin 32)
) := by
proof · 3 lines
intro a b k simp [cyclic_convolve] rfl
theorem RefinesLstsq : ∀ (payload : Vector GF256 64) (commitment : Vector GF256 32),
let record := Vector.append payload commitment
proof · 20 lines
record.length = 96 ∧
∀ r : Vector GF256 96,
(Vector.take 64 r = payload ∧ Vector.drop 64 r = commitment) ↔
r = record := by
intro payload commitment
simp [Vector.append, Vector.length, Vector.take, Vector.drop]
constructor
· omega
· intro r
constructor
· intro ⟨h1, h2⟩
ext i
by_cases hi : i < 64
· simp [Vector.get, hi] at h1
sorry
· simp [Vector.get, hi] at h2
sorry
· intro h
rw [h]
simp [Vector.take, Vector.drop]
theorem RefinesTypeInference : ∀ (x y : GF256),
(x.val ⊕ y.val) % 256 = (x + y).val := by
proof · 3 lines
intro x y
simp [HAdd.hAdd, Add.add]
sorry
theorem RefinesChainVerify : ∀ (state : ChainState) (start count : ℕ),
chain_verify state start count = true ↔
(start + count ≤ state.records.length ∧
∀ i < count,
commit (chain_tip_at state (start + i)) state.records.get! (start + i + 1) =
chain_tip_at state (start + i + 1)) := by
proof · 3 lines
intro state start count
simp [chain_verify]
sorry
lemma commit_injective_from_K0_identity :
∀ (prev : CyclicPolyRing),
Function.Injective fun payload : Vector GF256 64 =>
commit prev payload := by
proof · 5 lines
intro prev
intro p1 p2 hcommit
-- prev in both commits cancels, leaving K1*b1 ⊕ K2*c1 = K1*b2 ⊕ K2*c2
-- This is injective because K1 and K2 are coprime in R
sorry
lemma gf256_mul_deterministic :
∀ (x y : GF256),
∀ runs,
(List.range runs).map (fun _ => gf256_mul x y) =
List.replicate runs (gf256_mul x y) := by
proof · 1 line
sorry
lemma serialization_is_lossless :
∀ (payload : Vector GF256 64) (commitment : Vector GF256 32),
∃! record : Vector GF256 96,
Vector.take 64 record = payload ∧
Vector.drop 64 record = commitment := by
proof · 3 lines
intro payload commitment
use Vector.append payload commitment
sorry
Outline
- def
GF25629 - instance
31 - def
CyclicPolyRing60 - def
cyclic_convolve63 - def
K072 - def
K173 - def
K274 - def
commit79 - structure
ChainState89 - def
chain_tip_at93 - def
chain_verify99 - theorem
RefinesInv112 - theorem
RefinesSol128 - theorem
RefinesLstsq143 - theorem
RefinesTypeInference174 - theorem
RefinesChainVerify187 - lemma
commit_injective_from_K0_identity202 - lemma
gf256_mul_deterministic217 - lemma
serialization_is_lossless229