Merge pull request #604 from nspcc-dev/rfc6979-update

Update rfc6979 package to the latest version
This commit is contained in:
Roman Khimov 2020-01-17 17:23:02 +03:00 committed by GitHub
commit 2b02c145c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 55 deletions

4
go.mod
View file

@ -7,8 +7,8 @@ require (
github.com/go-redis/redis v6.10.2+incompatible github.com/go-redis/redis v6.10.2+incompatible
github.com/go-yaml/yaml v2.1.0+incompatible github.com/go-yaml/yaml v2.1.0+incompatible
github.com/mr-tron/base58 v1.1.2 github.com/mr-tron/base58 v1.1.2
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82 github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8
github.com/nspcc-dev/rfc6979 v0.1.0 github.com/nspcc-dev/rfc6979 v0.2.0
github.com/pkg/errors v0.8.1 github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.2.1 github.com/prometheus/client_golang v1.2.1
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0

8
go.sum
View file

@ -94,12 +94,16 @@ github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254 h1:A4OkQDQOSPsJF8qU
github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254/go.mod h1:w1Ln2aT+dBlPhLnuZhBV+DfPEdS2CHWWLp5JTScY3bw= github.com/nspcc-dev/dbft v0.0.0-20191205084618-dacb1a30c254/go.mod h1:w1Ln2aT+dBlPhLnuZhBV+DfPEdS2CHWWLp5JTScY3bw=
github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae h1:T5V1QANlNMKun0EPB3eqg2PTXG4rmLhzDyEiV63kdB0= github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae h1:T5V1QANlNMKun0EPB3eqg2PTXG4rmLhzDyEiV63kdB0=
github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae/go.mod h1:3FjXOoHmA51EGfb5GS/HOv7VdmngNRTssSeQ729dvGY= github.com/nspcc-dev/dbft v0.0.0-20191209120240-0d6b7568d9ae/go.mod h1:3FjXOoHmA51EGfb5GS/HOv7VdmngNRTssSeQ729dvGY=
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82 h1:jwFLk2NKgyUbmJGlZsn3qIjoRqUkKajXb45ZuWbvkT4= github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8 h1:WjfnKH75ncU5iySB6ooTsbg2P0q1iQoecTN4gHIuEbs=
github.com/nspcc-dev/dbft v0.0.0-20200113100648-b3578fc15d82/go.mod h1:IyIyVYKfi41kAlGWqicz9G8Iyni71Resuhtd9Y5ujJM= github.com/nspcc-dev/dbft v0.0.0-20200116150450-80b3f6f0dff8/go.mod h1:/YFK+XOxxg0Bfm6P92lY5eDSLYfp06XOdL8KAVgXjVk=
github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck= github.com/nspcc-dev/neofs-crypto v0.2.0 h1:ftN+59WqxSWz/RCgXYOfhmltOOqU+udsNQSvN6wkFck=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
github.com/nspcc-dev/neofs-crypto v0.2.3 h1:aca3X2aly92ENRbFK+kH6Hd+J9EQ4Eu6XMVoITSIKtc=
github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/rfc6979 v0.1.0 h1:Lwg7esRRoyK1Up/IN1vAef1EmvrBeMHeeEkek2fAJ6c= github.com/nspcc-dev/rfc6979 v0.1.0 h1:Lwg7esRRoyK1Up/IN1vAef1EmvrBeMHeeEkek2fAJ6c=
github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE=
github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso=
github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw= github.com/onsi/ginkgo v1.6.0 h1:Ix8l273rp3QzYgXSR+c8d1fTG7UPgYkOSELPhiY/YGw=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY= github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY=

View file

@ -24,6 +24,11 @@ func (p *privateKey) UnmarshalBinary(data []byte) (err error) {
return return
} }
// Sign implements dbft's crypto.PrivateKey interface.
func (p *privateKey) Sign(data []byte) ([]byte, error) {
return p.PrivateKey.Sign(data), nil
}
// publicKey is a wrapper around keys.PublicKey // publicKey is a wrapper around keys.PublicKey
// which implements crypto.PublicKey interface. // which implements crypto.PublicKey interface.
type publicKey struct { type publicKey struct {

View file

@ -82,9 +82,9 @@ func newBlock(index uint32, txs ...*transaction.Transaction) *Block {
panic(err) panic(err)
} }
b := b.GetHashableData() b := b.GetHashableData()
sig, err := pKey.Sign(b) sig := pKey.Sign(b)
if err != nil || len(sig) != 64 { if len(sig) != 64 {
panic(err) panic("wrong signature length")
} }
invScript = append(invScript, byte(opcode.PUSHBYTES64)) invScript = append(invScript, byte(opcode.PUSHBYTES64))
invScript = append(invScript, sig...) invScript = append(invScript, sig...)

View file

@ -105,16 +105,13 @@ func (p *PrivateKey) Signature() []byte {
} }
// Sign signs arbitrary length data using the private key. // Sign signs arbitrary length data using the private key.
func (p *PrivateKey) Sign(data []byte) ([]byte, error) { func (p *PrivateKey) Sign(data []byte) []byte {
var ( var (
privateKey = p.ecdsa() privateKey = p.ecdsa()
digest = sha256.Sum256(data) digest = sha256.Sum256(data)
) )
r, s, err := rfc6979.SignECDSA(privateKey, digest[:], sha256.New) r, s := rfc6979.SignECDSA(privateKey, digest[:], sha256.New)
if err != nil {
return nil, err
}
params := privateKey.Curve.Params() params := privateKey.Curve.Params()
curveOrderByteSize := params.P.BitLen() / 8 curveOrderByteSize := params.P.BitLen() / 8
@ -123,7 +120,7 @@ func (p *PrivateKey) Sign(data []byte) ([]byte, error) {
copy(signature[curveOrderByteSize-len(rBytes):], rBytes) copy(signature[curveOrderByteSize-len(rBytes):], rBytes)
copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes) copy(signature[curveOrderByteSize*2-len(sBytes):], sBytes)
return signature, nil return signature
} }
// ecsda converts the key to a usable ecsda.PrivateKey for signing data. // ecsda converts the key to a usable ecsda.PrivateKey for signing data.

