EdwardsCurve.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Elliptic\Curve;
  3. use Elliptic\Curve\EdwardsCurve\Point;
  4. use BN\BN;
  5. class EdwardsCurve extends BaseCurve
  6. {
  7. public $twisted;
  8. public $mOneA;
  9. public $extended;
  10. public $a;
  11. public $c;
  12. public $c2;
  13. public $d;
  14. public $d2;
  15. public $dd;
  16. public $oneC;
  17. function __construct($conf)
  18. {
  19. // NOTE: Important as we are creating point in Base.call()
  20. $this->twisted = ($conf["a"] | 0) != 1;
  21. $this->mOneA = $this->twisted && ($conf["a"] | 0) == -1;
  22. $this->extended = $this->mOneA;
  23. parent::__construct("edward", $conf);
  24. $this->a = (new BN($conf["a"], 16))->umod($this->red->m);
  25. $this->a = $this->a->toRed($this->red);
  26. $this->c = (new BN($conf["c"], 16))->toRed($this->red);
  27. $this->c2 = $this->c->redSqr();
  28. $this->d = (new BN($conf["d"], 16))->toRed($this->red);
  29. $this->dd = $this->d->redAdd($this->d);
  30. if (assert_options(ASSERT_ACTIVE)) {
  31. assert(!$this->twisted || $this->c->fromRed()->cmpn(1) == 0);
  32. }
  33. $this->oneC = ($conf["c"] | 0) == 1;
  34. }
  35. public function _mulA($num) {
  36. if ($this->mOneA)
  37. return $num->redNeg();
  38. else
  39. return $this->a->redMul($num);
  40. }
  41. public function _mulC($num) {
  42. if ($this->oneC)
  43. return $num;
  44. else
  45. return $this->c->redMul($num);
  46. }
  47. // Just for compatibility with Short curve
  48. public function jpoint($x, $y, $z, $t = null) {
  49. return $this->point($x, $y, $z, $t);
  50. }
  51. public function pointFromX($x, $odd = false) {
  52. $x = new BN($x, 16);
  53. if (!$x->red)
  54. $x = $x->toRed($this->red);
  55. $x2 = $x->redSqr();
  56. $rhs = $this->c2->redSub($this->a->redMul($x2));
  57. $lhs = $this->one->redSub($this->c2->redMul($this->d)->redMul($x2));
  58. $y2 = $rhs->redMul($lhs->redInvm());
  59. $y = $y2->redSqrt();
  60. if ($y->redSqr()->redSub($y2)->cmp($this->zero) != 0)
  61. throw new \Exception('invalid point');
  62. $isOdd = $y->fromRed()->isOdd();
  63. if ($odd && !$isOdd || !$odd && $isOdd)
  64. $y = $y->redNeg();
  65. return $this->point($x, $y);
  66. }
  67. public function pointFromY($y, $odd = false) {
  68. $y = new BN($y, 16);
  69. if (!$y->red)
  70. $y = $y->toRed($this->red);
  71. // x^2 = (y^2 - 1) / (d y^2 + 1)
  72. $y2 = $y->redSqr();
  73. $lhs = $y2->redSub($this->one);
  74. $rhs = $y2->redMul($this->d)->redAdd($this->one);
  75. $x2 = $lhs->redMul($rhs->redInvm());
  76. if ($x2->cmp($this->zero) == 0) {
  77. if ($odd)
  78. throw new \Exception('invalid point');
  79. else
  80. return $this->point($this->zero, $y);
  81. }
  82. $x = $x2->redSqrt();
  83. if ($x->redSqr()->redSub($x2)->cmp($this->zero) != 0)
  84. throw new \Exception('invalid point');
  85. if ($x->isOdd() != $odd)
  86. $x = $x->redNeg();
  87. return $this->point($x, $y);
  88. }
  89. public function validate($point) {
  90. if ($point->isInfinity())
  91. return true;
  92. // Curve: A * X^2 + Y^2 = C^2 * (1 + D * X^2 * Y^2)
  93. $point->normalize();
  94. $x2 = $point->x->redSqr();
  95. $y2 = $point->y->redSqr();
  96. $lhs = $x2->redMul($this->a)->redAdd($y2);
  97. $rhs = $this->c2->redMul($this->one->redAdd($this->d->redMul($x2)->redMul($y2)));
  98. return $lhs->cmp($rhs) == 0;
  99. }
  100. public function pointFromJSON($obj) {
  101. return Point::fromJSON($this, $obj);
  102. }
  103. public function point($x = null, $y = null, $z = null, $t = null) {
  104. return new Point($this, $x, $y, $z, $t);
  105. }
  106. }