Esempio n. 1
0
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 reply_commit(blockchain, logger, values):
        logger.debug("in reply_commit fxn")
        signature = values.get('tc_signed')
        node_address = values.get('address')
        if signature is None or node_address is None:
            return jsonify("Error: invalid json received, Bad request"), 400
        if node_address not in blockchain.public_key_list:
            return jsonify("Bad request"), 400

        signature = BLS.deserialize(signature, Signature)
        hash_of_priority_block = blockchain.proposed_block.get_hash()
        temp_array = []
        for c in hash_of_priority_block:
            temp_array.append(ord(c))
        msg = bytes(temp_array)

        signature.set_aggregation_info(
            AggregationInfo.from_msg(
                PublicKey.from_bytes(
                    bytes(blockchain.public_key_list[node_address],
                          "ISO-8859-1")), msg))
        verify_sign = signature.verify()

        if verify_sign:
            logger.debug("reply commit signature verified")
            blockchain.commit_accepted[node_address] = signature
            # print("commit accepted by ", len(blockchain.commit_accepted))
            return jsonify("True"), 200
        else:
            logger.warning("reply commit signature tempered")
            return jsonify("False"), 300
Esempio n. 3
0
def verify(message_hash: Hash32, pubkey: BLSPubkey, signature: BLSSignature,
           domain: Domain) -> bool:
    pubkey_chia = _pubkey_from_bytes(pubkey)
    signature_chia = _signature_from_bytes(signature)
    signature_chia.set_aggregation_info(
        AggregationInfo.from_msg(pubkey_chia,
                                 combine_domain(message_hash, domain)))
    return cast(bool, signature_chia.verify())
Esempio n. 4
0
    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()
Esempio n. 5
0
def test2():
    seed = bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
    seed2 = bytes([1, 20, 102, 229, 1, 157])

    sk = BLSPrivateKey.from_seed(seed)
    sk_cp = BLSPrivateKey.from_seed(seed)
    sk2 = BLSPrivateKey.from_seed(seed2)
    pk = sk.get_public_key()
    pk2 = sk2.get_public_key()
    assert (sk == sk_cp)
    assert (sk != sk2)
    assert (pk.get_fingerprint() == 0xddad59bb)

    print(sk, sk2)
    print(pk, pk2)
    pk2_ser = pk2.serialize()
    print(pk2_ser)
    pk2_copy = BLSPublicKey.from_bytes(pk2_ser)
    assert (pk2 == pk2_copy)
    assert (pk != pk2)
    assert (pk2.size() == 48)
    assert (sk2.size() == 32)

    message = bytes("this is the message", "utf-8")
    sig = sk.sign(message)
    sig_ser = sig.serialize()
    print("Sig ser is", sig_ser)
    sig_cp = BLSSignature.from_bytes(sig_ser)
    a1 = AggregationInfo.from_msg(pk, message)
    sig_cp.set_aggregation_info(a1)
    a2 = sig_cp.get_aggregation_info()
    assert (a1 == a2)
    print(a2)
    sig2 = sk2.sign(message)

    assert (sig.size() == 96)
    print(sig, sig2)
    print(sig2.serialize())
    print(sorted([sig, sig2]))
    assert (sig != sig2)
    assert (sig == sig_cp)

    sig_agg = BLS.aggregate_sigs([sig, sig2])

    result = BLS.verify(sig_cp)
    result2 = BLS.verify(sig2)
    result3 = BLS.verify(sig_agg)
    assert (result)
    assert (result2)
    assert (result3)

    sk2 = sk

    print(sk)
    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
Esempio n. 7
0
def verify_sig(msg, _pub_key, _sig):
    try:
        sig = Signature.from_bytes(_sig)
    except RuntimeError:
        raise ValueError("Bad signature")
    try:
        pub_key = PublicKey.from_bytes(_pub_key)
    except RuntimeError:
        raise ValueError("Bad public key")

    sig.set_aggregation_info(AggregationInfo.from_msg(pub_key, msg))
    ok = sig.verify()
    return ok
    def verify_signature(self):
        temp_array = []
        msg = self.sender + self.to + str(self.amount) + str(self.timestamp)
        for c in msg:
            temp_array.append(ord(c))
        msg = bytes(temp_array)

        _signature = bytes(self.signature, "ISO-8859-1")
        _signature = Signature.from_bytes(_signature)
        public_key = PublicKey.from_bytes(bytes(self.sender, "ISO-8859-1"))
        _signature.set_aggregation_info(
            AggregationInfo.from_msg(public_key, msg))
        return _signature.verify()
