Currently, the secp256k1 package is rather tightly bound to ECDSA signatures which makes them the only real first-class citizen in the code. In an effort to make it clear the ECDSA is only one possible digital signature algorithms and to enable Schnorr signatures to also be made first class citizens, this decouples all code related to producing and parsing ECDSA signatures into a separate package under secp256k1 named ecdsa. This is a fairly large change in terms of the number of lines changed, however, the vast majority of the changes are mechanical to deal with the package name changes and moved code. The only real new code is documentation for the new package. The following is a high level overview of the changes: - Rename signature.go to ecdsa/signature.go - Rename signature_test.go to ecdsa/signature_test.go - Rename sigerror.go to ecdsa/error.go - Rename SignatureErrorCode type to ErrorCode - Rename SignatureError type to Error - Rename sigerror_test.go to ecdsa/error_test.go - Rename signature_bench_test.go to ecdsa/bench_test.go - Move signing-related examples from example_test.go to ecdsa/example_test.go - Update all moved code to reference secp256k1 types and funcs - Remove Sign from the secp256k1.PrivateKey type in favor of ecdsa.Sign - Add ecdsa/README.md to describe the new package - Add ecdsa/doc.go to provide package documentation via godoc - Move signing examples from README.md to new ecdsa/README.md - Update txscript and rpcserver to use the new package methods
139 lines
4.6 KiB
Go
139 lines
4.6 KiB
Go
// Copyright (c) 2015-2016 The btcsuite developers
|
|
// Copyright (c) 2016-2020 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v3"
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v3/ecdsa"
|
|
)
|
|
|
|
// genRandomSig returns a random message, a signature of the message under the
|
|
// public key and the public key. This function is used to generate randomized
|
|
// test data.
|
|
func genRandomSig() (*chainhash.Hash, *ecdsa.Signature, *secp256k1.PublicKey, error) {
|
|
privKey, err := secp256k1.GeneratePrivateKey()
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
pub := privKey.PubKey()
|
|
|
|
var msgHash chainhash.Hash
|
|
if _, err := rand.Read(msgHash[:]); err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
sig := ecdsa.Sign(privKey, msgHash[:])
|
|
return &msgHash, sig, pub, nil
|
|
}
|
|
|
|
// TestSigCacheAddExists tests the ability to add, and later check the
|
|
// existence of a signature triplet in the signature cache.
|
|
func TestSigCacheAddExists(t *testing.T) {
|
|
sigCache := NewSigCache(200)
|
|
|
|
// Generate a random sigCache entry triplet.
|
|
msg1, sig1, key1, err := genRandomSig()
|
|
if err != nil {
|
|
t.Errorf("unable to generate random signature test data")
|
|
}
|
|
|
|
// Add the triplet to the signature cache.
|
|
sigCache.Add(*msg1, sig1, key1)
|
|
|
|
// The previously added triplet should now be found within the sigcache.
|
|
sig1Copy, _ := ecdsa.ParseDERSignature(sig1.Serialize())
|
|
key1Copy, _ := secp256k1.ParsePubKey(key1.SerializeCompressed())
|
|
if !sigCache.Exists(*msg1, sig1Copy, key1Copy) {
|
|
t.Errorf("previously added item not found in signature cache")
|
|
}
|
|
}
|
|
|
|
// TestSigCacheAddEvictEntry tests the eviction case where a new signature
|
|
// triplet is added to a full signature cache which should trigger randomized
|
|
// eviction, followed by adding the new element to the cache.
|
|
func TestSigCacheAddEvictEntry(t *testing.T) {
|
|
// Create a sigcache that can hold up to 100 entries.
|
|
sigCacheSize := uint(100)
|
|
sigCache := NewSigCache(sigCacheSize)
|
|
|
|
// Fill the sigcache up with some random sig triplets.
|
|
for i := uint(0); i < sigCacheSize; i++ {
|
|
msg, sig, key, err := genRandomSig()
|
|
if err != nil {
|
|
t.Fatalf("unable to generate random signature test data")
|
|
}
|
|
|
|
sigCache.Add(*msg, sig, key)
|
|
sigCopy, _ := ecdsa.ParseDERSignature(sig.Serialize())
|
|
keyCopy, _ := secp256k1.ParsePubKey(key.SerializeCompressed())
|
|
if !sigCache.Exists(*msg, sigCopy, keyCopy) {
|
|
t.Errorf("previously added item not found in signature " +
|
|
"cache")
|
|
}
|
|
}
|
|
|
|
// The sigcache should now have sigCacheSize entries within it.
|
|
if uint(len(sigCache.validSigs)) != sigCacheSize {
|
|
t.Fatalf("sigcache should now have %v entries, instead it has %v",
|
|
sigCacheSize, len(sigCache.validSigs))
|
|
}
|
|
|
|
// Add a new entry, this should cause eviction of a randomly chosen
|
|
// previous entry.
|
|
msgNew, sigNew, keyNew, err := genRandomSig()
|
|
if err != nil {
|
|
t.Fatalf("unable to generate random signature test data")
|
|
}
|
|
sigCache.Add(*msgNew, sigNew, keyNew)
|
|
|
|
// The sigcache should still have sigCache entries.
|
|
if uint(len(sigCache.validSigs)) != sigCacheSize {
|
|
t.Fatalf("sigcache should now have %v entries, instead it has %v",
|
|
sigCacheSize, len(sigCache.validSigs))
|
|
}
|
|
|
|
// The entry added above should be found within the sigcache.
|
|
sigNewCopy, _ := ecdsa.ParseDERSignature(sigNew.Serialize())
|
|
keyNewCopy, _ := secp256k1.ParsePubKey(keyNew.SerializeCompressed())
|
|
if !sigCache.Exists(*msgNew, sigNewCopy, keyNewCopy) {
|
|
t.Fatalf("previously added item not found in signature cache")
|
|
}
|
|
}
|
|
|
|
// TestSigCacheAddMaxEntriesZeroOrNegative tests that if a sigCache is created
|
|
// with a max size <= 0, then no entries are added to the sigcache at all.
|
|
func TestSigCacheAddMaxEntriesZeroOrNegative(t *testing.T) {
|
|
// Create a sigcache that can hold up to 0 entries.
|
|
sigCache := NewSigCache(0)
|
|
|
|
// Generate a random sigCache entry triplet.
|
|
msg1, sig1, key1, err := genRandomSig()
|
|
if err != nil {
|
|
t.Errorf("unable to generate random signature test data")
|
|
}
|
|
|
|
// Add the triplet to the signature cache.
|
|
sigCache.Add(*msg1, sig1, key1)
|
|
|
|
// The generated triplet should not be found.
|
|
sig1Copy, _ := ecdsa.ParseDERSignature(sig1.Serialize())
|
|
key1Copy, _ := secp256k1.ParsePubKey(key1.SerializeCompressed())
|
|
if sigCache.Exists(*msg1, sig1Copy, key1Copy) {
|
|
t.Errorf("previously added signature found in sigcache, but " +
|
|
"shouldn't have been")
|
|
}
|
|
|
|
// There shouldn't be any entries in the sigCache.
|
|
if len(sigCache.validSigs) != 0 {
|
|
t.Errorf("%v items found in sigcache, no items should have "+
|
|
"been added", len(sigCache.validSigs))
|
|
}
|
|
}
|