-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschnorrtest.py
More file actions
66 lines (47 loc) · 1.35 KB
/
schnorrtest.py
File metadata and controls
66 lines (47 loc) · 1.35 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
from hashlib import sha256
from random import randint
from Crypto.Util import number
def hashThis(r, M):
hash=sha256()
hash.update(str(r).encode())
hash.update(M.encode())
return int(hash.hexdigest(),16)
def produceKeys():
## Notation
# generator g
g = 10
# Prime q (for educational purpose I use explicitly a small prime number - for cryptographic purposes this would have to be much larger)
q = number.getPrime(70)
## Key generation
#Private signing key x
#x = 32991
# 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,g,q):
#M = "This is the message"
M = {
'sender': "bake1",
'recipient': "bake2",
'amount': 10,
}
M = str(M)
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
return s,e,M
def verifySigner(g,q,s,e,M):
rv = (pow(g, s, q) * pow (y, e, q)) % q
ev = hashThis(rv, M) % q
#print ("e " + str(e) + " should equal ev " + str(ev))
# e should equal ev
if str(e) == str(ev):
return "Author Verified!"
else:
return "Nope"
x,y,g,q = produceKeys()
s,e,M = signTransaction(x,g,q)
print(verifySigner(g,q,s,e,M))