-snip-
input two point
privatekey 3 - 2 = 1
point1 - point2 = point3
X Y
point1 f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9 388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672
point2 c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5 1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a
i need 3rd point subtract value
please write easy understand make python or explain mathematicaly
Actually the scalar subtraction is the addition where you change
y value of the second point from
y to
modulo - yPlease find the python code with the
sub function which subtracts one Point from another:
Demonstration of the function result:
>>> Point1 = Point(0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9, 0x388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672)
>>> Point2 = Point(0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5, 0x1ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a)
>>> Point3 = sub(Point1,Point2)
>>> hex(Point3.x),hex(Point3.y)
('0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', '0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8')
Python code:
import gmpy2
modulo = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
order = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
PG = Point(Gx,Gy)
Z = Point(0,0) # zero-point
# returns (g, x, y) a*x + b*y = gcd(x, y)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def modinv(m, n = modulo):
while m < 0:
m += n
g, x, _ = egcd(m, n)
if g == 1:
return x % n
else: print (' no inverse exist')
def mul2(Pmul2, p = modulo):
R = Point(0,0)
#c = 3*Pmul2.x*Pmul2.x*modinv(2*Pmul2.y, p) % p
c = 3*Pmul2.x*Pmul2.x*gmpy2.invert(2*Pmul2.y, p) % p
R.x = (c*c-2*Pmul2.x) % p
R.y = (c*(Pmul2.x - R.x)-Pmul2.y) % p
return R
def add(Padd, Q, p = modulo):
if Padd.x == Padd.y == 0: return Q
if Q.x == Q.y == 0: return Padd
if Padd == Q: return mul2(Q)
R = Point()
dx = (Q.x - Padd.x) % p
dy = (Q.y - Padd.y) % p
c = dy * gmpy2.invert(dx, p) % p
#c = dy * modinv(dx, p) % p
R.x = (c*c - Padd.x - Q.x) % p
R.y = (c*(Padd.x - R.x) - Padd.y) % p
return R
def mulk(k, Pmulk, p = modulo):
if k == 0: return Z
if k == 1: return Pmulk
if (k % 2 == 0): return mulk(k//2, mul2(Pmulk, p), p)
return add(Pmulk, mulk((k-1)//2, mul2(Pmulk, p), p), p)
def sub(P1, P2, p = modulo): #scalar subtraction P1-P2
if P1 == P2: return Z
if P2.x == P2.y == 0: return P1
return add (P1, Point(P2.x, modulo - P2.y))
If you do not have gmpy2 I recommend to install it (it is 50x faster than self implemented inverse), but you can just uncomment
c calculation in mul2 and add functions and comment the formula with gmpy2.
EDIT: corrected typo mistakes