예제 #1
0
class TestECPoint:
    @pytest.fixture(params=[(MPI(1), MPI(2), MPI(3)), (1, 2, 3)])
    def xyz(self, request):
        return request.param

    @pytest.fixture
    def point(self, xyz):
        return ECPoint(*xyz)

    @pytest.mark.parametrize("repr_", (repr, str), ids=lambda f: f.__name__)
    def test_repr(self, repr_, point):
        assert isinstance(repr_(point), str)

    def test_hash(self, point):
        assert isinstance(hash(point), int)

    def test_accessors(self, point, xyz):
        x, y, z = xyz
        assert point.x == x
        assert point.y == y
        assert point.z == z

    def test_eq_point(self, point, xyz):
        assert (point == ECPoint(*xyz)) is True
        assert (point == ECPoint(0, 0, 0)) is False

    def test_eq_zero(self):
        zero = ECPoint(0, 0, 0)
        assert (zero == 1) is False
        assert (zero == 0) is True
        assert (zero == ECPoint(0, 0, 0)) is True

    def test_bool(self, point):
        assert bool(point) is True
        assert bool(ECPoint(0, 0, 0)) is False
예제 #2
0
def test_or():
    assert MPI(12) | MPI(12) == 12
    assert MPI(12) | MPI(3) == 15
    assert MPI(15) | MPI(4) == 15
    assert MPI(15) | 4 == 15
    with pytest.raises(TypeError):
        assert 15 | MPI(4) == 15
예제 #3
0
def test_xor():
    assert MPI(12) ^ MPI(12) == 0
    assert MPI(12) ^ MPI(3) == 15
    assert MPI(15) ^ MPI(4) == 11
    assert MPI(15) ^ 4 == 11
    with pytest.raises(TypeError):
        assert 15 ^ MPI(4) == 11
예제 #4
0
def test_and():
    assert MPI(12) & MPI(12) == 12
    assert MPI(12) & MPI(3) == 0
    assert MPI(15) & MPI(4) == 4
    assert MPI(15) & 4 == 4
    with pytest.raises(TypeError):
        assert 15 & MPI(4) == 4
예제 #5
0
class TestECPoint:
    @pytest.fixture(params=[(MPI(1), MPI(2), MPI(3)), (1, 2, 3)])
    def xyz(self, request):
        return request.param

    @pytest.fixture
    def point(self, xyz):
        return ECPoint(*xyz)

    def test_accessors(self, point, xyz):
        x, y, z = xyz
        assert point.x == x
        assert point.y == y
        assert point.z == z

    def test_str(self, point):
        assert str(point) == "ECPoint(1, 2, 3)"

    def test_repr(self, point):
        assert repr(point) == "ECPoint(MPI(1), MPI(2), MPI(3))"

    def test_eq_point(self, point, xyz):
        assert (point == ECPoint(*xyz)) is True
        assert (point == ECPoint(0, 0, 0)) is False

    def test_eq_zero(self):
        zero = ECPoint(0, 0, 0)
        assert (zero == 1) is False
        assert (zero == 0) is True
        assert (zero == ECPoint(0, 0, 0)) is True

    def test_hash(self, point):
        zero = ECPoint(0, 0, 0)
        assert hash(zero) == hash(zero)
        assert hash(point) == hash(point)
        assert hash(zero) != hash(point)

    def test_bool(self, point):
        assert bool(point) is True
        assert bool(ECPoint(0, 0, 0)) is False
예제 #6
0
def test_lt_larger_number_is_true():
    assert (MPI(12) < MPI(42)) is True
    assert (MPI(12) < 42) is True
    assert (12 < MPI(42)) is True
예제 #7
0
def test_neq_same_numbers_is_false():
    assert (MPI(12) != MPI(12)) is False
    assert (MPI(12) != 12) is False
    assert (12 != MPI(12)) is False
예제 #8
0
def test_eq_same_number_is_true():
    assert (MPI(12) == MPI(12)) is True
    assert (MPI(12) == 12) is True
    assert (12 == MPI(12)) is True
