Skip to content

Instantly share code, notes, and snippets.

@ccastromar
Last active February 17, 2020 20:11
Show Gist options
  • Save ccastromar/ca80173bc5e8bbe88c57e62471fb38c2 to your computer and use it in GitHub Desktop.
Save ccastromar/ca80173bc5e8bbe88c57e62471fb38c2 to your computer and use it in GitHub Desktop.
Iteracion 2
# Copyright 2020 Carlos Castro Martos
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from perkle import MerkleTree
from Crypto.Hash import SHA3_256
sha3 = lambda x : SHA3_256.new(x).digest()
t1 = b"transaccion1"
t2 = b"transaccion2"
t3 = b"transaccion3"
t4 = b"transaccion4"
txs = [t1, t2, t3]
for t in txs:
print("La tx ",t, " tiene el hash ", sha3(t).hex())
m = MerkleTree(txs, sha3)
print("El hash raiz es:", m.root().hex())
print("El árbol de Merkle contiene los nodos:")
for n in m.tree:
for i in m.tree[n]:
print(n, i.hex())
index, proof_hashes = m.proof(b'transaccion3')
print("¿La transaccion4 con el hash", sha3(t4).hex(), "está en el árbol ?")
print(MerkleTree.verify(b'transaccion4',index,proof_hashes,m.root(),sha3))
print("¿La transaccion3 con el hash", sha3(t3).hex(), "está en el árbol ?")
print(MerkleTree.verify(b'transaccion3',index,proof_hashes,m.root(),sha3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment