Point.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Elliptic\Curve\BaseCurve;
  3. use Elliptic\Utils;
  4. abstract class Point
  5. {
  6. public $curve;
  7. public $type;
  8. public $precomputed;
  9. function __construct($curve, $type)
  10. {
  11. $this->curve = $curve;
  12. $this->type = $type;
  13. $this->precomputed = null;
  14. }
  15. abstract public function eq($other);
  16. public function validate() {
  17. return $this->curve->validate($this);
  18. }
  19. public function encodeCompressed($enc) {
  20. return $this->encode($enc, true);
  21. }
  22. public function encode($enc, $compact = false) {
  23. return Utils::encode($this->_encode($compact), $enc);
  24. }
  25. protected function _encode($compact)
  26. {
  27. $len = $this->curve->p->byteLength();
  28. $x = $this->getX()->toArray("be", $len);
  29. if( $compact )
  30. {
  31. array_unshift($x, ($this->getY()->isEven() ? 0x02 : 0x03));
  32. return $x;
  33. }
  34. return array_merge(array(0x04), $x, $this->getY()->toArray("be", $len));
  35. }
  36. public function precompute($power = null)
  37. {
  38. if( isset($this->precomputed) )
  39. return $this;
  40. $this->precomputed = array(
  41. "naf" => $this->_getNAFPoints(8),
  42. "doubles" => $this->_getDoubles(4, $power),
  43. "beta" => $this->_getBeta()
  44. );
  45. return $this;
  46. }
  47. protected function _hasDoubles($k)
  48. {
  49. if( !isset($this->precomputed) || !isset($this->precomputed["doubles"]) )
  50. return false;
  51. return count($this->precomputed["doubles"]["points"]) >= ceil(($k->bitLength() + 1) / $this->precomputed["doubles"]["step"]);
  52. }
  53. public function _getDoubles($step = null, $power = null)
  54. {
  55. if( isset($this->precomputed) && isset($this->precomputed["doubles"]) )
  56. return $this->precomputed["doubles"];
  57. $doubles = array( $this );
  58. $acc = $this;
  59. for($i = 0; $i < $power; $i += $step)
  60. {
  61. for($j = 0; $j < $step; $j++)
  62. $acc = $acc->dbl();
  63. array_push($doubles, $acc);
  64. }
  65. return array(
  66. "step" => $step,
  67. "points" => $doubles
  68. );
  69. }
  70. public function _getNAFPoints($wnd)
  71. {
  72. if( isset($this->precomputed) && isset($this->precomputed["naf"]) )
  73. return $this->precomputed["naf"];
  74. $res = array( $this );
  75. $max = (1 << $wnd) - 1;
  76. $dbl = $max === 1 ? null : $this->dbl();
  77. for($i = 1; $i < $max; $i++)
  78. array_push($res, $res[$i - 1]->add($dbl));
  79. return array(
  80. "wnd" => $wnd,
  81. "points" => $res
  82. );
  83. }
  84. public function _getBeta() {
  85. return null;
  86. }
  87. public function dblp($k)
  88. {
  89. $r = $this;
  90. for($i = 0; $i < $k; $i++)
  91. $r = $r->dbl();
  92. return $r;
  93. }
  94. }
  95. ?>