Cairn · module

Cairn.Frame.Bytes

lean/Cairn/Frame/Bytes.lean

The byte layer of the boundary format (docs/frame.md): little-endian primitives, three ways.

  • Enc.* encode to List UInt8. They are the specification: short, and what the round-trip theorems talk about.
  • Dec is a decoder over List UInt8 (consume a prefix, return the value and the rest). Each primitive has a round-trip lemma of the form d (Enc.x v ++ rest) = some (v, rest), which is what makes the whole-frame theorems compose.
  • Put.* append to a ByteArray. They are what runs; Put.x_data says each one appends exactly Enc.x to the buffer's bytes.

Integers that cross the boundary are 32-bit: Int as two's complement (i32), Nat as unsigned (u32). The round trip needs the value to fit, stated with FitsI32 and FitsU32.

Order

Definitions 51

def

Cairn.Wire.FitsU32

lean/Cairn/Frame/Bytes.lean:22
FitsU32 (n : Nat) : Prop
Instances

Decidable

def

Cairn.Wire.FitsI32

lean/Cairn/Frame/Bytes.lean:23
FitsI32 (v : Int) : Prop
Instances

Decidable

def

Cairn.Wire.FitsStr

lean/Cairn/Frame/Bytes.lean:25
FitsStr (s : String) : Prop

A string whose UTF-8 encoding has a u32 length.

Instances

Decidable

def

Cairn.Wire.FitsU64

lean/Cairn/Frame/Bytes.lean:26
FitsU64 (n : Nat) : Prop
Instances

Decidable

def

Cairn.Wire.FitsI64

lean/Cairn/Frame/Bytes.lean:27
FitsI64 (v : Int) : Prop
Instances

Decidable

def

Cairn.Wire.FitsBytes

lean/Cairn/Frame/Bytes.lean:29
FitsBytes (b : ByteArray) : Prop

A byte payload with a u32 length.

Instances

Decidable

def

Cairn.Wire.toU32

lean/Cairn/Frame/Bytes.lean:39
toU32 (v : Int) : Nat

Two's complement, as an unsigned 32-bit value.

def

Cairn.Wire.ofU32

lean/Cairn/Frame/Bytes.lean:40
ofU32 (n : Nat) : Int
def

Cairn.Wire.toU64

lean/Cairn/Frame/Bytes.lean:45
toU64 (v : Int) : Nat

Two's complement, as an unsigned 64-bit value.

def

Cairn.Wire.ofU64

lean/Cairn/Frame/Bytes.lean:46
ofU64 (n : Nat) : Int
def

Cairn.Wire.i32Word

lean/Cairn/Frame/Bytes.lean:56
i32Word (v : Int) : UInt32

The two's-complement word of v, without big-integer arithmetic: 2³² is not a small number to Lean's runtime, so v % 4294967296 (and even the literal) goes through GMP.

def

Cairn.Wire.satU32

lean/Cairn/Frame/Bytes.lean:73
satU32 (n : Nat) : Nat

Clamp to the largest u32 (font fields cross saturating, docs/frame.md).

def

Cairn.Wire.satI32

lean/Cairn/Frame/Bytes.lean:82
satI32 (v : Int) : Int

