Point.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Elliptic\Curve\ShortCurve;
  3. use JsonSerializable;
  4. use BN\BN;
  5. class Point extends \Elliptic\Curve\BaseCurve\Point implements JsonSerializable
  6. {
  7. public $x;
  8. public $y;
  9. public $inf;
  10. function __construct($curve, $x, $y, $isRed)
  11. {
  12. parent::__construct($curve, 'affine');
  13. if( $x == null && $y == null )
  14. {
  15. $this->x = null;
  16. $this->y = null;
  17. $this->inf = true;
  18. }
  19. else
  20. {
  21. $this->x = new BN($x, 16);
  22. $this->y = new BN($y, 16);
  23. // Force redgomery representation when loading from JSON
  24. if( $isRed )
  25. {
  26. $this->x->forceRed($this->curve->red);
  27. $this->y->forceRed($this->curve->red);
  28. }
  29. if( !$this->x->red )
  30. $this->x = $this->x->toRed($this->curve->red);
  31. if( !$this->y->red )
  32. $this->y = $this->y->toRed($this->curve->red);
  33. $this->inf = false;
  34. }
  35. }
  36. public function _getBeta()
  37. {
  38. if( !isset($this->curve->endo) )
  39. return null;
  40. if( isset($this->precomputed) && isset($this->precomputed["beta"]) )
  41. return $this->precomputed["beta"];
  42. $beta = $this->curve->point($this->x->redMul($this->curve->endo["beta"]), $this->y);
  43. if( isset($this->precomputed) )
  44. {
  45. $endoMul = function($p) {
  46. return $this->curve->point($p->x->redMul($this->curve->endo["beta"]), $p->y);
  47. };
  48. $beta->precomputed = array(
  49. "beta" => null,
  50. "naf" => null,
  51. "doubles" => null
  52. );
  53. if( isset($this->precomputed["naf"]) )
  54. {
  55. $beta->precomputed["naf"] = array(
  56. "wnd" => $this->precomputed["naf"]["wnd"],
  57. "points" => array_map($endoMul, $this->precomputed["naf"]["points"])
  58. );
  59. }
  60. if( isset($this->precomputed["doubles"]) )
  61. {
  62. $beta->precomputed["doubles"] = array(
  63. "step" => $this->precomputed["doubles"]["step"],
  64. "points" => array_map($endoMul, $this->precomputed["doubles"]["points"])
  65. );
  66. }
  67. $this->precomputed["beta"] = $beta;
  68. }
  69. return $beta;
  70. }
  71. //toJSON()
  72. #[\ReturnTypeWillChange]
  73. public function jsonSerialize()
  74. {
  75. $res = array($this->x, $this->y);
  76. if( !isset($this->precomputed) )
  77. return $res;
  78. $pre = array();
  79. $addPre = false;
  80. if( isset($this->precomputed["doubles"]) )
  81. {
  82. $pre["doubles"] = array(
  83. "step" => $this->precomputed["doubles"]["step"],
  84. "points" => array_slice($this->precomputed["doubles"]["points"], 1)
  85. );
  86. $addPre = true;
  87. }
  88. if( isset($this->precomputed["naf"]) )
  89. {
  90. $pre["naf"] = array(
  91. "naf" => $this->precomputed["naf"]["wnd"],
  92. "points" => array_slice($this->precomputed["naf"]["points"], 1)
  93. );
  94. $addPre = true;
  95. }
  96. if( $addPre )
  97. array_push($res, $pre);
  98. return $res;
  99. }
  100. public static function fromJSON($curve, $obj, $red)
  101. {
  102. if( is_string($obj) )
  103. $obj = json_decode($obj);
  104. $point = $curve->point($obj[0], $obj[1], $red);
  105. if( count($obj) === 2 )
  106. return $point;
  107. $pre = $obj[2];
  108. $point->precomputed = array("beta" => null);
  109. $obj2point = function($obj) use ($curve, $red) {
  110. return $curve->point($obj[0], $obj[1], $red);
  111. };
  112. if( isset($pre["doubles"]) )
  113. {
  114. $tmp = array_map($obj2point, $pre["doubles"]["points"]);
  115. array_unshift($tmp, $point);
  116. $point->precomputed["doubles"] = array(
  117. "step" => $pre["doubles"]["step"],
  118. "points" => $tmp
  119. );
  120. }
  121. if( isset($pre["naf"]) )
  122. {
  123. $tmp = array_map($obj2point, $pre["naf"]["points"]);
  124. array_unshift($tmp, $point);
  125. $point->precomputed["naf"] = array(
  126. "wnd" => $pre["naf"]["wnd"],
  127. "points" => $tmp
  128. );
  129. }
  130. return $point;
  131. }
  132. public function inspect()
  133. {
  134. if( $this->isInfinity() )
  135. return "<EC Point Infinity>";
  136. return "<EC Point x: " . $this->x->fromRed()->toString(16, 2) .
  137. " y: " . $this->y->fromRed()->toString(16, 2) . ">";
  138. }
  139. public function __debugInfo() {
  140. return [
  141. "EC Point" => ($this->isInfinity() ?
  142. "Infinity" :
  143. [
  144. "x" => $this->x->fromRed()->toString(16, 2),
  145. "y" => $this->y->fromRed()->toString(16, 2)
  146. ])
  147. ];
  148. }
  149. public function isInfinity() {
  150. return $this->inf;
  151. }
  152. public function add($point)
  153. {
  154. // O + P = P
  155. if( $this->inf )
  156. return $point;
  157. // P + O = P
  158. if( $point->inf )
  159. return $this;
  160. // P + P = 2P
  161. if( $this->eq($point) )
  162. return $this->dbl();
  163. // P + (-P) = O
  164. if( $this->neg()->eq($point) )
  165. return $this->curve->point(null, null);
  166. // P + Q = O
  167. if( $this->x->cmp($point->x) === 0 )
  168. return $this->curve->point(null, null);
  169. $c = $this->y->redSub($point->y);
  170. if( ! $c->isZero() )
  171. $c = $c->redMul($this->x->redSub($point->x)->redInvm());
  172. $nx = $c->redSqr()->redISub($this->x)->redISub($point->x);
  173. $ny = $c->redMul($this->x->redSub($nx))->redISub($this->y);
  174. return $this->curve->point($nx, $ny);
  175. }
  176. public function dbl()
  177. {
  178. if( $this->inf )
  179. return $this;
  180. // 2P = 0
  181. $ys1 = $this->y->redAdd($this->y);
  182. if( $ys1->isZero() )
  183. return $this->curve->point(null, null);
  184. $x2 = $this->x->redSqr();
  185. $dyinv = $ys1->redInvm();
  186. $c = $x2->redAdd($x2)->redIAdd($x2)->redIAdd($this->curve->a)->redMul($dyinv);
  187. $nx = $c->redSqr()->redISub($this->x->redAdd($this->x));
  188. $ny = $c->redMul($this->x->redSub($nx))->redISub($this->y);
  189. return $this->curve->point($nx, $ny);
  190. }
  191. public function getX() {
  192. return $this->x->fromRed();
  193. }
  194. public function getY() {
  195. return $this->y->fromRed();
  196. }
  197. public function mul($k)
  198. {
  199. $k = new BN($k, 16);
  200. if( $this->_hasDoubles($k) )
  201. return $this->curve->_fixedNafMul($this, $k);
  202. elseif( isset($this->curve->endo) )
  203. return $this->curve->_endoWnafMulAdd(array($this), array($k));
  204. return $this->curve->_wnafMul($this, $k);
  205. }
  206. public function mulAdd($k1, $p2, $k2, $j = false)
  207. {
  208. $points = array($this, $p2);
  209. $coeffs = array($k1, $k2);
  210. if( isset($this->curve->endo) )
  211. return $this->curve->_endoWnafMulAdd($points, $coeffs, $j);
  212. return $this->curve->_wnafMulAdd(1, $points, $coeffs, 2, $j);
  213. }
  214. public function jmulAdd($k1, $p2, $k2) {
  215. return $this->mulAdd($k1, $p2, $k2, true);
  216. }
  217. public function eq($point)
  218. {
  219. return (
  220. $this === $point ||
  221. $this->inf === $point->inf &&
  222. ($this->inf || $this->x->cmp($point->x) === 0 && $this->y->cmp($point->y) === 0)
  223. );
  224. }
  225. public function neg($precompute = false)
  226. {
  227. if( $this->inf )
  228. return $this;
  229. $res = $this->curve->point($this->x, $this->y->redNeg());
  230. if( $precompute && isset($this->precomputed) )
  231. {
  232. $res->precomputed = array();
  233. $pre = $this->precomputed;
  234. $negate = function($point) {
  235. return $point->neg();
  236. };
  237. if( isset($pre["naf"]) )
  238. {
  239. $res->precomputed["naf"] = array(
  240. "wnd" => $pre["naf"]["wnd"],
  241. "points" => array_map($negate, $pre["naf"]["points"])
  242. );
  243. }
  244. if( isset($pre["doubles"]) )
  245. {
  246. $res->precomputed["doubles"] = array(
  247. "step" => $pre["doubles"]["step"],
  248. "points" => array_map($negate, $pre["doubles"]["points"])
  249. );
  250. }
  251. }
  252. return $res;
  253. }
  254. public function toJ()
  255. {
  256. if( $this->inf )
  257. return $this->curve->jpoint(null, null, null);
  258. return $this->curve->jpoint($this->x, $this->y, $this->curve->one);
  259. }
  260. }
  261. ?>