rite-model 0.7.2

Domain model and intermediate representation for the Rite key ceremony DSL
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
//! How a value is written out for a person: the encodings `reveal` shows,
//! and the shape each gives a sheet of paper.
//!
//! The shape is a property of the encoding and not of the value, so
//! `rite script` prints it before the ceremony runs: how many
//! characters make a row, how many of those are parity, how characters
//! group for the eye, what the alphabet is and what the encoding is
//! called. Only the number of rows depends on the value, and a sheet does
//! not need it when every row has the same shape.
//!
//! `bip39` is words rather than characters: one word per row, numbered
//! as a wallet asks for them, so the rows are the length.

use std::fmt;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use zeroize::Zeroizing;

use crate::{bip39, paper32};

/// An encoding a value is shown in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(test, derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[cfg_attr(
    test,
    schemars(description = "The encoding a value is written on paper in.")
)]
#[non_exhaustive]
pub enum RevealFormat {
    /// Rows of 32 base-32 characters, 28 of data and 4 of parity, from an
    /// alphabet without `I`, `L`, `O` and `U`; a wrong character in a row
    /// is corrected, two unreadable ones recovered. The default. See
    /// [`paper32`].
    #[cfg_attr(
        test,
        schemars(
            description = "Rows of 32 base-32 characters, 28 of data and 4 of parity; a wrong \
            character in a row is corrected."
        )
    )]
    Paper32,
    /// Two hexadecimal digits per byte, upper case, in rows of 32.
    #[cfg_attr(
        test,
        schemars(description = "Two hexadecimal digits per byte, in rows of 32.")
    )]
    Hex,
    /// Words from the BIP-39 English list, one per row, as a hardware
    /// wallet shows its recovery phrase and takes it back. For entropy of
    /// 16 to 32 bytes in steps of 4. See [`bip39`].
    #[cfg_attr(
        test,
        schemars(
            description = "Words from the BIP-39 English list, one per row, for a wallet's \
            recovery phrase."
        )
    )]
    Bip39,
}

