subduction_ephemeral 0.12.0

Ephemeral (non-persisted) messaging for Subduction
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
//! Comprehensive property tests for the ephemeral message codec.
//!
//! `subduction_ephemeral` exposes two wire-level types:
//!
//! - [`EphemeralPayload`] — `Signed<EphemeralPayload>` with `SUE\x00`
//!   schema and discriminant `0x00`. Fields: `topic(32) + nonce(8) +
//!   timestamp(8) + bijou64(payload_len) + payload[len]`. The blob
//!   length prefix is `bijou64`-encoded, so this is the most
//!   variable-length-aware codec in the system.
//! - [`EphemeralMessage`] — outer envelope dispatching on the byte
//!   after the schema. `0x00` → `Signed<EphemeralPayload>` (the bytes
//!   *are* the Signed wire format), `0x01`–`0x03` → unsigned topic
//!   list control messages.
//!
//! What we want to hold:
//!
//! 1. **Round-trip** for every shape, including empty payloads and
//!    payloads at bijou64 tier boundaries.
//! 2. **No panics** on arbitrary bytes.
//! 3. **Truncation** at any prefix length yields a structured error.
//! 4. **Trailing bytes** are rejected by control-tag messages
//!    (`SizeMismatch`) but truncated by `Signed::try_decode`.
//! 5. **Schema/tag tampering** yields `InvalidSchema` /
//!    `InvalidEnumTag`, never panic.
//! 6. **Empty topic list** is rejected (the [`NonEmpty`] invariant).

#![cfg(feature = "bolero")]
#![allow(clippy::panic, clippy::expect_used, clippy::indexing_slicing)]

use std::vec::Vec;

use ed25519_dalek::{Signer as _, SigningKey};
use nonempty::NonEmpty;
use sedimentree_core::codec::{
    decode::{Decode, DecodeFields},
    encode::{Encode, EncodeFields},
    error::{DecodeError, InvalidSchema, SizeMismatch},
    schema::Schema,
};
use subduction_core::timestamp::TimestampSeconds;
use subduction_crypto::signed::{SCHEMA_SIZE, SIGNATURE_SIZE, Signed, VERIFYING_KEY_SIZE};
use subduction_ephemeral::{
    message::{EPHEMERAL_SCHEMA, EphemeralMessage, EphemeralPayload},
    topic::Topic,
};

// ── Generators ────────────────────────────────────────────────────────
//
// EphemeralPayload doesn't currently derive Arbitrary. Build it from
// arbitrary primitives directly. The payload length is bounded so
// proptests don't degenerate into huge buffers.

#[derive(Debug, Clone, arbitrary::Arbitrary)]
struct GenInputs {
    topic_bytes: [u8; 32],
    nonce: u64,
    /// Capped at 1024 by the payload generator to keep test runs fast.
    timestamp_secs: u64,
    /// Bounded payload to keep proptests fast. We cover the full range
    /// of bijou64 encoded lengths (1–9 bytes) at lengths 0..255 and
    /// 248..1024 — the tier-0/tier-1 boundary is 248.
    payload_seed: u16,
    payload_filler: u8,
}

fn build_payload(inputs: &GenInputs) -> EphemeralPayload {
    let len = (inputs.payload_seed as usize) % 1024;
    EphemeralPayload {
        id: Topic::new(inputs.topic_bytes),
        nonce: inputs.nonce,
        timestamp: TimestampSeconds::new(inputs.timestamp_secs),
        payload: vec![inputs.payload_filler; len],
    }
}

fn seal_sync<T: Schema + EncodeFields + DecodeFields>(key_bytes: [u8; 32], payload: &T) -> Vec<u8> {
    let signing_key = SigningKey::from_bytes(&key_bytes);
    let issuer = signing_key.verifying_key();

    let disc_size = usize::from(T::DISCRIMINANT.is_some());
    let total_size =
        SCHEMA_SIZE + disc_size + VERIFYING_KEY_SIZE + payload.fields_size() + SIGNATURE_SIZE;
    let mut bytes = Vec::with_capacity(total_size);

    bytes.extend_from_slice(&T::SCHEMA);
    if let Some(disc) = T::DISCRIMINANT {
        bytes.push(disc);
    }
    bytes.extend_from_slice(issuer.as_bytes());
    payload.encode_fields(&mut bytes);

    let signature = signing_key.sign(&bytes);
    bytes.extend_from_slice(&signature.to_bytes());

    bytes
}

