Skip to content

Commit

Permalink
don't mix import styles for imports from module
Browse files Browse the repository at this point in the history
  • Loading branch information
tomato42 committed Feb 28, 2023
1 parent afcd9d7 commit fd8b239
Showing 1 changed file with 56 additions and 46 deletions.
102 changes: 56 additions & 46 deletions src/ecdsa/test_pyecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import subprocess
import pytest
from binascii import hexlify, unhexlify
from hashlib import sha1, sha256, sha384, sha512
import hashlib
from functools import partial

Expand Down Expand Up @@ -97,11 +96,16 @@ def test_deterministic(self):
data = b("blahblah")
secexp = int("9d0219792467d7d37b4d43298a7d0c05", 16)

priv = SigningKey.from_secret_exponent(secexp, SECP256k1, sha256)
priv = SigningKey.from_secret_exponent(
secexp, SECP256k1, hashlib.sha256
)
pub = priv.get_verifying_key()

k = rfc6979.generate_k(
SECP256k1.generator.order(), secexp, sha256, sha256(data).digest()
SECP256k1.generator.order(),
secexp,
hashlib.sha256,
hashlib.sha256(data).digest(),
)

sig1 = priv.sign(data, k=k)
Expand All @@ -110,7 +114,7 @@ def test_deterministic(self):
sig2 = priv.sign(data, k=k)
self.assertTrue(pub.verify(sig2, data))

sig3 = priv.sign_deterministic(data, sha256)
sig3 = priv.sign_deterministic(data, hashlib.sha256)
self.assertTrue(pub.verify(sig3, data))

self.assertEqual(sig1, sig2)
Expand Down Expand Up @@ -601,27 +605,29 @@ def test_sign_with_too_long_hash(self):
sk.sign_digest(b("\xff") * 64)

def test_hashfunc(self):
sk = SigningKey.generate(curve=NIST256p, hashfunc=sha256)
sk = SigningKey.generate(curve=NIST256p, hashfunc=hashlib.sha256)
data = b("security level is 128 bits")
sig = sk.sign(data)
vk = VerifyingKey.from_string(
sk.get_verifying_key().to_string(), curve=NIST256p, hashfunc=sha256
sk.get_verifying_key().to_string(),
curve=NIST256p,
hashfunc=hashlib.sha256,
)
self.assertTrue(vk.verify(sig, data))

sk2 = SigningKey.generate(curve=NIST256p)
sig2 = sk2.sign(data, hashfunc=sha256)
sig2 = sk2.sign(data, hashfunc=hashlib.sha256)
vk2 = VerifyingKey.from_string(
sk2.get_verifying_key().to_string(),
curve=NIST256p,
hashfunc=sha256,
hashfunc=hashlib.sha256,
)
self.assertTrue(vk2.verify(sig2, data))

vk3 = VerifyingKey.from_string(
sk.get_verifying_key().to_string(), curve=NIST256p
)
self.assertTrue(vk3.verify(sig, data, hashfunc=sha256))
self.assertTrue(vk3.verify(sig, data, hashfunc=hashlib.sha256))

def test_public_key_recovery(self):
# Create keys
Expand Down Expand Up @@ -660,7 +666,7 @@ def test_public_key_recovery_with_custom_hash(self):
# Create keys
curve = BRAINPOOLP160r1

sk = SigningKey.generate(curve=curve, hashfunc=sha256)
sk = SigningKey.generate(curve=curve, hashfunc=hashlib.sha256)
vk = sk.get_verifying_key()

# Sign a message
Expand All @@ -669,7 +675,11 @@ def test_public_key_recovery_with_custom_hash(self):

# Recover verifying keys
recovered_vks = VerifyingKey.from_public_key_recovery(
signature, data, curve, hashfunc=sha256, allow_truncate=True
signature,
data,
curve,
hashfunc=hashlib.sha256,
allow_truncate=True,
)

# Test if each pk is valid
Expand All @@ -679,7 +689,7 @@ def test_public_key_recovery_with_custom_hash(self):

# Test if properties are equal
self.assertEqual(vk.curve, recovered_vk.curve)
self.assertEqual(sha256, recovered_vk.default_hashfunc)
self.assertEqual(hashlib.sha256, recovered_vk.default_hashfunc)

