MontCurve.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Elliptic\Curve;
  3. use Elliptic\Curve\MontCurve\Point;
  4. use Elliptic\Utils;
  5. use BN\BN;
  6. class MontCurve extends BaseCurve
  7. {
  8. public $a;
  9. public $b;
  10. public $i4;
  11. public $a24;
  12. function __construct($conf)
  13. {
  14. parent::__construct("mont", $conf);
  15. $this->a = (new BN($conf["a"], 16))->toRed($this->red);
  16. $this->b = (new BN($conf["b"], 16))->toRed($this->red);
  17. $this->i4 = (new BN(4))->toRed($this->red)->redInvm();
  18. $this->a24 = $this->i4->redMul($this->a->redAdd($this->two));
  19. }
  20. public function validate($point)
  21. {
  22. $x = $point->normalize()->x;
  23. $x2 = $x->redSqr();
  24. $rhs = $x2->redMul($x)->redAdd($x2->redMul($this->a))->redAdd($x);
  25. $y = $rhs->redSqr();
  26. return $y->redSqr()->cmp($rhs) ===0;
  27. }
  28. public function decodePoint($bytes, $enc = false) {
  29. return $this->point(Utils::toArray($bytes, $enc), 1);
  30. }
  31. public function point($x, $z) {
  32. return new Point($this, $x, $z);
  33. }
  34. public function pointFromJSON($obj) {
  35. return Point::fromJSON($this, $obj);
  36. }
  37. }
  38. ?>