Chains and archives
Use this notebook to verify chained consumption rows, signed anchors, key transitions, and an archive under caller-supplied expectations.
Download this .livemd file, open it in Livebook, and choose
Open → Import from file, then Run all. Use a supported Elixir and Erlang/OTP toolchain
with access to Hex. No secrets or external services are needed.
Build a deployment evidence chain, anchor its boundaries, authenticate an archive-key transition, and verify an immutable archive object against host-held expectations.
Chain, anchor, and transition facts carry trust: :not_evaluated. An anchored export also
carries authorization: :not_evaluated. The host owns completeness, trusted historical keys,
commitment semantics, archival storage, and audit policy. This notebook keeps bytes in memory
and starts no services or files; it does not contact an object store.
Open the published notebook in Livebook.
Setup
Install the exact published package. Fixed evaluation times keep the results reproducible; only safe semantic values are persisted, never keys, signatures, tokens, or nonces.
Mix.install([{:bounded_authority_protocol, "== 2.0.0"}])
:ok
Encode the consumption chain
Each row commits to a host record and links to its predecessor. Sequence one begins at the all-zero hash; later rows carry the hash of the previous canonical row.
alias BoundedAuthorityProtocol.V1
alias BoundedAuthorityProtocol.V1.{ConsumptionEntry, ChainInput, ExpectedChain}
chain_id = "urn:example:chain:deployments"
genesis = <<0::256>>
# The host's commitment for each accepted invocation, here a digest of a record it keeps:
commitments =
for record <- ["deploy web 1.4.2", "deploy api 2.0.1", "rollback web 1.4.1"],
do: :crypto.hash(:sha256, record)
{rows, head} =
commitments
|> Enum.with_index(1)
|> Enum.map_reduce(genesis, fn {commitment, sequence}, previous ->
entry = %ConsumptionEntry{
chain_id: chain_id,
sequence: sequence,
previous_hash: previous,
commitment: commitment
}
{:ok, encoded} = V1.encode_consumption_entry(entry, %{})
{encoded.bytes, encoded.hash}
end)
length(rows)
3
Verify the expected chain boundary
Use the predecessor and head held by the host, not boundaries chosen from the input. A self-consistent shortened chain alone cannot establish completeness.
expected_chain = %ExpectedChain{
chain_id: chain_id,
first_sequence: 1,
last_sequence: 3,
row_count: 3,
previous_hash: genesis,
last_hash: head,
bounds: %{}
}
{:ok, chain_facts} = V1.check_chain(%ChainInput{rows: rows}, expected_chain)
Map.take(chain_facts, [:row_count, :verification, :trust])
%{row_count: 3, verification: :boundary_consistent, trust: :not_evaluated}
Refuse changed rows and boundaries
Missing, reordered, truncated, or noncanonical rows fail the host-held boundary. The producer also refuses a first row with a nonzero predecessor.
[first, second, third] = rows
{:error, :invalid} = V1.check_chain(%ChainInput{rows: [first, third]}, expected_chain)
{:error, :invalid} = V1.check_chain(%ChainInput{rows: [first, third, second]}, expected_chain)
{:error, :invalid} =
V1.check_chain(%ChainInput{rows: [first, second]}, %{
expected_chain
| last_sequence: 2,
row_count: 2
})
{:error, :invalid} =
V1.check_chain(
%ChainInput{
rows: [String.replace(first, "\"sequence\":1", "\"sequence\":1 "), second, third]
},
expected_chain
)
{:error, :invalid} =
V1.encode_consumption_entry(
%ConsumptionEntry{
chain_id: chain_id,
sequence: 1,
previous_hash: <<1::256>>,
commitment: hd(commitments)
},
%{}
)
[:dropped, :reordered, :truncated, :noncanonical, :wrong_predecessor]
[:dropped, :reordered, :truncated, :noncanonical, :wrong_predecessor]
Prepare historical archive keys
Generate two archive keys and give each a host-owned validity window. Their fingerprints use the raw RFC 7638 thumbprint construction.
alias BoundedAuthorityProtocol.V1.{BoundaryAnchor, ExpectedAnchor, HistoricalPublicKey}
{key_a_public, key_a_private} = :crypto.generate_key(:eddsa, :ed25519)
{key_b_public, key_b_private} = :crypto.generate_key(:eddsa, :ed25519)
{:ok, key_a_fingerprint} = V1.Jwk.public_key_thumbprint_raw(key_a_public, %{})
{:ok, key_b_fingerprint} = V1.Jwk.public_key_thumbprint_raw(key_b_public, %{})
key_a = %HistoricalPublicKey{
key_id: "archive-2026-a",
public_key: key_a_public,
valid_from: 1_799_990_000,
valid_before: 1_800_010_000
}
key_b = %HistoricalPublicKey{
key_id: "archive-2026-b",
public_key: key_b_public,
valid_from: 1_800_005_000,
valid_before: :unbounded
}
sign = fn input, private_key ->
signature = :crypto.sign(:eddsa, :none, input.message, [private_key, :ed25519])
{:ok, compact} = V1.assemble_compact(input, signature)
compact
end
:archive_keys_ready
:archive_keys_ready
Sign the start anchor
Key A signs the chain head before any rows exist: sequence zero and the all-zero hash. The signed timestamp will be checked against key A’s validity window.
{:ok, start_input} =
V1.boundary_anchor_signing_input(
%BoundaryAnchor{
anchor_id: "urn:example:anchor:deployments:start",
anchored_at: 1_800_000_000,
chain_id: chain_id,
sequence: 0,
chain_hash: genesis,
key_id: "archive-2026-a",
public_key: key_a_public
},
%{}
)
start_anchor = sign.(start_input, key_a_private)
:start_anchor_signed
:start_anchor_signed
Verify the start anchor
Supply the anchor identity, time, sequence, head, and key fingerprint from host records. Verification authenticates only this expected statement under the supplied historical key.
expected_start = %ExpectedAnchor{
anchor_id: "urn:example:anchor:deployments:start",
anchored_at: 1_800_000_000,
chain_id: chain_id,
sequence: 0,
chain_hash: genesis,
key_id: "archive-2026-a",
key_fingerprint: key_a_fingerprint,
bounds: %{}
}
{:ok, anchor_facts} = V1.verify_historical_anchor(start_anchor, key_a, expected_start)
Map.take(anchor_facts, [:sequence, :verification, :trust])
%{sequence: 0, verification: :signature_and_window, trust: :not_evaluated}
Refuse the wrong anchor key or key window
A valid anchor signature is insufficient outside the historical key window or under a different archive key.
{:error, :invalid} =
V1.verify_historical_anchor(start_anchor, %{key_a | valid_from: 1_800_000_001}, expected_start)
{:error, :invalid} = V1.verify_historical_anchor(start_anchor, key_b, expected_start)
[:outside_key_window, :other_key]
[:outside_key_window, :other_key]
Sign the archive-key transition
Key A signs the identity and fingerprint of key B. The effective time is inside both key windows; no remote key discovery occurs.
alias BoundedAuthorityProtocol.V1.{KeyTransition, ExpectedKeyTransition}
{:ok, transition_input} =
V1.key_transition_signing_input(
%KeyTransition{
transition_id: "urn:example:transition:deployments:1",
chain_id: chain_id,
effective_at: 1_800_006_000,
current_key_id: "archive-2026-a",
current_public_key: key_a_public,
next_key_id: "archive-2026-b",
next_public_key: key_b_public
},
%{}
)
transition = sign.(transition_input, key_a_private)
:transition_signed
:transition_signed
Verify the transition
The verifier requires the exact current and successor keys and expected transition context. It authenticates a historical link, leaving the root of trust to the host.
expected_transition = %ExpectedKeyTransition{
transition_id: "urn:example:transition:deployments:1",
chain_id: chain_id,
effective_at: 1_800_006_000,
current_key_id: "archive-2026-a",
current_key_fingerprint: key_a_fingerprint,
next_key_id: "archive-2026-b",
next_key_fingerprint: key_b_fingerprint,
bounds: %{}
}
{:ok, transition_facts} =
V1.verify_key_transition(transition, key_a, key_b, expected_transition)
Map.take(transition_facts, [:verification, :trust])
%{verification: :authenticated_transition, trust: :not_evaluated}
Refuse reversed transition keys
The next key cannot stand in for the current signer. Swapping the supplied key history must fail even though both keys are otherwise valid.
{:error, :invalid} = V1.verify_key_transition(transition, key_b, key_a, expected_transition)
{:error, :invalid}
Sign the end anchor
Key B signs sequence three and the chain head after the transition. The host separately records the expected end boundary.
alias BoundedAuthorityProtocol.V1.{AnchoredExportInput, ExpectedExport}
{:ok, end_input} =
V1.boundary_anchor_signing_input(
%BoundaryAnchor{
anchor_id: "urn:example:anchor:deployments:end",
anchored_at: 1_800_007_000,
chain_id: chain_id,
sequence: 3,
chain_hash: head,
key_id: "archive-2026-b",
public_key: key_b_public
},
%{}
)
end_anchor = sign.(end_input, key_b_private)
expected_end = %ExpectedAnchor{
anchor_id: "urn:example:anchor:deployments:end",
anchored_at: 1_800_007_000,
chain_id: chain_id,
sequence: 3,
chain_hash: head,
key_id: "archive-2026-b",
key_fingerprint: key_b_fingerprint,
bounds: %{}
}
:end_anchor_signed
:end_anchor_signed
Frame the anchored export
Encode the rows, both anchors, and the transition against the expected boundaries. The returned chunks form one object with an exact byte count and SHA-256 digest.
{:ok, export} =
V1.encode_anchored_export(
%AnchoredExportInput{
rows: rows,
start_anchor: start_anchor,
transitions: [transition],
end_anchor: end_anchor
},
%ExpectedExport{
chain: expected_chain,
start_anchor: expected_start,
transitions: [expected_transition],
end_anchor: expected_end,
bounds: %{}
}
)
export_bytes = IO.iodata_length(export.chunks)
^export_bytes = export.byte_count
%{chunks: length(export.chunks), digest_bytes: byte_size(export.digest)}
%{chunks: 8, digest_bytes: 32}
Verify the immutable archive object
The object version and digest are out-of-band host records. Verify the complete bytes and ordered key history against them, including exact end of object.
alias BoundedAuthorityProtocol.V1.{ArchivedObject, ExpectedAnchoredExport, HistoricalKeyChain}
archived = %ArchivedObject{chunks: export.chunks, version: "object-version-7"}
key_history = %HistoricalKeyChain{keys: [key_a, key_b]}
expected_archive = %ExpectedAnchoredExport{
chain: expected_chain,
start_anchor: expected_start,
transitions: [expected_transition],
end_anchor: expected_end,
digest: export.digest,
object_version: "object-version-7",
bounds: %{}
}
{:ok, archive_facts} = V1.verify_anchored_export(archived, key_history, expected_archive)
Map.take(archive_facts, [
:row_count,
:transition_count,
:verification,
:trust,
:authorization
])
%{
row_count: 3,
transition_count: 1,
verification: :anchored_export,
trust: :not_evaluated,
authorization: :not_evaluated
}
Verify identical bytes with different chunking
Chunk boundaries are transport details. Recombining the same bytes does not change the archive identity or its verification result.
all_bytes = IO.iodata_to_binary(export.chunks)
{:ok, rechunked_facts} =
V1.verify_anchored_export(%{archived | chunks: [all_bytes]}, key_history, expected_archive)
Map.take(rechunked_facts, [:row_count, :verification, :authorization])
%{row_count: 3, verification: :anchored_export, authorization: :not_evaluated}
Refuse archive identity or history changes
Appending a byte, changing the object version or digest, omitting key history, or dropping the expected transition fails closed. None can be repaired by trusting the archive’s claims.
{:error, :invalid} =
V1.verify_anchored_export(
%{archived | chunks: export.chunks ++ [<<0>>]},
key_history,
expected_archive
)
{:error, :invalid} =
V1.verify_anchored_export(
%{archived | version: "object-version-8"},
key_history,
expected_archive
)
{:error, :invalid} =
V1.verify_anchored_export(archived, key_history, %{expected_archive | digest: <<0::256>>})
{:error, :invalid} =
V1.verify_anchored_export(archived, %HistoricalKeyChain{keys: [key_a]}, expected_archive)
{:error, :invalid} =
V1.encode_anchored_export(
%AnchoredExportInput{
rows: rows,
start_anchor: start_anchor,
transitions: [],
end_anchor: end_anchor
},
%ExpectedExport{
chain: expected_chain,
start_anchor: expected_start,
transitions: [expected_transition],
end_anchor: expected_end,
bounds: %{}
}
)
[:trailing_byte, :object_version, :digest, :key_history, :missing_transition]
[:trailing_byte, :object_version, :digest, :key_history, :missing_transition]
The package checks evidence but does not keep it. The host appends records atomically, chooses when to anchor, stores archives, and records each digest and object version. See evidence and operations for those boundaries.