Repository navigation
Expand file tree
/
Copy pathutil.py
More file actions
62 lines (54 loc) · 1.47 KB
/
Copy pathutil.py
File metadata and controls
62 lines (54 loc) · 1.47 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
from time import time
from math import floor
from random import randint
import numpy as np
start = None
def stat(msg):
global start
now = time()
if start is None:
start = now
print("\x1B[2m%10.4f %s\x1B[0m" % (now-start, msg))
def powmod(a, b, m):
""" Returns the power a**b % m """
# a^(2b) = (a^b)^2
# a^(2b+1) = a * (a^b)^2
if b==0:
return 1
return ((a if b%2==1 else 1) * powmod(a, b//2, m)**2) % m
def is_prime(p):
""" Returns whether p is probably prime """
for null in range(16):
a = randint(1,p-1)
if powmod(a,p-1,p) != 1:
return False
return True
def gen_prime(b):
""" Returns a prime p with b bits """
p = randint(2**(b-1), 2**b)
while not is_prime(p):
p = randint(2**(b-1), 2**b)
return p
def generateSophieGermainPrime(k):
""" Return a Sophie Germain prime p with k bits """
p = gen_prime(k-1)
sp = 2*p + 1
while not is_prime(sp):
p = gen_prime(k-1)
sp = 2*p + 1
return p
def generateSafePrime(k):
""" Return a safe prime p with k bits """
p = gen_prime(k-1)
sp = 2*p + 1
while not is_prime(sp):
p = gen_prime(k-1)
sp = 2*p + 1
return sp
def text2array(txt):
ary = []
for row in txt.split('\n'):
if row.strip() != '':
row = row.replace('[', '').replace(']', '').strip()
ary.append([int(x) for x in row.split()])
return np.array(ary, dtype=np.int64)