Secp256k1.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php declare(strict_types=1);
  2. namespace kornrunner;
  3. use InvalidArgumentException;
  4. use kornrunner\Serializer\HexPrivateKeySerializer;
  5. use kornrunner\Signature\Signer;
  6. use Mdanter\Ecc\Crypto\Signature\SignatureInterface;
  7. use Mdanter\Ecc\Curves\CurveFactory;
  8. use Mdanter\Ecc\Curves\SecgCurve;
  9. use Mdanter\Ecc\EccFactory;
  10. use Mdanter\Ecc\Primitives\PointInterface;
  11. use Mdanter\Ecc\Random\RandomGeneratorFactory;
  12. class Secp256k1
  13. {
  14. protected $adapter;
  15. protected $generator;
  16. protected $curve;
  17. protected $deserializer;
  18. protected $algorithm;
  19. public function __construct(string $hashAlgorithm='sha256') {
  20. $this->adapter = EccFactory::getAdapter();
  21. $this->generator = CurveFactory::getGeneratorByName(SecgCurve::NAME_SECP_256K1);
  22. $this->curve = $this->generator->getCurve();
  23. $this->deserializer = new HexPrivateKeySerializer($this->generator);
  24. $this->algorithm = $hashAlgorithm;
  25. }
  26. public function sign(string $hash, string $privateKey, array $options=[]): SignatureInterface {
  27. $key = $this->deserializer->parse($privateKey);
  28. $hex_hash = gmp_init($hash, 16);
  29. if (!isset($options['n'])) {
  30. $random = RandomGeneratorFactory::getHmacRandomGenerator($key, $hex_hash, $this->algorithm);
  31. $n = $this->generator->getOrder();
  32. $randomK = $random->generate($n);
  33. $options['n'] = $n;
  34. }
  35. if (!isset($options['canonical'])) {
  36. $options['canonical'] = true;
  37. }
  38. $signer = new Signer($this->adapter, $options);
  39. return $signer->sign($key, $hex_hash, $randomK);
  40. }
  41. public function verify(string $hash, SignatureInterface $signature, string $publicKey): bool
  42. {
  43. $gmpKey = $this->decodePoint($publicKey);
  44. $key = $this->generator->getPublickeyFrom($gmpKey->getX(), $gmpKey->getY());
  45. $hex_hash = gmp_init($hash, 16);
  46. $signer = new Signer($this->adapter);
  47. return $signer->verify($key, $signature, $hex_hash);
  48. }
  49. protected function decodePoint(string $publicKey): PointInterface
  50. {
  51. $order = $this->generator->getOrder();
  52. $orderString = gmp_strval($order, 16);
  53. $length = mb_strlen($orderString);
  54. $keyLength = mb_strlen($publicKey);
  55. $num = hexdec(mb_substr($publicKey, 0, 2));
  56. if (
  57. ($num === 4 || $num === 6 || $num === 7) &&
  58. ($length * 2 + 2) === $keyLength
  59. ) {
  60. $x = gmp_init(mb_substr($publicKey, 2, $length), 16);
  61. $y = gmp_init(mb_substr($publicKey, ($length + 2), $length), 16);
  62. if ($this->generator->isValid($x, $y) !== true) {
  63. throw new InvalidArgumentException('Invalid public key point x and y.');
  64. }
  65. return $this->curve->getPoint($x, $y, $order);
  66. } elseif (
  67. ($num === 2 || $num === 3) &&
  68. ($length + 2) === $keyLength
  69. ) {
  70. $x = gmp_init(mb_substr($publicKey, 2, $length), 16);
  71. $y = $this->curve->recoverYfromX($num === 3, $x);
  72. return $this->curve->getPoint($x, $y, $order);
  73. }
  74. throw new InvalidArgumentException('Invalid public key point format.');
  75. }
  76. }