Skip to content

Commit

Permalink
Expand test suites.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent Girod committed Apr 7, 2020
1 parent 71ddb6f commit 923c97f
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 13 deletions.
6 changes: 3 additions & 3 deletions petrelic/petlib/pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
25 changes: 22 additions & 3 deletions tests/test_additive_pairing.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -12,21 +21,29 @@ 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')
assert isinstance(h2, G1Element)
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')
Expand Down Expand Up @@ -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

Expand Down
25 changes: 20 additions & 5 deletions tests/test_bn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/test_multiplicative_pairing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from petrelic.multiplicative.pairing import (
BilinearGroupPair,
G1,
G1Element,
G2,
Expand All @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
55 changes: 54 additions & 1 deletion tests/test_pairing.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down
31 changes: 30 additions & 1 deletion tests/test_petlib_pairing.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
Expand Down

0 comments on commit 923c97f

Please sign in to comment.