def verify_proof(commit: Root, proof: Sequence[Root], leaf: Root, leaf_gindex: Gindex) -> bool: bit_iter, _ = gindex_bit_iter(leaf_gindex) dat: Root = leaf for bit, sibling in zip(reversed(list(bit_iter)), proof): if bit: # is the leaf on the right side? dat = merkle_hash(sibling, dat) else: dat = merkle_hash(dat, sibling) return dat == commit
def build_proof(anchor, leaf_index): if leaf_index <= 1: return [] # Nothing to prove / invalid index node = anchor proof = [] # Walk down, top to bottom to the leaf bit_iter, _ = gindex_bit_iter(leaf_index) for bit in bit_iter: # Always take the opposite hand for the proof. # 1 = right as leaf, thus get left if bit: proof.append(node.get_left().merkle_root()) node = node.get_right() else: proof.append(node.get_right().merkle_root()) node = node.get_left() return list(reversed(proof))
def build_proof(anchor: Node, leaf_index: Gindex) -> Sequence[Root]: if leaf_index <= 1: return [] # Nothing to proof / invalid index node = anchor proof = [] # Walk down, top to bottom to the leaf bit_iter, _ = gindex_bit_iter(leaf_index) for bit in bit_iter: # Always take the opposite hand for the proof. # 1 = right as leaf, thus get left if bit: proof.append(node.get_left().merkle_root()) node = node.get_right() else: proof.append(node.get_right().merkle_root()) node = node.get_left() # we want to see bottom to top proof.reverse() return proof