From 279482ce9911e686d81a24e0cfffadfdd76ef78b Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Fri, 3 Jan 2020 00:25:32 -0800 Subject: [PATCH] crypto: fix sr25519 from raw import (#4272) * fix raw import sr25519 * add sr25519 to multisig codec * bump go-schnorrkel Co-authored-by: Marko Fixes sr25519 pubkey generation and signing when importing from raw bytes --- crypto/multisig/codec.go | 3 +++ crypto/multisig/threshold_pubkey_test.go | 8 ++++++-- crypto/sr25519/privkey.go | 14 ++++++++------ go.mod | 4 ++-- go.sum | 8 ++++++++ 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/crypto/multisig/codec.go b/crypto/multisig/codec.go index 71e0db144..3a5869398 100644 --- a/crypto/multisig/codec.go +++ b/crypto/multisig/codec.go @@ -5,6 +5,7 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" + "github.com/tendermint/tendermint/crypto/sr25519" ) // TODO: Figure out API for others to either add their own pubkey types, or @@ -21,6 +22,8 @@ func init() { PubKeyMultisigThresholdAminoRoute, nil) cdc.RegisterConcrete(ed25519.PubKeyEd25519{}, ed25519.PubKeyAminoName, nil) + cdc.RegisterConcrete(sr25519.PubKeySr25519{}, + sr25519.PubKeyAminoName, nil) cdc.RegisterConcrete(secp256k1.PubKeySecp256k1{}, secp256k1.PubKeyAminoName, nil) } diff --git a/crypto/multisig/threshold_pubkey_test.go b/crypto/multisig/threshold_pubkey_test.go index a707c4b51..34b6a4773 100644 --- a/crypto/multisig/threshold_pubkey_test.go +++ b/crypto/multisig/threshold_pubkey_test.go @@ -9,6 +9,7 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" + "github.com/tendermint/tendermint/crypto/sr25519" ) // This tests multisig functionality, but it expects the first k signatures to be valid @@ -165,10 +166,13 @@ func generatePubKeysAndSignatures(n int, msg []byte) (pubkeys []crypto.PubKey, s signatures = make([][]byte, n) for i := 0; i < n; i++ { var privkey crypto.PrivKey - if rand.Int63()%2 == 0 { + switch rand.Int63() % 3 { + case 0: privkey = ed25519.GenPrivKey() - } else { + case 1: privkey = secp256k1.GenPrivKey() + case 2: + privkey = sr25519.GenPrivKey() } pubkeys[i] = privkey.PubKey() signatures[i], _ = privkey.Sign(msg) diff --git a/crypto/sr25519/privkey.go b/crypto/sr25519/privkey.go index 8c5a18b4d..17d33ebf2 100644 --- a/crypto/sr25519/privkey.go +++ b/crypto/sr25519/privkey.go @@ -23,11 +23,11 @@ func (privKey PrivKeySr25519) Bytes() []byte { // Sign produces a signature on the provided message. func (privKey PrivKeySr25519) Sign(msg []byte) ([]byte, error) { - secretKey := &(schnorrkel.SecretKey{}) - err := secretKey.Decode(privKey) + miniSecretKey, err := schnorrkel.NewMiniSecretKeyFromRaw(privKey) if err != nil { return []byte{}, err } + secretKey := miniSecretKey.ExpandEd25519() signingContext := schnorrkel.NewSigningContext([]byte{}, msg) @@ -42,14 +42,16 @@ func (privKey PrivKeySr25519) Sign(msg []byte) ([]byte, error) { // PubKey gets the corresponding public key from the private key. func (privKey PrivKeySr25519) PubKey() crypto.PubKey { - - secretKey := &(schnorrkel.SecretKey{}) - err := secretKey.Decode(privKey) + miniSecretKey, err := schnorrkel.NewMiniSecretKeyFromRaw(privKey) if err != nil { panic(fmt.Sprintf("Invalid private key: %v", err)) } + secretKey := miniSecretKey.ExpandEd25519() - pubkey, _ := secretKey.Public() + pubkey, err := secretKey.Public() + if err != nil { + panic(fmt.Sprintf("Could not generate public key: %v", err)) + } return PubKeySr25519(pubkey.Encode()) } diff --git a/go.mod b/go.mod index f7e847989..727bae0fd 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/tendermint/tendermint go 1.12 require ( - github.com/ChainSafe/go-schnorrkel v0.0.0-20191204112926-74e22bf42a85 + github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f github.com/VividCortex/gohistogram v1.0.0 // indirect github.com/Workiva/go-datastructures v1.0.50 github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d @@ -32,7 +32,7 @@ require ( github.com/stumble/gorocksdb v0.0.3 // indirect github.com/tendermint/go-amino v0.14.1 github.com/tendermint/tm-db v0.3.0 - golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 + golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 golang.org/x/net v0.0.0-20190628185345-da137c7871d7 golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect google.golang.org/grpc v1.26.0 diff --git a/go.sum b/go.sum index ab33be95b..dd8b13490 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/ChainSafe/go-schnorrkel v0.0.0-20191119190741-a5524ad9568e h1:rkK/bP+ github.com/ChainSafe/go-schnorrkel v0.0.0-20191119190741-a5524ad9568e/go.mod h1:+soe5c7df0vttdVAmHHSAenpDToZsECJR8psL1P/7Bg= github.com/ChainSafe/go-schnorrkel v0.0.0-20191204112926-74e22bf42a85 h1:cAkGY6Gq3Lpt9H+BE1kipBnUrRgcgLSfYjOVxdJJdV8= github.com/ChainSafe/go-schnorrkel v0.0.0-20191204112926-74e22bf42a85/go.mod h1:XghbtLKucwsjjP+kApu/dx0Zkbb4tB8nzI8hnwtYc34= +github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f h1:4O1om+UVU+Hfcihr1timk8YNXHxzZWgCo7ofnrZRApw= +github.com/ChainSafe/go-schnorrkel v0.0.0-20200102211924-4bcbc698314f/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/VividCortex/gohistogram v1.0.0 h1:6+hBz+qvs0JOrrNhhmR7lFxo5sINxBCGXrdtl/UvroE= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= @@ -36,6 +38,8 @@ github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= +github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d h1:49RLWk1j44Xu4fjHb6JFYmeUnDORVwHNkDxaQ0ctCVU= +github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y= github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -246,6 +250,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413 h1:ULYEB3JvPRE/IfO+9uO7vKV/xzVTO7XPAwm8xbf4w2g= +golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -258,6 +264,7 @@ golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190628185345-da137c7871d7 h1:rTIdg5QFRR7XCaK4LCjBiPbx8j4DQRpdYMnGn/bJUEU= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -275,6 +282,7 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181128092732-4ed8d59d0b35/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=