Skip to content

Commit

Permalink
upgrade typing
Browse files Browse the repository at this point in the history
move constants

import from py_ecc.fields and sort import

the cast is not necessary

disable type check
  • Loading branch information
ChihChengLiang committed Mar 6, 2019
1 parent e95a50e commit f78a2ce
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 48 deletions.
15 changes: 11 additions & 4 deletions py_ecc/bls_api/api.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from typing import (
Sequence,
)

from eth_typing import (
BLSPubkey,
BLSSignature,
Hash32,
)
from eth_utils import (
ValidationError,
)

from py_ecc.fields import (
optimized_bls12_381_FQ12 as FQ12,
)
from py_ecc.optimized_bls12_381 import (
FQ12,
G1,
Z1,
Z2,
Expand All @@ -15,12 +24,10 @@
neg,
pairing,
)

from .typing import (
BLSPubkey,
BLSSignature,
G1Uncompressed,
G2Uncompressed,
Hash32,
)
from .utils import (
G1_to_pubkey,
Expand Down
12 changes: 12 additions & 0 deletions py_ecc/bls_api/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
from py_ecc.fields import (
optimized_bls12_381_FQ2 as FQ2,
)
from py_ecc.optimized_bls12_381 import (
field_modulus as q,
)

G2_cofactor = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109 # noqa: E501
FQ2_order = q ** 2 - 1
eighth_roots_of_unity = tuple(
FQ2([1, 1]) ** ((FQ2_order * k) // 8)
for k in range(8)
)

POW_2_381 = 2**381
POW_2_382 = 2**382
Expand Down
2 changes: 1 addition & 1 deletion py_ecc/bls_api/hash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .typing import Hash32
from eth_typing import Hash32
from eth_hash.auto import keccak
from typing import Union

Expand Down
13 changes: 5 additions & 8 deletions py_ecc/bls_api/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
NewType,
Tuple,
)
from py_ecc.optimized_bls12_381.optimized_field_elements import (
FQ,
FQ2,
)


Hash32 = NewType("Hash32", bytes)
BLSPubkey = NewType('BLSPubkey', bytes) # bytes48
BLSSignature = NewType('BLSSignature', bytes) # bytes96
from py_ecc.fields import (
optimized_bls12_381_FQ as FQ,
optimized_bls12_381_FQ2 as FQ2,
)


# TODO: FQ and FQ2 here are invalid types, they need to be fixed in the future
G1Uncompressed = NewType('G1Uncompressed', Tuple[FQ, FQ, FQ])
G1Compressed = NewType('G1Compressed', int)

Expand Down
52 changes: 21 additions & 31 deletions py_ecc/bls_api/utils.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
from typing import ( # noqa: F401
Dict,
Sequence,
Tuple,
Union,
from eth_typing import (
BLSPubkey,
BLSSignature,
Hash32,
)

from eth_utils import (
big_endian_to_int,
)

from py_ecc.fields import (
optimized_bls12_381_FQ as FQ,
optimized_bls12_381_FQ2 as FQ2,
)
from py_ecc.optimized_bls12_381 import (
FQ,
FQ2,
Z1,
Z2,
b,
b2,
field_modulus as q,
is_inf,
is_on_curve,
multiply,
normalize,
Z1,
Z2,
)

from .constants import (
POW_2_381,
POW_2_382,
POW_2_383,
FQ2_order,
G2_cofactor,
eighth_roots_of_unity,
)
from .hash import (
hash_eth2,
)
from .typing import (
BLSPubkey,
BLSSignature,
G1Compressed,
G1Uncompressed,
G2Compressed,
G2Uncompressed,
Hash32,
)
from .constants import (
POW_2_381,
POW_2_382,
POW_2_383,
)

G2_cofactor = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109 # noqa: E501
FQ2_order = q ** 2 - 1
eighth_roots_of_unity = [
FQ2([1, 1]) ** ((FQ2_order * k) // 8)
for k in range(8)
]


#
# Helpers
#

def coerce_to_int(element: Union[int, FQ]) -> int:
return element if isinstance(element, int) else element.n


def modular_squareroot(value: FQ2) -> FQ2:
"""
Expand All @@ -69,8 +59,8 @@ def modular_squareroot(value: FQ2) -> FQ2:
if check in eighth_roots_of_unity[::2]:
x1 = candidate_squareroot / eighth_roots_of_unity[eighth_roots_of_unity.index(check) // 2]
x2 = -x1
x1_re, x1_im = coerce_to_int(x1.coeffs[0]), coerce_to_int(x1.coeffs[1])
x2_re, x2_im = coerce_to_int(x2.coeffs[0]), coerce_to_int(x2.coeffs[1])
x1_re, x1_im = x1.coeffs
x2_re, x2_im = x2.coeffs
return x1 if (x1_im > x2_im or (x1_im == x2_im and x1_re > x2_re)) else x2
return None

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
package_data={'py_ecc': ['py.typed']},
install_requires=[
"eth-hash[pycryptodome]",
"eth-typing>=2.0.0,<3.0.0",
"eth-typing>=2.1.0,<3.0.0",
"eth-utils>=1.3.0,<2",
"mypy-extensions>=0.4.1",
],
Expand Down
7 changes: 5 additions & 2 deletions tests/test_bls_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from eth_utils import (
big_endian_to_int,
)
import pytest

from py_ecc.bls_api.api import (
aggregate_pubkeys,
aggregate_signatures,
Expand All @@ -21,16 +23,17 @@
hash_to_G2,
signature_to_G2,
)
from py_ecc.fields import (
optimized_bls12_381_FQ2 as FQ2,
)
from py_ecc.optimized_bls12_381 import (
FQ2,
G1,
G2,
b2,
is_on_curve,
multiply,
normalize,
)
import pytest


@pytest.mark.parametrize(
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ extras=lint
commands=
flake8 {toxinidir}/py_ecc
flake8 {toxinidir}/tests
mypy --follow-imports=silent --ignore-missing-imports --no-strict-optional -p py_ecc.bls_api

0 comments on commit f78a2ce

Please sign in to comment.