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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
import secrets
from functools import wraps
from public import public
from .context import Action
@public
def gcd(a, b):
"""Euclid's greatest common denominator algorithm."""
if abs(a) < abs(b):
return gcd(b, a)
while abs(b) > 0:
q, r = divmod(a, b)
a, b = b, r
return a
@public
def extgcd(a, b):
"""Extended Euclid's greatest common denominator algorithm."""
if abs(b) > abs(a):
(x, y, d) = extgcd(b, a)
return y, x, d
if abs(b) == 0:
return 1, 0, a
x1, x2, y1, y2 = 0, 1, 1, 0
while abs(b) > 0:
q, r = divmod(a, b)
x = x2 - q * x1
y = y2 - q * y1
a, b, x2, x1, y2, y1 = b, r, x1, x, y1, y
return x2, y2, a
def check(func):
@wraps(func)
def method(self, other):
if type(self) is not type(other):
other = self.__class__(other, self.n)
else:
if self.n != other.n:
raise ValueError
return func(self, other)
return method
@public
class RandomModAction(Action):
"""A random sampling from Z_n."""
order: int
def __init__(self, order: int):
super().__init__()
self.order = order
def __repr__(self):
return f"{self.__class__.__name__}({self.order:x})"
@public
class Mod(object):
"""An element x of ℤₙ."""
def __init__(self, x: int, n: int):
self.x: int = x % n
self.n: int = n
@check
def __add__(self, other):
return Mod((self.x + other.x) % self.n, self.n)
@check
def __radd__(self, other):
return self + other
@check
def __sub__(self, other):
return Mod((self.x - other.x) % self.n, self.n)
@check
def __rsub__(self, other):
return -self + other
def __neg__(self):
return Mod(self.n - self.x, self.n)
def inverse(self):
x, y, d = extgcd(self.x, self.n)
return Mod(x, self.n)
def __invert__(self):
return self.inverse()
@check
def __mul__(self, other):
return Mod((self.x * other.x) % self.n, self.n)
@check
def __rmul__(self, other):
return self * other
@check
def __truediv__(self, other):
return self * ~other
@check
def __rtruediv__(self, other):
return ~self * other
@check
def __floordiv__(self, other):
return self * ~other
@check
def __rfloordiv__(self, other):
return ~self * other
@check
def __div__(self, other):
return self.__floordiv__(other)
@check
def __rdiv__(self, other):
return self.__rfloordiv__(other)
@check
def __divmod__(self, divisor):
q, r = divmod(self.x, divisor.x)
return Mod(q, self.n), Mod(r, self.n)
def __bytes__(self):
return self.x.to_bytes((self.n.bit_length() + 7) // 8, byteorder="big")
@staticmethod
def random(n: int):
with RandomModAction(n):
return Mod(secrets.randbelow(n), n)
def __int__(self):
return self.x
def __eq__(self, other):
if type(other) is int:
return self.x == (other % self.n)
if type(other) is not Mod:
return False
return self.x == other.x and self.n == other.n
def __ne__(self, other):
return not self == other
def __repr__(self):
return str(self.x)
def __pow__(self, n):
if type(n) is not int:
raise TypeError
q = self
r = self if n & 1 else Mod(1, self.n)
i = 2
while i <= n:
q = (q * q)
if n & i == i:
r = (q * r)
i = i << 1
return r
@public
class Undefined(Mod):
def __init__(self):
object.__init__(self)
def __add__(self, other):
raise NotImplementedError
def __radd__(self, other):
raise NotImplementedError
def __sub__(self, other):
raise NotImplementedError
def __rsub__(self, other):
raise NotImplementedError
def __neg__(self):
raise NotImplementedError
def __invert__(self):
raise NotImplementedError
def __mul__(self, other):
raise NotImplementedError
def __rmul__(self, other):
raise NotImplementedError
def __truediv__(self, other):
raise NotImplementedError
def __rtruediv__(self, other):
raise NotImplementedError
def __floordiv__(self, other):
raise NotImplementedError
def __rfloordiv__(self, other):
raise NotImplementedError
def __div__(self, other):
raise NotImplementedError
def __rdiv__(self, other):
raise NotImplementedError
def __divmod__(self, divisor):
raise NotImplementedError
def __bytes__(self):
raise NotImplementedError
def __int__(self):
raise NotImplementedError
def __eq__(self, other):
return False
def __ne__(self, other):
return False
def __repr__(self):
return "Undefined"
def __pow__(self, n):
raise NotImplementedError
|