publet-algorithms 0.2.1

Generic algorithms publet is built on: an RFC 6962 Merkle log, sorted-set membership with absence proofs, recursive content trees, exact fixed-point arithmetic, and deterministic trust propagation. Depends on nothing publet-specific.
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
//! Append-only Merkle log with inclusion and consistency proofs.
//!
//! Follows the algorithms of [RFC 6962] so that the constructions are
//! specified elsewhere and independently testable, rather than being an
//! invention of this project. Domain-separating leaf hashes (`0x00`) from
//! interior hashes (`0x01`) is what prevents a leaf from being presented as
//! an interior node, which would otherwise let one tree claim two shapes.
//!
//! [RFC 6962]: https://www.rfc-editor.org/rfc/rfc6962#section-2
//!
//! The log's leaves are successive membership roots, so a consistency proof
//! establishes that generation *m* is a prefix of generation *n*: that
//! nothing recorded earlier was rewritten.

use sha2::{Digest as _, Sha256};

/// A 32-byte digest.
pub type Hash = [u8; 32];

/// Hash of an empty tree, per RFC 6962: `HASH()`.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{empty_root, root, to_hex};
///
/// assert_eq!(root(&[]), empty_root());
/// // SHA-256 of nothing, as RFC 6962 defines it.
/// assert_eq!(
///     to_hex(&empty_root()),
///     "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
/// );
/// ```
#[must_use]
pub fn empty_root() -> Hash {
    Sha256::digest([]).into()
}

/// Leaf hash: `HASH(0x00 || leaf)`.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{leaf_hash, to_hex};
///
/// assert_eq!(
///     to_hex(&leaf_hash(b"a")),
///     "022a6979e6dab7aa5ae4c3e5e45f7e977112a7e63593820dbec1ec738a24f93c",
/// );
/// ```
#[must_use]
pub fn leaf_hash(leaf: &[u8]) -> Hash {
    let mut h = Sha256::new();
    h.update([0x00]);
    h.update(leaf);
    h.finalize().into()
}

/// Interior hash: `HASH(0x01 || left || right)`.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{leaf_hash, node_hash, root};
///
/// let (a, b) = (leaf_hash(b"a"), leaf_hash(b"b"));
/// assert_eq!(node_hash(&a, &b), root(&[a, b]));
/// // Order matters: a log is a sequence.
/// assert_ne!(node_hash(&a, &b), node_hash(&b, &a));
/// ```
#[must_use]
pub fn node_hash(left: &Hash, right: &Hash) -> Hash {
    let mut h = Sha256::new();
    h.update([0x01]);
    h.update(left);
    h.update(right);
    h.finalize().into()
}

/// Largest power of two strictly less than `n`.
///
/// RFC 6962's split point. Defined for `n > 1`.
fn split(n: u64) -> u64 {
    debug_assert!(n > 1);
    // The largest power of two strictly below n is the top bit of n - 1.
    // Doubling up to it instead never ends for n above 2^63: the shift
    // wraps to zero, and a peer's proof claiming such a size would hang
    // the verifier.
    1 << (u64::BITS - 1 - (n - 1).leading_zeros())
}

/// [`split`] for a number of leaves held in memory.
fn split_len(n: usize) -> usize {
    debug_assert!(n > 1);
    1 << (usize::BITS - 1 - (n - 1).leading_zeros())
}

/// Merkle Tree Hash over `leaves`, already leaf-hashed.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{leaf_hash, root, to_hex};
///
/// let leaves = [leaf_hash(b"a"), leaf_hash(b"b")];
/// assert_eq!(
///     to_hex(&root(&leaves)),
///     "b137985ff484fb600db93107c77b0365c80d78f5b429ded0fd97361d077999eb",
/// );
/// ```
#[must_use]
pub fn root(leaves: &[Hash]) -> Hash {
    match leaves.len() {
        0 => empty_root(),
        1 => leaves.first().copied().unwrap_or_else(empty_root),
        n => {
            let k = split_len(n);
            let (left, right) = leaves.split_at(k);
            node_hash(&root(left), &root(right))
        }
    }
}

/// Audit path proving `index` is in a tree of `leaves.len()` leaves.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{Hash, inclusion_proof, leaf_hash, root, verify_inclusion};
///
/// # let leaves: Vec<Hash> = ["a", "b", "c"].iter().map(|e| leaf_hash(e.as_bytes())).collect();
/// let path = inclusion_proof(&leaves, 2);
/// assert!(verify_inclusion(&leaves[2], 2, leaves.len() as u64, &path, &root(&leaves)));
/// ```
#[must_use]
pub fn inclusion_proof(leaves: &[Hash], index: usize) -> Vec<Hash> {
    let n = leaves.len();
    if index >= n {
        return Vec::new();
    }
    if n == 1 {
        return Vec::new();
    }
    let k = split_len(n);
    let (left, right) = leaves.split_at(k);
    if index < k {
        let mut path = inclusion_proof(left, index);
        path.push(root(right));
        path
    } else {
        let mut path = inclusion_proof(right, index - k);
        path.push(root(left));
        path
    }
}

