aboutsummaryrefslogtreecommitdiff
path: root/test/ec/test_mod.py
blob: 7234816f06624c0cf01a8ed747d1eefe8d98f8d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import warnings

import pytest
from sympy import FF, symbols

from pyecsca.ec.mod import (
    Mod,
    gcd,
    extgcd,
    Undefined,
    miller_rabin,
    has_gmp,
    RawMod,
    SymbolicMod,
    jacobi,
)
from pyecsca.ec.error import (
    NonInvertibleError,
    NonResidueError,
    NonInvertibleWarning,
    NonResidueWarning,
)
from pyecsca.misc.cfg import getconfig, TemporaryConfig


def test_gcd():
    assert gcd(15, 20) == 5
    assert extgcd(15, 0) == (1, 0, 15)
    assert extgcd(15, 20) == (-1, 1, 5)


def test_jacobi():
    assert jacobi(5, 1153486465415345646578465454655646543248656451) == 1
    assert jacobi(564786456646845, 46874698564153465453246546545456849797895547657) == -1
    assert jacobi(564786456646845, 46874698564153465453246546545456849797895) == 0


def test_miller_rabin():
    assert miller_rabin(2)
    assert miller_rabin(3)
    assert miller_rabin(5)
    assert not miller_rabin(8)
    assert miller_rabin(0xE807561107CCF8FA82AF74FD492543A918CA2E9C13750233A9)
    assert not miller_rabin(0x6F6889DEB08DA211927370810F026EB4C17B17755F72EA005)


def test_inverse():
    p = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF
    assert Mod(
        0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p
    ).inverse() == Mod(0x1CB2E5274BBA085C4CA88EEDE75AE77949E7A410C80368376E97AB22EB590F9D, p)
    with pytest.raises(NonInvertibleError):
        Mod(0, p).inverse()
    with pytest.raises(NonInvertibleError):
        Mod(5, 10).inverse()
    getconfig().ec.no_inverse_action = "warning"
    with warnings.catch_warnings(record=True) as w:
        Mod(0, p).inverse()
        assert issubclass(w[0].category, NonInvertibleWarning)
    with warnings.catch_warnings(record=True) as w:
        Mod(5, 10).inverse()
        assert issubclass(w[0].category, NonInvertibleWarning)
    getconfig().ec.no_inverse_action = "ignore"
    Mod(0, p).inverse()
    Mod(5, 10).inverse()
    getconfig().ec.no_inverse_action = "error"


def test_is_residue():
    assert Mod(4, 11).is_residue()
    assert not Mod(11, 31).is_residue()
    assert Mod(0, 7).is_residue()
    assert Mod(1, 2).is_residue()


def test_bit_length():
    x = Mod(3, 5)
    assert x.bit_length() == 2


def test_sqrt():
    p = 0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF
    assert Mod(
        0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC, p
    ).sqrt() in (
               0x9ADD512515B70D9EC471151C1DEC46625CD18B37BDE7CA7FB2C8B31D7033599D,
               0x6522AED9EA48F2623B8EEAE3E213B99DA32E74C9421835804D374CE28FCCA662,
           )
    with pytest.raises(NonResidueError):
        Mod(
            0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p
        ).sqrt()
    getconfig().ec.non_residue_action = "warning"
    with warnings.catch_warnings(record=True) as w:
        Mod(
            0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p
        ).sqrt()
        assert issubclass(w[0].category, NonResidueWarning)
    getconfig().ec.non_residue_action = "ignore"
    Mod(
        0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1, p
    ).sqrt()
    with TemporaryConfig() as cfg:
        cfg.ec.non_residue_action = "warning"
        with warnings.catch_warnings(record=True) as w:
            Mod(
                0x702BDAFD3C1C837B23A1CB196ED7F9FADB333C5CFE4A462BE32ADCD67BFB6AC1,
                p,
            ).sqrt()
            assert issubclass(w[0].category, NonResidueWarning)
    assert Mod(0, p).sqrt() == Mod(0, p)
    q = 0x75D44FEE9A71841AE8403C0C251FBAD
    assert Mod(0x591E0DB18CF1BD81A11B2985A821EB3, q).sqrt() in \
           (0x113B41A1A2B73F636E73BE3F9A3716E, 0x64990E4CF7BA44B779CC7DCC8AE8A3F)
    getconfig().ec.non_residue_action = "error"


def test_eq():
    assert Mod(1, 7) == 1
    assert Mod(1, 7) != "1"
    assert Mod(1, 7) == Mod(1, 7)
    assert Mod(1, 7) != Mod(5, 7)
    assert Mod(1, 7) != Mod(1, 5)


def test_pow():
    a = Mod(5, 7)
    assert a ** (-1) == a.inverse()
    assert a ** 0 == Mod(1, 7)
    assert a ** (-2) == a.inverse() ** 2


def test_wrong_mod():
    a = Mod(5, 7)
    b = Mod(4, 11)
    with pytest.raises(ValueError):
        a + b


def test_wrong_pow():
    a = Mod(5, 7)
    c = Mod(4, 11)
    with pytest.raises(TypeError):
        a ** c


def test_other():
    a = Mod(5, 7)
    b = Mod(3, 7)
    assert int(-a) == 2
    assert str(a) == "5"
    assert 6 - a == Mod(1, 7)
    assert a != b
    assert a / b == Mod(4, 7)
    assert a // b == Mod(4, 7)
    assert 5 / b == Mod(4, 7)
    assert 5 // b == Mod(4, 7)
    assert a / 3 == Mod(4, 7)
    assert a // 3 == Mod(4, 7)
    assert divmod(a, b) == (Mod(1, 7), Mod(2, 7))
    assert a + b == Mod(1, 7)
    assert 5 + b == Mod(1, 7)
    assert a + 3 == Mod(1, 7)
    assert a != 6
    assert hash(a) is not None


def test_undefined():
    u = Undefined()
    for k, meth in u.__class__.__dict__.items():
        if k in (
                "__module__",
                "__new__",
                "__init__",
                "__doc__",
                "__hash__",
                "__abstractmethods__",
                "_abc_impl",
                "__slots__",
                "x",
                "n"
        ):
            continue
        args = [5 for _ in range(meth.__code__.co_argcount - 1)]
        if k == "__repr__":
            assert meth(u) == "Undefined"
        elif k in ("__eq__", "__ne__"):
            assert not meth(u, *args)
        else:
            try:
                res = meth(u, *args)
                assert res == NotImplemented
            except NotImplementedError:
                pass


def test_implementation():
    if not has_gmp:
        pytest.skip("Only makes sense if more Mod implementations are available.")
    with TemporaryConfig() as cfg:
        cfg.ec.mod_implementation = "python"
        assert isinstance(Mod(5, 7), RawMod)


def test_symbolic():
    x = symbols("x")
    p = 13
    k = FF(p)
    sx = SymbolicMod(x, p)
    a = k(3)
    b = k(5)
    r = sx * a + b
    assert isinstance(r, SymbolicMod)
    assert r.n == p