Skip to content

eBPF-based security platform

proofs: prove what attest verify replays from the evidence's eventlog

Daniel Borkmannc0535e3678d1
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index 30b0fb6d..41739aa4 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -841,6 +841,17 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- ``record_word()``
- Up to 16 characters
- Passes exactly ``[a-z_]``
+ * - ``attest-replay-model``
+ - ``on_record()``
+ - Eventlogs of 1 to 3 records: an init of any reset count, the load
+ of a BPF upatch, the unload of a Java one, a tree operation or
+ another type, each with or without a record word, a digest, fields
+ that decode and a valid name; hex decoding and event digests as
+ stubs, the record types and their tags as copies
+ - Refuses exactly where ``step`` of ``Shirudo.AttestReplay`` does and
+ keeps the records of the last init, their digests, which are
+ derived, ``inits_epoch`` and the last init's reset count and digest
+ as it does; a record without a digest is refused
* - ``attest-pcr``
- ``on_pcr()`` of the evidence parser
- Every index and 64-byte value text
@@ -1150,6 +1161,23 @@ the model follows the C.
left out
- Review; the ``eventlog`` unit suite reads a line whose set entry
names no upatch
+ * - ``Shirudo.AttestReplay``, ``on_record()``, ``verify_epoch()``,
+ ``verify_replay()``, ``verify_derived()`` and the init check of
+ ``verify_state()`` of :src:`util/attest.c`
+ - ``select_sound``: when every check passes, the certified register
+ value is the fold of the last init record and the records after
+ it, that init carries the expected digest and the certified reset
+ count, and every record after it names an event whose digest is
+ its own; ``verify_history``: whatever extended the register, it
+ was extended by the expected init event and then by exactly the
+ events those records name, in order, or SHA-256 collides on the
+ strings of the two folds; ``passes_witness``: the checks can pass
+ - The register value and its certification as
+ ``verify_register()`` checks them; the expected init event, which
+ ``attest verify`` computes when given ``--cert``; SHA-256 as
+ ``Shirudo.Events`` takes it for ``verify_history``
+ - CBMC, ``attest-replay-model``, for ``on_record()`` up to three
+ records; the checks after it by review
* - ``Shirudo.SemVer``, ``bpm_semver_compare()`` of
:src:`util/bpm_package.c`
- ``compareStrings_refl``, ``compareStrings_swap``,
@@ -1492,8 +1520,10 @@ unit cases. Of :src:`util/sysinfo.c` the
proofs cover the image identity's parsers; reading the files they parse,
and the readers of ``/proc`` and the root file system, go to the kernel
and have none. Of :src:`util/attest.c` the proofs cover the functions
-the table names. ``attest_verify()``, ``verify_document()`` and the
-steps it runs a document through, ``evidence_parse()`` to
+the table names, and ``Shirudo.AttestReplay`` models how
+``attest_verify()`` selects and replays the eventlog's records. The
+other steps of ``verify_document()``, ``evidence_parse()`` to
+``verify_register()``, ``verify_anchor()`` and
``verify_expected_pcrs()``, ``check_signed()`` and
``pcr_values_digest()``, which the target checks its quote with too,
have none; the ``attest`` fuzz target runs them.
diff --git a/testing/proofs/cbmc/attest-replay-model.c b/testing/proofs/cbmc/attest-replay-model.c
new file mode 100644
index 00000000..23b0fb02
--- /dev/null
+++ b/testing/proofs/cbmc/attest-replay-model.c
@@ -0,0 +1,289 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ *
+ * on_record() of util/attest.c against step of the Lean model
+ * Shirudo.AttestReplay, on every eventlog of up to LEN records: an init
+ * record of any reset count, the load of a BPF upatch, the unload of a
+ * Java one, a tree operation or a type the verifier does not read, each
+ * with or without a word for its type, a digest, fields that decode and a
+ * name upatch_name_valid() takes. After each record, on_record() refuses
+ * exactly where step does, and keeps the records of the last init, their
+ * digests, which of them are derived, inits_epoch and the last init's
+ * reset count and digest as step does. Digests and decoded fields are one
+ * byte each; hex_decode() and the event digests are stubs, as util/hex.c
+ * and util/chain.c have proofs of their own, and the record types and
+ * their tags copies of those of util/eventlog.c and util/chain.c.
+ */
+
+#include "proof.h"
+/* on_record() keeps a missing reset count of -1 as a __u32 on purpose. */
+#pragma CPROVER check push
+#pragma CPROVER check disable "conversion"
+#include "../../../util/attest.c"
+#pragma CPROVER check pop
+
+/*
+ * The type into its record, at fixed indices: CBMC 5.95 places a write at
+ * an index the data chooses wrongly in an object this large. The labels,
+ * which only the report reads, keep their first 15 bytes.
+ */
+ssize_t strscpy(char *dst, const char *src, size_t size)
+{
+ bool end = false;
+ size_t i;
+
+ for (i = 0; i < 16 && i < size; i++) {
+ char c = end || i + 1 == size || i == 15 ? '\0' : src[i];
+
+ dst[i] = c;
+ end = end || !c;
+ }
+ return 0;
+}
+
+int strsprintf(char *buf, size_t size, const char *fmt, ...)
+{
+ buf[0] = '\0';
+ return 0;
+}
+
+/* A decoded value is its first byte, repeated; '!' and "" do not decode. */
+int hex_decode(const char *hex, __u8 *out, size_t n)
+{
+ if (!hex[0] || hex[0] == '!')
+ return -EINVAL;
+ if (n == 32)
+ memset(out, hex[0], 32);
+ return 0;
+}
+
+int sha256_digest_decode(const char *digest, __u8 out[32])
+{
+ return hex_decode(digest + 7, out, 32);
+}
+
+bool upatch_name_valid(const char *name)
+{
+ return name[0] && name[0] != '!';
+}
+
+bool eventlog_upatch_record_parse(const char *record, __u32 *type,
+ bool *load)
+{
+ static const struct {
+ char record[16];
+ __u32 type;
+ bool load;
+ } records[] = {
+ { "upatch_load", UPATCH_TYPE_BPF, true },
+ { "upatch_unload", UPATCH_TYPE_BPF, false },
+ { "java_load", UPATCH_TYPE_JAVA, true },
+ { "java_unload", UPATCH_TYPE_JAVA, false },
+ };
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(records); i++) {
+ if (!strcmp(records[i].record, record)) {
+ *type = records[i].type;
+ *load = records[i].load;
+ return true;
+ }
+ }
+ return false;
+}
+
+const char *chain_upatch_tag(__u32 type, bool load)
+{
+ switch (type) {
+ case UPATCH_TYPE_BPF:
+ return load ? "shirudo-upatch" : "shirudo-unload";
+ case UPATCH_TYPE_JAVA:
+ return load ? "shirudo-java" : "shirudo-java-unload";
+ default:
+ return NULL;
+ }
+}
+
+/* 0 to 3 for the four upatch tags, 0x80 for any other. */
+static __u8 tag_index(const char *tag)
+{
+ static const char tags[4][20] = {
+ "shirudo-upatch", "shirudo-unload", "shirudo-java",
+ "shirudo-java-unload",
+ };
+ __u8 i;
+
+ for (i = 0; i < 4; i++) {
+ if (!strcmp(tags[i], tag))
+ return i;
+ }
+ return 0x80;
+}
+
+/*
+ * An event's digest: a function of its fields, every byte the same. The
+ * digests 'a' and 'b' differ in their low two bits only, so a record
+ * derives its digest under the right tag and under no other: a BPF load
+ * keeps its payload byte, a Java unload flips both bits.
+ */
+int chain_upatch_event(const char *tag, const char *name,
+ const __u8 payload[32], __u8 event[32])
+{
+ memset(event, (__u8)(payload[0] ^ (name[0] ^ 'n') ^ tag_index(tag)),
+ 32);
+ return 0;
+}
+
+int chain_tree_event(__s64 seq, const __u8 operation[32], __u8 event[32])
+{
+ memset(event, (__u8)(operation[0] ^ ((__u8)seq ^ 1)), 32);
+ return 0;
+}
+
+int chain_key_event(const char *tag, const __u8 cert[32], __u8 event[32])
+{
+ memset(event, (__u8)(cert[0] ^ 0x20), 32);
+ return 0;
+}
+
+enum kind { K_INIT, K_LOAD, K_JAVA_UNLOAD, K_TREE, K_OTHER };
+
+/* step's state, each digest its byte. */
+struct model {
+ __u8 d[LEN];
+ bool derived[LEN];
+ size_t n;
+ __u32 inits, epoch;
+ bool have;
+ __u32 reset;
+ __u8 init_digest;
+};
+
+static void fill(char *s, size_t len, char c)
+{
+ memset(s, c, len);
+ s[len] = '\0';
+}
+
+/* One record into the parser's scratch, as json_parse() writes it. */
+static void any_record(struct parse *p, enum kind k, bool word, char digest,
+ char field, bool fields_ok, bool name_ok, __s64 reset)
+{
+ static const char types[6][16] = {
+ "init", "upatch_load", "java_unload", "tree_op", "zz", "Init",
+ };
+ size_t i;
+
+ for (i = 0; i < sizeof(p->type); i++)
+ p->type[i] = types[word ? k : 5][i];
+ if (digest)
+ fill(p->digest_hex, 64, digest);
+ if (k == K_INIT) {
+ p->rec_pcr = 9;
+ p->rec_phase_pcr = 11;
+ p->rec_reset = reset;
+ fill(p->pcr_before_hex, 64, fields_ok ? 'a' : '!');
+ fill(p->phase_before_hex, 64, 'a');
+ fill(p->nv_name_hex, 2 * TPM_NAME_LEN, 'a');
+ fill(p->anchor_digest_hex, 64, 'a');
+ fill(p->chain_hex, 64, 'a');
+ fill(p->writer_public_hex, 2, 'a');
+ } else if (k == K_LOAD || k == K_JAVA_UNLOAD) {
+ strscpy(p->upatch_name, name_ok ? "n" : "!",
+ sizeof(p->upatch_name));
+ fill(p->upatch_payload_hex, 64, field);
+ } else if (k == K_TREE) {
+ p->tree_seq = 1;
+ memcpy(p->tree_digest_hex, "sha256:", 7);
+ fill(p->tree_digest_hex + 7, 64, field);
+ }
+}
+
+/* step of Shirudo.AttestReplay, refusing as it does. */
+static bool model_step(struct model *m, enum kind k, bool word, char digest,
+ char field, bool fields_ok, bool name_ok, __s64 reset)
+{
+ __u8 event = 0;
+ bool have;
+
+ if (!word || !digest)
+ return false;
+ if (k == K_INIT) {
+ if (!fields_ok || reset > UINT32_MAX)
+ return false;
+ have = reset >= 0;
+ m->inits++;
+ m->epoch = m->inits == 1 ||
+ (have && m->have &&
+ (__u32)(reset & 0xffffffff) != m->reset) ?
+ 1 : m->epoch + 1;
+ m->have = have;
+ m->reset = (__u32)(reset & 0xffffffff);
+ m->init_digest = (__u8)digest;
+ m->d[0] = (__u8)digest;
+ m->derived[0] = false;
+ m->n = 1;
+ return true;
+ }
+ if ((k == K_LOAD || k == K_JAVA_UNLOAD) && !name_ok)
+ return false;
+ if (k == K_LOAD || k == K_TREE)
+ event = (__u8)field;
+ else if (k == K_JAVA_UNLOAD)
+ event = (__u8)(field ^ 3);
+ m->d[m->n] = (__u8)digest;
+ m->derived[m->n] = k != K_OTHER && event == (__u8)digest;
+ m->n++;
+ return true;
+}
+
+static struct parse parse;
+
+void harness(void)
+{
+ struct record recs[LEN];
+ struct parse *p = &parse;
+ struct model m = {};
+ size_t i, j;
+
+ p->recs = recs;
+ p->rec_pcr = -1;
+ p->rec_phase_pcr = -1;
+ p->rec_reset = -1;
+ for (i = 0; i < LEN; i++) {
+ enum kind k;
+ bool word, fields_ok, name_ok, took, model;
+ char digest, field;
+ __s64 reset;
+ int ret;
+
+ __CPROVER_assume(k >= K_INIT && k <= K_OTHER);
+ __CPROVER_assume(!digest || digest == 'a' || digest == 'b');
+ __CPROVER_assume(field == 'a' || field == 'b');
+ __CPROVER_assume((reset >= -1 && reset <= 2) ||
+ reset == (__s64)UINT32_MAX + 1);
+ any_record(p, k, word, digest, field, fields_ok, name_ok, reset);
+ ret = on_record(p);
+ took = !ret;
+ model = model_step(&m, k, word, digest, field, fields_ok,
+ name_ok, reset);
+ __CPROVER_assert(took == model, "refuses exactly where step does");
+ if (!took)
+ return;
+ __CPROVER_assert(p->n_recs == m.n, "this register's records");
+ for (j = 0; j < LEN && j < m.n; j++) {
+ __CPROVER_assert(p->recs[j].digest[0] == m.d[j] &&
+ p->recs[j].digest[31] == m.d[j],
+ "each with its own digest");
+ __CPROVER_assert(p->recs[j].derived == m.derived[j],
+ "derived as step says");
+ }
+ __CPROVER_assert(p->inits_epoch == m.epoch, "inits_epoch");
+ __CPROVER_assert(p->init.have_reset_count == m.have &&
+ (!m.have || p->init.reset_count == m.reset),
+ "the last init's reset count");
+ __CPROVER_assert(!m.inits || p->init.digest[0] == m.init_digest,
+ "the last init's digest");
+ }
+}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 1bf28e33..29ab4cf7 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -42,6 +42,7 @@ tpm-public --slice-formula --unwind 1 LEN=0-1
tpm-digest --unwind 2 --object-bits 12
tpm-kdf --unwind 3 --unwindset strlen.0:68 --object-bits 12 -DLABEL_MAX=66 -DOUT_MAX=64
attest-fields --unwind 18 LEN=0-16
+attest-replay-model --slice-formula --unwind 20 --unwindset memneq_explicit.0:33,strlen.0:70 LEN=1-3
attest-pcr --unwind 1 --unwindset harness.0:65,hex_decode.0:33,strlen.0:66 --no-array-field-sensitivity
eventlog-records --unwind 34 LEN=0-32
config-image --unwind 6 --unwindset memcmp.0:89,harness.0:33 LEN=0,511,512,4096
diff --git a/testing/proofs/lean4/Shirudo.lean b/testing/proofs/lean4/Shirudo.lean
index f65ef115..21705884 100644
--- a/testing/proofs/lean4/Shirudo.lean
+++ b/testing/proofs/lean4/Shirudo.lean
@@ -15,3 +15,4 @@ import Shirudo.UpatchLoad
import Shirudo.TreeSync
import Shirudo.TreeOverlap
import Shirudo.EventlogRecord
+import Shirudo.AttestReplay
diff --git a/testing/proofs/lean4/Shirudo/AttestReplay.lean b/testing/proofs/lean4/Shirudo/AttestReplay.lean
new file mode 100644
index 00000000..8bdc61e2
--- /dev/null
+++ b/testing/proofs/lean4/Shirudo/AttestReplay.lean
@@ -0,0 +1,357 @@
+-- SPDX-License-Identifier: BSD-2-Clause
+-- Copyright (c) 2026 Authors of shirudo
+
+import Shirudo.Events
+
+/-!
+How `attest verify` chooses the records it replays, in util/attest.c.
+`on_record()` reads the evidence's eventlog record by record: it refuses
+a type that is no record word, a digest that does not decode and, past
+`RECORDS_MAX` records of the current register, any record; an init record
+whose other fields do not decode or whose reset count passes a `__u32`;
+and a load or an unload of a name `upatch_name_valid()` refuses. An init
+record starts the register over: the records before it are dropped, it
+becomes the first, and it counts towards `inits_epoch`, which starts at 1
+on the first init record and on one whose reset count differs from the
+last one's, both given, and grows otherwise. Every other record follows;
+it is derived when its fields decode to an event whose digest is the
+record's. `verify_epoch()`, `verify_replay()`, `verify_derived()` and the
+init check of `verify_state()` then ask for one init record of this TPM
+reset, of the certified reset count, the records folding to the certified
+register value, every record after the init derived, and the init's digest
+the one the operator expects.
+
+A record is what `on_record()` takes from one element: whether its type
+is a record word, its digest if it decodes, and its kind with the fields
+`on_record()` reads. An event is `Shirudo.Events.Event`, so the digest a
+record derives is `Event.digest`. The register value, reset count and
+expected init digest stand for what `verify_register()` and the caller
+give; that the register's certification checks is the report's other
+lines.
+
+`select_sound`: when every check passes, the certified value is the fold
+of the last init record and the records after it, the init's digest is
+the expected one and its reset count the certified one, and every record
+after it carries an event whose digest is its own. `verify_history` puts
+it together with `Events.verify_sound`: whatever extended the register,
+it was extended by the expected init event and then by exactly the
+events the records after the last init name, in order, unless SHA-256
+collides on the strings the two folds hash.
+-/
+
+namespace Shirudo.AttestReplay
+open Shirudo Events
+
+/-- `RECORDS_MAX`. -/
+def recordsMax : Nat := 4096
+
+/-- What `on_record()` reads of a record beyond its type and digest. -/
+inductive Kind where
+ /-- An init record: its `tpm_reset_count`, negative when it has none,
+ and whether its other fields decode. -/
+ | init (reset : Int) (fieldsOk : Bool)
+ /-- A load or an unload: whether `upatch_name_valid()` takes its name,
+ and the event its fields name if they decode. -/
+ | upatch (nameOk : Bool) (event : Option Event)
+ /-- A tree operation, a key revocation or a type `on_record()` does not
+ read: the event its fields name if they decode, none otherwise. -/
+ | other (event : Option Event)
+
+/-- One element of the evidence's eventlog. -/
+structure Rec where
+ word : Bool
+ digest : Option Digest
+ kind : Kind
+
+/-- `(__u32)` of an `__s64` the parser read. -/
+def u32 (x : Int) : Nat := (x % 4294967296).toNat
+
+/-- What `on_record()` keeps: this register's records with whether each
+is derived, the init records counted, `inits_epoch`, and the last init's
+reset count and digest. -/
+structure St where
+ recs : List (Digest × Bool)
+ inits : Nat
+ epoch : Nat
+ haveReset : Bool
+ reset : Nat
+ initDigest : Digest
+
+def St.empty : St :=
+ { recs := [], inits := 0, epoch := 0, haveReset := false, reset := 0,
+ initDigest := Digest.zero }
+
+variable (sha : List UInt8 → Digest)
+
+/-- A record is derived when its fields name an event whose digest is
+the record's. -/
+def derivedOf (ev : Option Event) (d : Digest) : Bool :=
+ match ev with
+ | some e => (Event.digest sha e).bytes == d.bytes
+ | none => false
+
+/-- `on_record()`. -/
+def step (st : St) (r : Rec) : Option St :=
+ if recordsMax ≤ st.recs.length then none
+ else if !r.word then none
+ else
+ match r.digest with
+ | none => none
+ | some d =>
+ match r.kind with
+ | .init reset ok =>
+ if !ok || decide (4294967295 < reset) then none
+ else
+ some { recs := [(d, false)]
+ inits := st.inits + 1
+ epoch :=
+ if st.inits + 1 = 1 ∨ (0 ≤ reset ∧ st.haveReset = true ∧
+ u32 reset ≠ st.reset) then 1 else st.epoch + 1
+ haveReset := decide (0 ≤ reset)
+ reset := u32 reset
+ initDigest := d }
+ | .upatch nameOk ev =>
+ if !nameOk then none
+ else some { st with recs := st.recs ++ [(d, derivedOf sha ev d)] }
+ | .other ev =>
+ some { st with recs := st.recs ++ [(d, derivedOf sha ev d)] }
+
+/-- The eventlog read whole, from the zeroed state `attest_verify()`
+allocates. -/
+def parse (l : List Rec) : Option St := l.foldlM (step sha) St.empty
+
+/-- `verify_epoch()`, `verify_replay()`, `verify_derived()` and the init
+check of `verify_state()` pass, for the certified register value `value`
+and reset count `reset` and the expected init digest `init`. -/
+structure Passes (st : St) (value : Digest) (reset : Nat) (init : Digest) :
+ Prop where
+ epoch : st.epoch = 1
+ resetCount : st.haveReset = true ∧ st.reset = reset
+ replay : (st.recs.map Prod.fst).foldl (fold sha) Digest.zero = value
+ derived : ∀ x ∈ st.recs.tail, x.2 = true
+ initDigest : st.initDigest = init
+
+/-! ### The records after the last init -/
+
+def IsInit (r : Rec) : Prop := ∃ reset ok, r.kind = .init reset ok
+
+/-- The event a record's fields name. -/
+def eventOf (r : Rec) : Option Event :=
+ match r.kind with
+ | .init .. => none
+ | .upatch _ ev => ev
+ | .other ev => ev
+
+/-- What `on_record()` appends for a record after the init. -/
+def entryOf (r : Rec) : Digest × Bool :=
+ match r.digest with
+ | some d => (d, derivedOf sha (eventOf r) d)
+ | none => (Digest.zero, false)
+
+/-- What the state says of the records `done` read so far: none an init
+record yet, or the last init and the records after it, each with a
+digest. -/
+def Inv (done : List Rec) (st : St) : Prop :=
+ (st.inits = 0 ∧ st.epoch = 0 ∧ ∀ r ∈ done, ¬ IsInit r) ∨
+ (∃ pre i post reset ok d, done = pre ++ i :: post ∧
+ i.kind = .init reset ok ∧ i.digest = some d ∧ 0 < st.inits ∧
+ (∀ r ∈ post, ¬ IsInit r ∧ r.digest.isSome) ∧
+ st.recs = (d, false) :: post.map (entryOf sha) ∧ st.initDigest = d ∧
+ st.haveReset = decide (0 ≤ reset) ∧ st.reset = u32 reset)
+
+theorem inv_append {done : List Rec} {st : St} {r : Rec} {d : Digest}
+ {ev : Option Event} (hi : Inv sha done st) (hd : r.digest = some d)
+ (hev : eventOf r = ev) (hni : ¬ IsInit r) :
+ Inv sha (done ++ [r])
+ { st with recs := st.recs ++ [(d, derivedOf sha ev d)] } := by
+ have hent : entryOf sha r = (d, derivedOf sha ev d) := by
+ simp [entryOf, hd, hev]
+ rcases hi with ⟨h0, h1, hall⟩ | ⟨pre, i, post, rs, ok, di, hdone, hik, hid,
+ hpos, hpost, hrecs, hdig, hhave, hres⟩
+ · refine Or.inl ⟨h0, h1, ?_⟩
+ intro x hx
+ rcases List.mem_append.1 hx with hx | hx
+ · exact hall x hx
+ · simp only [List.mem_singleton] at hx
+ subst hx
+ exact hni
+ · refine Or.inr ⟨pre, i, post ++ [r], rs, ok, di, by rw [hdone]; simp,
+ hik, hid, hpos, ?_, ?_, hdig, hhave, hres⟩
+ · intro x hx
+ rcases List.mem_append.1 hx with hx | hx
+ · exact hpost x hx
+ · simp only [List.mem_singleton] at hx
+ subst hx
+ exact ⟨hni, by simp [hd]⟩
+ · simp [hrecs, hent]
+
+theorem step_inv {done : List Rec} {st st' : St} {r : Rec}
+ (hi : Inv sha done st) (h : step sha st r = some st') :
+ Inv sha (done ++ [r]) st' := by
+ unfold step at h
+ by_cases hmax : recordsMax ≤ st.recs.length
+ · simp [hmax] at h
+ cases hw : r.word
+ · simp [hmax, hw] at h
+ cases hd : r.digest with
+ | none => simp [hmax, hw, hd] at h
+ | some d =>
+ cases hk : r.kind with
+ | init reset ok =>
+ by_cases hbad : (!ok || decide (4294967295 < reset)) = true
+ · simp [hmax, hw, hd, hk, hbad] at h
+ have hbad' : (!ok || decide (4294967295 < reset)) = false := by
+ simpa using hbad
+ simp only [hmax, hw, hd, hk, hbad', Bool.not_true, Bool.false_eq_true,
+ ite_false, Option.some.injEq] at h
+ subst h
+ exact Or.inr ⟨done, r, [], reset, ok, d, rfl, hk, hd, Nat.succ_pos _,
+ (fun _ h => by cases h), rfl, rfl, rfl, rfl⟩
+ | upatch nameOk ev =>
+ cases hn : nameOk
+ · simp [hmax, hw, hd, hk, hn] at h
+ simp only [hmax, hw, hd, hk, hn, Bool.not_true, Bool.false_eq_true,
+ ite_false, Option.some.injEq] at h
+ subst h
+ exact inv_append sha hi hd (by simp [eventOf, hk])
+ (fun ⟨_, _, h⟩ => by rw [hk] at h; cases h)
+ | other ev =>
+ simp only [hmax, hw, hd, hk, Bool.not_true, Bool.false_eq_true,
+ ite_false, Option.some.injEq] at h
+ subst h
+ exact inv_append sha hi hd (by simp [eventOf, hk])
+ (fun ⟨_, _, h⟩ => by rw [hk] at h; cases h)
+
+theorem parse_inv : ∀ (l done : List Rec) (st st' : St), Inv sha done st →
+ l.foldlM (step sha) st = some st' → Inv sha (done ++ l) st'
+ | [], done, st, st', hi, h => by
+ simp only [List.foldlM_nil, pure, Option.some.injEq] at h
+ subst h
+ simpa using hi
+ | r :: l, done, st, st', hi, h => by
+ simp only [List.foldlM_cons] at h
+ cases hs : step sha st r with
+ | none => simp [hs] at h
+ | some st1 =>
+ simp only [hs, Option.bind_eq_bind, Option.bind_some] at h
+ have := parse_inv l (done ++ [r]) st1 st' (step_inv sha hi hs) h
+ simpa using this
+
+/-- The records after the init, each derived: the events they name. -/
+theorem post_events : ∀ (post : List Rec),
+ (∀ r ∈ post, r.digest.isSome = true) →
+ (∀ x ∈ post.map (entryOf sha), x.2 = true) →
+ ∃ evs : List Event, post.map eventOf = evs.map some ∧
+ (post.map (entryOf sha)).map Prod.fst = evs.map (Event.digest sha) ∧
+ post.map Rec.digest = evs.map fun e => some (Event.digest sha e)
+ | [], _, _ => ⟨[], rfl, rfl, rfl⟩
+ | r :: post, hsome, hder => by
+ obtain ⟨evs, h1, h2, h3⟩ := post_events post
+ (fun x hx => hsome x (List.mem_cons_of_mem r hx))
+ (fun x hx => hder x (List.mem_cons_of_mem _ hx))
+ cases hd : r.digest with
+ | none => have := hsome r List.mem_cons_self; simp [hd] at this
+ | some d =>
+ have hr := hder (entryOf sha r) List.mem_cons_self
+ cases he : eventOf r with
+ | none => simp [entryOf, hd, he, derivedOf] at hr
+ | some e =>
+ have hb : (Event.digest sha e).bytes = d.bytes := by
+ simpa [entryOf, hd, he, derivedOf] using hr
+ have hed : Event.digest sha e = d := Digest.ext_bytes hb
+ refine ⟨e :: evs, by simp [he, h1], ?_, ?_⟩
+ · simp [entryOf, hd, he, hed, h2]
+ · simp [hd, hed, h3]
+
+/-- The facts the checks leave about the records `l` the state was read
+from. -/
+theorem select_core {l : List Rec} {st : St} (h : parse sha l = some st)
+ {value : Digest} {reset : Nat} {init : Digest}
+ (hp : Passes sha st value reset init) :
+ ∃ (pre : List Rec) (i : Rec) (post : List Rec) (rs : Int) (ok : Bool)
+ (evs : List Event), l = pre ++ i :: post ∧ i.kind = .init rs ok ∧
+ i.digest = some init ∧ 0 ≤ rs ∧ u32 rs = reset ∧
+ (∀ r ∈ post, ¬ IsInit r) ∧ post.map eventOf = evs.map some ∧
+ post.map Rec.digest = evs.map (fun e => some (Event.digest sha e)) ∧
+ st.recs.map Prod.fst = init :: evs.map (Event.digest sha) := by
+ have hi := parse_inv sha l [] St.empty st
+ (Or.inl ⟨rfl, rfl, fun _ h => by cases h⟩) h
+ rcases hi with ⟨-, h0, -⟩ | ⟨pre, i, post, rs, ok, d, hdone, hik, hid, -,
+ hpost,
+ hrecs, hdig, hhave, hres⟩
+ · rw [hp.epoch] at h0
+ cases h0
+ · have hdi : d = init := hdig.symm.trans hp.initDigest
+ subst hdi
+ have hder : ∀ x ∈ post.map (entryOf sha), x.2 = true := by
+ intro x hx
+ exact hp.derived x (by rw [hrecs]; exact hx)
+ obtain ⟨evs, h1, h2, h3⟩ := post_events sha post
+ (fun r hr => (hpost r hr).2) hder
+ have hpos : 0 ≤ rs := by
+ have := hp.resetCount.1
+ rw [hhave] at this
+ simpa using this
+ refine ⟨pre, i, post, rs, ok, evs, by simpa using hdone, hik, hid, hpos,
+ hres ▸ hp.resetCount.2, fun r hr => (hpost r hr).1, h1, h3, ?_⟩
+ rw [hrecs]
+ simp [h2]
+
+/-- When every check passes, the certified register value is the fold of
+the last init record and the records after it, that init carries the
+expected digest and the certified reset count, and every record after it
+names an event whose digest is its own. -/
+theorem select_sound {l : List Rec} {st : St} (h : parse sha l = some st)
+ {value : Digest} {reset : Nat} {init : Digest}
+ (hp : Passes sha st value reset init) :
+ ∃ (pre : List Rec) (i : Rec) (post : List Rec) (rs : Int) (ok : Bool)
+ (evs : List Event), l = pre ++ i :: post ∧ i.kind = .init rs ok ∧
+ i.digest = some init ∧ 0 ≤ rs ∧ u32 rs = reset ∧
+ (∀ r ∈ post, ¬ IsInit r) ∧ post.map eventOf = evs.map some ∧
+ post.map Rec.digest = evs.map (fun e => some (Event.digest sha e)) ∧
+ (init :: evs.map (Event.digest sha)).foldl (fold sha) Digest.zero =
+ value := by
+ obtain ⟨pre, i, post, rs, ok, evs, h1, h2, h3, h4, h5, h6, h7, h8, h9⟩ :=
+ select_core sha h hp
+ exact ⟨pre, i, post, rs, ok, evs, h1, h2, h3, h4, h5, h6, h7, h8,
+ h9 ▸ hp.replay⟩
+
+/-- What `attest verify` concludes of the target's history. The operator
+expects the init event `ei`. Whatever extended the register, with
+whatever values `hs`, it was extended by `ei` and then by exactly the
+events the records after the last init name, in order, unless SHA-256
+collides on the 64-byte strings of the two folds or maps one of them to
+the zero register. -/
+theorem verify_history {l : List Rec} {st : St} (h : parse sha l = some st)
+ {value : Digest} {reset : Nat} (ei : Event)
+ (hp : Passes sha st value reset (Event.digest sha ei)) (hs : List Digest)
+ (hcert : hs.foldl (fold sha) Digest.zero = value)
+ (hsha : ∀ m ∈ foldInputs sha (st.recs.map Prod.fst) ++ foldInputs sha hs,
+ ∀ m' ∈ foldInputs sha (st.recs.map Prod.fst) ++ foldInputs sha hs,
+ sha m = sha m' → m = m')
+ (hzero : ∀ m ∈ foldInputs sha (st.recs.map Prod.fst) ++ foldInputs sha hs,
+ sha m ≠ Digest.zero) :
+ ∃ (pre : List Rec) (i : Rec) (post : List Rec) (evs : List Event),
+ l = pre ++ i :: post ∧ IsInit i ∧
+ (∀ r ∈ post, ¬ IsInit r) ∧ post.map eventOf = evs.map some ∧
+ hs = (ei :: evs).map (Event.digest sha) := by
+ obtain ⟨pre, i, post, rs, ok, evs, h1, h2, -, -, -, h6, h7, -, h9⟩ :=
+ select_core sha h hp
+ refine ⟨pre, i, post, evs, h1, ⟨rs, ok, h2⟩, h6, h7, ?_⟩
+ exact verify_sound sha (ei :: evs) (st.recs.map Prod.fst) hs value
+ (by simpa using h9) hp.replay hcert hsha hzero
+
+/-- The checks can pass: an init record of reset count 0 and a record
+that names its event, on any hash. -/
+theorem passes_witness (e : Event) (d : Digest) :
+ ∃ st, parse sha [⟨true, some d, .init 0 true⟩,
+ ⟨true, some (Event.digest sha e), .other (some e)⟩] = some st ∧
+ Passes sha st ((d :: [Event.digest sha e]).foldl (fold sha) Digest.zero)
+ 0 d := by
+ refine ⟨_, rfl, ⟨rfl, ⟨rfl, rfl⟩, rfl, ?_, rfl⟩⟩
+ intro x hx
+ simp only [List.singleton_append, List.tail_cons, List.mem_singleton] at hx
+ subst hx
+ simp [derivedOf]
+
+end Shirudo.AttestReplay