SignerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace kornrunner;
  3. use kornrunner\Serializer\HexPrivateKeySerializer;
  4. use kornrunner\Signature\Signer;
  5. use Mdanter\Ecc\Curves\CurveFactory;
  6. use Mdanter\Ecc\Curves\SecgCurve;
  7. use Mdanter\Ecc\EccFactory;
  8. use Mdanter\Ecc\Random\RandomGeneratorFactory;
  9. class SignerTest extends TestCase
  10. {
  11. public function testSign() {
  12. $adapter = EccFactory::getAdapter();
  13. $generator = CurveFactory::getGeneratorByName(SecgCurve::NAME_SECP_256K1);
  14. $deserializer = new HexPrivateKeySerializer($generator);
  15. $key = $deserializer->parse($this->testPrivateKey);
  16. $hash = gmp_init('98d22cdb65bbf8a392180cd2ee892b0a971c47e7d29daf31a3286d006b9db4dc', 16);
  17. $random = RandomGeneratorFactory::getHmacRandomGenerator($key, $hash, 'sha256');
  18. $n = $generator->getOrder();
  19. $randomK = $random->generate($n);
  20. $options = [
  21. 'n' => $n,
  22. 'canonical' => true
  23. ];
  24. $signer = new Signer($adapter, $options);
  25. $signature = $signer->sign($key, $hash, $randomK);
  26. $this->assertEquals('f67118680df5993e8efca4d3ecc4172ca4ac5e3e007ea774293e373864809703', gmp_strval($signature->getR(), 16));
  27. $this->assertEquals('47427f3633371c1a30abbb2b717dbd78ef63d5b19b5a951f9d681cccdd520320', gmp_strval($signature->getS(), 16));
  28. $this->assertTrue(in_array($signature->getRecoveryParam(), [0, 1]));
  29. $this->assertTrue($signer->verify($key->getPublicKey(), $signature, $hash));
  30. $options['canonical'] = false;
  31. $signer = new Signer($adapter, $options);
  32. $signature = $signer->sign($key, $hash, $randomK);
  33. $this->assertEquals('f67118680df5993e8efca4d3ecc4172ca4ac5e3e007ea774293e373864809703', gmp_strval($signature->getR(), 16));
  34. $this->assertEquals('b8bd80c9ccc8e3e5cf5444d48e824285cb4b073513ee0b1c226a41bff2e43e21', gmp_strval($signature->getS(), 16));
  35. $this->assertTrue(in_array($signature->getRecoveryParam(), [0, 1]));
  36. $this->assertTrue($signer->verify($key->getPublicKey(), $signature, $hash));
  37. }
  38. public function testVerify() {
  39. $adapter = EccFactory::getAdapter();
  40. $generator = CurveFactory::getGeneratorByName(SecgCurve::NAME_SECP_256K1);
  41. $deserializer = new HexPrivateKeySerializer($generator);
  42. $key = $deserializer->parse($this->testPrivateKey);
  43. $hash = gmp_init('98d22cdb65bbf8a392180cd2ee892b0a971c47e7d29daf31a3286d006b9db4dc', 16);
  44. $random = RandomGeneratorFactory::getHmacRandomGenerator($key, $hash, 'sha256');
  45. $n = $generator->getOrder();
  46. $randomK = $random->generate($n);
  47. $options = [
  48. 'n' => $n,
  49. 'canonical' => true
  50. ];
  51. $signer = new Signer($adapter, $options);
  52. $signature = $signer->sign($key, $hash, $randomK);
  53. $this->assertTrue($signer->verify($key->getPublicKey(), $signature, $hash));
  54. $options['canonical'] = false;
  55. $signer = new Signer($adapter, $options);
  56. $signature = $signer->sign($key, $hash, $randomK);
  57. $this->assertTrue($signer->verify($key->getPublicKey(), $signature, $hash));
  58. }
  59. }