Skip to content

Commit

Permalink
fix: match comets bls implmentation (backport #22613) (#22621)
Browse files Browse the repository at this point in the history
Co-authored-by: Marko <[email protected]>
Co-authored-by: sontrinh16 <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
4 people authored Nov 22, 2024
1 parent 0d34054 commit c8ef0ef
Show file tree
Hide file tree
Showing 62 changed files with 281 additions and 381 deletions.
7 changes: 3 additions & 4 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ require (
github.com/spf13/pflag v1.0.5
go.uber.org/mock v0.5.0 // indirect
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.35.1
google.golang.org/protobuf v1.35.2
gotest.tools/v3 v3.5.1
sigs.k8s.io/yaml v1.4.0
)

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.2-20240701160653-fedbb9acfd2f.1 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.2-20240130113600-88ef6483f90f.1 // indirect
cosmossdk.io/collections v0.4.1-0.20241107084833-00f3065e70ee // indirect
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 // indirect
cosmossdk.io/errors v1.0.1 // indirect
Expand Down Expand Up @@ -53,7 +53,6 @@ require (
github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 // indirect
github.com/cosmos/crypto v0.1.2 // indirect
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.7.0
Expand Down
14 changes: 6 additions & 8 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 h1:DIUSA9vcIz63uUotWfbXXlwv1iTL+C0O2kEMLsnIIbc=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1/go.mod h1:JTBMfyi+qAXUHumX+rcD2WIq9FNWmdcNh5MjBnSw0L0=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 h1:F78ecjvMtgd1aZ1Aj9cvBjURxVGCYvRM+OOy5eR+pjw=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.2-20240701160653-fedbb9acfd2f.1 h1:V3ILbKtI97qU35SYV+5ZuiLfFCNl3mrD0d7DmnRS5hU=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.2-20240701160653-fedbb9acfd2f.1/go.mod h1:wqOtclGXqh+7m/L6fn0SatBJ+m9E5muF/41a3LgIPVI=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.2-20240130113600-88ef6483f90f.1 h1:FYhA+EOS18QBdffLVpisUjBGsCtUljMx4bFOsZT81ZM=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.2-20240130113600-88ef6483f90f.1/go.mod h1:17Ax38yd8pg56din4ecwSDBRCSX0qLcif5Cdf8ayto4=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cosmossdk.io/api v0.7.3-0.20240924065902-eb7653cfecdf h1:CttA/mEIxGm4E7vwrjUpju7/Iespns08d9bOza70cIc=
Expand Down Expand Up @@ -133,8 +133,6 @@ github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 h1:V3WlarcZwlYY
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22/go.mod h1:Z8IXcFJ9PqKK6BIsVOB3QXtkKoqUOp1vRvPT39kOXEA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/crypto v0.1.2 h1:Yn500sPY+9sKVdhiPUSDtt8JOpBGMB515dOmla4zfls=
github.com/cosmos/crypto v0.1.2/go.mod h1:b6VWz3HczIpBaQPvI7KrbQeF3pXHh0al3T5e0uwMBQw=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
Expand Down Expand Up @@ -701,8 +699,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
6 changes: 4 additions & 2 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"cosmossdk.io/core/registry"

bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
Expand All @@ -18,13 +20,13 @@ func RegisterCrypto(registrar registry.AminoRegistrar) {
ed25519.PubKeyName)
registrar.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName)
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12_381.PubKeyName)
registrar.RegisterConcrete(&bls12_381.PubKey{}, bls12381.PubKeyName)
registrar.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute)
registrar.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
registrar.RegisterConcrete(&ed25519.PrivKey{},
ed25519.PrivKeyName)
registrar.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName)
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12_381.PrivKeyName)
registrar.RegisterConcrete(&bls12_381.PrivKey{}, bls12381.PrivKeyName)
}
6 changes: 4 additions & 2 deletions crypto/codec/pubkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package codec

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"cosmossdk.io/errors"

cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys"
Expand Down Expand Up @@ -29,7 +31,7 @@ func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
return &secp256k1.PubKey{
Key: pk.Value,
}, nil
case bls12_381.PubKeyName:
case bls12381.PubKeyName:
return &bls12_381.PubKey{
Key: pk.Value,
}, nil
Expand Down Expand Up @@ -60,7 +62,7 @@ func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) {
}, nil
case *bls12_381.PubKey:
return cryptokeys.JSONPubkey{
KeyType: bls12_381.PubKeyName,
KeyType: bls12381.PubKeyName,
Value: pk.Bytes(),
}, nil
default:
Expand Down
21 changes: 0 additions & 21 deletions crypto/keys/bls12_381/const.go

This file was deleted.

7 changes: 4 additions & 3 deletions crypto/keys/bls12_381/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/cometbft/cometbft/crypto"
bls "github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {

// Type returns the type.
func (PrivKey) Type() string {
return KeyType
return bls.KeyType
}

// Sign signs the given byte array. If msg is larger than
Expand All @@ -70,7 +71,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {

// UnmarshalAmino overrides Amino binary marshaling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PrivKeySize {
if len(bz) != bls.PrivKeySize {
return errors.New("invalid privkey size")
}
privKey.Key = bz
Expand Down Expand Up @@ -119,7 +120,7 @@ func (pubKey PubKey) Bytes() []byte {

// Type returns the key's type.
func (PubKey) Type() string {
return KeyType
return bls.KeyType
}

// Equals returns true if the other's type is the same and their bytes are deeply equal.
Expand Down
49 changes: 21 additions & 28 deletions crypto/keys/bls12_381/key_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/tmhash"

bls12381 "github.com/cosmos/crypto/curves/bls12381"
"github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
Expand All @@ -32,20 +31,20 @@ var (

// NewPrivateKeyFromBytes build a new key from the given bytes.
func NewPrivateKeyFromBytes(bz []byte) (PrivKey, error) {
secretKey, err := bls12381.SecretKeyFromBytes(bz)
secretKey, err := bls12381.NewPrivateKeyFromBytes(bz)
if err != nil {
return PrivKey{}, err
}
return PrivKey{
Key: secretKey.Marshal(),
Key: secretKey.Bytes(),
}, nil
}

// GenPrivKey generates a new key.
func GenPrivKey() (PrivKey, error) {
secretKey, err := bls12381.RandKey()
secretKey, err := bls12381.GenPrivKey()
return PrivKey{
Key: secretKey.Marshal(),
Key: secretKey.Bytes(),
}, err
}

Expand All @@ -57,13 +56,13 @@ func (privKey PrivKey) Bytes() []byte {
// PubKey returns the private key's public key. If the privkey is not valid
// it returns a nil value.
func (privKey PrivKey) PubKey() cryptotypes.PubKey {
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
if err != nil {
return nil
}

return &PubKey{
Key: secretKey.PublicKey().Marshal(),
Key: secretKey.PubKey().Bytes(),
}
}

Expand All @@ -74,24 +73,23 @@ func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool {

// Type returns the type.
func (PrivKey) Type() string {
return KeyType
return bls12381.KeyType
}

// Sign signs the given byte array. If msg is larger than
// MaxMsgLen, SHA256 sum will be signed instead of the raw bytes.
func (privKey PrivKey) Sign(msg []byte) ([]byte, error) {
secretKey, err := bls12381.SecretKeyFromBytes(privKey.Key)
secretKey, err := bls12381.NewPrivateKeyFromBytes(privKey.Key)
if err != nil {
return nil, err
}

if len(msg) > MaxMsgLen {
if len(msg) > bls12381.MaxMsgLen {
hash := sha256.Sum256(msg)
sig := secretKey.Sign(hash[:])
return sig.Marshal(), nil
return secretKey.Sign(hash[:])
}
sig := secretKey.Sign(msg)
return sig.Marshal(), nil

return secretKey.Sign(msg)
}

// MarshalAmino overrides Amino binary marshaling.
Expand All @@ -101,7 +99,7 @@ func (privKey PrivKey) MarshalAmino() ([]byte, error) {

// UnmarshalAmino overrides Amino binary marshaling.
func (privKey *PrivKey) UnmarshalAmino(bz []byte) error {
if len(bz) != PrivKeySize {
if len(bz) != bls12381.PrivKeySize {
return errors.New("invalid privkey size")
}
privKey.Key = bz
Expand Down Expand Up @@ -135,35 +133,30 @@ var _ cryptotypes.PubKey = &PubKey{}
//
// The function will panic if the public key is invalid.
func (pubKey PubKey) Address() crypto.Address {
pk, _ := bls12381.PublicKeyFromBytes(pubKey.Key)
if len(pk.Marshal()) != PubKeySize {
pk, _ := bls12381.NewPublicKeyFromBytes(pubKey.Key)
if len(pk.Bytes()) != bls12381.PubKeySize {
panic("pubkey is incorrect size")
}
return crypto.Address(tmhash.SumTruncated(pubKey.Key))
}

// VerifySignature verifies the given signature.
func (pubKey PubKey) VerifySignature(msg, sig []byte) bool {
if len(sig) != SignatureLength {
if len(sig) != bls12381.SignatureLength {
return false
}

pubK, err := bls12381.PublicKeyFromBytes(pubKey.Key)
pubK, err := bls12381.NewPublicKeyFromBytes(pubKey.Key)
if err != nil { // invalid pubkey
return false
}

if len(msg) > MaxMsgLen {
if len(msg) > bls12381.MaxMsgLen {
hash := sha256.Sum256(msg)
msg = hash[:]
}

ok, err := bls12381.VerifySignature(sig, [MaxMsgLen]byte(msg[:MaxMsgLen]), pubK)
if err != nil { // bad signature
return false
}

return ok
return pubK.VerifySignature(msg, sig)
}

// Bytes returns the byte format.
Expand All @@ -173,7 +166,7 @@ func (pubKey PubKey) Bytes() []byte {

// Type returns the key's type.
func (PubKey) Type() string {
return KeyType
return bls12381.KeyType
}

// Equals returns true if the other's type is the same and their bytes are deeply equal.
Expand Down
4 changes: 3 additions & 1 deletion crypto/keys/jsonkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keys

import (
bls "github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -26,7 +28,7 @@ func (pk JSONPubkey) Address() types.Address {
Key: pk.Value,
}
return secp256k1.Address()
case bls12_381.PubKeyName:
case bls.PubKeyName:
bls12_381 := bls12_381.PubKey{
Key: pk.Value,
}
Expand Down
4 changes: 3 additions & 1 deletion crypto/keys/multisig/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package multisig

import (
"github.com/cometbft/cometbft/crypto/bls12381"

"github.com/cosmos/cosmos-sdk/codec"
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand All @@ -26,7 +28,7 @@ func init() {
AminoCdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName)
AminoCdc.RegisterConcrete(&bls12_381.PubKey{},
bls12_381.PubKeyName)
bls12381.PubKeyName)
AminoCdc.RegisterConcrete(&LegacyAminoPubKey{},
PubKeyAminoRoute)
}
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ go 1.23.1
module github.com/cosmos/cosmos-sdk

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 // indirect
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.2-20240701160653-fedbb9acfd2f.1 // indirect
cosmossdk.io/api v0.8.0 // main
cosmossdk.io/collections v0.4.1-0.20241107084833-00f3065e70ee // main
cosmossdk.io/core v1.0.0-alpha.6
Expand All @@ -24,7 +24,6 @@ require (
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/crypto v0.1.2
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogogateway v1.2.0
github.com/cosmos/gogoproto v1.7.0
Expand Down Expand Up @@ -59,14 +58,14 @@ require (
golang.org/x/sync v0.9.0
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1
google.golang.org/grpc v1.68.0
google.golang.org/protobuf v1.35.1
google.golang.org/protobuf v1.35.2
gotest.tools/v3 v3.5.1
pgregory.net/rapid v1.1.0
sigs.k8s.io/yaml v1.4.0
)

require (
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.2-20240130113600-88ef6483f90f.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
Expand Down
Loading

0 comments on commit c8ef0ef

Please sign in to comment.