chia-consensus 0.36.0

Utility functions and types used by the Chia blockchain full node
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
use chia_sha2::Sha256;
use hex_literal::hex;

fn get_bit(val: &[u8; 32], bit: u8) -> u8 {
    ((val[(bit / 8) as usize] & (0x80 >> (bit & 7))) != 0).into()
}

#[repr(u8)]
#[derive(PartialEq, Eq, Copy, Clone)]
pub(crate) enum NodeType {
    Empty,
    Term,
    Mid,
    // this is a middle node where both its children are terminals
    // or there is a straight-line of one-sided middle nodes ending in such a
    // double-terminal tree. This property determines where we need to insert
    // empty nodes
    MidDbl,
}

fn encode_type(t: NodeType) -> u8 {
    match t {
        NodeType::Empty => 0,
        NodeType::Term => 1,
        NodeType::Mid | NodeType::MidDbl => 2,
    }
}

pub(crate) fn hash(
    ltype: NodeType,
    rtype: NodeType,
    left: &[u8; 32],
    right: &[u8; 32],
) -> [u8; 32] {
    let mut hasher = Sha256::new();
    hasher.update(hex!(
        "000000000000000000000000000000000000000000000000000000000000"
    ));
    hasher.update([encode_type(ltype), encode_type(rtype)]);
    hasher.update(left);
    hasher.update(right);
    hasher.finalize()
}

pub(crate) const BLANK: [u8; 32] =
    hex!("0000000000000000000000000000000000000000000000000000000000000000");

/// This function performs an in-place, recursive radix sort of the range.
/// as each level returns, values are hashed pair-wise and as a hash tree.
/// the return value is the merkle tree root that the values in the range form
fn radix_sort(range: &mut [[u8; 32]], depth: u8) -> ([u8; 32], NodeType) {
    assert!(!range.is_empty());

    if range.len() == 1 {
        return (range[0], NodeType::Term);
    }

    // first sort the range based on the bit at "depth" (starting with the most
    // significant bit). It also sorts the two resulting ranges recursively by
    // the next bit. The return value is the SHA256 digest of the resulting
    // merkle tree. Any node that only has a children on one side, is a no-op,
    // where that child's hash is forwarded up the tree.
    let mut left: i32 = 0;
    let mut right = range.len() as i32 - 1;

    // move 0 bits to the left, and 1 bits to the right
    while left <= right {
        let left_bit = get_bit(&range[left as usize], depth);
        let right_bit = get_bit(&range[right as usize], depth);

        if left_bit == 1 && right_bit == 0 {
            range.swap(left as usize, right as usize);
            left += 1;
            right -= 1;
        } else {
            if left_bit == 0 {
                left += 1;
            }
            if right_bit == 1 {
                right -= 1;
            }
        }
    }

    // we now have one or two branches of the tree, at this depth
    // if either left or right is empty, this level of the tree does not hash
    // anything, but just forwards the hash of the one sub tree. Otherwise, it
    // computes the hashes of the two sub trees and combines them in a hash.

    let left_empty = left == 0;
    let right_empty = right == range.len() as i32 - 1;

    if left_empty || right_empty {
        if depth == 255 {
            // if every bit is identical, we have a duplicate value
            // duplicate values are collapsed (since this is a set)
            // so just return one of the duplicates as if there was only one
            debug_assert!(range.len() > 1);
            debug_assert!(range[0] == range[1]);
            (range[0], NodeType::Term)
        } else {
            // this means either the left or right bucket/sub tree was empty.
            let (child_hash, child_type) = radix_sort(range, depth + 1);

            // in this case we may need to insert an Empty node (prefix 0 and a
            // blank hash)
            if child_type == NodeType::Mid {
                if left_empty {
                    (
                        hash(NodeType::Empty, child_type, &BLANK, &child_hash),
                        NodeType::Mid,
                    )
                } else {
                    (
                        hash(child_type, NodeType::Empty, &child_hash, &BLANK),
                        NodeType::Mid,
                    )
                }
            } else {
                (child_hash, child_type)
            }
        }
    } else if depth == 255 {
        // this is an edge case where we make it all the way down to the
        // bottom of the tree, and split the last pair. This has the same
        // effect as the else-block, but since we use u8 for depth, it would
        // overflow
        debug_assert!(range.len() > 1);
        debug_assert!(left < range.len() as i32);
        (
            hash(
                NodeType::Term,
                NodeType::Term,
                &range[0],
                &range[left as usize],
            ),
            NodeType::MidDbl,
        )
    } else {
        let (left_hash, left_type) = radix_sort(&mut range[..left as usize], depth + 1);
        let (right_hash, right_type) = radix_sort(&mut range[left as usize..], depth + 1);
        let node_type = if left_type == NodeType::Term && right_type == NodeType::Term {
            NodeType::MidDbl
        } else {
            NodeType::Mid
        };
        (
            hash(left_type, right_type, &left_hash, &right_hash),
            node_type,
        )
    }
}

