From b615805bfb4b1d10cb2c191198a941e6bd163c20 Mon Sep 17 00:00:00 2001 From: daeMOn Date: Wed, 24 Nov 2021 15:30:43 +0100 Subject: [PATCH] feat: backport caelum bls on v0.42 sdk (#119) * Bls (#104) * bls signature for basic account * benchmark for bls and ed25519 * added bls sig verify cost to genesis * Revert "Merge branch 'fetchai:master' into bls" This reverts commit a5dd8ea9c1597c285e9375f168dd7af51e76f6b3, reversing changes made to 082e0710e3dd2f96733e76f8e2f928c2d7e4ab29. * format using go tools * nuisance golangci-lint errors * Bls (#105) * bls signature for basic account * benchmark for bls and ed25519 * added bls sig verify cost to genesis * Revert "Merge branch 'fetchai:master' into bls" This reverts commit a5dd8ea9c1597c285e9375f168dd7af51e76f6b3, reversing changes made to 082e0710e3dd2f96733e76f8e2f928c2d7e4ab29. * format using go tools * nuisance golangci-lint errors * POP interfaces in accounts and authentication * add bls multsig operations * fixed golangci-lint error * changes after comments * change codespace for invalid pop error * extend migrate with bls cost * set bls cost multiplier * fix ante test errors * feat: Caelum (#111) * bls signature for basic account * benchmark for bls and ed25519 * added bls sig verify cost to genesis * Revert "Merge branch 'fetchai:master' into bls" This reverts commit a5dd8ea9c1597c285e9375f168dd7af51e76f6b3, reversing changes made to 082e0710e3dd2f96733e76f8e2f928c2d7e4ab29. * format using go tools * nuisance golangci-lint errors * Bls (#104) * bls signature for basic account * benchmark for bls and ed25519 * added bls sig verify cost to genesis * Revert "Merge branch 'fetchai:master' into bls" This reverts commit a5dd8ea9c1597c285e9375f168dd7af51e76f6b3, reversing changes made to 082e0710e3dd2f96733e76f8e2f928c2d7e4ab29. * format using go tools * nuisance golangci-lint errors * POP interfaces in accounts and authentication * add bls multsig operations * fixed golangci-lint error * changes after comments * initial commit from regen-ledger/x/group v1.0.0 * minor changes to bls12381 key generation * initial commit from regen-ledger/proto/regen/group v1.0.0 * group module compatibility for fetchai cosomos-sdk * add bls account restriction to group members * fix bug in setting pop * make msg uniqueness checking optional * add bls basic/aggregate vote * add checking on empty messages/public keys * add gas caclulation/consumption for verifying aggregated votes * minor change to gas calculation for voteagg * initial commit for orm and types from regen-ledger v2.0.0-beta1 * upgrade testsuite to regen-ledger v2.0.0-beta1 * make bls requirement for group members optional * add tests for bls related group operations * client and server for poll and aggregated votes and integration tests * fix bls related test errors * fix proto-lint errors * goimport format * proto comments * update blst to v0.3.5 and more tests for bls * Update x/auth/ante/sigverify.go Co-authored-by: daeMOn * Update x/group/client/util.go Co-authored-by: daeMOn Co-authored-by: daeMOn * fix: bls12381 compat with sdk v0.42 * fix: buf lint / breaking command changes * fix: staking test want more gas * chores: drop arm from CI test matrix * chores: fix lint Co-authored-by: kitounliu <58184672+kitounliu@users.noreply.github.com> Co-authored-by: kitty --- .github/workflows/test.yml | 3 +- Makefile | 4 +- client/flags/flags.go | 2 +- crypto/codec/amino.go | 5 + crypto/codec/proto.go | 2 + crypto/hd/algo.go | 42 ++ crypto/keyring/keyring.go | 2 +- crypto/keys/bls12381/bls12381.go | 230 +++++++++++ crypto/keys/bls12381/bls12381_test.go | 204 ++++++++++ crypto/keys/bls12381/keys.pb.go | 496 +++++++++++++++++++++++ crypto/keys/bls12381/multisig.go | 116 ++++++ crypto/keys/bls12381/multisig_test.go | 123 ++++++ crypto/keys/ed25519/ed25519_test.go | 14 + docs/core/proto-docs.md | 56 +++ go.mod | 10 +- go.sum | 31 +- proto/cosmos/auth/v1beta1/auth.proto | 4 + proto/cosmos/crypto/bls12381/keys.proto | 22 + testutil/testdata/tx.go | 12 +- types/errors/errors.go | 3 + x/airdrop/types/airdrop.pb.go | 7 +- x/airdrop/types/genesis.pb.go | 5 +- x/airdrop/types/query.pb.go | 7 +- x/airdrop/types/tx.pb.go | 7 +- x/auth/ante/ante.go | 1 + x/auth/ante/ante_test.go | 8 +- x/auth/ante/sigverify.go | 51 +++ x/auth/legacy/v040/migrate.go | 4 + x/auth/legacy/v040/migrate_test.go | 8 + x/auth/simulation/genesis.go | 14 +- x/auth/types/account.go | 23 ++ x/auth/types/auth.pb.go | 162 ++++++-- x/auth/types/params.go | 23 +- x/auth/types/params_test.go | 12 +- x/staking/client/rest/grpc_query_test.go | 7 +- 35 files changed, 1632 insertions(+), 88 deletions(-) create mode 100644 crypto/keys/bls12381/bls12381.go create mode 100644 crypto/keys/bls12381/bls12381_test.go create mode 100644 crypto/keys/bls12381/keys.pb.go create mode 100644 crypto/keys/bls12381/multisig.go create mode 100644 crypto/keys/bls12381/multisig_test.go create mode 100644 proto/cosmos/crypto/bls12381/keys.proto diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 90251aae31..d9f412bf17 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,7 +35,8 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-arch: ["amd64", "arm", "arm64"] + # dropped arm because of bls + go-arch: ["amd64"] #, "arm", "arm64"] steps: - uses: actions/checkout@v2 - uses: actions/setup-go@v2.1.3 diff --git a/Makefile b/Makefile index aa41896960..a6b14ba11c 100644 --- a/Makefile +++ b/Makefile @@ -396,10 +396,10 @@ proto-format: find ./ -not -path "./third_party/*" -name *.proto -exec clang-format -i {}; fi proto-lint: - @$(DOCKER_BUF) check lint --error-format=json + @$(DOCKER_BUF) lint --error-format=json proto-check-breaking: - @$(DOCKER_BUF) check breaking --against-input $(HTTPS_GIT)#branch=master + @$(DOCKER_BUF) breaking --against-input $(HTTPS_GIT)#branch=master TM_URL = https://raw.githubusercontent.com/tendermint/tendermint/v0.34.0-rc6/proto/tendermint diff --git a/client/flags/flags.go b/client/flags/flags.go index 7d4de556b6..d6127ccfef 100644 --- a/client/flags/flags.go +++ b/client/flags/flags.go @@ -15,7 +15,7 @@ const ( // failures due to state changes that might occur between the tx simulation // and the actual run. DefaultGasAdjustment = 1.0 - DefaultGasLimit = 200000 + DefaultGasLimit = 210000 GasFlagAuto = "auto" // DefaultKeyringBackend diff --git a/crypto/codec/amino.go b/crypto/codec/amino.go index d50a08864c..0043c6855c 100644 --- a/crypto/codec/amino.go +++ b/crypto/codec/amino.go @@ -1,6 +1,7 @@ package codec import ( + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" "github.com/tendermint/tendermint/crypto/sr25519" "github.com/cosmos/cosmos-sdk/codec" @@ -22,6 +23,8 @@ func RegisterCrypto(cdc *codec.LegacyAmino) { secp256k1.PubKeyName, nil) cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{}, kmultisig.PubKeyAminoRoute, nil) + cdc.RegisterConcrete(&bls12381.PubKey{}, + bls12381.PubKeyName, nil) cdc.RegisterInterface((*cryptotypes.PrivKey)(nil), nil) cdc.RegisterConcrete(sr25519.PrivKey{}, @@ -30,4 +33,6 @@ func RegisterCrypto(cdc *codec.LegacyAmino) { ed25519.PrivKeyName, nil) cdc.RegisterConcrete(&secp256k1.PrivKey{}, secp256k1.PrivKeyName, nil) + cdc.RegisterConcrete(&bls12381.PrivKey{}, + bls12381.PrivKeyName, nil) } diff --git a/crypto/codec/proto.go b/crypto/codec/proto.go index 9c07ca1105..6bdac15ab6 100644 --- a/crypto/codec/proto.go +++ b/crypto/codec/proto.go @@ -2,6 +2,7 @@ package codec import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -14,4 +15,5 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ed25519.PubKey{}) registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &secp256k1.PubKey{}) registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &multisig.LegacyAminoPubKey{}) + registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &bls12381.PubKey{}) } diff --git a/crypto/hd/algo.go b/crypto/hd/algo.go index f934ad08ae..fcd4851996 100644 --- a/crypto/hd/algo.go +++ b/crypto/hd/algo.go @@ -1,6 +1,7 @@ package hd import ( + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" bip39 "github.com/cosmos/go-bip39" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -20,11 +21,15 @@ const ( Ed25519Type = PubKeyType("ed25519") // Sr25519Type represents the Sr25519Type signature system. Sr25519Type = PubKeyType("sr25519") + // Bls12381Type represents the Bls12381Type signature system. + Bls12381Type = PubKeyType("bls12381") ) var ( // Secp256k1 uses the Bitcoin secp256k1 ECDSA parameters. Secp256k1 = secp256k1Algo{} + // Bls12381 uses blst implememtation of bls signatures + Bls12381 = bls12381Algo{} ) type DeriveFn func(mnemonic string, bip39Passphrase, hdPath string) ([]byte, error) @@ -69,3 +74,40 @@ func (s secp256k1Algo) Generate() GenerateFn { return &secp256k1.PrivKey{Key: bzArr} } } + +type bls12381Algo struct { +} + +func (s bls12381Algo) Name() PubKeyType { + return Bls12381Type +} + +// todo: replace bitcoin private key generation +// Derive derives and returns the bls12381 private key for the given seed and HD path. +func (s bls12381Algo) Derive() DeriveFn { + return func(mnemonic string, bip39Passphrase, hdPath string) ([]byte, error) { + seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase) + if err != nil { + return nil, err + } + + masterPriv, ch := ComputeMastersFromSeed(seed) + if len(hdPath) == 0 { + return masterPriv[:], nil + } + derivedKey, err := DerivePrivateKeyForPath(masterPriv, ch, hdPath) + + return derivedKey, err + } +} + +// Generate generates a bls12381 private key from the given bytes. +func (s bls12381Algo) Generate() GenerateFn { + return func(bz []byte) types.PrivKey { + var bzArr = make([]byte, bls12381.SeedSize) + copy(bzArr, bz) + sk := bls12381.GenPrivKeyFromSecret(bzArr) + + return sk + } +} diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index b18d607322..18f813ca1e 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -200,7 +200,7 @@ type keystore struct { func newKeystore(kr keyring.Keyring, opts ...Option) keystore { // Default options for keybase options := Options{ - SupportedAlgos: SigningAlgoList{hd.Secp256k1}, + SupportedAlgos: SigningAlgoList{hd.Secp256k1, hd.Bls12381}, SupportedAlgosLedger: SigningAlgoList{hd.Secp256k1}, } diff --git a/crypto/keys/bls12381/bls12381.go b/crypto/keys/bls12381/bls12381.go new file mode 100644 index 0000000000..7aa9ffb98c --- /dev/null +++ b/crypto/keys/bls12381/bls12381.go @@ -0,0 +1,230 @@ +package bls12381 + +import ( + "crypto/subtle" + "fmt" + "io" + + "github.com/cosmos/cosmos-sdk/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/types/errors" + blst "github.com/supranational/blst/bindings/go" + "github.com/tendermint/tendermint/crypto" + "github.com/tendermint/tendermint/crypto/tmhash" +) + +const ( + PrivKeyName = "tendermint/PrivKeyBls12381" + PubKeyName = "tendermint/PubKeyBls12381" + // PubKeySize is is the size, in bytes, of public keys as used in this package. + PubKeySize = 96 + // PrivKeySize is the size, in bytes, of private keys as used in this package. + // Uncompressed public key + PrivKeySize = 32 + // SignatureSize is the size of a bls signature. Namely the size of a compressed + // G2 point. + SignatureSize = 96 + keyType = "bls12381" + SeedSize = 32 +) + +var _ cryptotypes.PrivKey = &PrivKey{} +var _ codec.AminoMarshaler = &PrivKey{} + +// Bytes returns the byte representation of the Private Key. +func (privKey *PrivKey) Bytes() []byte { + return privKey.Key +} + +// PubKey performs the point-scalar multiplication from the privKey on the +// generator point to get the pubkey. +func (privKey *PrivKey) PubKey() cryptotypes.PubKey { + sk := new(blst.SecretKey).Deserialize(privKey.Key) + if sk == nil { + panic("Failed to deserialize secret key!") + } + pk := new(blst.P1Affine).From(sk) + pkBytes := pk.Serialize() + + return &PubKey{Key: pkBytes} +} + +// Sign produces a signature on the provided message. +// This assumes the privkey is wellformed in the golang format. + +func (privKey *PrivKey) Sign(msg []byte) ([]byte, error) { + dst := []byte("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_") + sk := new(blst.SecretKey).Deserialize(privKey.Key) + if sk == nil { + panic("Failed to deserialize secret key!") + } + + sig := new(blst.P2Affine).Sign(sk, msg, dst) + if sig == nil { + panic("Failed to sign message!") + } + + sigBytes := sig.Compress() + + return sigBytes, nil +} + +// Equals - you probably don't need to use this. +// Runs in constant time based on length of the +func (privKey *PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool { + return privKey.Type() == other.Type() && subtle.ConstantTimeCompare(privKey.Bytes(), other.Bytes()) == 1 +} + +func (privKey *PrivKey) Type() string { + return keyType +} + +// MarshalAmino overrides Amino binary marshalling. +func (privKey PrivKey) MarshalAmino() ([]byte, error) { + return privKey.Key, nil +} + +// UnmarshalAmino overrides Amino binary marshalling. +func (privKey *PrivKey) UnmarshalAmino(bz []byte) error { + if len(bz) != PrivKeySize { + return fmt.Errorf("invalid privkey size") + } + privKey.Key = bz + + return nil +} + +// MarshalAminoJSON overrides Amino JSON marshalling. +func (privKey PrivKey) MarshalAminoJSON() ([]byte, error) { + // When we marshal to Amino JSON, we don't marshal the "key" field itself, + // just its contents (i.e. the key bytes). + return privKey.MarshalAmino() +} + +// UnmarshalAminoJSON overrides Amino JSON marshalling. +func (privKey *PrivKey) UnmarshalAminoJSON(bz []byte) error { + return privKey.UnmarshalAmino(bz) +} + +// GenPrivKey generates a new BLS private key on curve bls12-381 private key. +// It uses OS randomness to generate the private key. +func GenPrivKey() *PrivKey { + return &PrivKey{Key: genPrivKey(crypto.CReader())} +} + +// genPrivKey generates a new bls12381 private key using the provided reader. +func genPrivKey(rand io.Reader) []byte { + var ikm [SeedSize]byte + _, err := io.ReadFull(rand, ikm[:]) + if err != nil { + panic(err) + } + + sk := blst.KeyGen(ikm[:]) + if sk == nil { + panic("failed to generate secret key!") + } + + skBytes := sk.Serialize() + + return skBytes +} + +// GenPrivKeyFromSecret hashes the secret with SHA2, and uses +// that 32 byte output to create the private key. +// NOTE: secret should be the output of a KDF like bcrypt, +// if it's derived from user input. +func GenPrivKeyFromSecret(secret []byte) *PrivKey { + ikm := crypto.Sha256(secret) // Not Ripemd160 because we want 32 bytes. + + sk := blst.KeyGen(ikm) + if sk == nil { + panic("failed to generate secret key from ikm") + } + skBytes := sk.Serialize() + + return &PrivKey{Key: skBytes} +} + +var _ cryptotypes.PubKey = &PubKey{} +var _ codec.AminoMarshaler = &PubKey{} + +// Validate public key, infinity and subgroup checking +func (pubKey *PubKey) Validate() bool { + pk := new(blst.P1Affine).Deserialize(pubKey.Key) + return pk.KeyValidate() +} + +// Address is the SHA256-20 of the raw pubkey bytes. +func (pubKey *PubKey) Address() crypto.Address { + if len(pubKey.Key) != PubKeySize { + panic("pubkey is incorrect size") + } + return crypto.Address(tmhash.SumTruncated(pubKey.Key)) +} + +// Bytes returns the PubKey byte format. +func (pubKey *PubKey) Bytes() []byte { + return pubKey.Key +} + +// VerifySignature assumes public key is already validated +func (pubKey *PubKey) VerifySignature(msg []byte, sig []byte) bool { + // make sure we use the same algorithm to sign + pk := new(blst.P1Affine).Deserialize(pubKey.Key) + if pk == nil { + panic("Failed to deserialize public key") + } + + sigma := new(blst.P2Affine).Uncompress(sig) + if sigma == nil { + panic("Failed to deserialize signature") + } + + dst := []byte("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_") + + return sigma.Verify(true, pk, false, msg, dst) +} + +func (pubKey *PubKey) String() string { + return fmt.Sprintf("PubKeyBls12381{%X}", pubKey.Key) +} + +func (pubKey *PubKey) Type() string { + return keyType +} + +func (pubKey *PubKey) Equals(other cryptotypes.PubKey) bool { + if pubKey.Type() != other.Type() { + return false + } + + return subtle.ConstantTimeCompare(pubKey.Bytes(), other.Bytes()) == 1 +} + +// MarshalAmino overrides Amino binary marshalling. +func (pubKey PubKey) MarshalAmino() ([]byte, error) { + return pubKey.Key, nil +} + +// UnmarshalAmino overrides Amino binary marshalling. +func (pubKey *PubKey) UnmarshalAmino(bz []byte) error { + if len(bz) != PubKeySize { + return errors.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size") + } + pubKey.Key = bz + + return nil +} + +// MarshalAminoJSON overrides Amino JSON marshalling. +func (pubKey PubKey) MarshalAminoJSON() ([]byte, error) { + // When we marshal to Amino JSON, we don't marshal the "key" field itself, + // just its contents (i.e. the key bytes). + return pubKey.MarshalAmino() +} + +// UnmarshalAminoJSON overrides Amino JSON marshalling. +func (pubKey *PubKey) UnmarshalAminoJSON(bz []byte) error { + return pubKey.UnmarshalAmino(bz) +} diff --git a/crypto/keys/bls12381/bls12381_test.go b/crypto/keys/bls12381/bls12381_test.go new file mode 100644 index 0000000000..0e4bbf6ac2 --- /dev/null +++ b/crypto/keys/bls12381/bls12381_test.go @@ -0,0 +1,204 @@ +package bls12381_test + +import ( + "encoding/base64" + "testing" + + "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" + bench "github.com/cosmos/cosmos-sdk/crypto/keys/internal/benchmarking" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto" +) + +func TestSignAndValidateBls12381(t *testing.T) { + privKey := bls12381.GenPrivKey() + pubKey := privKey.PubKey() + + msg := crypto.CRandBytes(1000) + sig, err := privKey.Sign(msg) + require.Nil(t, err) + + // Test the signature + assert.True(t, pubKey.VerifySignature(msg, sig)) + +} + +func TestKeyFromSecret(t *testing.T) { + insecureSeed := []byte("a random number for testing") + privKey := bls12381.GenPrivKeyFromSecret(insecureSeed) + pubKey := privKey.PubKey() + + msg := []byte("hello") + sig, err := privKey.Sign(msg) + require.Nil(t, err) + assert.True(t, pubKey.VerifySignature(msg, sig)) +} + +func TestPubKeyEquals(t *testing.T) { + bls12381PubKey := bls12381.GenPrivKey().PubKey().(*bls12381.PubKey) + + testCases := []struct { + msg string + pubKey cryptotypes.PubKey + other cryptotypes.PubKey + expectEq bool + }{ + { + "different bytes", + bls12381PubKey, + bls12381.GenPrivKey().PubKey(), + false, + }, + { + "equals", + bls12381PubKey, + &bls12381.PubKey{ + Key: bls12381PubKey.Key, + }, + true, + }, + { + "different types", + bls12381PubKey, + secp256k1.GenPrivKey().PubKey(), + false, + }, + } + + for _, tc := range testCases { + t.Run(tc.msg, func(t *testing.T) { + eq := tc.pubKey.Equals(tc.other) + require.Equal(t, eq, tc.expectEq) + }) + } +} + +func TestPrivKeyEquals(t *testing.T) { + bls12381PrivKey := bls12381.GenPrivKey() + + testCases := []struct { + msg string + privKey cryptotypes.PrivKey + other cryptotypes.PrivKey + expectEq bool + }{ + { + "different bytes", + bls12381PrivKey, + bls12381.GenPrivKey(), + false, + }, + { + "equals", + bls12381PrivKey, + &bls12381.PrivKey{ + Key: bls12381PrivKey.Key, + }, + true, + }, + { + "different types", + bls12381PrivKey, + secp256k1.GenPrivKey(), + false, + }, + } + + for _, tc := range testCases { + t.Run(tc.msg, func(t *testing.T) { + eq := tc.privKey.Equals(tc.other) + require.Equal(t, eq, tc.expectEq) + }) + } +} + +func TestMarshalAmino(t *testing.T) { + aminoCdc := codec.NewLegacyAmino() + privKey := bls12381.GenPrivKey() + pubKey := privKey.PubKey().(*bls12381.PubKey) + + testCases := []struct { + desc string + msg codec.AminoMarshaler + typ interface{} + expBinary []byte + expJSON string + }{ + { + "bls12381 private key", + privKey, + &bls12381.PrivKey{}, + append([]byte{32}, privKey.Bytes()...), // Length-prefixed. + "\"" + base64.StdEncoding.EncodeToString(privKey.Bytes()) + "\"", + }, + { + "bls12381 public key", + pubKey, + &bls12381.PubKey{}, + append([]byte{96}, pubKey.Bytes()...), // Length-prefixed. + "\"" + base64.StdEncoding.EncodeToString(pubKey.Bytes()) + "\"", + }, + } + + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + // Do a round trip of encoding/decoding binary. + bz, err := aminoCdc.MarshalBinaryBare(tc.msg) + require.NoError(t, err) + require.Equal(t, tc.expBinary, bz) + + err = aminoCdc.UnmarshalBinaryBare(bz, tc.typ) + require.NoError(t, err) + + require.Equal(t, tc.msg, tc.typ) + + // Do a round trip of encoding/decoding JSON. + bz, err = aminoCdc.MarshalJSON(tc.msg) + require.NoError(t, err) + require.Equal(t, tc.expJSON, string(bz)) + + err = aminoCdc.UnmarshalJSON(bz, tc.typ) + require.NoError(t, err) + + require.Equal(t, tc.msg, tc.typ) + }) + } +} + +func TestMarshalJSON(t *testing.T) { + require := require.New(t) + privKey := bls12381.GenPrivKey() + pk := privKey.PubKey() + + registry := types.NewInterfaceRegistry() + cryptocodec.RegisterInterfaces(registry) + cdc := codec.NewProtoCodec(registry) + + bz, err := cdc.MarshalInterfaceJSON(pk) + require.NoError(err) + + var pk2 cryptotypes.PubKey + err = cdc.UnmarshalInterfaceJSON(bz, &pk2) + require.NoError(err) + require.True(pk2.Equals(pk)) +} + +func BenchmarkSignBls(b *testing.B) { + privKey := bls12381.GenPrivKey() + + bench.BenchmarkSigning(b, privKey) + +} + +func BenchmarkVerifyBls(b *testing.B) { + privKey := bls12381.GenPrivKey() + + bench.BenchmarkVerification(b, privKey) +} diff --git a/crypto/keys/bls12381/keys.pb.go b/crypto/keys/bls12381/keys.pb.go new file mode 100644 index 0000000000..9a7569ca71 --- /dev/null +++ b/crypto/keys/bls12381/keys.pb.go @@ -0,0 +1,496 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: cosmos/crypto/bls12381/keys.proto + +package bls12381 + +import ( + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// PubKey defines a bls public key +// Key is the uncompressed form of the pubkey. The first byte depends is a 0x02 byte +// if the y-coordinate is the lexicographically largest of the two associated with +// the x-coordinate. Otherwise the first byte is a 0x03. +// This prefix is followed with the x-coordinate. +type PubKey struct { + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (m *PubKey) Reset() { *m = PubKey{} } +func (*PubKey) ProtoMessage() {} +func (*PubKey) Descriptor() ([]byte, []int) { + return fileDescriptor_295d2962e809fcdb, []int{0} +} +func (m *PubKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PubKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PubKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PubKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PubKey.Merge(m, src) +} +func (m *PubKey) XXX_Size() int { + return m.Size() +} +func (m *PubKey) XXX_DiscardUnknown() { + xxx_messageInfo_PubKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PubKey proto.InternalMessageInfo + +func (m *PubKey) GetKey() []byte { + if m != nil { + return m.Key + } + return nil +} + +// PrivKey defines a bls private key. +type PrivKey struct { + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (m *PrivKey) Reset() { *m = PrivKey{} } +func (m *PrivKey) String() string { return proto.CompactTextString(m) } +func (*PrivKey) ProtoMessage() {} +func (*PrivKey) Descriptor() ([]byte, []int) { + return fileDescriptor_295d2962e809fcdb, []int{1} +} +func (m *PrivKey) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *PrivKey) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_PrivKey.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *PrivKey) XXX_Merge(src proto.Message) { + xxx_messageInfo_PrivKey.Merge(m, src) +} +func (m *PrivKey) XXX_Size() int { + return m.Size() +} +func (m *PrivKey) XXX_DiscardUnknown() { + xxx_messageInfo_PrivKey.DiscardUnknown(m) +} + +var xxx_messageInfo_PrivKey proto.InternalMessageInfo + +func (m *PrivKey) GetKey() []byte { + if m != nil { + return m.Key + } + return nil +} + +func init() { + proto.RegisterType((*PubKey)(nil), "cosmos.crypto.bls12381.PubKey") + proto.RegisterType((*PrivKey)(nil), "cosmos.crypto.bls12381.PrivKey") +} + +func init() { proto.RegisterFile("cosmos/crypto/bls12381/keys.proto", fileDescriptor_295d2962e809fcdb) } + +var fileDescriptor_295d2962e809fcdb = []byte{ + // 184 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x4c, 0xce, 0x2f, 0xce, + 0xcd, 0x2f, 0xd6, 0x4f, 0x2e, 0xaa, 0x2c, 0x28, 0xc9, 0xd7, 0x4f, 0xca, 0x29, 0x36, 0x34, 0x32, + 0xb6, 0x30, 0xd4, 0xcf, 0x4e, 0xad, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x83, + 0x28, 0xd1, 0x83, 0x28, 0xd1, 0x83, 0x29, 0x91, 0x12, 0x49, 0xcf, 0x4f, 0xcf, 0x07, 0x2b, 0xd1, + 0x07, 0xb1, 0x20, 0xaa, 0x95, 0x14, 0xb8, 0xd8, 0x02, 0x4a, 0x93, 0xbc, 0x53, 0x2b, 0x85, 0x04, + 0xb8, 0x98, 0xb3, 0x53, 0x2b, 0x25, 0x18, 0x15, 0x18, 0x35, 0x78, 0x82, 0x40, 0x4c, 0x2b, 0x96, + 0x19, 0x0b, 0xe4, 0x19, 0x94, 0xa4, 0xb9, 0xd8, 0x03, 0x8a, 0x32, 0xcb, 0xb0, 0x2a, 0x71, 0xf2, + 0x3e, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, + 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, 0x86, 0x28, 0xc3, 0xf4, 0xcc, 0x92, 0x8c, + 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0x98, 0xa3, 0xc1, 0x94, 0x6e, 0x71, 0x4a, 0x36, 0xcc, + 0xfd, 0x20, 0x67, 0xc3, 0x3d, 0x91, 0xc4, 0x06, 0x76, 0x92, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, + 0x0e, 0x2e, 0xb6, 0x08, 0xe5, 0x00, 0x00, 0x00, +} + +func (m *PubKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PubKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PubKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Key) > 0 { + i -= len(m.Key) + copy(dAtA[i:], m.Key) + i = encodeVarintKeys(dAtA, i, uint64(len(m.Key))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *PrivKey) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *PrivKey) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *PrivKey) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Key) > 0 { + i -= len(m.Key) + copy(dAtA[i:], m.Key) + i = encodeVarintKeys(dAtA, i, uint64(len(m.Key))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintKeys(dAtA []byte, offset int, v uint64) int { + offset -= sovKeys(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *PubKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Key) + if l > 0 { + n += 1 + l + sovKeys(uint64(l)) + } + return n +} + +func (m *PrivKey) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Key) + if l > 0 { + n += 1 + l + sovKeys(uint64(l)) + } + return n +} + +func sovKeys(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozKeys(x uint64) (n int) { + return sovKeys(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *PubKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowKeys + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PubKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PubKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowKeys + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthKeys + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthKeys + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...) + if m.Key == nil { + m.Key = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipKeys(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthKeys + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *PrivKey) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowKeys + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: PrivKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: PrivKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowKeys + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthKeys + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthKeys + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...) + if m.Key == nil { + m.Key = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipKeys(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthKeys + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipKeys(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowKeys + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowKeys + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowKeys + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthKeys + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupKeys + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthKeys + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthKeys = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowKeys = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupKeys = fmt.Errorf("proto: unexpected end of group") +) diff --git a/crypto/keys/bls12381/multisig.go b/crypto/keys/bls12381/multisig.go new file mode 100644 index 0000000000..e7a854c463 --- /dev/null +++ b/crypto/keys/bls12381/multisig.go @@ -0,0 +1,116 @@ +package bls12381 + +import ( + "encoding/base64" + "fmt" + + blst "github.com/supranational/blst/bindings/go" +) + +func aggregatePublicKey(pks []*PubKey) (*blst.P1Affine, error) { + pubkeys := make([]*blst.P1Affine, len(pks)) + for i, pk := range pks { + pubkeys[i] = new(blst.P1Affine).Deserialize(pk.Key) + if pubkeys[i] == nil { + return nil, fmt.Errorf("failed to deserialize public key") + } + } + + aggregator := new(blst.P1Aggregate) + b := aggregator.Aggregate(pubkeys, false) + if !b { + return nil, fmt.Errorf("failed to aggregate public keys") + } + apk := aggregator.ToAffine() + + return apk, nil +} + +// AggregateSignature combines a set of verified signatures into a single bls signature +func AggregateSignature(sigs [][]byte) ([]byte, error) { + sigmas := make([]*blst.P2Affine, len(sigs)) + for i, sig := range sigs { + sigmas[i] = new(blst.P2Affine).Uncompress(sig) + if sigmas[i] == nil { + return nil, fmt.Errorf("failed to deserialize the %d-th signature", i) + } + } + + aggregator := new(blst.P2Aggregate) + b := aggregator.Aggregate(sigmas, false) + if !b { + return nil, fmt.Errorf("failed to aggregate signatures") + } + aggSigBytes := aggregator.ToAffine().Compress() + return aggSigBytes, nil +} + +// VerifyMultiSignature assumes public key is already validated +func VerifyMultiSignature(msg []byte, sig []byte, pks []*PubKey) error { + return VerifyAggregateSignature([][]byte{msg}, false, sig, [][]*PubKey{pks}) +} + +func Unique(msgs [][]byte) bool { + if len(msgs) <= 1 { + return true + } + msgMap := make(map[string]bool, len(msgs)) + for _, msg := range msgs { + s := base64.StdEncoding.EncodeToString(msg) + if _, ok := msgMap[s]; ok { + return false + } + msgMap[s] = true + } + return true +} + +func VerifyAggregateSignature(msgs [][]byte, msgCheck bool, sig []byte, pkss [][]*PubKey) error { + n := len(msgs) + if n == 0 { + return fmt.Errorf("messages cannot be empty") + } + + for i, msg := range msgs { + if len(msg) == 0 { + return fmt.Errorf("%d-th message is empty", i) + } + } + + if len(pkss) != n { + return fmt.Errorf("the number of messages and public key sets must match") + } + + for i, pks := range pkss { + if len(pks) == 0 { + return fmt.Errorf("%d-th public key set is empty", i) + } + } + + if msgCheck { + if !Unique(msgs) { + return fmt.Errorf("messages must be pairwise distinct") + } + } + + apks := make([]*blst.P1Affine, len(pkss)) + for i, pks := range pkss { + apk, err := aggregatePublicKey(pks) + if err != nil { + return fmt.Errorf("cannot aggregate public keys: %s", err.Error()) + } + apks[i] = apk + } + + sigma := new(blst.P2Affine).Uncompress(sig) + if sigma == nil { + return fmt.Errorf("failed to deserialize signature") + } + + dst := []byte("BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_") + if !sigma.AggregateVerify(true, apks, false, msgs, dst) { + return fmt.Errorf("failed to verify signature") + } + + return nil +} diff --git a/crypto/keys/bls12381/multisig_test.go b/crypto/keys/bls12381/multisig_test.go new file mode 100644 index 0000000000..20168ee4cc --- /dev/null +++ b/crypto/keys/bls12381/multisig_test.go @@ -0,0 +1,123 @@ +package bls12381_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + bls "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" +) + +func TestBlsMultiSig(t *testing.T) { + total := 5 + pks := make([]*bls.PubKey, total) + sigs := make([][]byte, total) + msg := []byte("hello world") + for i := 0; i < total; i++ { + sk := bls.GenPrivKey() + pk, ok := sk.PubKey().(*bls.PubKey) + require.True(t, ok) + + sig, err := sk.Sign(msg) + require.Nil(t, err) + + pks[i] = pk + sigs[i] = sig + } + + aggSig, err := bls.AggregateSignature(sigs) + require.Nil(t, err) + + assert.Nil(t, bls.VerifyMultiSignature(msg, aggSig, pks)) + +} + +func TestBlsAggSig(t *testing.T) { + total := 5 + pks := make([][]*bls.PubKey, total) + sigs := make([][]byte, total) + msgs := make([][]byte, total) + for i := 0; i < total; i++ { + msgs[i] = []byte(fmt.Sprintf("message %d", i)) + sk := bls.GenPrivKey() + pk, ok := sk.PubKey().(*bls.PubKey) + require.True(t, ok) + + sig, err := sk.Sign(msgs[i]) + require.Nil(t, err) + + pks[i] = []*bls.PubKey{pk} + sigs[i] = sig + } + + aggSig, err := bls.AggregateSignature(sigs) + require.Nil(t, err) + + assert.Nil(t, bls.VerifyAggregateSignature(msgs, true, aggSig, pks)) +} + +func benchmarkBlsVerifyMulti(total int, b *testing.B) { + pks := make([]*bls.PubKey, total) + sigs := make([][]byte, total) + msg := []byte("hello world") + for i := 0; i < total; i++ { + sk := bls.GenPrivKey() + pk, ok := sk.PubKey().(*bls.PubKey) + require.True(b, ok) + + sig, err := sk.Sign(msg) + require.Nil(b, err) + + pks[i] = pk + sigs[i] = sig + } + + aggSig, err := bls.AggregateSignature(sigs) + require.Nil(b, err) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + bls.VerifyMultiSignature(msg, aggSig, pks) + } +} + +func BenchmarkBlsVerifyMulti8(b *testing.B) { benchmarkBlsVerifyMulti(8, b) } +func BenchmarkBlsVerifyMulti16(b *testing.B) { benchmarkBlsVerifyMulti(16, b) } +func BenchmarkBlsVerifyMulti32(b *testing.B) { benchmarkBlsVerifyMulti(32, b) } +func BenchmarkBlsVerifyMulti64(b *testing.B) { benchmarkBlsVerifyMulti(64, b) } +func BenchmarkBlsVerifyMulti128(b *testing.B) { benchmarkBlsVerifyMulti(128, b) } + +func benchmarkBlsVerifyAgg(total int, b *testing.B) { + pks := make([][]*bls.PubKey, total) + sigs := make([][]byte, total) + msgs := make([][]byte, total) + for i := 0; i < total; i++ { + msgs[i] = []byte(fmt.Sprintf("message %d", i)) + sk := bls.GenPrivKey() + pk, ok := sk.PubKey().(*bls.PubKey) + require.True(b, ok) + + sig, err := sk.Sign(msgs[i]) + require.Nil(b, err) + + pks[i] = []*bls.PubKey{pk} + sigs[i] = sig + } + + aggSig, err := bls.AggregateSignature(sigs) + require.Nil(b, err) + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + bls.VerifyAggregateSignature(msgs, false, aggSig, pks) + } +} + +func BenchmarkBlsVerifyAgg8(b *testing.B) { benchmarkBlsVerifyAgg(8, b) } +func BenchmarkBlsVerifyAgg16(b *testing.B) { benchmarkBlsVerifyAgg(16, b) } +func BenchmarkBlsVerifyAgg32(b *testing.B) { benchmarkBlsVerifyAgg(32, b) } +func BenchmarkBlsVerifyAgg64(b *testing.B) { benchmarkBlsVerifyAgg(64, b) } +func BenchmarkBlsVerifyAgg128(b *testing.B) { benchmarkBlsVerifyAgg(128, b) } diff --git a/crypto/keys/ed25519/ed25519_test.go b/crypto/keys/ed25519/ed25519_test.go index 59cce4066a..2291a01de8 100644 --- a/crypto/keys/ed25519/ed25519_test.go +++ b/crypto/keys/ed25519/ed25519_test.go @@ -3,6 +3,7 @@ package ed25519_test import ( stded25519 "crypto/ed25519" "encoding/base64" + bench "github.com/cosmos/cosmos-sdk/crypto/keys/internal/benchmarking" "testing" "github.com/stretchr/testify/assert" @@ -228,3 +229,16 @@ func TestMarshalAmino_BackwardsCompatibility(t *testing.T) { }) } } + +func BenchmarkSignEd25519(b *testing.B) { + privKey := ed25519.GenPrivKey() + + bench.BenchmarkSigning(b, privKey) + +} + +func BenchmarkVerifyEd25519(b *testing.B) { + privKey := ed25519.GenPrivKey() + + bench.BenchmarkVerification(b, privKey) +} diff --git a/docs/core/proto-docs.md b/docs/core/proto-docs.md index 114db56ecc..e1cc1ec4c7 100644 --- a/docs/core/proto-docs.md +++ b/docs/core/proto-docs.md @@ -168,6 +168,10 @@ - [Msg](#cosmos.crisis.v1beta1.Msg) +- [cosmos/crypto/bls12381/keys.proto](#cosmos/crypto/bls12381/keys.proto) + - [PrivKey](#cosmos.crypto.bls12381.PrivKey) + - [PubKey](#cosmos.crypto.bls12381.PubKey) + - [cosmos/crypto/ed25519/keys.proto](#cosmos/crypto/ed25519/keys.proto) - [PrivKey](#cosmos.crypto.ed25519.PrivKey) - [PubKey](#cosmos.crypto.ed25519.PubKey) @@ -1143,6 +1147,7 @@ type for additional functionality (e.g. vesting). | `pub_key` | [google.protobuf.Any](#google.protobuf.Any) | | | | `account_number` | [uint64](#uint64) | | | | `sequence` | [uint64](#uint64) | | | +| `pop_is_valid` | [bool](#bool) | | | @@ -1179,6 +1184,7 @@ Params defines the parameters for the auth module. | `tx_size_cost_per_byte` | [uint64](#uint64) | | | | `sig_verify_cost_ed25519` | [uint64](#uint64) | | | | `sig_verify_cost_secp256k1` | [uint64](#uint64) | | | +| `sig_verify_cost_bls12381` | [uint64](#uint64) | | | @@ -2782,6 +2788,56 @@ Msg defines the bank Msg service. + +

Top

+ +## cosmos/crypto/bls12381/keys.proto + + + + + +### PrivKey +PrivKey defines a bls private key. + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `key` | [bytes](#bytes) | | | + + + + + + + + +### PubKey +PubKey defines a bls public key +Key is the uncompressed form of the pubkey. The first byte depends is a 0x02 byte +if the y-coordinate is the lexicographically largest of the two associated with +the x-coordinate. Otherwise the first byte is a 0x03. +This prefix is followed with the x-coordinate. + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `key` | [bytes](#bytes) | | | + + + + + + + + + + + + + + +

Top

diff --git a/go.mod b/go.mod index dcae062fd3..4b4c677524 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ require ( github.com/DataDog/zstd v1.4.5 // indirect github.com/armon/go-metrics v0.3.8 github.com/bgentry/speakeasy v0.1.0 - github.com/btcsuite/btcd v0.21.0-beta - github.com/btcsuite/btcutil v1.0.2 + github.com/btcsuite/btcd v0.22.0-beta + github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce github.com/confio/ics23/go v0.6.6 github.com/cosmos/go-bip39 v1.0.0 github.com/cosmos/iavl v0.17.1 @@ -27,6 +27,7 @@ require ( github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/hashicorp/golang-lru v0.5.4 + github.com/lib/pq v1.10.2 // indirect github.com/magiconair/properties v1.8.5 github.com/mattn/go-isatty v0.0.12 github.com/mitchellh/mapstructure v1.3.3 // indirect @@ -45,13 +46,14 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.7.0 + github.com/supranational/blst v0.3.5 github.com/tendermint/btcd v0.1.1 github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 github.com/tendermint/go-amino v0.16.0 github.com/tendermint/tendermint v0.34.13 github.com/tendermint/tm-db v0.6.4 - golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad - google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f + golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 + google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c google.golang.org/grpc v1.40.0 google.golang.org/protobuf v1.26.0 gopkg.in/ini.v1 v1.61.0 // indirect diff --git a/go.sum b/go.sum index b48ef06495..e595cc51c3 100644 --- a/go.sum +++ b/go.sum @@ -68,13 +68,15 @@ github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edY github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/btcsuite/btcd v0.0.0-20190115013929-ed77733ec07d/go.mod h1:d3C0AkH6BRcvO8T0UEPu53cnw4IbV63x1bEjildYhO0= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= -github.com/btcsuite/btcd v0.21.0-beta h1:At9hIZdJW0s9E/fAz28nrz6AmcNlSVucCH796ZteX1M= github.com/btcsuite/btcd v0.21.0-beta/go.mod h1:ZSWyehm27aAuS9bvkATT+Xte3hjHZ+MRgMY/8NJ7K94= +github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo= +github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= -github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts= github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= +github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I= @@ -363,8 +365,9 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.2.0 h1:LXpIM/LZ5xGFhOpXAQUIMM1HdyqzVYM13zNdjCEEcA0= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= +github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs= github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= @@ -602,6 +605,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/supranational/blst v0.3.5 h1:/pey7U712GgJBSD1XTiJ5iBqjYIH3QNdrjRoGXlJJ60= +github.com/supranational/blst v0.3.5/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk= github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= @@ -635,6 +640,7 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/zondax/hid v0.9.0 h1:eiT3P6vNxAEVxXMw66eZUAAnU2zD33JBkfG/EnfAKl8= github.com/zondax/hid v0.9.0/go.mod h1:l5wttcP0jwtdLjqjMMWFVEE7d1zO0jvSPA9OPZxWpEM= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= @@ -674,8 +680,8 @@ golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201117144127-c1f2f97bffc9/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -690,6 +696,7 @@ golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHl golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= @@ -698,6 +705,7 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -728,6 +736,8 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f h1:w6wWR0H+nyVpbSAQbzVEIACVyr/h8l/BEkY6Sokc7Eg= golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -742,6 +752,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -789,8 +800,11 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fRMqdrVDbu7MQcku54gg= golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -801,6 +815,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -827,10 +842,12 @@ golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -864,8 +881,8 @@ google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEY google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20201111145450-ac7456db90a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20201119123407-9b1e624d6bc4/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f h1:izedQ6yVIc5mZsRuXzmSreCOlzI0lCU1HpG8yEdMiKw= -google.golang.org/genproto v0.0.0-20210114201628-6edceaf6022f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c h1:wtujag7C+4D6KMoulW9YauvK2lgdvCMS260jsqqBXr0= +google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o= google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= diff --git a/proto/cosmos/auth/v1beta1/auth.proto b/proto/cosmos/auth/v1beta1/auth.proto index 72e1d9ec28..9be92bd2ce 100644 --- a/proto/cosmos/auth/v1beta1/auth.proto +++ b/proto/cosmos/auth/v1beta1/auth.proto @@ -2,6 +2,7 @@ syntax = "proto3"; package cosmos.auth.v1beta1; import "cosmos_proto/cosmos.proto"; +//import "cosmos.proto"; import "gogoproto/gogo.proto"; import "google/protobuf/any.proto"; @@ -22,6 +23,7 @@ message BaseAccount { [(gogoproto.jsontag) = "public_key,omitempty", (gogoproto.moretags) = "yaml:\"public_key\""]; uint64 account_number = 3 [(gogoproto.moretags) = "yaml:\"account_number\""]; uint64 sequence = 4; + bool pop_is_valid = 5; } // ModuleAccount defines an account for modules that holds coins on a pool. @@ -47,4 +49,6 @@ message Params { [(gogoproto.customname) = "SigVerifyCostED25519", (gogoproto.moretags) = "yaml:\"sig_verify_cost_ed25519\""]; uint64 sig_verify_cost_secp256k1 = 5 [(gogoproto.customname) = "SigVerifyCostSecp256k1", (gogoproto.moretags) = "yaml:\"sig_verify_cost_secp256k1\""]; + uint64 sig_verify_cost_bls12381 = 6 + [(gogoproto.customname) = "SigVerifyCostBls12381", (gogoproto.moretags) = "yaml:\"sig_verify_cost_bls12381\""]; } diff --git a/proto/cosmos/crypto/bls12381/keys.proto b/proto/cosmos/crypto/bls12381/keys.proto new file mode 100644 index 0000000000..54740c32c9 --- /dev/null +++ b/proto/cosmos/crypto/bls12381/keys.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package cosmos.crypto.bls12381; + +import "gogoproto/gogo.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381"; + +// PubKey defines a bls public key +// Key is the uncompressed form of the pubkey. The first byte depends is a 0x02 byte +// if the y-coordinate is the lexicographically largest of the two associated with +// the x-coordinate. Otherwise the first byte is a 0x03. +// This prefix is followed with the x-coordinate. +message PubKey { + option (gogoproto.goproto_stringer) = false; + + bytes key = 1; +} + +// PrivKey defines a bls private key. +message PrivKey { + bytes key = 1; +} \ No newline at end of file diff --git a/testutil/testdata/tx.go b/testutil/testdata/tx.go index 1538460830..c58fdc70ca 100644 --- a/testutil/testdata/tx.go +++ b/testutil/testdata/tx.go @@ -3,6 +3,8 @@ package testdata import ( "encoding/json" + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -16,6 +18,14 @@ func KeyTestPubAddr() (cryptotypes.PrivKey, cryptotypes.PubKey, sdk.AccAddress) return key, pub, addr } +// KeyTestPubAddrBls12381 generates a new secp256k1 keypair. +func KeyTestPubAddrBls12381() (cryptotypes.PrivKey, cryptotypes.PubKey, sdk.AccAddress) { + key := bls12381.GenPrivKey() + pub := key.PubKey() + addr := sdk.AccAddress(pub.Address()) + return key, pub, addr +} + // NewTestFeeAmount is a test fee amount. func NewTestFeeAmount() sdk.Coins { return sdk.NewCoins(sdk.NewInt64Coin("atom", 150)) @@ -23,7 +33,7 @@ func NewTestFeeAmount() sdk.Coins { // NewTestGasLimit is a test fee gas limit. func NewTestGasLimit() uint64 { - return 100000 + return 150000 } // NewTestMsg creates a message for testing with the given signers. diff --git a/types/errors/errors.go b/types/errors/errors.go index 026f5f569b..f2a699c53e 100644 --- a/types/errors/errors.go +++ b/types/errors/errors.go @@ -134,6 +134,9 @@ var ( // supported. ErrNotSupported = Register(RootCodespace, 37, "feature not supported") + // ErrInvalidPop to doc + ErrInvalidPop = Register(RootCodespace, 111000, "invalid pop for public key") + // ErrPanic is only set when we recover from a panic, so we know to // redact potentially sensitive system info ErrPanic = Register(UndefinedCodespace, 111222, "panic") diff --git a/x/airdrop/types/airdrop.pb.go b/x/airdrop/types/airdrop.pb.go index fcc8c4c5c8..d92faf136e 100644 --- a/x/airdrop/types/airdrop.pb.go +++ b/x/airdrop/types/airdrop.pb.go @@ -5,15 +5,14 @@ package types import ( fmt "fmt" - io "io" - math "math" - math_bits "math/bits" - github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" types "github.com/cosmos/cosmos-sdk/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" _ "github.com/regen-network/cosmos-proto" + io "io" + math "math" + math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/airdrop/types/genesis.pb.go b/x/airdrop/types/genesis.pb.go index 14f9227735..c390336d28 100644 --- a/x/airdrop/types/genesis.pb.go +++ b/x/airdrop/types/genesis.pb.go @@ -5,12 +5,11 @@ package types import ( fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" io "io" math "math" math_bits "math/bits" - - _ "github.com/gogo/protobuf/gogoproto" - proto "github.com/gogo/protobuf/proto" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/airdrop/types/query.pb.go b/x/airdrop/types/query.pb.go index 9df8650bd1..782aabf26c 100644 --- a/x/airdrop/types/query.pb.go +++ b/x/airdrop/types/query.pb.go @@ -6,10 +6,6 @@ package types import ( context "context" fmt "fmt" - io "io" - math "math" - math_bits "math/bits" - query "github.com/cosmos/cosmos-sdk/types/query" _ "github.com/gogo/protobuf/gogoproto" grpc1 "github.com/gogo/protobuf/grpc" @@ -18,6 +14,9 @@ import ( grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/airdrop/types/tx.pb.go b/x/airdrop/types/tx.pb.go index b4c18b2a0b..748f1dbc5a 100644 --- a/x/airdrop/types/tx.pb.go +++ b/x/airdrop/types/tx.pb.go @@ -6,16 +6,15 @@ package types import ( context "context" fmt "fmt" - io "io" - math "math" - math_bits "math/bits" - _ "github.com/gogo/protobuf/gogoproto" grpc1 "github.com/gogo/protobuf/grpc" proto "github.com/gogo/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" ) // Reference imports to suppress errors if they are not otherwise used. diff --git a/x/auth/ante/ante.go b/x/auth/ante/ante.go index 8cc025bad2..ee474b8fbc 100644 --- a/x/auth/ante/ante.go +++ b/x/auth/ante/ante.go @@ -28,6 +28,7 @@ func NewAnteHandler( NewDeductFeeDecorator(ak, bankKeeper), NewSigGasConsumeDecorator(ak, sigGasConsumer), NewSigVerificationDecorator(ak, signModeHandler), + NewSetPopValidDecorator(ak), NewIncrementSequenceDecorator(ak), ) } diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 7659f3e104..50a71141ef 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -562,7 +562,7 @@ func (suite *AnteTestSuite) TestAnteHandlerMemoGas() { "tx with memo has enough gas", func() { feeAmount = sdk.NewCoins(sdk.NewInt64Coin("atom", 0)) - gasLimit = 50000 + gasLimit = 60000 suite.txBuilder.SetMemo(strings.Repeat("0123456789", 10)) }, false, @@ -1091,9 +1091,9 @@ func (suite *AnteTestSuite) TestAnteHandlerReCheck() { name string params types.Params }{ - {"memo size check", types.NewParams(1, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, - {"txsize check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, 10000000, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1)}, - {"sig verify cost check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, 100000000)}, + {"memo size check", types.NewParams(1, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381)}, + {"txsize check", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, 10000000, types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381)}, + {"sig verify cost check secp256k1", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, types.DefaultSigVerifyCostED25519, 100000000, types.DefaultSigVerifyCostBls12381)}, } for _, tc := range testCases { // set testcase parameters diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index 6aa0c2a345..5ef9ffbfae 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "fmt" + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -84,6 +85,15 @@ func (spkd SetPubKeyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b if acc.GetPubKey() != nil { continue } + + // Validate public key for bls12381 so that the public key only needs to be checked once + pubkey, ok := pk.(*bls12381.PubKey) + if ok { + if !pubkey.Validate() { + return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "Invalid public key: either infinity or not subgroup element") + } + } + err = acc.SetPubKey(pk) if err != nil { return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, err.Error()) @@ -308,6 +318,43 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul return next(ctx, tx, simulate) } +// SetPopValidDecorator handles the validation status of the proof-of-possession (POP) of an individual public key. +// A valid transaction and signature can be viewed as a POP for the signer's public key. +// POP is required when forming a compact multisig group in order to prevent rogue public key attacks. +type SetPopValidDecorator struct { + ak AccountKeeper +} + +func NewSetPopValidDecorator(ak AccountKeeper) SetPopValidDecorator { + return SetPopValidDecorator{ + ak: ak, + } +} + +func (spvd SetPopValidDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { + sigTx, ok := tx.(authsigning.SigVerifiableTx) + if !ok { + return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "invalid transaction type") + } + + for _, addr := range sigTx.GetSigners() { + acc := spvd.ak.GetAccount(ctx, addr) + pk := acc.GetPubKey() + + switch pk.(type) { + case *bls12381.PubKey, *secp256k1.PubKey, *ed25519.PubKey: + if !acc.GetPopValid() { + if err := acc.SetPopValid(true); err != nil { + return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidPop, err.Error()) + } + spvd.ak.SetAccount(ctx, acc) + } + } + } + + return next(ctx, tx, simulate) +} + // IncrementSequenceDecorator handles incrementing sequences of all signers. // Use the IncrementSequenceDecorator decorator to prevent replay attacks. Note, // there is no need to execute IncrementSequenceDecorator on RecheckTX since @@ -408,6 +455,10 @@ func DefaultSigVerificationGasConsumer( } return nil + case *bls12381.PubKey: + meter.ConsumeGas(params.SigVerifyCostBls12381, "ante verify: bls12381") + return nil + default: return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey) } diff --git a/x/auth/legacy/v040/migrate.go b/x/auth/legacy/v040/migrate.go index 363ec7ba82..f7ef9b4c92 100644 --- a/x/auth/legacy/v040/migrate.go +++ b/x/auth/legacy/v040/migrate.go @@ -8,6 +8,9 @@ import ( v040vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" ) +// blsCostMultiplier is used to calculate bls signature verification cost = blsCostMultiplier * SigVerifyCostED25519 +const blsCostMultiplier = 10 + // convertBaseAccount converts a 0.39 BaseAccount to a 0.40 BaseAccount. func convertBaseAccount(old *v039auth.BaseAccount) *v040auth.BaseAccount { var any *codectypes.Any @@ -118,6 +121,7 @@ func Migrate(authGenState v039auth.GenesisState) *v040auth.GenesisState { MaxMemoCharacters: authGenState.Params.MaxMemoCharacters, TxSigLimit: authGenState.Params.TxSigLimit, TxSizeCostPerByte: authGenState.Params.TxSizeCostPerByte, + SigVerifyCostBls12381: blsCostMultiplier * authGenState.Params.SigVerifyCostED25519, SigVerifyCostED25519: authGenState.Params.SigVerifyCostED25519, SigVerifyCostSecp256k1: authGenState.Params.SigVerifyCostSecp256k1, }, diff --git a/x/auth/legacy/v040/migrate_test.go b/x/auth/legacy/v040/migrate_test.go index e241046095..1cbe94276c 100644 --- a/x/auth/legacy/v040/migrate_test.go +++ b/x/auth/legacy/v040/migrate_test.go @@ -89,6 +89,7 @@ func TestMigrate(t *testing.T) { "@type": "/cosmos.auth.v1beta1.BaseAccount", "account_number": "1", "address": "cosmos13syh7de9xndv9wmklccpfvc0d8dcyvay4s6z6l", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "A8oWyJkohwy8XZ0Df92jFMBTtTPMvYJplYIrlEHTKPYk" @@ -100,6 +101,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1v57fx2l2rt6ehujuu99u2fw05779m5e2ux4z2h", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "AruDygh5HprMOpHOEato85dLgAsybMJVyxBGUa3KuWCr" @@ -116,6 +118,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos18hnp9fjflrkeeqn4gmhjhzljusxzmjeartdckw", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "A5aEFDIdQHh0OYmNXNv1sHBNURDWWgVkXC2IALcWLLwJ" @@ -148,6 +151,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1t9kvvejvk6hjtddx6antck39s206csqduq3ke3", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "AoXDzxwTnljemHxfnJcwrKqODBP6Q2l3K3U3UhVDzyah" @@ -172,6 +176,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1s4ss9zquz7skvguechzlk3na635jdrecl0sgy2", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "A2a4P4TQ1OKzpfu0eKnCoEtmTvoiclSx0G9higenUGws" @@ -207,6 +212,7 @@ func TestMigrate(t *testing.T) { "base_account": { "account_number": "1", "address": "cosmos1mcc6rwrj4hswf8p9ct82c7lmf77w9tuk07rha4", + "pop_is_valid": false, "pub_key": { "@type": "/cosmos.crypto.secp256k1.PubKey", "key": "A4tuAfmZlhjK5cjp6ImR704miybHnITVNOyJORdDPFu3" @@ -228,12 +234,14 @@ func TestMigrate(t *testing.T) { "@type": "/cosmos.auth.v1beta1.BaseAccount", "account_number": "1", "address": "cosmos16ydaqh0fcnh4qt7a3jme4mmztm2qel5axcpw00", + "pop_is_valid": false, "pub_key": null, "sequence": "0" } ], "params": { "max_memo_characters": "10", + "sig_verify_cost_bls12381": "400", "sig_verify_cost_ed25519": "40", "sig_verify_cost_secp256k1": "50", "tx_sig_limit": "20", diff --git a/x/auth/simulation/genesis.go b/x/auth/simulation/genesis.go index 2da36b54fa..c0311e66a2 100644 --- a/x/auth/simulation/genesis.go +++ b/x/auth/simulation/genesis.go @@ -19,6 +19,7 @@ const ( TxSizeCostPerByte = "tx_size_cost_per_byte" SigVerifyCostED25519 = "sig_verify_cost_ed25519" SigVerifyCostSECP256K1 = "sig_verify_cost_secp256k1" + SigVerifyCostBLS12381 = "sig_verify_cost_bls12381" ) // RandomGenesisAccounts defines the default RandomGenesisAccountsFn used on the SDK. @@ -87,6 +88,11 @@ func GenSigVerifyCostSECP256K1(r *rand.Rand) uint64 { return uint64(simulation.RandIntBetween(r, 500, 1000)) } +// GenSigVerifyCostBLS12381 randomized SigVerifyCostBLS12381 +func GenSigVerifyCostBLS12381(r *rand.Rand) uint64 { + return uint64(simulation.RandIntBetween(r, 6000, 12000)) +} + // RandomizedGenState generates a random GenesisState for auth func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn types.RandomGenesisAccountsFn) { var maxMemoChars uint64 @@ -119,8 +125,14 @@ func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn type func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) }, ) + var sigVerifyCostBLS12381 uint64 + simState.AppParams.GetOrGenerate( + simState.Cdc, SigVerifyCostBLS12381, &sigVerifyCostBLS12381, simState.Rand, + func(r *rand.Rand) { sigVerifyCostBLS12381 = GenSigVerifyCostBLS12381(r) }, + ) + params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte, - sigVerifyCostED25519, sigVerifyCostSECP256K1) + sigVerifyCostED25519, sigVerifyCostSECP256K1, sigVerifyCostBLS12381) genesisAccs := randGenAccountsFn(simState) authGenesis := types.NewGenesisState(params, genesisAccs) diff --git a/x/auth/types/account.go b/x/auth/types/account.go index eb9939ffce..9b1b89ec6c 100644 --- a/x/auth/types/account.go +++ b/x/auth/types/account.go @@ -32,6 +32,7 @@ func NewBaseAccount(address sdk.AccAddress, pubKey cryptotypes.PubKey, accountNu Address: address.String(), AccountNumber: accountNumber, Sequence: sequence, + PopIsValid: false, } err := acc.SetPubKey(pubKey) @@ -117,6 +118,20 @@ func (acc *BaseAccount) SetSequence(seq uint64) error { return nil } +// SetPopValid - Implements sdk.AccountI. +func (acc *BaseAccount) SetPopValid(isValid bool) error { + if acc.PubKey == nil { + return errors.New("public key is not set yet") + } + acc.PopIsValid = isValid + return nil +} + +// GetPopValid - Implements sdk.AccountI. +func (acc *BaseAccount) GetPopValid() bool { + return acc.PopIsValid +} + // Validate checks for errors on the account fields func (acc BaseAccount) Validate() error { if acc.Address == "" || acc.PubKey == nil { @@ -222,6 +237,11 @@ func (ma ModuleAccount) SetSequence(seq uint64) error { return fmt.Errorf("not supported for module accounts") } +// SetPopValid - Implements AccountI +func (ma ModuleAccount) SetPopValid(isValid bool) error { + return fmt.Errorf("not supported for module accounts") +} + // Validate checks for errors on the account fields func (ma ModuleAccount) Validate() error { if strings.TrimSpace(ma.Name) == "" { @@ -324,6 +344,9 @@ type AccountI interface { GetSequence() uint64 SetSequence(uint64) error + GetPopValid() bool + SetPopValid(bool) error + // Ensure that account implements stringer String() string } diff --git a/x/auth/types/auth.pb.go b/x/auth/types/auth.pb.go index fa52ac5a8b..c8afc4e899 100644 --- a/x/auth/types/auth.pb.go +++ b/x/auth/types/auth.pb.go @@ -33,6 +33,7 @@ type BaseAccount struct { PubKey *types.Any `protobuf:"bytes,2,opt,name=pub_key,json=pubKey,proto3" json:"public_key,omitempty" yaml:"public_key"` AccountNumber uint64 `protobuf:"varint,3,opt,name=account_number,json=accountNumber,proto3" json:"account_number,omitempty" yaml:"account_number"` Sequence uint64 `protobuf:"varint,4,opt,name=sequence,proto3" json:"sequence,omitempty"` + PopIsValid bool `protobuf:"varint,5,opt,name=pop_is_valid,json=popIsValid,proto3" json:"pop_is_valid,omitempty"` } func (m *BaseAccount) Reset() { *m = BaseAccount{} } @@ -113,6 +114,7 @@ type Params struct { TxSizeCostPerByte uint64 `protobuf:"varint,3,opt,name=tx_size_cost_per_byte,json=txSizeCostPerByte,proto3" json:"tx_size_cost_per_byte,omitempty" yaml:"tx_size_cost_per_byte"` SigVerifyCostED25519 uint64 `protobuf:"varint,4,opt,name=sig_verify_cost_ed25519,json=sigVerifyCostEd25519,proto3" json:"sig_verify_cost_ed25519,omitempty" yaml:"sig_verify_cost_ed25519"` SigVerifyCostSecp256k1 uint64 `protobuf:"varint,5,opt,name=sig_verify_cost_secp256k1,json=sigVerifyCostSecp256k1,proto3" json:"sig_verify_cost_secp256k1,omitempty" yaml:"sig_verify_cost_secp256k1"` + SigVerifyCostBls12381 uint64 `protobuf:"varint,6,opt,name=sig_verify_cost_bls12381,json=sigVerifyCostBls12381,proto3" json:"sig_verify_cost_bls12381,omitempty" yaml:"sig_verify_cost_bls12381"` } func (m *Params) Reset() { *m = Params{} } @@ -182,6 +184,13 @@ func (m *Params) GetSigVerifyCostSecp256k1() uint64 { return 0 } +func (m *Params) GetSigVerifyCostBls12381() uint64 { + if m != nil { + return m.SigVerifyCostBls12381 + } + return 0 +} + func init() { proto.RegisterType((*BaseAccount)(nil), "cosmos.auth.v1beta1.BaseAccount") proto.RegisterType((*ModuleAccount)(nil), "cosmos.auth.v1beta1.ModuleAccount") @@ -191,49 +200,53 @@ func init() { func init() { proto.RegisterFile("cosmos/auth/v1beta1/auth.proto", fileDescriptor_7e1f7e915d020d2d) } var fileDescriptor_7e1f7e915d020d2d = []byte{ - // 674 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x54, 0x4d, 0x4f, 0xdb, 0x4a, - 0x14, 0x8d, 0x5f, 0xf2, 0xf8, 0x98, 0x00, 0x12, 0x26, 0x80, 0x93, 0xf7, 0x64, 0x5b, 0x5e, 0xe5, - 0x49, 0x2f, 0x8e, 0x92, 0x8a, 0x4a, 0x64, 0x51, 0x15, 0xd3, 0x2e, 0x50, 0x0b, 0x42, 0x46, 0xea, - 0xa2, 0xaa, 0xe4, 0x8e, 0x9d, 0xc1, 0x58, 0x64, 0x32, 0xc6, 0x33, 0x46, 0x31, 0xbf, 0xa0, 0xcb, - 0x2e, 0xbb, 0xe4, 0x47, 0xf0, 0x0f, 0xba, 0xe9, 0x12, 0xb1, 0xea, 0xca, 0xad, 0xc2, 0xa6, 0xea, - 0x32, 0xfb, 0x4a, 0x95, 0x67, 0x9c, 0x90, 0xa0, 0x74, 0x95, 0xb9, 0xe7, 0x9c, 0x7b, 0xee, 0x9d, - 0x7b, 0xe3, 0x01, 0xaa, 0x47, 0x28, 0x26, 0xb4, 0x09, 0x63, 0x76, 0xd6, 0xbc, 0x6c, 0xb9, 0x88, - 0xc1, 0x16, 0x0f, 0xcc, 0x30, 0x22, 0x8c, 0xc8, 0x1b, 0x82, 0x37, 0x39, 0x94, 0xf3, 0xb5, 0xaa, - 0x00, 0x1d, 0x2e, 0x69, 0xe6, 0x0a, 0x1e, 0xd4, 0x2a, 0x3e, 0xf1, 0x89, 0xc0, 0xb3, 0x53, 0x8e, - 0x56, 0x7d, 0x42, 0xfc, 0x1e, 0x6a, 0xf2, 0xc8, 0x8d, 0x4f, 0x9b, 0xb0, 0x9f, 0x08, 0xca, 0xf8, - 0x25, 0x81, 0xb2, 0x05, 0x29, 0xda, 0xf3, 0x3c, 0x12, 0xf7, 0x99, 0xac, 0x80, 0x45, 0xd8, 0xed, - 0x46, 0x88, 0x52, 0x45, 0xd2, 0xa5, 0xfa, 0xb2, 0x3d, 0x0e, 0xe5, 0x77, 0x60, 0x31, 0x8c, 0x5d, - 0xe7, 0x1c, 0x25, 0xca, 0x5f, 0xba, 0x54, 0x2f, 0xb7, 0x2b, 0xa6, 0xb0, 0x35, 0xc7, 0xb6, 0xe6, - 0x5e, 0x3f, 0xb1, 0x1a, 0x3f, 0x53, 0xad, 0x12, 0xc6, 0x6e, 0x2f, 0xf0, 0x32, 0xed, 0xff, 0x04, - 0x07, 0x0c, 0xe1, 0x90, 0x25, 0xa3, 0x54, 0x5b, 0x4f, 0x20, 0xee, 0x75, 0x8c, 0x07, 0xd6, 0xb0, - 0x17, 0xc2, 0xd8, 0x7d, 0x85, 0x12, 0xf9, 0x39, 0x58, 0x83, 0xa2, 0x05, 0xa7, 0x1f, 0x63, 0x17, - 0x45, 0x4a, 0x51, 0x97, 0xea, 0x25, 0xab, 0x3a, 0x4a, 0xb5, 0x4d, 0x91, 0x36, 0xcb, 0x1b, 0xf6, - 0x6a, 0x0e, 0x1c, 0xf1, 0x58, 0xae, 0x81, 0x25, 0x8a, 0x2e, 0x62, 0xd4, 0xf7, 0x90, 0x52, 0xca, - 0x72, 0xed, 0x49, 0xdc, 0x51, 0x3e, 0x5c, 0x6b, 0x85, 0x4f, 0xd7, 0x5a, 0xe1, 0xc7, 0xb5, 0x56, - 0xb8, 0xbb, 0x69, 0x2c, 0xe5, 0xd7, 0x3d, 0x30, 0x3e, 0x4b, 0x60, 0xf5, 0x90, 0x74, 0xe3, 0xde, - 0x64, 0x02, 0xef, 0xc1, 0x8a, 0x0b, 0x29, 0x72, 0x72, 0x77, 0x3e, 0x86, 0x72, 0x5b, 0x37, 0xe7, - 0x6c, 0xc2, 0x9c, 0x9a, 0x9c, 0xf5, 0xcf, 0x6d, 0xaa, 0x49, 0xa3, 0x54, 0xdb, 0x10, 0xdd, 0x4e, - 0x7b, 0x18, 0x76, 0xd9, 0x9d, 0x9a, 0xb1, 0x0c, 0x4a, 0x7d, 0x88, 0x11, 0x1f, 0xe3, 0xb2, 0xcd, - 0xcf, 0xb2, 0x0e, 0xca, 0x21, 0x8a, 0x70, 0x40, 0x69, 0x40, 0xfa, 0x54, 0x29, 0xea, 0xc5, 0xfa, - 0xb2, 0x3d, 0x0d, 0x75, 0x6a, 0xe3, 0x3b, 0xdc, 0xdd, 0x34, 0xd6, 0x66, 0x5a, 0x3e, 0x30, 0xbe, - 0x15, 0xc1, 0xc2, 0x31, 0x8c, 0x20, 0xa6, 0xf2, 0x11, 0xd8, 0xc0, 0x70, 0xe0, 0x60, 0x84, 0x89, - 0xe3, 0x9d, 0xc1, 0x08, 0x7a, 0x0c, 0x45, 0x62, 0x99, 0x25, 0x4b, 0x1d, 0xa5, 0x5a, 0x4d, 0xf4, - 0x37, 0x47, 0x64, 0xd8, 0xeb, 0x18, 0x0e, 0x0e, 0x11, 0x26, 0xfb, 0x13, 0x4c, 0xde, 0x05, 0x2b, - 0x6c, 0xe0, 0xd0, 0xc0, 0x77, 0x7a, 0x01, 0x0e, 0x18, 0x6f, 0xba, 0x64, 0x6d, 0x3f, 0x5c, 0x74, - 0x9a, 0x35, 0x6c, 0xc0, 0x06, 0x27, 0x81, 0xff, 0x3a, 0x0b, 0x64, 0x1b, 0x6c, 0x72, 0xf2, 0x0a, - 0x39, 0x1e, 0xa1, 0xcc, 0x09, 0x51, 0xe4, 0xb8, 0x09, 0x43, 0xf9, 0x6a, 0xf5, 0x51, 0xaa, 0xfd, - 0x3b, 0xe5, 0xf1, 0x58, 0x66, 0xd8, 0xeb, 0x99, 0xd9, 0x15, 0xda, 0x27, 0x94, 0x1d, 0xa3, 0xc8, - 0x4a, 0x18, 0x92, 0x2f, 0xc0, 0x76, 0x56, 0xed, 0x12, 0x45, 0xc1, 0x69, 0x22, 0xf4, 0xa8, 0xdb, - 0xde, 0xd9, 0x69, 0xed, 0x8a, 0xa5, 0x5b, 0x9d, 0x61, 0xaa, 0x55, 0x4e, 0x02, 0xff, 0x0d, 0x57, - 0x64, 0xa9, 0x2f, 0x5f, 0x70, 0x7e, 0x94, 0x6a, 0xaa, 0xa8, 0xf6, 0x07, 0x03, 0xc3, 0xae, 0xd0, - 0x99, 0x3c, 0x01, 0xcb, 0x09, 0xa8, 0x3e, 0xce, 0xa0, 0xc8, 0x0b, 0xdb, 0x3b, 0x4f, 0xcf, 0x5b, - 0xca, 0xdf, 0xbc, 0xe8, 0xb3, 0x61, 0xaa, 0x6d, 0xcd, 0x14, 0x3d, 0x19, 0x2b, 0x46, 0xa9, 0xa6, - 0xcf, 0x2f, 0x3b, 0x31, 0x31, 0xec, 0x2d, 0x3a, 0x37, 0xb7, 0xb3, 0x94, 0xff, 0x67, 0x25, 0x6b, - 0xff, 0xcb, 0x50, 0x95, 0x6e, 0x87, 0xaa, 0xf4, 0x7d, 0xa8, 0x4a, 0x1f, 0xef, 0xd5, 0xc2, 0xed, - 0xbd, 0x5a, 0xf8, 0x7a, 0xaf, 0x16, 0xde, 0xfe, 0xe7, 0x07, 0xec, 0x2c, 0x76, 0x4d, 0x8f, 0xe0, - 0xfc, 0x2d, 0xc8, 0x7f, 0x1a, 0xb4, 0x7b, 0xde, 0x1c, 0x88, 0xa7, 0x85, 0x25, 0x21, 0xa2, 0xee, - 0x02, 0xff, 0x52, 0x9f, 0xfc, 0x0e, 0x00, 0x00, 0xff, 0xff, 0x49, 0x90, 0x16, 0xd9, 0x76, 0x04, + // 738 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x54, 0xcf, 0x4e, 0xe3, 0x46, + 0x18, 0x8f, 0x21, 0x0d, 0x61, 0x02, 0x48, 0x98, 0x04, 0x9c, 0xb4, 0xf2, 0x58, 0x3e, 0xa5, 0x52, + 0xe3, 0x28, 0x41, 0x54, 0x25, 0xaa, 0xaa, 0x62, 0xda, 0x03, 0x6a, 0x41, 0xc8, 0x48, 0x1c, 0xaa, + 0x4a, 0xde, 0xb1, 0x33, 0x18, 0x8b, 0x38, 0x63, 0x3c, 0x63, 0x14, 0xf3, 0x04, 0x7b, 0xdc, 0xe3, + 0xde, 0x96, 0x87, 0xe0, 0x0d, 0xf6, 0xb2, 0x47, 0xc4, 0x61, 0xb5, 0x27, 0x6b, 0x15, 0x2e, 0xab, + 0x3d, 0xfa, 0x09, 0x56, 0x1e, 0x3b, 0x21, 0x41, 0xd9, 0x53, 0xfc, 0xfd, 0xbe, 0xdf, 0x9f, 0x99, + 0x6f, 0x26, 0x03, 0x64, 0x9b, 0x50, 0x8f, 0xd0, 0x36, 0x0a, 0xd9, 0x65, 0xfb, 0xa6, 0x63, 0x61, + 0x86, 0x3a, 0xbc, 0xd0, 0xfc, 0x80, 0x30, 0x22, 0x6e, 0x65, 0x7d, 0x8d, 0x43, 0x79, 0xbf, 0x51, + 0xcf, 0x40, 0x93, 0x53, 0xda, 0x39, 0x83, 0x17, 0x8d, 0xaa, 0x43, 0x1c, 0x92, 0xe1, 0xe9, 0x57, + 0x8e, 0xd6, 0x1d, 0x42, 0x9c, 0x01, 0x6e, 0xf3, 0xca, 0x0a, 0x2f, 0xda, 0x68, 0x18, 0x65, 0x2d, + 0xf5, 0xdd, 0x12, 0xa8, 0xe8, 0x88, 0xe2, 0x03, 0xdb, 0x26, 0xe1, 0x90, 0x89, 0x12, 0x58, 0x41, + 0xfd, 0x7e, 0x80, 0x29, 0x95, 0x04, 0x45, 0x68, 0xae, 0x1a, 0x93, 0x52, 0xfc, 0x1f, 0xac, 0xf8, + 0xa1, 0x65, 0x5e, 0xe1, 0x48, 0x5a, 0x52, 0x84, 0x66, 0xa5, 0x5b, 0xd5, 0x32, 0x5b, 0x6d, 0x62, + 0xab, 0x1d, 0x0c, 0x23, 0xbd, 0xf5, 0x35, 0x86, 0x55, 0x3f, 0xb4, 0x06, 0xae, 0x9d, 0x72, 0x7f, + 0x21, 0x9e, 0xcb, 0xb0, 0xe7, 0xb3, 0x28, 0x89, 0xe1, 0x66, 0x84, 0xbc, 0x41, 0x4f, 0x7d, 0xee, + 0xaa, 0x46, 0xc9, 0x0f, 0xad, 0x7f, 0x70, 0x24, 0xfe, 0x09, 0x36, 0x50, 0xb6, 0x04, 0x73, 0x18, + 0x7a, 0x16, 0x0e, 0xa4, 0x65, 0x45, 0x68, 0x16, 0xf5, 0x7a, 0x12, 0xc3, 0x5a, 0x26, 0x9b, 0xef, + 0xab, 0xc6, 0x7a, 0x0e, 0x9c, 0xf0, 0x5a, 0x6c, 0x80, 0x32, 0xc5, 0xd7, 0x21, 0x1e, 0xda, 0x58, + 0x2a, 0xa6, 0x5a, 0x63, 0x5a, 0x8b, 0x0a, 0x58, 0xf3, 0x89, 0x6f, 0xba, 0xd4, 0xbc, 0x41, 0x03, + 0xb7, 0x2f, 0xfd, 0xa0, 0x08, 0xcd, 0xb2, 0x01, 0x7c, 0xe2, 0x1f, 0xd1, 0xf3, 0x14, 0xe9, 0x49, + 0xaf, 0xef, 0x60, 0xe1, 0xed, 0x1d, 0x2c, 0x7c, 0xb9, 0x83, 0x85, 0xc7, 0xfb, 0x56, 0x39, 0x1f, + 0xc8, 0x91, 0xfa, 0x5e, 0x00, 0xeb, 0xc7, 0xa4, 0x1f, 0x0e, 0xa6, 0x33, 0x7a, 0x05, 0xd6, 0x2c, + 0x44, 0xb1, 0x99, 0xe7, 0xf3, 0x41, 0x55, 0xba, 0x8a, 0xb6, 0xe0, 0xac, 0xb4, 0x99, 0xd9, 0xea, + 0x3f, 0x3e, 0xc4, 0x50, 0x48, 0x62, 0xb8, 0x95, 0xed, 0x67, 0xd6, 0x43, 0x35, 0x2a, 0xd6, 0xcc, + 0x29, 0x88, 0xa0, 0x38, 0x44, 0x1e, 0xe6, 0x83, 0x5e, 0x35, 0xf8, 0xb7, 0xa8, 0x80, 0x8a, 0x8f, + 0x03, 0xcf, 0xa5, 0xd4, 0x25, 0x43, 0x2a, 0x2d, 0x2b, 0xcb, 0xcd, 0x55, 0x63, 0x16, 0xea, 0x35, + 0x26, 0x7b, 0x78, 0xbc, 0x6f, 0x6d, 0xcc, 0x2d, 0xf9, 0x48, 0xfd, 0x58, 0x04, 0xa5, 0x53, 0x14, + 0x20, 0x8f, 0x8a, 0x27, 0x60, 0xcb, 0x43, 0x23, 0xd3, 0xc3, 0x1e, 0x31, 0xed, 0x4b, 0x14, 0x20, + 0x9b, 0xe1, 0x20, 0x3b, 0xee, 0xa2, 0x2e, 0x27, 0x31, 0x6c, 0x64, 0xeb, 0x5b, 0x40, 0x52, 0x8d, + 0x4d, 0x0f, 0x8d, 0x8e, 0xb1, 0x47, 0x0e, 0xa7, 0x98, 0xb8, 0x0f, 0xd6, 0xd8, 0xc8, 0xa4, 0xae, + 0x63, 0x0e, 0x5c, 0xcf, 0x65, 0x7c, 0xd1, 0x45, 0x7d, 0xe7, 0x79, 0xa3, 0xb3, 0x5d, 0xd5, 0x00, + 0x6c, 0x74, 0xe6, 0x3a, 0xff, 0xa6, 0x85, 0x68, 0x80, 0x1a, 0x6f, 0xde, 0x62, 0xd3, 0x26, 0x94, + 0x99, 0x3e, 0x0e, 0x4c, 0x2b, 0x62, 0x38, 0x3f, 0x7c, 0x25, 0x89, 0xe1, 0x4f, 0x33, 0x1e, 0x2f, + 0x69, 0xaa, 0xb1, 0x99, 0x9a, 0xdd, 0xe2, 0x43, 0x42, 0xd9, 0x29, 0x0e, 0xf4, 0x88, 0x61, 0xf1, + 0x1a, 0xec, 0xa4, 0x69, 0x37, 0x38, 0x70, 0x2f, 0xa2, 0x8c, 0x8f, 0xfb, 0xdd, 0xbd, 0xbd, 0xce, + 0x7e, 0x76, 0x2d, 0xf4, 0xde, 0x38, 0x86, 0xd5, 0x33, 0xd7, 0x39, 0xe7, 0x8c, 0x54, 0xfa, 0xf7, + 0x5f, 0xbc, 0x9f, 0xc4, 0x50, 0xce, 0xd2, 0xbe, 0x63, 0xa0, 0x1a, 0x55, 0x3a, 0xa7, 0xcb, 0x60, + 0x31, 0x02, 0xf5, 0x97, 0x0a, 0x8a, 0x6d, 0xbf, 0xbb, 0xf7, 0xeb, 0x55, 0x87, 0xdf, 0xb5, 0xa2, + 0xfe, 0xc7, 0x38, 0x86, 0xdb, 0x73, 0xa1, 0x67, 0x13, 0x46, 0x12, 0x43, 0x65, 0x71, 0xec, 0xd4, + 0x44, 0x35, 0xb6, 0xe9, 0x42, 0xad, 0x18, 0x02, 0xe9, 0xa5, 0xca, 0x1a, 0xd0, 0x4e, 0x77, 0xf7, + 0xb7, 0x8e, 0x54, 0xe2, 0xc9, 0xbf, 0x8f, 0x63, 0x58, 0x9b, 0x4b, 0xd6, 0x73, 0x42, 0x12, 0x43, + 0xb8, 0x38, 0x78, 0x62, 0xa1, 0x1a, 0x35, 0xba, 0x48, 0xd9, 0x2b, 0xe7, 0x7f, 0x15, 0x41, 0x3f, + 0xfc, 0x30, 0x96, 0x85, 0x87, 0xb1, 0x2c, 0x7c, 0x1e, 0xcb, 0xc2, 0x9b, 0x27, 0xb9, 0xf0, 0xf0, + 0x24, 0x17, 0x3e, 0x3d, 0xc9, 0x85, 0xff, 0x7e, 0x76, 0x5c, 0x76, 0x19, 0x5a, 0x9a, 0x4d, 0xbc, + 0xfc, 0x91, 0xca, 0x7f, 0x5a, 0xb4, 0x7f, 0xd5, 0x1e, 0x65, 0x6f, 0x1e, 0x8b, 0x7c, 0x4c, 0xad, + 0x12, 0x7f, 0x42, 0x76, 0xbf, 0x05, 0x00, 0x00, 0xff, 0xff, 0xd9, 0x96, 0xbb, 0xf8, 0x0f, 0x05, 0x00, 0x00, } @@ -271,6 +284,9 @@ func (this *Params) Equal(that interface{}) bool { if this.SigVerifyCostSecp256k1 != that1.SigVerifyCostSecp256k1 { return false } + if this.SigVerifyCostBls12381 != that1.SigVerifyCostBls12381 { + return false + } return true } func (m *BaseAccount) Marshal() (dAtA []byte, err error) { @@ -293,6 +309,16 @@ func (m *BaseAccount) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.PopIsValid { + i-- + if m.PopIsValid { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x28 + } if m.Sequence != 0 { i = encodeVarintAuth(dAtA, i, uint64(m.Sequence)) i-- @@ -396,6 +422,11 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.SigVerifyCostBls12381 != 0 { + i = encodeVarintAuth(dAtA, i, uint64(m.SigVerifyCostBls12381)) + i-- + dAtA[i] = 0x30 + } if m.SigVerifyCostSecp256k1 != 0 { i = encodeVarintAuth(dAtA, i, uint64(m.SigVerifyCostSecp256k1)) i-- @@ -455,6 +486,9 @@ func (m *BaseAccount) Size() (n int) { if m.Sequence != 0 { n += 1 + sovAuth(uint64(m.Sequence)) } + if m.PopIsValid { + n += 2 + } return n } @@ -502,6 +536,9 @@ func (m *Params) Size() (n int) { if m.SigVerifyCostSecp256k1 != 0 { n += 1 + sovAuth(uint64(m.SigVerifyCostSecp256k1)) } + if m.SigVerifyCostBls12381 != 0 { + n += 1 + sovAuth(uint64(m.SigVerifyCostBls12381)) + } return n } @@ -646,6 +683,26 @@ func (m *BaseAccount) Unmarshal(dAtA []byte) error { break } } + case 5: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field PopIsValid", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuth + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.PopIsValid = bool(v != 0) default: iNdEx = preIndex skippy, err := skipAuth(dAtA[iNdEx:]) @@ -941,6 +998,25 @@ func (m *Params) Unmarshal(dAtA []byte) error { break } } + case 6: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field SigVerifyCostBls12381", wireType) + } + m.SigVerifyCostBls12381 = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuth + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.SigVerifyCostBls12381 |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipAuth(dAtA[iNdEx:]) diff --git a/x/auth/types/params.go b/x/auth/types/params.go index 13db369d23..632de20cae 100644 --- a/x/auth/types/params.go +++ b/x/auth/types/params.go @@ -15,6 +15,7 @@ const ( DefaultTxSizeCostPerByte uint64 = 10 DefaultSigVerifyCostED25519 uint64 = 590 DefaultSigVerifyCostSecp256k1 uint64 = 1000 + DefaultSigVerifyCostBls12381 uint64 = 6300 ) // Parameter keys @@ -24,13 +25,14 @@ var ( KeyTxSizeCostPerByte = []byte("TxSizeCostPerByte") KeySigVerifyCostED25519 = []byte("SigVerifyCostED25519") KeySigVerifyCostSecp256k1 = []byte("SigVerifyCostSecp256k1") + KeySigVerifyCostBls12381 = []byte("SigVerifyCostBls12381") ) var _ paramtypes.ParamSet = &Params{} // NewParams creates a new Params object func NewParams( - maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64, + maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64, sigVerifyCostBls12381 uint64, ) Params { return Params{ MaxMemoCharacters: maxMemoCharacters, @@ -38,6 +40,7 @@ func NewParams( TxSizeCostPerByte: txSizeCostPerByte, SigVerifyCostED25519: sigVerifyCostED25519, SigVerifyCostSecp256k1: sigVerifyCostSecp256k1, + SigVerifyCostBls12381: sigVerifyCostBls12381, } } @@ -55,6 +58,7 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { paramtypes.NewParamSetPair(KeyTxSizeCostPerByte, &p.TxSizeCostPerByte, validateTxSizeCostPerByte), paramtypes.NewParamSetPair(KeySigVerifyCostED25519, &p.SigVerifyCostED25519, validateSigVerifyCostED25519), paramtypes.NewParamSetPair(KeySigVerifyCostSecp256k1, &p.SigVerifyCostSecp256k1, validateSigVerifyCostSecp256k1), + paramtypes.NewParamSetPair(KeySigVerifyCostBls12381, &p.SigVerifyCostBls12381, validateSigVerifyCostBls12381), } } @@ -66,6 +70,7 @@ func DefaultParams() Params { TxSizeCostPerByte: DefaultTxSizeCostPerByte, SigVerifyCostED25519: DefaultSigVerifyCostED25519, SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1, + SigVerifyCostBls12381: DefaultSigVerifyCostBls12381, } } @@ -114,6 +119,19 @@ func validateSigVerifyCostSecp256k1(i interface{}) error { return nil } +func validateSigVerifyCostBls12381(i interface{}) error { + v, ok := i.(uint64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v == 0 { + return fmt.Errorf("invalid BLS12381 signature verification cost: %d", v) + } + + return nil +} + func validateMaxMemoCharacters(i interface{}) error { v, ok := i.(uint64) if !ok { @@ -151,6 +169,9 @@ func (p Params) Validate() error { if err := validateSigVerifyCostSecp256k1(p.SigVerifyCostSecp256k1); err != nil { return err } + if err := validateSigVerifyCostBls12381(p.SigVerifyCostBls12381); err != nil { + return err + } if err := validateMaxMemoCharacters(p.MaxMemoCharacters); err != nil { return err } diff --git a/x/auth/types/params_test.go b/x/auth/types/params_test.go index fcec36cb83..854514499f 100644 --- a/x/auth/types/params_test.go +++ b/x/auth/types/params_test.go @@ -26,15 +26,17 @@ func TestParams_Validate(t *testing.T) { }{ {"default params", types.DefaultParams(), nil}, {"invalid tx signature limit", types.NewParams(types.DefaultMaxMemoCharacters, 0, types.DefaultTxSizeCostPerByte, - types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1), fmt.Errorf("invalid tx signature limit: 0")}, + types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381), fmt.Errorf("invalid tx signature limit: 0")}, {"invalid ED25519 signature verification cost", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, - 0, types.DefaultSigVerifyCostSecp256k1), fmt.Errorf("invalid ED25519 signature verification cost: 0")}, + 0, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381), fmt.Errorf("invalid ED25519 signature verification cost: 0")}, {"invalid SECK256k1 signature verification cost", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, - types.DefaultSigVerifyCostED25519, 0), fmt.Errorf("invalid SECK256k1 signature verification cost: 0")}, + types.DefaultSigVerifyCostED25519, 0, types.DefaultSigVerifyCostBls12381), fmt.Errorf("invalid SECK256k1 signature verification cost: 0")}, + {"invalid BLS12381 signature verification cost", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, + types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, 0), fmt.Errorf("invalid BLS12381 signature verification cost: 0")}, {"invalid max memo characters", types.NewParams(0, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, - types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1), fmt.Errorf("invalid max memo characters: 0")}, + types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381), fmt.Errorf("invalid max memo characters: 0")}, {"invalid tx size cost per byte", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, 0, - types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1), fmt.Errorf("invalid tx size cost per byte: 0")}, + types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSecp256k1, types.DefaultSigVerifyCostBls12381), fmt.Errorf("invalid tx size cost per byte: 0")}, } for _, tt := range tests { tt := tt diff --git a/x/staking/client/rest/grpc_query_test.go b/x/staking/client/rest/grpc_query_test.go index 42d7974517..491f09bbcb 100644 --- a/x/staking/client/rest/grpc_query_test.go +++ b/x/staking/client/rest/grpc_query_test.go @@ -1,3 +1,4 @@ +//go:build norace // +build norace package rest_test @@ -50,13 +51,15 @@ func (s *IntegrationTestSuite) SetupSuite() { val2 := s.network.Validators[1] // redelegate - _, err = stakingtestutil.MsgRedelegateExec(val.ClientCtx, val.Address, val.ValAddress, val2.ValAddress, unbond) + out, err := stakingtestutil.MsgRedelegateExec(val.ClientCtx, val.Address, val.ValAddress, val2.ValAddress, unbond) + s.T().Log(out) s.Require().NoError(err) _, err = s.network.WaitForHeight(1) s.Require().NoError(err) // unbonding - _, err = stakingtestutil.MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond) + out, err = stakingtestutil.MsgUnbondExec(val.ClientCtx, val.Address, val.ValAddress, unbond) + s.T().Log(out) s.Require().NoError(err) _, err = s.network.WaitForHeight(1) s.Require().NoError(err)