Skip to main content

gent_protocol/
configuration_log.rs

1//! Public ordering metadata. Configuration, node permissions, and encryption
2//! keys belong only inside `ciphertext`, never in this protocol's headers.
3use serde::{Deserialize, Serialize};
4
5pub const MAX_SEQUENCE: u64 = (1 << 53) - 1;
6pub const MAX_CIPHERTEXT_BYTES: usize = 4 * 1024 * 1024;
7pub const MAX_COMMIT_BYTES: usize = MAX_CIPHERTEXT_BYTES * 4 / 3 + 8192;
8
9#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
10#[serde(deny_unknown_fields)]
11pub struct Head {
12    pub sequence: u64,
13    pub digest: [u8; 32],
14    /// An ephemeral admission key, rotated on every commit. Its private part
15    /// is distributed inside encrypted envelopes to authorized nodes only.
16    pub write_key: [u8; 32],
17}
18
19#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
20#[serde(deny_unknown_fields)]
21pub struct Header {
22    pub version: u8,
23    pub cluster: String,
24    pub origin: String,
25    pub operation: String,
26    /// A salted commitment to the requested operation; never a bare hash of
27    /// guessable private configuration. Stable across conflict rebases.
28    pub intent: [u8; 32],
29    pub previous: Head,
30    pub next_write_key: [u8; 32],
31}
32
33#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
34#[serde(deny_unknown_fields)]
35pub struct Commit {
36    pub header: Header,
37    /// Canonical standard base64, bounded before parsing at ingress.
38    pub ciphertext: String,
39    pub signature: Vec<u8>,
40}
41
42#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
43#[serde(deny_unknown_fields)]
44pub struct Receipt {
45    pub cluster: String,
46    pub origin: String,
47    pub operation: String,
48    pub intent: [u8; 32],
49    pub head: Head,
50}
51
52/// Provisioned once through the account control plane. A service bootstrap is
53/// not endpoint trust: clients independently pin and verify the encrypted
54/// migration from their existing authority checkpoint.
55#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
56#[serde(deny_unknown_fields)]
57pub struct Bootstrap {
58    pub cluster: String,
59    pub head: Head,
60}
61
62pub fn valid_id(value: &str) -> bool {
63    uuid::Uuid::parse_str(value).is_ok_and(|id| !id.is_nil() && id.to_string() == value)
64}
65
66impl Head {
67    pub fn valid(&self) -> bool {
68        self.sequence <= MAX_SEQUENCE && self.digest != [0; 32] && self.write_key != [0; 32]
69    }
70}
71
72impl Header {
73    pub fn valid(&self) -> bool {
74        self.version == 1
75            && valid_id(&self.cluster)
76            && valid_id(&self.origin)
77            && valid_id(&self.operation)
78            && self.intent != [0; 32]
79            && self.previous.valid()
80            && self.previous.sequence < MAX_SEQUENCE
81            && self.next_write_key != [0; 32]
82            && self.next_write_key != self.previous.write_key
83    }
84}