示例#1
0
def test_value() -> None:
    grp = ZqGroup(mpz(53))
    assert grp(10) + grp(20) == grp(30)
    assert 20 + grp(10) == grp(30)
    assert grp(40) + grp(50) == grp(37)
    assert grp(40) + 50 == grp(37)
    assert ZqGroup(mpz(53))(20) != grp(20)

    with pytest.raises(TypeError, match="Group mismatch"):
        grp(10) + ZqGroup(mpz(53))(10)

    with pytest.raises(TypeError):
        grp(10) + cast(int, "twenty")

    assert -grp(10) == grp(43)
    assert grp(10) * grp(22) == grp(8)
    assert 22 * grp(10) == grp(8)
    with pytest.raises(TypeError, match="Group mismatch"):
        grp(10) * ZqGroup(mpz(53))(10)
    with pytest.raises(TypeError):
        grp(10) * cast(int, "twentytwo")

    # 1 = 16 * 10 - 3 * 53
    assert 16 == grp(10).inv
    assert repr(grp(10)) == "ZqGroup(53)(10)"
    assert grp(10).asn1.dump() == b"\x02\x01\x0a"
示例#2
0
def test_init() -> None:
    with pytest.raises(ValueError, match="q is negative"):
        ZqGroup(mpz(-13))

    with pytest.raises(ValueError, match="q not prime"):
        ZqGroup(mpz(12))

    with pytest.raises(ValueError, match=r"2q \+ 1 not prime"):
        ZqGroup(mpz(13))
示例#3
0
def test_value_pow() -> None:
    grp = QrGroup(mpz(23))
    assert grp(13) ** -1 == grp(16)
    assert grp(13) ** ZqGroup(mpz(11))(5) == grp(4)
    assert ((grp(13) ** Fraction(3, 4)) ** Fraction(4, 1)) ** Fraction(1, 3) == grp(13)

    with pytest.raises(TypeError, match="incompatible groups"):
        grp(13) ** ZqGroup(mpz(29))(16)

    with pytest.raises(Exception, match="Modulo not supported"):
        pow(cast(int, grp(13)), -1, 123)

    with pytest.raises(TypeError):
        grp(13) ** cast(int, "two")
示例#4
0
def test_call() -> None:
    grp = ZqGroup(mpz(53))
    assert grp(20) == 20
    assert grp(200) == 41
    assert grp(PreGroupValue(20)) == 20

    with pytest.raises(ValueError, match="Not a valid group element"):
        grp(PreGroupValue(200))

    with pytest.raises(TypeError):
        grp(cast(int, "twenty"))
示例#5
0
def test_poly() -> None:
    grp = ZqGroup(mpz(11))

    # p(x) = 2 + 7x + 0x² + 1x³
    p = Poly([grp(2), grp(7), grp(0), grp(1)], grp(0))

    assert len(p) == 4
    assert p[0] == grp(2)
    assert p[2] == grp(0)

    assert p(5) == grp(2 + 35 + 0 + 125)

    assert repr(
        p
    ) == "Poly([ZqGroup(11)(2), ZqGroup(11)(7), ZqGroup(11)(0), ZqGroup(11)(1)])"
示例#6
0
def test_repr() -> None:
    assert repr(ZqGroup(mpz(53))) == "ZqGroup(53)"
示例#7
0
def test_rand_nonzero() -> None:
    tmp = {int(ZqGroup(mpz(2)).rand_nonzero) for __ in range(100)}
    assert tmp == {1}
示例#8
0
def test_rand() -> None:
    tmp = {int(ZqGroup(mpz(2)).rand) for __ in range(100)}
    assert 0 <= min(tmp) <= max(tmp) <= 1
示例#9
0
def test_len() -> None:
    assert ZqGroup(mpz(53)).len == 53