rings-node 1.0.0-pre.5

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! The fixed-length Sphinx header `χ = (α, β, γ)` (#834 L5′, D6″; paper Def. fixed-length header).
//!
//! Key transport is one blinded secp256k1 element per cell. Hop `i` holds the delegatee key
//! `d_i` with `pk_i = d_i·G`; the client draws `x ∈ Z_n^*` and, over `G ∖ {O}` and `Z_n^*` (so no
//! step can meet `O`, see `rings_core::ecc::prime_order`),
//!
//! ```text
//! x_1 = x,  α_i = x_i·G,  K_i = x(x_i·pk_i) = x(d_i·α_i)     (ECDH; the hop computes d_i·α_i)
//! (ρ-key, μ-key, blind) = HKDF-SHA256(salt = D, ikm = SEC1(α_i) ‖ K_i; "prg", "mac", "blind")
//! z_i = blind mod n ∈ Z_n^*     (the blinding factor; 64-byte wide reduction; 0 rejected)
//! x_{i+1} = x_i·z_i,  α_{i+1} = z_i·α_i
//! ρ_i = ChaCha20_{ρ-key}(0^{(Ĥ+1)ℓ}),   γ_i = HMAC-SHA256_{μ-key}(b ‖ β_i)[0, 16)
//! ```
//!
//! where `b` is the loop class, bound as its one-byte label (#834 H1: a cell relabelled to another
//! class fails `γ` at the next honest hop); `b` denotes the class only. `SEC1(α_i)` in the key
//! schedule binds the sign of `α_i`: `−α_i` has the same `K_i` but other keys. With `β` as `Ĥ`
//! blocks of `ℓ` bytes and `ρ[a, b)` the blocks `a … b − 1`, the routing information is
//!
//! ```text
//! φ_0 = ε,   φ_i = (φ_{i−1} ‖ 0^ℓ) ⊕ ρ_i[Ĥ−i+1, Ĥ+1)                               1 ≤ i < H
//! β_H = ((λ_H ‖ R) ⊕ ρ_H[0, Ĥ−H+1)) ‖ φ_{H−1}                                     R uniform
//! β_i = (λ_i ‖ β_{i+1}[0, Ĥ−1)) ⊕ ρ_i[0, Ĥ)                                       1 ≤ i < H
//! ```
//!
//! and hop `i` peels `(β_i ‖ 0^ℓ) ⊕ ρ_i = λ_i ‖ β_{i+1}`, where `λ_i` carries `γ_{i+1}`. At
//! `i = H` the next header goes to the client, position `H + 1` (D6′): `γ_{H+1} = t_⋄` is the
//! client's loop tag, drawn by the client and forwarded by the guard ([`OnionHeader::loop_tag`]).
//!
//! Laws:
//!
//! - **Correctness** (Prop. header correctness). For `H ≤ Ĥ` and `1 ≤ i ≤ H`,
//!   [`OnionHeader::peel`] of `χ_i` under `d_i` yields `λ_i` and `χ_{i+1}`, every `γ_i` verifies,
//!   and `χ_{H+1}` carries `t_⋄`. By induction down from `H`, the last `i − 1` blocks of `β_i` are
//!   `φ_{i−1}`, so the block that peeling appends, `ρ_i[Ĥ, Ĥ+1)`, is the last block of `φ_i`,
//!   hence of `β_{i+1}`.
//! - **Length** (L5′). `|χ| = 33 + Ĥℓ + 16` for every `H` and `i`: the header type has fixed
//!   width.
//! - **Rejection.** `peel` rejects an `α` that is not a curve point before any ECDH (a uniformly
//!   random 33-byte string, e.g. link cover under F = 0, is rejected there with probability
//!   `≈ 99.6 %`), and verifies `γ` before any layer is decoded. Both are one outcome,
//!   [`OnionPeelError::Invalid`]: the admission step charges a cell rejected at either point
//!   the same `u(b)` (#834), so a flood of invalid `α` is not free.

use arrayref::array_ref;
use arrayref::array_refs;
use chacha20::cipher::KeyIvInit;
use chacha20::cipher::StreamCipher;
use chacha20::ChaCha20;
use hmac::digest::Key;
use hmac::Hmac;
use hmac::KeyInit;
use hmac::Mac;
use rand::CryptoRng;
use rand::RngCore;
use rings_core::delegation::DelegateeKey;
use rings_core::ecc::prime_order::NonIdentityPoint;
use rings_core::ecc::prime_order::NonZeroScalar;
use rings_core::ecc::prime_order::SHARED_SECRET_BYTES;
use rings_core::ecc::prime_order::WIDE_SCALAR_BYTES;
use rings_core::ecc::PublicKey;
use rings_core::ecc::Secp256k1;
use sha2::Sha256;
use subtle::Choice;
use subtle::ConstantTimeEq;
use zeroize::Zeroizing;

use super::class::OnionLoopClass;
use super::hkdf_expand;
use super::hkdf_extract;
use super::layer::OnionLayer;
use super::layer::OnionLayerError;
use super::layer::ONION_LAYER_BYTES;
use super::xor_in_place;
use crate::onion::loop_shape::MAX_ONION_LOOP_HOPS;

/// `|α|`, a compressed SEC1 secp256k1 point.
const ONION_GROUP_ELEMENT_BYTES: usize = 33;

/// `|γ|`, the truncated header MAC.
pub(crate) const ONION_HEADER_MAC_BYTES: usize = 16;

/// `|β| = Ĥ·ℓ`, the routing information: exactly `Ĥ` layer slots whatever the loop's own `H`
/// (L5′).
pub(crate) const ONION_HEADER_ROUTING_BYTES: usize = MAX_ONION_LOOP_HOPS * ONION_LAYER_BYTES;

/// `|χ| = |α| + |β| + |γ|`.
pub(crate) const ONION_HEADER_BYTES: usize =
    ONION_GROUP_ELEMENT_BYTES + ONION_HEADER_ROUTING_BYTES + ONION_HEADER_MAC_BYTES;

/// HKDF salt `D` of the per-hop key schedule.
const HEADER_KDF_SALT: &[u8] = b"rings-node:onion-sphinx-header";

/// HKDF info label of the stream-cipher key.
const STREAM_INFO: &[u8] = b"prg";

/// HKDF info label of the MAC key.
const MAC_INFO: &[u8] = b"mac";

/// HKDF info label of the blinding factor.
const BLIND_INFO: &[u8] = b"blind";

/// Width of the stream-cipher key.
const STREAM_KEY_BYTES: usize = 32;

/// Width of the MAC key: one SHA-256 block, so HMAC uses it without hashing.
const MAC_KEY_BYTES: usize = 64;

/// One `ℓ`-byte block of `β` or `ρ`: one layer slot.
type Block = [u8; ONION_LAYER_BYTES];

/// `β`, `Ĥ` layer slots.
type Routing = [Block; MAX_ONION_LOOP_HOPS];

/// `ρ`, the pad of `β ‖ 0^ℓ`: `Ĥ + 1` blocks.
type Stream = [Block; MAX_ONION_LOOP_HOPS + 1];

/// `γ`, a truncated HMAC-SHA256; compared only in constant time.
#[derive(Clone, Copy, Debug)]
pub(crate) struct OnionHeaderMac([u8; ONION_HEADER_MAC_BYTES]);

/// The client's loop tag `t_⋄ = γ_{H+1}` (#834 D6′, H2), uniform, drawn by
/// [`OnionHeader::build`].
///
/// `Eq` and `Hash` are sound here although `γ` compares in constant time: the client must map
/// `t_⋄` to its loop state (D6′), and the only other party that sees `γ_{H+1}` is the guard,
/// which already knows it, so variable-time equality reveals it to nobody.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct OnionLoopTag([u8; ONION_HEADER_MAC_BYTES]);

impl OnionLoopTag {
    /// Wrap the `γ` bytes of a cell arriving at the client.
    pub(crate) const fn new(bytes: [u8; ONION_HEADER_MAC_BYTES]) -> Self {
        Self(bytes)
    }

    /// The `γ` bytes of the tag.
    #[cfg(all(test, rings_native))]
    pub(crate) const fn as_bytes(&self) -> &[u8; ONION_HEADER_MAC_BYTES] {
        &self.0
    }
}

/// The Sphinx header `χ = (α, β, γ)`, exactly `|χ|` bytes whatever the loop length.
#[derive(Clone, Debug)]
pub(crate) struct OnionHeader {
    /// `α`, SEC1 compressed; decoded when peeled.
    alpha: PublicKey<ONION_GROUP_ELEMENT_BYTES>,
    /// `β`.
    routing: Routing,
    /// `γ = MAC(b ‖ β)`.
    mac: OnionHeaderMac,
}

/// One position of a loop as the client builds it: the hop's key and its layer.
pub(crate) struct OnionHeaderHop {
    /// `pk_i`, the delegatee key of the hop at this position.
    pub(crate) public_key: PublicKey<33>,
    /// `λ_i` without `γ_{i+1}`, which the builder computes.
    pub(crate) layer: OnionLayer,
}

/// One validated position: `pk_i ∈ G ∖ {O}` and `λ_i`.
struct OnionRoutePosition {
    /// `pk_i`.
    recipient: NonIdentityPoint<Secp256k1>,
    /// `λ_i` without `γ_{i+1}`.
    layer: OnionLayer,
}

/// The positions `1 … H` of a loop, `1 ≤ H ≤ Ĥ`, every key a point: the outer positions and the
/// last one.
pub(crate) struct OnionHeaderRoute {
    /// Positions `1 … H − 1`, in visiting order.
    outer: Vec<OnionRoutePosition>,
    /// Position `H`, the guard facing the client.
    last: OnionRoutePosition,
}

/// The result of peeling one header value: this hop's layer and the header it forwards.
#[cfg(test)]
pub(crate) struct OnionPeeledHeader {
    /// `λ_i`.
    pub(crate) layer: OnionLayer,
    /// `χ_{i+1}`.
    pub(crate) next: OnionHeader,
}

/// Why a loop's positions are not a route.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub(crate) enum OnionHeaderRouteError {
    /// The loop has no position or more than `Ĥ`.
    #[error("loop of {0} positions is outside 1..={MAX_ONION_LOOP_HOPS}")]
    HopCount(usize),
    /// A hop's public key is not a secp256k1 point.
    #[error("onion hop public key is not a secp256k1 point")]
    PublicKey,
}

