You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.0 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/tendermint/ed25519"
  6. "github.com/tendermint/go-wire"
  7. )
  8. func TestSignAndValidateEd25519(t *testing.T) {
  9. privKey := GenPrivKeyEd25519()
  10. pubKey := privKey.PubKey()
  11. msg := CRandBytes(128)
  12. sig := privKey.Sign(msg)
  13. t.Logf("msg: %X, sig: %X", msg, sig)
  14. // Test the signature
  15. if !pubKey.VerifyBytes(msg, sig) {
  16. t.Errorf("Account message signature verification failed")
  17. }
  18. // Mutate the signature, just one bit.
  19. sigEd := sig.(SignatureEd25519)
  20. sigEd[0] ^= byte(0x01)
  21. sig = Signature(sigEd)
  22. if pubKey.VerifyBytes(msg, sig) {
  23. t.Errorf("Account message signature verification should have failed but passed instead")
  24. }
  25. }
  26. func TestSignAndValidateSecp256k1(t *testing.T) {
  27. privKey := GenPrivKeySecp256k1()
  28. pubKey := privKey.PubKey()
  29. msg := CRandBytes(128)
  30. sig := privKey.Sign(msg)
  31. t.Logf("msg: %X, sig: %X", msg, sig)
  32. // Test the signature
  33. if !pubKey.VerifyBytes(msg, sig) {
  34. t.Errorf("Account message signature verification failed")
  35. }
  36. // Mutate the signature, just one bit.
  37. sigEd := sig.(SignatureSecp256k1)
  38. sigEd[0] ^= byte(0x01)
  39. sig = Signature(sigEd)
  40. if pubKey.VerifyBytes(msg, sig) {
  41. t.Errorf("Account message signature verification should have failed but passed instead")
  42. }
  43. }
  44. func TestBinaryDecodeEd25519(t *testing.T) {
  45. privKey := GenPrivKeyEd25519()
  46. pubKey := privKey.PubKey()
  47. msg := CRandBytes(128)
  48. sig := privKey.Sign(msg)
  49. t.Logf("msg: %X, sig: %X", msg, sig)
  50. buf, n, err := new(bytes.Buffer), new(int), new(error)
  51. wire.WriteBinary(struct{ Signature }{sig}, buf, n, err)
  52. if *err != nil {
  53. t.Fatalf("Failed to write Signature: %v", err)
  54. }
  55. if len(buf.Bytes()) != ed25519.SignatureSize+1 {
  56. // 1 byte TypeByte, 64 bytes signature bytes
  57. t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
  58. }
  59. if buf.Bytes()[0] != SignatureTypeEd25519 {
  60. t.Fatalf("Unexpected signature type byte")
  61. }
  62. sigStruct := struct{ Signature }{}
  63. sig2 := wire.ReadBinary(sigStruct, buf, 0, n, err)
  64. if *err != nil {
  65. t.Fatalf("Failed to read Signature: %v", err)
  66. }
  67. // Test the signature
  68. if !pubKey.VerifyBytes(msg, sig2.(struct{ Signature }).Signature.(SignatureEd25519)) {
  69. t.Errorf("Account message signature verification failed")
  70. }
  71. }
  72. func TestBinaryDecodeSecp256k1(t *testing.T) {
  73. privKey := GenPrivKeySecp256k1()
  74. pubKey := privKey.PubKey()
  75. msg := CRandBytes(128)
  76. sig := privKey.Sign(msg)
  77. t.Logf("msg: %X, sig: %X", msg, sig)
  78. buf, n, err := new(bytes.Buffer), new(int), new(error)
  79. wire.WriteBinary(struct{ Signature }{sig}, buf, n, err)
  80. if *err != nil {
  81. t.Fatalf("Failed to write Signature: %v", err)
  82. }
  83. if buf.Bytes()[0] != SignatureTypeSecp256k1 {
  84. t.Fatalf("Unexpected signature type byte")
  85. }
  86. sigStruct := struct{ Signature }{}
  87. sig2 := wire.ReadBinary(sigStruct, buf, 0, n, err)
  88. if *err != nil {
  89. t.Fatalf("Failed to read Signature: %v", err)
  90. }
  91. // Test the signature
  92. if !pubKey.VerifyBytes(msg, sig2.(struct{ Signature }).Signature.(SignatureSecp256k1)) {
  93. t.Errorf("Account message signature verification failed")
  94. }
  95. }