ShortCurve.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace Elliptic\Curve;
  3. use Elliptic\Curve\ShortCurve\Point;
  4. use Elliptic\Curve\ShortCurve\JPoint;
  5. use BN\BN;
  6. use \Exception;
  7. class ShortCurve extends BaseCurve
  8. {
  9. public $a;
  10. public $b;
  11. public $tinv;
  12. public $zeroA;
  13. public $threeA;
  14. public $endo;
  15. private $_endoWnafT1;
  16. private $_endoWnafT2;
  17. function __construct($conf)
  18. {
  19. parent::__construct("short", $conf);
  20. $this->a = (new BN($conf["a"], 16))->toRed($this->red);
  21. $this->b = (new BN($conf["b"], 16))->toRed($this->red);
  22. $this->tinv = $this->two->redInvm();
  23. $this->zeroA = $this->a->fromRed()->isZero();
  24. $this->threeA = $this->a->fromRed()->sub($this->p)->cmpn(-3) === 0;
  25. // If curve is endomorphic, precalculate beta and lambda
  26. $this->endo = $this->_getEndomorphism($conf);
  27. $this->_endoWnafT1 = array(0,0,0,0);
  28. $this->_endoWnafT2 = array(0,0,0,0);
  29. }
  30. private function _getEndomorphism($conf)
  31. {
  32. // No efficient endomorphism
  33. if( !$this->zeroA || !isset($this->g) || !isset($this->n) || $this->p->modn(3) != 1 )
  34. return null;
  35. // Compute beta and lambda, that lambda * P = (beta * Px; Py)
  36. $beta = null;
  37. $lambda = null;
  38. if( isset($conf["beta"]) )
  39. $beta = (new BN($conf["beta"], 16))->toRed($this->red);
  40. else
  41. {
  42. $betas = $this->_getEndoRoots($this->p);
  43. // Choose smallest beta
  44. $beta = $betas[0]->cmp($betas[1]) < 0 ? $betas[0] : $betas[1];
  45. $beta = $beta->toRed($this->red);
  46. }
  47. if( isset($conf["lambda"]) )
  48. $lambda = new BN($conf["lambda"], 16);
  49. else
  50. {
  51. // Choose the lambda that is matching selected beta
  52. $lambdas = $this->_getEndoRoots($this->n);
  53. if( $this->g->mul($lambdas[0])->x->cmp($this->g->x->redMul($beta)) == 0 )
  54. $lambda = $lambdas[0];
  55. else
  56. {
  57. $lambda = $lambdas[1];
  58. if (assert_options(ASSERT_ACTIVE)) {
  59. assert($this->g->mul($lambda)->x->cmp($this->g->x->redMul($beta)) === 0);
  60. }
  61. }
  62. }
  63. // Get basis vectors, used for balanced length-two representation
  64. $basis = null;
  65. if( !isset($conf["basis"]) )
  66. $basis = $this->_getEndoBasis($lambda);
  67. else
  68. {
  69. $callback = function($vector) {
  70. return array(
  71. "a" => new BN($vector["a"], 16),
  72. "b" => new BN($vector["b"], 16)
  73. );
  74. };
  75. $basis = array_map($callback, $conf["basis"]);
  76. }
  77. return array(
  78. "beta" => $beta,
  79. "lambda" => $lambda,
  80. "basis" => $basis
  81. );
  82. }
  83. private function _getEndoRoots($num)
  84. {
  85. // Find roots of for x^2 + x + 1 in F
  86. // Root = (-1 +- Sqrt(-3)) / 2
  87. //
  88. $red = $num === $this->p ? $this->red : BN::mont($num);
  89. $tinv = (new BN(2))->toRed($red)->redInvm();
  90. $ntinv = $tinv->redNeg();
  91. $s = (new BN(3))->toRed($red)->redNeg()->redSqrt()->redMul($tinv);
  92. return array(
  93. $ntinv->redAdd($s)->fromRed(),
  94. $ntinv->redSub($s)->fromRed()
  95. );
  96. }
  97. private function _getEndoBasis($lambda)
  98. {
  99. // aprxSqrt >= sqrt(this.n)
  100. $aprxSqrt = $this->n->ushrn(intval($this->n->bitLength() / 2));
  101. // 3.74
  102. // Run EGCD, until r(L + 1) < aprxSqrt
  103. $u = $lambda;
  104. $v = $this->n->_clone();
  105. $x1 = new BN(1);
  106. $y1 = new BN(0);
  107. $x2 = new BN(0);
  108. $y2 = new BN(1);
  109. // NOTE: all vectors are roots of: a + b * lambda = 0 (mod n)
  110. $a0 = 0;
  111. $b0 = 0;
  112. // First vector
  113. $a1 = 0;
  114. $b1 = 0;
  115. // Second vector
  116. $a2 = 0;
  117. $b2 = 0;
  118. $prevR = 0;
  119. $i = 0;
  120. $r = 0;
  121. $x = 0;
  122. while( ! $u->isZero() )
  123. {
  124. $q = $v->div($u);
  125. $r = $v->sub($q->mul($u));
  126. $x = $x2->sub($q->mul($x1));
  127. $y = $y2->sub($q->mul($y2));
  128. if( !$a1 && $r->cmp($aprxSqrt) < 0 )
  129. {
  130. $a0 = $prevR->neg();
  131. $b0 = $x1;
  132. $a1 = $r->neg();
  133. $b1 = $x;
  134. }
  135. elseif($a1 && ++$i === 2)
  136. break;
  137. $prevR = $r;
  138. $v = $u;
  139. $u = $r;
  140. $x2 = $x1;
  141. $x1 = $x;
  142. $y2 = $y1;
  143. $y1 = $y;
  144. }
  145. $a2 = $r->neg();
  146. $b2 = $x;
  147. $len1 = $a1->sqr()->add($b1->sqr());
  148. $len2 = $a2->sqr()->add($b2->sqr());
  149. if( $len2->cmp($len1) >= 0 )
  150. {
  151. $a2 = $a0;
  152. $b2 = $b0;
  153. }
  154. // Normalize signs
  155. if( $a1->negative() )
  156. {
  157. $a1 = $a1->neg();
  158. $b1 = $b1->neg();
  159. }
  160. if( $a2->negative() )
  161. {
  162. $a2 = $a2->neg();
  163. $b2 = $b2->neg();
  164. }
  165. return array(
  166. array( "a" => $a1, "b" => $b1 ),
  167. array( "a" => $a2, "b" => $b2 ),
  168. );
  169. }
  170. public function _endoSplit($k)
  171. {
  172. $basis = $this->endo["basis"];
  173. $v1 = $basis[0];
  174. $v2 = $basis[1];
  175. $c1 = $v2["b"]->mul($k)->divRound($this->n);
  176. $c2 = $v1["b"]->neg()->mul($k)->divRound($this->n);
  177. $p1 = $c1->mul($v1["a"]);
  178. $p2 = $c2->mul($v2["a"]);
  179. $q1 = $c1->mul($v1["b"]);
  180. $q2 = $c2->mul($v2["b"]);
  181. //Calculate answer
  182. $k1 = $k->sub($p1)->sub($p2);
  183. $k2 = $q1->add($q2)->neg();
  184. return array( "k1" => $k1, "k2" => $k2 );
  185. }
  186. public function pointFromX($x, $odd)
  187. {
  188. $x = new BN($x, 16);
  189. if( !$x->red )
  190. $x = $x->toRed($this->red);
  191. $y2 = $x->redSqr()->redMul($x)->redIAdd($x->redMul($this->a))->redIAdd($this->b);
  192. $y = $y2->redSqrt();
  193. if( $y->redSqr()->redSub($y2)->cmp($this->zero) !== 0 )
  194. throw new Exception("Invalid point");
  195. // XXX Is there any way to tell if the number is odd without converting it
  196. // to non-red form?
  197. $isOdd = $y->fromRed()->isOdd();
  198. if( $odd != $isOdd )
  199. $y = $y->redNeg();
  200. return $this->point($x, $y);
  201. }
  202. public function validate($point)
  203. {
  204. if( $point->inf )
  205. return true;
  206. $x = $point->x;
  207. $y = $point->y;
  208. $ax = $this->a->redMul($x);
  209. $rhs = $x->redSqr()->redMul($x)->redIAdd($ax)->redIAdd($this->b);
  210. return $y->redSqr()->redISub($rhs)->isZero();
  211. }
  212. public function _endoWnafMulAdd($points, $coeffs, $jacobianResult = false)
  213. {
  214. $npoints = &$this->_endoWnafT1;
  215. $ncoeffs = &$this->_endoWnafT2;
  216. for($i = 0; $i < count($points); $i++)
  217. {
  218. $split = $this->_endoSplit($coeffs[$i]);
  219. $p = $points[$i];
  220. $beta = $p->_getBeta();
  221. if( $split["k1"]->negative() )
  222. {
  223. $split["k1"]->ineg();
  224. $p = $p->neg(true);
  225. }
  226. if( $split["k2"]->negative() )
  227. {
  228. $split["k2"]->ineg();
  229. $beta = $beta->neg(true);
  230. }
  231. $npoints[$i * 2] = $p;
  232. $npoints[$i * 2 + 1] = $beta;
  233. $ncoeffs[$i * 2] = $split["k1"];
  234. $ncoeffs[$i * 2 + 1] = $split["k2"];
  235. }
  236. $res = $this->_wnafMulAdd(1, $npoints, $ncoeffs, $i * 2, $jacobianResult);
  237. // Clean-up references to points and coefficients
  238. for($j = 0; $j < 2 * $i; $j++)
  239. {
  240. $npoints[$j] = null;
  241. $ncoeffs[$j] = null;
  242. }
  243. return $res;
  244. }
  245. public function point($x, $y, $isRed = false) {
  246. return new Point($this, $x, $y, $isRed);
  247. }
  248. public function pointFromJSON($obj, $red) {
  249. return Point::fromJSON($this, $obj, $red);
  250. }
  251. public function jpoint($x, $y, $z) {
  252. return new JPoint($this, $x, $y, $z);
  253. }
  254. }
  255. ?>