-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShenScheme.py
158 lines (117 loc) · 4.53 KB
/
ShenScheme.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
import math, time, binascii
import myToolBox
import head
class ShenScheme:
def __init__(self, taskNum, blockNum, segNum, userParam):
self._taskNum = taskNum
self._blockNum = blockNum
self._segmentNum = segNum
self._blockSize = self._segmentNum * 20
self._tags = []
self.verInfos = []
self.group = head.group
self.g = head.g
self._x = userParam['x']
self.v = self.g ** self._x
self.u = userParam['u']
self.fileName = 'shen' + userParam['fileName']
def TagGen(self):
self.fnameSig = self.group.hash(self.fileName, G1) ** self._x
self.verInfos = [head.verInfos[i] for i in range(self._blockNum)]
signatures = list()
for i in range(self._blockNum):
f = open(head.testDatafilePath, 'rb')
f.seek(self._taskNum + i * self._blockSize, 0)
blockBytes = f.read(self._blockSize)
f.close()
mi = self.group.init(ZR, 0)
for j in range(self._segmentNum):
mij = blockBytes[20 * j:20 * (j + 1)]
mij = int(binascii.hexlify(mij), 16)
mij = self.group.init(ZR, mij)
mi += mij
sig = self.group.hash(self.verInfos[i], G1) * self.u ** mi
sig = sig ** self._x
signatures.append(sig)
self._tags = signatures
def TagGen4Update(self):
self.verInfos = [head.verInfos[i] for i in range(self._blockNum)]
signatures = list()
for i in range(self._blockNum):
f = open(head.testDatafilePath, 'rb')
f.seek(self._taskNum + i * self._blockSize, 0)
blockBytes = f.read(self._blockSize)
f.close()
mi = self.group.init(ZR, 0)
for j in range(self._segmentNum):
mij = blockBytes[20 * j:20 * (j + 1)]
mij = int(binascii.hexlify(mij), 16)
mij = self.group.init(ZR, mij)
mi += mij
sig = self.group.hash(self.verInfos[i], G1) * self.u ** mi
sig = sig ** self._x
signatures.append(sig)
self._tags = signatures
def Challenge(self, CNum):
assert CNum > 0
assert CNum < self._blockNum
chal = dict()
selectBlockSet = myToolBox.getUniqueRandomNum(self._blockNum, CNum)
chal = {i : self.group.random(ZR) for i in selectBlockSet}
return chal
def Proof(self, chal):
assert isinstance(chal, dict)
proof = dict()
M = self.group.init(ZR, 0)
T = self.group.init(G1, 1)
for i in chal.keys():
f = open(head.testDatafilePath, 'rb')
f.seek(self._taskNum + i * self._blockSize, 0)
blockBytes = f.read(self._blockSize)
f.close()
mi = self.group.init(ZR, 0)
for j in range(self._segmentNum):
mij = blockBytes[20*j:20*(j+1)]
mij = int(binascii.hexlify(mij), 16)
mij = self.group.init(ZR, mij)
mi += mij
M += mi * chal[i]
T *= self._tags[i] ** chal[i]
proof['T'] = T
proof['M'] = M
return proof
def Verify(self, chal, proof):
assert isinstance(proof, dict)
hname = self.group.hash(self.fileName, G1)
l = self.group.pair_prod(hname, self.v)
r = self.group.pair_prod(self.fnameSig, self.g)
assert l == r
DI = self.group.init(GT, 1)
for i in chal.keys():
hwi = self.group.hash(self.verInfos[i], G1)
DI *= self.group.pair_prod(hwi, self.v) ** chal[i]
lhs = self.group.pair_prod(proof['T'], self.g)
rhs = DI * self.group.pair_prod(self.u ** proof['M'], self.v)
return lhs == rhs
if __name__ == '__main__':
t0 = time.perf_counter()
testCase = ShenScheme(0, 10, 50, head.userParam)
t1 = time.perf_counter()
print('Initialize time :', (t1 - t0) * 1000, 'ms')
t0 = time.perf_counter()
testCase.TagGen()
t1 = time.perf_counter()
print('TagGen time :', (t1 - t0) * 1000, 'ms')
t0 = time.perf_counter()
chal = testCase.Challenge(4)
t1 = time.perf_counter()
print('Challenge time :', (t1 - t0) * 1000, 'ms')
t0 = time.perf_counter()
proof = testCase.Proof(chal)
t1 = time.perf_counter()
print('Proof time :', (t1 - t0) * 1000, 'ms')
t0 = time.perf_counter()
result = testCase.Verify(chal, proof)
t1 = time.perf_counter()
print('Verification time :', (t1 - t0) * 1000, 'ms')
print(result)