-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
72 lines (60 loc) · 1.46 KB
/
functions.py
File metadata and controls
72 lines (60 loc) · 1.46 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
import hashlib
import math
import random
from primitives import *
def mgf1(message, length, hash_used=hashlib.sha3_512):
counter = 0
output = ''
while len(output) < length:
c = i2osp(counter, 4)
output += hash_used(message + c).digest()
counter += 1
return output[:length]
def mod_inverse(a, m):
m0 = m
y = 0
x = 1
while a > 1:
# q is quotient
q = a // m
t = m
m = a % m
a = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if x < 0:
x = x + m0
return x
def lcm(x, y):
"""This function takes two
integers and returns the L.C.M."""
least_common_multiple = (x * y) // math.gcd(x, y)
return least_common_multiple
def is_prime(n):
"""
Miller-Rabin primality test.
A return value of False means n is certainly not prime. A return value of
True means n is very likely a prime.
"""
# Miller-Rabin test for prime
s = 0
d = n - 1
while d % 2 == 0:
d >>= 1
s += 1
assert (2 ** s * d == n - 1)
def trial_composite(num):
if pow(num, d, n) == 1:
return False
for i in range(s):
if pow(num, 2 ** i * d, n) == n - 1:
return False
return True
for _ in range(8): # number of trials
a = random.randrange(2, n)
if trial_composite(a):
return False
return True