Skip to main content

hash_node_ref

Function hash_node_ref 

Source
pub fn hash_node_ref(
    node: &NodeRef,
    v: &Validation,
) -> Result<Option<(NodeHash, Option<TallyList>)>, String>
Expand description

Get the hash of a node in a merkle tally tree.

The hash value is generated from this nodes tally and if applicable the hash of the nodes child nodes or vote reference.

Example:

use tallytree::generate::generate_tree;
use tallytree::hash::hash_node_ref;
use tallytree::Validation;
let tree = generate_tree(vec![
    ([0xaa; 32], vec![1, 0]),
    ([0xbb; 32], vec![0, 1]),
    ([0xcc; 32], vec![1, 0]),
], false).unwrap();
let hash = hash_node_ref(&tree, &Validation::Strict);
let hash_child = hash_node_ref(&tree.unwrap().left, &Validation::Strict);
Examples found in repository?
examples/create-proofs.rs (line 26)
10fn main() {
11    // A vote with 3 voters where:
12    //
13    // - 0xaa votes for the first option
14    // - 0xcc votes for the first option
15    // - 0xdd votes for the second option.
16    let tree = generate_tree(
17        vec![
18            ([0xaa; 32], vec![1, 0]),
19            ([0xcc; 32], vec![1, 0]),
20            ([0xdd; 32], vec![0, 1]),
21        ],
22        true,
23    )
24    .unwrap();
25    let v = &Validation::Strict;
26    let merkle_root_hash = hash_node_ref(&tree, v).unwrap().unwrap().0;
27
28    // Prove that 0xaa's vote was tallied.
29    let (inclusion, _) = create_inclusion_exclusion_proof(&tree, &[0xaa; 32], v).unwrap();
30    let (proof_hash, _, vote) = verify_inclusion_proof(&inclusion, &[0xaa; 32], v).unwrap();
31    assert_eq!(proof_hash, merkle_root_hash);
32    println!("Voter 0xaa has provably voted {:?}.", vote);
33
34    // Proof that 0xbb did not cast a vote.
35    let (_, exclusion) = create_inclusion_exclusion_proof(&tree, &[0xbb; 32], v).unwrap();
36    let (proof_hash, _) = verify_exclusion_proof(&exclusion, &[0xbb; 32], v).unwrap();
37    assert_eq!(proof_hash, merkle_root_hash);
38    println!("Voter 0xbb has provably NOT voted.");
39
40    // Proof that there are votes in the merkle tally tree.
41    let votes_proof = create_proof_of_vote_count(&tree, v).unwrap();
42    let (votes, proof_hash, _) = verify_proof_of_vote_count(&votes_proof).unwrap();
43    assert_eq!(proof_hash, merkle_root_hash);
44    assert_eq!(votes, 3);
45    println!("There are provably 3 votes in the merkle tally tree.");
46}