View file

@ -48,8 +48,7 @@ func TestSigning(t *testing.T) {
//Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299 //Uy = 7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299
PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721") PrivateKey, _ := NewPrivateKeyFromHex("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721")
data, err := PrivateKey.Sign([]byte("sample")) data := PrivateKey.Sign([]byte("sample"))
assert.Nil(t, err)
r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716" r := "EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716"
s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8" s := "F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8"

View file

@ -13,8 +13,7 @@ func TestPubKeyVerify(t *testing.T) {
privKey, err := NewPrivateKey() privKey, err := NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
signedData, err := privKey.Sign(data) signedData := privKey.Sign(data)
assert.Nil(t, err)
pubKey := privKey.PublicKey() pubKey := privKey.PublicKey()
result := pubKey.Verify(signedData, hashedData.BytesBE()) result := pubKey.Verify(signedData, hashedData.BytesBE())
expected := true expected := true
@ -28,7 +27,7 @@ func TestWrongPubKey(t *testing.T) {
privKey, _ := NewPrivateKey() privKey, _ := NewPrivateKey()
sample := []byte("sample") sample := []byte("sample")
hashedData := hash.Sha256(sample) hashedData := hash.Sha256(sample)
signedData, _ := privKey.Sign(sample) signedData := privKey.Sign(sample)
secondPrivKey, _ := NewPrivateKey() secondPrivKey, _ := NewPrivateKey()
wrongPubKey := secondPrivKey.PublicKey() wrongPubKey := secondPrivKey.PublicKey()

View file

@ -96,7 +96,6 @@ func SignTx(tx *transaction.Transaction, wif *keys.WIF) error {
// GetInvocationScript returns NEO VM script containing transaction signature. // GetInvocationScript returns NEO VM script containing transaction signature.
func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) { func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, error) {
var ( var (
err error
buf = io.NewBufBinWriter() buf = io.NewBufBinWriter()
signature []byte signature []byte
) )
@ -105,10 +104,7 @@ func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, er
return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary") return nil, errs.Wrap(buf.Err, "Failed to encode transaction to binary")
} }
data := buf.Bytes() data := buf.Bytes()
signature, err = wif.PrivateKey.Sign(data[:(len(data) - 1)]) signature = wif.PrivateKey.Sign(data[:(len(data) - 1)])
if err != nil {
return nil, errs.Wrap(err, "Failed ti sign transaction with private key")
}
return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil return append([]byte{byte(opcode.PUSHBYTES64)}, signature...), nil
} }

View file

@ -2477,8 +2477,7 @@ func TestCHECKSIGNoSigLoaded(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := "NEO - An Open Network For Smart Economy" msg := "NEO - An Open Network For Smart Economy"
sig, err := pk.Sign([]byte(msg)) sig := pk.Sign([]byte(msg))
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(sig) vm.estack.PushVal(sig)
@ -2491,8 +2490,7 @@ func TestCHECKSIGBadKey(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes()[:4] pbytes := pk.PublicKey().Bytes()[:4]
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2506,8 +2504,7 @@ func TestCHECKSIGWrongSig(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2523,8 +2520,7 @@ func TestCHECKSIGGood(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2540,8 +2536,7 @@ func TestVERIFYGood(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(msg) vm.estack.PushVal(msg)
@ -2557,8 +2552,7 @@ func TestVERIFYBad(t *testing.T) {
pk, err := keys.NewPrivateKey() pk, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig, err := pk.Sign(msg) sig := pk.Sign(msg)
assert.Nil(t, err)
pbytes := pk.PublicKey().Bytes() pbytes := pk.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.estack.PushVal(util.ArrayReverse(msg)) vm.estack.PushVal(util.ArrayReverse(msg))
@ -2592,10 +2586,8 @@ func TestCHECKMULTISIGNotEnoughKeys(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
vm.SetCheckedHash(hash.Sha256(msg).BytesBE()) vm.SetCheckedHash(hash.Sha256(msg).BytesBE())
@ -2611,10 +2603,8 @@ func TestCHECKMULTISIGNoHash(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
@ -2630,10 +2620,8 @@ func TestCHECKMULTISIGBadKey(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes()[:4] pbytes2 := pk2.PublicKey().Bytes()[:4]
vm := load(prog) vm := load(prog)
@ -2650,10 +2638,8 @@ func TestCHECKMULTISIGBadSig(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)
@ -2672,10 +2658,8 @@ func TestCHECKMULTISIGGood(t *testing.T) {
pk2, err := keys.NewPrivateKey() pk2, err := keys.NewPrivateKey()
assert.Nil(t, err) assert.Nil(t, err)
msg := []byte("NEO - An Open Network For Smart Economy") msg := []byte("NEO - An Open Network For Smart Economy")
sig1, err := pk1.Sign(msg) sig1 := pk1.Sign(msg)
assert.Nil(t, err) sig2 := pk2.Sign(msg)
sig2, err := pk2.Sign(msg)
assert.Nil(t, err)
pbytes1 := pk1.PublicKey().Bytes() pbytes1 := pk1.PublicKey().Bytes()
pbytes2 := pk2.PublicKey().Bytes() pbytes2 := pk2.PublicKey().Bytes()
vm := load(prog) vm := load(prog)