/// Calculate the merkle set root hash for the tree containing all elements in
/// leafs. The order of leafs does not affect the merkle tree (nor the root
/// hash).
pub fn compute_merkle_set_root(leafs: &mut [[u8; 32]]) -> [u8; 32] {
    // There's a special case for empty sets
    if leafs.is_empty() {
        return BLANK;
    }

    match radix_sort(leafs, 0) {
        (hash, NodeType::Term) => {
            // if there's only a single item in the set, we prepend "Term"
            // and hash it
            // the reason we don't just check the length of "leafs" is that it
            // may contain duplicates and boil down to a single node
            // (effectively), which is a case we need to support
            let mut hasher = Sha256::new();
            hasher.update([NodeType::Term as u8]);
            hasher.update(hash);
            hasher.finalize()
        }
        (hash, NodeType::Mid | NodeType::MidDbl) => hash,
        (_, NodeType::Empty) => panic!("unexpected"),
    }
}

#[cfg(test)]
pub mod test {
    use super::*;

    fn h2(buf1: &[u8], buf2: &[u8]) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(buf1);
        hasher.update(buf2);
        hasher.finalize()
    }

    const PREFIX: [u8; 30] = hex!("000000000000000000000000000000000000000000000000000000000000");

    fn hashdown(buf1: &[u8], buf2: &[u8], buf3: &[u8]) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(PREFIX);
        hasher.update(buf1);
        hasher.update(buf2);
        hasher.update(buf3);
        hasher.finalize()
    }

    #[test]
    fn test_get_bit_msb() {
        let val1 = hex!("8000000000000000000000000000000000000000000000000000000000000000");
        assert_eq!(get_bit(&val1, 0), 1);
        assert_eq!(get_bit(&val1, 1), 0);
        assert_eq!(get_bit(&val1, 2), 0);
        assert_eq!(get_bit(&val1, 3), 0);
        assert_eq!(get_bit(&val1, 4), 0);
        assert_eq!(get_bit(&val1, 5), 0);
        assert_eq!(get_bit(&val1, 6), 0);
        assert_eq!(get_bit(&val1, 7), 0);
        assert_eq!(get_bit(&val1, 8), 0);
        assert_eq!(get_bit(&val1, 9), 0);
        assert_eq!(get_bit(&val1, 248), 0);
        assert_eq!(get_bit(&val1, 249), 0);
        assert_eq!(get_bit(&val1, 250), 0);
        assert_eq!(get_bit(&val1, 251), 0);
        assert_eq!(get_bit(&val1, 252), 0);
        assert_eq!(get_bit(&val1, 253), 0);
        assert_eq!(get_bit(&val1, 254), 0);
        assert_eq!(get_bit(&val1, 255), 0);
    }

    #[test]
    fn test_get_bit_lsb() {
        let val1 = hex!("000000000000000000000000000000000000000000000000000000000000000f");
        assert_eq!(get_bit(&val1, 0), 0);
        assert_eq!(get_bit(&val1, 1), 0);
        assert_eq!(get_bit(&val1, 2), 0);
        assert_eq!(get_bit(&val1, 3), 0);
        assert_eq!(get_bit(&val1, 4), 0);
        assert_eq!(get_bit(&val1, 5), 0);
        assert_eq!(get_bit(&val1, 6), 0);
        assert_eq!(get_bit(&val1, 7), 0);
        assert_eq!(get_bit(&val1, 8), 0);
        assert_eq!(get_bit(&val1, 9), 0);
        assert_eq!(get_bit(&val1, 248), 0);
        assert_eq!(get_bit(&val1, 249), 0);
        assert_eq!(get_bit(&val1, 250), 0);
        assert_eq!(get_bit(&val1, 251), 0);
        assert_eq!(get_bit(&val1, 252), 1);
        assert_eq!(get_bit(&val1, 253), 1);
        assert_eq!(get_bit(&val1, 254), 1);
        assert_eq!(get_bit(&val1, 255), 1);
    }

    #[test]
    fn test_get_bit_mixed() {
        let val1 = hex!("5555000000000000000000000000000000000000000000000000000000000000");
        assert_eq!(get_bit(&val1, 0), 0);
        assert_eq!(get_bit(&val1, 1), 1);
        assert_eq!(get_bit(&val1, 2), 0);
        assert_eq!(get_bit(&val1, 3), 1);
        assert_eq!(get_bit(&val1, 4), 0);
        assert_eq!(get_bit(&val1, 5), 1);
        assert_eq!(get_bit(&val1, 6), 0);
        assert_eq!(get_bit(&val1, 7), 1);
        assert_eq!(get_bit(&val1, 8), 0);
        assert_eq!(get_bit(&val1, 9), 1);
        assert_eq!(get_bit(&val1, 10), 0);
        assert_eq!(get_bit(&val1, 11), 1);
        assert_eq!(get_bit(&val1, 12), 0);
        assert_eq!(get_bit(&val1, 13), 1);
        assert_eq!(get_bit(&val1, 14), 0);
        assert_eq!(get_bit(&val1, 15), 1);
    }

    #[allow(clippy::many_single_char_names)]
    fn merkle_tree_5() -> ([u8; 32], Vec<[u8; 32]>) {
        let a = hex!("5800000000000000000000000000000000000000000000000000000000000000");
        let b = hex!("2300000000000000000000000000000000000000000000000000000000000000");
        let c = hex!("2100000000000000000000000000000000000000000000000000000000000000");
        let d = hex!("ca00000000000000000000000000000000000000000000000000000000000000");
        let e = hex!("2000000000000000000000000000000000000000000000000000000000000000");

        // build the expected tree bottom up, since that's simpler
        let expected = hashdown(&[1, 1], &e, &c);
        let expected = hashdown(&[2, 1], &expected, &b);
        let expected = hashdown(&[2, 0], &expected, &BLANK);
        let expected = hashdown(&[2, 0], &expected, &BLANK);
        let expected = hashdown(&[2, 0], &expected, &BLANK);
        let expected = hashdown(&[0, 2], &BLANK, &expected);
        let expected = hashdown(&[2, 1], &expected, &a);
        let expected = hashdown(&[2, 1], &expected, &d);

        (expected, vec![a, b, c, d, e])
        // this tree looks like this:
        //
        //             o
        //            / \
        //           o   d
        //          / \
        //         o   a
        //        / \
        //       E   o
        //          / \
        //         o   E
        //        / \
        //       o   E
        //      / \
        //     o   E
        //    / \
        //   o   b
        //  / \
        // e   c
    }

    fn merkle_tree_left_edge() -> ([u8; 32], Vec<[u8; 32]>) {
        let a = hex!("8000000000000000000000000000000000000000000000000000000000000000");
        let b = hex!("0000000000000000000000000000000000000000000000000000000000000001");
        let c = hex!("0000000000000000000000000000000000000000000000000000000000000002");
        let d = hex!("0000000000000000000000000000000000000000000000000000000000000003");

        let mut expected = hashdown(&[1, 1], &c, &d);
        expected = hashdown(&[1, 2], &b, &expected);

        for _i in 0..253 {
            expected = hashdown(&[2, 0], &expected, &BLANK);
        }

        expected = hashdown(&[2, 1], &expected, &a);
        (expected, vec![a, b, c, d])
        // this tree looks like this:
        //           o
        //          / \
        //         o   a
        //        / \
        //       o   E
        //      / \
        //     .   E
        //     .
        //     .
        //    / \
        //   o   E
        //  / \
        // b   o
        //    / \
        //   c   d
    }

    fn merkle_tree_left_edge_duplicates() -> ([u8; 32], Vec<[u8; 32]>) {
        let a = hex!("8000000000000000000000000000000000000000000000000000000000000000");
        let b = hex!("0000000000000000000000000000000000000000000000000000000000000001");
        let c = hex!("0000000000000000000000000000000000000000000000000000000000000002");
        let d = hex!("0000000000000000000000000000000000000000000000000000000000000003");

        let mut expected = hashdown(&[1, 1], &c, &d);
        expected = hashdown(&[1, 2], &b, &expected);

        for _i in 0..253 {
            expected = hashdown(&[2, 0], &expected, &BLANK);
        }

        expected = hashdown(&[2, 1], &expected, &a);

        // all fields are duplicated
        (expected, vec![a, b, c, d, a, b, c, d])
        // this tree looks like this:
        //           o
        //          / \
        //         o   a
        //        / \
        //       o   E
        //      / \
        //     .   E
        //     .
        //     .
        //    / \
        //   o   E
        //  / \
        // b   o
        //    / \
        //   c   d
    }

    fn merkle_tree_right_edge() -> ([u8; 32], Vec<[u8; 32]>) {
        let a = hex!("4000000000000000000000000000000000000000000000000000000000000000");
        let b = hex!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
        let c = hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
        let d = hex!("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd");

        let mut expected = hashdown(&[1, 1], &c, &b);
        expected = hashdown(&[1, 2], &d, &expected);

        for _i in 0..253 {
            expected = hashdown(&[0, 2], &BLANK, &expected);
        }

        expected = hashdown(&[1, 2], &a, &expected);
        (expected, vec![a, b, c, d])
        // this tree looks like this:
        //           o
        //          / \
        //         a   o
        //            / \
        //           E   o
        //              / \
        //             E   o
        //                 .
        //                 .
        //                 .
        //                 o
        //                / \
        //               d   o
        //                  / \
        //                 c   b
    }

    pub fn merkle_set_test_cases() -> Vec<([u8; 32], Vec<[u8; 32]>)> {
        let a = hex!("7000000000000000000000000000000000000000000000000000000000000000");
        let b = hex!("7100000000000000000000000000000000000000000000000000000000000000");
        let c = hex!("8000000000000000000000000000000000000000000000000000000000000000");
        let d = hex!("8100000000000000000000000000000000000000000000000000000000000000");

        let root4 = hashdown(
            &[2_u8, 2],
            &hashdown(&[1_u8, 1], &a, &b),
            &hashdown(&[1_u8, 1], &c, &d),
        );

        let root3 = hashdown(&[2_u8, 1], &hashdown(&[1_u8, 1], &a, &b), &c);

        vec![
            // duplicates
            (BLANK, vec![]),
            (h2(&[1_u8], &a), vec![a, a]),
            (h2(&[1_u8], &a), vec![a, a, a, a]),
            // rotations (with duplicates)
            (root4, vec![a, b, c, d, a]),
            (root4, vec![b, c, d, a, a]),
            (root4, vec![c, d, a, b, a]),
            (root4, vec![d, a, b, c, a]),
            // reverse rotations (with duplicates)
            (root4, vec![d, c, b, a, a]),
            (root4, vec![c, b, a, d, a]),
            (root4, vec![b, a, d, c, a]),
            (root4, vec![a, d, c, b, a]),
            // shuffled (with duplicates)
            (root4, vec![c, a, d, b, a]),
            (root4, vec![d, c, b, a, a]),
            (root4, vec![c, d, a, b, a]),
            (root4, vec![a, b, c, d, a]),
            // singles
            (h2(&[1_u8], &a), vec![a]),
            (h2(&[1_u8], &b), vec![b]),
            (h2(&[1_u8], &c), vec![c]),
            (h2(&[1_u8], &d), vec![d]),
            // pairs
            (hashdown(&[1_u8, 1], &a, &b), vec![a, b]),
            (hashdown(&[1_u8, 1], &a, &b), vec![b, a]),
            (hashdown(&[1_u8, 1], &a, &c), vec![a, c]),
            (hashdown(&[1_u8, 1], &a, &c), vec![c, a]),
            (hashdown(&[1_u8, 1], &a, &d), vec![a, d]),
            (hashdown(&[1_u8, 1], &a, &d), vec![d, a]),
            (hashdown(&[1_u8, 1], &b, &c), vec![b, c]),
            (hashdown(&[1_u8, 1], &b, &c), vec![c, b]),
            (hashdown(&[1_u8, 1], &b, &d), vec![b, d]),
            (hashdown(&[1_u8, 1], &b, &d), vec![d, b]),
            (hashdown(&[1_u8, 1], &c, &d), vec![c, d]),
            (hashdown(&[1_u8, 1], &c, &d), vec![d, c]),
            // triples
            (root3, vec![a, b, c]),
            (root3, vec![a, c, b]),
            (root3, vec![b, a, c]),
            (root3, vec![b, c, a]),
            (root3, vec![c, a, b]),
            (root3, vec![c, b, a]),
            // quads
            // rotations
            (root4, vec![a, b, c, d]),
            (root4, vec![b, c, d, a]),
            (root4, vec![c, d, a, b]),
            (root4, vec![d, a, b, c]),
            // reverse rotations
            (root4, vec![d, c, b, a]),
            (root4, vec![c, b, a, d]),
            (root4, vec![b, a, d, c]),
            (root4, vec![a, d, c, b]),
            // shuffled
            (root4, vec![c, a, d, b]),
            (root4, vec![d, c, b, a]),
            (root4, vec![c, d, a, b]),
            (root4, vec![a, b, c, d]),
            // a few special case trees
            merkle_tree_5(),
            merkle_tree_left_edge(),
            merkle_tree_left_edge_duplicates(),
            merkle_tree_right_edge(),
        ]
    }

    #[test]
    fn test_compute_merkle_root() {
        for (root, mut leafs) in merkle_set_test_cases() {
            assert_eq!(compute_merkle_set_root(&mut leafs), root);
        }
    }
}