1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file at
4 // https://github.com/golang/crypto/blob/master/LICENSE.
18 "github.com/vapor/crypto/ed25519/internal/edwards25519"
21 type zeroReader struct{}
23 func (zeroReader) Read(buf []byte) (int, error) {
30 func TestUnmarshalMarshal(t *testing.T) {
31 pub, _, _ := GenerateKey(rand.Reader)
33 var A edwards25519.ExtendedGroupElement
35 copy(pubBytes[:], pub)
36 if !A.FromBytes(&pubBytes) {
37 t.Fatalf("ExtendedGroupElement.FromBytes failed")
44 t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", pubBytes, pub2)
48 func TestSignVerify(t *testing.T) {
50 public, private, _ := GenerateKey(zero)
52 message := []byte("test message")
53 sig := Sign(private, message)
54 if !Verify(public, message, sig) {
55 t.Errorf("valid signature rejected")
58 wrongMessage := []byte("wrong message")
59 if Verify(public, wrongMessage, sig) {
60 t.Errorf("signature of different message accepted")
64 func TestGolden(t *testing.T) {
65 // sign.input.gz is a selection of test cases from
66 // http://ed25519.cr.yp.to/python/sign.input
67 testDataZ, err := os.Open("testdata/sign.input.gz")
71 defer testDataZ.Close()
72 testData, err := gzip.NewReader(testDataZ)
76 defer testData.Close()
78 scanner := bufio.NewScanner(testData)
84 line := scanner.Text()
85 parts := strings.Split(line, ":")
87 t.Fatalf("bad number of parts on line %d", lineNo)
90 privBytes, _ := hex.DecodeString(parts[0])
91 pubKey, _ := hex.DecodeString(parts[1])
92 msg, _ := hex.DecodeString(parts[2])
93 sig, _ := hex.DecodeString(parts[3])
94 // The signatures in the test vectors also include the message
95 // at the end, but we just want R and S.
96 sig = sig[:SignatureSize]
98 if l := len(pubKey); l != PublicKeySize {
99 t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
102 var priv [PrivateKeySize]byte
103 copy(priv[:], privBytes)
104 copy(priv[32:], pubKey)
106 sig2 := Sign(priv[:], msg)
107 if !bytes.Equal(sig, sig2[:]) {
108 t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
111 if !Verify(pubKey, msg, sig2) {
112 t.Errorf("signature failed to verify on line %d", lineNo)
116 if err := scanner.Err(); err != nil {
117 t.Fatalf("error reading test data: %s", err)
121 func BenchmarkKeyGeneration(b *testing.B) {
123 for i := 0; i < b.N; i++ {
124 if _, _, err := GenerateKey(zero); err != nil {
130 func BenchmarkSigning(b *testing.B) {
132 _, priv, err := GenerateKey(zero)
136 message := []byte("Hello, world!")
138 for i := 0; i < b.N; i++ {
143 func BenchmarkVerification(b *testing.B) {
145 pub, priv, err := GenerateKey(zero)
149 message := []byte("Hello, world!")
150 signature := Sign(priv, message)
152 for i := 0; i < b.N; i++ {
153 Verify(pub, message, signature)