Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created May 2, 2016 13:37
Show Gist options
  • Save AndyNovo/f9d408b8801d5fa94ff097b2f3d163c0 to your computer and use it in GitHub Desktop.
Save AndyNovo/f9d408b8801d5fa94ff097b2f3d163c0 to your computer and use it in GitHub Desktop.
def invMod(x, p):
return pow(x, p-2, p)
def addECC(x1,y1,x2,y2,a,b,p):
s = (invMod(x2-x1, p)*(y2-y1)) % p
if ((x1 - x2) % p == 0) and ((y1-y2) % p == 0):
s=(invMod(2*y1, p)*(3*x1*x1 + a)) % p
x3 = (s*s -x1 -x2) %p
y3 = (s*(x1-x3) -y1) % p
return x3,y3
def scalar(c, px, py, a, b, p):
#poor man's fast multiplication more for teaching than performance
mult2={}
mult2[0]=(px,py)
binc=bin(c)[2:]
k=len(binc)
tempx=px
tempy=py
for i in range(1,k+1):
mult2[i]=addECC(tempx,tempy,tempx,tempy,a,b,p)
tempx=mult2[i][0]
tempy=mult2[i][1]
largest1=k-1
ansx,ansy=mult2[largest1]
for i in range(1,k):
exp=k-1-i
if binc[i]=='1':
ansx, ansy = addECC(ansx, ansy, mult2[exp][0], mult2[exp][1], a, b, p)
return ansx, ansy
f=file('parameters','r')
lines=f.readlines()
f.close()
params = {}
currentHex=''
currentParam=''
def h2i(hexLines):
if (hexLines == ''):
return 0
return int(hexLines.replace(' ','').replace(':',''), 16)
def splitPoint(hexLines):
gen=hexLines.replace(' ','').replace(':','')[2:]
gl=len(gen)
return (int(gen[:gl/2],16), int(gen[gl/2:], 16))
ecpoints = ["Gener", "pub"]
for line in lines:
if line[0].isalpha():
if (currentHex != '' and currentParam != ''):
print "key:",currentParam
if not currentParam in ecpoints:
params[currentParam]=h2i(currentHex)
else:
params[currentParam]=splitPoint(currentHex)
currentParam = line.strip().replace(':','')[:5]
currentHex=''
else:
currentHex+=line.strip()
Private-Key: (521 bit)
priv:
00:a1:61:70:89:50:b2:d1:c6:0e:b2:38:e5:d5:30:
26:03:25:32:13:8e:33:80:1d:89:08:0e:48:1b:96:
07:46:6e:73:28:12:65:f9:23:8f:10:a4:6f:d9:4c:
2f:85:b4:ea:39:8a:ca:f0:fc:46:bb:22:cf:df:aa:
f2:d3:d3:39:45:1b
pub:
04:01:9c:c9:84:2f:11:16:17:c7:3d:66:6e:92:33:
27:2f:ca:d4:5d:6e:87:93:84:4c:fe:07:d8:54:65:
b8:25:39:3c:40:59:d5:10:b2:ca:7b:f7:ce:e6:94:
b2:f6:cf:e8:31:83:a8:9d:db:43:16:aa:a1:ef:9f:
72:f9:39:b9:cf:7d:1f:00:4f:45:2e:03:de:ec:b8:
f7:d1:f7:ba:05:98:cb:e2:d4:67:7b:98:a3:d8:1c:
a9:ed:42:4d:e3:e6:04:1a:dd:fb:93:07:21:2c:a6:
cf:e3:af:70:51:eb:ad:06:2b:0b:67:d9:2f:3a:b1:
f1:2a:a6:ad:c7:bc:d5:3a:5b:ce:bb:66:a3
Field Type: prime-field
Prime:
01:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff
A:
01:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:fc
B:
51:95:3e:b9:61:8e:1c:9a:1f:92:9a:21:a0:b6:85:
40:ee:a2:da:72:5b:99:b3:15:f3:b8:b4:89:91:8e:
f1:09:e1:56:19:39:51:ec:7e:93:7b:16:52:c0:bd:
3b:b1:bf:07:35:73:df:88:3d:2c:34:f1:ef:45:1f:
d4:6b:50:3f:00
Generator (uncompressed):
04:00:c6:85:8e:06:b7:04:04:e9:cd:9e:3e:cb:66:
23:95:b4:42:9c:64:81:39:05:3f:b5:21:f8:28:af:
60:6b:4d:3d:ba:a1:4b:5e:77:ef:e7:59:28:fe:1d:
c1:27:a2:ff:a8:de:33:48:b3:c1:85:6a:42:9b:f9:
7e:7e:31:c2:e5:bd:66:01:18:39:29:6a:78:9a:3b:
c0:04:5c:8a:5f:b4:2c:7d:1b:d9:98:f5:44:49:57:
9b:44:68:17:af:bd:17:27:3e:66:2c:97:ee:72:99:
5e:f4:26:40:c5:50:b9:01:3f:ad:07:61:35:3c:70:
86:a2:72:c2:40:88:be:94:76:9f:d1:66:50
Order:
01:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:ff:
ff:ff:ff:fa:51:86:87:83:bf:2f:96:6b:7f:cc:01:
48:f7:09:a5:d0:3b:b5:c9:b8:89:9c:47:ae:bb:6f:
b7:1e:91:38:64:09
Cofactor: 1 (0x1)
Seed:
d0:9e:88:00:29:1c:b8:53:96:cc:67:17:39:32:84:
aa:a0:da:64:ba
execfile('param_reader.py')
p=params['Prime']
gx=params['Gener'][0]
gy=params['Gener'][1]
a=params['A']
b=params['B']
q=params['Order']
x=params['priv']
pubx,puby=params['pub']
execfile('ecc.py')
import hashlib
m_text="What a wonderful world we live in, where number theory is the secret language of daily commerce!"
m=int(hashlib.sha256(m_text).hexdigest(), 16)
alpha=m
import random
y=random.randint(2, q-2)
bx,by=scalar(y, gx, gy, a, b, p)
beta=bx%q
s = invMod(y, q) * (((beta*x)%q + alpha) % q) % q
r=beta
sinv = invMod(s, q)
u1=sinv*alpha % q
u2 = sinv*beta % q
p1 = scalar(u1, gx, gy, a, b, p)
p2 = scalar(u2, pubx, puby, a, b, p)
testP = addECC(p1[0], p1[1], p2[0], p2[1], a, b, p)
if bx == testP[0]:
print "Valid signature"
else:
print "INVALID SIGNATURE, stop interacting with this entity!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment