Skip to content

Commit

Permalink
Coincurve library experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout committed Jun 27, 2024
1 parent f2c8e85 commit 4ba8e1d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 64 deletions.
129 changes: 78 additions & 51 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ attrs = ">=21.4.0"
base58 = ">=2.1.1"
cattrs = ">=22.1.0"
click = ">=8.1.3"
coincurve = ">=20.0.0"
cryptography = ">=42.0.4"
deprecation = ">=2.1.0"
docker = ">=6.0.0"
Expand All @@ -58,13 +59,11 @@ py-ecc = ">=7.0.0"
pysodium = ">=0.7.10"
python-dateutil = ">=2.8.2"
requests = ">=2.28.2"
secp256k1 = ">=0.14.0"
simplejson = ">=3.17.6"
strict-rfc3339 = ">=0.7"
tabulate = ">=0.9.0"
testcontainers = ">=3.7.0"
tqdm = ">=4.62.3"
setuptools = ">=70.1.0"
simple-bson = ">=0.0.3"

[tool.poetry.dev-dependencies]
Expand Down
26 changes: 15 additions & 11 deletions src/pytezos/crypto/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ def get_passphrase(passphrase: PassphraseInput = None, alias: Optional[str] = No
class CryptoExtraFallback:
def __getattr__(self, item):
raise ImportError(
"Please, install packages libsodium-dev, libsecp256k1-dev, and libgmp-dev, "
"and Python libraries pysodium, secp256k1, and fastecdsa"
"Please, install packages libsodium-dev, and libgmp-dev, "
"and Python libraries pysodium, coincurve, and fastecdsa"
)

def __call__(self, *args, **kwargs):
self.__getattr__('throw')


try:
import coincurve # type: ignore
import fastecdsa.curve # type: ignore
import fastecdsa.ecdsa # type: ignore
import fastecdsa.encoding.sec1 # type: ignore
import fastecdsa.keys # type: ignore
import pysodium # type: ignore
import secp256k1 # type: ignore
from fastecdsa.encoding.util import bytes_to_int # type: ignore
except ImportError as e:
coincurve = CryptoExtraFallback()
pysodium = CryptoExtraFallback()
secp256k1 = CryptoExtraFallback()
fastecdsa = CryptoExtraFallback()
bytes_to_int = CryptoExtraFallback()
__crypto__ = False
Expand Down Expand Up @@ -151,8 +151,8 @@ def from_secret_exponent(
public_point, secret_exponent = pysodium.crypto_sign_seed_keypair(seed=secret_exponent)
# Secp256k1
elif curve == b'sp':
sk = secp256k1.PrivateKey(secret_exponent)
public_point = sk.pubkey.serialize()
sk = coincurve.PrivateKey(secret_exponent)
public_point = sk.public_key.format(compressed=True)
# P256
elif curve == b'p2':
pk = fastecdsa.keys.get_public_key(bytes_to_int(secret_exponent), curve=fastecdsa.curve.P256)
Expand Down Expand Up @@ -444,8 +444,9 @@ def sign(self, message: Union[str, bytes], generic: bool = False):
signature = pysodium.crypto_sign_detached(digest, self.secret_exponent)
# Secp256k1
elif self.curve == b"sp":
pk = secp256k1.PrivateKey(self.secret_exponent)
signature = pk.ecdsa_serialize_compact(pk.ecdsa_sign(encoded_message, digest=blake2b_32))
pk = coincurve.PrivateKey(self.secret_exponent)
signature = pk.sign(encoded_message, hasher=lambda x: blake2b_32(x).digest())

# P256
elif self.curve == b"p2":
r, s = fastecdsa.ecdsa.sign(msg=encoded_message, d=bytes_to_int(self.secret_exponent), hashfunc=blake2b_32)
Expand Down Expand Up @@ -489,9 +490,12 @@ def verify(self, signature: Union[str, bytes], message: Union[str, bytes]) -> bo
raise ValueError('Signature is invalid.') from exc
# Secp256k1
elif self.curve == b"sp":
pk = secp256k1.PublicKey(self.public_point, raw=True)
sig = pk.ecdsa_deserialize_compact(decoded_signature)
if not pk.ecdsa_verify(encoded_message, sig, digest=blake2b_32):
pk = coincurve.PublicKey(self.public_point)
if not pk.verify(
signature=decoded_signature,
message=encoded_message,
hasher=lambda x: blake2b_32(x).digest(),
):
raise ValueError('Signature is invalid.')
# P256
elif self.curve == b"p2":
Expand Down

0 comments on commit 4ba8e1d

Please sign in to comment.