montgomery.q 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. :name:fe:x2:z2:x3:z3:tmp0:tmp1:
  2. fe r:var/r=fe:
  3. enter f:enter/f:>X2=fe#1:>Z2=fe#2:>X3=fe#3:>Z3=fe#4:
  4. return:nofallthrough:<X4=fe#1:<Z4=fe#2:<X5=fe#3:<Z5=fe#4:leave:
  5. h=f+g:<f=fe:<g=fe:>h=fe:asm/fe_add(>h,<f,<g);:
  6. h=f-g:<f=fe:<g=fe:>h=fe:asm/fe_sub(>h,<f,<g);:
  7. h=f*g:<f=fe:<g=fe:>h=fe:asm/fe_mul(>h,<f,<g);:
  8. h=f^2:<f=fe:>h=fe:asm/fe_sq(>h,<f);:
  9. h=X1*g:<g=fe:>h=fe:asm/fe_mul(>h,x1,<g);:
  10. h=a24*g:<g=fe:>h=fe:asm/fe_mul121666(>h,<g);:
  11. assign x2 to h:<h=fe#1:
  12. assign z2 to h:<h=fe#2:
  13. assign x3 to h:<h=fe#3:
  14. assign z3 to h:<h=fe#4:
  15. :
  16. fe X2
  17. fe Z2
  18. fe X3
  19. fe Z3
  20. fe X4
  21. fe Z4
  22. fe X5
  23. fe Z5
  24. fe A
  25. fe B
  26. fe C
  27. fe D
  28. fe E
  29. fe AA
  30. fe BB
  31. fe DA
  32. fe CB
  33. fe t0
  34. fe t1
  35. fe t2
  36. fe t3
  37. fe t4
  38. enter ladder
  39. # http://hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
  40. # reordered to fit into 2 temporaries
  41. D = X3-Z3
  42. B = X2-Z2
  43. A = X2+Z2
  44. C = X3+Z3
  45. DA = D*A
  46. CB = C*B
  47. BB = B^2
  48. AA = A^2
  49. t0 = DA+CB
  50. assign x3 to t0
  51. t1 = DA-CB
  52. X4 = AA*BB
  53. E = AA-BB
  54. t2 = t1^2
  55. t3 = a24*E
  56. X5 = t0^2
  57. t4 = BB+t3
  58. Z5 = X1*t2
  59. Z4 = E*t4
  60. # double-check against Theorem B.1 in Curve25519 paper:
  61. # X4-(X2^2-Z2^2)^2
  62. # Z4-4*X2*Z2*(X2^2+(4*a24-2)*X2*Z2+Z2^2)
  63. # X5-4*(X2*X3-Z2*Z3)^2
  64. # Z5-4*(X2*Z3-Z2*X3)^2*X1
  65. return