I am writing a small cryptography project with an elliptic curve, and the program works well when I use an affine coordinate system, which means that each point is represented by two coordinates (x ', y').
Now I am trying to replace the affine coordinate system with a Jacobian coordinate system in which each point is represented by three coordinates (x, y, z), x '= x / z² and y' = y / z³.
I would like to know how to convert affine coordinates to Jacobian coordinates **. In some textbooks, people use the formula: (x, y) = (x, y, 1) which means that the z-coordinate is always equal to one. But I'm not sure if this is correct.
Then, to add points along the elliptic curve, calculate P (x1, y1, z1) + Q (x2, y2, z2) = R (x3, y3, z3). I used the following formulas in my program:
u1 = x1.z2² u2 = x2.z1² s1 = y1.z2³ s2 = y2.z1³ h = u2 - u1 r = s2 - s1 x3 = r² - h³ - 2.u1.h² Y3 = r. (U1.h² - x3) - s1.h³ z3 = z1.z2.h
But when I test my program, I get some negative coordinates, for example. (-2854978200, -5344897546224.578). And when I try to convert the result back to an affine coordinate system with the formula (x '= x / z², y' = y / z³), I get (-8545, -27679), in fact the x coordinate is -8545.689 .... Coordinate jacobian x is not divisible by z².
What if the coordinates are not integers? And if they are negative? I tried MOD with the field size of my curve, but the result is also not correct.
Thus, a point using jacobian coordinates (x,y,1) is correct, but not unique. All points satisfying the condition (a^2.x,a^3.y,a) are equivalent. And in my program, the curve is determined in the main field, so when I calculate u1, u2, s1, s2 ... should I apply MOD p to each variable?
And to convert the final result back to affine coordinates, for example. The x coordinate is actually not a division, is it a modular inverse? For example, my curve is defined in a finite simple field p=11 , and I have a point using jacobian coordinates (15,3,2) to convert the jacobian x coordinate to an affine x coordinate, I have to calculate 2^2 = 4 => x = 4^-1 mod p => x = 3 and 15.3 mod p = 1 , so the affine coordinate x is 1, is this correct?
The purpose of the Jacobian coordinates is to avoid division during addition. But, as Thomas Pornin said, when we calculate P1 + P2 = P3 , there are some special cases to handle.
- P1 and P2 are both infinite:
P3=infinite . - P1 is infinite:
P3=P2 . - P2 is infinite:
P3=P1 . - P1 and P2 have the same x coordinate, but different y coordinates or both y coordinates are 0:
P3=infinite . - P1 and P2 have a different x coordinate:
Addition formula . - P1 and P2 have the same coordinates:
Doubling formula .
And here are the prototypes of my C functions:
jac_addition(jacobian *, point *, jacobian *); jac_doubling(jacobian *, jacobian *);
point is a structure representing the point defined in the affine coordinate system, and jacobian for the Jacobian system.
The problem is that when I handle these special cases, especially the fourth, I still translate both points back to affine coordinates or I can’t compare their coordinates, which means that I still need to calculate the division.