Skip to main content

hyprstream_rpc/auth/
det_cbor.rs

1//! The ONE deterministic-CBOR (RFC 8949) byte encoder for v16 content
2//! thumbprints. A direct port of WS-A's frozen `gen_proof_vectors.py::enc`
3//! (at `af08825528627069b3cfbdd763948c4a24689cb5`), restricted to the value
4//! subset the v16 content thumbprints use.
5//!
6//! Both the mint side (WS-B `signer_suite_thumbprint`) and the verify side
7//! (WS-C signer-suite/cnf resolution AND the replay-namespace preimages, which
8//! reuse the identical `enc` primitive over different array shapes) build their
9//! preimages with THIS encoder — there is exactly one det-CBOR implementation
10//! in the crate, so mint and verify are provably byte-identical.
11
12/// A value in the RFC 8949 deterministic-CBOR subset the v16 content
13/// thumbprints use: unsigned integers, byte strings, text strings, and
14/// (possibly nested) definite-length arrays. This is all A's `enc` needs for
15/// the signer-suite and replay-namespace preimages.
16#[derive(Clone, Debug)]
17pub enum DetCborValue<'a> {
18    /// CBOR major type 0 (unsigned integer) — e.g. an enrollment epoch.
19    Uint(u64),
20    /// CBOR major type 2 (byte string) — a raw public key.
21    Bytes(&'a [u8]),
22    /// CBOR major type 3 (text string) — a suite ID / domain separator.
23    Text(&'a str),
24    /// CBOR major type 4 (definite-length array).
25    Array(Vec<DetCborValue<'a>>),
26}
27
28/// Append the RFC 8949 deterministic head for major type `major` and
29/// length/value `n` (shortest form).
30fn head(out: &mut Vec<u8>, major: u8, n: u64) {
31    let mt = major << 5;
32    if n < 24 {
33        out.push(mt | (n as u8));
34    } else if n < 0x100 {
35        out.push(mt | 24);
36        out.push(n as u8);
37    } else if n < 0x1_0000 {
38        out.push(mt | 25);
39        out.extend_from_slice(&(n as u16).to_be_bytes());
40    } else if n < 0x1_0000_0000 {
41        out.push(mt | 26);
42        out.extend_from_slice(&(n as u32).to_be_bytes());
43    } else {
44        out.push(mt | 27);
45        out.extend_from_slice(&n.to_be_bytes());
46    }
47}
48
49fn append(out: &mut Vec<u8>, value: &DetCborValue) {
50    match value {
51        DetCborValue::Uint(n) => head(out, 0, *n),
52        DetCborValue::Bytes(b) => {
53            head(out, 2, b.len() as u64);
54            out.extend_from_slice(b);
55        }
56        DetCborValue::Text(s) => {
57            let b = s.as_bytes();
58            head(out, 3, b.len() as u64);
59            out.extend_from_slice(b);
60        }
61        DetCborValue::Array(items) => {
62            head(out, 4, items.len() as u64);
63            for item in items {
64                append(out, item);
65            }
66        }
67    }
68}
69
70/// Deterministically CBOR-encode `value` to its canonical byte string.
71#[must_use]
72pub fn det_cbor(value: &DetCborValue) -> Vec<u8> {
73    let mut out = Vec::new();
74    append(&mut out, value);
75    out
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn heads_use_shortest_form() {
84        // 0..23 encode in one byte; 24 spills to a 1-byte length.
85        assert_eq!(det_cbor(&DetCborValue::Uint(0)), vec![0x00]);
86        assert_eq!(det_cbor(&DetCborValue::Uint(23)), vec![0x17]);
87        assert_eq!(det_cbor(&DetCborValue::Uint(24)), vec![0x18, 24]);
88        assert_eq!(det_cbor(&DetCborValue::Uint(255)), vec![0x18, 255]);
89        assert_eq!(det_cbor(&DetCborValue::Uint(256)), vec![0x19, 0x01, 0x00]);
90    }
91
92    #[test]
93    fn text_and_array_shapes() {
94        // ["a", []] → 82 61 61 80
95        let v = DetCborValue::Array(vec![DetCborValue::Text("a"), DetCborValue::Array(vec![])]);
96        assert_eq!(det_cbor(&v), vec![0x82, 0x61, 0x61, 0x80]);
97    }
98}