github.com/cloudflare/circl@v1.5.0/sign/ed25519/ed25519_test.go (about) 1 package ed25519_test 2 3 import ( 4 "testing" 5 6 "github.com/cloudflare/circl/sign/ed25519" 7 ) 8 9 type zeroReader struct{} 10 11 func (zeroReader) Read(buf []byte) (int, error) { 12 for i := range buf { 13 buf[i] = 0 14 } 15 return len(buf), nil 16 } 17 18 func TestMalleability(t *testing.T) { 19 // https://tools.ietf.org/html/rfc8032#section-5.1.7 adds an additional test 20 // that s be in [0, order). This prevents someone from adding a multiple of 21 // order to s and obtaining a second valid signature for the same message. 22 msg := []byte{0x54, 0x65, 0x73, 0x74} 23 sig := []byte{ 24 0x7c, 0x38, 0xe0, 0x26, 0xf2, 0x9e, 0x14, 0xaa, 0xbd, 0x05, 0x9a, 25 0x0f, 0x2d, 0xb8, 0xb0, 0xcd, 0x78, 0x30, 0x40, 0x60, 0x9a, 0x8b, 26 0xe6, 0x84, 0xdb, 0x12, 0xf8, 0x2a, 0x27, 0x77, 0x4a, 0xb0, 0x67, 27 0x65, 0x4b, 0xce, 0x38, 0x32, 0xc2, 0xd7, 0x6f, 0x8f, 0x6f, 0x5d, 28 0xaf, 0xc0, 0x8d, 0x93, 0x39, 0xd4, 0xee, 0xf6, 0x76, 0x57, 0x33, 29 0x36, 0xa5, 0xc5, 0x1e, 0xb6, 0xf9, 0x46, 0xb3, 0x1d, 30 } 31 publicKey := []byte{ 32 0x7d, 0x4d, 0x0e, 0x7f, 0x61, 0x53, 0xa6, 0x9b, 0x62, 0x42, 0xb5, 33 0x22, 0xab, 0xbe, 0xe6, 0x85, 0xfd, 0xa4, 0x42, 0x0f, 0x88, 0x34, 34 0xb1, 0x08, 0xc3, 0xbd, 0xae, 0x36, 0x9e, 0xf5, 0x49, 0xfa, 35 } 36 37 if ed25519.Verify(publicKey, msg, sig) { 38 t.Fatal("non-canonical signature accepted") 39 } 40 } 41 42 func TestPublic(t *testing.T) { 43 var zero zeroReader 44 pub, priv, err := ed25519.GenerateKey(zero) 45 if err != nil { 46 t.Fatal(err) 47 } 48 if !priv.Equal(priv) { 49 t.FailNow() 50 } 51 if !pub.Equal(pub) { 52 t.FailNow() 53 } 54 pub2 := priv.Public() 55 if !pub.Equal(pub2) { 56 t.FailNow() 57 } 58 } 59 60 func BenchmarkKeyGeneration(b *testing.B) { 61 var zero zeroReader 62 for i := 0; i < b.N; i++ { 63 if _, _, err := ed25519.GenerateKey(zero); err != nil { 64 b.Fatal(err) 65 } 66 } 67 } 68 69 func BenchmarkNewKeyFromSeed(b *testing.B) { 70 seed := make([]byte, ed25519.SeedSize) 71 b.ReportAllocs() 72 for i := 0; i < b.N; i++ { 73 _ = ed25519.NewKeyFromSeed(seed) 74 } 75 } 76 77 func BenchmarkSigning(b *testing.B) { 78 var zero zeroReader 79 _, priv, err := ed25519.GenerateKey(zero) 80 if err != nil { 81 b.Fatal(err) 82 } 83 message := []byte("Hello, world!") 84 b.ReportAllocs() 85 b.ResetTimer() 86 for i := 0; i < b.N; i++ { 87 ed25519.Sign(priv, message) 88 } 89 } 90 91 func BenchmarkVerification(b *testing.B) { 92 var zero zeroReader 93 pub, priv, err := ed25519.GenerateKey(zero) 94 if err != nil { 95 b.Fatal(err) 96 } 97 message := []byte("Hello, world!") 98 signature := ed25519.Sign(priv, message) 99 b.ResetTimer() 100 for i := 0; i < b.N; i++ { 101 ed25519.Verify(pub, message, signature) 102 } 103 }