def calculate_merkle_link(hashes, index): # XXX optimize this hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)] while len(hash_list) > 1: hash_list = [( lambda _left=left, _right=right: sha3( merkle_record_type.pack(dict(left=_left(), right=_right()))), left_f or right_f, (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)], ) for (left, left_f, left_l), ( right, right_f, right_l) in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])] res = [x['hash']() for x in hash_list[0][2]] assert hash_list[0][1] if p2pool.DEBUG: new_hashes = [ random.randrange(2**256) if x is None else x for x in hashes ] assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes) assert index == sum( k * 2**i for i, k in enumerate([1 - x['side'] for x in hash_list[0][2]])) return dict(branch=res, index=index)
def check_merkle_link(tip_hash, link): if link['index'] >= 2**len(link['branch']): raise ValueError('index too large') return reduce(lambda c, (i, h): sha3(merkle_record_type.pack( dict(left=h, right=c) if (link['index'] >> i) & 1 else dict(left=c, right=h) )), enumerate(link['branch']), tip_hash)
def calculate_merkle_link(hashes, index): # XXX optimize this hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)] while len(hash_list) > 1: hash_list = [ ( lambda _left=left, _right=right: sha3(merkle_record_type.pack(dict(left=_left(), right=_right()))), left_f or right_f, (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)], ) for (left, left_f, left_l), (right, right_f, right_l) in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]]) ] res = [x['hash']() for x in hash_list[0][2]] assert hash_list[0][1] if p2pool.DEBUG: new_hashes = [random.randrange(2**256) if x is None else x for x in hashes] assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes) assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]])) return dict(branch=res, index=index)
def merkle_hash(hashes): if not hashes: return 0 hash_list = list(hashes) while len(hash_list) > 1: hash_list = [sha3(merkle_record_type.pack(dict(left=left, right=right))) for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])] return hash_list[0]
def check_merkle_link(tip_hash, link): if link['index'] >= 2**len(link['branch']): raise ValueError('index too large') return reduce( lambda c, (i, h): sha3( merkle_record_type.pack( dict(left=h, right=c) if (link['index'] >> i) & 1 else dict(left=c, right=h))), enumerate(link['branch']), tip_hash)
def merkle_hash(hashes): if not hashes: return 0 hash_list = list(hashes) while len(hash_list) > 1: hash_list = [ sha3(merkle_record_type.pack(dict(left=left, right=right))) for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]]) ] return hash_list[0]
def sign(message, privateKey): curve = Curve() r = 0 s = 0 while r == 0 or s == 0: k = random.randint(1, curve.n-1) R = multiply(curve.G, k) R.x = int(R.x) r = R.x % curve.n modResult = inverse_mod(k, curve.n) byteMessageDigest = bytearray(sha3(message)) e = 0 for byte in byteMessageDigest: e += byte s = (modResult * (e + privateKey * r)) % curve.n # Embed signature at the beginning of message signature = "\n--BEGIN SIGNATURE--\n" + str(hex(r)) + "\n" + str(hex(s)) + "\n--END SIGNATURE--\n" message = message + signature return message
def verify(messageEmbed): curve = Curve() inf = float('inf') # Extract signature from message # messageEmbed = messageEmbed.replace("\\n", "\n") messageArr = messageEmbed.split("\n--END SIGNATURE--\n") if (len(messageArr) < 2): return "Message not digitally signed" publicKeyString = messageArr[1].split(",") x_point = int(publicKeyString[0]) y_point = int(publicKeyString[1]) publicKey = Point(x_point, y_point) messageArr2 = messageArr[0].split("\n--BEGIN SIGNATURE--\n") signature = messageArr2[1].split("\n") message = messageArr2[0] r = int(signature[0], 16) s = int(signature[1], 16) if (r >= 1 and r <= curve.n-1 and s >= 1 and s <= curve.n-1): byteMessageDigest = bytearray(sha3(message)) e = 0 for byte in byteMessageDigest: e += byte w = inverse_mod(s, curve.n) u1 = ((e % curve.n) * w) % curve.n u2 = ((r % curve.n) * w) % curve.n X = add(multiply(curve.G, u1), multiply(publicKey, u2)) if (X.x == inf): return False X.x = int(X.x) v = X.x % curve.n return v == r else: return False
def __add_key_value_pair__(self,key_str,value): hash_object = sha3.keccak_256('b'key_str) [div, ind] = divmod(sha3(key_str),self.m)