def throw_wrong_type(): private_key = ExtendedPrivateKey.from_seed(b"foo").get_private_key() message_hash = bytes([10] * 32) sig_prepend = private_key.sign_prepend_prehashed(message_hash).serialize() sig_secure = private_key.sign_prehashed(message_hash).serialize() try: Signature.from_bytes(sig_prepend) except ValueError: try: PrependSignature.from_bytes(sig_secure) except ValueError: return assert False assert False
def _signature_from_bytes(signature: BLSSignature) -> Signature: if signature == EMPTY_SIGNATURE: raise ValueError(f"Invalid signature (EMPTY_SIGNATURE): {signature}") elif len(signature) != 96: raise ValueError( f"Invalid signaute length, expect 96 got {len(signature)}. Signature: {signature}" ) try: return Signature.from_bytes(signature) except (RuntimeError, ValueError) as error: raise ValidationError(f"Bad signature: {signature}, {error}")
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()
def deserialize(_obj, _type): try: if (type(_obj) != PrivateKey) or (type(_obj) != PublicKey) or (type(_obj) != Signature): _obj = bytes(_obj, "ISO-8859-1") if _type == PrivateKey: return PrivateKey.from_bytes(_obj) elif _type == PublicKey: return PublicKey.from_bytes(_obj) elif _type == Signature: return Signature.from_bytes(_obj) else: _obj except Exception as e: print("given parameters compatibility error") print(e) return None
def test2(): seed = bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) seed2 = bytes([1, 20, 102, 229, 1, 157]) sk = PrivateKey.from_seed(seed) sk_cp = PrivateKey.from_seed(seed) sk2 = PrivateKey.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) sk2_ser = sk2.serialize() pk2_ser = pk2.serialize() pk2_copy = PublicKey.from_bytes(pk2_ser) assert (pk2 == pk2_copy) assert (pk != pk2) assert (len(pk2_ser) == 48) assert (len(sk2_ser) == 32) message = bytes("this is the message", "utf-8") sig = sk.sign(message) sig_ser = sig.serialize() sig_cp = Signature.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) sig2 = sk2.sign(message) assert (len(sig_ser) == 96) assert (sig != sig2) assert (sig == sig_cp) sig_agg = Signature.aggregate([sig, sig2]) result = sig_cp.verify() result2 = sig2.verify() result3 = sig_agg.verify() assert (result) assert (result2) assert (result3) sk2 = sk
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)
def sigs_deseriazlize(_sigs): try: sigs = list(map(lambda x: Signature.from_bytes(x), _sigs)) except RuntimeError: raise ValueError("Bad signature") return sigs
def _signature_from_bytes(signature: BLSSignature) -> Signature: try: return Signature.from_bytes(signature) except (RuntimeError, ValueError) as error: raise ValidationError(f"Bad signature: {signature}, {error}")