Skip to main content

rings_node/onion/sphinx/
class.rs

1//! Loop classes `b` and the carry width `C_b` (#834 D6, L3).
2//!
3//! A loop's class is one of six cell lengths (#834 D6: `16 KiB … 12 MiB`), and a cell of the loop
4//! is exactly `b` bytes: the class is the cell's length, visible to every hop, and the only size a
5//! hop can observe.
6//!
7//! ```text
8//! š”…   = {16 KiB, 64 KiB, 256 KiB, 1 MiB, 4 MiB, 12 MiB}   OnionLoopClass
9//! u(b) = b / 16 KiB         admission units              u(16 KiB) = 1, u(12 MiB) = 768
10//! C_b = b āˆ’ |χ| āˆ’ F          carry slot                     F = 0: a cell's class is its length
11//! Cā‚€  = C_b āˆ’ Ļ„              padded value width             Ļ„ = 16
12//! ```
13//!
14//! Law (L5′): `C_b` is a function of `b` alone, never of the loop length `H` or a position `i`.
15//! `C_b ≄ 32` for every class, which keeps every AEZ call on the AEZ-core path.
16
17use std::num::NonZeroU32;
18
19use serde::Deserialize;
20use serde::Serialize;
21
22use super::carry::ONION_CARRY_AUTHENTICATOR_BYTES;
23use super::header::ONION_HEADER_BYTES;
24
25/// Cell framing `F` beyond `χ ‖ y`: none, since a cell's class is its length.
26pub(crate) const ONION_CELL_FRAMING_BYTES: usize = 0;
27
28/// The class `b` of one loop, `16 KiB … 12 MiB` (#834 D6, L3): every cell of the loop, replies
29/// included, is exactly `b` bytes.
30///
31/// A session's caller chooses `b`, trading per-cell payload capacity against leakage and
32/// admission cost:
33///
34/// - **Leakage (#834 leakage table).** A cell's class is its byte length on the wire (`F = 0`), so
35///   every hop along the loop sees `b`, while everything inside the cell is hidden. A small class
36///   set bounds padding overhead without exposing a byte-accurate traffic fingerprint.
37/// - **Admission cost (#834 L9).** Every cell of class `b` costs its receiver
38///   `u(b) = b / 16 KiB` admission units ([`Self::units`], `1` unit at `16 KiB` up to `768` units
39///   at `12 MiB`) and occupies `u(b)` emission slots on a constant-rate link.
40///
41/// The `repr(u8)` discriminant is the one-byte label the header MAC binds.
42#[derive(
43    Clone, Copy, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
44)]
45#[repr(u8)]
46pub enum OnionLoopClass {
47    /// Sixteen KiB, the default and least class: one admission unit (`u = 1`).
48    #[default]
49    #[serde(alias = "16KiB", alias = "16 KiB", alias = "16kib")]
50    KiB16,
51    /// Sixty-four KiB: four admission units (`u = 4`).
52    #[serde(alias = "64KiB", alias = "64 KiB", alias = "64kib")]
53    KiB64,
54    /// 256 KiB: sixteen admission units (`u = 16`).
55    #[serde(alias = "256KiB", alias = "256 KiB", alias = "256kib")]
56    KiB256,
57    /// One MiB: 64 admission units (`u = 64`).
58    #[serde(alias = "1MiB", alias = "1 MiB", alias = "1mib")]
59    MiB1,
60    /// Four MiB: 256 admission units (`u = 256`).
61    #[serde(alias = "4MiB", alias = "4 MiB", alias = "4mib")]
62    MiB4,
63    /// Twelve MiB, the largest class: `u_max = 768` admission units.
64    #[serde(alias = "12MiB", alias = "12 MiB", alias = "12mib")]
65    MiB12,
66}
67
68impl OnionLoopClass {
69    /// The default class, `b = 16 KiB`.
70    pub const DEFAULT: Self = Self::KiB16;
71
72    /// Every class, in increasing size.
73    pub const ALL: [Self; 6] = [
74        Self::KiB16,
75        Self::KiB64,
76        Self::KiB256,
77        Self::MiB1,
78        Self::MiB4,
79        Self::MiB12,
80    ];
81
82    /// `b`, the cell length in bytes.
83    pub const fn cell_bytes(self) -> usize {
84        match self {
85            Self::KiB16 => 16 * 1024,
86            Self::KiB64 => 64 * 1024,
87            Self::KiB256 => 256 * 1024,
88            Self::MiB1 => 1024 * 1024,
89            Self::MiB4 => 4 * 1024 * 1024,
90            Self::MiB12 => 12 * 1024 * 1024,
91        }
92    }
93
94    /// `u(b) = b / 16 KiB`, the admission units one cell of the class costs its receiver (#834
95    /// L9): `1 + (u(b) āˆ’ 1)`, exact since every class is a positive whole number of units that
96    /// fits `u32` (asserted below).
97    pub const fn units(self) -> NonZeroU32 {
98        let units = self.cell_bytes() / Self::DEFAULT.cell_bytes();
99        NonZeroU32::MIN.saturating_add(units.saturating_sub(1) as u32)
100    }
101
102    /// `C_b = b āˆ’ |χ| āˆ’ F`, the carry slot width.
103    pub(crate) const fn carry_bytes(self) -> usize {
104        self.cell_bytes() - ONION_HEADER_BYTES - ONION_CELL_FRAMING_BYTES
105    }
106
107    /// `Cā‚€ = C_b āˆ’ Ļ„`, the width of a padded carry value.
108    pub(crate) const fn carry_value_bytes(self) -> usize {
109        self.carry_bytes() - ONION_CARRY_AUTHENTICATOR_BYTES
110    }
111
112    /// `Cā‚€ āˆ’ 1`, the widest value `pad` admits: `|v| < Cā‚€` (L3).
113    pub(crate) const fn value_capacity(self) -> usize {
114        self.carry_value_bytes() - 1
115    }
116
117    /// `b` as the one-byte string the header MAC binds (#834 H1: `γ = MAC(b ‖ β)`): the
118    /// `repr(u8)` discriminant, injective on classes.
119    pub(crate) const fn mac_label(self) -> [u8; 1] {
120        [self as u8]
121    }
122
123    /// The class whose cells are `length` bytes long, if any: the decoder of `F = 0`.
124    pub(crate) fn from_cell_bytes(length: usize) -> Option<Self> {
125        Self::ALL
126            .into_iter()
127            .find(|class| class.cell_bytes() == length)
128    }
129
130    /// The least class `b` whose padded value slot admits `value_bytes` (`value_bytes < Cā‚€`,
131    /// #834 L3), or `None` above `MiB12`'s capacity.
132    pub fn least_for_value_bytes(value_bytes: usize) -> Option<Self> {
133        Self::ALL
134            .into_iter()
135            .find(|class| value_bytes <= class.value_capacity())
136    }
137}
138
139// `DEFAULT` is the least class, `KiB16`; it leaves an
140// AEZ-core carry slot (`C_b ≄ 32`) and room for the padding marker, and every larger class leaves
141// more.
142const _: () = assert!(OnionLoopClass::DEFAULT.carry_value_bytes() >= 32);
143
144// Every class is a whole number of units, and the largest (12 MiB = 768 units) fits `u32`, so
145// `units` is exact.
146const _: () = assert!(
147    OnionLoopClass::MiB12
148        .cell_bytes()
149        .is_multiple_of(OnionLoopClass::DEFAULT.cell_bytes())
150        && OnionLoopClass::MiB12.cell_bytes() / OnionLoopClass::DEFAULT.cell_bytes()
151            <= u32::MAX as usize
152        && OnionLoopClass::MiB12.units().get() == 768
153);