hiss 0.1.0

Static, type-level Noise Protocol Framework with pluggable hardware-backed crypto.
Documentation
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Type-level tokens for Noise handshake patterns.
//!
//! Each token is a zero-sized type representing a single operation
//! in a Noise handshake message. Tokens are composed into messages
//! using [`Cons`]/[`Nil`] type-level lists.
//!
//! # Wire size computation
//!
//! The [`WireSize`] trait computes the exact byte size of a token
//! list at compile time. Only two tokens contribute bytes to the
//! wire:
//!
//! - **`E`** — always `PUBLIC_KEY_SIZE` (plaintext ephemeral).
//! - **`S`** — `PUBLIC_KEY_SIZE` if unkeyed, or
//!   `PUBLIC_KEY_SIZE + TAG_SIZE` if a cipher key has been
//!   established by a preceding DH or PSK token.
//!
//! DH tokens (`Ee`, `Es`, `Se`, `Ss`) and `Psk` contribute zero
//! bytes but transition the symmetric state to *keyed*. This
//! transition is monotonic — once keyed, it stays keyed.
//!
//! The trait tracks both keyed and unkeyed branches simultaneously,
//! using const-`if` to propagate the keyed state through the
//! Cons-list. This avoids unstable `generic_const_exprs`.

use std::marker::PhantomData;

use super::cipher::Cipher;
use crate::curve::Curve;

// ── Handshake tokens ────────────────────────────────────────────

/// Generate and transmit a local ephemeral public key.
pub struct E;

/// Transmit the local static public key (encrypted after key material is established).
pub struct S;

/// DH between both parties' ephemeral keys.
pub struct Ee;

/// DH between initiator's ephemeral and responder's static key.
pub struct Es;

/// DH between initiator's static and responder's ephemeral key.
pub struct Se;

/// DH between both parties' static keys.
pub struct Ss;

/// Inject a pre-shared key into the handshake state.
pub struct Psk;

// ── Type-level list ─────────────────────────────────────────────

/// Terminator for a type-level list.
pub struct Nil;

/// Cons cell: head `H` followed by tail `T`.
pub struct Cons<H, T>(PhantomData<fn() -> (H, T)>);

// ── Message direction ───────────────────────────────────────────

/// Message sent by the initiator (→).
pub struct ToResponder;

/// Message sent by the responder (←).
pub struct ToInitiator;

/// A directed message: a direction paired with a list of tokens.
pub struct Message<Dir, Tokens>(PhantomData<fn() -> (Dir, Tokens)>);

// ── PSK presence (compile-time) ─────────────────────────────────

/// Per-token flag: whether a token is the [`Psk`] token.
///
/// Total over every token (so the fold never needs negative reasoning), this
/// is the leaf of the [`ContainsPsk`] fold that derives a pattern's PSK
/// modifier from its message list rather than a hand-written constant.
#[doc(hidden)]
pub trait TokenFlag {
    /// `true` only for [`Psk`].
    const IS_PSK: bool;
}
impl TokenFlag for E {
    const IS_PSK: bool = false;
}
impl TokenFlag for S {
    const IS_PSK: bool = false;
}
impl TokenFlag for Ee {
    const IS_PSK: bool = false;
}
impl TokenFlag for Es {
    const IS_PSK: bool = false;
}
impl TokenFlag for Se {
    const IS_PSK: bool = false;
}
impl TokenFlag for Ss {
    const IS_PSK: bool = false;
}
impl TokenFlag for Psk {
    const IS_PSK: bool = true;
}

/// Fold over a single message's token list: `true` if any token is [`Psk`].
#[doc(hidden)]
pub trait TokensContainPsk {
    /// Whether the token list contains a [`Psk`] token.
    const VALUE: bool;
}
impl TokensContainPsk for Nil {
    const VALUE: bool = false;
}
impl<Tok: TokenFlag, Rest: TokensContainPsk> TokensContainPsk for Cons<Tok, Rest> {
    const VALUE: bool = Tok::IS_PSK || Rest::VALUE;
}

