ED25519Test.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. require_once __DIR__ . "/../vendor/autoload.php";
  3. use \Elliptic\EdDSA;
  4. use \Elliptic\Utils;
  5. function toHex($arg) { return strtoupper(Utils::toHex($arg)); }
  6. class ED25519Test extends \PHPUnit\Framework\TestCase {
  7. public function derivations() {
  8. $data = json_decode( file_get_contents(__DIR__ . "/fixtures/derivation-fixtures"), true);
  9. $data = array_slice($data, 0, 50);
  10. return array_map(function($set) { return [$set]; }, $data);
  11. }
  12. /**
  13. * @dataProvider derivations
  14. */
  15. public function test_derivations_can_compute_correct_a_and_A_from_secret($test) {
  16. $ed25519 = new EdDSA("ed25519");
  17. $secret = Utils::toArray($test["secret_hex"], 'hex');
  18. $key = $ed25519->keyFromSecret($secret);
  19. $this->assertEquals( toHex($key->privBytes()), $test["a_hex"] );
  20. $xRecovered = toHex( $ed25519->encodeInt(
  21. $ed25519->decodePoint( $key->pubBytes() )->getX()) );
  22. $this->assertEquals( $xRecovered, $test["A_P"]["x"] );
  23. $this->assertEquals( toHex( $key->pubBytes() ), $test["A_hex"] );
  24. }
  25. public function signLines() {
  26. $data = file_get_contents(__DIR__ . "/fixtures/sign.input");
  27. $lines = array_filter( explode("\n", $data), function($line) { return strlen($line) > 0; });
  28. $lines = array_slice($lines, 0, 50);
  29. return array_map(function($line) { return [$line]; }, $lines);
  30. }
  31. /**
  32. * @dataProvider signLines
  33. */
  34. public function test_sign_input_test_vectors($line) {
  35. $split = explode(':', strtoupper($line));
  36. $ed25519 = new EdDSA("ed25519");
  37. $key = $ed25519->keyFromSecret(substr($split[0], 0, 64));
  38. $expectedPk = substr($split[0], 64);
  39. $this->assertEquals( toHex($key->pubBytes()), $expectedPk);
  40. $msg = Utils::toArray($split[2], 'hex');
  41. $sig = $key->sign($msg)->toHex();
  42. $sigR = substr($sig, 0, 64);
  43. $sigS = substr($sig, 64);
  44. $this->assertEquals($sigR, substr($split[3], 0, 64));
  45. $this->assertEquals($sigS, substr($split[3], 64, 64));
  46. $this->assertTrue($key->verify($msg, $sig));
  47. if (count($msg) == 0) {
  48. $forged = [ 0x78 ] /* ord('x') */;
  49. } else {
  50. $forged = $msg;
  51. $forged[count($msg) - 1] = ($msg[count($msg) - 1] + 1) % 256;
  52. }
  53. $this->assertNotTrue($key->verify($forged, $sig));
  54. }
  55. public function test_eddsa_has_encodingLength_of_32() {
  56. $ed25519 = new EdDSA("ed25519");
  57. $this->assertEquals(32, $ed25519->encodingLength);
  58. }
  59. public function test_eddsa_can_sign_and_verify_messages() {
  60. $ed25519 = new EdDSA("ed25519");
  61. $secret = array_fill(0, 32, 0);
  62. $msg = [ 0xB, 0xE, 0xE, 0xF ];
  63. $key = $ed25519->keyFromSecret($secret);
  64. $sig = $key->sign($msg)->toHex();
  65. $R = '8F1B9A7FDB22BCD2C15D4695B1CE2B063CBFAEC9B00BE360427BAC9533943F6C';
  66. $S = '5F0B380FD7F2E43B70AB2FA29F6C6E3FFC1012710E174786814012324BF19B0C';
  67. $this->assertEquals(substr($sig, 0, 64), $R);
  68. $this->assertEquals(substr($sig, 64), $S);
  69. $this->assertTrue($key->verify($msg, $sig));
  70. }
  71. static $secret = '0000000000000000000000000000000000000000000000000000000000000000';
  72. public function test_eddsa_keypair_can_be_created_with_keyFromSecret_or_keyFromPublic() {
  73. $ed25519 = new EdDSA("ed25519");
  74. $pair = $ed25519->keyFromSecret(self::$secret);
  75. $pubKey = $ed25519->keyFromPublic( toHex($pair->pubBytes()) );
  76. $this->assertTrue( is_a($pubKey->pub(), $ed25519->pointClass) );
  77. $this->assertTrue( $pubKey->pub()->eq($pair->pub()));
  78. }
  79. public function test_eddsa_keypair_getSecret_returns_bytes_with_optional_encoding() {
  80. $ed25519 = new EdDSA("ed25519");
  81. $pair = $ed25519->keyFromSecret(self::$secret);
  82. $this->assertTrue( is_array($pair->getSecret()) );
  83. $this->assertTrue( $pair->getSecret('hex') == self::$secret);
  84. }
  85. public function test_eddsa_keypair_getPub_returns_bytes_with_optional_encoding() {
  86. $ed25519 = new EdDSA("ed25519");
  87. $pair = $ed25519->keyFromSecret(self::$secret);
  88. $this->assertTrue( is_array($pair->getPublic()) );
  89. $this->assertEquals( $pair->getPublic('hex'),
  90. '3b6a27bcceb6a42d62a3a8d02a6f0d73653215771de243a63ac048a18b59da29');
  91. }
  92. }