SNAPKITTYWEST's picture
push from SNAPKITTYWEST/hyperkitty-constraint-dsl
224e773 verified
Raw History Blame Contribute Delete
10.1 kB
39
def CBOREncoding : Type := Vector UInt8 256  -- max size for a SEB record
42
def CBOR_MAJOR_BYTES : UInt8 := 0x40
45
def cbor_encode_seb_record (record : Vector UInt8 96) : CBOREncoding :=
body Β· 10 lines
  -- Byte string major type = 2, length 96 = 0x58 (1-byte length follows)
  -- 0x5858: major=2, additional=24, then 0x60 (= 96)
  let prefix := Vector.replicate 256 (0x00 : UInt8)
  let prefix := prefix.set 0 0x58  -- major type 2, additional length 24
  let prefix := prefix.set 1 0x60  -- 96 in decimal
  Vector.zipWith (fun i a =>
    if i < 2 then a
    else if i - 2 < 96 then record.get ⟨i - 2, by omega⟩
    else 0x00
  ) prefix (Vector.replicate 256 (0x00 : UInt8))
62
theorem cbor_encoding_is_canonical :
  βˆ€ (record : Vector UInt8 96),
    βˆ€ enc1 enc2 : CBOREncoding,
      cbor_encode_seb_record record = enc1 β†’
      cbor_encode_seb_record record = enc2 β†’
      enc1 = enc2 := by
proof Β· 2 lines
  intro record enc1 enc2 h1 h2
  rw [h1, h2]
72
theorem cbor_encoding_unique :
  βˆ€ (record : Vector UInt8 96),
    βˆƒ! enc : CBOREncoding, enc = cbor_encode_seb_record record := by
proof Β· 3 lines
  intro record
  use cbor_encode_seb_record record
  simp
89
def Blake3Hash : Type := Vector UInt8 32
92
opaque blake3_hash : Vector UInt8 β†’ Blake3Hash
96
axiom blake3_deterministic :
  βˆ€ (x y : Vector UInt8),
    x = y β†’ blake3_hash x = blake3_hash y
101
axiom blake3_collision_resistant :
  βˆ€ (x y : Vector UInt8),
    blake3_hash x = blake3_hash y β†’ x = y ∨ computationally_infeasible_to_find_collision
105
axiom computationally_infeasible_to_find_collision : Prop
111
theorem blake3_hash_is_deterministic :
  βˆ€ (record : Vector UInt8 96),
    blake3_hash (record.map (Fin.cast) : Vector UInt8 96) =
    blake3_hash (record.map (Fin.cast) : Vector UInt8 96) := by
proof Β· 2 lines
  intro record
  rfl
119
theorem blake3_hash_reproducible :
  βˆ€ (record : Vector UInt8 96),
    let hash1 := blake3_hash (record.map (Fin.cast) : Vector UInt8 96)
proof Β· 5 lines
    let hash2 := blake3_hash (record.map (Fin.cast) : Vector UInt8 96)
    hash1 = hash2 := by
  intro record
  simp
  rfl
142
def Ed25519PublicKey : Type := Vector UInt8 32
143
def Ed25519SecretKey : Type := Vector UInt8 32
144
def Ed25519Signature : Type := Vector UInt8 64
147
opaque ed25519_sign : Ed25519SecretKey β†’ Vector UInt8 β†’ Ed25519Signature
148
opaque ed25519_verify : Ed25519PublicKey β†’ Vector UInt8 β†’ Ed25519Signature β†’ Bool
152
axiom ed25519_signature_deterministic :
  βˆ€ (sk : Ed25519SecretKey) (msg : Vector UInt8),
    let sig1 := ed25519_sign sk msg
body Β· 2 lines
    let sig2 := ed25519_sign sk msg
    sig1 = sig2
160
axiom ed25519_unforgeability :
  βˆ€ (pk : Ed25519PublicKey) (msg : Vector UInt8) (sig : Ed25519Signature),
    ed25519_verify pk msg sig = true β†’
    (βˆƒ sk : Ed25519SecretKey,
       ed25519_sign sk msg = sig) ∨
    discrete_log_broken
167
axiom discrete_log_broken : Prop  -- This is the hard computational assumption
178
theorem ed25519_signature_is_unforgeable :
  βˆ€ (pk : Ed25519PublicKey) (msg : Vector UInt8) (sig : Ed25519Signature),
    ed25519_verify pk msg sig = true β†’
    (βˆƒ sk : Ed25519SecretKey,
       ed25519_sign sk msg = sig ∧
       -- This sk correctly produced sig
       ed25519_verify pk msg (ed25519_sign sk msg) = true) ∨
    -- Or an attacker solved ECDLP
    discrete_log_broken := by
proof Β· 3 lines
  intro pk msg sig hverify
  -- Apply unforgeability axiom
  apply ed25519_unforgeability pk msg sig hverify
192
theorem ed25519_seb_record_signature_is_deterministic :
  βˆ€ (sk : Ed25519SecretKey) (record : Vector UInt8 96),
    let sig1 := ed25519_sign sk record
proof Β· 4 lines
    let sig2 := ed25519_sign sk record
    sig1 = sig2 := by
  intro sk record
  apply ed25519_signature_deterministic
211
theorem canonical_serialization_chain :
  βˆ€ (record : Vector UInt8 96) (sk : Ed25519SecretKey),
    let encoding := cbor_encode_seb_record record
proof Β· 14 lines
    let hash := blake3_hash (encoding.map (Fin.cast) : Vector UInt8 256)
    let sig := ed25519_sign sk (encoding.map (Fin.cast) : Vector UInt8 256)
    -- The encoding, hash, and signature are all canonical/unique
    (βˆƒ! enc : CBOREncoding, enc = encoding) ∧
    (βˆƒ! h : Blake3Hash, h = hash) ∧
    (βˆƒ! s : Ed25519Signature, s = sig) := by
  intro record sk
  constructor
  Β· apply cbor_encoding_unique
  constructor
  Β· use blake3_hash _
    simp
  Β· use ed25519_sign sk _
    simp