/// Fold over a pattern's message list: `true` if any message contains a
/// [`Psk`] token.
///
/// Derives a pattern's PSK modifier directly from its `Messages`, so the
/// derived `HAS_PSK` cannot drift out of sync with the token sequence. This is
/// a separate trait from [`TokensContainPsk`] because the two fold over
/// different `Cons` shapes (`Cons<Message, _>` vs `Cons<Token, _>`), which a
/// single trait could not cover without an overlapping impl.
#[doc(hidden)]
pub trait ContainsPsk {
    /// Whether the message list contains a [`Psk`] token.
    const VALUE: bool;
}
impl ContainsPsk for Nil {
    const VALUE: bool = false;
}
impl<Dir, Toks: TokensContainPsk, Rest: ContainsPsk> ContainsPsk
    for Cons<Message<Dir, Toks>, Rest>
{
    const VALUE: bool = <Toks as TokensContainPsk>::VALUE || <Rest as ContainsPsk>::VALUE;
}

// ── Wire size computation ─────────────────────────────────────────

/// Compile-time wire size computation for Noise handshake tokens.
///
/// Computes the total bytes a token or token list contributes to the
/// wire, and tracks whether the symmetric state becomes keyed. Both
/// keyed and unkeyed starting states are computed simultaneously,
/// avoiding unstable const generic chaining.
///
/// The `HAS_PSK` parameter reflects the pattern's PSK modifier — it
/// affects whether the [`E`] token calls `mix_key`, which transitions
/// the symmetric state to keyed.
pub trait WireSize<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> {
    /// Wire bytes when the symmetric state starts unkeyed.
    const SIZE_UNKEYED: usize;
    /// Wire bytes when the symmetric state starts keyed.
    const SIZE_KEYED: usize;
    /// Whether the state is keyed after processing, given an unkeyed start.
    const KEYED_AFTER_UNKEYED: bool;
    /// Whether the state is keyed after processing, given a keyed start.
    const KEYED_AFTER_KEYED: bool;
}

// ── E ──────────────────────────────────────────────────────────────
// Always writes PUBLIC_KEY_SIZE bytes (ephemeral is never encrypted).
// In a PSK pattern, E also calls mix_key(e_pub), transitioning to keyed.

impl<Cu: Curve, Ci: Cipher> WireSize<Cu, Ci, false> for E {
    const SIZE_UNKEYED: usize = Cu::PUBLIC_KEY_SIZE;
    const SIZE_KEYED: usize = Cu::PUBLIC_KEY_SIZE;
    const KEYED_AFTER_UNKEYED: bool = false;
    const KEYED_AFTER_KEYED: bool = true;
}

impl<Cu: Curve, Ci: Cipher> WireSize<Cu, Ci, true> for E {
    const SIZE_UNKEYED: usize = Cu::PUBLIC_KEY_SIZE;
    const SIZE_KEYED: usize = Cu::PUBLIC_KEY_SIZE;
    const KEYED_AFTER_UNKEYED: bool = true; // PSK pattern: E calls mix_key
    const KEYED_AFTER_KEYED: bool = true;
}

// ── S ──────────────────────────────────────────────────────────────
// Plaintext when unkeyed (PUBLIC_KEY_SIZE), encrypted when keyed
// (PUBLIC_KEY_SIZE + TAG_SIZE). S itself does not call mix_key.

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for S {
    const SIZE_UNKEYED: usize = Cu::PUBLIC_KEY_SIZE;
    const SIZE_KEYED: usize = Cu::PUBLIC_KEY_SIZE + Ci::TAG_SIZE;
    const KEYED_AFTER_UNKEYED: bool = false;
    const KEYED_AFTER_KEYED: bool = true;
}

// ── DH tokens (Ee, Es, Se, Ss) ────────────────────────────────────
// Zero wire bytes. All call mix_key, transitioning to keyed.

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Ee {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = true;
    const KEYED_AFTER_KEYED: bool = true;
}

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Es {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = true;
    const KEYED_AFTER_KEYED: bool = true;
}

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Se {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = true;
    const KEYED_AFTER_KEYED: bool = true;
}

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Ss {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = true;
    const KEYED_AFTER_KEYED: bool = true;
}

// ── Psk ────────────────────────────────────────────────────────────
// Zero wire bytes. Calls mix_key_and_hash, transitioning to keyed.

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Psk {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = true;
    const KEYED_AFTER_KEYED: bool = true;
}

// ── Nil ────────────────────────────────────────────────────────────
// Empty list: zero bytes, keyed state preserved.

impl<Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Nil {
    const SIZE_UNKEYED: usize = 0;
    const SIZE_KEYED: usize = 0;
    const KEYED_AFTER_UNKEYED: bool = false;
    const KEYED_AFTER_KEYED: bool = true;
}

