From 24e6ba94c7dd884abb510f2ba53995ec005d7782 Mon Sep 17 00:00:00 2001 From: Bhargavasomu Date: Fri, 14 Dec 2018 00:49:10 +0530 Subject: [PATCH] Modify tests as per the refactored API --- tests/test_bn128.py | 297 --------------------------------- tests/test_curve_generators.py | 76 +++++++++ tests/test_field_elements.py | 68 ++++++++ tests/test_pairing.py | 190 +++++++++++++++++++++ tests/test_secp256k1.py | 8 +- tox.ini | 5 +- 6 files changed, 342 insertions(+), 302 deletions(-) delete mode 100644 tests/test_bn128.py create mode 100644 tests/test_curve_generators.py create mode 100644 tests/test_field_elements.py create mode 100644 tests/test_pairing.py diff --git a/tests/test_bn128.py b/tests/test_bn128.py deleted file mode 100644 index 723fb306..00000000 --- a/tests/test_bn128.py +++ /dev/null @@ -1,297 +0,0 @@ -import time - -import pytest - -from py_ecc import bn128, optimized_bn128 - - -@pytest.fixture(params=[bn128, optimized_bn128]) -def lib(request): - return request.param - - -@pytest.fixture -def FQ(lib): - return lib.FQ - - -@pytest.fixture -def FQ2(lib): - return lib.FQ2 - - -@pytest.fixture -def FQ12(lib): - return lib.FQ12 - - -@pytest.fixture -def field_modulus(lib): - return lib.field_modulus - -@pytest.fixture -def G1(lib): - return lib.G1 - - -@pytest.fixture -def G2(lib): - return lib.G2 - - -@pytest.fixture -def G12(lib): - return lib.G12 - - -@pytest.fixture -def b(lib): - return lib.b - - -@pytest.fixture -def b2(lib): - return lib.b2 - - -@pytest.fixture -def b12(lib): - return lib.b12 - - -@pytest.fixture -def is_inf(lib): - return lib.is_inf - - -@pytest.fixture -def is_on_curve(lib): - return lib.is_on_curve - - -@pytest.fixture -def eq(lib): - return lib.eq - - -@pytest.fixture -def add(lib): - return lib.add - - -@pytest.fixture -def double(lib): - return lib.double - - -@pytest.fixture -def curve_order(lib): - return lib.curve_order - - -@pytest.fixture -def multiply(lib): - return lib.multiply - - -@pytest.fixture -def pairing(lib): - return lib.pairing - - -@pytest.fixture -def neg(lib): - return lib.neg - - -def test_FQ_object(FQ, field_modulus): - assert FQ(2) * FQ(2) == FQ(4) - assert FQ(2) / FQ(7) + FQ(9) / FQ(7) == FQ(11) / FQ(7) - assert FQ(2) * FQ(7) + FQ(9) * FQ(7) == FQ(11) * FQ(7) - assert FQ(9) ** field_modulus == FQ(9) - - -def test_FQ2_object(FQ2, field_modulus): - x = FQ2([1, 0]) - f = FQ2([1, 2]) - fpx = FQ2([2, 2]) - one = FQ2.one() - assert x + f == fpx - assert f / f == one - assert one / f + x / f == (one + x) / f - assert one * f + x * f == (one + x) * f - assert x ** (field_modulus ** 2 - 1) == one - - -def test_FQ12_object(FQ12, field_modulus): - x = FQ12([1] + [0] * 11) - f = FQ12([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) - fpx = FQ12([2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) - one = FQ12.one() - assert x + f == fpx - assert f / f == one - assert one / f + x / f == (one + x) / f - assert one * f + x * f == (one + x) * f - # This check takes too long - # assert x ** (field_modulus ** 12 - 1) == one - - -def test_G1_object(G1, eq, double, add, multiply, curve_order, is_inf): - assert eq(add(add(double(G1), G1), G1), double(double(G1))) - assert not eq(double(G1), G1) - assert eq(add(multiply(G1, 9), multiply(G1, 5)), add(multiply(G1, 12), multiply(G1, 2))) - assert is_inf(multiply(G1, curve_order)) - - -def test_G2_object(G2, b2, eq, add, double, multiply, is_inf, curve_order, field_modulus, is_on_curve): - assert eq(add(add(double(G2), G2), G2), double(double(G2))) - assert not eq(double(G2), G2) - assert eq(add(multiply(G2, 9), multiply(G2, 5)), add(multiply(G2, 12), multiply(G2, 2))) - assert is_inf(multiply(G2, curve_order)) - assert not is_inf(multiply(G2, 2 * field_modulus - curve_order)) - assert is_on_curve(multiply(G2, 9), b2) - - -def test_G12_object(G12, b12, eq, add, double, multiply, is_on_curve, is_inf, curve_order): - assert eq(add(add(double(G12), G12), G12), double(double(G12))) - assert not eq(double(G12), G12) - assert eq(add(multiply(G12, 9), multiply(G12, 5)), add(multiply(G12, 12), multiply(G12, 2))) - assert is_on_curve(multiply(G12, 9), b12) - assert is_inf(multiply(G12, curve_order)) - - -def test_pairing_negative_G1(pairing, G1, G2, FQ12, curve_order, multiply, neg): - p1 = pairing(G2, G1) - pn1 = pairing(G2, neg(G1)) - - assert p1 * pn1 == FQ12.one() - - -def test_pairing_negative_G2(pairing, G1, G2, FQ12, curve_order, multiply, neg): - p1 = pairing(G2, G1) - pn1 = pairing(G2, neg(G1)) - np1 = pairing(neg(G2), G1) - - assert p1 * np1 == FQ12.one() - assert pn1 == np1 - - -def test_pairing_output_order(G1, G2, FQ12, pairing, curve_order): - p1 = pairing(G2, G1) - - assert p1 ** curve_order == FQ12.one() - - -def test_pairing_bilinearity_on_G1(G1, G2, neg, multiply, pairing): - p1 = pairing(G2, G1) - p2 = pairing(G2, multiply(G1, 2)) - np1 = pairing(neg(G2), G1) - - assert p1 * p1 == p2 - - -def test_pairing_is_non_degenerate(G1, G2, neg, pairing, multiply): - p1 = pairing(G2, G1) - p2 = pairing(G2, multiply(G1, 2)) - np1 = pairing(neg(G2), G1) - - assert p1 != p2 and p1 != np1 and p2 != np1 - - -def test_pairing_bilinearity_on_G2(G1, G2, pairing, multiply): - p1 = pairing(G2, G1) - po2 = pairing(multiply(G2, 2), G1) - - assert p1 * p1 == po2 - - -def test_pairing_composit_check(G1, G2, multiply, pairing): - p3 = pairing(multiply(G2, 27), multiply(G1, 37)) - po3 = pairing(G2, multiply(G1, 999)) - assert p3 == po3 - - -""" -for lib in (bn128, optimized_bn128): - FQ, FQ2, FQ12, field_modulus = lib.FQ, lib.FQ2, lib.FQ12, lib.field_modulus - assert FQ(2) * FQ(2) == FQ(4) - assert FQ(2) / FQ(7) + FQ(9) / FQ(7) == FQ(11) / FQ(7) - assert FQ(2) * FQ(7) + FQ(9) * FQ(7) == FQ(11) * FQ(7) - assert FQ(9) ** field_modulus == FQ(9) - print('FQ works fine') - - x = FQ2([1, 0]) - f = FQ2([1, 2]) - fpx = FQ2([2, 2]) - one = FQ2.one() - assert x + f == fpx - assert f / f == one - assert one / f + x / f == (one + x) / f - assert one * f + x * f == (one + x) * f - assert x ** (field_modulus ** 2 - 1) == one - print('FQ2 works fine') - - x = FQ12([1] + [0] * 11) - f = FQ12([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) - fpx = FQ12([2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) - one = FQ12.one() - assert x + f == fpx - assert f / f == one - assert one / f + x / f == (one + x) / f - assert one * f + x * f == (one + x) * f - # This check takes too long - # assert x ** (field_modulus ** 12 - 1) == one - print('FQ12 works fine') - - G1, G2, G12, b, b2, b12, is_inf, is_on_curve, eq, add, double, curve_order, multiply = \ - lib.G1, lib.G2, lib.G12, lib.b, lib.b2, lib.b12, lib.is_inf, lib.is_on_curve, lib.eq, lib.add, lib.double, lib.curve_order, lib.multiply - - assert eq(add(add(double(G1), G1), G1), double(double(G1))) - assert not eq(double(G1), G1) - assert eq(add(multiply(G1, 9), multiply(G1, 5)), add(multiply(G1, 12), multiply(G1, 2))) - assert is_inf(multiply(G1, curve_order)) - print('G1 works fine') - - assert eq(add(add(double(G2), G2), G2), double(double(G2))) - assert not eq(double(G2), G2) - assert eq(add(multiply(G2, 9), multiply(G2, 5)), add(multiply(G2, 12), multiply(G2, 2))) - assert is_inf(multiply(G2, curve_order)) - assert not is_inf(multiply(G2, 2 * field_modulus - curve_order)) - assert is_on_curve(multiply(G2, 9), b2) - print('G2 works fine') - - assert eq(add(add(double(G12), G12), G12), double(double(G12))) - assert not eq(double(G12), G12) - assert eq(add(multiply(G12, 9), multiply(G12, 5)), add(multiply(G12, 12), multiply(G12, 2))) - assert is_on_curve(multiply(G12, 9), b12) - assert is_inf(multiply(G12, curve_order)) - print('G12 works fine') - - pairing, neg = lib.pairing, lib.neg - - print('Starting pairing tests') - a = time.time() - p1 = pairing(G2, G1) - pn1 = pairing(G2, neg(G1)) - assert p1 * pn1 == FQ12.one() - print('Pairing check against negative in G1 passed') - np1 = pairing(neg(G2), G1) - assert p1 * np1 == FQ12.one() - assert pn1 == np1 - print('Pairing check against negative in G2 passed') - assert p1 ** curve_order == FQ12.one() - print('Pairing output has correct order') - p2 = pairing(G2, multiply(G1, 2)) - assert p1 * p1 == p2 - print('Pairing bilinearity in G1 passed') - assert p1 != p2 and p1 != np1 and p2 != np1 - print('Pairing is non-degenerate') - po2 = pairing(multiply(G2, 2), G1) - assert p1 * p1 == po2 - print('Pairing bilinearity in G2 passed') - p3 = pairing(multiply(G2, 27), multiply(G1, 37)) - po3 = pairing(G2, multiply(G1, 999)) - assert p3 == po3 - print('Composite check passed') - print('Total time for pairings: %.3f' % (time.time() - a)) -""" diff --git a/tests/test_curve_generators.py b/tests/test_curve_generators.py new file mode 100644 index 00000000..cc561336 --- /dev/null +++ b/tests/test_curve_generators.py @@ -0,0 +1,76 @@ +import pytest + +from py_ecc import ( + field_elements, + optimized_field_elements, +) + +from py_ecc.bls12_381_curve import ( + BLS12_381_Curve, + Optimized_BLS12_381_Curve, +) +from py_ecc.bn128_curve import ( + BN128_Curve, + Optimized_BN128_Curve, +) + +from py_ecc.curve_properties import ( + curve_properties, + optimized_curve_properties, +) + +from py_ecc.field_properties import ( + field_properties, +) + + +@pytest.mark.parametrize( + 'curve_obj,G1', + ( + (BN128_Curve(), curve_properties["bn128"]["G1"]), + (Optimized_BN128_Curve(), optimized_curve_properties["bn128"]["G1"]), + (BLS12_381_Curve(), curve_properties["bls12_381"]["G1"]), + (Optimized_BLS12_381_Curve(), optimized_curve_properties["bls12_381"]["G1"]), + ), +) +def test_G1_object(curve_obj, G1): + assert curve_obj.eq(curve_obj.add(curve_obj.add(curve_obj.double(G1), G1), G1), curve_obj.double(curve_obj.double(G1))) + assert not curve_obj.eq(curve_obj.double(G1), G1) + assert curve_obj.eq(curve_obj.add(curve_obj.multiply(G1, 9), curve_obj.multiply(G1, 5)), curve_obj.add(curve_obj.multiply(G1, 12), curve_obj.multiply(G1, 2))) + assert curve_obj.is_inf(curve_obj.multiply(G1, curve_obj.curve_order)) + + +@pytest.mark.parametrize( + 'curve_obj,G2,field_modulus', + ( + (BN128_Curve(), curve_properties["bn128"]["G2"], field_properties["bn128"]["field_modulus"]), + (Optimized_BN128_Curve(), optimized_curve_properties["bn128"]["G2"], field_properties["bn128"]["field_modulus"]), + (BLS12_381_Curve(), curve_properties["bls12_381"]["G2"], field_properties["bls12_381"]["field_modulus"]), + (Optimized_BLS12_381_Curve(), optimized_curve_properties["bls12_381"]["G2"], field_properties["bls12_381"]["field_modulus"]), + ), +) +def test_G2_object(curve_obj, G2, field_modulus): + assert curve_obj.eq(curve_obj.add(curve_obj.add(curve_obj.double(G2), G2), G2), curve_obj.double(curve_obj.double(G2))) + assert not curve_obj.eq(curve_obj.double(G2), G2) + assert curve_obj.eq(curve_obj.add(curve_obj.multiply(G2, 9), curve_obj.multiply(G2, 5)), curve_obj.add(curve_obj.multiply(G2, 12), curve_obj.multiply(G2, 2))) + assert curve_obj.is_inf(curve_obj.multiply(G2, curve_obj.curve_order)) + assert not curve_obj.is_inf(curve_obj.multiply(G2, 2 * field_modulus - curve_obj.curve_order)) + assert curve_obj.is_on_curve(curve_obj.multiply(G2, 9), curve_obj.b2) + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + Optimized_BN128_Curve(), + BLS12_381_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_G12_object(curve_obj): + G12 = curve_obj.G12 + assert curve_obj.eq(curve_obj.add(curve_obj.add(curve_obj.double(G12), G12), G12), curve_obj.double(curve_obj.double(G12))) + assert not curve_obj.eq(curve_obj.double(G12), G12) + assert curve_obj.eq(curve_obj.add(curve_obj.multiply(G12, 9), curve_obj.multiply(G12, 5)), curve_obj.add(curve_obj.multiply(G12, 12), curve_obj.multiply(G12, 2))) + assert curve_obj.is_on_curve(curve_obj.multiply(G12, 9), curve_obj.b12) + assert curve_obj.is_inf(curve_obj.multiply(G12, curve_obj.curve_order)) diff --git a/tests/test_field_elements.py b/tests/test_field_elements.py new file mode 100644 index 00000000..aca2832c --- /dev/null +++ b/tests/test_field_elements.py @@ -0,0 +1,68 @@ +import pytest + +from py_ecc import ( + field_elements, + optimized_field_elements, +) +from py_ecc.field_properties import ( + field_properties, +) + + +# Tests both field_elements and optimized_field_elements +@pytest.fixture(params=[field_elements, optimized_field_elements]) +def lib(request): + return request.param + + +@pytest.fixture +def FQ(lib): + return lib.FQ + + +@pytest.fixture +def FQ2(lib): + return lib.FQ2 + + +@pytest.fixture +def FQ12(lib): + return lib.FQ12 + + +def test_FQ_object(FQ): + for curve_name in ("bn128", "bls12_381"): + field_modulus = field_properties[curve_name]["field_modulus"] + assert FQ(2, curve_name) * FQ(2, curve_name) == FQ(4, curve_name) + assert FQ(2, curve_name) / FQ(7, curve_name) + FQ(9, curve_name) / FQ(7, curve_name) == FQ(11, curve_name) / FQ(7, curve_name) + assert FQ(2, curve_name) * FQ(7, curve_name) + FQ(9, curve_name) * FQ(7, curve_name) == FQ(11, curve_name) * FQ(7, curve_name) + assert FQ(9, curve_name) ** field_modulus == FQ(9, curve_name) + + +def test_FQ2_object(FQ2): + for curve_name in ("bn128", "bls12_381"): + field_modulus = field_properties[curve_name]["field_modulus"] + x = FQ2([1, 0], curve_name) + f = FQ2([1, 2], curve_name) + fpx = FQ2([2, 2], curve_name) + one = FQ2.one(curve_name) + assert x + f == fpx + assert f / f == one + assert one / f + x / f == (one + x) / f + assert one * f + x * f == (one + x) * f + assert x ** (field_modulus ** 2 - 1) == one + + +def test_FQ12_object(FQ12): + for curve_name in ("bn128", "bls12_381"): + field_modulus = field_properties[curve_name]["field_modulus"] + x = FQ12([1] + [0] * 11, curve_name) + f = FQ12([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], curve_name) + fpx = FQ12([2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], curve_name) + one = FQ12.one(curve_name) + assert x + f == fpx + assert f / f == one + assert one / f + x / f == (one + x) / f + assert one * f + x * f == (one + x) * f + # This check takes too long + # assert x ** (field_modulus ** 12 - 1) == one diff --git a/tests/test_pairing.py b/tests/test_pairing.py new file mode 100644 index 00000000..f82fb89d --- /dev/null +++ b/tests/test_pairing.py @@ -0,0 +1,190 @@ +import pytest + +from py_ecc.bls12_381_curve import ( + BLS12_381_Curve, + Optimized_BLS12_381_Curve, +) +from py_ecc.bn128_curve import ( + BN128_Curve, + Optimized_BN128_Curve, +) + +from py_ecc.field_elements import ( + FQ, + FQ2, + FQ12, +) +from py_ecc.optimized_field_elements import ( + FQ as optimized_FQ, + FQ2 as optimized_FQ2, + FQ12 as optimized_FQ12, +) + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + BLS12_381_Curve(), + ), +) +def test_linefunc_consistency_object_plain_curves(curve_obj): + one, two, three = curve_obj.G1, curve_obj.double(curve_obj.G1), curve_obj.multiply(curve_obj.G1, 3) + negone, negtwo, negthree = ( + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 1), + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 2), + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 3), + ) + + assert curve_obj.linefunc(one, two, one) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, two) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, three) != FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, negthree) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, one) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, negone) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, two) != FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, one) == FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, two) != FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, negtwo) == FQ(0, curve_obj.curve_name) + + +@pytest.mark.parametrize( + 'curve_obj', + ( + Optimized_BN128_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_linefunc_consistency_object_optimized_curves(curve_obj): + one, two, three = curve_obj.G1, curve_obj.double(curve_obj.G1), curve_obj.multiply(curve_obj.G1, 3) + negone, negtwo, negthree = ( + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 1), + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 2), + curve_obj.multiply(curve_obj.G1, curve_obj.curve_order - 3), + ) + + assert curve_obj.linefunc(one, two, one)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, two)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, three)[0] != optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, two, negthree)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, one)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, negone)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, negone, two)[0] != optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, one)[0] == optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, two)[0] != optimized_FQ(0, curve_obj.curve_name) + assert curve_obj.linefunc(one, one, negtwo)[0] == optimized_FQ(0, curve_obj.curve_name) + + +@pytest.mark.parametrize( + 'curve_obj,FQ12', + ( + (BN128_Curve(), FQ12), + (Optimized_BN128_Curve(), optimized_FQ12), + (BLS12_381_Curve(), FQ12), + (Optimized_BLS12_381_Curve(), optimized_FQ12), + ), +) +def test_pairing_negative_G1(curve_obj, FQ12): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + pn1 = curve_obj.pairing(curve_obj.G2, curve_obj.neg(curve_obj.G1)) + + assert p1 * pn1 == FQ12.one(curve_obj.curve_name) + + +@pytest.mark.parametrize( + 'curve_obj,FQ12', + ( + (BN128_Curve(), FQ12), + (Optimized_BN128_Curve(), optimized_FQ12), + (BLS12_381_Curve(), FQ12), + (Optimized_BLS12_381_Curve(), optimized_FQ12), + ), +) +def test_pairing_negative_G2(curve_obj, FQ12): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + pn1 = curve_obj.pairing(curve_obj.G2, curve_obj.neg(curve_obj.G1)) + np1 = curve_obj.pairing(curve_obj.neg(curve_obj.G2), curve_obj.G1) + + assert p1 * np1 == FQ12.one(curve_obj.curve_name) + assert pn1 == np1 + + +@pytest.mark.parametrize( + 'curve_obj,FQ12', + ( + (BN128_Curve(), FQ12), + (Optimized_BN128_Curve(), optimized_FQ12), + (BLS12_381_Curve(), FQ12), + (Optimized_BLS12_381_Curve(), optimized_FQ12), + ), +) +def test_pairing_output_order(curve_obj, FQ12): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + + assert p1 ** curve_obj.curve_order == FQ12.one(curve_obj.curve_name) + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + Optimized_BN128_Curve(), + BLS12_381_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_pairing_bilinearity_on_G1(curve_obj): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + p2 = curve_obj.pairing(curve_obj.G2, curve_obj.multiply(curve_obj.G1, 2)) + np1 = curve_obj.pairing(curve_obj.neg(curve_obj.G2), curve_obj.G1) + + assert p1 * p1 == p2 + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + Optimized_BN128_Curve(), + BLS12_381_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_pairing_is_non_degenerate(curve_obj): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + p2 = curve_obj.pairing(curve_obj.G2, curve_obj.multiply(curve_obj.G1, 2)) + np1 = curve_obj.pairing(curve_obj.neg(curve_obj.G2), curve_obj.G1) + + assert p1 != p2 and p1 != np1 and p2 != np1 + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + Optimized_BN128_Curve(), + BLS12_381_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_pairing_bilinearity_on_G2(curve_obj): + p1 = curve_obj.pairing(curve_obj.G2, curve_obj.G1) + po2 = curve_obj.pairing(curve_obj.multiply(curve_obj.G2, 2), curve_obj.G1) + + assert p1 * p1 == po2 + + +@pytest.mark.parametrize( + 'curve_obj', + ( + BN128_Curve(), + Optimized_BN128_Curve(), + BLS12_381_Curve(), + Optimized_BLS12_381_Curve(), + ), +) +def test_pairing_composit_check(curve_obj): + p3 = curve_obj.pairing(curve_obj.multiply(curve_obj.G2, 27), curve_obj.multiply(curve_obj.G1, 37)) + po3 = curve_obj.pairing(curve_obj.G2, curve_obj.multiply(curve_obj.G1, 999)) + + assert p3 == po3 diff --git a/tests/test_secp256k1.py b/tests/test_secp256k1.py index a56bba1b..b24e0856 100644 --- a/tests/test_secp256k1.py +++ b/tests/test_secp256k1.py @@ -1,6 +1,12 @@ -from py_ecc.secp256k1 import privtopub, ecdsa_raw_sign, ecdsa_raw_recover import binascii +from py_ecc.secp256k1 import ( + ecdsa_raw_recover, + ecdsa_raw_sign, + privtopub, +) + + priv = binascii.unhexlify('792eca682b890b31356247f2b04662bff448b6bb19ea1c8ab48da222c894ef9b') pub = (20033694065814990006010338153307081985267967222430278129327181081381512401190, 72089573118161052907088366229362685603474623289048716349537937839432544970413) diff --git a/tox.ini b/tox.ini index 1b3f3474..71ba30b2 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,4 @@ basepython = basepython=python extras=lint commands= - flake8 {toxinidir}/py_ecc - mypy --strict --follow-imports=silent --ignore-missing-imports --no-strict-optional -p py_ecc.bn128 - mypy --strict --follow-imports=silent --ignore-missing-imports --no-strict-optional -p py_ecc.optimized_bn128 - mypy --strict --follow-imports=silent --ignore-missing-imports --no-strict-optional -p py_ecc.secp256k1 + flake8 {toxinidir}/py_ecc {toxinidir}/tests