Skip to content

Commit

Permalink
Added type hints -- removed 12 type annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
6ug committed Oct 18, 2018
1 parent c866d3d commit 04e4e10
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 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
8 changes: 4 additions & 4 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 typing import Tuple

sys.setrecursionlimit(10000)

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: Tuple[int, int, int]) -> 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: Tuple[int, int, int], b: Tuple[int, int, int]) -> Tuple[int, int]:
dega = deg(a)
degb = deg(b)
temp = [x for x in a]
Expand Down
18 changes: 9 additions & 9 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 typing import Tuple, Union

if sys.version_info.major == 2:
safe_ord = ord
Expand Down Expand Up @@ -43,12 +43,12 @@ def inv(a, n):
return lm % n


def to_jacobian(p):
def to_jacobian(p: Tuple[int, int]) -> Tuple[int, int, int]:
o = (p[0], p[1], 1)
return o


def jacobian_double(p):
def jacobian_double(p: Tuple[int, int, int]) -> Tuple[int, int, int]:
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: Tuple[int, int, int], q: Tuple[int, int, int]) -> Tuple[int, int, int]:
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: Tuple[int, int, int]) -> Tuple[int, int]:
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: Tuple[int, int], n: int) -> Tuple[int, int]:
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) -> Tuple[int, int]:
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) -> Tuple[int, int, int]:

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: Tuple[int, int, int]) -> Union[bool, Tuple[int, int]]:
v, r, s = vrs
if not (27 <= v <= 34):
raise ValueError("%d must in range 27-31" % v)
Expand Down

0 comments on commit 04e4e10

Please sign in to comment.