Advertisement
AnthonyCagliano

Untitled

Dec 8th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // given ptP and ptQ, return addition result in ptP
  2. void point_add(struct Point *p, struct Point *q){
  3. // P + Q = R
  4. // (xp, yp) + (xq, yq) = (xr, yr)
  5. // Y = (yq - yp)/(xq - xp) (isn't that some kind of distance)?
  6. // xr = Y^2 + Y - xp - xq
  7. // yr = Y(xp - xr) - yp
  8. // assert: neither P or Q are point at infinity, and Px != Qx
  9. // if P or Q is point at infinity, R = other point
  10. // if P = Q, double instead
  11. // if Px == Qx, set P to point at infinity
  12.  
  13. if(!point_iszero(q)) {
  14. if(point_iszero(p)) memcpy(p, q, sizeof(struct Point));
  15. else {
  16. if(point_isequal(p, q)) point_double(p);
  17. else if(bigint_isequal(p->x, q->x)) memset(p, 0, sizeof(struct Point));
  18. else {
  19.  
  20. BIGINT t1,t2,t3,t4;
  21.  
  22. // Yp + Yq
  23. bigint_add(t1, p->y, q->y);
  24. // Xp + Xq
  25. bigint_add(t2, p->x, q->x);
  26. // inv(t2)
  27. bigint_invert(t3, t2);
  28. // (Py + Yq) / (Xp + Xq)
  29. bigint_mul(t1, t1, t3); // t1 = slope
  30.  
  31. // slope^2
  32. bigint_mul(t4, t1, t1);
  33. // + slope
  34. bigint_add(t4, t4, t1);
  35. // + Xp + Xq
  36. bigint_add(t4, t4, t2); // we have Xres
  37.  
  38. // Xp - Xres
  39. bigint_add(t3, p->x, t4);
  40. // * slope
  41. bigint_mul(t3, t3, t1);
  42. // + Yq
  43. bigint_add(t3, t3, p->y);
  44.  
  45. // copy Xres and Y res to P
  46. memcpy(p->x, t4, sizeof t4);
  47. bigint_add(p->y, t3, p->x);
  48.  
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement