PointCodecTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. require_once __DIR__ . "/../vendor/autoload.php";
  3. class PointCodecTest extends \PHPUnit\Framework\TestCase {
  4. function makeShortTest($definition) {
  5. $curve = \Elliptic\Curves::getCurve("secp256k1")->curve;
  6. return function() use($curve, $definition) {
  7. $co = $definition["coordinates"];
  8. $p = $curve->point($co["x"], $co["y"]);
  9. // Encodes as expected
  10. $this->assertEquals($p->encode('hex'), $definition["encoded"]);
  11. $this->assertEquals($p->encodeCompressed('hex'), $definition["compactEncoded"]);
  12. // Decodes as expected
  13. $this->assertTrue($curve->decodePoint($definition["encoded"], 'hex')->eq($p));
  14. $this->assertTrue($curve->decodePoint($definition["compactEncoded"], 'hex')->eq($p));
  15. $this->assertTrue($curve->decodePoint($definition["hybrid"], 'hex')->eq($p));
  16. };
  17. }
  18. function makeMontTest($definition) {
  19. $curve = \Elliptic\Curves::getCurve("curve25519")->curve;
  20. return function() use ($definition, $curve) {
  21. $co = $definition["coordinates"];
  22. $p = $curve->point($co["x"], $co["z"]);
  23. $encoded = $p->encode('hex');
  24. $decoded = $curve->decodePoint($encoded, 'hex');
  25. $this->assertTrue($decoded->eq($p));
  26. $this->assertEquals($encoded, $definition["encoded"]);
  27. };
  28. }
  29. static $shortPointEvenY;
  30. static $shortPointOddY;
  31. public function test_should_throw_when_trying_to_decode_random_bytes() {
  32. $this->expectException(\Exception::class);
  33. \Elliptic\Curves::getCurve("secp256k1")->curve->decodePoint(
  34. '05' .
  35. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798');
  36. }
  37. public function test_should_be_able_to_encode_and_decode_a_short_curve_point_with_even_Y() {
  38. $f = $this->makeShortTest(self::$shortPointEvenY);
  39. $f();
  40. }
  41. public function test_should_be_able_to_encode_and_decode_a_short_curve_point_with_odd_Y() {
  42. $f = $this->makeShortTest(self::$shortPointOddY);
  43. $f();
  44. }
  45. public function test_should_be_able_to_encode_and_decode_a_mont_curve_point() {
  46. $f = $this->makeMontTest([
  47. "coordinates" => [
  48. // curve25519.curve.g.mul(new BN('6')).getX().toString(16, 2)
  49. "x" => '26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531',
  50. "z" => '1'
  51. ],
  52. "encoded" =>
  53. '26954ccdc99ebf34f8f1dde5e6bb080685fec73640494c28f9fe0bfa8c794531'
  54. ]);
  55. $f();
  56. }
  57. }
  58. PointCodecTest::$shortPointEvenY = [
  59. "coordinates" => [
  60. "x" => '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
  61. "y" => '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
  62. ],
  63. "compactEncoded" =>
  64. '02' .
  65. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
  66. "encoded" =>
  67. '04' .
  68. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' .
  69. '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
  70. "hybrid" =>
  71. '06' .
  72. '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' .
  73. '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8'
  74. ];
  75. PointCodecTest::$shortPointOddY = [
  76. "coordinates" => [
  77. "x" => 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556',
  78. "y" => 'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297'
  79. ],
  80. "compactEncoded" =>
  81. '03' .
  82. 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556',
  83. "encoded" =>
  84. '04' .
  85. 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' .
  86. 'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297',
  87. "hybrid" =>
  88. '07' .
  89. 'fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556' .
  90. 'ae12777aacfbb620f3be96017f45c560de80f0f6518fe4a03c870c36b075f297'
  91. ];