Skip to main content

zaun_community_protocol/signed/rotation/
chain.rs

1//! Rotation-chain verification (specification §4.6).
2
3use super::{ChainHead, RootRotation, SignedRootRotation};
4use crate::crypto::{self, Context};
5use crate::encoding::{DecodeError, Reader};
6use crate::ids::CommunityId;
7use crate::keys::PublicKey;
8use crate::signed::error::{RotationError, SignedError};
9use crate::signed::object::{Signed, read_format};
10
11/// Bytes before the first key in both `RootRotation` (`format`, `community_id`,
12/// `rotation_seq`) and `CommunityDescriptor` (`format`, `community_id`, `revision`).
13const KEY_OFFSET: usize = 32 + 8;
14
15/// The **claimed** genesis key: the first rotation's `old_root`, or the descriptor's
16/// `root_public_key` when the chain is empty (§4.6).
17///
18/// The key is read from unverified bytes at a fixed offset. It is authenticated only by
19/// [`verify_chain`], which checks that it derives the expected community id, and then by the
20/// signatures it verifies.
21///
22/// # Errors
23/// [`DecodeError`] when the bytes are too short or the key has an unknown suite.
24pub fn claimed_genesis(
25    descriptor_bytes: &[u8],
26    chain: &[SignedRootRotation],
27) -> Result<PublicKey, DecodeError> {
28    // SPEC: §3.6 says to verify before decoding, but the genesis key needed for that is
29    // inside the bytes. Reading it at its fixed offset and binding it by the community-id
30    // hash before any signature check is the narrowest reading that can work.
31    let bytes = chain
32        .first()
33        .map_or(descriptor_bytes, |first| first.bytes.as_slice());
34    let mut r = Reader::new(bytes);
35    read_format(&mut r)?;
36    r.fixed(KEY_OFFSET)?;
37    r.get()
38}
39
40/// Verifies a rotation chain from the genesis key (§4.6): `genesis` must derive
41/// `community_id`; for rotation `i` (1-based, list order) `sig_old` verifies under `Kᵢ₋₁`
42/// **before** decoding, then `community_id`, `rotation_seq = i`, `old_root = Kᵢ₋₁`,
43/// `new_suite ≥` the previous suite and `sig_new` under `new_root` are checked.
44///
45/// # Errors
46/// [`RotationError`] naming the first failing rotation.
47pub fn verify_chain(
48    community_id: &CommunityId,
49    genesis: &PublicKey,
50    chain: &[SignedRootRotation],
51) -> Result<ChainHead, RotationError> {
52    if crypto::community_id(genesis) != *community_id {
53        return Err(RotationError::Genesis);
54    }
55    let mut head = ChainHead::genesis_only(genesis.clone());
56    for (position, link) in chain.iter().enumerate() {
57        let index = u64::try_from(position + 1).map_err(|_| RotationError::TooLong)?;
58        let signed = |error| RotationError::Signed { index, error };
59        // SPEC: §3.3 applies a community's `min_suite` to its signatures, but a historical
60        // rotation signed by a retired lower-suite key must stay verifiable after a suite
61        // upgrade. Rotation signatures therefore use the suite of the key that made them;
62        // `new_suite` never decreasing is the downgrade protection for the chain.
63        let rotation = Signed::<RootRotation>::verify_and_decode(
64            &link.bytes,
65            &link.sig_old,
66            head.key(),
67            head.suite(),
68        )
69        .map_err(signed)?
70        .into_object();
71        if rotation.community_id != *community_id {
72            return Err(RotationError::Community(index));
73        }
74        if rotation.rotation_seq != index {
75            return Err(RotationError::Sequence(index));
76        }
77        if rotation.old_root != *head.key() {
78            return Err(RotationError::OldRoot(index));
79        }
80        if rotation.new_suite < head.suite() {
81            return Err(RotationError::SuiteDowngrade(index));
82        }
83        let ctx = Context::RootRotation;
84        crypto::verify(
85            &rotation.new_root,
86            &link.sig_new,
87            ctx,
88            &link.bytes,
89            rotation.new_suite,
90        )
91        .map_err(|e| signed(SignedError::Signature(e)))?;
92        head.push(rotation.new_root, rotation.new_suite);
93    }
94    Ok(head)
95}