1 // Copyright 2012 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.
18 "github.com/agl/ed25519/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
34 if !A.FromBytes(pub) {
35 t.Fatalf("ExtendedGroupElement.FromBytes failed")
42 t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", *pub, pub2)
46 func TestSignVerify(t *testing.T) {
48 public, private, _ := GenerateKey(zero)
50 message := []byte("test message")
51 sig := Sign(private, message)
52 if !Verify(public, message, sig) {
53 t.Errorf("valid signature rejected")
56 wrongMessage := []byte("wrong message")
57 if Verify(public, wrongMessage, sig) {
58 t.Errorf("signature of different message accepted")
62 func TestGolden(t *testing.T) {
63 // sign.input.gz is a selection of test cases from
64 // http://ed25519.cr.yp.to/python/sign.input
65 testDataZ, err := os.Open("testdata/sign.input.gz")
69 defer testDataZ.Close()
70 testData, err := gzip.NewReader(testDataZ)
74 defer testData.Close()
76 in := bufio.NewReaderSize(testData, 1<<12)
80 lineBytes, isPrefix, err := in.ReadLine()
82 t.Fatal("bufio buffer too small")
88 t.Fatalf("error reading test data: %s", err)
91 line := string(lineBytes)
92 parts := strings.Split(line, ":")
94 t.Fatalf("bad number of parts on line %d", lineNo)
97 privBytes, _ := hex.DecodeString(parts[0])
98 pubKeyBytes, _ := hex.DecodeString(parts[1])
99 msg, _ := hex.DecodeString(parts[2])
100 sig, _ := hex.DecodeString(parts[3])
101 // The signatures in the test vectors also include the message
102 // at the end, but we just want R and S.
103 sig = sig[:SignatureSize]
105 if l := len(pubKeyBytes); l != PublicKeySize {
106 t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
109 var priv [PrivateKeySize]byte
110 copy(priv[:], privBytes)
111 copy(priv[32:], pubKeyBytes)
113 sig2 := Sign(&priv, msg)
114 if !bytes.Equal(sig, sig2[:]) {
115 t.Errorf("different signature result on line %d: %x vs %x", lineNo, sig, sig2)
118 var pubKey [PublicKeySize]byte
119 copy(pubKey[:], pubKeyBytes)
120 if !Verify(&pubKey, msg, sig2) {
121 t.Errorf("signature failed to verify on line %d", lineNo)
126 func BenchmarkKeyGeneration(b *testing.B) {
128 for i := 0; i < b.N; i++ {
129 if _, _, err := GenerateKey(zero); err != nil {
135 func BenchmarkSigning(b *testing.B) {
137 _, priv, err := GenerateKey(zero)
141 message := []byte("Hello, world!")
143 for i := 0; i < b.N; i++ {
148 func BenchmarkVerification(b *testing.B) {
150 pub, priv, err := GenerateKey(zero)
154 message := []byte("Hello, world!")
155 signature := Sign(priv, message)
157 for i := 0; i < b.N; i++ {
158 Verify(pub, message, signature)