Wire.lean
Declarations on this page (17)
- abbrev MessageKey
- abbrev SerializedState
- abbrev SerializedMessage
- def empty_state
- abbrev emptyState
- inductive Version
- def toU8
- def fromU8?
- def toI32
- def MAX_VARINT_BYTES_LEN
- def encodeVarintLoop
- def encode_varint
- def decodeVarintLoop
- def decode_varint
- structure MsgPreamble
- def msg_preamble
- abbrev msgPreamble
SPQR V1: wire format, Version, varint, and msg_preamble (src/)
Trusted in part. The theorems of Computational/ use only part of this file (see Computational/): the rest (encodeVarint, decodeVarint, decodeVarintAt, decode_varint_at, MsgPreamble. / toLibsignalSpqr, the Version constants V0, V1, MAX and DISABLED, Version. and Version.) is checked by Lean.
Defines the shared wire-format aliases (MessageKey, SerializedState, SerializedMessage, empty_state / emptyState), Version (toU8, toI32), 10-byte LEB128 varint codecs (MAX_VARINT_BYTES_LEN, encode_varint, decode_varint), and message header parser (MsgPreamble, msg_preamble / msgPreamble), together with equivalence theorems against LibsignalSpqr.lean (decode, decode_).
@[expose] public sectionnamespace CryptoVerification.LibsignalSpqrV1
Wire type aliases and Version (src/)
abbrev MessageKey
abbrev MessageKey := Option Bytesabbrev SerializedState
abbrev SerializedState := Bytesabbrev SerializedMessage
abbrev SerializedMessage := Bytesdef empty_state
def empty_state : SerializedState := []abbrev emptyState
abbrev emptyState : SerializedState := empty_stateinductive Version
SPQR protocol version (pub enum Version, src/).
inductive Version where
| v0
| v1
deriving DecidableEq, Repr, Inhabitednamespace Version4 declarations not shown (lines 54–60): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification.
def fromU8?
1 declaration not shown (lines 71–74): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification.
1 declaration not shown (lines 80–83): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification.
end Version
Varint encoding/decoding and msg_preamble (src/)
def MAX_VARINT_BYTES_LEN
def MAX_VARINT_BYTES_LEN : Nat := 10def encodeVarintLoop
def encodeVarintLoop : Nat → BitVec 64 → Bytes
| 0, _ => []
| fuel + 1, a =>
let byte : BitVec 8 := (a &&& 0x7f#64).truncate 8
if a < 0x80#64 then
[byte]
else
(0x80#8 ||| byte) :: encodeVarintLoop fuel (a >>> 7)def encode_varint
encode_varint (src/).
def encode_varint (a : BitVec 64) (into : SerializedMessage := []) : SerializedMessage :=
into ++ encodeVarintLoop MAX_VARINT_BYTES_LEN a1 declaration not shown (lines 104–105): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification.
def decodeVarintLoop
Inner loop of decode_varint (src/); at i = 9, << 63 drops bits 1..6 of b & 0x7f.
def decodeVarintLoop : Nat → BitVec 64 → Bytes → Option (BitVec 64 × Bytes)
| _, _, [] => none
| i, out, b :: rest =>
let out := out ||| ((b &&& 0x7f#8).zeroExtend 64 <<< (7 * i))
if b &&& 0x80#8 = 0#8 then
some (out, rest)
else if i + 1 = MAX_VARINT_BYTES_LEN then
none
else
decodeVarintLoop (i + 1) out restdef decode_varint
LEB128 u64 decoder (decode_varint, src/); returns .error .msgDecode on truncation or a continuation bit on the 10th byte.
def decode_varint (fromSlice : Bytes) : Except Error (BitVec 64 × Bytes) :=
match decodeVarintLoop 0 0#64 fromSlice with
| some res => .ok res
| none => .error .msgDecode3 declarations not shown (lines 127–139): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., CryptoVerification., CryptoVerification.
structure MsgPreamble
Parsed SPQR message header (struct MsgPreamble<'a>, src/).
4 declarations not shown (lines 151–165): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification.
def msg_preamble
msg_preamble (src/): returns all-zero preamble on [], otherwise parses the version byte, nonzero u64 varint epoch, and u32 varint index.
def msg_preamble (msg : SerializedMessage) : Except Error MsgPreamble :=
match msg with
| [] => .ok { version := 0#8, epoch := 0#64, index := 0#32, remaining := [] }
| version :: rest => do
let (epoch, rest) ← decode_varint rest
if epoch = 0#64 then
.error .msgDecode
else
let (index64, rest) ← decode_varint rest
if index64.toNat < 2 ^ 32 then
.ok { version := version, epoch := epoch, index := index64.setWidth 32, remaining := rest }
else
.error .msgDecodeabbrev msgPreamble
abbrev msgPreamble := msg_preamble3 declarations not shown (lines 189–230): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., CryptoVerification., CryptoVerification.
end CryptoVerification.LibsignalSpqrV1