-
Notifications
You must be signed in to change notification settings - Fork 0
/
ECDH.py
34 lines (28 loc) · 1.06 KB
/
ECDH.py
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
import hashlib
Pcurve = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCEAF48A03BBFD25E8CD0364141
Acurve = 0; Bcurve = 7
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
GPoint = (Gx,Gy)
privKey = 0xAAAAA
def modinv(a,n=Pcurve):
lm, hm = 1,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 ECadd(a,b):
LamAdd = ((b[1]-a[1])*modinv(b[0]-a[0],Pcurve))%Pcurve
x = (LamAdd*LamAdd-a[0]-b[0]) % Pcurve
y = (LamAdd*(a[0]-x)-a[1]) % Pcurve
return (x,y)
def ECdouble(a):
Lam = ((3*a[0]*a[0]+Acurve)*modinv((2*a[1]),Pcurve))%Pcurve
x = (Lam*Lam-2*a[0])%Pcurve
y = (Lam*(a[0]-x)-a[1])%Pcurve
return(x,y)
def EccMultiply(GenPoint, ScalarHex):
if ScalarHex == 0 or ScalarHex >= N: raise Exeption("Invalid Scalar/Privat Key")