Skip to main content

quick_start/
quick_start.rs

1//! The README's Quick Start, kept compiling by `cargo check --examples`.
2
3use pertaro::{PertaroExpert243, Sentinel, Word256, MAX_PACKED, ROOT_SENTINEL};
4
5fn main() {
6    // 1. A 256-byte word with a seed, a parent link, and in-band sentinels.
7    let mut raw = Word256 {
8        hash: 0xcbf2_9ce4_8422_2325,
9        parent: 0,
10        trits: [0u8; 244],
11    };
12
13    // Address leaf [1, 0, 2, 1, 2] in the 5-tier ternary trie (each tier in 0..3).
14    let target_leaf = PertaroExpert243::trie_index([1, 0, 2, 1, 2]);
15    raw.trits[target_leaf] = 42; // residual override: +42 mod 243
16
17    // Stamp a defeated branch with an in-band Tombstone (245).
18    let defeated_leaf = PertaroExpert243::trie_index([2, 2, 0, 1, 0]);
19    raw.trits[defeated_leaf] = Sentinel::Tombstone as u8;
20
21    // Mark the root horizon sentinel at trits[243].
22    raw.trits[ROOT_SENTINEL] = Sentinel::NullNode as u8;
23
24    // 2. Wrap in the zero-cost view and materialize.
25    let expert = PertaroExpert243(raw);
26    let materialized: Word256 = expert.materialize();
27
28    assert!(materialized.trits[target_leaf] < MAX_PACKED);
29    assert_eq!(materialized.trits[defeated_leaf], Sentinel::Tombstone as u8);
30    assert_eq!(materialized.hash, expert.seed());
31
32    // 3. Reground the winning Tier-5 leaf (0..243) into a fresh seed.
33    let next_seed = expert.reground_seed(target_leaf as u8, Sentinel::BusIndirect);
34    assert_ne!(next_seed, expert.seed());
35
36    println!("leaf {target_leaf} -> {}", materialized.trits[target_leaf]);
37    println!("next seed {next_seed:#018x}");
38}