# Test if original vk is the list of recovered keys
self.assertIn(
Expand Down Expand Up @@ -1656,8 +1666,8 @@ def test_SECP256k1(self):
self._do(
generator=SECP256k1.generator,
secexp=int("9d0219792467d7d37b4d43298a7d0c05", 16),
hsh=sha256(b("sample")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("sample")).digest(),
hash_func=hashlib.sha256,
expected=int(
"8fa1f95d514760e498f28957b824ee6ec39ed64826ff4fecc2b5739ec45b91cd",
16,
Expand All @@ -1671,8 +1681,8 @@ def test_SECP256k1_2(self):
"cca9fbcc1b41e5a95d369eaa6ddcff73b61a4efaa279cfc6567e8daa39cbaf50",
16,
),
hsh=sha256(b("sample")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("sample")).digest(),
hash_func=hashlib.sha256,
expected=int(
"2df40ca70e639d89528a6b670d9d48d9165fdc0febc0974056bdce192b8e16a3",
16,
Expand All @@ -1683,39 +1693,39 @@ def test_SECP256k1_3(self):
self._do(
generator=SECP256k1.generator,
secexp=0x1,
hsh=sha256(b("Satoshi Nakamoto")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("Satoshi Nakamoto")).digest(),
hash_func=hashlib.sha256,
expected=0x8F8A276C19F4149656B280621E358CCE24F5F52542772691EE69063B74F15D15,
)

def test_SECP256k1_4(self):
self._do(
generator=SECP256k1.generator,
secexp=0x1,
hsh=sha256(
hsh=hashlib.sha256(
b(
"All those moments will be lost in time, like tears in rain. Time to die..."
)
).digest(),
hash_func=sha256,
hash_func=hashlib.sha256,
expected=0x38AA22D72376B4DBC472E06C3BA403EE0A394DA63FC58D88686C611ABA98D6B3,
)

def test_SECP256k1_5(self):
self._do(
generator=SECP256k1.generator,
secexp=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364140,
hsh=sha256(b("Satoshi Nakamoto")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("Satoshi Nakamoto")).digest(),
hash_func=hashlib.sha256,
expected=0x33A19B60E25FB6F4435AF53A3D42D493644827367E6453928554F43E49AA6F90,
)

def test_SECP256k1_6(self):
self._do(
generator=SECP256k1.generator,
secexp=0xF8B8AF8CE3C7CCA5E300D33939540C10D45CE001B8F252BFBC57BA0342904181,
hsh=sha256(b("Alan Turing")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("Alan Turing")).digest(),
hash_func=hashlib.sha256,
expected=0x525A82B70E67874398067543FD84C83D30C175FDC45FDEEE082FE13B1D7CFDF1,
)

Expand All @@ -1734,16 +1744,16 @@ def test_1(self):
"AF2BDBE1AA9B6EC1E2ADE1D694F41FC71A831D0268E9891562113D8A62ADD1BF"
)
),
hash_func=sha256,
hash_func=hashlib.sha256,
expected=int("23AF4074C90A02B3FE61D286D5C87F425E6BDD81B", 16),
)

def test_2(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha1(b("sample")).digest(),
hash_func=sha1,
hsh=hashlib.sha1(b("sample")).digest(),
hash_func=hashlib.sha1,
expected=int(
"37D7CA00D2C7B0E5E412AC03BD44BA837FDD5B28CD3B0021", 16
),
Expand All @@ -1753,8 +1763,8 @@ def test_3(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha256(b("sample")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("sample")).digest(),
hash_func=hashlib.sha256,
expected=int(
"32B1B6D7D42A05CB449065727A84804FB1A3E34D8F261496", 16
),
Expand All @@ -1764,8 +1774,8 @@ def test_4(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha512(b("sample")).digest(),
hash_func=sha512,
hsh=hashlib.sha512(b("sample")).digest(),
hash_func=hashlib.sha512,
expected=int(
"A2AC7AB055E4F20692D49209544C203A7D1F2C0BFBC75DB1", 16
),
Expand All @@ -1775,8 +1785,8 @@ def test_5(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha1(b("test")).digest(),
hash_func=sha1,
hsh=hashlib.sha1(b("test")).digest(),
hash_func=hashlib.sha1,
expected=int(
"D9CF9C3D3297D3260773A1DA7418DB5537AB8DD93DE7FA25", 16
),
Expand All @@ -1786,8 +1796,8 @@ def test_6(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha256(b("test")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("test")).digest(),
hash_func=hashlib.sha256,
expected=int(
"5C4CE89CF56D9E7C77C8585339B006B97B5F0680B4306C6C", 16
),
Expand All @@ -1797,8 +1807,8 @@ def test_7(self):
self._do(
generator=NIST192p.generator,
secexp=int("6FAB034934E4C0FC9AE67F5B5659A9D7D1FEFD187EE09FD4", 16),
hsh=sha512(b("test")).digest(),
hash_func=sha512,
hsh=hashlib.sha512(b("test")).digest(),
hash_func=hashlib.sha512,
expected=int(
"0758753A5254759C7CFBAD2E2D9B0792EEE44136C9480527", 16
),
Expand All @@ -1811,8 +1821,8 @@ def test_8(self):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
hsh=sha1(b("sample")).digest(),
hash_func=sha1,
hsh=hashlib.sha1(b("sample")).digest(),
hash_func=hashlib.sha1,
expected=int(
"089C071B419E1C2820962321787258469511958E80582E95D8378E0C2CCDB3CB42BEDE42F50E3FA3C71F5A76724281D31D9C89F0F91FC1BE4918DB1C03A5838D0F9",
16,
Expand All @@ -1826,8 +1836,8 @@ def test_9(self):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
hsh=sha256(b("sample")).digest(),
hash_func=sha256,
hsh=hashlib.sha256(b("sample")).digest(),
hash_func=hashlib.sha256,
expected=int(
"0EDF38AFCAAECAB4383358B34D67C9F2216C8382AAEA44A3DAD5FDC9C32575761793FEF24EB0FC276DFC4F6E3EC476752F043CF01415387470BCBD8678ED2C7E1A0",
16,
Expand All @@ -1841,8 +1851,8 @@ def test_10(self):
"0FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538",
16,
),
hsh=sha512(b("test")).digest(),
hash_func=sha512,
hsh=hashlib.sha512(b("test")).digest(),
hash_func=hashlib.sha512,
expected=int(
"16200813020EC986863BEDFC1B121F605C1215645018AEA1A7B215A564DE9EB1B38A67AA1128B80CE391C4FB71187654AAA3431027BFC7F395766CA988C964DC56D",
16,
Expand Down Expand Up @@ -2206,7 +2216,7 @@ def test_brainpoolP512r1(self):
"6FC98BD7E50211A4A27102FA3549DF79EBCB4BF246B80945CDDFE7D509BBFD7D",
"9E56F509196784D963D1C0A401510EE7ADA3DCC5DEE04B154BF61AF1D5A6DECE",
b"abc",
sha256,
hashlib.sha256,
"CB28E0999B9C7715FD0A80D8E47A77079716CBBF917DD72E97566EA1C066957C",
"86FA3BB4E26CAD5BF90B7F81899256CE7594BB1EA0C89212748BFF3B3D5B0315",
NIST256p,
Expand All @@ -2222,7 +2232,7 @@ def test_brainpoolP512r1(self):
"B4B74E44D71A13D568003D7489908D564C7761E229C58CBFA18950096EB7463B"
"854D7FA992F934D927376285E63414FA",
b"abc",
sha384,
hashlib.sha384,
"FB017B914E29149432D8BAC29A514640B46F53DDAB2C69948084E2930F1C8F7E"
"08E07C9C63F2D21A07DCB56A6AF56EB3",
"B263A1305E057F984D38726A1B46874109F417BCA112674C528262A40A629AF1"
Expand All @@ -2244,7 +2254,7 @@ def test_brainpoolP512r1(self):
"373778F9DE6B6497B1EF825FF24F42F9B4A4BD7382CFC3378A540B1B7F0C1B95"
"6C2F",
b"abc",
sha512,
hashlib.sha512,
"0154FD3836AF92D0DCA57DD5341D3053988534FDE8318FC6AAAAB68E2E6F4339"
"B19F2F281A7E0B22C269D93CF8794A9278880ED7DBB8D9362CAEACEE54432055"
"2251",
Expand Down

0 comments on commit fd8b239

Please sign in to comment.