Skip to content

Instantly share code, notes, and snippets.

@HarryR
Last active July 25, 2018 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryR/eb153a5555cc240f217956fc400282d4 to your computer and use it in GitHub Desktop.
Save HarryR/eb153a5555cc240f217956fc400282d4 to your computer and use it in GitHub Desktop.
Implementation of Schnorr signatures over secp256k1 - warning, this is vulnerable to the Related Key Attack...
from __future__ import print_function
from random import randint
from hashlib import sha256
from py_ecc.secp256k1.secp256k1 import add, multiply, inv, N, P, G
bytes_to_int = lambda x: reduce(lambda o, b: (o << 8) + ord(b), [0] + list(x))
rands = lambda: randint(1, N - 1)
sbmul = lambda s: multiply(G, s)
hashs = lambda *x: bytes_to_int(sha256('.'.join(['%X' for _ in range(0, len(x))]) % x).digest()) % N
hashp = lambda *x: hashs(*[item for sublist in x for item in sublist])
invmul = lambda x, y: (x * pow(y, P-2, P))
mulmod = lambda x, y: (x * y) % N
submod = lambda x, y: (x - y) % N
negp = lambda x: (x[0], -x[1])
# Our secret and public keys
x = rands()
xG = sbmul(x)
# Message to sign
m = rands()
# Create signature
k = rands()
kG = sbmul(k)
e = hashs(hashp(xG, kG), m)
s = submod(k, mulmod(x, e))
# Verify signature
sG = sbmul(s)
exG = multiply(xG, e)
kGv = add(sG, exG)
ev = hashs(hashp(xG, kGv), m)
print(ev == e)
print(sG == add(kGv, negp(exG)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment