Red.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace BN;
  3. use \Exception;
  4. use \BI\BigInteger;
  5. class Red
  6. {
  7. public $m;
  8. function __construct($m) {
  9. if( is_string($m) )
  10. $this->m = Red::primeByName($m);
  11. else
  12. $this->m = $m;
  13. if( !$this->m->gtn(1) )
  14. throw new Exception("Modulus must be greater than 1");
  15. }
  16. public static function primeByName($name)
  17. {
  18. switch($name) {
  19. case "k256":
  20. return new BN("ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f", 16);
  21. case "p224":
  22. return new BN("ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001", 16);
  23. case "p192":
  24. return new BN("ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff", 16);
  25. case "p25519":
  26. return new BN("7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed", 16);
  27. default:
  28. throw new Exception("Unknown prime name " . $name);
  29. }
  30. }
  31. public function verify1(BN $num)
  32. {
  33. if (assert_options(ASSERT_ACTIVE)) assert(!$num->negative()); //,"red works only with positives");
  34. assert($num->red); //, "red works only with red numbers");
  35. }
  36. public function verify2(BN $a, BN $b)
  37. {
  38. if (assert_options(ASSERT_ACTIVE)) assert(!$a->negative() && !$b->negative()); //, "red works only with positives");
  39. assert($a->red && ($a->red == $b->red)); //, "red works only with red numbers");
  40. }
  41. public function imod(BN &$a) {
  42. return $a->umod($this->m)->_forceRed($this);
  43. }
  44. public function neg(BN $a)
  45. {
  46. if( $a->isZero() )
  47. return $a->_clone();
  48. return $this->m->sub($a)->_forceRed($this);
  49. }
  50. public function add(BN $a, BN $b)
  51. {
  52. $this->verify2($a, $b);
  53. $res = $a->add($b);
  54. if( $res->cmp($this->m) >= 0 )
  55. $res->isub($this->m);
  56. return $res->_forceRed($this);
  57. }
  58. public function iadd(BN &$a, BN $b)
  59. {
  60. $this->verify2($a, $b);
  61. $a->iadd($b);
  62. if( $a->cmp($this->m) >= 0 )
  63. $a->isub($this->m);
  64. return $a;
  65. }
  66. public function sub(BN $a, BN $b)
  67. {
  68. $this->verify2($a, $b);
  69. $res = $a->sub($b);
  70. if( $res->negative() )
  71. $res->iadd($this->m);
  72. return $res->_forceRed($this);
  73. }
  74. public function isub(BN &$a, $b)
  75. {
  76. $this->verify2($a, $b);
  77. $a->isub($b);
  78. if( $a->negative() )
  79. $a->iadd($this->m);
  80. return $a;
  81. }
  82. public function shl(BN $a, $num) {
  83. $this->verify1($a);
  84. return $this->imod($a->ushln($num));
  85. }
  86. public function imul(BN &$a, BN $b) {
  87. $this->verify2($a, $b);
  88. $res = $a->imul($b);
  89. return $this->imod($res);
  90. }
  91. public function mul(BN $a, BN $b) {
  92. $this->verify2($a, $b);
  93. $res = $a->mul($b);
  94. return $this->imod($res);
  95. }
  96. public function sqr(BN $a) {
  97. $res = $a->_clone();
  98. return $this->imul($res, $a);
  99. }
  100. public function isqr(BN &$a) {
  101. return $this->imul($a, $a);
  102. }
  103. public function sqrt(BN $a) {
  104. if ($a->isZero())
  105. return $a->_clone();
  106. $mod3 = $this->m->andln(3);
  107. assert($mod3 % 2 == 1);
  108. // Fast case
  109. if ($mod3 == 3) {
  110. $pow = $this->m->add(new BN(1))->iushrn(2);
  111. return $this->pow($a, $pow);
  112. }
  113. // Tonelli-Shanks algorithm (Totally unoptimized and slow)
  114. //
  115. // Find Q and S, that Q * 2 ^ S = (P - 1)
  116. $q = $this->m->subn(1);
  117. $s = 0;
  118. while (!$q->isZero() && $q->andln(1) == 0) {
  119. $s++;
  120. $q->iushrn(1);
  121. }
  122. if (assert_options(ASSERT_ACTIVE)) assert(!$q->isZero());
  123. $one = (new BN(1))->toRed($this);
  124. $nOne = $one->redNeg();
  125. // Find quadratic non-residue
  126. // NOTE: Max is such because of generalized Riemann hypothesis.
  127. $lpow = $this->m->subn(1)->iushrn(1);
  128. $z = $this->m->bitLength();
  129. $z = (new BN(2 * $z * $z))->toRed($this);
  130. while ($this->pow($z, $lpow)->cmp($nOne) != 0) {
  131. $z->redIAdd($nOne);
  132. }
  133. $c = $this->pow($z, $q);
  134. $r = $this->pow($a, $q->addn(1)->iushrn(1));
  135. $t = $this->pow($a, $q);
  136. $m = $s;
  137. while ($t->cmp($one) != 0) {
  138. $tmp = $t;
  139. for ($i = 0; $tmp->cmp($one) != 0; $i++) {
  140. $tmp = $tmp->redSqr();
  141. }
  142. if ($i >= $m) {
  143. throw new \Exception("Assertion failed");
  144. }
  145. if ($m - $i - 1 > 54) {
  146. $b = $this->pow($c, (new BN(1))->iushln($m - $i - 1));
  147. } else {
  148. $b = clone($c);
  149. $b->bi = $c->bi->powMod(1 << ($m - $i - 1), $this->m->bi);
  150. }
  151. $r = $r->redMul($b);
  152. $c = $b->redSqr();
  153. $t = $t->redMul($c);
  154. $m = $i;
  155. }
  156. return $r;
  157. }
  158. public function invm(BN &$a) {
  159. $res = $a->invm($this->m);
  160. return $this->imod($res);
  161. }
  162. public function pow(BN $a, BN $num) {
  163. $r = clone($a);
  164. $r->bi = $a->bi->powMod($num->bi, $this->m->bi);
  165. return $r;
  166. }
  167. public function convertTo(BN $num) {
  168. $r = $num->umod($this->m);
  169. return $r === $num ? $r->_clone() : $r;
  170. }
  171. public function convertFrom(BN $num) {
  172. $res = $num->_clone();
  173. $res->red = null;
  174. return $res;
  175. }
  176. }
  177. ?>