SNAPKITTYWEST's picture
push from SNAPKITTYWEST/hyperkitty-constraint-dsl
224e773 verified
Raw History Blame Contribute Delete
9.31 kB
29
def GF256 : Type := Fin 256
31
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
}
60
def CyclicPolyRing : Type := Vector GF256 32
63
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)
    )
72
def K0 : CyclicPolyRing := Vector.replicate 32 0 |>.set 0 1
73
def K1 : CyclicPolyRing := Vector.replicate 32 0 |>.set 1 1
74
def K2 : CyclicPolyRing := Vector.replicate 32 0 |>.set 2 1
79
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)
89
structure ChainState where
body · 2 lines
  records : List (Vector GF256 64)
  tip : CyclicPolyRing
93
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
99
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)
112
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
128
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
143
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]
174
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
187
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
202
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
217
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
229
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