// ── Cons ───────────────────────────────────────────────────────────
// Recursive: head's KEYED_AFTER selects the tail's branch via const-if.

impl<H, T, Cu: Curve, Ci: Cipher, const HAS_PSK: bool> WireSize<Cu, Ci, HAS_PSK> for Cons<H, T>
where
    H: WireSize<Cu, Ci, HAS_PSK>,
    T: WireSize<Cu, Ci, HAS_PSK>,
{
    const SIZE_UNKEYED: usize = H::SIZE_UNKEYED
        + if H::KEYED_AFTER_UNKEYED {
            T::SIZE_KEYED
        } else {
            T::SIZE_UNKEYED
        };

    const SIZE_KEYED: usize = H::SIZE_KEYED + T::SIZE_KEYED;

    const KEYED_AFTER_UNKEYED: bool = if H::KEYED_AFTER_UNKEYED {
        T::KEYED_AFTER_KEYED
    } else {
        T::KEYED_AFTER_UNKEYED
    };

    const KEYED_AFTER_KEYED: bool = T::KEYED_AFTER_KEYED;
}

// ── Compile-time message size macro ────────────────────────────────
//
// Computes the total wire size of a Noise handshake message as a true
// `const`, usable in array sizes. The macro walks the token list,
// threading the keyed state through each token, and adds the trailing
// empty-payload tag if the cipher is keyed after all tokens.
//
// Usage:
//
// ```
// const MSG1: usize = noise_message_size!(
//     curve: P256,
//     cipher: ChaChaPoly,
//     has_psk: true,
//     keyed: false,
//     tokens: [E, Es, S, Ss, Psk],
// );
// ```

/// Compute the total wire size of a Noise handshake message at compile
/// time.
///
/// Walks the token list recursively, accumulating sizes and threading
/// the keyed state. Appends `TAG_SIZE` if the cipher is keyed after
/// all tokens (the empty-payload `EncryptAndHash("")` tag).
///
/// The result is a `const`-evaluable `usize` expression.
///
/// # Example
///
/// ```
/// use hiss::noise::curve::P256;
/// use hiss::noise::cipher::ChaChaPoly;
/// use hiss::noise_message_size;
///
/// // IKpsk1 msg1: -> e, es, s, ss, psk
/// const MSG1: usize = noise_message_size!(
///     curve: P256,
///     cipher: ChaChaPoly,
///     has_psk: true,
///     keyed: false,
///     tokens: [E, Es, S, Ss, Psk],
/// );
/// assert_eq!(MSG1, 162);
/// ```
#[macro_export]
macro_rules! noise_message_size {
    // Entry point — delegates to the recursive accumulator.
    (
        curve: $Cu:ty,
        cipher: $Ci:ty,
        has_psk: $has_psk:tt,
        keyed: $keyed:tt,
        tokens: [$($tokens:tt),* $(,)?],
    ) => {
        $crate::noise_message_size!(@accum
            curve: $Cu,
            cipher: $Ci,
            has_psk: $has_psk,
            keyed: $keyed,
            remaining: [$($tokens),*],
            size: 0,
        )
    };

    // Base case — no tokens left. Add the payload tag if keyed.
    (@accum
        curve: $Cu:ty,
        cipher: $Ci:ty,
        has_psk: $_psk:tt,
        keyed: true,
        remaining: [],
        size: $size:expr,
    ) => {
        $size + <$Ci as $crate::noise::cipher::Cipher>::TAG_SIZE
    };
    (@accum
        curve: $Cu:ty,
        cipher: $Ci:ty,
        has_psk: $_psk:tt,
        keyed: false,
        remaining: [],
        size: $size:expr,
    ) => {
        $size
    };

    // ── Recursive cases ──────────────────────────────────────────
    //
    // Each token needs explicit arms because macro_rules! cannot
    // use a macro invocation result as a tt for the next recursion.
    // The keyed-after logic is inlined per token.

    // E — size: PUBLIC_KEY_SIZE. Keyed-after: true if has_psk, else unchanged.
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: true, keyed: $keyed:tt,
        remaining: [E $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: true, keyed: true, remaining: [$($rest),*],
            size: $size + <$Cu as $crate::curve::Curve>::PUBLIC_KEY_SIZE,
        )
    };
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: false, keyed: $keyed:tt,
        remaining: [E $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: false, keyed: $keyed, remaining: [$($rest),*],
            size: $size + <$Cu as $crate::curve::Curve>::PUBLIC_KEY_SIZE,
        )
    };

    // S — size: PUBLIC_KEY_SIZE + TAG_SIZE if keyed, else PUBLIC_KEY_SIZE.
    //     Keyed-after: unchanged.
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: true,
        remaining: [S $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*],
            size: $size + <$Cu as $crate::curve::Curve>::PUBLIC_KEY_SIZE
                        + <$Ci as $crate::noise::cipher::Cipher>::TAG_SIZE,
        )
    };
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: false,
        remaining: [S $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: false, remaining: [$($rest),*],
            size: $size + <$Cu as $crate::curve::Curve>::PUBLIC_KEY_SIZE,
        )
    };

    // DH tokens (Es, Ee, Se, Ss) — zero bytes, always keys the state.
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: $keyed:tt,
        remaining: [Es $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*], size: $size,
        )
    };
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: $keyed:tt,
        remaining: [Ee $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*], size: $size,
        )
    };
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: $keyed:tt,
        remaining: [Se $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*], size: $size,
        )
    };
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: $keyed:tt,
        remaining: [Ss $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*], size: $size,
        )
    };

    // Psk — zero bytes, always keys the state.
    (@accum curve: $Cu:ty, cipher: $Ci:ty, has_psk: $psk:tt, keyed: $keyed:tt,
        remaining: [Psk $(, $rest:tt)*], size: $size:expr,
    ) => {
        $crate::noise_message_size!(@accum curve: $Cu, cipher: $Ci,
            has_psk: $psk, keyed: true, remaining: [$($rest),*], size: $size,
        )
    };
}