impl RevealFormat {
    /// The name a ceremony writes.
    pub fn as_str(self) -> &'static str {
        match self {
            RevealFormat::Paper32 => "paper32",
            RevealFormat::Hex => "hex",
            RevealFormat::Bip39 => "bip39",
        }
    }

    /// Whether the value is words, one per row, rather than characters.
    pub fn is_words(self) -> bool {
        match self {
            RevealFormat::Paper32 | RevealFormat::Hex => false,
            RevealFormat::Bip39 => true,
        }
    }

    /// What `length:` counts in this format, as a person reads it.
    pub fn unit(self) -> &'static str {
        match self {
            RevealFormat::Paper32 | RevealFormat::Hex => "bytes",
            RevealFormat::Bip39 => "words",
        }
    }

    /// A value of `bytes` bytes in this format's unit: bytes, or the words
    /// of a phrase.
    pub fn length_of(self, bytes: usize) -> usize {
        match self {
            RevealFormat::Paper32 | RevealFormat::Hex => bytes,
            RevealFormat::Bip39 => bip39::words_for(bytes),
        }
    }

    /// The bytes a `length:` in this format's unit stands for.
    pub fn bytes_for(self, length: usize) -> usize {
        match self {
            RevealFormat::Paper32 | RevealFormat::Hex => length,
            RevealFormat::Bip39 => bip39::bytes_for(length),
        }
    }

    /// The shape a sheet takes for this encoding.
    pub fn layout(self) -> Layout {
        match self {
            RevealFormat::Paper32 => Layout {
                format: self,
                group: 4,
                row: paper32::ROW_DATA,
                parity: paper32::ROW_PARITY,
                alphabet: "Digits and letters, never I, L, O or U; upper or lower case".to_string(),
                algorithm: "Paper32: rows of 28 characters and 4 of parity, Reed-Solomon over \
                            GF(32)"
                    .to_string(),
            },
            RevealFormat::Hex => Layout {
                format: self,
                group: 4,
                row: 32,
                parity: 0,
                alphabet: "Digits 0 to 9 and letters A to F".to_string(),
                algorithm: "Hexadecimal, two characters per byte".to_string(),
            },
            RevealFormat::Bip39 => Layout {
                format: self,
                group: bip39::WORD_MAX,
                row: bip39::WORD_MAX,
                parity: 0,
                alphabet: "Words from the BIP-39 English list, one per row; the first four \
                           letters are enough"
                    .to_string(),
                algorithm: "BIP-39, English: 11 bits per word, the last word carrying a \
                            SHA-256 checksum"
                    .to_string(),
            },
        }
    }

    /// The shape a `reveal` step gives from its definition: its format, or
    /// the default.
    pub fn layout_for(format: Option<Self>) -> Layout {
        format.unwrap_or(RevealFormat::Paper32).layout()
    }

    /// Rows a value of `bytes` bytes takes.
    pub fn rows(self, bytes: usize) -> usize {
        let layout = self.layout();
        self.characters(bytes)
            .div_ceil(layout.row.saturating_add(layout.parity).max(1))
    }

    /// One row as typed, checked on its own: the characters it should be,
    /// and the repair it took, in words. `row` is its number from 1, and
    /// `more` says rows are known to follow it.
    ///
    /// Every row but the last is full, since a row's place in the value is
    /// its position: a short row is the last one, and one with rows after
    /// it is a row with characters missing.
    ///
    /// ```
    /// use rite_model::RevealFormat;
    ///
    /// let row = RevealFormat::Paper32.read_row("E9MQ 8S8P H8B", 1, false).unwrap();
    /// assert_eq!(row.text.as_str(), "E9MQ8S8PH8B");
    /// assert!(!row.full);
    /// // A short row where more are to come has characters missing.
    /// assert!(RevealFormat::Paper32.read_row("E9MQ8S8PH8B", 1, true).is_err());
    /// ```
    ///
    /// # Errors
    ///
    /// What is wrong with the row, in words, for the person to fix.
    pub fn read_row(self, text: &str, row: usize, more: bool) -> Result<TypedRow, String> {
        let read = self.read_one_row(text, row)?;
        // A word is a whole row however long it is.
        if self.is_words() {
            return Ok(read);
        }
        let full = self.layout().row.saturating_add(self.layout().parity);
        let characters = read.text.chars().count();
        if more && characters < full {
            return Err(format!(
                "{characters} characters; every row but the last has {full}"
            ));
        }
        Ok(TypedRow {
            full: characters == full,
            ..read
        })
    }

    fn read_one_row(self, text: &str, row: usize) -> Result<TypedRow, String> {
        match self {
            RevealFormat::Paper32 => paper32::read_row(text, row)
                .map(|read| TypedRow {
                    text: read.text,
                    repair: read.repair.map(|r| r.to_string()),
                    full: false,
                })
                .map_err(|e| e.to_string()),
            RevealFormat::Hex => {
                let digits = hex_digits(text)?;
                let row_len = self.layout().row;
                if digits.is_empty() {
                    return Err("nothing to read".to_string());
                }
                if digits.len() > row_len {
                    return Err(format!(
                        "{} characters; a row has at most {row_len}",
                        digits.len()
                    ));
                }
                Ok(TypedRow {
                    text: digits,
                    repair: None,
                    full: false,
                })
            }
            RevealFormat::Bip39 => bip39::read_word(text, row)
                .map(|text| TypedRow {
                    text,
                    repair: None,
                    full: true,
                })
                .map_err(|e| e.to_string()),
        }
    }

    /// A whole value as typed, rows one after another: its bytes, and the
    /// repairs it took, in words.
    ///
    /// # Errors
    ///
    /// What is wrong with the text, in words.
    pub fn decode(self, text: &str) -> Result<TypedValue, String> {
        match self {
            RevealFormat::Paper32 => paper32::decode(text)
                .map(|decoded| TypedValue {
                    bytes: decoded.bytes,
                    repaired: decoded.repaired.iter().map(ToString::to_string).collect(),
                })
                .map_err(|e| e.to_string()),
            RevealFormat::Hex => {
                let digits = hex_digits(text)?;
                if digits.len() % 2 != 0 {
                    return Err(format!(
                        "{} hexadecimal digits; a byte is two",
                        digits.len()
                    ));
                }
                let bytes = digits
                    .as_bytes()
                    .chunks(2)
                    .map(|pair| {
                        std::str::from_utf8(pair)
                            .ok()
                            .and_then(|p| u8::from_str_radix(p, 16).ok())
                            .unwrap_or(0)
                    })
                    .collect();
                Ok(TypedValue {
                    bytes: Zeroizing::new(bytes),
                    repaired: Vec::new(),
                })
            }
            RevealFormat::Bip39 => bip39::decode(text)
                .map(|decoded| TypedValue {
                    bytes: decoded.bytes,
                    repaired: Vec::new(),
                })
                .map_err(|e| e.to_string()),
        }
    }

    /// Characters a value of `bytes` bytes takes, parity included.
    pub fn characters(self, bytes: usize) -> usize {
        match self {
            RevealFormat::Paper32 => {
                let symbols = bytes.saturating_mul(8).div_ceil(5);
                let rows = symbols.div_ceil(paper32::ROW_DATA);
                symbols.saturating_add(rows.saturating_mul(paper32::ROW_PARITY))
            }
            RevealFormat::Hex => bytes.saturating_mul(2),
            // A row of boxes per word, as long as the longest word.
            RevealFormat::Bip39 => bip39::words_for(bytes).saturating_mul(bip39::WORD_MAX),
        }
    }
}

impl fmt::Display for RevealFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A row [`RevealFormat::read_row`] read.
#[derive(Debug)]
pub struct TypedRow {
    /// The row as it should read, upper case, without separators.
    pub text: Zeroizing<String>,
    /// The repair it took, in words.
    pub repair: Option<String>,
    /// Whether the row has every character a row can have. A row that
    /// does not is the last one.
    pub full: bool,
}

/// A value [`RevealFormat::decode`] read.
#[derive(Debug)]
pub struct TypedValue {
    /// The bytes, wiped when dropped.
    pub bytes: Zeroizing<Vec<u8>>,
    /// The repairs, in words, one per row that took one.
    pub repaired: Vec<String>,
}

/// Hexadecimal digits in upper case, separators dropped.
fn hex_digits(text: &str) -> Result<Zeroizing<String>, String> {
    let mut digits = Zeroizing::new(String::with_capacity(text.len()));
    for (position, c) in text.chars().enumerate() {
        match c {
            ' ' | '-' | '|' | '\t' | '\n' | '\r' | '.' => {}
            c if c.is_ascii_hexdigit() => digits.push(c.to_ascii_uppercase()),
            _ => {
                return Err(format!(
                    "character {} is not a hexadecimal digit (0 to 9, A to F)",
                    position.saturating_add(1)
                ));
            }
        }
    }
    Ok(digits)
}

/// A `format:` value that names no encoding this build shows.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownRevealFormat(pub String);

impl fmt::Display for UnknownRevealFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "'{}' is not a format; use paper32, hex or bip39", self.0)
    }
}

impl std::error::Error for UnknownRevealFormat {}

impl FromStr for RevealFormat {
    type Err = UnknownRevealFormat;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "paper32" => Ok(RevealFormat::Paper32),
            "hex" => Ok(RevealFormat::Hex),
            "bip39" => Ok(RevealFormat::Bip39),
            other => Err(UnknownRevealFormat(other.to_string())),
        }
    }
}

/// The shape a written value takes: what a sheet pre-prints and how a
/// screen lays the characters out.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Layout {
    /// The encoding.
    pub format: RevealFormat,
    /// Characters per group, for the eye.
    pub group: usize,
    /// Data characters in a full row.
    pub row: usize,
    /// Parity characters closing every row; zero when there are none.
    pub parity: usize,
    /// What the characters can be, in words.
    pub alphabet: String,
    /// What the encoding is called, in small print.
    pub algorithm: String,
}

impl Default for Layout {
    fn default() -> Self {
        RevealFormat::Paper32.layout()
    }
}

impl Layout {
    /// Split a written value into rows, each its data in groups and its
    /// parity, for a screen to style each part. Words are a row each.
    pub fn rows<'a>(&self, text: &'a str) -> Vec<Row<'a>> {
        if self.format.is_words() {
            return text
                .split_whitespace()
                .map(|word| Row {
                    groups: vec![word],
                    parity: "",
                })
                .collect();
        }
        let row_len = self.row.saturating_add(self.parity).max(1);
        text.as_bytes()
            .chunks(row_len)
            .map(|chunk| {
                let line = std::str::from_utf8(chunk).unwrap_or_default();
                let data_len = line.len().saturating_sub(self.parity);
                let (data, parity) = line.split_at(data_len.min(line.len()));
                let groups = data
                    .as_bytes()
                    .chunks(self.group.max(1))
                    .map(|g| std::str::from_utf8(g).unwrap_or_default())
                    .collect();
                Row { groups, parity }
            })
            .collect()
    }
}

/// One row of a written value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Row<'a> {
    /// The data, grouped.
    pub groups: Vec<&'a str>,
    /// The parity characters, possibly empty.
    pub parity: &'a str,
}

/// Bytes as upper-case hex.
pub fn hex_upper(bytes: &[u8]) -> String {
    use std::fmt::Write as _;
    bytes.iter().fold(
        String::with_capacity(bytes.len().saturating_mul(2)),
        |mut out, b| {
            let _ = write!(out, "{b:02X}");
            out
        },
    )
}

#[cfg(test)]
#[allow(clippy::indexing_slicing)]
mod tests {
    use super::*;

    #[test]
    fn a_row_is_checked_on_its_own_and_a_value_read_whole() {
        // 30 bytes: a full row of 28 and 4, then 20 and 4.
        let bytes = [0x5A; 30];
        let text = paper32::encode(&bytes);
        let (first, second) = text.split_at(32);
        let row = RevealFormat::Paper32.read_row(first, 1, true).unwrap();
        assert_eq!(row.text.as_str(), first);
        assert!(row.repair.is_none());
        assert!(row.full);

        let mut slipped: Vec<char> = second.to_ascii_lowercase().chars().collect();
        slipped[3] = if slipped[3] == 'a' { 'b' } else { 'a' };
        let slipped: String = slipped.iter().collect();
        let row = RevealFormat::Paper32.read_row(&slipped, 2, false).unwrap();
        assert_eq!(row.text.as_str(), second);
        assert!(!row.full);
        assert!(row.repair.unwrap().starts_with("row 2: character 4"));
        assert!(RevealFormat::Paper32.read_row("ABCD", 1, false).is_err());
        // The short last row, where the sheet says more rows follow.
        let refused = RevealFormat::Paper32.read_row(second, 1, true).unwrap_err();
        assert!(
            refused.contains("every row but the last has 32"),
            "{refused}"
        );

        let value = RevealFormat::Paper32
            .decode(&format!("{first}\n{slipped}"))
            .unwrap();
        assert_eq!(*value.bytes, bytes);
        assert_eq!(value.repaired.len(), 1);

        let hex = RevealFormat::Hex.read_row("de ad-be ef", 1, false).unwrap();
        assert_eq!(hex.text.as_str(), "DEADBEEF");
        assert!(!hex.full);
        assert!(RevealFormat::Hex.read_row("DEAG", 1, false).is_err());
        assert!(RevealFormat::Hex.read_row("DEADBEEF", 1, true).is_err());
        assert_eq!(
            *RevealFormat::Hex.decode("DEAD\nbeef").unwrap().bytes,
            [0xDE, 0xAD, 0xBE, 0xEF]
        );
        assert!(RevealFormat::Hex.decode("DEA").is_err());
        assert_eq!(RevealFormat::Paper32.rows(35), 2);
        assert_eq!(RevealFormat::Hex.rows(35), 3);
    }

    #[test]
    fn a_paper32_string_splits_into_rows_of_groups_and_parity() {
        let layout = RevealFormat::Paper32.layout();
        let text = paper32::encode(&[0x5A; 35]);
        let rows = layout.rows(&text);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0].groups.len(), 7);
        assert!(rows[0].groups.iter().all(|g| g.len() == 4));
        assert_eq!(rows[0].parity.len(), 4);
        assert_eq!(rows[1].parity.len(), 4);
        assert_eq!(RevealFormat::Paper32.characters(35), 64);
        assert_eq!(RevealFormat::Paper32.characters(32), 60);
        assert_eq!(RevealFormat::Paper32.characters(1), 6);
    }

    #[test]
    fn hex_has_no_parity() {
        let layout = RevealFormat::Hex.layout();
        let rows = layout.rows("DEADBEEF01");
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].groups, ["DEAD", "BEEF", "01"]);
        assert_eq!(rows[0].parity, "");
        assert_eq!(hex_upper(&[0xde, 0xad]), "DEAD");
        assert_eq!(RevealFormat::Hex.characters(5), 10);
    }

    #[test]
    fn a_format_is_named_and_parsed() {
        assert_eq!("paper32".parse(), Ok(RevealFormat::Paper32));
        assert_eq!(RevealFormat::Hex.to_string(), "hex");
        assert_eq!(
            "words".parse::<RevealFormat>().unwrap_err().to_string(),
            "'words' is not a format; use paper32, hex or bip39"
        );
        assert_eq!("bip39".parse(), Ok(RevealFormat::Bip39));
    }

    #[test]
    fn a_phrase_is_a_word_per_row() {
        let phrase = bip39::encode(&[0u8; 32]).unwrap();
        let rows = RevealFormat::Bip39.layout().rows(&phrase);
        assert_eq!(rows.len(), 24);
        assert_eq!(rows[23].groups, ["art"]);
        assert_eq!(rows[0].parity, "");
        assert_eq!(RevealFormat::Bip39.rows(32), 24);
        assert_eq!(RevealFormat::Bip39.rows(16), 12);
        assert_eq!(RevealFormat::Bip39.length_of(32), 24);
        assert_eq!(RevealFormat::Bip39.bytes_for(24), 32);

        // A short word is a whole row, and the count says where the phrase ends.
        let row = RevealFormat::Bip39.read_row("ART", 24, false).unwrap();
        assert_eq!(row.text.as_str(), "art");
        assert!(row.full);
        assert!(RevealFormat::Bip39.read_row("zoo", 1, true).unwrap().full);
        assert!(RevealFormat::Bip39.read_row("abandonx", 1, true).is_err());
        let typed: Vec<&str> = phrase.split(' ').collect();
        assert_eq!(
            *RevealFormat::Bip39.decode(&typed.join("\n")).unwrap().bytes,
            [0u8; 32]
        );
    }
}