github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/crypto/ed25519/ed25519_test.go (about)

     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/rand"
    13  	"encoding/hex"
    14  	"os"
    15  	"strings"
    16  	"testing"
    17  
    18  	"github.com/bytom/bytom/crypto/ed25519/internal/edwards25519"
    19  )
    20  
    21  type zeroReader struct{}
    22  
    23  func (zeroReader) Read(buf []byte) (int, error) {
    24  	for i := range buf {
    25  		buf[i] = 0
    26  	}
    27  	return len(buf), nil
    28  }
    29  
    30  func TestUnmarshalMarshal(t *testing.T) {
    31  	pub, _, _ := GenerateKey(rand.Reader)
    32  
    33  	var A edwards25519.ExtendedGroupElement
    34  	var pubBytes [32]byte
    35  	copy(pubBytes[:], pub)
    36  	if !A.FromBytes(&pubBytes) {
    37  		t.Fatalf("ExtendedGroupElement.FromBytes failed")
    38  	}
    39  
    40  	var pub2 [32]byte
    41  	A.ToBytes(&pub2)
    42  
    43  	if pubBytes != pub2 {
    44  		t.Errorf("FromBytes(%v)->ToBytes does not round-trip, got %x\n", pubBytes, pub2)
    45  	}
    46  }
    47  
    48  func TestSignVerify(t *testing.T) {
    49  	var zero zeroReader
    50  	public, private, _ := GenerateKey(zero)
    51  
    52  	message := []byte("test message")
    53  	sig := Sign(private, message)
    54  	if !Verify(public, message, sig) {
    55  		t.Errorf("valid signature rejected")
    56  	}
    57  
    58  	wrongMessage := []byte("wrong message")
    59  	if Verify(public, wrongMessage, sig) {
    60  		t.Errorf("signature of different message accepted")
    61  	}
    62  }
    63  
    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")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	defer testDataZ.Close()
    72  	testData, err := gzip.NewReader(testDataZ)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer testData.Close()
    77  
    78  	scanner := bufio.NewScanner(testData)
    79  	lineNo := 0
    80  
    81  	for scanner.Scan() {
    82  		lineNo++
    83  
    84  		line := scanner.Text()
    85  		parts := strings.Split(line, ":")
    86  		if len(parts) != 5 {
    87  			t.Fatalf("bad number of parts on line %d", lineNo)
    88  		}
    89  
    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]
    97  
    98  		if l := len(pubKey); l != PublicKeySize {
    99  			t.Fatalf("bad public key length on line %d: got %d bytes", lineNo, l)
   100  		}
   101  
   102  		var priv [PrivateKeySize]byte
   103  		copy(priv[:], privBytes)
   104  		copy(priv[32:], pubKey)
   105  
   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)
   109  		}
   110  
   111  		if !Verify(pubKey, msg, sig2) {
   112  			t.Errorf("signature failed to verify on line %d", lineNo)
   113  		}
   114  	}
   115  
   116  	if err := scanner.Err(); err != nil {
   117  		t.Fatalf("error reading test data: %s", err)
   118  	}
   119  }
   120  
   121  func BenchmarkKeyGeneration(b *testing.B) {
   122  	var zero zeroReader
   123  	for i := 0; i < b.N; i++ {
   124  		if _, _, err := GenerateKey(zero); err != nil {
   125  			b.Fatal(err)
   126  		}
   127  	}
   128  }
   129  
   130  func BenchmarkSigning(b *testing.B) {
   131  	var zero zeroReader
   132  	_, priv, err := GenerateKey(zero)
   133  	if err != nil {
   134  		b.Fatal(err)
   135  	}
   136  	message := []byte("Hello, world!")
   137  	b.ResetTimer()
   138  	for i := 0; i < b.N; i++ {
   139  		Sign(priv, message)
   140  	}
   141  }
   142  
   143  func BenchmarkVerification(b *testing.B) {
   144  	var zero zeroReader
   145  	pub, priv, err := GenerateKey(zero)
   146  	if err != nil {
   147  		b.Fatal(err)
   148  	}
   149  	message := []byte("Hello, world!")
   150  	signature := Sign(priv, message)
   151  	b.ResetTimer()
   152  	for i := 0; i < b.N; i++ {
   153  		Verify(pub, message, signature)
   154  	}
   155  }