Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style fixes #170

Merged
merged 14 commits into from
Dec 17, 2019
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ matrix:
env: TOX_ENV=py37
dist: xenial
sudo: true
- python: 3.8
env: TOX_ENV=codechecks
dist: xenial
sudo: true
- python: 3.8
env: TOX_ENV=py38
dist: xenial
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],
install_requires=['six>=1.9.0'],
extras_require={
'gmpy2': 'gmpy2',
'gmpy': 'gmpy',
},
'gmpy2': 'gmpy2',
'gmpy': 'gmpy',
},
)
6 changes: 4 additions & 2 deletions speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def do(setup_statements, statement):
break
return x / number

prnt_form = ("{name:>16}{sep:1} {siglen:>6} {keygen:>9{form}}{unit:1} "
prnt_form = (
"{name:>16}{sep:1} {siglen:>6} {keygen:>9{form}}{unit:1} "
"{keygen_inv:>9{form_inv}} {sign:>9{form}}{unit:1} "
"{sign_inv:>9{form_inv}} {verify:>9{form}}{unit:1} "
"{verify_inv:>9{form_inv}}")
Expand Down Expand Up @@ -49,7 +50,8 @@ def do(setup_statements, statement):

print('')

ecdh_form = ("{name:>16}{sep:1} {ecdh:>9{form}}{unit:1} "
ecdh_form = (
"{name:>16}{sep:1} {ecdh:>9{form}}{unit:1} "
"{ecdh_inv:>9{form_inv}}")

print(ecdh_form.format(ecdh="ecdh", ecdh_inv="ecdh/s", name="", sep="",
Expand Down
6 changes: 3 additions & 3 deletions src/ecdsa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .keys import SigningKey, VerifyingKey, BadSignatureError, BadDigestError,\
MalformedPointError
MalformedPointError
from .curves import NIST192p, NIST224p, NIST256p, NIST384p, NIST521p,\
SECP256k1, BRAINPOOLP160r1, BRAINPOOLP192r1, BRAINPOOLP224r1,\
BRAINPOOLP256r1, BRAINPOOLP320r1, BRAINPOOLP384r1, BRAINPOOLP512r1
SECP256k1, BRAINPOOLP160r1, BRAINPOOLP192r1, BRAINPOOLP224r1,\
BRAINPOOLP256r1, BRAINPOOLP320r1, BRAINPOOLP384r1, BRAINPOOLP512r1
from .ecdh import ECDH, NoKeyError, NoCurveError, InvalidCurveError, \
InvalidSharedSecretError
from .der import UnexpectedDER
Expand Down
3 changes: 2 additions & 1 deletion src/ecdsa/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def str_idx_as_int(string, index):
if sys.version_info < (3, 0):
def normalise_bytes(buffer_object):
"""Cast the input into array of bytes."""
return buffer(buffer_object)
# flake8 runs on py3 where `buffer` indeed doesn't exist...
return buffer(buffer_object) # noqa: F821

def hmac_compat(ret):
return ret
Expand Down
4 changes: 2 additions & 2 deletions src/ecdsa/curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ["UnknownCurveError", "orderlen", "Curve", "NIST192p",
"NIST224p", "NIST256p", "NIST384p", "NIST521p", "curves",
"find_curve", "SECP256k1", "BRAINPOOLP160r1", "BRAINPOOLP192r1",
"BRAINPOOLP224r1", "BRAINPOOLP256r1" "BRAINPOOLP320r1",
"BRAINPOOLP224r1", "BRAINPOOLP256r1", "BRAINPOOLP320r1",
"BRAINPOOLP384r1", "BRAINPOOLP512r1"]


Expand Down Expand Up @@ -115,7 +115,7 @@ def __repr__(self):


curves = [NIST192p, NIST224p, NIST256p, NIST384p, NIST521p, SECP256k1,
BRAINPOOLP160r1, BRAINPOOLP192r1, BRAINPOOLP224r1, BRAINPOOLP256r1,
BRAINPOOLP160r1, BRAINPOOLP192r1, BRAINPOOLP224r1, BRAINPOOLP256r1,
BRAINPOOLP320r1, BRAINPOOLP384r1, BRAINPOOLP512r1]


Expand Down
8 changes: 4 additions & 4 deletions src/ecdsa/ecdh.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class InvalidSharedSecretError(Exception):
class ECDH(object):
"""
Elliptic-curve Diffie-Hellman (ECDH). A key agreement protocol.
Allows two parties, each having an elliptic-curve public-private key

Allows two parties, each having an elliptic-curve public-private key
pair, to establish a shared secret over an insecure channel
"""""

Expand Down Expand Up @@ -283,8 +283,8 @@ def generate_sharedsecret_bytes(self):
:rtype: byte string
"""
return number_to_string(
self.generate_sharedsecret(),
self.private_key.curve.order)
self.generate_sharedsecret(),
self.private_key.curve.order)

def generate_sharedsecret(self):
"""
Expand Down
20 changes: 10 additions & 10 deletions src/ecdsa/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ def __init__(self, generator, point, verify=True):
raise InvalidPointError("Generator point order is bad.")

def __eq__(self, other):
if isinstance(other, Public_key):
if isinstance(other, Public_key):
"""Return True if the points are identical, False otherwise."""
return self.curve == other.curve \
and self.point == other.point
and self.point == other.point
return NotImplemented

def verifies(self, hash, signature):
Expand Down Expand Up @@ -182,12 +182,12 @@ def __init__(self, public_key, secret_multiplier):

self.public_key = public_key
self.secret_multiplier = secret_multiplier

def __eq__(self, other):
if isinstance(other, Private_key):
if isinstance(other, Private_key):
"""Return True if the points are identical, False otherwise."""
return self.public_key == other.public_key \
and self.secret_multiplier == other.secret_multiplier
and self.secret_multiplier == other.secret_multiplier
return NotImplemented

def sign(self, hash, random_k):
Expand Down Expand Up @@ -220,8 +220,8 @@ def sign(self, hash, random_k):
r = p1.x() % n
if r == 0:
raise RSZeroError("amazingly unlucky random number r")
s = (numbertheory.inverse_mod(k, n) *
(hash + (self.secret_multiplier * r) % n)) % n
s = (numbertheory.inverse_mod(k, n)
* (hash + (self.secret_multiplier * r) % n)) % n
if s == 0:
raise RSZeroError("amazingly unlucky random number s")
return Signature(r, s)
Expand Down Expand Up @@ -277,8 +277,8 @@ def point_is_valid(generator, x, y):
if not curve.contains_point(x, y):
return False
if curve.cofactor() != 1 and \
not n * ellipticcurve.PointJacobi(curve, x, y, 1)\
== ellipticcurve.INFINITY:
not n * ellipticcurve.PointJacobi(curve, x, y, 1)\
== ellipticcurve.INFINITY:
return False
return True

Expand Down Expand Up @@ -361,7 +361,7 @@ def point_is_valid(generator, x, y):
generator_secp256k1 = ellipticcurve.PointJacobi(
curve_secp256k1, _Gx, _Gy, 1, _r, generator=True)

# Brainpool P-160-r1
# Brainpool P-160-r1
_a = 0x340E7BE2A280EB74E2BE61BADA745D97E8F7C300
_b = 0x1E589A8595423412134FAA2DBDEC95C8D8675E58
_p = 0xE95E4A5F737059DC60DFC7AD95B3D8139515620F
Expand Down
24 changes: 12 additions & 12 deletions src/ecdsa/ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@

try:
from gmpy2 import mpz
GMPY=True
GMPY = True
except ImportError:
try:
from gmpy import mpz
GMPY=True
GMPY = True
except ImportError:
GMPY=False
GMPY = False


from six import python_2_unicode_compatible
Expand Down Expand Up @@ -84,13 +84,13 @@ def __init__(self, p, a, b, h=None):
self.__a = a
self.__b = b
self.__h = h

def __eq__(self, other):
if isinstance(other, CurveFp):
if isinstance(other, CurveFp):
"""Return True if the curves are identical, False otherwise."""
return self.__p == other.__p \
and self.__a == other.__a \
and self.__b == other.__b
and self.__a == other.__a \
and self.__b == other.__b
return NotImplemented

def __hash__(self):
Expand Down Expand Up @@ -155,7 +155,7 @@ def __init__(self, curve, x, y, z, order=None, generator=False):
self.__y = y
self.__z = z
self.__order = order
self.__precompute=[]
self.__precompute = []
if generator:
assert order
i = 1
Expand Down Expand Up @@ -192,7 +192,7 @@ def __eq__(self, other):
# compare the fractions by bringing them to the same denominator
# depend on short-circuit to save 4 multiplications in case of inequality
return (x1 * zz2 - x2 * zz1) % p == 0 and \
(y1 * zz2 * z2 - y2 * zz1 * z1) % p == 0
(y1 * zz2 * z2 - y2 * zz1 * z1) % p == 0

def order(self):
"""Return the order of the point.
Expand Down Expand Up @@ -604,10 +604,10 @@ def __init__(self, curve, x, y, order=None):

def __eq__(self, other):
"""Return True if the points are identical, False otherwise."""
if isinstance(other, Point):
if isinstance(other, Point):
return self.__curve == other.__curve \
and self.__x == other.__x \
and self.__y == other.__y
and self.__x == other.__x \
and self.__y == other.__y
return NotImplemented

def __neg__(self):
Expand Down
15 changes: 7 additions & 8 deletions src/ecdsa/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def __repr__(self):

def __eq__(self, other):
"""Return True if the points are identical, False otherwise."""
if isinstance(other, VerifyingKey):
if isinstance(other, VerifyingKey):
return self.curve == other.curve \
and self.pubkey == other.pubkey
return NotImplemented
Expand Down Expand Up @@ -245,9 +245,9 @@ def _from_hybrid(cls, string, curve, validate_point):
point = cls._from_raw_encoding(string[1:], curve)

# but validate if it's self-consistent if we're asked to do that
if validate_point and \
(point.y() & 1 and string[:1] != b('\x07') or
(not point.y() & 1) and string[:1] != b('\x06')):
if validate_point \
and (point.y() & 1 and string[:1] != b('\x07')
or (not point.y() & 1) and string[:1] != b('\x06')):
raise MalformedPointError("Inconsistent hybrid point encoding")

return point
Expand Down Expand Up @@ -677,10 +677,10 @@ def __init__(self, _error__please_use_generate=None):
self.baselen = None
self.verifying_key = None
self.privkey = None

def __eq__(self, other):
"""Return True if the points are identical, False otherwise."""
if isinstance(other, SigningKey):
if isinstance(other, SigningKey):
return self.curve == other.curve \
and self.verifying_key == other.verifying_key \
and self.privkey == other.privkey
Expand Down Expand Up @@ -967,8 +967,7 @@ def to_der(self, point_encoding="uncompressed"):
der.encode_integer(1),
der.encode_octet_string(self.to_string()),
der.encode_constructed(0, self.curve.encoded_oid),
der.encode_constructed(1, der.encode_bitstring(encoded_vk, 0)),
)
der.encode_constructed(1, der.encode_bitstring(encoded_vk, 0)))

def get_verifying_key(self):
"""
Expand Down
10 changes: 5 additions & 5 deletions src/ecdsa/numbertheory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
xrange = range
try:
from gmpy2 import powmod
GMPY2=True
GMPY=False
GMPY2 = True
GMPY = False
except ImportError:
GMPY2=False
GMPY2 = False
try:
from gmpy import mpz
GMPY=True
GMPY = True
except ImportError:
GMPY=False
GMPY = False

import math
import warnings
Expand Down
3 changes: 2 additions & 1 deletion src/ecdsa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def entropy_to_bits(ent_256):
return ''.join(bin(ord(x))[2:].zfill(8) for x in ent_256)


if sys.version < '2.7': #Can't add a method to a built-in type so we are stuck with this
if sys.version < '2.7':
# Can't add a method to a built-in type so we are stuck with this
def bit_length(x):
return len(bin(x)) - 2
else:
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ exclude = src/ecdsa/test*.py
# E305: expected 2 blank lines after class or function definition, found 1
# E501: line too long
# E502: the backslash is redundant between brackets
# E741: ambiguous variable name
# W391: blank line at end of file
ignore = E111,E114,E226,E231,E266,E302,E305,E501,E502,W391
# W503: line break before binary operator
ignore = E111,E114,E226,E231,E266,E302,E305,E501,E502,W391,E741,W503