foundation-ur 0.4.0

Implementation of Blockchain Common's Uniform Resources (UR) standard, with static memory allocation for embedded devices while also allowing to use dynamic memory allocation for platforms with more resources.
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
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-FileCopyrightText: © 2020 Dominik Spicher <dominikspicher@gmail.com>
// SPDX-License-Identifier: MIT

//! Decoder.

use core::fmt;

use crate::{
    collections::{Deque, Set, Vec},
    fountain::part::MessageDescription,
    fountain::{
        chooser,
        chooser::BaseFragmentChooser,
        part::{IndexedPart, Part},
    },
};

/// A [`decoder`](BaseDecoder) that uses [`alloc`] collection types.
#[cfg(feature = "alloc")]
pub type Decoder = BaseDecoder<Alloc>;

/// A [`decoder`](BaseDecoder) that uses fixed-capacity collection types.
pub type HeaplessDecoder<
    const MAX_MESSAGE_LEN: usize,
    const MAX_MIXED_PARTS: usize,
    const MAX_FRAGMENT_LEN: usize,
    const MAX_SEQUENCE_COUNT: usize,
    const QUEUE_SIZE: usize,
> = BaseDecoder<
    Heapless<MAX_MESSAGE_LEN, MAX_MIXED_PARTS, MAX_FRAGMENT_LEN, MAX_SEQUENCE_COUNT, QUEUE_SIZE>,
>;

impl<
        const MAX_MESSAGE_LEN: usize,
        const MAX_MIXED_PARTS: usize,
        const MAX_FRAGMENT_LEN: usize,
        const MAX_SEQUENCE_COUNT: usize,
        const QUEUE_SIZE: usize,
    >
    HeaplessDecoder<
        MAX_MESSAGE_LEN,
        MAX_MIXED_PARTS,
        MAX_FRAGMENT_LEN,
        MAX_SEQUENCE_COUNT,
        QUEUE_SIZE,
    >
{
    /// Constructs a new [`HeaplessDecoder`].
    pub const fn new() -> Self {
        Self {
            message: heapless::Vec::new(),
            mixed_parts: heapless::Vec::new(),
            received: heapless::IndexSet::new(),
            queue: heapless::Deque::new(),
            fragment_chooser: chooser::HeaplessFragmentChooser::new(),
            message_description: None,
        }
    }
}

/// A decoder capable of receiving and recombining fountain-encoded transmissions.
///
/// # Examples
///
/// See the [`crate::fountain`] module documentation for an example.
#[derive(Default)]
pub struct BaseDecoder<T: Types> {
    message: T::Message,
    mixed_parts: T::MixedParts,
    received: T::Indexes,
    queue: T::Queue,
    fragment_chooser: BaseFragmentChooser<T::Chooser>,
    message_description: Option<MessageDescription>,
}

impl<T: Types> BaseDecoder<T> {
    /// Receives a fountain-encoded part into the decoder.
    ///
    /// # Examples
    ///
    /// See the [`crate::fountain`] module documentation for an example.
    ///
    /// # Errors
    ///
    /// If the part would fail [`validate`] because it is inconsistent
    /// with previously received parts, an error will be returned.
    ///
    /// [`validate`]: BaseDecoder::is_part_consistent
    pub fn receive(&mut self, part: &Part) -> Result<bool, Error> {
        if self.is_complete() {
            return Ok(false);
        }

        if !part.is_valid() {
            return Err(Error::InvalidPart);
        }

        if self.is_empty() {
            let message_len = part.data.len() * usize::try_from(part.sequence_count).unwrap();
            if self.message.try_resize(message_len, 0).is_err() {
                return Err(Error::NotEnoughSpace {
                    needed: message_len,
                    capacity: self.message.capacity(),
                });
            }
            self.message_description = Some(part.to_message_description());
        } else if !self.is_part_consistent(part) {
            return Err(Error::InconsistentPart {
                received: part.to_message_description(),
                expected: self.message_description.clone().unwrap(),
            });
        }

        let indexes = self.fragment_chooser.choose_fragments(
            part.sequence,
            part.sequence_count,
            part.checksum,
        );

        let mut data = T::Fragment::default();
        if data.try_extend_from_slice(part.data).is_err() {
            return Err(Error::NotEnoughSpace {
                needed: part.data.len(),
                capacity: data.capacity(),
            });
        }

        let part = IndexedPart::new(data, indexes);
        self.queue.push_back(part);

        while !self.is_complete() && !self.queue.is_empty() {
            let part = self.queue.pop_front().unwrap();
            if part.is_simple() {
                self.process_simple(&part)?;
            } else {
                self.process_mixed(part);
            }
        }
        Ok(!self.is_complete())
    }