// ════════════════════════════════════════════════════════════════════════
// EphemeralPayload (the signable inner type)
// ════════════════════════════════════════════════════════════════════════

#[test]
fn ephemeral_payload_fields_round_trip() {
    bolero::check!()
        .with_arbitrary::<GenInputs>()
        .for_each(|inputs| {
            let payload = build_payload(inputs);
            let mut buf = Vec::new();
            payload.encode_fields(&mut buf);
            match EphemeralPayload::try_decode_fields(&buf) {
                Ok((decoded, consumed)) => {
                    assert_eq!(decoded, payload);
                    assert_eq!(consumed, buf.len(), "consumed must equal canonical size");
                    assert_eq!(consumed, decoded.fields_size());
                }
                Err(e) => panic!("decode must succeed for valid EphemeralPayload bytes: {e}"),
            }
        });
}

#[test]
fn ephemeral_payload_fields_size_matches_encoded_length() {
    bolero::check!()
        .with_arbitrary::<GenInputs>()
        .for_each(|inputs| {
            let payload = build_payload(inputs);
            let mut buf = Vec::new();
            payload.encode_fields(&mut buf);
            assert_eq!(buf.len(), payload.fields_size());
        });
}

#[test]
fn ephemeral_payload_decode_fields_does_not_panic() {
    bolero::check!()
        .with_arbitrary::<Vec<u8>>()
        .for_each(|bytes| {
            let _result = EphemeralPayload::try_decode_fields(bytes);
        });
}

#[test]
fn ephemeral_payload_round_trip_with_trailing_bytes() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, Vec<u8>)>()
        .for_each(|(inputs, trailing)| {
            let payload = build_payload(inputs);
            let mut buf = Vec::new();
            payload.encode_fields(&mut buf);
            let canonical_len = buf.len();
            buf.extend_from_slice(trailing);

            match EphemeralPayload::try_decode_fields(&buf) {
                Ok((decoded, consumed)) => {
                    assert_eq!(decoded, payload);
                    assert_eq!(consumed, canonical_len);
                }
                Err(e) => panic!("decode must accept canonical bytes + trailing: {e}"),
            }
        });
}

#[test]
fn ephemeral_payload_truncation_does_not_panic() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, u16)>()
        .for_each(|(inputs, drop_count)| {
            let payload = build_payload(inputs);
            let mut buf = Vec::new();
            payload.encode_fields(&mut buf);
            let new_len = buf.len().saturating_sub(*drop_count as usize);
            buf.truncate(new_len);
            let _result = EphemeralPayload::try_decode_fields(&buf);
        });
}

// ── Signed<EphemeralPayload> ──────────────────────────────────────────

#[test]
fn ephemeral_signed_round_trip() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32])>()
        .for_each(|(inputs, key_bytes)| {
            let payload = build_payload(inputs);
            let bytes = seal_sync(*key_bytes, &payload);
            let signed = Signed::<EphemeralPayload>::try_decode(&bytes).expect("decode");
            let verified = signed.try_verify().expect("verify");
            assert_eq!(verified.payload(), &payload);
        });
}

#[test]
fn ephemeral_signed_decode_random_bytes_no_panic() {
    bolero::check!()
        .with_arbitrary::<Vec<u8>>()
        .for_each(|bytes| {
            let _result = Signed::<EphemeralPayload>::try_decode(bytes);
        });
}

#[test]
fn ephemeral_signed_truncates_trailing_bytes() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32], Vec<u8>)>()
        .for_each(|(inputs, key_bytes, trailing)| {
            let payload = build_payload(inputs);
            let canonical = seal_sync(*key_bytes, &payload);
            let canonical_len = canonical.len();
            let mut with_trailing = canonical.clone();
            with_trailing.extend_from_slice(trailing);

            let signed = Signed::<EphemeralPayload>::try_decode(&with_trailing)
                .expect("decode with trailing");
            assert_eq!(signed.as_bytes().len(), canonical_len);
            signed.try_verify().expect("verify after truncation");
        });
}

#[test]
fn ephemeral_signed_truncation_does_not_panic() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32], u16)>()
        .for_each(|(inputs, key_bytes, drop_count)| {
            let payload = build_payload(inputs);
            let mut bytes = seal_sync(*key_bytes, &payload);
            let new_len = bytes.len().saturating_sub(*drop_count as usize);
            bytes.truncate(new_len);
            let _result = Signed::<EphemeralPayload>::try_decode(&bytes);
        });
}

