Skip to main content

create_proofs/
create-proofs.rs

1extern crate tallytree;
2use tallytree::generate::generate_tree;
3use tallytree::hash::hash_node_ref;
4use tallytree::proof::{
5    create_inclusion_exclusion_proof, create_proof_of_vote_count, verify_exclusion_proof,
6    verify_inclusion_proof, verify_proof_of_vote_count,
7};
8use tallytree::Validation;
9
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}