Point.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. public function jsonSerialize()
  73. {
  74. $res = array($this->x, $this->y);
  75. if( !isset($this->precomputed) )
  76. return $res;
  77. $pre = array();
  78. $addPre = false;
  79. if( isset($this->precomputed["doubles"]) )
  80. {
  81. $pre["doubles"] = array(
  82. "step" => $this->precomputed["doubles"]["step"],
  83. "points" => array_slice($this->precomputed["doubles"]["points"], 1)
  84. );
  85. $addPre = true;
  86. }
  87. if( isset($this->precomputed["naf"]) )
  88. {
  89. $pre["naf"] = array(
  90. "naf" => $this->precomputed["naf"]["wnd"],
  91. "points" => array_slice($this->precomputed["naf"]["points"], 1)
  92. );
  93. $addPre = true;
  94. }
  95. if( $addPre )
  96. array_push($res, $pre);
  97. return $res;
  98. }
  99. public static function fromJSON($curve, $obj, $red)
  100. {
  101. if( is_string($obj) )
  102. $obj = json_decode($obj);
  103. $point = $curve->point($obj[0], $obj[1], $red);
  104. if( count($obj) === 2 )
  105. return $point;
  106. $pre = $obj[2];
  107. $point->precomputed = array("beta" => null);
  108. $obj2point = function($obj) use ($curve, $red) {
  109. return $curve->point($obj[0], $obj[1], $red);
  110. };
  111. if( isset($pre["doubles"]) )
  112. {
  113. $tmp = array_map($obj2point, $pre["doubles"]["points"]);
  114. array_unshift($tmp, $point);
  115. $point->precomputed["doubles"] = array(
  116. "step" => $pre["doubles"]["step"],
  117. "points" => $tmp
  118. );
  119. }
  120. if( isset($pre["naf"]) )
  121. {
  122. $tmp = array_map($obj2point, $pre["naf"]["points"]);
  123. array_unshift($tmp, $point);
  124. $point->precomputed["naf"] = array(
  125. "wnd" => $pre["naf"]["wnd"],
  126. "points" => $tmp
  127. );
  128. }
  129. return $point;
  130. }
  131. public function inspect()
  132. {
  133. if( $this->isInfinity() )
  134. return "<EC Point Infinity>";
  135. return "<EC Point x: " . $this->x->fromRed()->toString(16, 2) .
  136. " y: " . $this->y->fromRed()->toString(16, 2) . ">";
  137. }
  138. public function __debugInfo() {
  139. return [
  140. "EC Point" => ($this->isInfinity() ?
  141. "Infinity" :
  142. [
  143. "x" => $this->x->fromRed()->toString(16, 2),
  144. "y" => $this->y->fromRed()->toString(16, 2)
  145. ])
  146. ];
  147. }
  148. public function isInfinity() {
  149. return $this->inf;
  150. }
  151. public function add($point)
  152. {
  153. // O + P = P
  154. if( $this->inf )
  155. return $point;
  156. // P + O = P
  157. if( $point->inf )
  158. return $this;
  159. // P + P = 2P
  160. if( $this->eq($point) )
  161. return $this->dbl();
  162. // P + (-P) = O
  163. if( $this->neg()->eq($point) )
  164. return $this->curve->point(null, null);
  165. // P + Q = O
  166. if( $this->x->cmp($point->x) === 0 )
  167. return $this->curve->point(null, null);
  168. $c = $this->y->redSub($point->y);
  169. if( ! $c->isZero() )
  170. $c = $c->redMul($this->x->redSub($point->x)->redInvm());
  171. $nx = $c->redSqr()->redISub($this->x)->redISub($point->x);
  172. $ny = $c->redMul($this->x->redSub($nx))->redISub($this->y);
  173. return $this->curve->point($nx, $ny);
  174. }
  175. public function dbl()
  176. {
  177. if( $this->inf )
  178. return $this;
  179. // 2P = 0
  180. $ys1 = $this->y->redAdd($this->y);
  181. if( $ys1->isZero() )
  182. return $this->curve->point(null, null);
  183. $x2 = $this->x->redSqr();
  184. $dyinv = $ys1->redInvm();
  185. $c = $x2->redAdd($x2)->redIAdd($x2)->redIAdd($this->curve->a)->redMul($dyinv);
  186. $nx = $c->redSqr()->redISub($this->x->redAdd($this->x));
  187. $ny = $c->redMul($this->x->redSub($nx))->redISub($this->y);
  188. return $this->curve->point($nx, $ny);
  189. }
  190. public function getX() {
  191. return $this->x->fromRed();
  192. }
  193. public function getY() {
  194. return $this->y->fromRed();
  195. }
  196. public function mul($k)
  197. {
  198. $k = new BN($k, 16);
  199. if( $this->_hasDoubles($k) )
  200. return $this->curve->_fixedNafMul($this, $k);
  201. elseif( isset($this->curve->endo) )
  202. return $this->curve->_endoWnafMulAdd(array($this), array($k));
  203. return $this->curve->_wnafMul($this, $k);
  204. }
  205. public function mulAdd($k1, $p2, $k2, $j = false)
  206. {
  207. $points = array($this, $p2);
  208. $coeffs = array($k1, $k2);
  209. if( isset($this->curve->endo) )
  210. return $this->curve->_endoWnafMulAdd($points, $coeffs, $j);
  211. return $this->curve->_wnafMulAdd(1, $points, $coeffs, 2, $j);
  212. }
  213. public function jmulAdd($k1, $p2, $k2) {
  214. return $this->mulAdd($k1, $p2, $k2, true);
  215. }
  216. public function eq($point)
  217. {
  218. return (
  219. $this === $point ||
  220. $this->inf === $point->inf &&
  221. ($this->inf || $this->x->cmp($point->x) === 0 && $this->y->cmp($point->y) === 0)
  222. );
  223. }
  224. public function neg($precompute = false)
  225. {
  226. if( $this->inf )
  227. return $this;
  228. $res = $this->curve->point($this->x, $this->y->redNeg());
  229. if( $precompute && isset($this->precomputed) )
  230. {
  231. $res->precomputed = array();
  232. $pre = $this->precomputed;
  233. $negate = function($point) {
  234. return $point->neg();
  235. };
  236. if( isset($pre["naf"]) )
  237. {
  238. $res->precomputed["naf"] = array(
  239. "wnd" => $pre["naf"]["wnd"],
  240. "points" => array_map($negate, $pre["naf"]["points"])
  241. );
  242. }
  243. if( isset($pre["doubles"]) )
  244. {
  245. $res->precomputed["doubles"] = array(
  246. "step" => $pre["doubles"]["step"],
  247. "points" => array_map($negate, $pre["doubles"]["points"])
  248. );
  249. }
  250. }
  251. return $res;
  252. }
  253. public function toJ()
  254. {
  255. if( $this->inf )
  256. return $this->curve->jpoint(null, null, null);
  257. return $this->curve->jpoint($this->x, $this->y, $this->curve->one);
  258. }
  259. }
  260. ?>