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
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-FileCopyrightText: © 2020 Dominik Spicher <dominikspicher@gmail.com>
// SPDX-License-Identifier: MIT
//! `ur` is a crate to interact with ["Uniform Resources (UR)"] encodings
//! of binary data.
//!
//! The encoding scheme is optimized for transport in URIs and QR codes.
//!
//! The [encoder](BaseEncoder) allows a byte payload to be transmitted in
//! multiple stages, respecting maximum size requirements. Under the hood, a
//! [`fountain`] encoder is used to create an unbounded stream of URIs, subsets
//! of which can be recombined at the receiving side into the payload.
//!
//! For example:
//!
//! ```
//! # use foundation_ur::{HeaplessDecoder, HeaplessEncoder};
//! # let mut encoder: HeaplessEncoder<32, 32> = HeaplessEncoder::new();
//! # let mut decoder: HeaplessDecoder<100, 32, 32, 32, 32, 32> = HeaplessDecoder::new();
//! const MAX_FRAGMENT_LENGTH: usize = 5;
//!
//! let data = "Ten chars!".repeat(10);
//!
//! encoder.start("bytes", data.as_bytes(), MAX_FRAGMENT_LENGTH);
//! assert_eq!(
//! encoder.next_part().to_string(),
//! "ur:bytes/1-20/lpadbbcsiecyvdidatkpfeghihjtcxiabdfevlms"
//! );
//!
//! while !decoder.is_complete() {
//! let sequence = encoder.current_sequence();
//! let part = encoder.next_part();
//! // Simulate some communication loss
//! if sequence & 1 > 0 {
//! decoder.receive(part).unwrap();
//! }
//! }
//! assert_eq!(decoder.message().unwrap().as_deref(), Some(data.as_bytes()));
//! ```
//!
//! ["Uniform Resources (UR)"]: https://github.com/BlockchainCommons/Research/blob/master/papers/bcr-2020-005-ur.md
//! [`fountain`]: https://en.wikipedia.org/wiki/Fountain_code
//!
//! The following useful building blocks are also part of the public API:
//!
//! - The [`bytewords`] module contains functionality to encode byte payloads
//! into a suitable alphabet, achieving hexadecimal byte-per-character
//! efficiency.
//!
//! - The [`fountain`] module provides an implementation of a fountain
//! encoder, which splits up a byte payload into multiple segments and
//! emits an unbounded stream of parts which can be recombined at the
//! receiving decoder side.
extern crate alloc;
extern crate core;
pub use *;
pub use *;
const CRC32: Crc = new;