-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecdsatx.py
More file actions
285 lines (167 loc) · 4.39 KB
/
Copy pathecdsatx.py
File metadata and controls
285 lines (167 loc) · 4.39 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
from node.schnorr import verifySigner
import ecdsa
import json
import ast
from hashlib import sha256
import time
def produceKeys():
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.get_verifying_key()
return sk,vk
def signTx(sk,vk):
print(vk.to_pem().decode(),sk.to_pem().decode())
#print(vk)
vk = vk.to_pem().decode()
m = {
"sender": vk,
"recipient": 123456789101110293732647,
"amount": 10,
}
m = json.dumps(m).encode('utf-8')
sig = sk.sign(m)
# Turn Sig into json serializable
sig = sig.decode(errors='ignore')
m = json.loads(m.decode('utf-8'))
m["sign"] = sig
m = json.dumps(m)
#print(m)
return m
def verifySign(m):
m = json.loads(m)
vk = m["sender"]
vk = vk.encode()
sig = m["sign"]
sig = sig.encode()
m.pop("sign")
m = json.dumps(m, indent=2).encode('utf-8')
vk = ecdsa.VerifyingKey.from_string(vk, curve=ecdsa.SECP256k1, hashfunc=sha256) # the default is sha1
return vk.verify(sig, m)
"""
sk, vk = produceKeys()
m = signTx(sk,vk)
print(verifySign(m))
"""
def doEcdsa():
l_time1 = []
l_time2 = []
l_time3 = []
for i in range(100):
t1 = time.time()
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.get_verifying_key()
t2 = time.time()
l_time1.append(t2-t1)
m = b'{"sender":sender,"recipient":sender,"amount:amount}'
t1 = time.time()
sig = sk.sign(m)
t2 = time.time()
l_time2.append(t2-t1)
t1 = time.time()
assert vk.verify(sig,m)
t2 = time.time()
l_time3.append(t2-t1)
print("time for producing key:", l_time1)
print("time for signing:", l_time2)
print("time for verifying:", l_time3)
return l_time1,l_time2,l_time3
def doSchnorr():
l_time1 = []
l_time2 = []
l_time3 = []
from hashlib import sha256
from random import randint
from Crypto.Util import number
import json
def hashThis(r, M):
hash=sha256()
hash.update(str(r).encode())
hash.update(M.encode())
return int(hash.hexdigest(),16)
for i in range(100):
g = 10
q = number.getPrime(70)
t1 = time.time()
x = randint(1,q-1)
y = pow(g, x, q)
t2 = time.time()
l_time1.append(t2-t1)
recipient = 123
amount = 10
# M is message/ transactuion to sign
M = {
"sender": y,
"recipient": recipient,
"amount": amount,
}
#print(M,type(M))
M = json.dumps(M)
#print(M,type(M))
t1 = time.time()
k = randint(1, q - 1)
r = pow(g, k, q)
e = hashThis(r, M) % q # part 1 of signature
s = (k - (x * e)) % (q-1) # part 2 of signature
t2 = time.time()
l_time2.append(t2-t1)
M = json.loads(M)
M["sign1"] = s
M["sign2"] = e
M["gen"] = g
M["prime"] = q
#print(M,type(M))
M = json.dumps(M)
#print(M,type(M))
M = json.loads(M)
s = M["sign1"]
M.pop("sign1")
e = M["sign2"]
M.pop("sign2")
g = M["gen"]
M.pop("gen")
q = M["prime"]
M.pop("prime")
y = M["sender"]
M = json.dumps(M)
t1 = time.time()
rv = (pow(g, s, q) * pow (y, e, q)) % q
ev = hashThis(rv, M) % q
t2 = time.time()
assert str(e) == str(ev)
l_time3.append(t2-t1)
print("time for producing key:", l_time1)
print("time for signing:", l_time2)
print("time for verifying:", l_time3)
return l_time1,l_time2,l_time3
print("For Schnorr: ")
s1,s2,s3 = doSchnorr()
print("For Ecdsa: ")
e1,e2,e3 = doEcdsa()
from matplotlib import pyplot as plt
import numpy as np
difference = []
zip_object = zip(s1,e1)
for s, e in zip_object:
difference.append(s-e)
print("1 difference:",difference)
difference = []
zip_object = zip(s2,e2)
for s, e in zip_object:
difference.append(s-e)
print("2 difference:",difference)
difference = []
zip_object = zip(s3,e3)
for s, e in zip_object:
difference.append(s-e)
print("3 difference:",difference)
plt.figure(figsize=(12, 3))
plt.subplot(131)
plt.plot(s1, label="schnorr sign", ls="-")
plt.plot(e1, label="ecdsa sign", ls="-")
plt.subplot(132)
plt.plot(s2, label="schnorr signing", ls="--")
plt.plot(e2, label="ecdsa signing", ls="--")
plt.subplot(133)
plt.plot(s3, label="schnorr verification", ls="-.")
plt.plot(e3, label="ecdsa verification", ls="-.")
plt.show()
plt.savefig("matplotlib.png") #savefig, don't show