29 protected function __construct()
31 $this->params[
'q'] = gmp_sub(gmp_pow(2, 255), 19);
32 $this->params[
'l'] = gmp_add(gmp_pow(2, 252),
'27742317777372353535851937790883648493');
33 $this->params[
'd'] = gmp_mul(-121665, $this->inv(121666));
34 $this->params[
'I'] = gmp_powm(
36 gmp_div_q(gmp_sub($this->params[
'q'], 1), 4),
39 $By = gmp_mul(4, $this->inv(5));
40 $Bx = $this->xrecover($By);
41 $this->params[
'B'] = array($Bx, $By);
44 public static function getInstance()
46 if (self::$instance === null) {
47 self::$instance =
new static();
49 return self::$instance;
52 public function __get($name)
54 return $this->params[$name];
57 public function __isset($name)
59 return isset($this->params[$name]);
62 protected function inv($x)
64 return gmp_powm($x, gmp_sub($this->params[
'q'], 2), $this->params[
'q']);
67 public function xrecover($y)
70 gmp_sub(gmp_mul($y, $y), 1),
71 $this->inv(gmp_add(gmp_mul(gmp_mul($this->params[
'd'], $y), $y), 1))
73 $x = gmp_powm($xx, gmp_div_q(gmp_add($this->params[
'q'], 3), 8), $this->params[
'q']);
74 $t = gmp_mod(gmp_sub(gmp_mul($x, $x), $xx), $this->params[
'q']);
76 $x = gmp_mod(gmp_mul($x, $this->params[
'I']), $this->params[
'q']);
78 if (gmp_cmp(gmp_mod($x, 2), 0)) {
79 $x = gmp_sub($this->params[
'q'], $x);
84 public function edwards($P, $Q)
99 gmp_add(gmp_mul($x1, $y2), gmp_mul($x2, $y1)),
100 $this->inv(gmp_add(1, $t))
103 gmp_add(gmp_mul($y1, $y2), gmp_mul($x1, $x2)),
104 $this->inv(gmp_sub(1, $t))
108 gmp_mod($x3, $this->params[
'q']),
109 gmp_mod($y3, $this->params[
'q'])
113 public function scalarmult($P, $e)
116 throw new \InvalidArgumentException();
119 foreach (array($P[0], $P[1], $e) as $t) {
120 if (!((is_resource($t) && get_resource_type($t) ===
'GMP integer') ||
121 (is_object($t) && ($t instanceof \GMP)))) {
122 throw new \InvalidArgumentException();
126 $s = gmp_strval($e, 2);
128 $res = array(gmp_init(0), gmp_init(1));
130 for ($i = 0; $i < $len; $i++) {
131 $res = $this->edwards($res, $res);
132 if ($s[$i] ===
'1') {
133 $res = $this->edwards($res, $P);
$params
Holds various parameters for the curve (q, l, d, I, B).
static $instance
Singleton instance.