OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / crypto / ed25519 / ed25519_test.go
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.
5
6 package ed25519
7
8 import (
9         "bufio"
10         "bytes"
11         "compress/gzip"
12         "crypto"
13         "crypto/rand"
14         "encoding/hex"
15         "os"
16         "strings"
17         "testing"
18
19         "github.com/vapor/crypto/ed25519/internal/edwards25519"
20 )
21
22 type zeroReader struct{}
23
24 func (zeroReader) Read(buf []byte) (int, error) {
25         for i := range buf {
26                 buf[i] = 0
27         }
28         return len(buf), nil
29 }
30
31 func TestUnmarshalMarshal(t *testing.T) {
32         pub, _, _ := GenerateKey(rand.Reader)
33
34         var A edwards25519.ExtendedGroupElement
35         var pubBytes [32]byte
36         copy(pubBytes[:], pub)
37         if !A.FromBytes(&pubBytes) {
38                 t.Fatalf("ExtendedGroupElement.FromBytes failed")
39         }
40
41         var pub2 [32]byte
42         A.ToBytes(&pub2)
43
44         if pubBytes != pub2 {
45                 t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", pubBytes, pub2)
46         }
47 }
48
49 func TestSignVerify(t *testing.T) {
50         var zero zeroReader
51         public, private, _ := GenerateKey(zero)
52
53         message := []byte("test message")
54         sig := Sign(private, message)
55         if !Verify(public, message, sig) {
56                 t.Errorf("valid signature rejected")
57         }
58
59         wrongMessage := []byte("wrong message")
60         if Verify(public, wrongMessage, sig) {
61                 t.Errorf("signature of different message accepted")
62         }
63 }
64
65 func TestCryptoSigner(t *testing.T) {
66         var zero zeroReader
67         public, private, _ := GenerateKey(zero)
68
69         signer := crypto.Signer(private)
70
71         publicInterface := signer.Public()
72         public2, ok := publicInterface.(PublicKey)
73         if !ok {
74                 t.Fatalf("expected PublicKey from Public() but got %T", publicInterface)
75         }
76
77         if !bytes.Equal(public, public2) {
78                 t.Errorf("public keys do not match: original:%x vs Public():%x", public, public2)
79         }
80
81         message := []byte("message")
82         var noHash crypto.Hash
83         signature, err := signer.Sign(zero, message, noHash)
84         if err != nil {
85                 t.Fatalf("error from Sign(): %s", err)
86         }
87
88         if !Verify(public, message, signature) {
89                 t.Errorf("Verify failed on signature from Sign()")
90         }
91 }
92
93 func TestGolden(t *testing.T) {
94         // sign.input.gz is a selection of test cases from
95         // http://ed25519.cr.yp.to/python/sign.input
96         testDataZ, err := os.Open("testdata/sign.input.gz")
97         if err != nil {
98                 t.Fatal(err)
99         }
100         defer testDataZ.Close()
101         testData, err := gzip.NewReader(testDataZ)
102         if err != nil {
103                 t.Fatal(err)
104         }
105         defer testData.Close()
106
107         scanner := bufio.NewScanner(testData)
108         lineNo := 0
109
110         for scanner.Scan() {
111                 lineNo++
112
113                 line := scanner.Text()
114                 parts := strings.Split(line, ":")
115                 if len(parts) != 5 {
116                         t.Fatalf("bad number of parts on line %d", lineNo)
117                 }
118
119                 privBytes, _ := hex.DecodeString(parts[0])
120                 pubKey, _ := hex.DecodeString(parts[1])
121                 msg, _ := hex.DecodeString(parts[2])
122                 sig, _ := hex.DecodeString(parts[3])
123                 // The signatures in the test vectors also include the message
124                 // at the end, but we just want R and S.
125                 sig = sig[:SignatureSize]
126
127                 if l := len(pubKey); l != PublicKeySize {
128                         t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
129                 }
130
131                 var priv [PrivateKeySize]byte
132                 copy(priv[:], privBytes)
133                 copy(priv[32:], pubKey)
134
135                 sig2 := Sign(priv[:], msg)
136                 if !bytes.Equal(sig, sig2[:]) {
137                         t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
138                 }
139
140                 if !Verify(pubKey, msg, sig2) {
141                         t.Errorf("signature failed to verify on line %d", lineNo)
142                 }
143         }
144
145         if err := scanner.Err(); err != nil {
146                 t.Fatalf("error reading test data: %s", err)
147         }
148 }
149
150 func BenchmarkKeyGeneration(b *testing.B) {
151         var zero zeroReader
152         for i := 0; i < b.N; i++ {
153                 if _, _, err := GenerateKey(zero); err != nil {
154                         b.Fatal(err)
155                 }
156         }
157 }
158
159 func BenchmarkSigning(b *testing.B) {
160         var zero zeroReader
161         _, priv, err := GenerateKey(zero)
162         if err != nil {
163                 b.Fatal(err)
164         }
165         message := []byte("Hello, world!")
166         b.ResetTimer()
167         for i := 0; i < b.N; i++ {
168                 Sign(priv, message)
169         }
170 }
171
172 func BenchmarkVerification(b *testing.B) {
173         var zero zeroReader
174         pub, priv, err := GenerateKey(zero)
175         if err != nil {
176                 b.Fatal(err)
177         }
178         message := []byte("Hello, world!")
179         signature := Sign(priv, message)
180         b.ResetTimer()
181         for i := 0; i < b.N; i++ {
182                 Verify(pub, message, signature)
183         }
184 }