예제 #1
0
def test_bilinear_math():
    from honeybadgermpc.betterpairing import ZR, G1, G2, GT, pair

    a = G1.rand()
    b = G2.rand()
    c = pair(a, b)
    i = ZR.random()
    assert pair(a**i, b) == c**i
    assert pair(a, b**i) == c**i
    assert pair(a**i, b**i) == c**(i**2)
    a.preprocess(8)
    b.preprocess(3)
    c.preprocess(5)
    i = ZR.random()
    assert pair(a**i, b) == c**i
    assert pair(a, b**i) == c**i
    assert pair(a**i, b**i) == c**(i**2)
    a **= ZR.random()
    b **= ZR.random()
    c **= ZR.random()
    a2 = G1.rand()
    assert a / a2 == a * a2**-1
    b2 = G2.rand()
    assert b / b2 == b * b2**-1
    c2 = GT.rand()
    assert c / c2 == c * c2**-1
    assert (a**-1)**-1 == a
    assert (b**-1)**-1 == b
    assert (c**-1)**-1 == c
예제 #2
0
def test_serialization():
    from honeybadgermpc.betterpairing import ZR, G1, G2, GT

    a = ZR.random()
    b = G1.rand()
    c = G2.rand()
    d = GT.rand()
    assert a == ZR(a.__getstate__())
    # assert b == G1(b.__getstate__())
    assert c == G2(c.__getstate__())
    assert d == GT(d.__getstate__())

    bb = G1()
    bb.__setstate__(b.__getstate__())
    assert bb == b
예제 #3
0
def test_hashing():
    from honeybadgermpc.betterpairing import ZR, G1, G2
    import pickle

    crs = G1.hash_many(b"honeybadger", 10) + G2.hash_many(b"honeybadger", 2)
    assert crs[0] != crs[1]
    assert type(crs[0]) is G1
    assert type(crs[11]) is G2
    assert len(crs) == 12
    c = ZR.hash(pickle.dumps(crs))
    assert type(c) is ZR
    c2 = ZR.hash(pickle.dumps(crs))
    assert c == c2
    g = G1.hash(pickle.dumps(crs))
    g2 = G1.hash(pickle.dumps(crs))
    ghat = G2.hash(pickle.dumps(crs))
    ghat2 = G2.hash(pickle.dumps(crs))
    assert g == g2
    assert ghat == ghat2
예제 #4
0
def gen_pc_const_crs(t, alpha=None, g=None, h=None, ghat=None):
    nonetype = type(None)
    assert type(t) is int
    assert type(alpha) in (ZR, int, nonetype)
    assert type(g) in (G1, nonetype)
    assert type(h) in (G1, nonetype)
    assert type(ghat) in (G2, nonetype)
    if alpha is None:
        alpha = ZR.random(0)
    if g is None:
        g = G1.rand([0, 0, 0, 1])
    if h is None:
        h = G1.rand([0, 0, 0, 1])
    if ghat is None:
        ghat = G2.rand([0, 0, 0, 1])
    (gs, ghats, hs) = ([], [], [])
    for i in range(t + 1):
        gs.append(g**(alpha**i))
    for i in range(2):
        ghats.append(ghat**(alpha**i))
    for i in range(t + 1):
        hs.append(h**(alpha**i))
    crs = [gs, ghats, hs]
    return crs