Skip to content

Commit 96778a1

Browse files
authored
crypto/secp256k1: sign with deterministic K (rfc6979) (bnb-chain#3561)
1 parent 935d891 commit 96778a1

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎crypto/secp256k1/secp256.go‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ import (
4040
"errors"
4141
"math/big"
4242
"unsafe"
43-
44-
"github.com/ethereum/go-ethereum/crypto/randentropy"
4543
)
4644

4745
var (
@@ -89,13 +87,11 @@ func Sign(msg []byte, seckey []byte) ([]byte, error) {
8987
}
9088

9189
var (
92-
msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
93-
nonce = randentropy.GetEntropyCSPRNG(32)
94-
noncefunc = &(*C.secp256k1_nonce_function_default)
95-
noncefuncData = unsafe.Pointer(&nonce[0])
96-
sigstruct C.secp256k1_ecdsa_recoverable_signature
90+
msgdata = (*C.uchar)(unsafe.Pointer(&msg[0]))
91+
noncefunc = C.secp256k1_nonce_function_rfc6979
92+
sigstruct C.secp256k1_ecdsa_recoverable_signature
9793
)
98-
if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, noncefuncData) == 0 {
94+
if C.secp256k1_ecdsa_sign_recoverable(context, &sigstruct, msgdata, seckeydata, noncefunc, nil) == 0 {
9995
return nil, ErrSignFailed
10096
}
10197

‎crypto/secp256k1/secp256_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ func TestSignAndRecover(t *testing.T) {
112112
}
113113
}
114114

115+
func TestSignDeterministic(t *testing.T) {
116+
_, seckey := generateKeyPair()
117+
msg := make([]byte, 32)
118+
copy(msg, "hi there")
119+
120+
sig1, err := Sign(msg, seckey)
121+
if err != nil {
122+
t.Fatal(err)
123+
}
124+
sig2, err := Sign(msg, seckey)
125+
if err != nil {
126+
t.Fatal(err)
127+
}
128+
if !bytes.Equal(sig1, sig2) {
129+
t.Fatal("signatures not equal")
130+
}
131+
}
132+
115133
func TestRandomMessagesWithSameKey(t *testing.T) {
116134
pubkey, seckey := generateKeyPair()
117135
keys := func() ([]byte, []byte) {

0 commit comments

Comments
 (0)