/// Render a hash as lower-case hexadecimal.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::to_hex;
///
/// assert_eq!(to_hex(&[0xab; 32]), "ab".repeat(32));
/// ```
#[must_use]
pub fn to_hex(hash: &Hash) -> String {
    use std::fmt::Write as _;
    hash.iter()
        .fold(String::with_capacity(64), |mut acc, byte| {
            let _ = write!(acc, "{byte:02x}");
            acc
        })
}

/// Whether the node carrying the leaf is the left or right child.
enum Side {
    Left,
    Right,
}

/// Recompute a root from a leaf and its audit path.
///
/// [`inclusion_proof`] emits the path from the leaf upward, as RFC 6962
/// specifies. Which side each sibling belongs on is only known by
/// descending from the root, so the descent is done first and the sides are
/// then applied in reverse against the path.
///
/// `index` and `size` are `u64`, as RFC 6962's tree sizes are, on every
/// target: they come with a proof from someone else, whose log may be
/// larger than this machine's `usize`. A 32-bit or `wasm32` verifier
/// checks a proof for a log of any size.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{Hash, inclusion_proof, leaf_hash, root, verify_inclusion};
///
/// # let leaves: Vec<Hash> = ["a", "b", "c"].iter().map(|e| leaf_hash(e.as_bytes())).collect();
/// let head = root(&leaves);
/// let path = inclusion_proof(&leaves, 1);
/// assert!(verify_inclusion(&leaves[1], 1, 3, &path, &head));
/// // The same path does not prove a different entry, or a different position.
/// assert!(!verify_inclusion(&leaf_hash(b"forged"), 1, 3, &path, &head));
/// assert!(!verify_inclusion(&leaves[1], 0, 3, &path, &head));
/// ```
#[must_use]
pub fn verify_inclusion(
    leaf: &Hash,
    index: u64,
    size: u64,
    path: &[Hash],
    expected_root: &Hash,
) -> bool {
    if index >= size || size == 0 {
        return false;
    }

    let mut sides = Vec::new();
    let (mut idx, mut sz) = (index, size);
    while sz > 1 {
        let k = split(sz);
        if idx < k {
            sides.push(Side::Left);
            sz = k;
        } else {
            sides.push(Side::Right);
            idx -= k;
            sz -= k;
        }
    }

    if sides.len() != path.len() {
        return false;
    }

    let mut computed = *leaf;
    for (sibling, side) in path.iter().zip(sides.iter().rev()) {
        computed = match side {
            Side::Left => node_hash(&computed, sibling),
            Side::Right => node_hash(sibling, &computed),
        };
    }
    computed == *expected_root
}

/// Proof that a tree of `m` leaves is a prefix of one of `leaves.len()`.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{Hash, consistency_proof, leaf_hash, root, verify_consistency};
///
/// # let leaves: Vec<Hash> = ["a", "b", "c"].iter().map(|e| leaf_hash(e.as_bytes())).collect();
/// // The first two entries form a log that the three-entry log extends.
/// let proof = consistency_proof(&leaves, 2);
/// assert!(verify_consistency(2, 3, &root(&leaves[..2]), &root(&leaves), &proof));
/// ```
#[must_use]
pub fn consistency_proof(leaves: &[Hash], m: usize) -> Vec<Hash> {
    let n = leaves.len();
    if m == 0 || m > n {
        return Vec::new();
    }
    if m == n {
        return Vec::new();
    }
    subproof(leaves, m, true)
}

fn subproof(leaves: &[Hash], m: usize, start: bool) -> Vec<Hash> {
    let n = leaves.len();
    if m == n {
        if start {
            return Vec::new();
        }
        return vec![root(leaves)];
    }
    let k = split_len(n);
    let (left, right) = leaves.split_at(k);
    if m <= k {
        let mut path = subproof(left, m, start);
        path.push(root(right));
        path
    } else {
        let mut path = subproof(right, m - k, false);
        path.push(root(left));
        path
    }
}

/// Decompose an inclusion path into its inner and border parts.
///
/// `inner` counts the levels where the path turns, `border` the levels
/// above where it only ever folds right. Naming them is what lets a
/// consistency proof be checked without rebuilding either tree.
fn decompose(index: u64, size: u64) -> (u32, u32) {
    let inner = u64::BITS - (index ^ (size - 1)).leading_zeros();
    // `inner` reaches 64 when the size is above 2^63, and shifting a u64 by
    // 64 overflows: a panic where overflow is checked, a wrong answer where
    // it is not. Every bit is then inside, so none is on the border.
    let border = index.checked_shr(inner).unwrap_or(0).count_ones();
    (inner, border)
}

fn chain_inner(seed: Hash, path: &[Hash], index: u64) -> Hash {
    let mut acc = seed;
    for (i, sibling) in path.iter().enumerate() {
        acc = if (index >> i) & 1 == 0 {
            node_hash(&acc, sibling)
        } else {
            node_hash(sibling, &acc)
        };
    }
    acc
}

/// As [`chain_inner`], but folding only where the path turns right.
fn chain_inner_right(seed: Hash, path: &[Hash], index: u64) -> Hash {
    let mut acc = seed;
    for (i, sibling) in path.iter().enumerate() {
        if (index >> i) & 1 == 1 {
            acc = node_hash(sibling, &acc);
        }
    }
    acc
}

fn chain_border_right(seed: Hash, path: &[Hash]) -> Hash {
    path.iter()
        .fold(seed, |acc, sibling| node_hash(sibling, &acc))
}

/// Verify that `old_root` at size `m` is a prefix of `new_root` at size `n`.
///
/// This is the reconstruction of RFC 6962 section 2.1.2: the proof is split
/// into an inner and a border part, each folded in turn, yielding both roots
/// from one path. Getting it wrong is not visible in ordinary use -- proofs
/// would simply always fail, or worse, always pass -- so the round-trip
/// tests exercise every `(m, n)` pair rather than a sample.
///
/// The sizes are `u64` on every target, for the reason
/// [`verify_inclusion`]'s are.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::{Hash, consistency_proof, leaf_hash, root, verify_consistency};
///
/// # let leaves: Vec<Hash> = ["a", "b", "c"].iter().map(|e| leaf_hash(e.as_bytes())).collect();
/// let old = root(&leaves[..2]);
/// let proof = consistency_proof(&leaves, 2);
/// assert!(verify_consistency(2, 3, &old, &root(&leaves), &proof));
///
/// // A log that rewrote its history is not consistent with what was seen before.
/// let rewritten: Vec<Hash> = ["a", "B", "c"].iter().map(|e| leaf_hash(e.as_bytes())).collect();
/// assert!(!verify_consistency(2, 3, &old, &root(&rewritten), &consistency_proof(&rewritten, 2)));
/// ```
#[must_use]
pub fn verify_consistency(
    m: u64,
    n: u64,
    old_root: &Hash,
    new_root: &Hash,
    proof: &[Hash],
) -> bool {
    if m > n {
        return false;
    }
    if m == n {
        return proof.is_empty() && old_root == new_root;
    }
    if m == 0 {
        // Every tree contains the empty prefix.
        return proof.is_empty();
    }
    if proof.is_empty() {
        return false;
    }

    let (mut inner, border) = decompose(m - 1, n);
    let shift = m.trailing_zeros();
    inner -= shift;

    // When `m` is an exact power of two the old root is the seed and is not
    // carried in the proof; otherwise the proof supplies it.
    let (seed, start) = if m == 1u64 << shift {
        (*old_root, 0usize)
    } else {
        match proof.first() {
            Some(h) => (*h, 1usize),
            None => return false,
        }
    };

    let expected = start + inner as usize + border as usize;
    if proof.len() != expected {
        return false;
    }
    let Some(rest) = proof.get(start..) else {
        return false;
    };
    let Some(inner_part) = rest.get(..inner as usize) else {
        return false;
    };
    let Some(border_part) = rest.get(inner as usize..) else {
        return false;
    };

    let mask = (m - 1) >> shift;
    let recomputed_old = chain_border_right(chain_inner_right(seed, inner_part, mask), border_part);
    let recomputed_new = chain_border_right(chain_inner(seed, inner_part, mask), border_part);

    recomputed_old == *old_root && recomputed_new == *new_root
}

/// Checkpoint origins for a client catching up to `head`.
///
/// Exponentially spaced, so a client far behind is covered in O(log n)
/// fetches rather than one per entry.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::checkpoints;
///
/// // Behind a head at 100: entries 1, 2, 4, 8, ... back from it.
/// assert_eq!(checkpoints(100), [36, 68, 84, 92, 96, 98, 99]);
/// ```
#[must_use]
pub fn checkpoints(head: u64) -> Vec<u64> {
    let mut out = Vec::new();
    let mut step = 1u64;
    while step <= head {
        out.push(head - step);
        step = step.saturating_mul(2);
    }
    out.sort_unstable();
    out.dedup();
    out
}

/// How many fetches a client at `current` needs to reach `head`.
///
/// With checkpoints at every power of two behind the head, a client greedily
/// takes the largest jump that does not overshoot. The distance is therefore
/// covered in one fetch per set bit -- `count_ones`, which is bounded by
/// `log2(distance) + 1` and never exceeds 64.
///
/// # Example
///
/// ```
/// use publet_algorithms::log::fetches_required;
///
/// // 0 to 300 is a distance with four set bits, so four fetches.
/// assert_eq!(fetches_required(0, 300), 4);
/// assert_eq!(fetches_required(300, 300), 0);
/// ```
#[must_use]
pub fn fetches_required(current: u64, head: u64) -> u32 {
    if current >= head {
        return 0;
    }
    (head - current).count_ones()
}