From 2d571e1067b3fb805dc12b5bf63268f7d9d3a032 Mon Sep 17 00:00:00 2001 From: 6ug Date: Thu, 18 Oct 2018 21:52:42 +0530 Subject: [PATCH 1/2] Added mypy check to tox * Added type hints -- removed 12 type annotation * Adding py_ecc module and mypy changes --- py_ecc/bn128/bn128_curve.py | 3 ++- py_ecc/bn128/bn128_field_elements.py | 10 +++++----- py_ecc/secp256k1/secp256k1.py | 22 +++++++++++----------- py_ecc/types.py | 5 +++++ tox.ini | 1 + 5 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 py_ecc/types.py diff --git a/py_ecc/bn128/bn128_curve.py b/py_ecc/bn128/bn128_curve.py index c6337971..17938fac 100644 --- a/py_ecc/bn128/bn128_curve.py +++ b/py_ecc/bn128/bn128_curve.py @@ -6,6 +6,7 @@ FQ2, FQ12, ) +from typing import Tuple, Union curve_order = 21888242871839275222246405745257275088548364400416034343698204186575808495617 @@ -42,7 +43,7 @@ # Check if a point is the point at infinity -def is_inf(pt): +def is_inf(pt: Tuple[int]) -> bool: return pt is None diff --git a/py_ecc/bn128/bn128_field_elements.py b/py_ecc/bn128/bn128_field_elements.py index 6012d1ce..21300aca 100644 --- a/py_ecc/bn128/bn128_field_elements.py +++ b/py_ecc/bn128/bn128_field_elements.py @@ -1,7 +1,8 @@ from __future__ import absolute_import import sys - +from typing import NewType, Tuple +from py_ecc.types import Point2D, Point3D sys.setrecursionlimit(10000) @@ -12,7 +13,6 @@ else: int_types = (int,) - # The prime modulus of the field field_modulus = 21888242871839275222246405745257275088696311157297823662689037894645226208583 # See, it's prime! @@ -24,7 +24,7 @@ # Extended euclidean algorithm to find modular inverses for # integers -def inv(a, n): +def inv(a: int, n: int) -> int: if a == 0: return 0 lm, hm = 1, 0 @@ -119,14 +119,14 @@ def zero(cls): # Utility methods for polynomial math -def deg(p): +def deg(p: Point3D) -> int: d = len(p) - 1 while p[d] == 0 and d: d -= 1 return d -def poly_rounded_div(a, b): +def poly_rounded_div(a: Point3D, b: Point3D) -> Point2D: dega = deg(a) degb = deg(b) temp = [x for x in a] diff --git a/py_ecc/secp256k1/secp256k1.py b/py_ecc/secp256k1/secp256k1.py index db93ee56..7314e195 100644 --- a/py_ecc/secp256k1/secp256k1.py +++ b/py_ecc/secp256k1/secp256k1.py @@ -1,7 +1,8 @@ import hashlib import hmac import sys - +from typing import NewType, Tuple, Union +from py_ecc.types import Point2D, Point3D if sys.version_info.major == 2: safe_ord = ord @@ -12,7 +13,6 @@ def safe_ord(value): else: return ord(value) - # Elliptic curve parameters (secp256k1) P = 2**256 - 2**32 - 977 N = 115792089237316195423570985008687907852837564279074904382605163141518161494337 @@ -43,12 +43,12 @@ def inv(a, n): return lm % n -def to_jacobian(p): +def to_jacobian(p: Point2D) -> Point3D: o = (p[0], p[1], 1) return o -def jacobian_double(p): +def jacobian_double(p: Point3D) -> Point3D: if not p[1]: return (0, 0, 0) ysq = (p[1] ** 2) % P @@ -60,7 +60,7 @@ def jacobian_double(p): return (nx, ny, nz) -def jacobian_add(p, q): +def jacobian_add(p: Point3D, q: Point3D) -> Point3D: if not p[1]: return q if not q[1]: @@ -84,7 +84,7 @@ def jacobian_add(p, q): return (nx, ny, nz) -def from_jacobian(p): +def from_jacobian(p: Point3D) -> Point2D: z = inv(p[2], P) return ((p[0] * z**2) % P, (p[1] * z**3) % P) @@ -102,7 +102,7 @@ def jacobian_multiply(a, n): return jacobian_add(jacobian_double(jacobian_multiply(a, n // 2)), a) -def multiply(a, n): +def multiply(a: Point2D, n: int) -> Point2D: return from_jacobian(jacobian_multiply(to_jacobian(a), n)) @@ -110,7 +110,7 @@ def add(a, b): return from_jacobian(jacobian_add(to_jacobian(a), to_jacobian(b))) -def privtopub(privkey): +def privtopub(privkey: str) -> Point2D: return multiply(G, bytes_to_int(privkey)) @@ -125,7 +125,7 @@ def deterministic_generate_k(msghash, priv): # bytes32, bytes32 -> v, r, s (as numbers) -def ecdsa_raw_sign(msghash, priv): +def ecdsa_raw_sign(msghash: str, priv: str) -> Point3D: z = bytes_to_int(msghash) k = deterministic_generate_k(msghash, priv) @@ -137,7 +137,7 @@ def ecdsa_raw_sign(msghash, priv): return v, r, s -def ecdsa_raw_recover(msghash, vrs): +def ecdsa_raw_recover(msghash: str, vrs: Point3D) -> Point2D: v, r, s = vrs if not (27 <= v <= 34): raise ValueError("%d must in range 27-31" % v) @@ -148,7 +148,7 @@ def ecdsa_raw_recover(msghash, vrs): # If xcubedaxb is not a quadratic residue, then r cannot be the x coord # for a point on the curve, and so the sig is invalid if (xcubedaxb - y * y) % P != 0 or not (r % N) or not (s % N): - return False + raise ValueError("sig is invalid, %d cannot be the x coord for point on curve" % r) z = bytes_to_int(msghash) Gz = jacobian_multiply((Gx, Gy, 1), (N - z) % N) XY = jacobian_multiply((x, y, 1), s) diff --git a/py_ecc/types.py b/py_ecc/types.py new file mode 100644 index 00000000..f0e95c11 --- /dev/null +++ b/py_ecc/types.py @@ -0,0 +1,5 @@ +# Represents common DRY module to avoid repetition +from typing import Tuple + +Point2D = Tuple[int, int] +Point3D = Tuple[int, int, int] diff --git a/tox.ini b/tox.ini index ed3662a2..90e76a90 100644 --- a/tox.ini +++ b/tox.ini @@ -26,3 +26,4 @@ basepython=python extras=lint commands= flake8 {toxinidir}/py_ecc + mypy --follow-imports=silent --warn-unused-ignores --ignore-missing-imports --no-strict-optional --check-untyped-defs --disallow-incomplete-defs --disallow-untyped-defs --disallow-any-generics -p py_ecc From d86ca0e2afe7a69cc4b6b967bc81ba0b03a2db90 Mon Sep 17 00:00:00 2001 From: 6ug Date: Sun, 28 Oct 2018 03:28:31 +0530 Subject: [PATCH 2/2] Added CI failure fixes * Removed lints errors * Fixed 3.4 failures --- py_ecc/bn128/bn128_curve.py | 2 +- py_ecc/bn128/bn128_field_elements.py | 1 - py_ecc/secp256k1/secp256k1.py | 1 - setup.py | 3 ++- tox.ini | 2 +- 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/py_ecc/bn128/bn128_curve.py b/py_ecc/bn128/bn128_curve.py index 17938fac..0f6c8b07 100644 --- a/py_ecc/bn128/bn128_curve.py +++ b/py_ecc/bn128/bn128_curve.py @@ -6,7 +6,7 @@ FQ2, FQ12, ) -from typing import Tuple, Union +from typing import Tuple curve_order = 21888242871839275222246405745257275088548364400416034343698204186575808495617 diff --git a/py_ecc/bn128/bn128_field_elements.py b/py_ecc/bn128/bn128_field_elements.py index 21300aca..d562bcc5 100644 --- a/py_ecc/bn128/bn128_field_elements.py +++ b/py_ecc/bn128/bn128_field_elements.py @@ -1,7 +1,6 @@ from __future__ import absolute_import import sys -from typing import NewType, Tuple from py_ecc.types import Point2D, Point3D sys.setrecursionlimit(10000) diff --git a/py_ecc/secp256k1/secp256k1.py b/py_ecc/secp256k1/secp256k1.py index 7314e195..1467143a 100644 --- a/py_ecc/secp256k1/secp256k1.py +++ b/py_ecc/secp256k1/secp256k1.py @@ -1,7 +1,6 @@ import hashlib import hmac import sys -from typing import NewType, Tuple, Union from py_ecc.types import Point2D, Point3D if sys.version_info.major == 2: diff --git a/setup.py b/setup.py index 836d81b0..b93a2fcf 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,8 @@ "pytest-xdist" ], 'lint': [ - "flake8==3.4.1" + "flake8==3.4.1", + "mypy==0.641" ], } diff --git a/tox.ini b/tox.ini index 90e76a90..cee77cc5 100644 --- a/tox.ini +++ b/tox.ini @@ -14,7 +14,7 @@ commands=py.test {posargs:tests} passenv= PYTEST_ADDOPTS deps = -extras = test +extras = test, lint basepython = py34: python3.4 py35: python3.5