Utils.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Elliptic;
  3. use \Exception;
  4. use BN\BN;
  5. class Utils
  6. {
  7. public static function toArray($msg, $enc = false)
  8. {
  9. if( is_array($msg) )
  10. return array_slice($msg, 0);
  11. if( !$msg )
  12. return array();
  13. if( !is_string($msg) )
  14. throw new Exception("Not implemented");
  15. if( !$enc )
  16. return array_slice(unpack("C*", $msg), 0);
  17. if( $enc === "hex" )
  18. return array_slice(unpack("C*", hex2bin($msg)), 0);
  19. return $msg;
  20. }
  21. public static function toHex($msg)
  22. {
  23. if( is_string($msg) )
  24. return bin2hex($msg);
  25. if( !is_array($msg) )
  26. throw new Exception("Not implemented");
  27. $binary = call_user_func_array("pack", array_merge(["C*"], $msg));
  28. return bin2hex($binary);
  29. }
  30. public static function toBin($msg, $enc = false)
  31. {
  32. if( is_array($msg) )
  33. return call_user_func_array("pack", array_merge(["C*"], $msg));
  34. if( $enc === "hex" )
  35. return hex2bin($msg);
  36. return $msg;
  37. }
  38. public static function encode($arr, $enc)
  39. {
  40. if( $enc === "hex" )
  41. return self::toHex($arr);
  42. return $arr;
  43. }
  44. // Represent num in a w-NAF form
  45. public static function getNAF($num, $w)
  46. {
  47. $naf = array();
  48. $ws = 1 << ($w + 1);
  49. $k = clone($num);
  50. while( $k->cmpn(1) >= 0 )
  51. {
  52. if( !$k->isOdd() )
  53. array_push($naf, 0);
  54. else
  55. {
  56. $mod = $k->andln($ws - 1);
  57. $z = $mod;
  58. if( $mod > (($ws >> 1) - 1))
  59. $z = ($ws >> 1) - $mod;
  60. $k->isubn($z);
  61. array_push($naf, $z);
  62. }
  63. // Optimization, shift by word if possible
  64. $shift = (!$k->isZero() && $k->andln($ws - 1) === 0) ? ($w + 1) : 1;
  65. for($i = 1; $i < $shift; $i++)
  66. array_push($naf, 0);
  67. $k->iushrn($shift);
  68. }
  69. return $naf;
  70. }
  71. // Represent k1, k2 in a Joint Sparse Form
  72. public static function getJSF($k1, $k2)
  73. {
  74. $jsf = array( array(), array() );
  75. $k1 = $k1->_clone();
  76. $k2 = $k2->_clone();
  77. $d1 = 0;
  78. $d2 = 0;
  79. while( $k1->cmpn(-$d1) > 0 || $k2->cmpn(-$d2) > 0 )
  80. {
  81. // First phase
  82. $m14 = ($k1->andln(3) + $d1) & 3;
  83. $m24 = ($k2->andln(3) + $d2) & 3;
  84. if( $m14 === 3 )
  85. $m14 = -1;
  86. if( $m24 === 3 )
  87. $m24 = -1;
  88. $u1 = 0;
  89. if( ($m14 & 1) !== 0 )
  90. {
  91. $m8 = ($k1->andln(7) + $d1) & 7;
  92. $u1 = ( ($m8 === 3 || $m8 === 5) && $m24 === 2 ) ? -$m14 : $m14;
  93. }
  94. array_push($jsf[0], $u1);
  95. $u2 = 0;
  96. if( ($m24 & 1) !== 0 )
  97. {
  98. $m8 = ($k2->andln(7) + $d2) & 7;
  99. $u2 = ( ($m8 === 3 || $m8 === 5) && $m14 === 2 ) ? -$m24 : $m24;
  100. }
  101. array_push($jsf[1], $u2);
  102. // Second phase
  103. if( (2 * $d1) === ($u1 + 1) )
  104. $d1 = 1 - $d1;
  105. if( (2 * $d2) === ($u2 + 1) )
  106. $d2 = 1 - $d2;
  107. $k1->iushrn(1);
  108. $k2->iushrn(1);
  109. }
  110. return $jsf;
  111. }
  112. public static function intFromLE($bytes) {
  113. return new BN($bytes, 'hex', 'le');
  114. }
  115. public static function parseBytes($bytes) {
  116. if (is_string($bytes))
  117. return self::toArray($bytes, 'hex');
  118. return $bytes;
  119. }
  120. public static function randBytes($count)
  121. {
  122. $res = "";
  123. for($i = 0; $i < $count; $i++)
  124. $res .= chr(rand(0, 255));
  125. return $res;
  126. }
  127. public static function optionAssert(&$array, $key, $value = false, $required = false)
  128. {
  129. if( isset($array[$key]) )
  130. return;
  131. if( $required )
  132. throw new Exception("Missing option " . $key);
  133. $array[$key] = $value;
  134. }
  135. }
  136. ?>