diff --git a/petrelic/petlib/pairing.py b/petrelic/petlib/pairing.py index 86f1a01..fbb8a30 100644 --- a/petrelic/petlib/pairing.py +++ b/petrelic/petlib/pairing.py @@ -16,9 +16,9 @@ class BilinearGroupPair: """ def __init__(self): - self.GT = GTGroup(self) - self.G1 = G1Group(self) - self.G2 = G2Group(self) + self.GT = GTGroup() + self.G1 = G1Group() + self.G2 = G2Group() def groups(self): """ diff --git a/tests/test_additive_pairing.py b/tests/test_additive_pairing.py index 55bac3c..57614bf 100644 --- a/tests/test_additive_pairing.py +++ b/tests/test_additive_pairing.py @@ -1,6 +1,15 @@ import pytest -from petrelic.additive.pairing import G1, G1Element, G2, G2Element, GT, GTElement, NoAffineCoordinateForECPoint +from petrelic.additive.pairing import ( + BilinearGroupPair, + G1, + G1Element, + G2, + G2Element, + GT, + GTElement, + NoAffineCoordinateForECPoint +) from petrelic.bn import Bn @@ -12,13 +21,21 @@ def group(request): def element(request): return request.param +def test_bgp(): + bgp = BilinearGroupPair() + groups = bgp.groups() + + assert isinstance(groups[0], G1) + assert isinstance(groups[1], G2) + assert isinstance(groups[2], GT) + def test_is_valid(group): assert group.generator().is_valid() assert (100 * group.generator()).is_valid() -def test_hash_to_point_G1(group): +def test_hash_to_point_G1(): h1 = G1.hash_to_point(b'foo') assert isinstance(h1, G1Element) h2 = G1.hash_to_point(b'bar') @@ -26,7 +43,7 @@ def test_hash_to_point_G1(group): assert h1 != h2 -def test_hash_to_point_G2(group): +def test_hash_to_point_G2(): h1 = G2.hash_to_point(b'foo') assert isinstance(h1, G2Element) h2 = G2.hash_to_point(b'bar') @@ -66,6 +83,8 @@ def test_ec_arithmetic(group): neutral_element = group.neutral_element() + assert group.infinity() == neutral_element + assert g + neutral_element == g assert neutral_element + neutral_element == neutral_element diff --git a/tests/test_bn.py b/tests/test_bn.py index db9bc8b..7abbd02 100644 --- a/tests/test_bn.py +++ b/tests/test_bn.py @@ -5,6 +5,14 @@ def test_bn_constructors(): + assert Bn.from_num(100) == 100 + assert Bn.from_num(-100) == -100 + + assert Bn.from_num(Bn(100)) == 100 + assert Bn.from_num(Bn(-100)) == -100 + + assert Bn.from_num("100") == NotImplemented + assert Bn.from_decimal("100") == 100 assert Bn.from_decimal("-100") == -100 @@ -53,11 +61,12 @@ def test_bn_large_negative_integer(): def test_bn_prime(): - p = Bn.get_prime(128) - assert p > Bn(0) - assert p.is_prime() - assert not Bn(16).is_prime() - assert p.num_bits() > 127 + for safe in (0, 1): + p = Bn.get_prime(128, safe=safe) + assert p > Bn(0) + assert p.is_prime() + assert not Bn(16).is_prime() + assert p.num_bits() > 127 def test_bn_arithmetic(): @@ -95,6 +104,12 @@ def test_bn_arithmetic(): assert pow(Bn(2), Bn(8), Bn(27)) == Bn(2 ** 8 % 27) pow(Bn(10), Bn(10)).binary() + with pytest.raises(Exception): + pow(Bn(10), -1) + + assert pow(Bn(2), -1, 27) == 14 + + assert pow(Bn(2), 0, 27) == 1 assert pow(Bn(2), 8, 27) == 2 ** 8 % 27 diff --git a/tests/test_multiplicative_pairing.py b/tests/test_multiplicative_pairing.py index b6dcae5..e74b4c5 100644 --- a/tests/test_multiplicative_pairing.py +++ b/tests/test_multiplicative_pairing.py @@ -1,6 +1,7 @@ import pytest from petrelic.multiplicative.pairing import ( + BilinearGroupPair, G1, G1Element, G2, @@ -27,6 +28,15 @@ def element(request): return request.param +def test_bgp(): + bgp = BilinearGroupPair() + groups = bgp.groups() + + assert isinstance(groups[0], G1) + assert isinstance(groups[1], G2) + assert isinstance(groups[2], GT) + + def test_order(group): g = group.generator() o = group.order() @@ -101,6 +111,7 @@ def test_mul_pow(group): def test_pow_identity(group): g = group.generator() assert group.neutral_element() == g ** 0 + assert group.unity() == group.neutral_element() def test_pow_inverse(group): diff --git a/tests/test_pairing.py b/tests/test_pairing.py index ba99f26..f554d96 100644 --- a/tests/test_pairing.py +++ b/tests/test_pairing.py @@ -1,4 +1,15 @@ -from petrelic.native.pairing import G1, G1Element, G2, G2Element, GT, GTElement, NoAffineCoordinateForECPoint +import copy + +from petrelic.native.pairing import ( + BilinearGroupPair, + G1, + G1Element, + G2, + G2Element, + GT, + GTElement, + NoAffineCoordinateForECPoint +) from petrelic.bn import Bn import pytest @@ -12,15 +23,45 @@ def group(request): def element(request): return request.param + +def test_bgp(): + bgp = BilinearGroupPair() + groups = bgp.groups() + + assert isinstance(groups[0], G1) + assert isinstance(groups[1], G2) + assert isinstance(groups[2], GT) + + def test_is_valid(group): assert group.generator().is_valid() assert (100 * group.generator()).is_valid() def test_is_valid_gt(): + assert GT.unity().is_valid() assert GT.generator().is_valid() assert (GT.generator() ** 100).is_valid() +def test_copy(group): + elem = 42 * group.generator() + elem_copy = copy.copy(elem) + + assert elem == elem_copy + + elem += group.generator() + assert elem != elem_copy + + +def test_copy_gt(): + elem = GT.generator() ** 42 + elem_copy = copy.copy(elem) + + assert elem == elem_copy + + elem *= GT.generator() + assert elem != elem_copy + def test_hash_to_point_G1(group): h1 = G1.hash_to_point(b'foo') @@ -49,6 +90,8 @@ def test_ec_from_x(group): def test_ec_arithmetic(group): g = group.generator() + assert not g == 5 + assert g != 5 assert g + g == g + g assert g + g == g.double() assert g + g == Bn(2) * g @@ -60,6 +103,14 @@ def test_ec_arithmetic(group): d[2 * g] = 2 assert d[2 * g] == 2 + q = group.generator() + q *= 10 + + assert q == g * 10 + + q *= 10 + assert q == g * 10 * 10 + # Test long names assert (g + g).eq(g + g) assert g + g == g.add(g) @@ -71,6 +122,8 @@ def test_ec_arithmetic(group): def test_gt_multiplication(): g = GT.generator() + assert not g == 5 + assert g != 5 assert g * g == g * g assert g * g == g.square() assert g * g == g ** Bn(2) diff --git a/tests/test_petlib_pairing.py b/tests/test_petlib_pairing.py index ab4978f..a17da12 100644 --- a/tests/test_petlib_pairing.py +++ b/tests/test_petlib_pairing.py @@ -1,4 +1,14 @@ -from petrelic.petlib.pairing import G1Group, G1Elem, G2Group, G2Elem, GTGroup, GTElem +import copy + +from petrelic.petlib.pairing import ( + BilinearGroupPair, + G1Group, + G1Elem, + G2Group, + G2Elem, + GTGroup, + GTElem +) from petrelic.bn import Bn import pytest @@ -12,6 +22,25 @@ def group(request): return G2Group() +def test_bgp(): + bgp = BilinearGroupPair() + groups = bgp.groups() + + assert isinstance(groups[0], G1Group) + assert isinstance(groups[1], G2Group) + assert isinstance(groups[2], GTGroup) + + +def test_copy(group): + elem = 42 * group.generator() + elem_copy = copy.copy(elem) + + assert elem == elem_copy + + elem += group.generator() + assert elem != elem_copy + + def test_check_point(group): assert group.check_point(group.generator()) assert group.check_point(100 * group.generator())