def verify_multiple( pubkeys: Sequence[BLSPubkey], message_hashes: Sequence[Hash32], signature: BLSSignature, domain: Domain, ) -> bool: len_msgs = len(message_hashes) len_pubkeys = len(pubkeys) if len_pubkeys != len_msgs: raise ValueError( "len(pubkeys) (%s) should be equal to len(message_hashes) (%s)" % (len_pubkeys, len_msgs)) message_hashes_with_domain = [ combine_domain(message_hash, domain) for message_hash in message_hashes ] pubkeys_chia = map(_pubkey_from_bytes, pubkeys) aggregate_infos = [ AggregationInfo.from_msg(pubkey_chia, message_hash) for pubkey_chia, message_hash in zip(pubkeys_chia, message_hashes_with_domain) ] merged_info = AggregationInfo.merge_infos(aggregate_infos) signature_chia = _signature_from_bytes(signature) signature_chia.set_aggregation_info(merged_info) return cast(bool, signature_chia.verify())
def verified_commit(blockchain, logger, values): block_hash = values.get('block') if block_hash is None: return jsonify("tempered Data"), 401 block = blockchain.commit_verified_list.get(block_hash) if block is None: return jsonify("verification block missing"), 402 signers = values.get('n_list') co_sig = values.get('co_sig') if (signers is None) or (co_sig is None): return jsonify("tempered block data"), 403 co_sig = BLS.deserialize(co_sig, Signature) flag = block.verify_block(blockchain) if not flag: return jsonify("invalid block!"), 301 node_address = block.harvester block_hash_hexdigest = block.get_hash() if node_address in blockchain.public_key_list: if len(signers) / len(blockchain.public_key_list) > 0.66: temp_array = [] for c in block_hash_hexdigest: temp_array.append(ord(c)) msg = bytes(temp_array) agg_info_list = [] for node in signers: if node in blockchain.public_key_list: agg_info = AggregationInfo.from_msg( PublicKey.from_bytes( bytes(blockchain.public_key_list[node], "ISO-8859-1")), msg) agg_info_list.append(agg_info) else: return jsonify("BlockChain couldn't updated "), 302 agg_public_key = AggregationInfo.merge_infos(agg_info_list) co_sig.set_aggregation_info(agg_public_key) verify_signature = co_sig.verify() if verify_signature: logger.debug("hey you verified commit block" + block.get_hash()) block.signers = signers block.signature = values.get('co_sig') blockchain.update_blockchain(block) return jsonify("BlockChain should be updated "), 200 else: return jsonify("BlockChain couldn't updated "), 303 else: logger.warning("you didn't get majority") return jsonify("BlockChain couldn't updated "), 304 logger.debug("node address didn't exists") return jsonify("BlockChain couldn't updated "), 305
def verify_sign(_data, _signature, public_key_list): if type(_signature) != Signature: _signature = BLS.deserialize(_signature, Signature) if type(_signature) != Signature: return None if type(_data) != str: return None temp_array = [] for c in _data: temp_array.append(ord(c)) msg = bytes(temp_array) agg_info_list = [] if type(public_key_list) == list: for pk in public_key_list: if type(pk) == PublicKey: agg_info = AggregationInfo.from_msg(pk, msg) agg_info_list.append(agg_info) else: pk = BLS.deserialize(pk, PublicKey) if type(pk) == PublicKey: agg_info = AggregationInfo.from_msg(pk, msg) agg_info_list.append(agg_info) else: return None else: if type(public_key_list) == PrivateKey: agg_info = AggregationInfo.from_msg(public_key_list, msg) agg_info_list.append(agg_info) else: public_key_list = BLS.deserialize(public_key_list, PublicKey) if type(public_key_list) == PublicKey: agg_info = AggregationInfo.from_msg(public_key_list, msg) agg_info_list.append(agg_info) else: return None agg_public_key = AggregationInfo.merge_infos(agg_info_list) _signature.set_aggregation_info(agg_public_key) return _signature.verify()
def verify_block_signature(self, blockchain): if type(self.txn) == list: txn_hash = self.txn txn_hash.sort() elif type(self.txn) == dict: txn_hash = list(self.txn.keys()) txn_hash.sort() else: return False msg = dumps({ "index": self.index, "harvester": self.harvester, "previous_hash": self.previous_hash, "txn": txn_hash, "signature": "", "signers": "", "timestamp": self.timestamp, }) temp_array = [] for c in msg: temp_array.append(ord(c)) msg = bytes(temp_array) if type(self.signature) == Signature: pass else: self.signature = BLS.deserialize(self.signature, Signature) agg_info_list = [] for node in self.signers: if node in blockchain.public_key_list: agg_info = AggregationInfo.from_msg( PublicKey.from_bytes( bytes(blockchain.public_key_list[node], "ISO-8859-1")), msg) agg_info_list.append(agg_info) else: return False agg_public_key = AggregationInfo.merge_infos(agg_info_list) self.signature.set_aggregation_info(agg_public_key) return self.signature.verify()
def verify_offline_block_signature(self, blockchain): if type(self.txn) == list: txn_hash = self.txn txn_hash.sort() elif type(self.txn) == dict: txn_hash = list(self.txn.keys()) txn_hash.sort() else: return False block = copy(self) block.signature = "" block.signers = "" block.txn = txn_hash msg = block.get_hash() temp_array = [] for c in msg: temp_array.append(ord(c)) msg = bytes(temp_array) if type(self.signature) == Signature: pass else: self.signature = BLS.deserialize(self.signature, Signature) agg_info_list = [] for node in self.signers: if node in blockchain.public_key_list: agg_info = AggregationInfo.from_msg( PublicKey.from_bytes( bytes(blockchain.public_key_list[node], "ISO-8859-1")), msg) agg_info_list.append(agg_info) else: return False agg_public_key = AggregationInfo.merge_infos(agg_info_list) self.signature.set_aggregation_info(agg_public_key) return self.signature.verify()
def verify(self): aggregation_infos = [] sig = self.signature in_value = 0 for input in self.inputs: if input.verify() == False: return false aggregation_infos.append(AggregationInfo.from_msg(input.prevout.spendingkey.pk, input.hash())) in_value = in_value + input.prevout.value out_value = 0 for output in self.outputs: if output.verify() == False: return false aggregation_infos.append(AggregationInfo.from_msg(output.blindingkey.pk, output.hash())) out_value = out_value + output.value sig.set_aggregation_info(AggregationInfo.merge_infos(aggregation_infos)) return sig.verify() and in_value >= out_value
def test1(): seed = bytes([ 0, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192, 19, 18, 12, 89, 6, 220, 18, 102, 58, 209, 82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22 ]) sk = PrivateKey.from_seed(seed) pk = sk.get_public_key() msg = bytes([100, 2, 254, 88, 90, 45, 23]) sig = sk.sign(msg) sk_bytes = sk.serialize() pk_bytes = pk.serialize() sig_bytes = sig.serialize() sk = PrivateKey.from_bytes(sk_bytes) pk = PublicKey.from_bytes(pk_bytes) sig = Signature.from_bytes(sig_bytes) sig.set_aggregation_info(AggregationInfo.from_msg(pk, msg)) ok = sig.verify() assert (ok) seed = bytes([1]) + seed[1:] sk1 = PrivateKey.from_seed(seed) seed = bytes([2]) + seed[1:] sk2 = PrivateKey.from_seed(seed) pk1 = sk1.get_public_key() sig1 = sk1.sign(msg) pk2 = sk2.get_public_key() sig2 = sk2.sign(msg) agg_sig = Signature.aggregate([sig1, sig2]) agg_pubkey = PublicKey.aggregate([pk1, pk2]) agg_sig.set_aggregation_info(AggregationInfo.from_msg(agg_pubkey, msg)) assert (agg_sig.verify()) seed = bytes([3]) + seed[1:] sk3 = PrivateKey.from_seed(seed) pk3 = sk3.get_public_key() msg2 = bytes([100, 2, 254, 88, 90, 45, 23]) sig1 = sk1.sign(msg) sig2 = sk2.sign(msg) sig3 = sk3.sign(msg2) agg_sig_l = Signature.aggregate([sig1, sig2]) agg_sig_final = Signature.aggregate([agg_sig_l, sig3]) sig_bytes = agg_sig_final.serialize() agg_sig_final = Signature.from_bytes(sig_bytes) a1 = AggregationInfo.from_msg(pk1, msg) a2 = AggregationInfo.from_msg(pk2, msg) a3 = AggregationInfo.from_msg(pk3, msg2) a1a2 = AggregationInfo.merge_infos([a1, a2]) a_final = AggregationInfo.merge_infos([a1a2, a3]) print(a_final) agg_sig_final.set_aggregation_info(a_final) ok = agg_sig_final.verify() ok = agg_sig_l.verify() agg_sig_final = agg_sig_final.divide_by([agg_sig_l]) ok = agg_sig_final.verify() agg_sk = PrivateKey.aggregate([sk1, sk2], [pk1, pk2]) agg_sk.sign(msg) seed = bytes([ 1, 50, 6, 244, 24, 199, 1, 25, 52, 88, 192, 19, 18, 12, 89, 6, 220, 18, 102, 58, 209, 82, 12, 62, 89, 110, 182, 9, 44, 20, 254, 22 ]) esk = ExtendedPrivateKey.from_seed(seed) epk = esk.get_extended_public_key() sk_child = esk.private_child(0).private_child(5) pk_child = epk.public_child(0).public_child(5) buffer1 = pk_child.serialize() buffer2 = sk_child.serialize() print(len(buffer1), buffer1) print(len(buffer2), buffer2) assert (sk_child.get_extended_public_key() == pk_child)
#The Fog Node receives the data packets and the aggregated signature from the IoT device #The public key of the IoT device is known publicly by the entire system import timeit import pickle from blspy import (PrivateKey, PublicKey, InsecureSignature, Signature, PrependSignature, AggregationInfo, ExtendedPrivateKey, Threshold, Util) num = 3 a=[0]*num # Create aggregation information (or deserialize it) dt3 = timeit.default_timer() for x in range(num): a[x] = AggregationInfo.from_msg(pk[x], msg[x]) for i in range(0,num-1,2): a1a2 = AggregationInfo.merge_infos([a[i], a[i+1]]) a_final = AggregationInfo.merge_infos([a1a2, a[i+2]]) a[i+2]=a_final agg_sig_final.set_aggregation_info(a_final) ok = agg_sig_final.verify() dt4 = timeit.default_timer() print ("Signature Verification Time:",(dt4-dt3)*1000,"ms")