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.
19 "github.com/vapor/crypto/ed25519/internal/edwards25519"
22 type zeroReader struct{}
24 func (zeroReader) Read(buf []byte) (int, error) {
31 func TestUnmarshalMarshal(t *testing.T) {
32 pub, _, _ := GenerateKey(rand.Reader)
34 var A edwards25519.ExtendedGroupElement
36 copy(pubBytes[:], pub)
37 if !A.FromBytes(&pubBytes) {
38 t.Fatalf("ExtendedGroupElement.FromBytes failed")
45 t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", pubBytes, pub2)
49 func TestSignVerify(t *testing.T) {
51 public, private, _ := GenerateKey(zero)
53 message := []byte("test message")
54 sig := Sign(private, message)
55 if !Verify(public, message, sig) {
56 t.Errorf("valid signature rejected")
59 wrongMessage := []byte("wrong message")
60 if Verify(public, wrongMessage, sig) {
61 t.Errorf("signature of different message accepted")
65 func TestCryptoSigner(t *testing.T) {
67 public, private, _ := GenerateKey(zero)
69 signer := crypto.Signer(private)
71 publicInterface := signer.Public()
72 public2, ok := publicInterface.(PublicKey)
74 t.Fatalf("expected PublicKey from Public() but got %T", publicInterface)
77 if !bytes.Equal(public, public2) {
78 t.Errorf("public keys do not match: original:%x vs Public():%x", public, public2)
81 message := []byte("message")
82 var noHash crypto.Hash
83 signature, err := signer.Sign(zero, message, noHash)
85 t.Fatalf("error from Sign(): %s", err)
88 if !Verify(public, message, signature) {
89 t.Errorf("Verify failed on signature from Sign()")
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")
100 defer testDataZ.Close()
101 testData, err := gzip.NewReader(testDataZ)
105 defer testData.Close()
107 scanner := bufio.NewScanner(testData)
113 line := scanner.Text()
114 parts := strings.Split(line, ":")
116 t.Fatalf("bad number of parts on line %d", lineNo)
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]
127 if l := len(pubKey); l != PublicKeySize {
128 t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
131 var priv [PrivateKeySize]byte
132 copy(priv[:], privBytes)
133 copy(priv[32:], pubKey)
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)
140 if !Verify(pubKey, msg, sig2) {
141 t.Errorf("signature failed to verify on line %d", lineNo)
145 if err := scanner.Err(); err != nil {
146 t.Fatalf("error reading test data: %s", err)
150 func BenchmarkKeyGeneration(b *testing.B) {
152 for i := 0; i < b.N; i++ {
153 if _, _, err := GenerateKey(zero); err != nil {
159 func BenchmarkSigning(b *testing.B) {
161 _, priv, err := GenerateKey(zero)
165 message := []byte("Hello, world!")
167 for i := 0; i < b.N; i++ {
172 func BenchmarkVerification(b *testing.B) {
174 pub, priv, err := GenerateKey(zero)
178 message := []byte("Hello, world!")
179 signature := Sign(priv, message)
181 for i := 0; i < b.N; i++ {
182 Verify(pub, message, signature)