Title: update my code please|| wrong x print?
Post by: cixegz on February 10, 2022, 11:54:30 AM
run this code python2 # -*- coding: utf-8 -*- # run python2 # Elliptic curve point operations (Python 2.7)
def OnCurve(x,y): # Check if the point is on the curve A = (y*y)%P B = (x*x*x)%P C = False if A == (B + 7): C = True return C
def legendre_symbol(a,p): ls = pow(a, (p - 1) / 2, p) return -1 if ls == p - 1 else ls
def modsqrt(a,p): # Square root A modulo P if legendre_symbol(a, p) != 1: return 0 elif a == 0: return 0 elif p == 2: return p elif p % 4 == 3: return pow(a, (p + 1) / 4, p) s = p - 1 e = 0 while s % 2 == 0: s /= 2 e += 1 n = 2 while legendre_symbol(n, p) != -1: n += 1 x = pow(a, (s + 1) / 2, p) b = pow(a, s, p) g = pow(n, s, p) r = e while True: t = b m = 0 for m in xrange(r): if t == 1: break t = pow(t, 2, p) if m == 0: return x gs = pow(g, 2 ** (r - m - 1), p) g = (gs * gs) % p x = (x * gs) % p b = (b * g) % p r = m
def modinv(a,n): # Extended Euclidean Algorithm in elliptic curves lm, hm = 1,0 low, high = a%n,n while low > 1: ratio = high/low nm = hm - lm * ratio new = high - low * ratio hm = lm high = low lm = nm low = new return lm % n
def ECadd(xp,yp,xq,yq): # EC point addition m = ((yq-yp) * modinv(xq-xp,P))%P xr = (m*m-xp-xq)%P yr = (m*(xp-xr)-yp)%P return (xr,yr)
def ECdouble(xp,yp): # EC point doubling LamNumer = 3*xp*xp+Acurve LamDenom = 2*yp Lam = (LamNumer * modinv(LamDenom,P)) % P xr = (Lam*Lam-2*xp) % P yr = (Lam*(xp-xr)-yp) % P return (xr,yr)
def ECmul(xs,ys,Scalar): # EC point multiplication if Scalar == 0 or Scalar >= N: raise Exception("Invalid Scalar/Private Key") ScalarBin = str(bin(Scalar))[2:] Qx,Qy=xs,ys for i in range (1,len(ScalarBin)): Qx,Qy = ECdouble(Qx,Qy) if ScalarBin[i] == "1": Qx,Qy = ECadd(Qx,Qy,xs,ys) return (Qx,Qy)
def ECsub(xp,yp,xq,yq): # EC point subtraction X = (((yp+yq)*modinv(xq-xp,P))**2-xp-xq)%P A = (xp + X + xq)%P B = modsqrt(A,P) B1 = P - B Y = yq - (xq - X) * B X = X % P Y = Y % P if not OnCurve(X,Y): Y = yq - (xq - X) * B1 Y = Y % P return X,Y
def ECdiv(Qx,Qy,Scalar): # EC point division A = (N-1)/Scalar Px,Py = ECmul(Qx,Qy,A) Py = P-Py return Px,Py
P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1 N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 Gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 Acurve = 0
ukx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 # or your enter publickey x,y uky = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
#you need add ,sub ,div fadd = ECadd(ukx,uky,ukx,uky) # 1 + 1 =2 print ('fadd',hex(fadd[0]),hex(fadd[1]))
fsub = ECsub(fadd[0],fadd[1],Gx,Gy) # 2 -1 = 1 print ('fsub',hex(fsub[0]),hex(fsub[1]))
fdub = ECdouble(ukx,uky) # 1 * 2 = 2 print ('fdub',hex(fdub[0]),hex(fdub[1]))
fdiv = ECdiv(fdub[0],fdub[1],2) # 2 / 2 = 1 print ('fdiv',hex(fdiv[0]),hex(fdiv[1]))
output is: X is incorrect Y output is correct ('fadd', '0xc8333020c4688a754bf3ad462f1e9f1fac80649a463ae4d4c1afd48d20fccffL', '0xb7c52588d95c3b9aa25b0403f1eef75702e84bb7597aabe663b82f6f04ef2777L') ('fsub', '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798L', '0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L') ('fdub', '0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5L', '0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52aL') ('fdiv', '0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798L', '0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8L') my output X value incorrect print why? what is mistake this code Y value is correct print update please need correct output: fadd 1 + 1 = 2 fsub 2 - 1 = 1 fdub 1 * 2 = 2 fdiv 2 / 2 = 1
Title: Re: update my code please|| wrong x print?
Post by: coder0x15 on April 18, 2022, 12:36:00 PM
hi! you wrote to the author of this script? may be he can help you https://github.com/ragestack/EC-Point-Operations (https://github.com/ragestack/EC-Point-Operations)
Title: Re: update my code please|| wrong x print?
Post by: ymgve2 on April 24, 2022, 06:21:21 PM
The library is bad, when adding two points you first HAVE to check if they are the same point, and if they are, you have to use the doubling function. The code doesn't check, and therefore uses the wrong formulas when you add 1 + 1.
|