    /// Checks whether a [`Part`] is receivable by the decoder.
    ///
    /// This can fail if other parts were previously received whose
    /// metadata (such as number of segments) is inconsistent with the
    /// present [`Part`]. Note that a fresh decoder will always return
    /// false here.
    #[must_use]
    pub fn is_part_consistent(&self, part: &Part) -> bool {
        match self.message_description {
            Some(ref message_description) => part == message_description,
            None => false,
        }
    }

    /// If [`complete`], returns the decoded message, `None` otherwise.
    ///
    /// # Errors
    ///
    /// If an inconsistent internal state is detected, an error will be returned.
    ///
    /// # Examples
    ///
    /// See the [`crate::fountain`] module documentation for an example.
    ///
    /// [`complete`]: BaseDecoder::is_complete
    pub fn message(&self) -> Result<Option<&[u8]>, Error> {
        if self.is_complete() {
            if self.message[self.message_description.as_ref().unwrap().message_length..]
                .iter()
                .any(|&b| b != 0)
            {
                return Err(Error::InvalidPadding);
            }

            Ok(Some(
                &self.message[..self.message_description.as_ref().unwrap().message_length],
            ))
        } else {
            Ok(None)
        }
    }

    /// Returns whether the decoder is complete and hence the message available.
    ///
    /// # Examples
    ///
    /// See the [`crate::fountain`] module documentation for an example.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        if self.is_empty() {
            return false;
        }

        self.received.len()
            == self
                .message_description
                .as_ref()
                .unwrap()
                .sequence_count
                .try_into()
                .unwrap()
    }

    /// Calculate estimated percentage of completion.
    pub fn estimated_percent_complete(&self) -> f64 {
        if self.is_complete() {
            return 1.0;
        }

        if self.is_empty() {
            return 0.0;
        }

        let estimated_input_parts =
            f64::from(self.message_description.as_ref().unwrap().sequence_count) * 1.75;
        let received_parts = u32::try_from(self.received.len()).unwrap();
        f64::min(0.99, f64::from(received_parts) / estimated_input_parts)
    }

    /// Returns `true` if the decoder doesn't contain any data.
    ///
    /// Once a part is successfully [received](Self::receive) this method will
    /// return `false`.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.message.is_empty()
            && self.mixed_parts.is_empty()
            && self.received.is_empty()
            && self.queue.is_empty()
            && self.message_description.is_none()
    }

    /// Clear the decoder so that it can be used again.
    pub fn clear(&mut self) {
        self.message.clear();
        self.mixed_parts.clear();
        self.received.clear();
        self.queue.clear();
        self.message_description = None;

        debug_assert!(self.is_empty());
    }

    fn reduce_mixed(&mut self, part: &IndexedPart<T::Fragment, T::Indexes>) {
        self.mixed_parts.retain_mut(|mixed_part| {
            mixed_part.reduce(part);

            if mixed_part.is_simple() {
                self.queue.push_back(mixed_part.clone());
            }

            !mixed_part.is_simple()
        });
    }

    fn process_simple(&mut self, part: &IndexedPart<T::Fragment, T::Indexes>) -> Result<(), Error> {
        let index = *part.indexes.first().unwrap();
        if self.received.contains(&index) {
            return Ok(());
        }

        self.reduce_mixed(part);

        let offset = index * self.message_description.as_ref().unwrap().fragment_length;
        self.message[offset..offset + self.message_description.as_ref().unwrap().fragment_length]
            .copy_from_slice(&part.data);
        self.received
            .insert(index)
            .map_err(|_| Error::TooManyFragments)?;

        Ok(())
    }

    fn process_mixed(&mut self, mut part: IndexedPart<T::Fragment, T::Indexes>) {
        for mixed_part in (&self.mixed_parts as &[IndexedPart<T::Fragment, T::Indexes>]).iter() {
            if part.indexes == mixed_part.indexes {
                return;
            }
        }

        // Reduce this part by all simple parts.
        for &index in self.received.iter() {
            let offset = index * self.message_description.as_ref().unwrap().fragment_length;
            part.reduce_by_simple(
                &self.message
                    [offset..offset + self.message_description.as_ref().unwrap().fragment_length],
                index,
            );
            if part.is_simple() {
                break;
            }
        }

        // Then reduce this part by all the mixed parts.
        if !part.is_simple() {
            for mixed_part in self.mixed_parts.iter() {
                part.reduce(mixed_part);
                if part.is_simple() {
                    break;
                }
            }
        }

        if part.is_simple() {
            self.queue.push_back(part);
        } else {
            self.reduce_mixed(&part);
            self.mixed_parts.try_push(part).ok();
        }
    }
}

/// Types for [`BaseDecoder`].
pub trait Types: Default {
    /// Decoded message buffer.
    type Message: Vec<u8>;

    /// Mixed parts storage.
    type MixedParts: Vec<IndexedPart<Self::Fragment, Self::Indexes>>;

    /// Fragment buffer.
    type Fragment: Clone + Vec<u8>;

    /// Indexes storage.
    type Indexes: PartialEq + Set<usize>;

    /// Part queue.
    type Queue: Deque<IndexedPart<Self::Fragment, Self::Indexes>>;

    /// Fragment chooser types.
    type Chooser: chooser::Types;
}

/// [`alloc`] types for [`BaseDecoder`].
#[derive(Default)]
#[cfg(feature = "alloc")]
pub struct Alloc;

#[cfg(feature = "alloc")]
impl Types for Alloc {
    type Message = alloc::vec::Vec<u8>;
    type MixedParts =
        alloc::vec::Vec<IndexedPart<alloc::vec::Vec<u8>, alloc::collections::BTreeSet<usize>>>;
    type Fragment = alloc::vec::Vec<u8>;
    type Indexes = alloc::collections::BTreeSet<usize>;
    type Queue = alloc::collections::VecDeque<
        IndexedPart<alloc::vec::Vec<u8>, alloc::collections::BTreeSet<usize>>,
    >;
    type Chooser = chooser::Alloc;
}

/// [`heapless`] types for [`BaseDecoder`].
#[derive(Default)]
pub struct Heapless<
    const MAX_MESSAGE_LEN: usize,
    const MAX_MIXED_PARTS: usize,
    const MAX_FRAGMENT_LEN: usize,
    const MAX_SEQUENCE_COUNT: usize,
    const QUEUE_SIZE: usize,
>;

impl<
        const MAX_MESSAGE_LEN: usize,
        const MAX_MIXED_PARTS: usize,
        const MAX_FRAGMENT_LEN: usize,
        const MAX_SEQUENCE_COUNT: usize,
        const QUEUE_SIZE: usize,
    > Types
    for Heapless<MAX_MESSAGE_LEN, MAX_MIXED_PARTS, MAX_FRAGMENT_LEN, MAX_SEQUENCE_COUNT, QUEUE_SIZE>
{
    type Message = heapless::Vec<u8, MAX_MESSAGE_LEN>;

    type MixedParts = heapless::Vec<
        IndexedPart<
            heapless::Vec<u8, MAX_FRAGMENT_LEN>,
            heapless::FnvIndexSet<usize, MAX_SEQUENCE_COUNT>,
        >,
        MAX_MIXED_PARTS,
    >;

    type Fragment = heapless::Vec<u8, MAX_FRAGMENT_LEN>;

    type Indexes = heapless::FnvIndexSet<usize, MAX_SEQUENCE_COUNT>;

    type Queue = heapless::Deque<
        IndexedPart<
            heapless::Vec<u8, MAX_FRAGMENT_LEN>,
            heapless::FnvIndexSet<usize, MAX_SEQUENCE_COUNT>,
        >,
        QUEUE_SIZE,
    >;

    type Chooser = chooser::Heapless<MAX_SEQUENCE_COUNT>;
}

/// Errors that can happen during decoding.
#[derive(Debug)]
pub enum Error {
    /// The padding is invalid.
    InvalidPadding,
    /// The received part is inconsistent with the previously received ones.
    InconsistentPart {
        /// The description of the message from the received part.
        received: MessageDescription,
        /// The expected description of the message originated from the previous parts scanned.
        expected: MessageDescription,
    },
    /// The received part is empty.
    InvalidPart,
    /// Not enough space to receive the part.
    NotEnoughSpace {
        /// Needed space.
        needed: usize,
        /// Current capacity.
        capacity: usize,
    },
    /// Too many fragments.
    TooManyFragments,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidPadding => write!(f, "Invalid padding")?,
            Error::InconsistentPart { received, expected } => {
                write!(f, "Inconsistent part: ")?;

                if received.sequence_count != expected.sequence_count {
                    write!(
                        f,
                        "sequence count mismatch (received {}, expected {}). ",
                        received.sequence_count, expected.sequence_count
                    )?;
                }

                if received.message_length != expected.message_length {
                    write!(
                        f,
                        "message length mismatch (received {}, expected {}). ",
                        received.message_length, expected.message_length
                    )?;
                }

                if received.checksum != expected.checksum {
                    write!(
                        f,
                        "checksum mismatch (received {:X}, expected {:X}). ",
                        received.checksum, expected.checksum
                    )?;
                }

                if received.fragment_length != expected.fragment_length {
                    write!(
                        f,
                        "checksum mismatch (received {:X}, expected {:X}). ",
                        received.fragment_length, expected.fragment_length
                    )?;
                }
            }
            Error::InvalidPart => write!(f, "The scanned part is empty")?,
            Error::NotEnoughSpace { needed, capacity } => {
                write!(f, "Not enough space: needed {needed}, capacity {capacity}")?
            }
            Error::TooManyFragments => write!(f, "Too many fragments for the current message")?,
        };
        Ok(())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[cfg(test)]
#[cfg(feature = "alloc")]
pub mod tests {
    use super::*;
    use crate::fountain::fragment_length;
    use crate::{fountain::Encoder, xoshiro::test_utils::make_message};

    const MESSAGE_SIZE: usize = 32767;
    const MAX_FRAGMENT_LEN: usize = 1000;
    const MAX_SEQUENCE_COUNT: usize = 64;
    const MAX_MESSAGE_SIZE: usize =
        fragment_length(MESSAGE_SIZE, MAX_FRAGMENT_LEN) * MAX_SEQUENCE_COUNT;
    const SEED: &str = "Wolf";

    fn message() -> alloc::vec::Vec<u8> {
        make_message(SEED, MESSAGE_SIZE)
    }

    #[test]
    fn test_decoder() {
        fn test<T: Types>(decoder: &mut BaseDecoder<T>) {
            let message = message();
            let mut encoder = Encoder::new();
            encoder.start(&message, MAX_FRAGMENT_LEN);
            while !decoder.is_complete() {
                assert_eq!(decoder.message().unwrap(), None);
                let part = encoder.next_part();
                let _next = decoder.receive(&part).unwrap();
            }
            assert_eq!(decoder.message().unwrap(), Some(message.as_slice()));
        }

        let mut heapless_decoder: HeaplessDecoder<
            MAX_MESSAGE_SIZE,
            MAX_SEQUENCE_COUNT,
            MAX_FRAGMENT_LEN,
            MAX_SEQUENCE_COUNT,
            MAX_SEQUENCE_COUNT,
        > = HeaplessDecoder::new();
        let mut decoder = Decoder::default();

        test(&mut heapless_decoder);
        test(&mut decoder);
    }

    #[test]
    fn test_decoder_skip_some_simple_fragments() {
        let message = make_message(SEED, MESSAGE_SIZE);
        let mut encoder = Encoder::new();
        encoder.start(&message, MAX_FRAGMENT_LEN);
        let mut decoder = Decoder::default();
        let mut skip = false;
        while !decoder.is_complete() {
            let part = encoder.next_part();
            if !skip {
                let _next = decoder.receive(&part);
            }
            skip = !skip;
        }
        assert_eq!(decoder.message().unwrap(), Some(message.as_slice()));
    }

    #[test]
    fn test_decoder_receive_return_value() {
        let message = make_message(SEED, MESSAGE_SIZE);
        let mut encoder = Encoder::new();
        encoder.start(&message, MAX_FRAGMENT_LEN);
        let mut decoder = Decoder::default();
        let part = encoder.next_part();
        assert!(decoder.receive(&part).unwrap());
        // non-valid
        let mut part = encoder.next_part();
        part.checksum += 1;
        // TODO:
        // assert!(matches!(
        //     decoder.receive(&part),
        //     Err(Error::InconsistentPart)
        // ));
        // decoder complete
        while !decoder.is_complete() {
            let part = encoder.next_part();
            decoder.receive(&part).unwrap();
        }
        let part = encoder.next_part();
        assert!(!decoder.receive(&part).unwrap());
    }

    #[test]
    fn test_decoder_part_validation() {
        fn test<T: Types>(decoder: &mut BaseDecoder<T>) {
            let mut encoder = Encoder::new();
            encoder.start("foo".as_bytes(), 2);

            let mut part = encoder.next_part();
            assert!(decoder.receive(&part).unwrap());
            assert!(decoder.is_part_consistent(&part));
            part.checksum += 1;
            assert!(!decoder.is_part_consistent(&part));
            part.checksum -= 1;
            part.message_length += 1;
            assert!(!decoder.is_part_consistent(&part));
            part.message_length -= 1;
            part.sequence_count += 1;
            assert!(!decoder.is_part_consistent(&part));
            part.sequence_count -= 1;
            part.data = &[0];
            assert!(!decoder.is_part_consistent(&part));
        }

        let mut heapless_decoder: HeaplessDecoder<8, 8, 8, 8, 8> = HeaplessDecoder::new();
        let mut decoder = Decoder::default();

        test(&mut heapless_decoder);
        test(&mut decoder);
    }

    #[test]
    fn test_empty_decoder_empty_part() {
        fn test<T: Types>(decoder: &mut BaseDecoder<T>) {
            let mut part = Part {
                sequence: 12,
                sequence_count: 8,
                message_length: 100,
                checksum: 0x1234_5678,
                data: &[1, 5, 3, 3, 5],
            };

            // Check sequence_count.
            part.sequence_count = 0;
            assert!(matches!(decoder.receive(&part), Err(Error::InvalidPart)));
            part.sequence_count = 8;

            // Check message_length.
            part.message_length = 0;
            assert!(matches!(decoder.receive(&part), Err(Error::InvalidPart)));
            part.message_length = 100;

            // Check data.
            part.data = &[];
            assert!(matches!(decoder.receive(&part), Err(Error::InvalidPart)));
            part.data = &[1, 5, 3, 3, 5];

            // Should not validate as there aren't any previous parts received.
            assert!(!decoder.is_part_consistent(&part));
        }

        let mut heapless_decoder: HeaplessDecoder<100, 8, 5, 8, 8> = HeaplessDecoder::new();
        let mut decoder = Decoder::default();

        test(&mut heapless_decoder);
        test(&mut decoder);
    }
}