import sympy from random import randint import hashlib def genSchnorr(bits = 1024, subbits=256): q = sympy.randprime(2**subbits, 2**(subbits+1)) while True: t = randint(2**(bits-subbits), 2**(bits-subbits+1)-1) p = t*q + 1 if sympy.isprime(p): break while True: h = randint(2**subbits, 2**(subbits+1)-1) g = pow(h, t, p) if g != 1: break x = randint(2, 2**subbits-1) y = pow(g, x, p) return (p, q, g, x, y) def sha256int(m): H = hashlib.sha256(m.encode()).hexdigest() return int(H, 16) def sign(p, q, g, x, m): k = randint(1, q-1) r = sha256int(m + str(pow(g, k, p))) s = (k + x*r) % q return r, s def verify(p, q, g, y, m, r, s): w = pow(g, s, p) * pow(y, -r, p) % p return sha256int(m + str(w)) == r def shadysign(p, q, g, x, m): k = (sha256int(str(x)) + randint(1, 2**18)) % q r = sha256int(m + str(pow(g, k, p))) s = (k + x*r) % q return r, s def genHW(): p, q, g, x, y = genSchnorr() shadysigs = [] for m in range(2**10): r, s = shadysign(p, q, g, x, str(m)) shadysigs.append((r,s)) return ############################################################################### p, q, g, x, y = genSchnorr() m = 'Experimental text to sign!' r, s = sign(p, q, g, x, m) print(r,s) print(verify(p, q, g, y, m, r, s)) m = 'Experimental text to sign!' r, s = shadysign(p, q, g, x, m) print(r,s) print(verify(p, q, g, y, m, r, s))