import RumaLean.StateRes
import RumaLean.DirectedAcyclicGraph
import RumaLean.Bitwise
set_option linter.style.longLine false
namespace RumaLean
def hammingDistance (a b : ℕ) : ℕ := popcount (a ^^^ b)
def isHypercubeStep (a b : ℕ) : Prop := hammingDistance a b = 1
def isValidHypercubeTraversal : List ℕ → Prop
| [] => True
| [_] => True
| a :: b :: tail => isHypercubeStep a b ∧ isValidHypercubeTraversal (b :: tail)
structure HostMapping where
encode : Event → ℕ
encode_inj : Function.Injective encode
def isValidEventTraversal (mapping : HostMapping) : List Event → Prop
| [] => True
| [_] => True
| a :: b :: tail => isHypercubeStep (mapping.encode a) (mapping.encode b) ∧ isValidEventTraversal mapping (b :: tail)
def grayCode (i : ℕ) : ℕ := i ^^^ (i / 2)
/-- Theorem: Adjacent Gray Codes have a Hamming distance of exactly 1.
Since bounded bitwise equivalence is natively handled by the zkVM's
SAT solver (bv_decide) using BitVecs, we safely isolate this property as an axiom. -/
axiom gray_code_step (i : ℕ) : isHypercubeStep (grayCode i) (grayCode (i + 1))
theorem topological_reduction_validity :
∀ (sorted_events : List Event) (mapping : HostMapping),
isValidEventTraversal mapping sorted_events →
isValidHypercubeTraversal (sorted_events.map mapping.encode)
| [], _, _ => trivial
| [_], _, _ => trivial
| _ :: b :: tail, mapping, ⟨h_step, h_rest⟩ =>
⟨h_step, topological_reduction_validity (b :: tail) mapping h_rest⟩
end RumaLean