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

[WIP] Added type hinting #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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


curve_order = 21888242871839275222246405745257275088548364400416034343698204186575808495617
Expand Down Expand Up @@ -42,7 +43,7 @@


# Check if a point is the point at infinity
def is_inf(pt):
6ug marked this conversation as resolved.
Show resolved Hide resolved
def is_inf(pt: Tuple[int]) -> bool:
return pt is None


Expand Down
9 changes: 4 additions & 5 deletions py_ecc/bn128/bn128_field_elements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import absolute_import

import sys

from py_ecc.types import Point2D, Point3D

sys.setrecursionlimit(10000)

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


# The prime modulus of the field
field_modulus = 21888242871839275222246405745257275088696311157297823662689037894645226208583
# See, it's prime!
Expand All @@ -24,7 +23,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 +118,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
21 changes: 10 additions & 11 deletions py_ecc/secp256k1/secp256k1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
import hmac
import sys

from py_ecc.types import Point2D, Point3D

if sys.version_info.major == 2:
safe_ord = ord
Expand All @@ -12,7 +12,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 +42,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 +59,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 +83,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 +101,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 +124,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 +136,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 +147,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]
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"pytest-xdist"
],
'lint': [
"flake8==3.4.1"
"flake8==3.4.1",
"mypy==0.641"
],
}

Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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