mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-07 21:44:27 +00:00
crypto,secp256k1: test SignUnsafe counter behavior
This commit is contained in:
parent
fac8fc3c6c
commit
cd895cf007
2 changed files with 89 additions and 0 deletions
|
|
@ -130,6 +130,45 @@ func TestSignDeterministic(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSignUnsafeCounter(t *testing.T) {
|
||||||
|
pubkey, seckey := generateKeyPair()
|
||||||
|
msg := make([]byte, 32)
|
||||||
|
copy(msg, "hi there")
|
||||||
|
|
||||||
|
sig, err := Sign(msg, seckey)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sigUnsafe0, err := SignUnsafe(msg, seckey, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(sig, sigUnsafe0) {
|
||||||
|
t.Fatal("counter=0 should match Sign")
|
||||||
|
}
|
||||||
|
sigUnsafe1a, err := SignUnsafe(msg, seckey, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sigUnsafe1b, err := SignUnsafe(msg, seckey, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(sigUnsafe1a, sigUnsafe1b) {
|
||||||
|
t.Fatal("counter=1 signatures not equal")
|
||||||
|
}
|
||||||
|
if bytes.Equal(sig, sigUnsafe1a) {
|
||||||
|
t.Fatal("counter=1 should not match counter=0 signature")
|
||||||
|
}
|
||||||
|
pubkeyRecovered, err := RecoverPubkey(msg, sigUnsafe1a)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(pubkey, pubkeyRecovered) {
|
||||||
|
t.Fatal("recovered pubkey mismatch")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRandomMessagesWithSameKey(t *testing.T) {
|
func TestRandomMessagesWithSameKey(t *testing.T) {
|
||||||
pubkey, seckey := generateKeyPair()
|
pubkey, seckey := generateKeyPair()
|
||||||
keys := func() ([]byte, []byte) {
|
keys := func() ([]byte, []byte) {
|
||||||
|
|
|
||||||
50
crypto/signature_unsafe_test.go
Normal file
50
crypto/signature_unsafe_test.go
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package crypto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSignUnsafeCounter(t *testing.T) {
|
||||||
|
key, err := ecdsa.GenerateKey(S256(), rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
hash := make([]byte, 32)
|
||||||
|
copy(hash, "hi there")
|
||||||
|
|
||||||
|
sig, err := Sign(hash, key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sigUnsafe0, err := SignUnsafe(hash, key, 0)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(sig, sigUnsafe0) {
|
||||||
|
t.Fatal("counter=0 should match Sign")
|
||||||
|
}
|
||||||
|
sigUnsafe1a, err := SignUnsafe(hash, key, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sigUnsafe1b, err := SignUnsafe(hash, key, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(sigUnsafe1a, sigUnsafe1b) {
|
||||||
|
t.Fatal("counter=1 signatures not equal")
|
||||||
|
}
|
||||||
|
if bytes.Equal(sig, sigUnsafe1a) {
|
||||||
|
t.Fatal("counter=1 should not match counter=0 signature")
|
||||||
|
}
|
||||||
|
pub, err := SigToPub(hash, sigUnsafe1a)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if pub.X.Cmp(key.PublicKey.X) != 0 || pub.Y.Cmp(key.PublicKey.Y) != 0 {
|
||||||
|
t.Fatal("recovered pubkey mismatch")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue