def test_public_key_is_hashable(): sk = SecretKey.random() pk = sk.public_key() sk2 = SecretKey.random() pk2 = sk2.public_key() assert hash(pk) != hash(pk2) pk3 = PublicKey.from_bytes(bytes(pk)) assert hash(pk) == hash(pk3)
def test_signature_str(): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) signature = signer.sign(b'peace at dawn') s = str(signature) assert 'Signature' in s
def test_public_key_serialization(): sk = SecretKey.random() pk = sk.public_key() encoded_key = bytes(pk) decoded_key = PublicKey.from_bytes(encoded_key) assert pk == decoded_key
def test_gen_key(): sk = SecretKey.random() assert type(sk) == SecretKey pk = sk.public_key() assert type(pk) == PublicKey pk2 = sk.public_key() assert pk == pk2
def test_sign_and_verify(execution_number): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) message = b"peace at dawn" + str(execution_number).encode() signature = signer.sign(message) assert signature.verify(pk, message)
def test_sign_serialize_and_verify(execution_number): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) message = b"peace at dawn" + str(execution_number).encode() signature = signer.sign(message) signature_bytes = bytes(signature) signature_restored = Signature.from_bytes(signature_bytes) assert signature_restored.verify(pk, message)
def test_verification_fail(): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) message = b"peace at dawn" signature = signer.sign(message) # wrong message wrong_message = b"no peace at dawn" assert not signature.verify(pk, wrong_message) # bad signature signature_bytes = bytes(signature) signature_bytes = b'\x00' + signature_bytes[1:] signature_restored = Signature.from_bytes(signature_bytes) assert not signature_restored.verify(pk, message)
def test_signature_is_hashable(): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) message = b'peace at dawn' message2 = b'no peace at dawn' signature = signer.sign(message) signature2 = signer.sign(message2) assert hash(signature) != hash(signature2) signature_restored = Signature.from_bytes(bytes(signature)) assert signature == signature_restored assert hash(signature) == hash(signature_restored) # Different hash, since signing involves some randomness signature3 = signer.sign(message) assert hash(signature) != hash(signature3)
def test_secret_scalar(): sk = SecretKey.random() assert sk.secret_scalar() == sk._scalar_key
def test_public_key_str(): pk = SecretKey.random().public_key() s = str(pk) assert 'PublicKey' in s
def test_public_key_point(): pk = SecretKey.random().public_key() assert bytes(pk) == bytes(pk.point())
def test_signer_pubkey(): sk = SecretKey.random() pk = sk.public_key() signer = Signer(sk) assert signer.verifying_key() == pk
def test_signer_hash(): signer = Signer(SecretKey.random()) # Insecure Python hash, shouldn't be available. with pytest.raises(RuntimeError): hash(signer)
def test_signer_str(): signer = Signer(SecretKey.random()) s = str(signer) assert s == "Signer:..."
def test_secret_key_serialization(): sk = SecretKey.random() encoded_key = sk.to_secret_bytes() decoded_key = SecretKey.from_bytes(encoded_key) assert sk.to_secret_bytes() == decoded_key.to_secret_bytes()
def test_secret_key_hash(): sk = SecretKey.random() # Insecure Python hash, shouldn't be available. with pytest.raises(RuntimeError): hash(sk)
def test_secret_key_str(): sk = SecretKey.random() s = str(sk) assert s == "SecretKey:..."
def test_signer_bytes(): signer = Signer(SecretKey.random()) # Shouldn't be able to serialize. with pytest.raises(RuntimeError): bytes(signer)