예제 #9
0
def test_mul():
    assert MPI(12) * MPI(2) == 24
    assert MPI(12) * 2 == 24
    assert 12 * MPI(2) == 24
예제 #10
0
def test_add():
    assert MPI(12) + MPI(12) == 24
    assert MPI(12) + 12 == 24
    assert 12 + MPI(12) == 24
예제 #11
0
def test_is_integral():
    assert isinstance(MPI(42), numbers.Integral)
예제 #12
0
def test_lshift():
    assert MPI(12) << MPI(2) == 48
    assert MPI(12) << 2 == 48
    with pytest.raises(TypeError):
        assert 12 << MPI(2) == 48
예제 #13
0
def test_rshift():
    assert MPI(12) >> MPI(2) == 3
    assert MPI(12) >> 2 == 3
    with pytest.raises(TypeError):
        assert 12 >> MPI(2) == 3
예제 #14
0
def test_float():
    assert float(MPI(12)) == 12.0
예제 #15
0
def test_bool():
    assert bool(MPI(0)) is False
예제 #16
0
def test_to_bytes_overflow():
    value = unhexlify(b"DEEADBEEFF")
    mpi = MPI.from_bytes(value, byteorder="big")
    with pytest.raises(OverflowError):
        mpi.to_bytes(2, byteorder="big")
예제 #17
0
def test_hash():
    assert isinstance(hash(MPI(1337)), int)
예제 #18
0
def test_repr(repr_):
    assert isinstance(repr_(MPI(69)), str)
예제 #19
0
def test_prime():
    assert MPI.prime(512).is_prime()
예제 #20
0
def test_floordiv():
    assert MPI(24) // MPI(2) == 12
    assert MPI(24) // 2 == 12
    assert 24 // MPI(2) == 12
예제 #21
0
def test_sub():
    assert MPI(12) - MPI(5) == 7
    assert MPI(12) - 5 == 7
    assert 12 - MPI(5) == 7
예제 #22
0
def test_mod():
    assert MPI(12) % MPI(10) == 2
    assert MPI(12) % 10 == 2
    assert 12 % MPI(10) == 2
예제 #23
0
def test_pow():
    assert MPI(12).__pow__(5, 12 ** 5 + 1) == 248832
    assert pow(MPI(12), 5, 12 ** 5 + 1) == 248832
    assert MPI(12).__pow__(5, 7) == 3
    assert pow(MPI(12), 5, 7) == 3
예제 #24
0
def test_bit_length(value):
    mpi = MPI(value)
    assert mpi == value
    assert mpi.bit_length() == value.bit_length()
예제 #25
0
def test_eq_different_numbers_is_false():
    assert (MPI(12) == MPI(42)) is False
    assert (MPI(12) == 42) is False
    assert (12 == MPI(42)) is False
예제 #26
0
def test_from_empty_bytes():
    value = b""
    big = MPI.from_bytes(value, byteorder="big")
    little = MPI.from_bytes(value, byteorder="little")
    assert big == little == 0
    assert big.bit_length() == little.bit_length() == 0
예제 #27
0
def test_neq_different_numbers_is_true():
    assert (MPI(12) != MPI(42)) is True
    assert (MPI(12) != 42) is True
    assert (12 != MPI(42)) is True
예제 #28
0
def test_from_bytes():
    value = unhexlify(b"DEADBEEF")
    mpi = MPI.from_bytes(value, byteorder="big")
    assert mpi.to_bytes(4, byteorder="big") == unhexlify(b"DEADBEEF")
    assert mpi.to_bytes(4, byteorder="little") == unhexlify(b"EFBEADDE")
    assert mpi == int(hexlify(value), 16)
예제 #29
0
def test_lt_smaller_number_is_false():
    assert (MPI(42) < MPI(12)) is False
    assert (MPI(42) < 12) is False
    assert (42 < MPI(12)) is False
예제 #30
0
def test_pickle():
    value = MPI(1337)
    assert value == pickle.loads(pickle.dumps(value))