def main(): N = 4 # Limited number of messages # Generate 'N' private/public key pairs (Xi, Yi) from the Lamport signature scheme key_pairs = [LamportSignature() for _ in range(N) ] # Private key of the Merkle signature scheme # The leaves of the tree are the hashed values of the public keys Y0, ..., YN mk = MerkleTree(n_leaves=N) for i in range(N): mk.add_node(key_pairs[i].get_key('public', concatenate=True), (0, i), hashed=False) # Build Merkle tree mk.generate_tree() pub = mk.get_root() # Public key of the Merkle signature scheme # Merkle signature generation using a chosen pair of keys (Xi, Yi) from the Lamport signature scheme # sig = concatenate(sig_prime, Yi, auth(0), ..., auth(n-1)) # see https://en.wikipedia.org/wiki/Merkle_signature_scheme pair = 3 # (Xi, Yi) pair number sig = [] M = "test" sig_prime = key_pairs[pair].sign(M) sig.append(sig_prime) # Add sig_prime to the signature 'sig'. sig.append(key_pairs[pair].get_key( 'public', concatenate=True)) # Add Yi to the signature 'sig'. sig.append(mk.get_authentification_path_hashes( pair)) # Add auth(0), ..., auth(n-1) to the signature 'sig'. # Send to receiver the public key 'pub' (tree root), the message 'M' and the Merkle signature 'sig'. client = Client() msg = (N, pair, pub, M, sig) client.send(pickle.dumps(msg)) client.close()
def test_root_even_transactions(self): # based on the block #125552 in the mainchain transactions = [ TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("51d37bdd871c9e1f4d5541be67a6ab62" "5e32028744d7d4609d0c37747b40cd2d"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("60c25dda8d41f8d3d7d5c6249e2ea1b0" "5a25bf7ae2ad6d904b512b31f997e1a1"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("01f314cdd8566d3e5dbdd97de2d9fbfb" "fd6873e916a00d48758282cbb81a45b9"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("b519286a1040da6ad83c783eb2872659" "eaf57b1bec088e614776ffe7dc8f6d01"))) ] tree = MerkleTree(transactions) root = bytes.fromhex( "2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3") self.assertEqual(root, tree.root)
def hash_transactions(self): ''' get hashed transactions by merkle tree ''' tx_byte_list = [utils.serialize(tx) for tx in self._transaction] merkle_tree = MerkleTree(tx_byte_list) return utils.decode(binascii.hexlify(merkle_tree.root_hash))
def hash_transactions(self): tx_byte_lst = [] for tx in self._tx_lst: tx_byte_lst.append(tx.to_bytes()) m_tree = MerkleTree(tx_byte_lst) return utils.decode(binascii.hexlify(m_tree.root_hash))
def hashTX(self): # return a hash of the transactions in the block tx_hashs = [] for tx in self.data: tx_hashs.append(pickle.dumps(tx.ID)) m_tree = MerkleTree(tx_hashs) return utils.sum256(m_tree.root_hash)
def recvDecision(self, final_decision, body): if final_decision == 'commit': block = pickle.loads(body['block']) if not self.current_execution or self.current_execution.block.bid != block.bid: raise ValueError('Invalid CurrentExecution. Decision invalid.') self.bch.appendBlock(block) for txn in block.txns: for k in txn.rw_set.write_set.keys(): if k in self.data: self.data[k] = (self.mht.kv_map[k], block.bid, block.bid) elif final_decision == 'abort': kv_map = { k : self.data[k][0] for k in self.data } self.mht = MerkleTree(kv_map) self.current_execution = None
def __init__(self, filepath : str): self.pointers = {} self.dirtype = isdir(filepath) self.filename = Path(filepath).name if not self.dirtype: with open(filepath) as f: self.content = f.read() self.hash = self._hash((self.filename + self.content).encode('utf-8')) else: self.content = self._iterate_directory_contents(filepath) nodes_in_str_array = list(map(lambda x: str(x), self.content)) if nodes_in_str_array: self.hash = self._hash((self.filename + MerkleTree(nodes_in_str_array).root_hash).encode('utf-8')) else: self.hash = self._hash(self.filename.encode('utf-8'))
def create_merkle(self, transaction_queue, tx_to_ignore=None): block = self.blockchain.last_block() if block is None: # genesis block, create a ledger ledger = Ledger() else: # work on latest ledger ledger = block.ledger list_of_raw_transactions = [] list_of_validated_transactions = [] # get transactions from queue while not transaction_queue.empty(): list_of_raw_transactions.append(transaction_queue.get()) # verify transactions for transaction in list_of_raw_transactions: if tx_to_ignore is not None and transaction in tx_to_ignore: print(f"Ignoring transaction: {transaction}") elif ledger.verify_transaction( transaction, list_of_validated_transactions, binascii.hexlify(block.header_hash()).decode(), self.blockchain): # only verified transactions get included in merkletree list_of_validated_transactions.append(transaction) print("Verification complete.") merkletree = MerkleTree() # coinbase transaction is the first tx in the merkle tree coinbase_sender_pk = SigningKey.generate() coinbase_sender_vk = coinbase_sender_pk.get_verifying_key() merkletree.add( Transaction(coinbase_sender_vk, self.public_key, 100, sender_pk=coinbase_sender_pk).to_json()) ledger.coinbase_transaction(self.public_key_str) # add other transactions to merkle tree for transaction in list_of_validated_transactions: transaction_object = transaction.to_json() merkletree.add(transaction_object) merkletree.build() return merkletree, ledger
def __init__(self, idx, ver, pre_hash, ts, fee, tr_list, thresh, addr): self.idx = idx self.ver = ver self.pre_hash = pre_hash self.ts = ts self.fee = fee self.thresh = thresh self.addr = addr self.nonce = 0 self.tr_list = tr_list mt = MerkleTree(self.tr_list) if len(tr_list) > 0: self.mt_root = mt.root_val else: self.mt_root = utils.get_hash(None)
def test_root_odd_transactions(self): # based on the block #125553 in the mainchain transactions = [ TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("3cfc035221a3d8eb8cdef98330467dea" "51ee8f75cf0cfa2fcc1bb1e150191e57"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("446e1c006bbc0d61f7fe4f6a325d468d" "6dd6016ace9b611370c9854e57aab0ac"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("d59ad81b484be54d97f693cfe1a5f450" "1948ffce13f4a558e6feb3713a1eeff6"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("6d1e61f43ec0eba4804ad74aaeff1e13" "7bef9bdf57098c352e6e8aeb27b95c6a"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("068b30fb5b6989bfbe0c0d5e5bca5dd9" "f9eb100dda21fbac0b16fed436da8f0b"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("fd48d02e10d6629d385f642879b0dbe6" "a12551fd011797f82dc80d56216bfcc4"))), TestMerkleTree.TransactionMock( to_rpc_byte_order( bytes.fromhex("2ec24502228833d687e9036a047f9d33" "880f34b237a6703c864de55a6df1013a"))) ] tree = MerkleTree(transactions) root = bytes.fromhex( "53fb6ea244d5f501a22c95c4c56701d70a6e115c5476ed95280cb22149c171b3") self.assertEqual(root, tree.root)
def __init__(self, coinbase_data, coinbase_value, wallet_address, transactions, previous_block_hash, difficulty_bits, target, time): """ Takes the given raw data from a mining request and produces a mineable block accessible from the attribute `block`. The target hash value will be held in the attribute `target`. :param coinbase_data: data to include as a script to the TxIn of the coinbase transaction :param coinbase_value: Reward for mining this block. This value should contain the block subsidy (based on height of the blockchain) plus the fees of the included `transactions`. :param wallet_address: address that will receive the bitcoins mined (through the coinbase transaction) :param transactions: transactions that we want to put in the mineable block :param previous_block_hash: hash (RPC format) of the previous block :param difficulty_bits: packed representation (bits) of the current network's difficulty as an integer :param target: target value for the mining task, which indicates that we must mine a block that hashes to a value below that target :param time: unix time since epoch in seconds """ pubkey = p2pkh_address_to_pubkey_hash(wallet_address) coinbase = serialize_coinbase_transaction(pubkey, coinbase_data, coinbase_value) coinbase_tx = Transaction(coinbase) transactions = [coinbase_tx] + transactions tree = MerkleTree(transactions) self.target = target self.block = Block(previous_block_hash, tree, time, difficulty_bits)
def main(): N = 4 # Limited number of messages # Generate 'N' private/public key pairs (Xi, Yi) from the Lamport signature scheme key_pairs = [LamportSignature() for _ in range(N) ] # Private key of the Merkle signature scheme # The leaves of the tree are the hashed values of the public keys Y0, ..., YN mk = MerkleTree(n_leaves=N) for i in range(N): mk.add_node(key_pairs[i].get_key('public', concatenate=True), (0, i), hashed=False) # Build Merkle tree mk.generate_tree() pub = mk.get_root() # Public key of the Merkle signature scheme # Merkle signature generation using a chosen pair of keys (Xi, Yi) from the Lamport signature scheme # sig = concatenate(sig_prime, Yi, auth(0), ..., auth(n-1)) # see https://en.wikipedia.org/wiki/Merkle_signature_scheme pair = 3 # (Xi, Yi) pair number sig = [] M = "test" sig_prime = key_pairs[pair].sign(M) sig.append(sig_prime) # Add sig_prime to the signature 'sig'. sig.append(key_pairs[pair].get_key( 'public', concatenate=True)) # Add Yi to the signature 'sig'. sig.append(mk.get_authentification_path_hashes( pair)) # Add auth(0), ..., auth(n-1) to the signature 'sig'. ###################################################################### # Merkle signature verification. ###################################################################### # Receiver knows the public key 'pub' (tree root), the message 'M' and the Merkle signature 'sig'. pub_receiver = pub M_receiver = M sig_receiver = sig # First, the receiver verifies the one time signature 'sig_prime' of the message 'M' using the Lamport key 'Yi'. print("Check one time signature of the received message: " + M_receiver) result = LamportSignature.verify( M_receiver, sig_receiver[0], LamportSignature.decatenate_key(sig_receiver[1])) print("One-time signature is: " + str(result)) # If 'sig_prime' is a valid signature of 'M', the receiver computes the leaf corresponding to the Lamport key 'Yi'. if result: mk_receiver = MerkleTree(n_leaves=N) mk_receiver.add_node(sig_receiver[1], (0, pair), hashed=False) mk_receiver.generate_tree() # The nodes of the authentification path are computed. for i, (level, index) in enumerate( mk_receiver.get_authentification_path(pair)): mk_receiver.add_node(sig_receiver[2][i], (level, index), hashed=True) mk_receiver.generate_tree() # If the tree's root equals the public key 'pub' the signature is valid. result = mk_receiver.get_root() == pub_receiver print("Merkle signature is: " + str(result))
def __init__(self, *args, **kwargs): kwargs.setdefault('transactions', MerkleTree()) super().__init__(*args, **kwargs)
def main(): # Merkle signature verification. server = Server() data = server.receive() server.close() data = pickle.loads(data) # Receiver knows the public key 'pub' (tree root), the message 'M' and the Merkle signature 'sig'. N, pair, pub_receiver, M_receiver, sig_receiver = data # First, the receiver verifies the one time signature 'sig_prime' of the message 'M' using the Lamport key 'Yi'. print("Check one time signature of the received message: " + M_receiver) result = LamportSignature.verify( M_receiver, sig_receiver[0], LamportSignature.decatenate_key(sig_receiver[1])) print("One-time signature is: " + str(result)) # If 'sig_prime' is a valid signature of 'M', the receiver computes the leaf corresponding to the Lamport key 'Yi'. if result: mk_receiver = MerkleTree(n_leaves=N) mk_receiver.add_node(sig_receiver[1], (0, pair), hashed=False) mk_receiver.generate_tree() # The nodes of the authentification path are computed. for i, (level, index) in enumerate( mk_receiver.get_authentification_path(pair)): mk_receiver.add_node(sig_receiver[2][i], (level, index), hashed=True) mk_receiver.generate_tree() # If the tree's root equals the public key 'pub' the signature is valid. result = mk_receiver.get_root() == pub_receiver print("Merkle signature is: " + str(result))
from merkle_tree import MerkleTree, verify_audit_trail file = '01234567' chunks = list(file) merkle_tree = MerkleTree(chunks) print("root hash=", merkle_tree.root.hash) chunk_hash = MerkleTree.compute_hash("2") print("Hash of '2' is:", chunk_hash) audit_trail = merkle_tree.get_audit_trail(chunk_hash) print("audit trail:", audit_trail) print('start verify:') result = verify_audit_trail(chunk_hash, audit_trail) print(result)
def createMHT(shard_i, num_elements): strt = shard_i * num_elements + 1 kv_map = { bytes('k'+str(i), 'utf-8'): bytes('v'+str(i), 'utf-8') for i in range(strt, strt + num_elements) } return MerkleTree(kv_map)
from os import listdir from hashlib import sha256 from merkle_tree import MerkleTree hashes = {} file = 'hello_big.txt' with open(file) as f: lines = f.read().split('\n') hash = [] hash_of_hash = [] merkle_tree = MerkleTree(lines) root_hash = merkle_tree.root_hash hashes[root_hash] = [] for line in lines: hashes[root_hash].append(line) print(hashes)
def upload_file(): if request.method == 'POST': time = str(int(datetime.now().timestamp())) f = request.files['file'] f.save(secure_filename("student_data.csv")) #print(request) filedata = pd.read_csv('student_data.csv') tree = MerkleTree() columns = list(filedata.columns) certyear = int(0) for i in range(len(filedata)): st = "" data = {} certyear = int(str(filedata['Year'][i])) for j in range(len(columns)): s = filedata[columns[j]][i] data[columns[j]] = str(s) st = st + str(s) json_data = json.dumps(data) tree.add(st) tree.createTree() directory = "Year" + str(certyear) + "-" + time Utils.writeToFile(directory, "root.txt", Web3.toHex(tree.getMerkleRoot().value)) # generate the certificate json with hash and merklepath in header # and data in certificate for i in range(len(filedata)): data = {} header = {} header['hash'] = Web3.toHex(tree.getLeafHash(i)) path = [] for x in range(len(tree.getMerklePath(i))): path.append(Web3.toHex(tree.getMerklePath(i)[x])) header['merkleproof'] = path data['header'] = header certificateData = {} for j in range(len(columns)): certificateData[columns[j]] = str(filedata[columns[j]][i]) data['certificate'] = certificateData json_data = json.dumps(data) #qr = pyqrcode.create(str(json_data)) #qr.png(Utils.savePath + directory+"/"+certificateData['ID']+'.png', scale=5) Utils.writeToFile(directory, certificateData['ID'] + ".json", json_data) z = Utils.createZip(directory) print("Created file : " + z) return render_template('publish.html', roothash=Web3.toHex(tree.getMerkleRoot().value), year=certyear, downloadpath=z) else: abort(404)