Skip to content

Commit

Permalink
Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed Aug 14, 2024
1 parent 6314fd9 commit 302e929
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 8 additions & 1 deletion chia/_tests/util/test_secp256r1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from chia.util.ints import uint32
from chia.util.key_types import Secp256r1PrivateKey, Secp256r1PublicKey
from chia.util.key_types import Secp256r1PrivateKey, Secp256r1PublicKey, Secp256r1Signature
from chia.util.keychain import generate_mnemonic, mnemonic_to_seed


Expand All @@ -23,3 +23,10 @@ def test_key_drivers() -> None:
pk = sk.public_key()
assert Secp256r1PublicKey.from_bytes(bytes(pk)) == pk
assert pk.get_fingerprint() < uint32.MAXIMUM
with pytest.raises(NotImplementedError):
sk.derive_unhardened(1)

sig = sk.sign(b"foo")
assert Secp256r1Signature.from_bytes(bytes(sig)) == sig
with pytest.raises(NotImplementedError):
sk.sign(b"foo", final_pk=pk)
12 changes: 9 additions & 3 deletions chia/util/key_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def from_bytes(cls, blob: bytes) -> Secp256r1PublicKey:
pk = serialization.load_der_public_key(blob)
if isinstance(pk, ec.EllipticCurvePublicKey):
return Secp256r1PublicKey(pk)
else:
else: # pragma: no cover
# Not sure how to test this, it's really just for mypy sake
raise ValueError("Could not load EllipticCurvePublicKey provided blob")

def derive_unhardened(self, index: int) -> Secp256r1PublicKey:
Expand All @@ -45,6 +46,10 @@ class Secp256r1Signature:
def __bytes__(self) -> bytes:
return self._buf

@classmethod
def from_bytes(cls, blob: bytes) -> Secp256r1Signature:
return cls(blob)


# A wrapper for SigningKey that conforms to the SecretInfo protocol
@dataclass(frozen=True)
Expand All @@ -66,7 +71,8 @@ def from_bytes(cls, blob: bytes) -> Secp256r1PrivateKey:
sk = serialization.load_der_private_key(blob, password=None)
if isinstance(sk, ec.EllipticCurvePrivateKey):
return Secp256r1PrivateKey(sk)
else:
else: # pragma: no cover
# Not sure how to test this, it's really just for mypy sake
raise ValueError("Could not load EllipticCurvePrivateKey provided blob")

def public_key(self) -> Secp256r1PublicKey:
Expand All @@ -80,7 +86,7 @@ def from_seed(cls, seed: bytes) -> Secp256r1PrivateKey:

def sign(self, msg: bytes, final_pk: Optional[Secp256r1PublicKey] = None) -> Secp256r1Signature:
if final_pk is not None:
raise ValueError("SECP256r1 does not support signature aggregation")
raise NotImplementedError("SECP256r1 does not support signature aggregation")
der_sig = self._private_key.sign(msg, ec.ECDSA(hashes.SHA256(), deterministic_signing=True))
r, s = decode_dss_signature(der_sig)
sig = r.to_bytes(32, byteorder="big") + s.to_bytes(32, byteorder="big")
Expand Down

0 comments on commit 302e929

Please sign in to comment.