#[cfg(test)]
#[allow(clippy::bool_assert_comparison)]
mod tests {
    use super::*;
    use crate::noise::cipher::ChaChaPoly;
    use crate::noise::curve::P256;

    // IKpsk1 token lists (HAS_PSK = true)
    type Msg1Tokens = Cons<E, Cons<Es, Cons<S, Cons<Ss, Cons<Psk, Nil>>>>>; // -> e, es, s, ss, psk
    type Msg2Tokens = Cons<E, Cons<Ee, Cons<Se, Nil>>>; // <- e, ee, se

    #[test]
    fn ikpsk1_msg1_size() {
        // msg1 starts unkeyed. E writes 65 bytes, Es keys the state,
        // S is encrypted (65 + 16 = 81), Ss and Psk write 0 bytes.
        assert_eq!(
            <Msg1Tokens as WireSize<P256, ChaChaPoly, true>>::SIZE_UNKEYED,
            65 + 81 // e_pub + encrypted(s_pub) + tag
        );
        // After msg1, state is keyed.
        assert_eq!(
            <Msg1Tokens as WireSize<P256, ChaChaPoly, true>>::KEYED_AFTER_UNKEYED,
            true
        );
    }

    #[test]
    fn ikpsk1_msg2_size() {
        // msg2 starts keyed (from msg1). E writes 65 bytes.
        assert_eq!(
            <Msg2Tokens as WireSize<P256, ChaChaPoly, true>>::SIZE_KEYED,
            65
        );
        // Still keyed after msg2.
        assert_eq!(
            <Msg2Tokens as WireSize<P256, ChaChaPoly, true>>::KEYED_AFTER_KEYED,
            true
        );
    }

    #[test]
    fn non_psk_e_does_not_key() {
        // Without PSK modifier, E does not transition to keyed.
        assert_eq!(
            <E as WireSize<P256, ChaChaPoly, false>>::KEYED_AFTER_UNKEYED,
            false
        );

        // Hypothetical non-PSK pattern: e, s (both unkeyed).
        type EsThenS = Cons<E, Cons<S, Nil>>;
        assert_eq!(
            <EsThenS as WireSize<P256, ChaChaPoly, false>>::SIZE_UNKEYED,
            65 + 65 // both plaintext
        );
    }

    #[test]
    fn dh_before_s_encrypts_s() {
        // e, es, s — Es keys the state, so S is encrypted.
        type EEsS = Cons<E, Cons<Es, Cons<S, Nil>>>;
        assert_eq!(
            <EEsS as WireSize<P256, ChaChaPoly, false>>::SIZE_UNKEYED,
            65 + (65 + 16) // E plaintext (65), Es (0), S encrypted (65 + 16)
        );
    }

