def test_verify_multi_sig_works_for_invalid_signature(generator, message): sign_key1 = SignKey.new(None) ver_key1 = VerKey.new(generator, sign_key1) sign_key2 = SignKey.new(None) ver_key2 = VerKey.new(generator, SignKey.new(None)) signature1 = Bls.sign(message, sign_key1) signature2 = Bls.sign(message, sign_key2) multi_signature_invalid = MultiSignature.new([signature1, signature2]) valid = Bls.verify_multi_sig(multi_signature_invalid, message, [ver_key1, ver_key2], generator) assert not valid
def verify_key_proof_of_possession(self, key_proof, pk: str) -> bool: bls_key_proof = IndyCryptoBlsUtils.bls_from_str( key_proof, ProofOfPossession) bls_pk = IndyCryptoBlsUtils.bls_from_str(pk, VerKey) if None in [bls_key_proof, bls_pk]: return False return Bls.verify_pop(bls_key_proof, bls_pk, self._generator)
def verify_sig(self, signature: str, message: bytes, pk: str) -> bool: bls_signature = IndyCryptoBlsUtils.bls_from_str(signature, Signature) if bls_signature is None: return False bls_pk = IndyCryptoBlsUtils.bls_from_str(pk, VerKey) if bls_pk is None: return False return Bls.verify(bls_signature, message, bls_pk, self._generator)
def verify_key_proof_of_possession(self, key_proof, pk: str) -> bool: bls_key_proof = IndyCryptoBlsUtils.bls_from_str(key_proof, ProofOfPossession) bls_pk = IndyCryptoBlsUtils.bls_from_str(pk, VerKey) if None in [bls_key_proof, bls_pk]: return False return Bls.verify_pop(bls_key_proof, bls_pk, self._generator)
def verify_multi_sig(self, signature: str, message: str, pks: Sequence[str]) -> bool: epks = [IndyCryptoBlsUtils.bls_from_str(p, VerKey) for p in pks] multi_signature = \ IndyCryptoBlsUtils.bls_from_str(signature, MultiSignature) # type: MultiSignature message_bytes = IndyCryptoBlsUtils.msg_to_bls_bytes(message) return Bls.verify_multi_sig(multi_sig=multi_signature, message=message_bytes, ver_keys=epks, gen=self._generator)
def demo(): # Create generator generator = Generator.new() # Create first key pair sign_key1 = SignKey.new(None) ver_key1 = VerKey.new(generator, sign_key1) # Create second keys pair based on seed seed = bytes([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8 ]) sign_key2 = SignKey.new(seed) ver_key2 = VerKey.new(generator, sign_key2) # Sample message message = bytes([1, 2, 3, 4, 5]) # Sign message with first sing key signature1 = Bls.sign(message, sign_key1) # Verify first signature with first ver key valid = Bls.verify(signature1, message, ver_key1, generator) assert valid # Sign message with second sing key signature2 = Bls.sign(message, sign_key2) # Verify second signature with second ver key valid = Bls.verify(signature2, message, ver_key2, generator) assert valid # Create multi signature multi_sig = MultiSignature.new([signature1, signature2]) # Verify multi signature valid = Bls.verify_multi_sig(multi_sig, message, [ver_key1, ver_key2], generator) assert valid
def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[Optional[VerKey]]) -> bool: # TODO: is it expected that we return False if one of the keys is None? if None in pks: return False multi_signature = \ IndyCryptoBlsUtils.bls_from_str(signature, MultiSignature) # type: MultiSignature if multi_signature is None: return False return Bls.verify_multi_sig(multi_sig=multi_signature, message=message, ver_keys=pks, gen=self._generator)
def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[str]) -> bool: epks = [IndyCryptoBlsUtils.bls_from_str(p, VerKey) for p in pks] if None in epks: return False multi_signature = \ IndyCryptoBlsUtils.bls_from_str(signature, MultiSignature) # type: MultiSignature if multi_signature is None: return False return Bls.verify_multi_sig(multi_sig=multi_signature, message=message, ver_keys=epks, gen=self._generator)
def verify_key_proof_of_possession(self, key_proof: Optional[ProofOfPossession], bls_pk: Optional[VerKey]) -> bool: if None in [key_proof, bls_pk]: return False return Bls.verify_pop(key_proof, bls_pk, self._generator)
def test_verify__multi_sig_works(generator: Generator, message: bytes, multi_sig: MultiSignature, ver_key1: VerKey, ver_key2: VerKey): valid = Bls.verify_multi_sig(multi_sig, message, [ver_key1, ver_key2], generator) assert valid
def test_verify_for_seed(generator: Generator, message: bytes, ver_key2: VerKey, signature2: Signature): valid = Bls.verify(signature2, message, ver_key2, generator) assert valid
def sign(self, message: bytes) -> str: sign = Bls.sign(message, self._sk_bls) return IndyCryptoBlsUtils.bls_to_str(sign)
def sign(self, message: str) -> str: bts = IndyCryptoBlsUtils.msg_to_bls_bytes(message) sign = Bls.sign(bts, self._sk_bls) return IndyCryptoBlsUtils.bls_to_str(sign)
def test_verify(generator: Generator, message: bytes, ver_key1: VerKey, signature1: Signature): valid = Bls.verify(signature1, message, ver_key1, generator) assert valid
def test_verify_pop(generator: Generator, ver_key1: VerKey, pop: ProofOfPossession): valid = Bls.verify_pop(pop, ver_key1, generator) assert valid
def signature2(message: bytes, sign_key2: SignKey) -> Signature: signature = Bls.sign(message, sign_key2) assert type(signature) is Signature assert signature.c_instance is not None return signature
def verify_sig(self, signature: str, message: str, pk: str) -> bool: return Bls.verify(IndyCryptoBlsUtils.bls_from_str(signature, Signature), IndyCryptoBlsUtils.msg_to_bls_bytes(message), IndyCryptoBlsUtils.bls_from_str(pk, VerKey), self._generator)