Esempio n. 9
0
    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
Esempio n. 10
0
def test_vectors():
    sk1 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5]))
    pk1 = sk1.get_public_key()
    sig1 = sk1.sign(bytes([7, 8, 9]))

    sk2 = PrivateKey.from_seed(bytes([1, 2, 3, 4, 5, 6]))
    pk2 = sk2.get_public_key()
    sig2 = sk2.sign(bytes([7, 8, 9]))
    assert (sk1.serialize() == bytes.fromhex(
        "022fb42c08c12de3a6af053880199806532e79515f94e83461612101f9412f9e"))
    assert (pk1.get_fingerprint() == 0x26d53247)
    assert (pk2.get_fingerprint() == 0x289bb56e)
    assert (sig1.serialize() == bytes.fromhex(
        "93eb2e1cb5efcfb31f2c08b235e8203a67265bc6a13d9f0ab77727293b74a357ff0459ac210dc851fcb8a60cb7d393a419915cfcf83908ddbeac32039aaa3e8fea82efcb3ba4f740f20c76df5e97109b57370ae32d9b70d256a98942e5806065"
    ))
    assert (sig2.serialize() == bytes.fromhex(
        "975b5daa64b915be19b5ac6d47bc1c2fc832d2fb8ca3e95c4805d8216f95cf2bdbb36cc23645f52040e381550727db420b523b57d494959e0e8c0c6060c46cf173872897f14d43b2ac2aec52fc7b46c02c5699ff7a10beba24d3ced4e89c821e"
    ))

    agg_sig = Signature.aggregate([sig1, sig2])
    agg_pk = PublicKey.aggregate([pk1, pk2])
    agg_sk = PrivateKey.aggregate([sk1, sk2], [pk1, pk2])
    assert (agg_sig.serialize() == bytes.fromhex(
        "0a638495c1403b25be391ed44c0ab013390026b5892c796a85ede46310ff7d0e0671f86ebe0e8f56bee80f28eb6d999c0a418c5fc52debac8fc338784cd32b76338d629dc2b4045a5833a357809795ef55ee3e9bee532edfc1d9c443bf5bc658"
    ))
    assert (agg_sk.sign(bytes([7, 8, 9])).serialize() == agg_sig.serialize())

    assert (sig1.verify())
    assert (agg_sig.verify())

    agg_sig.set_aggregation_info(
        AggregationInfo.from_msg(agg_pk, bytes([7, 8, 9])))
    assert (agg_sig.verify())

    sig1.set_aggregation_info(sig2.get_aggregation_info())
    assert (not sig1.verify())

    sig3 = sk1.sign(bytes([1, 2, 3]))
    sig4 = sk1.sign(bytes([1, 2, 3, 4]))
    sig5 = sk2.sign(bytes([1, 2]))

    agg_sig2 = Signature.aggregate([sig3, sig4, sig5])
    assert (agg_sig2.verify())
    assert (agg_sig2.serialize() == bytes.fromhex(
        "8b11daf73cd05f2fe27809b74a7b4c65b1bb79cc1066bdf839d96b97e073c1a635d2ec048e0801b4a208118fdbbb63a516bab8755cc8d850862eeaa099540cd83621ff9db97b4ada857ef54c50715486217bd2ecb4517e05ab49380c041e159b"
    ))
Esempio n. 11
0
    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()
Esempio n. 12
0
    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()
Esempio n. 13
0
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")
	





Esempio n. 15
0
    def verify(self):
        msg = self.hash()
        sig = self.signature
        sig.set_aggregation_info(AggregationInfo.from_msg(self.blindingkey.pk, msg))

        return sig.verify()
#The Fog Node receives the data packets and the signatures from the IoT device
#The public key of the IoT device is known publicly by the entire system

import timeit
from blspy import (PrivateKey, PublicKey, InsecureSignature, Signature,
                   PrependSignature, AggregationInfo, ExtendedPrivateKey,
                   Threshold, Util)
data = []
num = 3 #the number of data packets

	# Add information required for verification, to sig object
	dt3 = timeit.default_timer()
	for x in range(num):
		sig[x].set_aggregation_info(AggregationInfo.from_msg(pk[x], msg[x]))
		ok = sig[x].verify()
	print ("Signature Verification Time:",(dt4-dt3)*1000,"ms")