예제 #1
0
파일: mast.py 프로젝트: seanjensengrey/MAST
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
예제 #2
0
파일: mast.py 프로젝트: JeremyRubin/MAST
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
예제 #3
0
파일: mast.py 프로젝트: JeremyRubin/MAST
 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
예제 #4
0
파일: mast.py 프로젝트: seanjensengrey/MAST
 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
예제 #5
0
파일: mast.py 프로젝트: seanjensengrey/MAST
    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:
예제 #6
0
파일: mast.py 프로젝트: JeremyRubin/MAST
        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])