rings-node 1.0.0-pre.6

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
//! Loop classes `b` and the carry width `C_b` (#834 D6, L3).
//!
//! A loop's class is one of six cell lengths (#834 D6: `16 KiB … 12 MiB`), and a cell of the loop
//! is exactly `b` bytes: the class is the cell's length, visible to every hop, and the only size a
//! hop can observe.
//!
//! ```text
//! š”…   = {16 KiB, 64 KiB, 256 KiB, 1 MiB, 4 MiB, 12 MiB}   OnionLoopClass
//! u(b) = b / 16 KiB         admission units              u(16 KiB) = 1, u(12 MiB) = 768
//! C_b = b āˆ’ |χ| āˆ’ F          carry slot                     F = 0: a cell's class is its length
//! Cā‚€  = C_b āˆ’ Ļ„              padded value width             Ļ„ = 16
//! ```
//!
//! Law (L5′): `C_b` is a function of `b` alone, never of the loop length `H` or a position `i`.
//! `C_b ≄ 32` for every class, which keeps every AEZ call on the AEZ-core path.

use std::num::NonZeroU32;

use serde::Deserialize;
use serde::Serialize;

use super::carry::ONION_CARRY_AUTHENTICATOR_BYTES;
use super::header::ONION_HEADER_BYTES;

/// Cell framing `F` beyond `χ ‖ y`: none, since a cell's class is its length.
pub(crate) const ONION_CELL_FRAMING_BYTES: usize = 0;

/// The class `b` of one loop, `16 KiB … 12 MiB` (#834 D6, L3): every cell of the loop, replies
/// included, is exactly `b` bytes.
///
/// A session's caller chooses `b`, trading per-cell payload capacity against leakage and
/// admission cost:
///
/// - **Leakage (#834 leakage table).** A cell's class is its byte length on the wire (`F = 0`), so
///   every hop along the loop sees `b`, while everything inside the cell is hidden. A small class
///   set bounds padding overhead without exposing a byte-accurate traffic fingerprint.
/// - **Admission cost (#834 L9).** Every cell of class `b` costs its receiver
///   `u(b) = b / 16 KiB` admission units ([`Self::units`], `1` unit at `16 KiB` up to `768` units
///   at `12 MiB`) and occupies `u(b)` emission slots on a constant-rate link.
///
/// The `repr(u8)` discriminant is the one-byte label the header MAC binds.
#[derive(
    Clone, Copy, Debug, Default, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize,
)]
#[repr(u8)]
pub enum OnionLoopClass {
    /// Sixteen KiB, the default and least class: one admission unit (`u = 1`).
    #[default]
    #[serde(alias = "16KiB", alias = "16 KiB", alias = "16kib")]
    KiB16,
    /// Sixty-four KiB: four admission units (`u = 4`).
    #[serde(alias = "64KiB", alias = "64 KiB", alias = "64kib")]
    KiB64,
    /// 256 KiB: sixteen admission units (`u = 16`).
    #[serde(alias = "256KiB", alias = "256 KiB", alias = "256kib")]
    KiB256,
    /// One MiB: 64 admission units (`u = 64`).
    #[serde(alias = "1MiB", alias = "1 MiB", alias = "1mib")]
    MiB1,
    /// Four MiB: 256 admission units (`u = 256`).
    #[serde(alias = "4MiB", alias = "4 MiB", alias = "4mib")]
    MiB4,
    /// Twelve MiB, the largest class: `u_max = 768` admission units.
    #[serde(alias = "12MiB", alias = "12 MiB", alias = "12mib")]
    MiB12,
}

impl OnionLoopClass {
    /// The default class, `b = 16 KiB`.
    pub const DEFAULT: Self = Self::KiB16;

    /// Every class, in increasing size.
    pub const ALL: [Self; 6] = [
        Self::KiB16,
        Self::KiB64,
        Self::KiB256,
        Self::MiB1,
        Self::MiB4,
        Self::MiB12,
    ];

    /// `b`, the cell length in bytes.
    pub const fn cell_bytes(self) -> usize {
        match self {
            Self::KiB16 => 16 * 1024,
            Self::KiB64 => 64 * 1024,
            Self::KiB256 => 256 * 1024,
            Self::MiB1 => 1024 * 1024,
            Self::MiB4 => 4 * 1024 * 1024,
            Self::MiB12 => 12 * 1024 * 1024,
        }
    }

    /// `u(b) = b / 16 KiB`, the admission units one cell of the class costs its receiver (#834
    /// L9): `1 + (u(b) āˆ’ 1)`, exact since every class is a positive whole number of units that
    /// fits `u32` (asserted below).
    pub const fn units(self) -> NonZeroU32 {
        let units = self.cell_bytes() / Self::DEFAULT.cell_bytes();
        NonZeroU32::MIN.saturating_add(units.saturating_sub(1) as u32)
    }

    /// `C_b = b āˆ’ |χ| āˆ’ F`, the carry slot width.
    pub(crate) const fn carry_bytes(self) -> usize {
        self.cell_bytes() - ONION_HEADER_BYTES - ONION_CELL_FRAMING_BYTES
    }

    /// `Cā‚€ = C_b āˆ’ Ļ„`, the width of a padded carry value.
    pub(crate) const fn carry_value_bytes(self) -> usize {
        self.carry_bytes() - ONION_CARRY_AUTHENTICATOR_BYTES
    }

    /// `Cā‚€ āˆ’ 1`, the widest value `pad` admits: `|v| < Cā‚€` (L3).
    pub(crate) const fn value_capacity(self) -> usize {
        self.carry_value_bytes() - 1
    }

    /// `b` as the one-byte string the header MAC binds (#834 H1: `γ = MAC(b ‖ β)`): the
    /// `repr(u8)` discriminant, injective on classes.
    pub(crate) const fn mac_label(self) -> [u8; 1] {
        [self as u8]
    }

    /// The class whose cells are `length` bytes long, if any: the decoder of `F = 0`.
    pub(crate) fn from_cell_bytes(length: usize) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|class| class.cell_bytes() == length)
    }

    /// The least class `b` whose padded value slot admits `value_bytes` (`value_bytes < Cā‚€`,
    /// #834 L3), or `None` above `MiB12`'s capacity.
    pub fn least_for_value_bytes(value_bytes: usize) -> Option<Self> {
        Self::ALL
            .into_iter()
            .find(|class| value_bytes <= class.value_capacity())
    }
}

// `DEFAULT` is the least class, `KiB16`; it leaves an
// AEZ-core carry slot (`C_b ≄ 32`) and room for the padding marker, and every larger class leaves
// more.
const _: () = assert!(OnionLoopClass::DEFAULT.carry_value_bytes() >= 32);

// Every class is a whole number of units, and the largest (12 MiB = 768 units) fits `u32`, so
// `units` is exact.
const _: () = assert!(
    OnionLoopClass::MiB12
        .cell_bytes()
        .is_multiple_of(OnionLoopClass::DEFAULT.cell_bytes())
        && OnionLoopClass::MiB12.cell_bytes() / OnionLoopClass::DEFAULT.cell_bytes()
            <= u32::MAX as usize
        && OnionLoopClass::MiB12.units().get() == 768
);