From ae40b729ecba8283960f9968261236d3f661544c Mon Sep 17 00:00:00 2001 From: kitounliu <58184672+kitounliu@users.noreply.github.com> Date: Tue, 20 Jul 2021 17:14:58 +0100 Subject: [PATCH] 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 --- 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 | 225 +++++++++++ crypto/keys/bls12381/bls12381_test.go | 36 ++ crypto/keys/bls12381/keys.pb.go | 496 ++++++++++++++++++++++++ crypto/keys/ed25519/ed25519_test.go | 14 + docs/core/proto-docs.md | 55 +++ go.mod | 1 + go.sum | 2 + proto/cosmos/auth/v1beta1/auth.proto | 3 + proto/cosmos/crypto/bls12381/keys.proto | 22 ++ 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/sigverify.go | 14 + x/auth/simulation/genesis.go | 14 +- x/auth/types/auth.pb.go | 128 +++--- x/auth/types/params.go | 23 +- 21 files changed, 1048 insertions(+), 62 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 proto/cosmos/crypto/bls12381/keys.proto 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..4806b26685 --- /dev/null +++ b/crypto/keys/bls12381/bls12381.go @@ -0,0 +1,225 @@ +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 secp256k1 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. + skBytes := blst.KeyGen(ikm).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..48fbd02faf --- /dev/null +++ b/crypto/keys/bls12381/bls12381_test.go @@ -0,0 +1,36 @@ +package bls12381_test + +import ( + "github.com/cosmos/cosmos-sdk/crypto/keys/bls12381" + bench "github.com/cosmos/cosmos-sdk/crypto/keys/internal/benchmarking" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto" + "testing" +) + +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 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/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..6c16c68c78 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) @@ -1179,6 +1183,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 +2787,56 @@ Msg defines the bank Msg service. + +
+ +## 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) | | | + + + + + + + + + + + + + + + diff --git a/go.mod b/go.mod index dcae062fd3..fa880ef0fd 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,7 @@ 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.4 github.com/tendermint/btcd v0.1.1 github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 github.com/tendermint/go-amino v0.16.0 diff --git a/go.sum b/go.sum index b48ef06495..1654eaaae0 100644 --- a/go.sum +++ b/go.sum @@ -603,6 +603,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ 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/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= +github.com/supranational/blst v0.3.4 h1:iZE9lBMoywK2uy2U/5hDOvobQk9FnOQ2wNlu9GmRCoA= +github.com/supranational/blst v0.3.4/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw= 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= github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c h1:g+WoO5jjkqGAzHWCjJB1zZfXPIAaDpzXIEJ0eS6B5Ok= diff --git a/proto/cosmos/auth/v1beta1/auth.proto b/proto/cosmos/auth/v1beta1/auth.proto index 72e1d9ec28..799568c186 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"; @@ -47,4 +48,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/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/sigverify.go b/x/auth/ante/sigverify.go index 6aa0c2a345..db5df87a9a 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()) @@ -408,6 +418,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/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/auth.pb.go b/x/auth/types/auth.pb.go index fa52ac5a8b..83918950e7 100644 --- a/x/auth/types/auth.pb.go +++ b/x/auth/types/auth.pb.go @@ -113,6 +113,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 +183,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,50 +199,52 @@ func init() { func init() { proto.RegisterFile("cosmos/auth/v1beta1/auth.proto", fileDescriptor_7e1f7e915d020d2d) } var fileDescriptor_7e1f7e915d020d2d = []byte{ - // 674 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8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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, - 0x00, 0x00, + // 707 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x54, 0x3f, 0x4f, 0xdb, 0x40, + 0x14, 0x8f, 0x4b, 0x1a, 0xe0, 0x02, 0x48, 0x98, 0x04, 0x9c, 0xb4, 0xf2, 0x59, 0x9e, 0x52, 0xa9, + 0x71, 0x94, 0x20, 0xaa, 0x12, 0x55, 0x55, 0x31, 0xed, 0x80, 0x5a, 0x10, 0x32, 0x52, 0x87, 0xaa, + 0x92, 0x6b, 0x3b, 0x87, 0xb1, 0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4e, 0x66, 0x6a, 0xc5, 0x08, 0x48, 0xf7, 0x55, 0x76, 0x97, 0x34, 0x5b, + 0x9b, 0xcf, 0x9b, 0x52, 0x81, 0x25, 0xbf, 0x18, 0x24, 0xb0, 0x3c, 0x95, 0xac, 0x67, 0x84, 0x34, + 0x81, 0x70, 0x76, 0xf0, 0xc8, 0x42, 0x35, 0xca, 0x64, 0x96, 0xb2, 0xbd, 0x90, 0xfd, 0x55, 0x04, + 0x7d, 0xf7, 0xfb, 0x40, 0x16, 0xae, 0x07, 0xb2, 0xf0, 0x6b, 0x20, 0x0b, 0x5f, 0x6e, 0xe5, 0xdc, + 0xf5, 0xad, 0x9c, 0xfb, 0x79, 0x2b, 0xe7, 0x3e, 0x3c, 0x71, 0x3d, 0x7a, 0x12, 0xd9, 0x9a, 0x83, + 0xfd, 0xec, 0x09, 0xca, 0x7e, 0xea, 0xa4, 0x73, 0xda, 0xe8, 0xf3, 0x17, 0x8d, 0xc6, 0x01, 0x22, + 0x76, 0x81, 0x3d, 0x10, 0x9b, 0xff, 0x02, 0x00, 0x00, 0xff, 0xff, 0x5a, 0x52, 0xeb, 0xf4, 0xed, + 0x04, 0x00, 0x00, } func (this *Params) Equal(that interface{}) bool { @@ -271,6 +281,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) { @@ -396,6 +409,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-- @@ -502,6 +520,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 } @@ -941,6 +962,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 }