Clamp to the i32 range (a text draw's tracking crosses saturating, like font fields).

def

Cairn.Wire.ofBytes

lean/Cairn/Frame/Bytes.lean:90
ofBytes (a b c d : UInt8) : Nat
def

Cairn.Wire.Enc.u32

lean/Cairn/Frame/Bytes.lean:101
u32 (n : Nat) : List UInt8
def

Cairn.Wire.Enc.i32

lean/Cairn/Frame/Bytes.lean:104
i32 (v : Int) : List UInt8
def

Cairn.Wire.Enc.bool

lean/Cairn/Frame/Bytes.lean:106
bool (b : Bool) : List UInt8
def

Cairn.Wire.Enc.u64

lean/Cairn/Frame/Bytes.lean:109
u64 (n : Nat) : List UInt8

Low word, then high word.

def

Cairn.Wire.Enc.i64

lean/Cairn/Frame/Bytes.lean:111
i64 (v : Int) : List UInt8
def

Cairn.Wire.Enc.bytes

lean/Cairn/Frame/Bytes.lean:114
bytes (b : ByteArray) : List UInt8

u32 byte length, then the bytes.

def

Cairn.Wire.Enc.str

lean/Cairn/Frame/Bytes.lean:117
str (s : String) : List UInt8

u32 byte length, then the UTF-8 bytes.

def

Cairn.Wire.Enc.pad

lean/Cairn/Frame/Bytes.lean:120
pad (n : Nat) : List UInt8

Reserved bytes: written as zero, ignored when read.

def

Cairn.Wire.Enc.sect

lean/Cairn/Frame/Bytes.lean:123
sect (body : List UInt8) : List UInt8

A sized section: u32 byte length, then the body.

def

Cairn.Wire.Dec

lean/Cairn/Frame/Bytes.lean:135
Dec (α : Type) : Type

Consume a prefix of the input; none is a decoding error.

Instances

Monad

def

Cairn.Wire.Dec.pure

lean/Cairn/Frame/Bytes.lean:141
pure {α : Type} (a : α) : Dec α
def

Cairn.Wire.Dec.bind

lean/Cairn/Frame/Bytes.lean:143
bind {α β : Type} (d : Dec α) (f : α → Dec β) : Dec β
def

Cairn.Wire.Dec.fail

lean/Cairn/Frame/Bytes.lean:152
fail {α : Type} : Dec α
def

Cairn.Wire.Dec.byte

lean/Cairn/Frame/Bytes.lean:166
byte : Dec UInt8
def

Cairn.Wire.Dec.u32

lean/Cairn/Frame/Bytes.lean:170
u32 : Dec Nat
def

Cairn.Wire.Dec.i32

lean/Cairn/Frame/Bytes.lean:174
i32 : Dec Int
def

Cairn.Wire.Dec.bool

lean/Cairn/Frame/Bytes.lean:176
bool : Dec Bool
def

Cairn.Wire.Dec.u64

lean/Cairn/Frame/Bytes.lean:182
u64 : Dec Nat
def

Cairn.Wire.Dec.i64

lean/Cairn/Frame/Bytes.lean:187
i64 : Dec Int
def

Cairn.Wire.Dec.take

lean/Cairn/Frame/Bytes.lean:189
take (n : Nat) : Dec (List UInt8)
def

Cairn.Wire.Dec.skip

lean/Cairn/Frame/Bytes.lean:192
skip (n : Nat) : Dec Unit
def

Cairn.Wire.Dec.str

lean/Cairn/Frame/Bytes.lean:195
str : Dec String
def

Cairn.Wire.Dec.bytes

lean/Cairn/Frame/Bytes.lean:202
bytes : Dec ByteArray
def

Cairn.Wire.Dec.count

lean/Cairn/Frame/Bytes.lean:208
count {α : Type} (d : Dec α) : Nat → Dec (List α)

n values, one after another.

def

Cairn.Wire.Dec.sect

lean/Cairn/Frame/Bytes.lean:217
sect {α : Type} (d : Dec α) : Dec α

A sized section: read the u32 byte length, decode the body with d, which must consume it exactly.

def

Cairn.Wire.Dec.run

lean/Cairn/Frame/Bytes.lean:225
run {α : Type} (d : Dec α) (l : List UInt8) : Option α

Decode all of l; trailing bytes are an error.

def

Cairn.Wire.Put.u8

lean/Cairn/Frame/Bytes.lean:318
u8 (b : ByteArray) (v : UInt8) : ByteArray
def

Cairn.Wire.Put.word

lean/Cairn/Frame/Bytes.lean:321
word (b : ByteArray) (w : UInt32) : ByteArray

A machine word, little-endian, by shifts.

def

Cairn.Wire.Put.u32

lean/Cairn/Frame/Bytes.lean:324
u32 (b : ByteArray) (n : Nat) : ByteArray
def

Cairn.Wire.Put.i32

lean/Cairn/Frame/Bytes.lean:326
i32 (b : ByteArray) (v : Int) : ByteArray
def

Cairn.Wire.Put.bool

lean/Cairn/Frame/Bytes.lean:328
bool (b : ByteArray) (v : Bool) : ByteArray
def

Cairn.Wire.Put.str

lean/Cairn/Frame/Bytes.lean:330
str (b : ByteArray) (s : String) : ByteArray
def

Cairn.Wire.Put.u64

lean/Cairn/Frame/Bytes.lean:332
u64 (b : ByteArray) (n : Nat) : ByteArray
def

Cairn.Wire.Put.i64

lean/Cairn/Frame/Bytes.lean:335
i64 (b : ByteArray) (v : Int) : ByteArray
def

Cairn.Wire.Put.bytes

lean/Cairn/Frame/Bytes.lean:337
bytes (b x : ByteArray) : ByteArray
def

Cairn.Wire.Put.pad

lean/Cairn/Frame/Bytes.lean:339
pad (b : ByteArray) : Nat → ByteArray
def

Cairn.Wire.Put.setU32

lean/Cairn/Frame/Bytes.lean:344
setU32 (b : ByteArray) (i n : Nat) : ByteArray

Overwrite the four bytes at i with n (for a section length, once the body is known).

Theorems 45

theorem

Cairn.Wire.fitsU32_zero

provedlean/Cairn/Frame/Bytes.lean:42
fitsU32_zero : FitsU32 0
theorem

Cairn.Wire.toU64_fits

provedlean/Cairn/Frame/Bytes.lean:49
toU64_fits (v : Int) : FitsU64 (toU64 v)
theorem

Cairn.Wire.ofU64_toU64

provedlean/Cairn/Frame/Bytes.lean:51
ofU64_toU64 {v : Int} (h : FitsI64 v) : ofU64 (toU64 v) = v
theorem

Cairn.Wire.i32Word_eq

provedlean/Cairn/Frame/Bytes.lean:59
i32Word_eq (v : Int) : i32Word v = UInt32.ofNat (toU32 v)
theorem

Cairn.Wire.toU32_fits

provedlean/Cairn/Frame/Bytes.lean:67
toU32_fits (v : Int) : FitsU32 (toU32 v)
theorem

Cairn.Wire.ofU32_toU32

provedlean/Cairn/Frame/Bytes.lean:69
ofU32_toU32 {v : Int} (h : FitsI32 v) : ofU32 (toU32 v) = v
theorem

Cairn.Wire.satU32_of_fits

provedlean/Cairn/Frame/Bytes.lean:75
satU32_of_fits {n : Nat} (h : FitsU32 n) : satU32 n = n
theorem

Cairn.Wire.satU32_fits

provedlean/Cairn/Frame/Bytes.lean:78
satU32_fits (n : Nat) : FitsU32 (satU32 n)
theorem

Cairn.Wire.satI32_of_fits

provedlean/Cairn/Frame/Bytes.lean:84
satI32_of_fits {v : Int} (h : FitsI32 v) : satI32 v = v
theorem

Cairn.Wire.satI32_fits

provedlean/Cairn/Frame/Bytes.lean:87
satI32_fits (v : Int) : FitsI32 (satI32 v)
theorem

Cairn.Wire.ofBytes_lt

provedlean/Cairn/Frame/Bytes.lean:93
ofBytes_lt (a b c d : UInt8) : ofBytes a b c d < 4294967296
theorem

Cairn.Wire.Enc.length_u32

provedlean/Cairn/Frame/Bytes.lean:125
length_u32 (n : Nat) : (u32 n).length = 4
theorem

Cairn.Wire.Enc.length_i32

provedlean/Cairn/Frame/Bytes.lean:126
length_i32 (v : Int) : (i32 v).length = 4
theorem

Cairn.Wire.Enc.length_u64

provedlean/Cairn/Frame/Bytes.lean:127
length_u64 (n : Nat) : (u64 n).length = 8
theorem

Cairn.Wire.Enc.length_i64

provedlean/Cairn/Frame/Bytes.lean:128
length_i64 (v : Int) : (i64 v).length = 8
theorem

Cairn.Wire.Dec.pure_apply

provedlean/Cairn/Frame/Bytes.lean:154
pure_apply {α : Type} (a : α) (l : List UInt8) : pure a l = some (a, l)
theorem

Cairn.Wire.Dec.bind_apply

provedlean/Cairn/Frame/Bytes.lean:156
bind_apply {α β : Type} (d : Dec α) (f : α → Dec β) (l : List UInt8) :
  (d >>= f) l =
    match d l with
    | some (a, r) => f a r
    | none => none
theorem

Cairn.Wire.Dec.fail_apply

provedlean/Cairn/Frame/Bytes.lean:159
fail_apply {α : Type} (l : List UInt8) : fail l = none
theorem

Cairn.Wire.Dec.bind_of_eq

provedlean/Cairn/Frame/Bytes.lean:162
bind_of_eq {α β : Type} {d : Dec α} {f : α → Dec β} {l r : List UInt8}
  {a : α} (h : d l = some (a, r)) : (d >>= f) l = f a r

The step every compound round-trip proof takes: run the first decoder, continue.

theorem

Cairn.Wire.Dec.byte_cons

provedlean/Cairn/Frame/Bytes.lean:232
byte_cons (b : UInt8) (r : List UInt8) : byte (b :: r) = some (b, r)
theorem

Cairn.Wire.Dec.u32_enc

provedlean/Cairn/Frame/Bytes.lean:234
u32_enc {n : Nat} (h : FitsU32 n) (r : List UInt8) :
  u32 (Enc.u32 n ++ r) = some (n, r)
theorem

Cairn.Wire.Dec.i32_enc

provedlean/Cairn/Frame/Bytes.lean:242
i32_enc {v : Int} (h : FitsI32 v) (r : List UInt8) :
  i32 (Enc.i32 v ++ r) = some (v, r)
theorem

Cairn.Wire.Dec.u64_enc

provedlean/Cairn/Frame/Bytes.lean:246
u64_enc {n : Nat} (h : FitsU64 n) (r : List UInt8) :
  u64 (Enc.u64 n ++ r) = some (n, r)
theorem

Cairn.Wire.Dec.i64_enc

provedlean/Cairn/Frame/Bytes.lean:254
i64_enc {v : Int} (h : FitsI64 v) (r : List UInt8) :
  i64 (Enc.i64 v ++ r) = some (v, r)
theorem

Cairn.Wire.Dec.bool_enc

provedlean/Cairn/Frame/Bytes.lean:258
bool_enc (b : Bool) (r : List UInt8) : bool (Enc.bool b ++ r) = some (b, r)
theorem

Cairn.Wire.Dec.take_enc

provedlean/Cairn/Frame/Bytes.lean:261
take_enc (l r : List UInt8) : take l.length (l ++ r) = some (l, r)
theorem

Cairn.Wire.Dec.skip_pad

provedlean/Cairn/Frame/Bytes.lean:265
skip_pad (n : Nat) (r : List UInt8) : skip n (Enc.pad n ++ r) = some ((), r)
theorem

Cairn.Wire.Dec.fromUTF8?_toUTF8

provedlean/Cairn/Frame/Bytes.lean:269
fromUTF8?_toUTF8 (s : String) :
  String.fromUTF8? { data := s.toUTF8.data.toList.toArray } = some s
theorem

Cairn.Wire.Dec.str_enc

provedlean/Cairn/Frame/Bytes.lean:274
str_enc {s : String} (h : FitsStr s) (r : List UInt8) :
  str (Enc.str s ++ r) = some (s, r)
theorem

Cairn.Wire.Dec.bytes_enc

provedlean/Cairn/Frame/Bytes.lean:284
bytes_enc {b : ByteArray} (h : FitsBytes b) (r : List UInt8) :
  bytes (Enc.bytes b ++ r) = some (b, r)
theorem

Cairn.Wire.Dec.count_enc

provedlean/Cairn/Frame/Bytes.lean:291
count_enc {α : Type} {d : Dec α} {e : α → List UInt8} (xs : List α)
  (h : ∀ (x : α), x ∈ xs → ∀ (r : List UInt8), d (e x ++ r) = some (x, r))
  (r : List UInt8) : d.count xs.length (List.flatMap e xs ++ r) = some (xs, r)
theorem

Cairn.Wire.Dec.sect_enc

provedlean/Cairn/Frame/Bytes.lean:301
sect_enc {α : Type} {d : Dec α} {body : List UInt8} {a : α}
  (hd : d body = some (a, [])) (hn : FitsU32 body.length) (r : List UInt8) :
  d.sect (Enc.sect body ++ r) = some (a, r)
theorem

Cairn.Wire.Dec.run_enc

provedlean/Cairn/Frame/Bytes.lean:307
run_enc {α : Type} {d : Dec α} {l : List UInt8} {a : α}
  (h : d (l ++ []) = some (a, [])) : d.run l = some a
theorem

Cairn.Wire.Put.u8_data

provedlean/Cairn/Frame/Bytes.lean:348
u8_data (b : ByteArray) (v : UInt8) :
  (u8 b v).data.toList = b.data.toList ++ [v]
theorem

Cairn.Wire.Put.shift8

provedlean/Cairn/Frame/Bytes.lean:352
shift8 (n : Nat) : (UInt32.ofNat n >>> 8).toUInt8 = UInt8.ofNat (n / 256)
theorem

Cairn.Wire.Put.shift16

provedlean/Cairn/Frame/Bytes.lean:358
shift16 (n : Nat) : (UInt32.ofNat n >>> 16).toUInt8 = UInt8.ofNat (n / 65536)
theorem

Cairn.Wire.Put.shift24

provedlean/Cairn/Frame/Bytes.lean:364
shift24 (n : Nat) :
  (UInt32.ofNat n >>> 24).toUInt8 = UInt8.ofNat (n / 16777216)
theorem

Cairn.Wire.Put.u32_data

provedlean/Cairn/Frame/Bytes.lean:370
u32_data (b : ByteArray) (n : Nat) :
  (u32 b n).data.toList = b.data.toList ++ Enc.u32 n
theorem

Cairn.Wire.Put.i32_data

provedlean/Cairn/Frame/Bytes.lean:377
i32_data (b : ByteArray) (v : Int) :
  (i32 b v).data.toList = b.data.toList ++ Enc.i32 v
theorem

Cairn.Wire.Put.bool_data

provedlean/Cairn/Frame/Bytes.lean:381
bool_data (b : ByteArray) (v : Bool) :
  (bool b v).data.toList = b.data.toList ++ Enc.bool v
theorem

Cairn.Wire.Put.str_data

provedlean/Cairn/Frame/Bytes.lean:385
str_data (b : ByteArray) (s : String) :
  (str b s).data.toList = b.data.toList ++ Enc.str s
theorem

Cairn.Wire.Put.u64_data

provedlean/Cairn/Frame/Bytes.lean:390
u64_data (b : ByteArray) (n : Nat) :
  (u64 b n).data.toList = b.data.toList ++ Enc.u64 n
theorem

Cairn.Wire.Put.i64_data

provedlean/Cairn/Frame/Bytes.lean:394
i64_data (b : ByteArray) (v : Int) :
  (i64 b v).data.toList = b.data.toList ++ Enc.i64 v
theorem

Cairn.Wire.Put.bytes_data

provedlean/Cairn/Frame/Bytes.lean:398
bytes_data (b x : ByteArray) :
  (bytes b x).data.toList = b.data.toList ++ Enc.bytes x
theorem

Cairn.Wire.Put.pad_data

provedlean/Cairn/Frame/Bytes.lean:403
pad_data (b : ByteArray) (n : Nat) :
  (pad b n).data.toList = b.data.toList ++ Enc.pad n

Instances 7

instance

Cairn.Wire.instDecidableFitsU32

lean/Cairn/Frame/Bytes.lean:31
instDecidableFitsU32 (n : Nat) : Decidable (FitsU32 n)
instance

Cairn.Wire.instDecidableFitsI32

lean/Cairn/Frame/Bytes.lean:32
instDecidableFitsI32 (v : Int) : Decidable (FitsI32 v)
instance

Cairn.Wire.instDecidableFitsStr

lean/Cairn/Frame/Bytes.lean:33
instDecidableFitsStr (s : String) : Decidable (FitsStr s)
instance

Cairn.Wire.instDecidableFitsU64

lean/Cairn/Frame/Bytes.lean:34
instDecidableFitsU64 (n : Nat) : Decidable (FitsU64 n)
instance

Cairn.Wire.instDecidableFitsI64

lean/Cairn/Frame/Bytes.lean:35
instDecidableFitsI64 (v : Int) : Decidable (FitsI64 v)
instance

Cairn.Wire.instDecidableFitsBytes

lean/Cairn/Frame/Bytes.lean:36
instDecidableFitsBytes (b : ByteArray) : Decidable (FitsBytes b)
instance

Cairn.Wire.Dec.instMonad

lean/Cairn/Frame/Bytes.lean:148
instMonad : Monad Dec