def prove(proofList, data, mroot, debug=False): if debug: print print "---- Prove Root" print "mr = ", mroot print lastHash = proofList[0][0] dataQ = deque(data) for c1, c2 in proofList: if debug: print "PROVE:", lastHash print "HASHES:", c1, c2 print "HASH TO:", crypto.hashable(c1 + c2).hash() print if lastHash not in [c1, c2]: return False if dataQ and dataQ[0].hash() in [c1, c2]: dataQ.popleft() lastHash = crypto.hashable(c1 + c2).hash() if debug: print "FINAL HASH:", lastHash, lastHash == mroot print return (not dataQ) and lastHash == mroot
def prove(proofList, data, mroot, debug=False): if debug: print print "---- Prove Root" print "mr = ", mroot print lastHash = proofList[0][0] dataQ = deque(data) for c1, c2 in proofList: if debug: print "PROVE:", lastHash print "HASHES:", c1, c2 print "HASH TO:", crypto.hashable(c1+c2).hash() print if lastHash not in [c1,c2]: return False if dataQ and dataQ[0].hash() in [c1,c2]: dataQ.popleft() lastHash = crypto.hashable(c1+c2).hash() if debug: print "FINAL HASH:", lastHash, lastHash ==mroot print return (not dataQ) and lastHash == mroot
def __init__(self, items): self.items = map(MerkleNode, items) things = deque(self.items) if len(things) == 0: raise ValueError("Cannot Construct Merkle Tree with empty list") while True: fst = things.popleft() if things: snd = things.popleft() p = MerkleNode(crypto.hashable(fst.hash()+snd.hash()), c1=fst, c2=snd) snd.parent = p fst.parent = p things.append(p) else: self.node = fst break
def __init__(self, items): self.items = map(MerkleNode, items) things = deque(self.items) if len(things) == 0: raise ValueError("Cannot Construct Merkle Tree with empty list") while True: fst = things.popleft() if things: snd = things.popleft() p = MerkleNode(crypto.hashable(fst.hash() + snd.hash()), c1=fst, c2=snd) snd.parent = p fst.parent = p things.append(p) else: self.node = fst break
return (not dataQ) and lastHash == mroot # Given a megaproof from generateFullProofUpward, # Verify all of the content is correct and everying can be proved def upwardProve((proofList, data, mroot), megaroot, debug=False): if debug: print print "##################" print "###Begin Proof####" print "##################" # THe map hashable is critical as data gets popped if not prove(proofList, map(hashable, data[:-1]), mroot, debug=debug): return False l = proofList[-1] # TODO is there a reason this can't just go into prove? return crypto.hashable(l[0] + l[1]).hash() == megaroot and hashable( data[-1]).hash() in l[-1] def toScript((pl, data, mroot), megaroot): dataQ = deque(data[1:]) code = putStr(data[0]) code.extend([OP_DUP, OP_TOALTSTACK, OP_SHA256, OP_NOP, OP_NOP]) lastHash = hashable(data[0]).hash() b = base64.b64encode for i, (c1, c2) in enumerate(pl): h = hashable(dataQ[0]).hash() if dataQ else None print map(base64.b64encode, [c1, c2, lastHash]) if dataQ and h in [c1, c2]: d = dataQ.popleft() if hashable(d).hash() == c1:
print return (not dataQ) and lastHash == mroot # Given a megaproof from generateFullProofUpward, # Verify all of the content is correct and everying can be proved def upwardProve((proofList, data, mroot), megaroot, debug=False): if debug: print print "##################" print "###Begin Proof####" print "##################" # THe map hashable is critical as data gets popped if not prove(proofList, map(hashable,data[:-1]), mroot, debug=debug): return False l = proofList[-1] # TODO is there a reason this can't just go into prove? return crypto.hashable(l[0]+l[1]).hash() == megaroot and hashable(data[-1]).hash() in l[-1] def toScript((pl, data, mroot), megaroot): dataQ = deque(data[1:]) code = putStr(data[0]) code.extend([OP_DUP, OP_TOALTSTACK, OP_SHA256, OP_NOP, OP_NOP]) lastHash = hashable(data[0]).hash() b = base64.b64encode for i, (c1, c2) in enumerate(pl): h = hashable(dataQ[0]).hash() if dataQ else None print map(base64.b64encode,[c1, c2, lastHash]) if dataQ and h in [c1,c2]: d = dataQ.popleft() if hashable(d).hash() == c1: code.extend(putStr(c1)) code.extend(putStr(d)) code.extend([OP_2DUP, OP_SHA256, OP_EQUALVERIFY, OP_DROP, OP_TOALTSTACK])