#[test]
fn ephemeral_signed_bit_flip_breaks_verification() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32], u32, u8)>()
        .for_each(|(inputs, key_bytes, byte_idx_seed, bit_idx_seed)| {
            let payload = build_payload(inputs);
            let mut bytes = seal_sync(*key_bytes, &payload);
            if bytes.is_empty() {
                return;
            }
            let byte_idx = (*byte_idx_seed as usize) % bytes.len();
            let bit_idx = bit_idx_seed % 8;
            bytes[byte_idx] ^= 1 << bit_idx;

            if let Ok(signed) = Signed::<EphemeralPayload>::try_decode(&bytes) {
                assert!(
                    signed.try_verify().is_err(),
                    "tampered Ephemeral bytes must not verify"
                );
            }
        });
}

// ════════════════════════════════════════════════════════════════════════
// EphemeralMessage envelope
// ════════════════════════════════════════════════════════════════════════

/// Wire bytes from a sealed `Signed<EphemeralPayload>` decode as
/// `EphemeralMessage::Ephemeral`.
#[test]
fn ephemeral_message_decodes_signed_ephemeral() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32])>()
        .for_each(|(inputs, key_bytes)| {
            let payload = build_payload(inputs);
            let bytes = seal_sync(*key_bytes, &payload);
            match EphemeralMessage::try_decode(&bytes) {
                Ok(EphemeralMessage::Ephemeral(signed)) => {
                    let verified = signed.try_verify().expect("verify");
                    assert_eq!(verified.payload(), &payload);
                }
                Ok(other) => panic!("expected Ephemeral, got {other:?}"),
                Err(e) => panic!("decode must succeed: {e}"),
            }
        });
}

#[test]
fn ephemeral_message_subscribe_round_trip() {
    bolero::check!()
        .with_arbitrary::<(Vec<[u8; 32]>, [u8; 32])>()
        .for_each(|(extra_topics, head_topic)| {
            let mut topics = NonEmpty::new(Topic::new(*head_topic));
            for t in extra_topics {
                topics.push(Topic::new(*t));
            }
            // u16 limits the count.
            if topics.len() > u16::MAX as usize {
                return;
            }
            let msg = EphemeralMessage::Subscribe { topics };
            let encoded = msg.encode();
            let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");
            assert_eq!(decoded, msg);
        });
}

#[test]
fn ephemeral_message_unsubscribe_round_trip() {
    bolero::check!()
        .with_arbitrary::<(Vec<[u8; 32]>, [u8; 32])>()
        .for_each(|(extra_topics, head_topic)| {
            let mut topics = NonEmpty::new(Topic::new(*head_topic));
            for t in extra_topics {
                topics.push(Topic::new(*t));
            }
            if topics.len() > u16::MAX as usize {
                return;
            }
            let msg = EphemeralMessage::Unsubscribe { topics };
            let encoded = msg.encode();
            let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");
            assert_eq!(decoded, msg);
        });
}

#[test]
fn ephemeral_message_subscribe_rejected_round_trip() {
    bolero::check!()
        .with_arbitrary::<(Vec<[u8; 32]>, [u8; 32])>()
        .for_each(|(extra_topics, head_topic)| {
            let mut topics = NonEmpty::new(Topic::new(*head_topic));
            for t in extra_topics {
                topics.push(Topic::new(*t));
            }
            if topics.len() > u16::MAX as usize {
                return;
            }
            let msg = EphemeralMessage::SubscribeRejected { topics };
            let encoded = msg.encode();
            let decoded = EphemeralMessage::try_decode(&encoded).expect("decode");
            assert_eq!(decoded, msg);
        });
}

#[test]
fn ephemeral_message_random_bytes_never_panic() {
    bolero::check!()
        .with_arbitrary::<Vec<u8>>()
        .for_each(|bytes| {
            let _result = EphemeralMessage::try_decode(bytes);
        });
}

#[test]
fn ephemeral_message_wrong_schema_rejected() {
    bolero::check!()
        .with_arbitrary::<([u8; 4], [u8; 32])>()
        .for_each(|(bad_schema, topic)| {
            if bad_schema == &EPHEMERAL_SCHEMA {
                return;
            }
            let topics = NonEmpty::new(Topic::new(*topic));
            let msg = EphemeralMessage::Subscribe { topics };
            let mut encoded = msg.encode();
            encoded[..4].copy_from_slice(bad_schema);
            let result = EphemeralMessage::try_decode(&encoded);
            assert!(
                matches!(
                    result,
                    Err(DecodeError::InvalidSchema(InvalidSchema { .. }))
                ),
                "wrong schema must yield InvalidSchema, got {result:?}"
            );
        });
}