/// The blinding factor `z_i` derived for a position is `0 mod n` (probability `2^−256`).
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
#[error("onion header blinding factor is zero")]
pub(crate) struct OnionBlindingError;

/// Why a hop did not peel a header.
#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
pub(crate) enum OnionPeelError {
    /// `α` is not a secp256k1 point, or `γ` does not verify: one outcome, charged alike.
    #[error("onion header is invalid")]
    Invalid,
    /// The derived blinding factor is zero.
    #[error(transparent)]
    Blinding(#[from] OnionBlindingError),
    /// The peeled layer is not a layer encoding.
    #[error(transparent)]
    Layer(#[from] OnionLayerError),
}

/// The per-hop secrets derived from `(α_i, K_i)`, zeroized on drop.
struct OnionHopSecrets {
    /// `ρ_i`.
    stream: Zeroizing<Stream>,
    /// The HMAC key of `γ_i`.
    mac_key: Zeroizing<[u8; MAC_KEY_BYTES]>,
    /// `z_i`.
    blinding: NonZeroScalar<Secp256k1>,
}

impl OnionHeaderMac {
    /// Wrap MAC bytes.
    pub(crate) const fn new(bytes: [u8; ONION_HEADER_MAC_BYTES]) -> Self {
        Self(bytes)
    }

    /// Return the MAC bytes.
    pub(crate) const fn to_bytes(self) -> [u8; ONION_HEADER_MAC_BYTES] {
        self.0
    }
}

impl ConstantTimeEq for OnionHeaderMac {
    /// Equality in time independent of the MAC bytes.
    fn ct_eq(&self, other: &Self) -> Choice {
        self.0.ct_eq(&other.0)
    }
}

impl OnionHeaderRoute {
    /// Accept the positions `hops = (pk_i, λ_i)_{i=1…H}` of a loop, validating every key once.
    ///
    /// # Errors
    ///
    /// [`OnionHeaderRouteError::HopCount`] unless `1 ≤ H ≤ Ĥ`, and
    /// [`OnionHeaderRouteError::PublicKey`] for a key that is not a curve point.
    pub(crate) fn new(hops: Vec<OnionHeaderHop>) -> Result<Self, OnionHeaderRouteError> {
        let count = hops.len();
        let mut positions = hops
            .into_iter()
            .map(|hop| {
                NonIdentityPoint::try_from(hop.public_key)
                    .map(|recipient| OnionRoutePosition {
                        recipient,
                        layer: hop.layer,
                    })
                    .map_err(|_| OnionHeaderRouteError::PublicKey)
            })
            .collect::<Result<Vec<_>, _>>()?;
        positions
            .pop()
            .filter(|_| count <= MAX_ONION_LOOP_HOPS)
            .map(|last| Self {
                outer: positions,
                last,
            })
            .ok_or(OnionHeaderRouteError::HopCount(count))
    }
}

impl OnionHopSecrets {
    /// The key schedule of one position from `α_i` (encoded) and `K_i`.
    ///
    /// # Errors
    ///
    /// [`OnionBlindingError`] if the blinding material reduces to `0 mod n`; the client then
    /// builds again with fresh randomness, a hop drops the cell.
    fn derive(
        alpha: &PublicKey<ONION_GROUP_ELEMENT_BYTES>,
        shared: &[u8; SHARED_SECRET_BYTES],
    ) -> Result<Self, OnionBlindingError> {
        let kdf = hkdf_extract(HEADER_KDF_SALT, &[alpha.0.as_slice(), shared.as_slice()]);
        let blinding =
            NonZeroScalar::from_wide_bytes(&hkdf_expand::<WIDE_SCALAR_BYTES>(&kdf, &[BLIND_INFO]))
                .ok_or(OnionBlindingError)?;
        let mut stream = Zeroizing::new([[0; ONION_LAYER_BYTES]; MAX_ONION_LOOP_HOPS + 1]);
        ChaCha20::new(
            &(*hkdf_expand::<STREAM_KEY_BYTES>(&kdf, &[STREAM_INFO])).into(),
            &[0; 12].into(),
        )
        .apply_keystream(stream.as_flattened_mut());
        Ok(Self {
            stream,
            mac_key: hkdf_expand(&kdf, &[MAC_INFO]),
            blinding,
        })
    }

    /// `γ = HMAC-SHA256_μ(b ‖ β)[0, 16)`: truncation keeps the leftmost 16 of the 32 bytes.
    fn mac(&self, class: OnionLoopClass, routing: &[Block]) -> OnionHeaderMac {
        let mut key = Zeroizing::new(Key::<Hmac<Sha256>>::default());
        key.iter_mut()
            .zip(self.mac_key.iter())
            .for_each(|(slot, byte)| *slot = *byte);
        let mut mac = <Hmac<Sha256> as KeyInit>::new(&key);
        mac.update(&class.mac_label());
        mac.update(routing.as_flattened());
        let mut truncated = [0; ONION_HEADER_MAC_BYTES];
        truncated
            .iter_mut()
            .zip(mac.finalize().into_bytes())
            .for_each(|(slot, byte)| *slot = byte);
        OnionHeaderMac(truncated)
    }

    /// `(β ‖ 0^ℓ) ⊕ ρ` for the hop, or `(λ ‖ β′) ⊕ ρ[0, Ĥ)` for the client: `blocks ⊕ ρ` over
    /// the common prefix.
    fn mask(&self, blocks: &mut [Block]) {
        blocks
            .iter_mut()
            .zip(self.stream.iter())
            .for_each(|(block, mask)| xor_in_place(block.iter_mut(), mask.iter()));
    }
}

impl OnionHeader {
    /// The fields `α ‖ β ‖ γ` of an `|χ|`-byte string, total: the width is the type. `α` is
    /// decoded as a point only when the header is peeled. The callers are a symbol hop's step,
    /// which keeps the next header for its reply block, and the reply block's decoder.
    pub(super) fn of(bytes: &[u8; ONION_HEADER_BYTES]) -> Self {
        let (alpha, routing, mac) = array_refs![
            bytes,
            ONION_GROUP_ELEMENT_BYTES,
            ONION_HEADER_ROUTING_BYTES,
            ONION_HEADER_MAC_BYTES
        ];
        Self {
            alpha: PublicKey(*alpha),
            routing: std::array::from_fn(|slot| {
                *array_ref![routing, slot * ONION_LAYER_BYTES, ONION_LAYER_BYTES]
            }),
            mac: OnionHeaderMac(*mac),
        }
    }

    /// Append the header's encoding `α ‖ β ‖ γ`, exactly `|χ|` bytes, to `bytes`.
    pub(super) fn encode_into(&self, bytes: &mut Vec<u8>) {
        bytes.extend_from_slice(self.alpha.0.as_slice());
        bytes.extend_from_slice(self.routing.as_flattened());
        bytes.extend_from_slice(self.mac.0.as_slice());
    }

    /// The header's encoding `α ‖ β ‖ γ`, exactly `|χ|` bytes.
    #[cfg(test)]
    pub(super) fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(ONION_HEADER_BYTES);
        self.encode_into(&mut bytes);
        bytes
    }

    /// `t_⋄ = γ_{H+1}`: the loop tag, read by the client from the header the guard forwards.
    #[cfg(test)]
    pub(super) const fn loop_tag(&self) -> OnionLoopTag {
        OnionLoopTag(self.mac.0)
    }

    /// Build `χ_1` for a loop of class `b`, drawing `x`, `R` and the client tag `t_⋄` from
    /// `rng`; returns the header and `t_⋄`.
    ///
    /// ```text
    /// x ← Z_n^*,  t_⋄ ← {0,1}^128
    /// for i = 1 … H:     α_i = x_i·G,  K_i = x(x_i·pk_i),  secrets_i,  x_{i+1} = x_i·z_i
    /// for i = 1 … H−1:   φ_i = (φ_{i−1} ‖ 0^ℓ) ⊕ ρ_i[Ĥ−i+1, Ĥ+1)      (tail-aligned blocks)
    /// β_H = ((λ_H(γ_{H+1} = t_⋄) ‖ R) ⊕ ρ_H[0, Ĥ)), then its last H−1 blocks := φ_{H−1}
    /// γ_H = MAC_H(b ‖ β_H)
    /// for i = H−1 … 1:   β_i = (λ_i(γ_{i+1}) ‖ β_{i+1}[0, Ĥ−1)) ⊕ ρ_i[0, Ĥ),  γ_i = MAC_i(b ‖ β_i)
    /// return ((α_1, β_1, γ_1), t_⋄)
    /// ```
    ///
    /// # Errors
    ///
    /// [`OnionBlindingError`] for a zero blinding factor (probability `2^−256`), after which the
    /// client builds again with fresh randomness.
    pub(super) fn build(
        route: &OnionHeaderRoute,
        class: OnionLoopClass,
        rng: &mut (impl CryptoRng + RngCore),
    ) -> Result<(Self, OnionLoopTag), OnionBlindingError> {
        let mut tag = OnionLoopTag([0; ONION_HEADER_MAC_BYTES]);
        rng.fill_bytes(&mut tag.0);
        let (exponent, outer) = route.outer.iter().try_fold(
            (
                NonZeroScalar::random_with_rng(&mut *rng),
                Vec::with_capacity(route.outer.len()),
            ),
            |(exponent, mut schedule), position| {
                let (alpha, secrets) = Self::position(position, &exponent)?;
                let next = &exponent * &secrets.blinding;
                schedule.push((alpha, secrets));
                Ok::<_, OnionBlindingError>((next, schedule))
            },
        )?;
        let (alpha, secrets) = Self::position(&route.last, &exponent)?;
        let filler = outer
            .iter()
            .fold(Vec::<Block>::new(), |mut filler, (_, secrets)| {
                filler.push([0; ONION_LAYER_BYTES]);
                filler
                    .iter_mut()
                    .rev()
                    .zip(secrets.stream.iter().rev())
                    .for_each(|(block, mask)| xor_in_place(block.iter_mut(), mask.iter()));
                filler
            });
        let mut routing: Routing = [[0; ONION_LAYER_BYTES]; MAX_ONION_LOOP_HOPS];
        rng.fill_bytes(routing.as_flattened_mut());
        let [first, ..] = &mut routing;
        *first = *route.last.layer.encode(&OnionHeaderMac(tag.0));
        secrets.mask(&mut routing);
        routing
            .iter_mut()
            .rev()
            .zip(filler.iter().rev())
            .for_each(|(block, filler)| *block = *filler);
        let innermost = Self {
            alpha,
            mac: secrets.mac(class, &routing),
            routing,
        };
        let header = route.outer.iter().zip(outer.iter()).rev().fold(
            innermost,
            |next, (position, (alpha, secrets))| {
                let mut routing = next.routing;
                routing.rotate_right(1);
                let [first, ..] = &mut routing;
                *first = *position.layer.encode(&next.mac);
                secrets.mask(&mut routing);
                Self {
                    alpha: *alpha,
                    mac: secrets.mac(class, &routing),
                    routing,
                }
            },
        );
        Ok((header, tag))
    }

    /// The client's view of one position under exponent `x_i`: `α_i` and the secrets of
    /// `K_i = x(x_i·pk_i)`.
    fn position(
        position: &OnionRoutePosition,
        exponent: &NonZeroScalar<Secp256k1>,
    ) -> Result<(PublicKey<ONION_GROUP_ELEMENT_BYTES>, OnionHopSecrets), OnionBlindingError> {
        let alpha = PublicKey::from(&NonIdentityPoint::generator_mul(exponent));
        let secrets = OnionHopSecrets::derive(&alpha, &position.recipient.shared_secret(exponent))?;
        Ok((alpha, secrets))
    }

    /// Peel `χ_i` of a class-`b` cell under the hop's delegatee key: `(λ_i, χ_{i+1})`, on an
    /// owned copy. A cell peels its header in place with [`peel_in_place`]; this is the same
    /// transition on the header value.
    ///
    /// # Errors
    ///
    /// The [`OnionPeelError`] of [`peel_in_place`].
    #[cfg(test)]
    pub(super) fn peel(
        &self,
        class: OnionLoopClass,
        key: &DelegateeKey,
    ) -> Result<OnionPeeledHeader, OnionPeelError> {
        let mut bytes = [0; ONION_HEADER_BYTES];
        bytes
            .iter_mut()
            .zip(self.to_bytes())
            .for_each(|(slot, byte)| *slot = byte);
        let layer = peel_in_place(&mut bytes, class, std::slice::from_ref(key))?;
        let next = Self::of(&bytes);
        Ok(OnionPeeledHeader { layer, next })
    }
}

/// Peel the header `χ_i` held in `bytes`, replacing it by `χ_{i+1}` in the same bytes, and
/// return `λ_i`. The class is the observed cell length, supplied by the cell parser alone;
/// `keys` lists the hop's active key followed by any retained unexpired previous keys (#1223).
///
/// ```text
/// bytes = α_i ‖ β_i ‖ γ_i
/// α_i ∈ G ∖ {O} ?                        else Invalid   (decoded before any ECDH)
/// K_i = x(d_i·α_i);  secrets_i            else Blinding  (z_i ≡ 0, probability 2^−256)
/// γ_i = MAC_i(b ‖ β_i) ?                 else Invalid   (constant time, before decoding)
/// λ_i ‖ β_{i+1} = (β_i ‖ 0^ℓ) ⊕ ρ_i;  decode λ_i = (layer, γ_{i+1})   else Layer
/// bytes ← z_i·α_i ‖ β_{i+1} ‖ γ_{i+1}      (α_{i+1} ∈ G ∖ {O} by type)
/// ```
///
/// The bytes are rewritten only once every check has passed, so a rejected header leaves them
/// as they arrived. `λ_i` and the pad `ρ_i` live in zeroizing buffers only.
///
/// # Errors
///
/// [`OnionPeelError::Invalid`], [`OnionPeelError::Blinding`] and [`OnionPeelError::Layer`], in
/// that order of checking.
pub(super) fn peel_in_place(
    bytes: &mut [u8; ONION_HEADER_BYTES],
    class: OnionLoopClass,
    keys: &[DelegateeKey],
) -> Result<OnionLayer, OnionPeelError> {
    let (alpha_bytes, rest) = bytes
        .split_first_chunk_mut::<ONION_GROUP_ELEMENT_BYTES>()
        .ok_or(OnionPeelError::Invalid)?;
    let (routing, mac) = rest
        .split_last_chunk_mut::<ONION_HEADER_MAC_BYTES>()
        .ok_or(OnionPeelError::Invalid)?;
    let encoded_alpha = PublicKey(*alpha_bytes);
    let alpha = NonIdentityPoint::<Secp256k1>::try_from(encoded_alpha)
        .map_err(|_| OnionPeelError::Invalid)?;
    let (blocks, []) = routing.as_chunks_mut::<ONION_LAYER_BYTES>() else {
        return Err(OnionPeelError::Invalid);
    };
    let expected_mac = OnionHeaderMac(*mac);
    let secrets = keys
        .iter()
        .find_map(|key| {
            match OnionHopSecrets::derive(&encoded_alpha, &key.diffie_hellman(&alpha)) {
                Ok(secrets) => bool::from(secrets.mac(class, blocks).ct_eq(&expected_mac))
                    .then_some(Ok(secrets)),
                Err(error) => Some(Err(OnionPeelError::from(error))),
            }
        })
        .ok_or(OnionPeelError::Invalid)??;
    let mut padded = Zeroizing::new([[0; ONION_LAYER_BYTES]; MAX_ONION_LOOP_HOPS + 1]);
    padded
        .iter_mut()
        .zip(blocks.iter())
        .for_each(|(block, routing)| *block = *routing);
    secrets.mask(padded.as_mut_slice());
    let [layer, next_routing @ ..] = &*padded;
    let (layer, next_mac) = OnionLayer::decode(layer.as_slice())?;
    *alpha_bytes = PublicKey::<ONION_GROUP_ELEMENT_BYTES>::from(&(&alpha * &secrets.blinding)).0;
    blocks
        .iter_mut()
        .zip(next_routing.iter())
        .for_each(|(block, next)| *block = *next);
    *mac = next_mac.to_bytes();
    Ok(layer)
}