Skip to content

Commit

Permalink
Added mypy check to tox
Browse files Browse the repository at this point in the history
* Added type hints -- removed 12 type annotation
* Adding py_ecc module and mypy changes
  • Loading branch information
6ug committed Oct 25, 2018
1 parent 970ea66 commit 2d571e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
3 changes: 2 additions & 1 deletion py_ecc/bn128/bn128_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
FQ2,
FQ12,
)
from typing import Tuple, Union


curve_order = 21888242871839275222246405745257275088548364400416034343698204186575808495617
Expand Down Expand Up @@ -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


Expand Down
10 changes: 5 additions & 5 deletions py_ecc/bn128/bn128_field_elements.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -12,7 +13,6 @@
else:
int_types = (int,)


# The prime modulus of the field
field_modulus = 21888242871839275222246405745257275088696311157297823662689037894645226208583
# See, it's prime!
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
22 changes: 11 additions & 11 deletions py_ecc/secp256k1/secp256k1.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,7 +13,6 @@ def safe_ord(value):
else:
return ord(value)


# Elliptic curve parameters (secp256k1)
P = 2**256 - 2**32 - 977
N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
Expand Down Expand Up @@ -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
Expand All @@ -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]:
Expand All @@ -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)

Expand All @@ -102,15 +102,15 @@ 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))


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))


Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions py_ecc/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Represents common DRY module to avoid repetition
from typing import Tuple

Point2D = Tuple[int, int]
Point3D = Tuple[int, int, int]
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2d571e1

Please sign in to comment.