Skip to content

Commit

Permalink
Added some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
6ug committed Oct 18, 2018
1 parent d271223 commit e4de2be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 5 additions & 3 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
from typing import NewType, Tuple

sys.setrecursionlimit(10000)

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

Point2D = Tuple[int, int]
Point3D = Tuple[int, int, int]

# The prime modulus of the field
field_modulus = 21888242871839275222246405745257275088696311157297823662689037894645226208583
Expand Down Expand Up @@ -119,14 +121,14 @@ def zero(cls):


# Utility methods for polynomial math
def deg(p: Tuple[int, int, int]) -> int:
def deg(p: Point3D) -> int:
d = len(p) - 1
while p[d] == 0 and d:
d -= 1
return d


def poly_rounded_div(a: Tuple[int, int, int], b: Tuple[int, int, int]) -> Tuple[int, int]:
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: 12 additions & 10 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
from typing import NewType, Tuple, Union

if sys.version_info.major == 2:
safe_ord = ord
Expand All @@ -12,6 +12,8 @@ def safe_ord(value):
else:
return ord(value)

Point2D = Tuple[int, int]
Point3D = Tuple[int, int, int]

# Elliptic curve parameters (secp256k1)
P = 2**256 - 2**32 - 977
Expand Down Expand Up @@ -43,12 +45,12 @@ def inv(a, n):
return lm % n


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


def jacobian_double(p: Tuple[int, int, int]) -> Tuple[int, int, int]:
def jacobian_double(p: Point3D) -> Point3D:
if not p[1]:
return (0, 0, 0)
ysq = (p[1] ** 2) % P
Expand All @@ -60,7 +62,7 @@ def jacobian_double(p: Tuple[int, int, int]) -> Tuple[int, int, int]:
return (nx, ny, nz)


def jacobian_add(p: Tuple[int, int, int], q: Tuple[int, int, int]) -> Tuple[int, int, int]:
def jacobian_add(p: Point3D, q: Point3D) -> Point3D:
if not p[1]:
return q
if not q[1]:
Expand All @@ -84,7 +86,7 @@ def jacobian_add(p: Tuple[int, int, int], q: Tuple[int, int, int]) -> Tuple[int,
return (nx, ny, nz)


def from_jacobian(p: Tuple[int, int, int]) -> Tuple[int, int]:
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 +104,15 @@ def jacobian_multiply(a, n):
return jacobian_add(jacobian_double(jacobian_multiply(a, n // 2)), a)


def multiply(a: Tuple[int, int], n: int) -> Tuple[int, int]:
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: str) -> Tuple[int, int]:
def privtopub(privkey: str) -> Point2D:
return multiply(G, bytes_to_int(privkey))


Expand All @@ -125,7 +127,7 @@ def deterministic_generate_k(msghash, priv):


# bytes32, bytes32 -> v, r, s (as numbers)
def ecdsa_raw_sign(msghash: str, priv: str) -> Tuple[int, int, int]:
def ecdsa_raw_sign(msghash: str, priv: str) -> Point3D:

z = bytes_to_int(msghash)
k = deterministic_generate_k(msghash, priv)
Expand All @@ -137,7 +139,7 @@ def ecdsa_raw_sign(msghash: str, priv: str) -> Tuple[int, int, int]:
return v, r, s


def ecdsa_raw_recover(msghash: str, vrs: Tuple[int, int, int]) -> Union[bool, Tuple[int, int]]:
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 +150,7 @@ def ecdsa_raw_recover(msghash: str, vrs: Tuple[int, int, int]) -> Union[bool, Tu
# 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

0 comments on commit e4de2be

Please sign in to comment.