-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodMath.py
More file actions
310 lines (272 loc) · 9.15 KB
/
Copy pathmodMath.py
File metadata and controls
310 lines (272 loc) · 9.15 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from math import floor
from math import gcd
from LoopChain import methodLoop
import Fraction as fr
import FactNum as fn
def modInverse(num: int, modulus: int):
"""
Finds the inverse in mod n of a number.
:param num: number to find the inverse of
:param modulus: mod to find the inverse in
:return: num^-1 (mod modulus); None if invalid input
"""
if gcd(num, modulus) != 1 or num == 0 or modulus == 0:
return None
elif num == 1:
return 1
else:
currMod = modulus
currNum = num
currRem = currMod % currNum
equationList = [[currMod, currNum, floor(currMod / currNum), currRem]]
while currRem != 1:
currMod = currNum
currNum = currRem
currRem = currMod % currNum
equationList += [[currMod, currNum, floor(currMod / currNum), currRem]]
if len(equationList) > 1:
currCoeff = fr.Fraction(1, equationList[-1][2])
currCon = fr.Fraction(-equationList[-1][3], equationList[-1][2])
# currVar = equationList[-1][0]
for r in reversed(equationList[1:-1]):
tempCoeff = currCoeff + r[2]
tempCon = currCon
# currVar = r[1]
currCoeff = 1 / tempCoeff
currCon = -tempCon / tempCoeff
currCoeff += equationList[0][2]
# currVar = equationList[0][1]
return int(-currCoeff / currCon)
else:
return -floor(equationList[0][0] / equationList[0][1])
def loopModInverse(num: int, modulus: int):
"""
Finds the inverse in mod n of a number using the digit loop method.
:param num: number to find the inverse of
:param modulus: mod to find the inverse in
:return: num^-1 (mod modulus)
"""
if gcd(num, modulus) != 1:
return None
elif num == 1:
return 1
else:
digitList = methodLoop(num, lambda x: x * num % modulus)
if 1 in digitList:
return digitList[digitList.index(1) - 1]
else:
return None
def isPowerOf(num1: int, num2: int) -> bool:
x = num1
y = num2
if x == 0 or y == 0 or x == 1 or y == 1:
return False
while x % y == 0:
x = x / y
return x == 1
def powerOf(num1: int, num2: int):
x = num1
y = num2
ret = 0
while x % y == 0:
x = x / y
ret += 1
return ret
def totient(inputNum: int):
"""
Finds the totient of a positive integer.
:param inputNum: positive integer to find the totient of
:return: totient of the positive integer; none if invalid input
"""
if type(inputNum) is int or isinstance(inputNum, fn.FactNum):
facNum = fn.FactNum(abs(inputNum))
if facNum == 0:
return 0
elif facNum == 1:
return 1
else:
tot = 1
for p, n in facNum.factors.items():
tot *= (p ** (n - 1)) * (p - 1)
return tot
else:
return None
def simpleTotient(inputPrime: int, inputPower: int):
return (inputPrime ** (inputPower - 1)) * (inputPrime - 1)
def reducedTotient(inputNum: int):
"""
Finds the reduced totient of a positive integer.
:param inputNum: positive integer to find the reduced totient of
:return: reduced totient of the positive integer; none if invalid input
"""
if type(inputNum) is int or isinstance(inputNum, fn.FactNum):
facNum = fn.FactNum(abs(inputNum))
if facNum == 0:
return 0
elif facNum == 1:
return 1
else:
tot = 1
totList = []
for p, n in facNum.factors.items():
if p == 2 and n > 2:
tot *= int((p ** (n - 1)) * (p - 1) * (1 / 2))
totList += [int((p ** (n - 1)) * (p - 1) * (1 / 2))]
else:
tot *= ((p ** (n - 1)) * (p - 1))
totList += [((p ** (n - 1)) * (p - 1))]
if len(totList) == 1:
return tot
totDen = totList[0]
for i in range(1, len(totList)):
totDen = gcd(totDen, totList[i])
tot = int(tot / totDen)
return tot
else:
return None
def chiRemTheSolveDict(modDict: dict) -> int:
"""
Uses the chinese remainder theorem to find the solution to a set of congruencies. The congruencies must be in the
form of a dict with the key being the modulus and the value being the remainder at that modulus.
:param modDict: dict of the congruencies with key = modulus and value = x % modulus
:return: solution to the set of congruencies
"""
retValue = 0
retMod = 1
for modulus, value in modDict.items():
# print("modulus: " + str(modulus) + " value: " + str(value))
retMod *= modulus
currM = 1
for m2, v2 in modDict.items():
if m2 != modulus:
currM *= m2
if value != 0:
# print("value: " + str(value) + " currM: " + str(currM) + " modulus: " + str(modulus))
retValue += value * currM * modInverse(currM, modulus)
retValue = retValue % retMod
return retValue
def loopModPow(base: int, exponent: int, modulus: int):
if exponent == 0:
return 1
elif exponent > 0:
redExp = exponent % reducedTotient(modulus)
retVal = 1
for i in range(redExp):
retVal = (retVal * base) % modulus
return retVal
else:
ibase = modInverse(base, modulus)
if ibase is None:
return None
else:
redExp = (-exponent) % reducedTotient(modulus)
retVal = 1
for i in range(redExp):
retVal = (retVal * ibase) % modulus
return retVal
def isPrimRoot(num: int, modulus: int) -> bool:
if len(methodLoop(num, lambda x: x * num % modulus)) == modulus - 1:
return True
else:
return False
def discLogBrute(num: int, base: int, modulus: int):
"""
for an equation of the form b = a^x (mod p), where b = num, a = base, and p = modulus
:param num:
:param base:
:param modulus:
:return:
"""
redBase = base % modulus
redNum = num % modulus
currBase = 1
# print(reducedTotient(modulus))
# print("redBase: " + str(redBase))
# print("redNum: " + str(redNum))
if redNum == 1:
return 0
elif isPowerOf(redNum, redBase):
return powerOf(redNum, redBase)
elif not isPrimRoot(redBase, modulus):
return None
for i in range(reducedTotient(modulus)):
if currBase == redNum:
return i
else:
currBase = (currBase * redBase) % modulus
# print("currBase: " + str(currBase))
# print("i: " + str(i))
return None
def discLogComp(num: int, base: int, modulus: int, compBase: int, compPower: int):
"""
for an equation of the form b = a^x (mod p), where b = num, a = base, and p = modulus
:param num:
:param base:
:param modulus:
:param compBase:
:param compPower:
:return:
"""
xList = []
for i in range(1, compPower + 1):
currPow = 0
for x in xList:
currPow -= x
# print("base: " + str(base) + " currPow: " + str(currPow) + " modulus: " + str(modulus))
currNum = loopModPow(((num * loopModPow(base, currPow, modulus)) % modulus),
int((modulus - 1) / (compBase ** i)),
modulus)
for j in range(compBase):
currRes = loopModPow(base, int(j * (modulus - 1) / compBase), modulus)
if currRes == currNum:
xList += [j]
break
retVal = 0
for i in range(len(xList)):
retVal += xList[i] * (compBase ** i)
return retVal
def discLog(num: int, base: int, modulus: int):
redBase = base % modulus
redNum = num % modulus
if redNum == 1:
return 0
elif isPowerOf(redNum, redBase):
return powerOf(redNum, redBase)
if not isPrimRoot(redBase, modulus):
return None
factMod = fn.FactNum(modulus - 1)
congDict = {}
for p, n in factMod.factors.items():
congDict[(p ** n)] = discLogComp(redNum, redBase, modulus, p, n)
# print("dict: " + str(congDict))
# print("for equation: " + str(num) + " = " + str(base) + "^x (mod " + str(modulus) + ")")
retVal = chiRemTheSolveDict(congDict)
if retVal == 0 and redBase != 1:
return None
else:
return retVal
def loopSigPow(base: int, exponent: int, modulus: int):
if exponent == 0:
return 1
elif exponent > 0:
retVal = 1
currPow = 1
for i in range(exponent):
currPow = (currPow * base) % modulus
retVal += currPow
return retVal % modulus
else:
return None
def loopSigPowLen(base: int, exponent: int, modulus: int, stopVal: int = 1):
if exponent > 0:
retVal = 1
currPow = 1
for i in range(exponent):
currPow = (currPow * base) % modulus
retVal += currPow
retVal %= modulus
if retVal == stopVal:
return i+1
return None
else:
return None