Ejemplo n.º 1
0
def test_create_membership_proof():
    leaves = [b'a', b'b', b'c']
    merkle = FixedMerkle(2, leaves)
    proof_1 = merkle.create_membership_proof(b'a')
    proof_2 = merkle.create_membership_proof(b'c')
    assert merkle.check_membership(b'a', 0, proof_1) is True
    assert merkle.check_membership(b'c', 2, proof_2) is True
Ejemplo n.º 2
0
def test_check_membership(u):
    leaf_1 = b'\xff' * 31 + b'\x01'
    leaf_2 = b'\xff' * 31 + b'\x02'
    leaf_3 = b'\xff' * 31 + b'\x03'
    leaf_4 = b'\xff' * 31 + b'\x04'
    root = u.sha3(u.sha3(leaf_1 + leaf_2) + u.sha3(leaf_3 + leaf_4))
    zeros_hashes = get_empty_merkle_tree_hash(2)
    for i in range(13):
        root = u.sha3(root + zeros_hashes[-32:])
        zeros_hashes += u.sha3(zeros_hashes[-32:] + zeros_hashes[-32:])
    left_proof = leaf_2 + u.sha3(leaf_3 + leaf_4) + zeros_hashes
    left_middle_proof = leaf_1 + u.sha3(leaf_3 + leaf_4) + zeros_hashes
    right_middle_proof = leaf_4 + u.sha3(leaf_1 + leaf_2) + zeros_hashes
    right_proof = leaf_3 + u.sha3(leaf_1 + leaf_2) + zeros_hashes
    fixed_merkle = FixedMerkle(16, [leaf_1, leaf_2, leaf_3, leaf_4], True)
    assert fixed_merkle.check_membership(leaf_1, 0, left_proof) is True
    assert fixed_merkle.check_membership(leaf_2, 1, left_middle_proof) is True
    assert fixed_merkle.check_membership(leaf_3, 2, right_middle_proof) is True
    assert fixed_merkle.check_membership(leaf_4, 3, right_proof) is True