-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschnorr.py
More file actions
111 lines (74 loc) · 1.75 KB
/
schnorr.py
File metadata and controls
111 lines (74 loc) · 1.75 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
from hashlib import sha256
from random import randint
from Crypto.Util import number
import json
"""
TODO:
"""
def hashThis(r, M):
hash=sha256()
hash.update(str(r).encode())
hash.update(M.encode())
return int(hash.hexdigest(),16)
def produceKeys(q,g):
# generator g , initially it was 2
#g = 10
## Key generation:
#Private signing key x
# x <- Secret Key
x = randint(1,q-1)
# calculate public verification key y
y = pow(g, x, q)
return x,y,g,q
def signTransaction(x,y,g,q,recipient = 123, amount = 10):
# M is message/ transactuion to sign
k = randint(1, q - 1)
r = pow(g, k, q)
M = {
"sender": y,
"recipient": recipient,
"amount": amount,
}
#print(M,type(M))
M = json.dumps(M)
#print(M,type(M))
e = hashThis(r, M) % q # part 1 of signature
s = (k - (x * e)) % (q-1) # part 2 of signature
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))
return M
def verifySigner(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.pop("sender")
M = json.dumps(M)
#print(M,type(M))
rv = (pow(g, s, q) * pow (y, e, q)) % q
ev = hashThis(rv, M) % q
# e should equal ev
#print(str(e))
#print(str(ev))
#print("in verifySigner")
if str(e) == str(ev):
return True
else:
return False
"""
x,y,g,q = produceKeys()
M = signTransaction(x,y,g,q)
print(verifySigner(M))
"""