-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
165 lines (142 loc) · 4.56 KB
/
test.py
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
#coding=utf-8
#python 2.7.15
from math import sqrt
checkList = {}
with open("./cmake-build-debug/rsa_test.pk") as f:
for line in f.readlines():
tmp = line.split(":")
checkList[tmp[0]] = int(tmp[1].strip())
with open("./cmake-build-debug/rsa_test.sk") as f:
checkList['d'] = int(f.read().strip())
if __name__ == "__main__":
print checkList
# N = (2p'+1)(2q'+1)
assert ((2*checkList["p'"]+1) * (2*checkList["q'"]+1) == checkList['N']), "(2p'+1) * (2q'+1) != N"
# p'q' in [2^(k-1), 2^k-1]
assert ((checkList["p'"]) * (checkList["q'"]) >= pow(2, checkList["k"]-1)) and ((checkList["p'"]) * (checkList["q'"]) <= (pow(2, checkList["k"])-1)), "2^(k-1):%d<= p'*q':%d<=2^k-1:%d"%(pow(2, checkList["k"]-1), checkList["p'"]*checkList["q'"], pow(2, checkList["k"])-1)
# e > 2^k
assert (checkList["e"] > pow(2, checkList['k'])), "e<=2^k"
# ed = 1 mod p'q'
assert ((checkList['e']*checkList['d']) % (checkList["p'"]*checkList["q'"]) == 1), "ed:%d != 1(mod p'*q':%d)"%(checkList['e']*checkList['d'], checkList["p'"]*checkList["q'"])
g1 = checkList['g1']
g2 = checkList['g2']
e = checkList['e']
N = checkList['N']
for i in range(2, N):
if N % i == 0:
print i
i = 2
record = {1:1}
for i in range(1, N):
record[i*i%N] = 1
print i, len(record)
# cyclic group check
i = 1
pi = g1
while True:
if (pi % N == 1):
break
assert (pi % N != 0), "g1^%d devide p'"%i
pi = pi %N
#print i
#a = record[pi]
#assert (pi not in record), "g1^%d: not in QRn"%i
pi = (pi*g1) % N
i += 1
assert checkList["p'"]*checkList["q'"] == i, "order is p'q'"
i = 1
pi = g2
while True:
if (pi % N == 1):
break
assert (pi % N != 0), "g2^%d devide p'"%i
#print record[pi]
#assert (pi%N) not in record, "g2^%d not in QRn"%i
pi = (pi*g2) % N
i += 1
assert checkList["p'"]*checkList["q'"] == i, "order is p'q'"
with open("./cmake-build-debug/random.in") as f:
for line in f.readlines():
tmp = line.split(":")
checkList[tmp[0]] = int(tmp[1].strip())
with open("./cmake-build-debug/randoma.in") as f:
for line in f.readlines():
tmp = line.split(":")
checkList[tmp[0]] = int(tmp[1].strip())
with open("./cmake-build-debug/randomp.in") as f:
for line in f.readlines():
tmp = line.split(":")
checkList[tmp[0]] = int(tmp[1].strip())
with open("./cmake-build-debug/paraminfo.in") as f:
for line in f.readlines():
tmp = line.split(":")
checkList[tmp[0]] = tmp[1].rstrip().split(" ")
print tmp[1]
for i in range(len(checkList[tmp[0]])):
checkList[tmp[0]][i] = int(checkList[tmp[0]][i].strip())
t, u, sigma = checkList['t'], checkList['u'], checkList['sigma']
a = checkList['a']
z1, z2 = checkList['z1'], checkList['z2']
p = checkList['p']
file = checkList['file']
r = checkList['r']
tags = checkList['tags']
names = checkList['names']
coeff = checkList['coeff']
pp = checkList["p'"]
qp = checkList["q'"]
R = checkList['R']
print checkList
tmp = 1
for i in range(z1):
tmp = tmp * g1 % N
tmpa = tmp
tmp = 1
cnt = z2 * e
i = 0
while i < cnt:
tmp = tmp * g2 % N
i+= 1
tmpa *= tmp
tmpa = tmpa % N
assert tmpa == a
# a correct, sigma correct,
assert z2+R*p == sigma
for i in range(len(file)):
tmp = 1
tmp = tmp * r[i] % N
tmp = tmp *pow(g1, file[i], N)
tmp = pow(tmp, checkList['d'], N)
# print tmp, tags[i]
assert tmp == tags[i]
# tag correct
tmp = 1
for i in range(len(r)):
flag =False
for j in range(pp*qp):
if (tmp == r[i]):
flag = True
break
tmp = tmp * g1 % N
assert flag
# r correct in QRn
tmp = pow(g2, p, N)
for i in range(len(tags)):
tmp = tmp*pow(tags[i], coeff[i], N) % N# should be c[i], where is 1 now
assert tmp == t
# t correct
tmp = 0
for i in range(len(file)):
tmp = coeff[i] * file[i]# 1 is c[i]
tmp = tmp * R # 1 is R
tmp = (tmp + z1)
assert tmp == u
# u correct
tmp = pow(t, e*R, N) # 1 is R
tmp = a*tmp % N
print (tmp)
tmp = pow(g1, u, N)
tmp = tmp * pow(g2, e*sigma, N) % N
for i in range(len(r)):
tmp = tmp*pow(r[i], coeff[i]*R, N) % N
print (tmp)