Assemble.lean
Declarations on this page (13)
SPQR V1: concrete model assembly (spqrV1Assembled)
Trusted in part. The theorems of Computational/ use every definition in this file except saltToMessageKey and spqrV1RecvAssembled, which are checked by Lean (see Computational/).
Instantiates the abstract dependency bundles (spqrPolyApi, spqrAuthApi, spqrDeps) to produce the concrete SPQR V1 models spqrV1Assembled and spqrV1RecvAssembled (DEV-SPQR2). Proves chain and receive key-length invariants (Chain., recv_key_valid, recv_, recv_) and receive-panic monotonicity (toSpqr).
@[expose] public sectionnamespace CryptoVerification.LibsignalSpqrV1open CryptoGames.HKDF (Salt hkdfBlocks hkdfExpand)open CryptoGames.LibsignalSession (ShaCompress)open CryptoVerification.LibsignalTripleRatchet (SpqrModel)instance
def protoPolyEncoderToPb
def protoPolyEncoderToPb (pb : ProtoPolynomialEncoder) : PolynomialEncoder :=
{ idx := pb.idx, pts := pb.pts, polys := pb.polys }def pbToProtoPolyEncoder
def pbToProtoPolyEncoder (pb : PolynomialEncoder) : ProtoPolynomialEncoder :=
{ idx := pb.idx, pts := pb.pts, polys := pb.polys }def protoPolyDecoderToPb
def protoPolyDecoderToPb (pb : ProtoPolynomialDecoder) : PolynomialDecoder :=
{ ptsNeeded := pb.ptsNeeded, polys := pb.polys, pts := pb.pts, isComplete := pb.isComplete }def pbToProtoPolyDecoder
def pbToProtoPolyDecoder (pb : PolynomialDecoder) : ProtoPolynomialDecoder :=
{ ptsNeeded := pb.ptsNeeded, polys := pb.polys, pts := pb.pts, isComplete := pb.isComplete }def spqrPolyApi
Concrete PolyApi instance backed by PolyEncoder and PolyDecoder (Poly.lean).
def spqrPolyApi : PolyApi where
Chunk := Chunk
PolynomialError := PolynomialError
EncodingError := EncodingError
PolyEncoder := PolyEncoder
PolyDecoder := PolyDecoder
mkChunk := Chunk.mk
chunkIndex := Chunk.index
chunkData := Chunk.data
encode_bytes := fun msg =>
(PolyEncoder.encodeBytes msg).mapError PolyPanic.toSpqrPanic
next_chunk := fun enc =>
enc.nextChunk.mapError PolyPanic.toSpqrPanic
encoder_into_pb := fun enc =>
protoPolyEncoderToPb enc.intoPb
encoder_from_pb := fun pb =>
(PolyEncoder.fromPb (pbToProtoPolyEncoder pb)).mapError PolyPanic.toSpqrPanic
decoder_new := fun lenBytes =>
(PolyDecoder.new lenBytes).mapError PolyPanic.toSpqrPanic
add_chunk := fun dec chunk =>
(dec.addChunk chunk).mapError PolyPanic.toSpqrPanic
decoded_message := fun dec =>
dec.decodedMessage.mapError PolyPanic.toSpqrPanic
decoder_into_pb := fun dec =>
protoPolyDecoderToPb dec.intoPb
decoder_from_pb := fun pb =>
(PolyDecoder.fromPb (pbToProtoPolyDecoder pb)).mapError PolyPanic.toSpqrPanicdef spqrAuthApi
Concrete AuthApi instance backed by Authenticator (Auth.lean) and hkdfToVec F.
def spqrAuthApi (F : ShaCompress) : AuthApi where
Authenticator := Authenticator
new := fun rootKey ep => .ok (Authenticator.new F rootKey ep)
update := fun auth ep k => .ok (auth.update F ep k)
mac_hdr := fun auth ep hdr => auth.macHdr F ep hdr
mac_ct := fun auth ep ct => auth.macCt F ep ct
verify_hdr := fun auth ep hdr expectedMac => auth.verifyHdrError F ep hdr expectedMac
verify_ct := fun auth ep ct expectedMac => auth.verifyCtError F ep ct expectedMac
into_pb := Authenticator.intoPb
from_pb := Authenticator.fromPb
hkdf_to_vec := hkdfToVec F
2. Concrete SpqrApiDeps and Assembled Models (spqrV1Assembled, spqrV1RecvAssembled)
def liftStatesResult
def liftStatesResult {α : Type} (r : StatesResult α) : SpqrApiResult α :=
match r with
| .ok v => .ok v
| .error (.err e) => .error (.err e)
| .error (.panic _) => .error (.panic .v1)def pqRatchetStateToPb
def pqRatchetStateToPb (st : PqRatchetState) : PqRatchetStatePB V1State :=
{ inner := st.inner.map fun (.v1 v) => v
versionNegotiation := st.versionNegotiation
chain := st.chain }def pbToPqRatchetState
def pbToPqRatchetState (st : PqRatchetStatePB V1State) : PqRatchetState :=
{ inner := st.inner.map PqRatchetState.Inner.v1
versionNegotiation := st.versionNegotiation
chain := st.chain }2 declarations not shown (lines 118–130): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., CryptoVerification.
def spqrDeps
Concrete SpqrApiDeps bundle wiring Proto.lean (V1State, Proto., Proto.) and States.lean (States spqrPolyApi (spqrAuthApi F), Message spqrPolyApi) into Top.lean.
def spqrDeps (F : ShaCompress) (K : MlKem) : SpqrApiDeps where
V1StatePB := V1State
decodeState := fun bs =>
match Proto.decodeState bs with
| .ok st => .ok (pqRatchetStateToPb st)
| .error _ => .error ()
encodeState := fun st =>
Proto.encodeState (pbToPqRatchetState st)
States := States spqrPolyApi (spqrAuthApi F)
statesFromPb := fun pb =>
liftStatesResult (States.from_pb spqrPolyApi (spqrAuthApi F) pb)
statesIntoPb := States.into_pb
MessageV1 := Message spqrPolyApi
statesSend := fun st tape =>
match st.send K tape with
| .ok (sOut, tape') => (.ok (sOut.msg, sOut.key, sOut.state), tape')
| .error (.err e) => (.error (.err e), tape)
-- On a panic the returned tape is irrelevant: `toSpqrModel`'s `onPanic` clause never reads
-- `draws`.
| .error (.panic _) => (.error (.panic .v1), tape)
statesRecv := fun st msg =>
match st.recv K msg with
| .ok rOut => .ok (rOut.key, rOut.state)
| .error (.err e) => .error (.err e)
| .error (.panic _) => .error (.panic .v1)
msgSerialize := Message.serialize
msgDeserialize := fun ep rem =>
match Message.deserialize spqrPolyApi ep rem with
| .ok m => .ok m
| .error e => .error (.err e)
msgEpoch := Message.epoch
-- `States.init_a` / `init_b` never return `.error`; `.error .v1` keeps the match total.
initV1Inner := fun dir authKey =>
match dir with
| .a2b =>
match States.init_a spqrPolyApi (spqrAuthApi F) authKey with
| .ok st => .ok st.into_pb
| .error _ => .error .v1
| .b2a =>
match States.init_b spqrPolyApi (spqrAuthApi F) authKey with
| .ok st => .ok st.into_pb
| .error _ => .error .v1def spqrV1Assembled
Assembled SPQR V1 SpqrModel (toSpqrModel (spqrDeps F K) (spqrHkdfWith F)).
def spqrV1Assembled (F : ShaCompress) (K : MlKem) : SpqrModel :=
toSpqrModel (spqrDeps F K) (spqrHkdfWith F)25 declarations not shown (lines 181–665): theorems, which Lean checks, or definitions that no headline statement uses
CryptoVerification., _private., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., _private., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., CryptoVerification., _private., CryptoVerification., CryptoVerification., CryptoVerification.
end CryptoVerification.LibsignalSpqrV1