From 589debdbe1b52e52b99e88f556f887a6d635a841 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 23:14:27 +0000 Subject: [PATCH] id-sync: B24 nonce-bound topology probes Adds the B24 slice. A prober signs each probe (prober, target, scope, fresh nonce, sent and timeout times); the probed peer answers only a valid, unexpired probe addressed to it, signing a response that binds responder, prober, scope, nonce and the exact request digest. The prober accepts a response only from the peer the probe went to, with matching nonce, scope and request, in time, under that peer's key; anything else changes nothing. Answers mark the peer reachable and timeouts mark it unreachable, each for a finite TTL, and current evidence becomes this node's own partial B23 route observation. Probes run on an interval with nonce-derived jitter. Disabling stops probing and drops all probe-derived state but keeps configured peers. Refs #10265 Co-Authored-By: Claude Opus 5.5 --- .../federation/slices/b24_topology_probe.rs | 463 ++++++++++++++++ .../id/crates/id-sync/tests/federation_b24.rs | 523 ++++++++++++++++++ 2 files changed, 986 insertions(+) create mode 100644 pkgs/id/crates/id-sync/src/federation/slices/b24_topology_probe.rs create mode 100644 pkgs/id/crates/id-sync/tests/federation_b24.rs diff --git a/pkgs/id/crates/id-sync/src/federation/slices/b24_topology_probe.rs b/pkgs/id/crates/id-sync/src/federation/slices/b24_topology_probe.rs new file mode 100644 index 0000000000..cfc7eeb1f2 --- /dev/null +++ b/pkgs/id/crates/id-sync/src/federation/slices/b24_topology_probe.rs @@ -0,0 +1,463 @@ +//! B24: nonce-bound topology probes (#10265, statements S1.77 and S1.78; +//! design "Routing validation and reputation policy"). +//! +//! Routing state is an attack surface (S1.77): a forged "I reached that +//! peer" can redirect fan-out, hide results or manufacture loops. So a +//! node learns about its own links only from probes it sent and that +//! the probed peer answered under its own key: +//! +//! - A [`ProbeRequest`] is signed by the prober and names the target, +//! the scope, a fresh nonce, when it was sent and when it times out. +//! - A [`ProbeResponse`] is signed by the responder and binds the +//! responder, the prober, the scope, the nonce and the digest of the +//! exact request it answers. [`respond`] answers only a request that +//! is addressed to the responder, verifies under the prober's key and +//! has not timed out. +//! - A [`Prober`] keeps one probe in flight per configured peer. It +//! accepts a response only from the peer that probe went to, with the +//! same nonce, scope and request digest, before the timeout, with a +//! valid signature from that peer's configured key; anything else is +//! refused and changes nothing. A probe answered in time marks the +//! peer reachable; one that times out marks it unreachable. Either +//! piece of evidence lasts a finite TTL. +//! - Probes run on a locally configured interval with jitter. The jitter +//! is taken from the probe's own fresh nonce, so it is unpredictable +//! to peers and reproducible in tests. +//! - [`Prober::observation`] turns current evidence into this node's own +//! B23 [`RouteObservation`]: links from this node only, in its scope, +//! valid only while every link's evidence is. It is partial by design +//! (S1.78): peers never probed, or whose evidence expired, are simply +//! absent. Probing changes only this node's local scoped state; it +//! never writes anything about other observers. +//! +//! Rollback: [`Prober::disable`] stops probing, drops probes in flight +//! and expires all probe-derived evidence, while keeping the configured +//! peers and their keys. +//! +//! Signed bytes (integers big-endian, strings as a u32 length and UTF-8 +//! bytes; the signature is Ed25519 over the whole preimage): +//! +//! ```text +//! request: "id-sync/b24-probe-request/v1" prober target scope +//! nonce:32 sent_at:u64 expires_at:u64 +//! response: "id-sync/b24-probe-response/v1" responder prober scope +//! nonce:32 request_digest:32 responded_at:u64 +//! ``` +//! +//! `request_digest` is SHA-256 over the request preimage followed by its +//! signature. +//! +//! This module is deliberately self-contained: the federation roots +//! record slice paths without declaring them (slice 9110-02), so the +//! slice's integration test includes this file directly and no shared +//! root names it yet; #9110-09 wires versioned public exports after +//! module outputs land. + +use std::collections::BTreeMap; + +use ed25519_dalek::{Signature, Signer, SigningKey, VerifyingKey}; +use sha2::{Digest, Sha256}; + +use super::b23_routing_state::{LinkStatus, PeerId, RouteObservation}; + +/// Domain prefix of a probe request preimage. +pub const REQUEST_DOMAIN: &[u8] = b"id-sync/b24-probe-request/v1"; +/// Domain prefix of a probe response preimage. +pub const RESPONSE_DOMAIN: &[u8] = b"id-sync/b24-probe-response/v1"; + +/// A fresh probe nonce. +pub type Nonce = [u8; 32]; + +fn push_str(out: &mut Vec, value: &str) { + out.extend_from_slice(&u32::try_from(value.len()).unwrap_or(u32::MAX).to_be_bytes()); + out.extend_from_slice(value.as_bytes()); +} + +fn verify(key: &VerifyingKey, preimage: &[u8], signature: &[u8; 64]) -> bool { + key.verify_strict(preimage, &Signature::from_bytes(signature)) + .is_ok() +} + +/// A signed probe. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProbeRequest { + /// Who probes, and signs. + pub prober: PeerId, + /// Who is probed. + pub target: PeerId, + /// The routing scope. + pub scope: String, + /// Fresh per probe. + pub nonce: Nonce, + /// When it was sent. + pub sent_at: u64, + /// When it times out. + pub expires_at: u64, + /// The prober's signature over [`Self::preimage`]. + pub signature: [u8; 64], +} + +impl ProbeRequest { + /// The signed bytes. + pub fn preimage(&self) -> Vec { + let mut out = REQUEST_DOMAIN.to_vec(); + push_str(&mut out, &self.prober); + push_str(&mut out, &self.target); + push_str(&mut out, &self.scope); + out.extend_from_slice(&self.nonce); + out.extend_from_slice(&self.sent_at.to_be_bytes()); + out.extend_from_slice(&self.expires_at.to_be_bytes()); + out + } + + /// SHA-256 over the preimage and the signature. + pub fn digest(&self) -> [u8; 32] { + let mut h = Sha256::new(); + h.update(self.preimage()); + h.update(self.signature); + h.finalize().into() + } +} + +/// A signed answer to one probe. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProbeResponse { + /// Who answers, and signs. + pub responder: PeerId, + /// Who sent the probe. + pub prober: PeerId, + /// The routing scope. + pub scope: String, + /// The probe's nonce. + pub nonce: Nonce, + /// The digest of the exact request answered. + pub request_digest: [u8; 32], + /// When it answered. + pub responded_at: u64, + /// The responder's signature over [`Self::preimage`]. + pub signature: [u8; 64], +} + +impl ProbeResponse { + /// The signed bytes. + pub fn preimage(&self) -> Vec { + let mut out = RESPONSE_DOMAIN.to_vec(); + push_str(&mut out, &self.responder); + push_str(&mut out, &self.prober); + push_str(&mut out, &self.scope); + out.extend_from_slice(&self.nonce); + out.extend_from_slice(&self.request_digest); + out.extend_from_slice(&self.responded_at.to_be_bytes()); + out + } +} + +/// Why a responder will not answer. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RespondError { + /// The probe is addressed to someone else. + NotForMe, + /// The probe's signature does not verify under the prober's key. + BadSignature, + /// The probe has timed out. + Expired, +} + +/// Answer `request` as `responder`, checking it under `prober_key`. +pub fn respond( + key: &SigningKey, + responder: &str, + request: &ProbeRequest, + prober_key: &VerifyingKey, + now: u64, +) -> Result { + if request.target != responder { + return Err(RespondError::NotForMe); + } + if !verify(prober_key, &request.preimage(), &request.signature) { + return Err(RespondError::BadSignature); + } + if now >= request.expires_at { + return Err(RespondError::Expired); + } + let mut response = ProbeResponse { + responder: responder.to_owned(), + prober: request.prober.clone(), + scope: request.scope.clone(), + nonce: request.nonce, + request_digest: request.digest(), + responded_at: now, + signature: [0; 64], + }; + response.signature = key.sign(&response.preimage()).to_bytes(); + Ok(response) +} + +/// Locally configured probe timing. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct ProbeConfig { + /// Base time between probes of one peer. + pub interval: u64, + /// Up to this much is added to each interval. + pub jitter: u64, + /// How long a probe waits for its answer. + pub timeout: u64, + /// How long one probe's evidence lasts. + pub ttl: u64, +} + +/// Why a probe could not start or a response was refused. A refusal +/// changes nothing. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ProbeError { + /// Probing is disabled. + Disabled, + /// The peer is not configured. + UnknownPeer, + /// A probe to this peer is already in flight. + InFlight, + /// No probe to this responder is in flight. + Unsolicited, + /// The response names another prober. + WrongProber, + /// The response is for another scope. + WrongScope, + /// The response carries another nonce. + WrongNonce, + /// The response answers another request. + WrongRequest, + /// The response claims a time before the probe was sent or after now. + BadTime, + /// The probe has timed out. + Late, + /// The signature does not verify under the responder's key. + BadSignature, +} + +/// Why a probe could not be configured. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ConfigError { + /// A node does not probe itself. + SelfProbe, + /// Timeout and TTL must be positive. + ZeroDuration, +} + +/// One piece of probe evidence about a peer. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Evidence { + /// What the probe showed. + pub status: LinkStatus, + /// When it showed it. + pub observed_at: u64, + /// When the evidence stops counting. + pub expires_at: u64, +} + +/// A node's prober for one scope. +#[derive(Debug, Clone)] +pub struct Prober { + me: PeerId, + key: SigningKey, + scope: String, + config: ProbeConfig, + enabled: bool, + peers: BTreeMap, + next_due: BTreeMap, + in_flight: BTreeMap, + evidence: BTreeMap, +} + +impl Prober { + /// A prober for `peers`, each due at `now`. + pub fn new( + me: PeerId, + key: SigningKey, + scope: String, + config: ProbeConfig, + peers: BTreeMap, + now: u64, + ) -> Result { + if peers.contains_key(&me) { + return Err(ConfigError::SelfProbe); + } + if config.timeout == 0 || config.ttl == 0 { + return Err(ConfigError::ZeroDuration); + } + let next_due = peers.keys().map(|p| (p.clone(), now)).collect(); + Ok(Self { + me, + key, + scope, + config, + enabled: true, + peers, + next_due, + in_flight: BTreeMap::new(), + evidence: BTreeMap::new(), + }) + } + + /// Configured peers due a probe at `now`, with none in flight. + pub fn due(&self, now: u64) -> Vec { + if !self.enabled { + return Vec::new(); + } + self.next_due + .iter() + .filter(|(peer, at)| **at <= now && !self.in_flight.contains_key(*peer)) + .map(|(peer, _)| peer.clone()) + .collect() + } + + /// Sign and record a probe to `peer` with a fresh `nonce`. + pub fn start( + &mut self, + peer: &str, + nonce: Nonce, + now: u64, + ) -> Result { + if !self.enabled { + return Err(ProbeError::Disabled); + } + if !self.peers.contains_key(peer) { + return Err(ProbeError::UnknownPeer); + } + if self.in_flight.contains_key(peer) { + return Err(ProbeError::InFlight); + } + let mut request = ProbeRequest { + prober: self.me.clone(), + target: peer.to_owned(), + scope: self.scope.clone(), + nonce, + sent_at: now, + expires_at: now.saturating_add(self.config.timeout), + signature: [0; 64], + }; + request.signature = self.key.sign(&request.preimage()).to_bytes(); + let mut head = [0; 8]; + head.copy_from_slice(&nonce[..8]); + let jitter = u64::from_be_bytes(head) % self.config.jitter.saturating_add(1); + self.next_due.insert( + peer.to_owned(), + now.saturating_add(self.config.interval) + .saturating_add(jitter), + ); + self.in_flight.insert(peer.to_owned(), request.clone()); + Ok(request) + } + + /// Accept a response, marking its responder reachable. + pub fn receive(&mut self, response: &ProbeResponse, now: u64) -> Result<(), ProbeError> { + let request = self + .in_flight + .get(&response.responder) + .ok_or(ProbeError::Unsolicited)?; + if response.prober != self.me { + return Err(ProbeError::WrongProber); + } + if response.scope != self.scope { + return Err(ProbeError::WrongScope); + } + if response.nonce != request.nonce { + return Err(ProbeError::WrongNonce); + } + if response.request_digest != request.digest() { + return Err(ProbeError::WrongRequest); + } + if now >= request.expires_at { + return Err(ProbeError::Late); + } + if response.responded_at < request.sent_at || response.responded_at > now { + return Err(ProbeError::BadTime); + } + let key = self + .peers + .get(&response.responder) + .ok_or(ProbeError::Unsolicited)?; + if !verify(key, &response.preimage(), &response.signature) { + return Err(ProbeError::BadSignature); + } + self.in_flight.remove(&response.responder); + self.record(&response.responder, LinkStatus::Reachable, now); + Ok(()) + } + + fn record(&mut self, peer: &str, status: LinkStatus, at: u64) { + self.evidence.insert( + peer.to_owned(), + Evidence { + status, + observed_at: at, + expires_at: at.saturating_add(self.config.ttl), + }, + ); + } + + /// Mark every probe that timed out by `now` unreachable; returns the + /// peers, in order. + pub fn expire(&mut self, now: u64) -> Vec { + let late: Vec<(PeerId, u64)> = self + .in_flight + .iter() + .filter(|(_, r)| now >= r.expires_at) + .map(|(p, r)| (p.clone(), r.expires_at)) + .collect(); + for (peer, at) in &late { + self.in_flight.remove(peer); + self.record(peer, LinkStatus::Unreachable, *at); + } + late.into_iter().map(|(p, _)| p).collect() + } + + /// Current evidence about `peer`, if any. + pub fn evidence(&self, peer: &str, now: u64) -> Option { + self.evidence + .get(peer) + .copied() + .filter(|e| now < e.expires_at) + } + + /// This node's own observation from current evidence, or `None` when + /// it has none. + pub fn observation(&self, now: u64) -> Option { + let current: Vec<(&PeerId, &Evidence)> = self + .evidence + .iter() + .filter(|(_, e)| now < e.expires_at) + .collect(); + let expires_at = current.iter().map(|(_, e)| e.expires_at).min()?; + Some(RouteObservation { + observer: self.me.clone(), + scope: self.scope.clone(), + observed_at: now, + expires_at, + links: current + .into_iter() + .map(|(peer, e)| (peer.clone(), e.status)) + .collect(), + }) + } + + /// Stop probing and drop all probe-derived state; configured peers + /// and keys stay. + pub fn disable(&mut self) { + self.enabled = false; + self.in_flight.clear(); + self.evidence.clear(); + } + + /// Resume probing; every peer is due at `now`. + pub fn enable(&mut self, now: u64) { + self.enabled = true; + for at in self.next_due.values_mut() { + *at = now; + } + } + + /// Whether a probe to `peer` is in flight. + pub fn in_flight(&self, peer: &str) -> bool { + self.in_flight.contains_key(peer) + } +} diff --git a/pkgs/id/crates/id-sync/tests/federation_b24.rs b/pkgs/id/crates/id-sync/tests/federation_b24.rs new file mode 100644 index 0000000000..066d37b19d --- /dev/null +++ b/pkgs/id/crates/id-sync/tests/federation_b24.rs @@ -0,0 +1,523 @@ +//! B24 invariant test: nonce-bound topology probes (#10265, statements +//! S1.77 and S1.78). +//! +//! Oracle: **invariant** — a probe response binds the nonce and the +//! peer, and stale, vanished and appeared edges update only this node's +//! local scoped routing state. Golden vectors for both signed preimages +//! were computed independently with Python `cryptography` (Ed25519) and +//! `hashlib`. A reference state machine in this file tracks, per peer, +//! when the next probe is due, the probe in flight and the current +//! evidence; random streams start probes, deliver honest and tampered +//! responses (another nonce, scope, prober or request, a bad signature, +//! an impossible time, an unknown responder, a late answer), expire +//! probes, disable and re-enable, and compare every outcome and the +//! resulting observation with the slice. +//! +//! The slice modules are loaded by path: the federation roots record +//! them without declaring them (slice 9110-02), and B24 reaches B23 +//! through `super::`. + +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +#[path = "../src/federation/slices/b32_ingest_gate.rs"] +#[allow(dead_code)] +mod b32_ingest_gate; + +#[path = "../src/federation/slices/b23_routing_state.rs"] +#[allow(dead_code)] +mod b23_routing_state; + +#[path = "../src/federation/slices/b24_topology_probe.rs"] +mod b24_topology_probe; + +use std::collections::BTreeMap; +use std::fmt::Write as _; + +use b23_routing_state::{LinkStatus, RouteObservation}; +use b24_topology_probe::{ + ConfigError, Evidence, Nonce, ProbeConfig, ProbeError, ProbeRequest, ProbeResponse, Prober, + RespondError, respond, +}; +use ed25519_dalek::{Signer, SigningKey, VerifyingKey}; +use proptest::prelude::*; + +const REQ_PRE: &str = "69642d73796e632f6232342d70726f62652d726571756573742f76310000000161000000016200000001730707070707070707070707070707070707070707070707070707070707070707000000000000000a000000000000000f"; +const REQ_SIG: &str = "61e040ee2fdf519cbef3c587034178d83c88d169c83456406e85a0896a773de8a5ef809d2b918de25566bf54f16c2ff9e3c3eb3e33421c65b84bdaf068e23309"; +const REQ_DIG: &str = "55b50ac05166c58357e5ae1ef5636a2313eca460de2644ac125c799350dc9226"; +const RESP_PRE: &str = "69642d73796e632f6232342d70726f62652d726573706f6e73652f7631000000016200000001610000000173070707070707070707070707070707070707070707070707070707070707070755b50ac05166c58357e5ae1ef5636a2313eca460de2644ac125c799350dc9226000000000000000c"; +const RESP_SIG: &str = "20825285e52a01a6020b8d94409322618a75da17817274b397e716019cd3f432d8dcff453922735e9624ae2fd1880c4e8c3d77405667cc3f1fe87c482fe64001"; + +fn hex(bytes: &[u8]) -> String { + bytes.iter().fold(String::new(), |mut out, b| { + let _ = write!(out, "{b:02x}"); + out + }) +} + +fn key(seed: u8) -> SigningKey { + SigningKey::from_bytes(&[seed; 32]) +} + +fn pk(seed: u8) -> VerifyingKey { + key(seed).verifying_key() +} + +const CONFIG: ProbeConfig = ProbeConfig { + interval: 10, + jitter: 3, + timeout: 5, + ttl: 20, +}; + +const PEERS: [(&str, u8); 3] = [("b", 2), ("c", 3), ("d", 4)]; + +fn peers() -> BTreeMap { + PEERS + .iter() + .map(|(p, s)| ((*p).to_owned(), pk(*s))) + .collect() +} + +fn prober(now: u64) -> Prober { + Prober::new("a".into(), key(1), "s".into(), CONFIG, peers(), now).unwrap() +} + +fn seed_of(peer: &str) -> u8 { + PEERS + .iter() + .find(|(p, _)| *p == peer) + .map_or(9, |(_, s)| *s) +} + +#[test] +fn golden_vectors() { + let mut p = Prober::new( + "a".into(), + key(1), + "s".into(), + ProbeConfig { + timeout: 5, + ..CONFIG + }, + peers(), + 10, + ) + .unwrap(); + let req = p.start("b", [7; 32], 10).unwrap(); + assert_eq!(hex(&req.preimage()), REQ_PRE); + assert_eq!(hex(&req.signature), REQ_SIG); + assert_eq!(hex(&req.digest()), REQ_DIG); + let resp = respond(&key(2), "b", &req, &pk(1), 12).unwrap(); + assert_eq!(hex(&resp.preimage()), RESP_PRE); + assert_eq!(hex(&resp.signature), RESP_SIG); + p.receive(&resp, 13).unwrap(); + assert_eq!( + p.evidence("b", 13), + Some(Evidence { + status: LinkStatus::Reachable, + observed_at: 13, + expires_at: 33 + }) + ); +} + +#[test] +fn configuration_is_checked() { + let mut with_self = peers(); + with_self.insert("a".into(), pk(1)); + assert_eq!( + Prober::new("a".into(), key(1), "s".into(), CONFIG, with_self, 0).err(), + Some(ConfigError::SelfProbe) + ); + for bad in [ + ProbeConfig { + timeout: 0, + ..CONFIG + }, + ProbeConfig { ttl: 0, ..CONFIG }, + ] { + assert_eq!( + Prober::new("a".into(), key(1), "s".into(), bad, peers(), 0).err(), + Some(ConfigError::ZeroDuration) + ); + } +} + +#[test] +fn responder_answers_only_valid_probes_for_itself_in_time() { + let mut p = prober(0); + let req = p.start("b", [1; 32], 0).unwrap(); + assert_eq!( + respond(&key(3), "c", &req, &pk(1), 1), + Err(RespondError::NotForMe) + ); + assert_eq!( + respond(&key(2), "b", &req, &pk(4), 1), + Err(RespondError::BadSignature) + ); + let mut forged = req.clone(); + forged.scope = "other".into(); + assert_eq!( + respond(&key(2), "b", &forged, &pk(1), 1), + Err(RespondError::BadSignature) + ); + assert_eq!( + respond(&key(2), "b", &req, &pk(1), 5), + Err(RespondError::Expired) + ); + assert!(respond(&key(2), "b", &req, &pk(1), 4).is_ok()); +} + +#[test] +fn a_response_binds_nonce_and_peer() { + let mut p = prober(0); + let rb = p.start("b", [1; 32], 0).unwrap(); + let rc = p.start("c", [2; 32], 0).unwrap(); + let ok_b = respond(&key(2), "b", &rb, &pk(1), 1).unwrap(); + let ok_c = respond(&key(3), "c", &rc, &pk(1), 1).unwrap(); + + // c answers b's probe under its own key: the response names c, so it + // must match c's probe, whose nonce differs. + let cross = respond( + &key(3), + "c", + &ProbeRequest { + target: "c".into(), + ..rb.clone() + }, + &pk(1), + 1, + ); + assert_eq!(cross, Err(RespondError::BadSignature)); + let mut relabelled = ok_b.clone(); + relabelled.responder = "c".into(); + assert_eq!(p.receive(&relabelled, 2), Err(ProbeError::WrongNonce)); + // b's answer re-signed by c's key under b's name fails the signature. + let mut stolen = ok_b.clone(); + stolen.signature = key(3).sign(&stolen.preimage()).to_bytes(); + assert_eq!(p.receive(&stolen, 2), Err(ProbeError::BadSignature)); + // Answers to the wrong request or nonce are refused. + let mut other_nonce = ok_c.clone(); + other_nonce.nonce = [9; 32]; + assert_eq!(p.receive(&other_nonce, 2), Err(ProbeError::WrongNonce)); + let mut other_req = ok_c.clone(); + other_req.request_digest = rb.digest(); + assert_eq!(p.receive(&other_req, 2), Err(ProbeError::WrongRequest)); + assert!(p.in_flight("b") && p.in_flight("c")); + assert_eq!(p.evidence("b", 2), None); + + p.receive(&ok_b, 2).unwrap(); + // The same answer again is unsolicited: the probe is closed. + assert_eq!(p.receive(&ok_b, 2), Err(ProbeError::Unsolicited)); + p.receive(&ok_c, 3).unwrap(); +} + +#[test] +fn a_response_must_fall_between_send_and_receipt() { + let mut p = prober(0); + let rb = p.start("b", [1; 32], 3).unwrap(); + let resign = |r: &mut ProbeResponse| r.signature = key(2).sign(&r.preimage()).to_bytes(); + let mut early = respond(&key(2), "b", &rb, &pk(1), 3).unwrap(); + early.responded_at = 2; + resign(&mut early); + assert_eq!(p.receive(&early, 4), Err(ProbeError::BadTime)); + let mut future = respond(&key(2), "b", &rb, &pk(1), 3).unwrap(); + future.responded_at = 5; + resign(&mut future); + assert_eq!(p.receive(&future, 4), Err(ProbeError::BadTime)); + assert!(p.in_flight("b")); + let at_send = respond(&key(2), "b", &rb, &pk(1), 3).unwrap(); + p.receive(&at_send, 3).unwrap(); +} + +#[test] +fn timeouts_mark_unreachable_and_late_answers_change_nothing() { + let mut p = prober(0); + let rb = p.start("b", [1; 32], 0).unwrap(); + let ok = respond(&key(2), "b", &rb, &pk(1), 4).unwrap(); + assert_eq!(p.receive(&ok, 5), Err(ProbeError::Late)); + assert!(p.in_flight("b")); + assert_eq!(p.expire(4), Vec::::new()); + assert_eq!(p.expire(5), vec!["b".to_owned()]); + assert_eq!(p.receive(&ok, 5), Err(ProbeError::Unsolicited)); + assert_eq!( + p.evidence("b", 6).map(|e| e.status), + Some(LinkStatus::Unreachable) + ); + assert_eq!(p.evidence("b", 25), None); +} + +#[test] +fn probes_are_due_on_interval_plus_nonce_jitter() { + let mut p = prober(0); + assert_eq!(p.due(0), vec!["b".to_owned(), "c".into(), "d".into()]); + let mut nonce = [0; 32]; + nonce[7] = 6; // 6 mod (jitter 3 + 1) = 2 + p.start("b", nonce, 0).unwrap(); + assert_eq!(p.due(0), vec!["c".to_owned(), "d".into()]); + assert_eq!(p.start("b", nonce, 0), Err(ProbeError::InFlight)); + assert_eq!(p.start("z", nonce, 0), Err(ProbeError::UnknownPeer)); + p.expire(5); + assert!(!p.due(11).contains(&"b".to_owned())); + assert!(p.due(12).contains(&"b".to_owned())); +} + +#[test] +fn observation_is_local_partial_and_expires_with_its_evidence() { + let mut p = prober(0); + assert_eq!(p.observation(0), None); + let rb = p.start("b", [1; 32], 0).unwrap(); + p.start("c", [2; 32], 0).unwrap(); + p.receive(&respond(&key(2), "b", &rb, &pk(1), 1).unwrap(), 1) + .unwrap(); + p.expire(5); + let obs = p.observation(6).unwrap(); + assert_eq!(obs.observer, "a"); + assert_eq!(obs.scope, "s"); + assert_eq!(obs.observed_at, 6); + assert_eq!(obs.expires_at, 21); + assert_eq!( + obs.links, + BTreeMap::from([ + ("b".to_owned(), LinkStatus::Reachable), + ("c".to_owned(), LinkStatus::Unreachable) + ]) + ); + assert_eq!(RouteObservation::decode(&obs.encode()), Some(obs)); + // b's evidence has expired; c's remains until 25. + let later = p.observation(21).unwrap(); + assert_eq!( + later.links, + BTreeMap::from([("c".to_owned(), LinkStatus::Unreachable)]) + ); + assert_eq!(p.observation(25), None); +} + +#[test] +fn disable_drops_probe_state_and_keeps_peers() { + let mut p = prober(0); + let rb = p.start("b", [1; 32], 0).unwrap(); + let ok = respond(&key(2), "b", &rb, &pk(1), 1).unwrap(); + p.disable(); + assert_eq!(p.due(100), Vec::::new()); + assert_eq!(p.start("c", [2; 32], 1), Err(ProbeError::Disabled)); + assert_eq!(p.receive(&ok, 1), Err(ProbeError::Unsolicited)); + assert_eq!(p.observation(1), None); + p.enable(50); + assert_eq!(p.due(50).len(), 3); + let rc = p.start("c", [2; 32], 50).unwrap(); + p.receive(&respond(&key(3), "c", &rc, &pk(1), 51).unwrap(), 51) + .unwrap(); +} + +// ---- reference state machine ---------------------------------------- + +#[derive(Debug, Clone, Default)] +struct RefPeer { + due: u64, + flight: Option<(Nonce, u64, u64)>, + evidence: Option<(LinkStatus, u64, u64)>, +} + +#[derive(Debug, Clone)] +struct RefProber { + enabled: bool, + peers: BTreeMap, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Tamper { + None, + Prober, + Scope, + Nonce, + Request, + Time, + Signature, + Unknown, +} + +#[derive(Debug, Clone)] +enum Op { + Start(usize, [u8; 8]), + Answer(usize, Tamper), + Expire, + Tick(u64), + Disable, + Enable, +} + +fn arb_op() -> impl Strategy { + let tamper = prop_oneof![ + 6 => Just(Tamper::None), + 1 => Just(Tamper::Prober), + 1 => Just(Tamper::Scope), + 1 => Just(Tamper::Nonce), + 1 => Just(Tamper::Request), + 1 => Just(Tamper::Time), + 1 => Just(Tamper::Signature), + 1 => Just(Tamper::Unknown), + ]; + prop_oneof![ + 4 => (0usize..3, any::<[u8; 8]>()).prop_map(|(p, n)| Op::Start(p, n)), + 5 => (0usize..3, tamper).prop_map(|(p, t)| Op::Answer(p, t)), + 2 => Just(Op::Expire), + 3 => (0u64..8).prop_map(Op::Tick), + 1 => Just(Op::Disable), + 1 => Just(Op::Enable), + ] +} + +fn nonce_from(head: [u8; 8], salt: u64) -> Nonce { + let mut n = [0; 32]; + n[..8].copy_from_slice(&head); + n[8..16].copy_from_slice(&salt.to_be_bytes()); + n +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(512))] + + #[test] + fn prober_matches_the_reference(ops in prop::collection::vec(arb_op(), 1..60)) { + let mut p = prober(0); + let mut r = RefProber { + enabled: true, + peers: PEERS.iter().map(|(n, _)| ((*n).to_owned(), RefPeer::default())).collect(), + }; + let mut requests: BTreeMap = BTreeMap::new(); + let mut now = 0u64; + for (step, op) in ops.into_iter().enumerate() { + match op { + Op::Start(i, head) => { + let peer = PEERS[i].0; + let nonce = nonce_from(head, step as u64); + let rp = r.peers.get_mut(peer).unwrap(); + let want = if !r.enabled { + Err(ProbeError::Disabled) + } else if rp.flight.is_some() { + Err(ProbeError::InFlight) + } else { + rp.flight = Some((nonce, now, now + CONFIG.timeout)); + rp.due = now + CONFIG.interval + u64::from_be_bytes(head) % (CONFIG.jitter + 1); + Ok(()) + }; + let got = p.start(peer, nonce, now); + prop_assert_eq!(got.as_ref().map(|_| ()).map_err(|e| *e), want); + if let Ok(req) = got { + prop_assert_eq!(&req.target, peer); + prop_assert_eq!(req.nonce, nonce); + requests.insert(peer.to_owned(), req); + } + } + Op::Answer(i, tamper) => { + let peer = PEERS[i].0; + let Some(req) = requests.get(peer).cloned() else { continue }; + // The responder answers at the probe's send time; the + // answer is delivered now. + let mut resp: ProbeResponse = + respond(&key(seed_of(peer)), peer, &req, &pk(1), req.sent_at).unwrap(); + let resign = |resp: &mut ProbeResponse, seed: u8| { + resp.signature = key(seed).sign(&resp.preimage()).to_bytes(); + }; + match tamper { + Tamper::None => {} + Tamper::Prober => { resp.prober = "x".into(); resign(&mut resp, seed_of(peer)); } + Tamper::Scope => { resp.scope = "t".into(); resign(&mut resp, seed_of(peer)); } + Tamper::Nonce => { resp.nonce[31] ^= 1; resign(&mut resp, seed_of(peer)); } + Tamper::Request => { resp.request_digest[0] ^= 1; resign(&mut resp, seed_of(peer)); } + Tamper::Time => { resp.responded_at = now + 1; resign(&mut resp, seed_of(peer)); } + Tamper::Signature => resign(&mut resp, 9), + Tamper::Unknown => { resp.responder = "z".into(); resign(&mut resp, 9); } + } + let rp = r.peers.get_mut(peer).unwrap(); + let live = rp.flight.filter(|(n, _, _)| *n == req.nonce); + let want = match (tamper, live) { + (Tamper::Unknown, _) | (_, None) => Err(ProbeError::Unsolicited), + (Tamper::Prober, _) => Err(ProbeError::WrongProber), + (Tamper::Scope, _) => Err(ProbeError::WrongScope), + (Tamper::Nonce, _) => Err(ProbeError::WrongNonce), + (Tamper::Request, _) => Err(ProbeError::WrongRequest), + (_, Some((_, _, exp))) if now >= exp => Err(ProbeError::Late), + (Tamper::Time, _) => Err(ProbeError::BadTime), + (Tamper::Signature, _) => Err(ProbeError::BadSignature), + (Tamper::None, Some(_)) => { + rp.flight = None; + rp.evidence = Some((LinkStatus::Reachable, now, now + CONFIG.ttl)); + Ok(()) + } + }; + prop_assert_eq!(p.receive(&resp, now), want); + } + Op::Expire => { + let mut want = Vec::new(); + for (name, rp) in &mut r.peers { + if let Some((_, _, exp)) = rp.flight + && now >= exp + { + rp.flight = None; + rp.evidence = Some((LinkStatus::Unreachable, exp, exp + CONFIG.ttl)); + want.push(name.clone()); + } + } + prop_assert_eq!(p.expire(now), want); + } + Op::Tick(dt) => now += dt, + Op::Disable => { + r.enabled = false; + for rp in r.peers.values_mut() { + rp.flight = None; + rp.evidence = None; + } + p.disable(); + } + Op::Enable => { + r.enabled = true; + for rp in r.peers.values_mut() { + rp.due = now; + } + p.enable(now); + } + } + // Due peers, in-flight state, evidence and the observation. + let due: Vec = if r.enabled { + r.peers + .iter() + .filter(|(_, rp)| rp.due <= now && rp.flight.is_none()) + .map(|(n, _)| n.clone()) + .collect() + } else { + Vec::new() + }; + prop_assert_eq!(p.due(now), due); + let mut links = BTreeMap::new(); + let mut exp_min = None::; + for (name, rp) in &r.peers { + prop_assert_eq!(p.in_flight(name), rp.flight.is_some()); + let ev = rp.evidence.filter(|(_, _, exp)| now < *exp); + prop_assert_eq!( + p.evidence(name, now), + ev.map(|(status, observed_at, expires_at)| Evidence { status, observed_at, expires_at }) + ); + if let Some((status, _, exp)) = ev { + links.insert(name.clone(), status); + exp_min = Some(exp_min.map_or(exp, |m| m.min(exp))); + } + } + let obs = p.observation(now); + match exp_min { + None => prop_assert_eq!(obs, None), + Some(exp) => { + let obs = obs.unwrap(); + prop_assert_eq!(&obs.observer, "a"); + prop_assert_eq!(&obs.scope, "s"); + prop_assert_eq!(obs.observed_at, now); + prop_assert_eq!(obs.expires_at, exp); + prop_assert!(!obs.links.contains_key("a")); + prop_assert_eq!(&obs.links, &links); + prop_assert_eq!(RouteObservation::decode(&obs.encode()), Some(obs.clone())); + } + } + } + } +}