    // ── noise_message_size! macro tests ────────────────────────────

    #[test]
    fn macro_ikpsk1_msg1() {
        // -> e, es, s, ss, psk (starts unkeyed, HAS_PSK = true)
        // e(65) + es(0,keys) + s(65+16) + ss(0) + psk(0) + payload_tag(16) = 162
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: true,
            keyed: false,
            tokens: [E, Es, S, Ss, Psk],
        );
        assert_eq!(SIZE, 162);
    }

    #[test]
    fn macro_ikpsk1_msg2() {
        // <- e, ee, se (starts keyed, HAS_PSK = true)
        // e(65) + ee(0) + se(0) + payload_tag(16) = 81
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: true,
            keyed: true,
            tokens: [E, Ee, Se],
        );
        assert_eq!(SIZE, 81);
    }

    #[test]
    fn macro_n_msg1() {
        // -> e, es (starts unkeyed, no PSK)
        // e(65) + es(0,keys) + payload_tag(16) = 81
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: false,
            keyed: false,
            tokens: [E, Es],
        );
        assert_eq!(SIZE, 81);
    }

    #[test]
    fn macro_k_msg1() {
        // -> e, es, ss (starts unkeyed, no PSK)
        // e(65) + es(0,keys) + ss(0) + payload_tag(16) = 81
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: false,
            keyed: false,
            tokens: [E, Es, Ss],
        );
        assert_eq!(SIZE, 81);
    }

    #[test]
    fn macro_kpsk0_msg1() {
        // -> psk, e, es, ss (starts unkeyed, HAS_PSK = true)
        // psk(0,keys) + e(65) + es(0) + ss(0) + payload_tag(16) = 81
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: true,
            keyed: false,
            tokens: [Psk, E, Es, Ss],
        );
        assert_eq!(SIZE, 81);
    }

    #[test]
    fn macro_non_psk_e_s_unkeyed() {
        // Hypothetical: -> e, s (no PSK, no DH before S)
        // e(65) + s(65, plaintext because unkeyed) = 130
        // No payload tag — state is never keyed.
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: false,
            keyed: false,
            tokens: [E, S],
        );
        assert_eq!(SIZE, 130);
    }

    #[test]
    fn macro_dh_before_s_encrypts_s() {
        // -> e, es, s (no PSK, Es keys the state before S)
        // e(65) + es(0,keys) + s(65+16) + payload_tag(16) = 162
        const SIZE: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: false,
            keyed: false,
            tokens: [E, Es, S],
        );
        assert_eq!(SIZE, 162);
    }

    #[test]
    fn macro_sizes_are_const() {
        // Prove that the macro output is usable as an array size.
        const MSG1: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: true,
            keyed: false,
            tokens: [E, Es, S, Ss, Psk],
        );
        const MSG2: usize = noise_message_size!(
            curve: P256,
            cipher: ChaChaPoly,
            has_psk: true,
            keyed: true,
            tokens: [E, Ee, Se],
        );
        let _buf1: [u8; MSG1] = [0u8; 162];
        let _buf2: [u8; MSG2] = [0u8; 81];
    }

    #[test]
    fn macro_matches_wire_size_trait() {
        // Cross-check: macro output must match the WireSize trait computation
        // for every pattern we support.

        // IKpsk1 msg1 (WireSize: SIZE_UNKEYED + payload_tag)
        let trait_msg1 =
            <Msg1Tokens as WireSize<P256, ChaChaPoly, true>>::SIZE_UNKEYED + ChaChaPoly::TAG_SIZE; // payload tag (keyed after all tokens)
        const MACRO_MSG1: usize = noise_message_size!(
            curve: P256, cipher: ChaChaPoly, has_psk: true, keyed: false,
            tokens: [E, Es, S, Ss, Psk],
        );
        assert_eq!(MACRO_MSG1, trait_msg1);

        // IKpsk1 msg2 (WireSize: SIZE_KEYED + payload_tag)
        let trait_msg2 =
            <Msg2Tokens as WireSize<P256, ChaChaPoly, true>>::SIZE_KEYED + ChaChaPoly::TAG_SIZE;
        const MACRO_MSG2: usize = noise_message_size!(
            curve: P256, cipher: ChaChaPoly, has_psk: true, keyed: true,
            tokens: [E, Ee, Se],
        );
        assert_eq!(MACRO_MSG2, trait_msg2);
    }
}