#[test]
fn ephemeral_message_unknown_tag_rejected() {
    bolero::check!().with_arbitrary::<u8>().for_each(|bad_tag| {
        // Known tags are 0x00 (Ephemeral discriminant), 0x01, 0x02, 0x03.
        if matches!(*bad_tag, 0x00..=0x03) {
            return;
        }
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&EPHEMERAL_SCHEMA);
        bytes.push(*bad_tag);

        let result = EphemeralMessage::try_decode(&bytes);
        assert!(
            matches!(result, Err(DecodeError::InvalidEnumTag(_))),
            "unknown tag {bad_tag:#04x} must yield InvalidEnumTag, got {result:?}"
        );
    });
}

/// Trailing bytes after a control-tag message yield `SizeMismatch`.
#[test]
fn ephemeral_message_control_trailing_bytes_rejected() {
    bolero::check!()
        .with_arbitrary::<([u8; 32], Vec<u8>)>()
        .for_each(|(topic, trailing)| {
            if trailing.is_empty() {
                return;
            }
            let topics = NonEmpty::new(Topic::new(*topic));
            let msg = EphemeralMessage::Subscribe { topics };
            let mut encoded = msg.encode();
            encoded.extend_from_slice(trailing);

            let result = EphemeralMessage::try_decode(&encoded);
            assert!(
                matches!(result, Err(DecodeError::SizeMismatch(SizeMismatch { .. }))),
                "trailing bytes on control message must yield SizeMismatch, got {result:?}"
            );
        });
}

/// An empty topic list (count=0) is rejected at `decode_topic_list`.
///
/// The buffer is padded past the outer `payload.len() >= min_size` guard
/// (which is `count(2) + topic(32) = 34` bytes) so the inner `count == 0`
/// check is the one that fires. The error's `type_name` is the discriminator.
#[test]
fn ephemeral_message_empty_topic_list_rejected() {
    for tag in [0x01u8, 0x02u8, 0x03u8] {
        let mut buf = Vec::new();
        buf.extend_from_slice(&EPHEMERAL_SCHEMA);
        buf.push(tag);
        buf.extend_from_slice(&0_u16.to_be_bytes()); // count = 0
        buf.extend_from_slice(&[0u8; 32]); // padding to reach min_size

        let result = EphemeralMessage::try_decode(&buf);
        assert!(
            matches!(
                result,
                Err(DecodeError::MessageTooShort {
                    type_name: "EphemeralTopicList",
                    ..
                })
            ),
            "empty topic list (tag {tag:#04x}) must be rejected by decode_topic_list, got {result:?}"
        );
    }
}

#[test]
fn ephemeral_message_truncation_does_not_panic() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32], u16)>()
        .for_each(|(inputs, key_bytes, drop_count)| {
            let payload = build_payload(inputs);
            let mut bytes = seal_sync(*key_bytes, &payload);
            let new_len = bytes.len().saturating_sub(*drop_count as usize);
            bytes.truncate(new_len);
            let _result = EphemeralMessage::try_decode(&bytes);
        });
}

#[test]
fn ephemeral_message_bit_flip_does_not_panic() {
    bolero::check!()
        .with_arbitrary::<(GenInputs, [u8; 32], u32, u8)>()
        .for_each(|(inputs, key_bytes, byte_idx_seed, bit_idx_seed)| {
            let payload = build_payload(inputs);
            let mut bytes = seal_sync(*key_bytes, &payload);
            if bytes.is_empty() {
                return;
            }
            let byte_idx = (*byte_idx_seed as usize) % bytes.len();
            let bit_idx = bit_idx_seed % 8;
            bytes[byte_idx] ^= 1 << bit_idx;
            let _result = EphemeralMessage::try_decode(&bytes);
        });
}

/// The `EPHEMERAL_SCHEMA` named const and the trait-default `SCHEMA`
/// (composed from `PREFIX`, `TYPE_BYTE`, `VERSION`) must agree.
#[test]
fn ephemeral_schema_constants() {
    assert_eq!(EPHEMERAL_SCHEMA, EphemeralPayload::SCHEMA);
}