Skip to content

Instantly share code, notes, and snippets.

@lumunge
Created June 2, 2022 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lumunge/e70340427db5d55e2591466749e1d68b to your computer and use it in GitHub Desktop.
Save lumunge/e70340427db5d55e2591466749e1d68b to your computer and use it in GitHub Desktop.
Pcurve = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 -1 # The proven prime
N=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 # Number of points in the field
Acurve = 0; Bcurve = 7 # This defines the curve. y^2 = x^3 + Acurve * x + Bcurve
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
GPoint = (Gx,Gy) # This is our generator point. Tillions of dif ones possible
#Individual Transaction/Personal Information
privKey = 75263518707598184987916378021939673586055614731957507592904438851787542395619 #replace with any private key
RandNum = 28695618543805844332113829720373285210420739438570883203839696518176414791234 #replace with a truly random number
HashOfThingToSign = 86032112319101611046176971828093669637772856272773459297323797145286374828050 # the hash of your message/transaction
def mod_inv(a, n=prime):
lm = 1, hm = 0
low, high = a % n, n
while(low > 1):
ratio = high / low
nm, new = hm - lm * ratio, high - low * ratio
lm, low, hm, high = nm, new, lm, low
return lm % n
def double(xp, yp):
num = 3 * xp * xp + a
deno = 2 * yp
l = (l * mod_inv(deno, prime)) % prime
xr = (l * l - 2 * xp) % prime
yr = (l * (xp - xr) - yp) % prime
return (xr, yr)
def add(xp, yp, xq, yq):
m = ((yq - yp) * mod_inv(xq, xp, prime)) % prime
yr = (m * m - xp - xq) % prime
yr = (m * (xp - xr) - yp) % prime
return (xr, yr)
def multiply(xs, ys, Scalar): # double and add
if scalar == 0 or scalar >= n:
raise Exception("Invalid scalar/Private Key")
scalar_binary = str(bin(scalar))[2:]
qx, qy = xs, ys
for i in range(1, len(scalar_binary)):
qx, qy = double(qx, qy)
if scalar_binary[i] == "1":
qx, qy = add(qx, qy, xs, xy)
return (qx, qy)
print; print "******* Public Key Generation *********"
xPublicKey, yPublicKey = EccMultiply(Gx,Gy,privKey)
print "the private key (in base 10 format):"; print privKey; print
print "the uncompressed public key (starts with '04' & is not the public address):"; print "04",xPublicKey,yPublicKey
print; print "******* Signature Generation *********"
xRandSignPoint, yRandSignPoint = EccMultiply(Gx,Gy,RandNum)
r = xRandSignPoint % N; print "r =", r
s = ((HashOfThingToSign + r*privKey)*(modinv(RandNum,N))) % N; print "s =", s
print; print "******* Signature Verification *********>>"
w = modinv(s,N)
xu1, yu1 = EccMultiply(Gx,Gy,(HashOfThingToSign * w)%N)
xu2, yu2 = EccMultiply(xPublicKey,yPublicKey,(r*w)%N)
x,y = ECadd(xu1,yu1,xu2,yu2)
print r==x; print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment