I have a python script that does the ECC point addition (code paste below), it simply performs the P =Q1+Q2 on Jacob coordination.
However, when doing some regression tests, I found that if I exchange the P1 and P2 positions, I will get different results, one of which is correct.
Below is an example that simply using secp256k1 G point as one point, and 2*G as the 2nd point to run the test.
My questions (Update comments after get reply from @fgrieu)
1). The ECC point addition on a curve--would that be Commutative ( should be)?
2). I noticed that for the results, x coordinate are the same while y/z are different-- (they represent the same on affine coordinate).
3). Based on suggestions I update the script making it completed.
def Point_Add(self, Q1, Q2):
if (Q1.x==self.p):
return Q2
if (Q2.x==self.p):
return Q1
Q1z2 = (Q1.z*Q1.z) % self.p
Q2z2 = (Q2.z*Q2.z) % self.p
U1 = (Q1.x*Q2z2) % self.p
U2 = (Q2.x*Q1z2) % self.p
S1 = (Q1.y*Q2z2*Q2.z) % self.p
S2 = (Q2.y*Q1z2*Q1.z) % self.p
if (U1 == U2):
if (S1!=S2): # oppsite pair, i.e. Q1 = -Q2
return self.Unit
else: # Q1 = Q2
return self.Point_Double(Q1)
H = (U2-U1) % self.p
R = (S2-S1) % self.p
H2 = (H*H) % self.p
H3 = (H2*H) % self.p
x3 = (R*R-H3-2*U1*H2 ) % self.p
y3 = (R*(U1*H2-x3)-S1*H3 ) % self.p
z3 = (H*Q1.z*Q2.z) % self.p
return JBPoint(self, x3, y3, z3)
Test result
Debug 1: test P=Q1+Q2:
Point.X(Jacob): 0xca90ef9b06d7eb51d650e9145e3083cbd8df8759168862036f97a358f089848
Point.Y(Jacob): 0x435afe76017b8d55d04ff8a98dd60b2ba7eb6f87f6b28182ca4493d7165dd127
Point.Z(Jacob): 0x9242fa9c0b9f23a3bfea6a0eb6dbcfcbc4853fe9a25ee948105dc66a2a9b5baa
Point.X(affine): 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9
Point.Y(affine): 0x388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672
Point.X(affine): 112711660439710606056748659173929673102114977341539408544630613555209775888121
Point.Y(affine): 25583027980570883691656905877401976406448868254816295069919888960541586679410
Debug 2: test P=Q2+Q1:
Point.X(Jacob): 0xca90ef9b06d7eb51d650e9145e3083cbd8df8759168862036f97a358f089848
Point.Y(Jacob): 0xbca50189fe8472aa2fb007567229f4d458149078094d7e7d35bb6c27e9a22b08
Point.Z(Jacob): 0x6dbd0563f460dc5c401595f1492430343b7ac0165da116b7efa23994d564a085
Point.X(affine): 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9
Point.Y(affine): 0x388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672
Point.X(affine): 112711660439710606056748659173929673102114977341539408544630613555209775888121
Point.Y(affine): 25583027980570883691656905877401976406448868254816295069919888960541586679410