ShortCurve.php 8.0 KB

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