diff --git a/Makefile b/Makefile index 4fc8409abeb3..92ec980dbdf3 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,8 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=sim \ -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \ -X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \ -X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)" \ - -X github.com/cometbft/cometbft/version.TMCoreSemVer=$(TMVERSION) + -X github.com/tendermint/tendermint/version.TMCoreSemVer=$(TMVERSION) \ + -X github.com/tendermint/tendermint/crypto/algo.Algo=sm2 # DB backend selection diff --git a/api/cosmos/auth/v1beta1/auth.pulsar.go b/api/cosmos/auth/v1beta1/auth.pulsar.go index bba61615d462..540636cccb47 100644 --- a/api/cosmos/auth/v1beta1/auth.pulsar.go +++ b/api/cosmos/auth/v1beta1/auth.pulsar.go @@ -1785,6 +1785,7 @@ var ( fd_Params_tx_size_cost_per_byte protoreflect.FieldDescriptor fd_Params_sig_verify_cost_ed25519 protoreflect.FieldDescriptor fd_Params_sig_verify_cost_secp256k1 protoreflect.FieldDescriptor + fd_Params_sig_verify_cost_sm2 protoreflect.FieldDescriptor ) func init() { @@ -1795,6 +1796,7 @@ func init() { fd_Params_tx_size_cost_per_byte = md_Params.Fields().ByName("tx_size_cost_per_byte") fd_Params_sig_verify_cost_ed25519 = md_Params.Fields().ByName("sig_verify_cost_ed25519") fd_Params_sig_verify_cost_secp256k1 = md_Params.Fields().ByName("sig_verify_cost_secp256k1") + fd_Params_sig_verify_cost_sm2 = md_Params.Fields().ByName("sig_verify_cost_sm2") } var _ protoreflect.Message = (*fastReflection_Params)(nil) @@ -1892,6 +1894,12 @@ func (x *fastReflection_Params) Range(f func(protoreflect.FieldDescriptor, proto return } } + if x.SigVerifyCostSm2 != uint64(0) { + value := protoreflect.ValueOfUint64(x.SigVerifyCostSm2) + if !f(fd_Params_sig_verify_cost_sm2, value) { + return + } + } } // Has reports whether a field is populated. @@ -1917,6 +1925,8 @@ func (x *fastReflection_Params) Has(fd protoreflect.FieldDescriptor) bool { return x.SigVerifyCostEd25519 != uint64(0) case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": return x.SigVerifyCostSecp256K1 != uint64(0) + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + return x.SigVerifyCostSm2 != uint64(0) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -1943,6 +1953,8 @@ func (x *fastReflection_Params) Clear(fd protoreflect.FieldDescriptor) { x.SigVerifyCostEd25519 = uint64(0) case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": x.SigVerifyCostSecp256K1 = uint64(0) + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + x.SigVerifyCostSm2 = uint64(0) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -1974,6 +1986,9 @@ func (x *fastReflection_Params) Get(descriptor protoreflect.FieldDescriptor) pro case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": value := x.SigVerifyCostSecp256K1 return protoreflect.ValueOfUint64(value) + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + value := x.SigVerifyCostSm2 + return protoreflect.ValueOfUint64(value) default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -2004,6 +2019,8 @@ func (x *fastReflection_Params) Set(fd protoreflect.FieldDescriptor, value proto x.SigVerifyCostEd25519 = value.Uint() case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": x.SigVerifyCostSecp256K1 = value.Uint() + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + x.SigVerifyCostSm2 = value.Uint() default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -2034,6 +2051,8 @@ func (x *fastReflection_Params) Mutable(fd protoreflect.FieldDescriptor) protore panic(fmt.Errorf("field sig_verify_cost_ed25519 of message cosmos.auth.v1beta1.Params is not mutable")) case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": panic(fmt.Errorf("field sig_verify_cost_secp256k1 of message cosmos.auth.v1beta1.Params is not mutable")) + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + panic(fmt.Errorf("field sig_verify_cost_sm2 of message cosmos.auth.v1beta1.Params is not mutable")) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -2057,6 +2076,8 @@ func (x *fastReflection_Params) NewField(fd protoreflect.FieldDescriptor) protor return protoreflect.ValueOfUint64(uint64(0)) case "cosmos.auth.v1beta1.Params.sig_verify_cost_secp256k1": return protoreflect.ValueOfUint64(uint64(0)) + case "cosmos.auth.v1beta1.Params.sig_verify_cost_sm2": + return protoreflect.ValueOfUint64(uint64(0)) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.auth.v1beta1.Params")) @@ -2141,6 +2162,9 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { if x.SigVerifyCostSecp256K1 != 0 { n += 1 + runtime.Sov(uint64(x.SigVerifyCostSecp256K1)) } + if x.SigVerifyCostSm2 != 0 { + n += 1 + runtime.Sov(uint64(x.SigVerifyCostSm2)) + } if x.unknownFields != nil { n += len(x.unknownFields) } @@ -2170,6 +2194,11 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { i -= len(x.unknownFields) copy(dAtA[i:], x.unknownFields) } + if x.SigVerifyCostSm2 != 0 { + i = runtime.EncodeVarint(dAtA, i, uint64(x.SigVerifyCostSm2)) + i-- + dAtA[i] = 0x30 + } if x.SigVerifyCostSecp256K1 != 0 { i = runtime.EncodeVarint(dAtA, i, uint64(x.SigVerifyCostSecp256K1)) i-- @@ -2339,6 +2368,25 @@ func (x *fastReflection_Params) ProtoMethods() *protoiface.Methods { break } } + case 6: + if wireType != 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field SigVerifyCostSm2", wireType) + } + x.SigVerifyCostSm2 = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + x.SigVerifyCostSm2 |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := runtime.Skip(dAtA[iNdEx:]) @@ -2561,6 +2609,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"` SigVerifyCostEd25519 uint64 `protobuf:"varint,4,opt,name=sig_verify_cost_ed25519,json=sigVerifyCostEd25519,proto3" json:"sig_verify_cost_ed25519,omitempty"` SigVerifyCostSecp256K1 uint64 `protobuf:"varint,5,opt,name=sig_verify_cost_secp256k1,json=sigVerifyCostSecp256k1,proto3" json:"sig_verify_cost_secp256k1,omitempty"` + SigVerifyCostSm2 uint64 `protobuf:"varint,6,opt,name=sig_verify_cost_sm2,json=sigVerifyCostSm2,proto3" json:"sig_verify_cost_sm2,omitempty"` } func (x *Params) Reset() { @@ -2618,6 +2667,13 @@ func (x *Params) GetSigVerifyCostSecp256K1() uint64 { return 0 } +func (x *Params) GetSigVerifyCostSm2() uint64 { + if x != nil { + return x.SigVerifyCostSm2 + } + return 0 +} + var File_cosmos_auth_v1beta1_auth_proto protoreflect.FileDescriptor var file_cosmos_auth_v1beta1_auth_proto_rawDesc = []byte{ @@ -2668,7 +2724,7 @@ var file_cosmos_auth_v1beta1_auth_proto_rawDesc = []byte{ 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x64, 0x65, 0x72, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x0e, 0x64, 0x65, 0x72, 0x69, 0x76, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x4b, 0x65, - 0x79, 0x73, 0x22, 0xd7, 0x02, 0x0a, 0x06, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x2e, 0x0a, + 0x79, 0x73, 0x22, 0x9c, 0x03, 0x0a, 0x06, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x2e, 0x0a, 0x13, 0x6d, 0x61, 0x78, 0x5f, 0x6d, 0x65, 0x6d, 0x6f, 0x5f, 0x63, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x04, 0x52, 0x11, 0x6d, 0x61, 0x78, 0x4d, 0x65, 0x6d, 0x6f, 0x43, 0x68, 0x61, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73, 0x12, 0x20, 0x0a, @@ -2687,22 +2743,26 @@ var file_cosmos_auth_v1beta1_auth_proto_rawDesc = []byte{ 0x05, 0x20, 0x01, 0x28, 0x04, 0x42, 0x1a, 0xe2, 0xde, 0x1f, 0x16, 0x53, 0x69, 0x67, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x73, 0x74, 0x53, 0x65, 0x63, 0x70, 0x32, 0x35, 0x36, 0x6b, 0x31, 0x52, 0x16, 0x73, 0x69, 0x67, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x73, 0x74, - 0x53, 0x65, 0x63, 0x70, 0x32, 0x35, 0x36, 0x6b, 0x31, 0x3a, 0x21, 0xe8, 0xa0, 0x1f, 0x01, 0x8a, - 0xe7, 0xb0, 0x2a, 0x18, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x73, 0x64, 0x6b, 0x2f, 0x78, - 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2f, 0x50, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x42, 0xc4, 0x01, 0x0a, - 0x17, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x61, 0x75, 0x74, 0x68, - 0x2e, 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0x42, 0x09, 0x41, 0x75, 0x74, 0x68, 0x50, 0x72, - 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x30, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, - 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x61, - 0x75, 0x74, 0x68, 0x2f, 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0x3b, 0x61, 0x75, 0x74, 0x68, - 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x41, 0x58, 0xaa, 0x02, 0x13, - 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x41, 0x75, 0x74, 0x68, 0x2e, 0x56, 0x31, 0x62, 0x65, - 0x74, 0x61, 0x31, 0xca, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x75, 0x74, - 0x68, 0x5c, 0x56, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0xe2, 0x02, 0x1f, 0x43, 0x6f, 0x73, 0x6d, - 0x6f, 0x73, 0x5c, 0x41, 0x75, 0x74, 0x68, 0x5c, 0x56, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0x5c, - 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x15, 0x43, 0x6f, - 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, 0x41, 0x75, 0x74, 0x68, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x65, - 0x74, 0x61, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x53, 0x65, 0x63, 0x70, 0x32, 0x35, 0x36, 0x6b, 0x31, 0x12, 0x43, 0x0a, 0x13, 0x73, 0x69, 0x67, + 0x5f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x5f, 0x63, 0x6f, 0x73, 0x74, 0x5f, 0x73, 0x6d, 0x32, + 0x18, 0x06, 0x20, 0x01, 0x28, 0x04, 0x42, 0x14, 0xe2, 0xde, 0x1f, 0x10, 0x53, 0x69, 0x67, 0x56, + 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x73, 0x74, 0x53, 0x6d, 0x32, 0x52, 0x10, 0x73, 0x69, + 0x67, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x73, 0x74, 0x53, 0x6d, 0x32, 0x3a, 0x21, + 0xe8, 0xa0, 0x1f, 0x01, 0x8a, 0xe7, 0xb0, 0x2a, 0x18, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, + 0x73, 0x64, 0x6b, 0x2f, 0x78, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2f, 0x50, 0x61, 0x72, 0x61, 0x6d, + 0x73, 0x42, 0xc4, 0x01, 0x0a, 0x17, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0x42, 0x09, 0x41, + 0x75, 0x74, 0x68, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x30, 0x63, 0x6f, 0x73, 0x6d, + 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x73, + 0x6d, 0x6f, 0x73, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2f, 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, + 0x3b, 0x61, 0x75, 0x74, 0x68, 0x76, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0xa2, 0x02, 0x03, 0x43, + 0x41, 0x58, 0xaa, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x41, 0x75, 0x74, 0x68, + 0x2e, 0x56, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0xca, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, + 0x73, 0x5c, 0x41, 0x75, 0x74, 0x68, 0x5c, 0x56, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0xe2, 0x02, + 0x1f, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x41, 0x75, 0x74, 0x68, 0x5c, 0x56, 0x31, 0x62, + 0x65, 0x74, 0x61, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, + 0xea, 0x02, 0x15, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, 0x41, 0x75, 0x74, 0x68, 0x3a, + 0x3a, 0x56, 0x31, 0x62, 0x65, 0x74, 0x61, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/api/cosmos/crypto/sm2/keys.pulsar.go b/api/cosmos/crypto/sm2/keys.pulsar.go new file mode 100644 index 000000000000..ad5b2349a17d --- /dev/null +++ b/api/cosmos/crypto/sm2/keys.pulsar.go @@ -0,0 +1,1056 @@ +// Code generated by protoc-gen-go-pulsar. DO NOT EDIT. +package sm2 + +import ( + _ "cosmossdk.io/api/amino" + fmt "fmt" + runtime "github.com/cosmos/cosmos-proto/runtime" + _ "github.com/cosmos/gogoproto/gogoproto" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoiface "google.golang.org/protobuf/runtime/protoiface" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + io "io" + reflect "reflect" + sync "sync" +) + +var ( + md_PubKey protoreflect.MessageDescriptor + fd_PubKey_key protoreflect.FieldDescriptor +) + +func init() { + file_cosmos_crypto_sm2_keys_proto_init() + md_PubKey = File_cosmos_crypto_sm2_keys_proto.Messages().ByName("PubKey") + fd_PubKey_key = md_PubKey.Fields().ByName("key") +} + +var _ protoreflect.Message = (*fastReflection_PubKey)(nil) + +type fastReflection_PubKey PubKey + +func (x *PubKey) ProtoReflect() protoreflect.Message { + return (*fastReflection_PubKey)(x) +} + +func (x *PubKey) slowProtoReflect() protoreflect.Message { + mi := &file_cosmos_crypto_sm2_keys_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +var _fastReflection_PubKey_messageType fastReflection_PubKey_messageType +var _ protoreflect.MessageType = fastReflection_PubKey_messageType{} + +type fastReflection_PubKey_messageType struct{} + +func (x fastReflection_PubKey_messageType) Zero() protoreflect.Message { + return (*fastReflection_PubKey)(nil) +} +func (x fastReflection_PubKey_messageType) New() protoreflect.Message { + return new(fastReflection_PubKey) +} +func (x fastReflection_PubKey_messageType) Descriptor() protoreflect.MessageDescriptor { + return md_PubKey +} + +// Descriptor returns message descriptor, which contains only the protobuf +// type information for the message. +func (x *fastReflection_PubKey) Descriptor() protoreflect.MessageDescriptor { + return md_PubKey +} + +// Type returns the message type, which encapsulates both Go and protobuf +// type information. If the Go type information is not needed, +// it is recommended that the message descriptor be used instead. +func (x *fastReflection_PubKey) Type() protoreflect.MessageType { + return _fastReflection_PubKey_messageType +} + +// New returns a newly allocated and mutable empty message. +func (x *fastReflection_PubKey) New() protoreflect.Message { + return new(fastReflection_PubKey) +} + +// Interface unwraps the message reflection interface and +// returns the underlying ProtoMessage interface. +func (x *fastReflection_PubKey) Interface() protoreflect.ProtoMessage { + return (*PubKey)(x) +} + +// Range iterates over every populated field in an undefined order, +// calling f for each field descriptor and value encountered. +// Range returns immediately if f returns false. +// While iterating, mutating operations may only be performed +// on the current field descriptor. +func (x *fastReflection_PubKey) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) { + if len(x.Key) != 0 { + value := protoreflect.ValueOfBytes(x.Key) + if !f(fd_PubKey_key, value) { + return + } + } +} + +// Has reports whether a field is populated. +// +// Some fields have the property of nullability where it is possible to +// distinguish between the default value of a field and whether the field +// was explicitly populated with the default value. Singular message fields, +// member fields of a oneof, and proto2 scalar fields are nullable. Such +// fields are populated only if explicitly set. +// +// In other cases (aside from the nullable cases above), +// a proto3 scalar field is populated if it contains a non-zero value, and +// a repeated field is populated if it is non-empty. +func (x *fastReflection_PubKey) Has(fd protoreflect.FieldDescriptor) bool { + switch fd.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + return len(x.Key) != 0 + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", fd.FullName())) + } +} + +// Clear clears the field such that a subsequent Has call reports false. +// +// Clearing an extension field clears both the extension type and value +// associated with the given field number. +// +// Clear is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PubKey) Clear(fd protoreflect.FieldDescriptor) { + switch fd.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + x.Key = nil + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", fd.FullName())) + } +} + +// Get retrieves the value for a field. +// +// For unpopulated scalars, it returns the default value, where +// the default value of a bytes scalar is guaranteed to be a copy. +// For unpopulated composite types, it returns an empty, read-only view +// of the value; to obtain a mutable reference, use Mutable. +func (x *fastReflection_PubKey) Get(descriptor protoreflect.FieldDescriptor) protoreflect.Value { + switch descriptor.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + value := x.Key + return protoreflect.ValueOfBytes(value) + default: + if descriptor.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", descriptor.FullName())) + } +} + +// Set stores the value for a field. +// +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType. +// When setting a composite type, it is unspecified whether the stored value +// aliases the source's memory in any way. If the composite value is an +// empty, read-only value, then it panics. +// +// Set is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PubKey) Set(fd protoreflect.FieldDescriptor, value protoreflect.Value) { + switch fd.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + x.Key = value.Bytes() + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", fd.FullName())) + } +} + +// Mutable returns a mutable reference to a composite type. +// +// If the field is unpopulated, it may allocate a composite value. +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType +// if not already stored. +// It panics if the field does not contain a composite type. +// +// Mutable is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PubKey) Mutable(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + panic(fmt.Errorf("field key of message cosmos.crypto.sm2.PubKey is not mutable")) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", fd.FullName())) + } +} + +// NewField returns a new value that is assignable to the field +// for the given descriptor. For scalars, this returns the default value. +// For lists, maps, and messages, this returns a new, empty, mutable value. +func (x *fastReflection_PubKey) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.crypto.sm2.PubKey.key": + return protoreflect.ValueOfBytes(nil) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PubKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PubKey does not contain field %s", fd.FullName())) + } +} + +// WhichOneof reports which field within the oneof is populated, +// returning nil if none are populated. +// It panics if the oneof descriptor does not belong to this message. +func (x *fastReflection_PubKey) WhichOneof(d protoreflect.OneofDescriptor) protoreflect.FieldDescriptor { + switch d.FullName() { + default: + panic(fmt.Errorf("%s is not a oneof field in cosmos.crypto.sm2.PubKey", d.FullName())) + } + panic("unreachable") +} + +// GetUnknown retrieves the entire list of unknown fields. +// The caller may only mutate the contents of the RawFields +// if the mutated bytes are stored back into the message with SetUnknown. +func (x *fastReflection_PubKey) GetUnknown() protoreflect.RawFields { + return x.unknownFields +} + +// SetUnknown stores an entire list of unknown fields. +// The raw fields must be syntactically valid according to the wire format. +// An implementation may panic if this is not the case. +// Once stored, the caller must not mutate the content of the RawFields. +// An empty RawFields may be passed to clear the fields. +// +// SetUnknown is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PubKey) SetUnknown(fields protoreflect.RawFields) { + x.unknownFields = fields +} + +// IsValid reports whether the message is valid. +// +// An invalid message is an empty, read-only value. +// +// An invalid message often corresponds to a nil pointer of the concrete +// message type, but the details are implementation dependent. +// Validity is not part of the protobuf data model, and may not +// be preserved in marshaling or other operations. +func (x *fastReflection_PubKey) IsValid() bool { + return x != nil +} + +// ProtoMethods returns optional fastReflectionFeature-path implementations of various operations. +// This method may return nil. +// +// The returned methods type is identical to +// "google.golang.org/protobuf/runtime/protoiface".Methods. +// Consult the protoiface package documentation for details. +func (x *fastReflection_PubKey) ProtoMethods() *protoiface.Methods { + size := func(input protoiface.SizeInput) protoiface.SizeOutput { + x := input.Message.Interface().(*PubKey) + if x == nil { + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: 0, + } + } + options := runtime.SizeInputToOptions(input) + _ = options + var n int + var l int + _ = l + l = len(x.Key) + if l > 0 { + n += 1 + l + runtime.Sov(uint64(l)) + } + if x.unknownFields != nil { + n += len(x.unknownFields) + } + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: n, + } + } + + marshal := func(input protoiface.MarshalInput) (protoiface.MarshalOutput, error) { + x := input.Message.Interface().(*PubKey) + if x == nil { + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + options := runtime.MarshalInputToOptions(input) + _ = options + size := options.Size(x) + dAtA := make([]byte, size) + i := len(dAtA) + _ = i + var l int + _ = l + if x.unknownFields != nil { + i -= len(x.unknownFields) + copy(dAtA[i:], x.unknownFields) + } + if len(x.Key) > 0 { + i -= len(x.Key) + copy(dAtA[i:], x.Key) + i = runtime.EncodeVarint(dAtA, i, uint64(len(x.Key))) + i-- + dAtA[i] = 0xa + } + if input.Buf != nil { + input.Buf = append(input.Buf, dAtA...) + } else { + input.Buf = dAtA + } + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + unmarshal := func(input protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) { + x := input.Message.Interface().(*PubKey) + if x == nil { + return protoiface.UnmarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Flags: input.Flags, + }, nil + } + options := runtime.UnmarshalInputToOptions(input) + _ = options + dAtA := input.Buf + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, 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 protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: PubKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: PubKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.Key = append(x.Key[:0], dAtA[iNdEx:postIndex]...) + if x.Key == nil { + x.Key = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := runtime.Skip(dAtA[iNdEx:]) + if err != nil { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if (iNdEx + skippy) > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + if !options.DiscardUnknown { + x.unknownFields = append(x.unknownFields, dAtA[iNdEx:iNdEx+skippy]...) + } + iNdEx += skippy + } + } + + if iNdEx > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, nil + } + return &protoiface.Methods{ + NoUnkeyedLiterals: struct{}{}, + Flags: protoiface.SupportMarshalDeterministic | protoiface.SupportUnmarshalDiscardUnknown, + Size: size, + Marshal: marshal, + Unmarshal: unmarshal, + Merge: nil, + CheckInitialized: nil, + } +} + +var ( + md_PrivKey protoreflect.MessageDescriptor + fd_PrivKey_key protoreflect.FieldDescriptor +) + +func init() { + file_cosmos_crypto_sm2_keys_proto_init() + md_PrivKey = File_cosmos_crypto_sm2_keys_proto.Messages().ByName("PrivKey") + fd_PrivKey_key = md_PrivKey.Fields().ByName("key") +} + +var _ protoreflect.Message = (*fastReflection_PrivKey)(nil) + +type fastReflection_PrivKey PrivKey + +func (x *PrivKey) ProtoReflect() protoreflect.Message { + return (*fastReflection_PrivKey)(x) +} + +func (x *PrivKey) slowProtoReflect() protoreflect.Message { + mi := &file_cosmos_crypto_sm2_keys_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +var _fastReflection_PrivKey_messageType fastReflection_PrivKey_messageType +var _ protoreflect.MessageType = fastReflection_PrivKey_messageType{} + +type fastReflection_PrivKey_messageType struct{} + +func (x fastReflection_PrivKey_messageType) Zero() protoreflect.Message { + return (*fastReflection_PrivKey)(nil) +} +func (x fastReflection_PrivKey_messageType) New() protoreflect.Message { + return new(fastReflection_PrivKey) +} +func (x fastReflection_PrivKey_messageType) Descriptor() protoreflect.MessageDescriptor { + return md_PrivKey +} + +// Descriptor returns message descriptor, which contains only the protobuf +// type information for the message. +func (x *fastReflection_PrivKey) Descriptor() protoreflect.MessageDescriptor { + return md_PrivKey +} + +// Type returns the message type, which encapsulates both Go and protobuf +// type information. If the Go type information is not needed, +// it is recommended that the message descriptor be used instead. +func (x *fastReflection_PrivKey) Type() protoreflect.MessageType { + return _fastReflection_PrivKey_messageType +} + +// New returns a newly allocated and mutable empty message. +func (x *fastReflection_PrivKey) New() protoreflect.Message { + return new(fastReflection_PrivKey) +} + +// Interface unwraps the message reflection interface and +// returns the underlying ProtoMessage interface. +func (x *fastReflection_PrivKey) Interface() protoreflect.ProtoMessage { + return (*PrivKey)(x) +} + +// Range iterates over every populated field in an undefined order, +// calling f for each field descriptor and value encountered. +// Range returns immediately if f returns false. +// While iterating, mutating operations may only be performed +// on the current field descriptor. +func (x *fastReflection_PrivKey) Range(f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) { + if len(x.Key) != 0 { + value := protoreflect.ValueOfBytes(x.Key) + if !f(fd_PrivKey_key, value) { + return + } + } +} + +// Has reports whether a field is populated. +// +// Some fields have the property of nullability where it is possible to +// distinguish between the default value of a field and whether the field +// was explicitly populated with the default value. Singular message fields, +// member fields of a oneof, and proto2 scalar fields are nullable. Such +// fields are populated only if explicitly set. +// +// In other cases (aside from the nullable cases above), +// a proto3 scalar field is populated if it contains a non-zero value, and +// a repeated field is populated if it is non-empty. +func (x *fastReflection_PrivKey) Has(fd protoreflect.FieldDescriptor) bool { + switch fd.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + return len(x.Key) != 0 + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", fd.FullName())) + } +} + +// Clear clears the field such that a subsequent Has call reports false. +// +// Clearing an extension field clears both the extension type and value +// associated with the given field number. +// +// Clear is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PrivKey) Clear(fd protoreflect.FieldDescriptor) { + switch fd.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + x.Key = nil + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", fd.FullName())) + } +} + +// Get retrieves the value for a field. +// +// For unpopulated scalars, it returns the default value, where +// the default value of a bytes scalar is guaranteed to be a copy. +// For unpopulated composite types, it returns an empty, read-only view +// of the value; to obtain a mutable reference, use Mutable. +func (x *fastReflection_PrivKey) Get(descriptor protoreflect.FieldDescriptor) protoreflect.Value { + switch descriptor.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + value := x.Key + return protoreflect.ValueOfBytes(value) + default: + if descriptor.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", descriptor.FullName())) + } +} + +// Set stores the value for a field. +// +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType. +// When setting a composite type, it is unspecified whether the stored value +// aliases the source's memory in any way. If the composite value is an +// empty, read-only value, then it panics. +// +// Set is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PrivKey) Set(fd protoreflect.FieldDescriptor, value protoreflect.Value) { + switch fd.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + x.Key = value.Bytes() + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", fd.FullName())) + } +} + +// Mutable returns a mutable reference to a composite type. +// +// If the field is unpopulated, it may allocate a composite value. +// For a field belonging to a oneof, it implicitly clears any other field +// that may be currently set within the same oneof. +// For extension fields, it implicitly stores the provided ExtensionType +// if not already stored. +// It panics if the field does not contain a composite type. +// +// Mutable is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PrivKey) Mutable(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + panic(fmt.Errorf("field key of message cosmos.crypto.sm2.PrivKey is not mutable")) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", fd.FullName())) + } +} + +// NewField returns a new value that is assignable to the field +// for the given descriptor. For scalars, this returns the default value. +// For lists, maps, and messages, this returns a new, empty, mutable value. +func (x *fastReflection_PrivKey) NewField(fd protoreflect.FieldDescriptor) protoreflect.Value { + switch fd.FullName() { + case "cosmos.crypto.sm2.PrivKey.key": + return protoreflect.ValueOfBytes(nil) + default: + if fd.IsExtension() { + panic(fmt.Errorf("proto3 declared messages do not support extensions: cosmos.crypto.sm2.PrivKey")) + } + panic(fmt.Errorf("message cosmos.crypto.sm2.PrivKey does not contain field %s", fd.FullName())) + } +} + +// WhichOneof reports which field within the oneof is populated, +// returning nil if none are populated. +// It panics if the oneof descriptor does not belong to this message. +func (x *fastReflection_PrivKey) WhichOneof(d protoreflect.OneofDescriptor) protoreflect.FieldDescriptor { + switch d.FullName() { + default: + panic(fmt.Errorf("%s is not a oneof field in cosmos.crypto.sm2.PrivKey", d.FullName())) + } + panic("unreachable") +} + +// GetUnknown retrieves the entire list of unknown fields. +// The caller may only mutate the contents of the RawFields +// if the mutated bytes are stored back into the message with SetUnknown. +func (x *fastReflection_PrivKey) GetUnknown() protoreflect.RawFields { + return x.unknownFields +} + +// SetUnknown stores an entire list of unknown fields. +// The raw fields must be syntactically valid according to the wire format. +// An implementation may panic if this is not the case. +// Once stored, the caller must not mutate the content of the RawFields. +// An empty RawFields may be passed to clear the fields. +// +// SetUnknown is a mutating operation and unsafe for concurrent use. +func (x *fastReflection_PrivKey) SetUnknown(fields protoreflect.RawFields) { + x.unknownFields = fields +} + +// IsValid reports whether the message is valid. +// +// An invalid message is an empty, read-only value. +// +// An invalid message often corresponds to a nil pointer of the concrete +// message type, but the details are implementation dependent. +// Validity is not part of the protobuf data model, and may not +// be preserved in marshaling or other operations. +func (x *fastReflection_PrivKey) IsValid() bool { + return x != nil +} + +// ProtoMethods returns optional fastReflectionFeature-path implementations of various operations. +// This method may return nil. +// +// The returned methods type is identical to +// "google.golang.org/protobuf/runtime/protoiface".Methods. +// Consult the protoiface package documentation for details. +func (x *fastReflection_PrivKey) ProtoMethods() *protoiface.Methods { + size := func(input protoiface.SizeInput) protoiface.SizeOutput { + x := input.Message.Interface().(*PrivKey) + if x == nil { + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: 0, + } + } + options := runtime.SizeInputToOptions(input) + _ = options + var n int + var l int + _ = l + l = len(x.Key) + if l > 0 { + n += 1 + l + runtime.Sov(uint64(l)) + } + if x.unknownFields != nil { + n += len(x.unknownFields) + } + return protoiface.SizeOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Size: n, + } + } + + marshal := func(input protoiface.MarshalInput) (protoiface.MarshalOutput, error) { + x := input.Message.Interface().(*PrivKey) + if x == nil { + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + options := runtime.MarshalInputToOptions(input) + _ = options + size := options.Size(x) + dAtA := make([]byte, size) + i := len(dAtA) + _ = i + var l int + _ = l + if x.unknownFields != nil { + i -= len(x.unknownFields) + copy(dAtA[i:], x.unknownFields) + } + if len(x.Key) > 0 { + i -= len(x.Key) + copy(dAtA[i:], x.Key) + i = runtime.EncodeVarint(dAtA, i, uint64(len(x.Key))) + i-- + dAtA[i] = 0xa + } + if input.Buf != nil { + input.Buf = append(input.Buf, dAtA...) + } else { + input.Buf = dAtA + } + return protoiface.MarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Buf: input.Buf, + }, nil + } + unmarshal := func(input protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) { + x := input.Message.Interface().(*PrivKey) + if x == nil { + return protoiface.UnmarshalOutput{ + NoUnkeyedLiterals: input.NoUnkeyedLiterals, + Flags: input.Flags, + }, nil + } + options := runtime.UnmarshalInputToOptions(input) + _ = options + dAtA := input.Buf + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, 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 protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: PrivKey: wiretype end group for non-group") + } + if fieldNum <= 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: PrivKey: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.Key = append(x.Key[:0], dAtA[iNdEx:postIndex]...) + if x.Key == nil { + x.Key = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := runtime.Skip(dAtA[iNdEx:]) + if err != nil { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if (iNdEx + skippy) > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + if !options.DiscardUnknown { + x.unknownFields = append(x.unknownFields, dAtA[iNdEx:iNdEx+skippy]...) + } + iNdEx += skippy + } + } + + if iNdEx > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, nil + } + return &protoiface.Methods{ + NoUnkeyedLiterals: struct{}{}, + Flags: protoiface.SupportMarshalDeterministic | protoiface.SupportUnmarshalDiscardUnknown, + Size: size, + Marshal: marshal, + Unmarshal: unmarshal, + Merge: nil, + CheckInitialized: nil, + } +} + +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.27.0 +// protoc (unknown) +// source: cosmos/crypto/sm2/keys.proto + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// PubKey defines a secp256k1 public key +// Key is the compressed 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 { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *PubKey) Reset() { + *x = PubKey{} + if protoimpl.UnsafeEnabled { + mi := &file_cosmos_crypto_sm2_keys_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PubKey) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PubKey) ProtoMessage() {} + +// Deprecated: Use PubKey.ProtoReflect.Descriptor instead. +func (*PubKey) Descriptor() ([]byte, []int) { + return file_cosmos_crypto_sm2_keys_proto_rawDescGZIP(), []int{0} +} + +func (x *PubKey) GetKey() []byte { + if x != nil { + return x.Key + } + return nil +} + +// PrivKey defines a secp256k1 private key. +type PrivKey struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` +} + +func (x *PrivKey) Reset() { + *x = PrivKey{} + if protoimpl.UnsafeEnabled { + mi := &file_cosmos_crypto_sm2_keys_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PrivKey) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PrivKey) ProtoMessage() {} + +// Deprecated: Use PrivKey.ProtoReflect.Descriptor instead. +func (*PrivKey) Descriptor() ([]byte, []int) { + return file_cosmos_crypto_sm2_keys_proto_rawDescGZIP(), []int{1} +} + +func (x *PrivKey) GetKey() []byte { + if x != nil { + return x.Key + } + return nil +} + +var File_cosmos_crypto_sm2_keys_proto protoreflect.FileDescriptor + +var file_cosmos_crypto_sm2_keys_proto_rawDesc = []byte{ + 0x0a, 0x1c, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2f, + 0x73, 0x6d, 0x32, 0x2f, 0x6b, 0x65, 0x79, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x11, + 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x73, 0x6d, + 0x32, 0x1a, 0x11, 0x61, 0x6d, 0x69, 0x6e, 0x6f, 0x2f, 0x61, 0x6d, 0x69, 0x6e, 0x6f, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x14, 0x67, 0x6f, 0x67, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, + 0x67, 0x6f, 0x67, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x47, 0x0a, 0x06, 0x50, 0x75, + 0x62, 0x4b, 0x65, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x3a, 0x2b, 0x98, 0xa0, 0x1f, 0x00, 0x8a, 0xe7, 0xb0, 0x2a, + 0x14, 0x74, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x74, 0x2f, 0x50, 0x75, 0x62, 0x4b, + 0x65, 0x79, 0x53, 0x6d, 0x32, 0x92, 0xe7, 0xb0, 0x2a, 0x09, 0x6b, 0x65, 0x79, 0x5f, 0x66, 0x69, + 0x65, 0x6c, 0x64, 0x22, 0x45, 0x0a, 0x07, 0x50, 0x72, 0x69, 0x76, 0x4b, 0x65, 0x79, 0x12, 0x10, + 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, + 0x3a, 0x28, 0x8a, 0xe7, 0xb0, 0x2a, 0x15, 0x74, 0x65, 0x6e, 0x64, 0x65, 0x72, 0x6d, 0x69, 0x6e, + 0x74, 0x2f, 0x50, 0x72, 0x69, 0x76, 0x4b, 0x65, 0x79, 0x53, 0x6d, 0x32, 0x92, 0xe7, 0xb0, 0x2a, + 0x09, 0x6b, 0x65, 0x79, 0x5f, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x42, 0xac, 0x01, 0x0a, 0x15, 0x63, + 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, + 0x2e, 0x73, 0x6d, 0x32, 0x42, 0x09, 0x4b, 0x65, 0x79, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, + 0x01, 0x5a, 0x22, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x73, 0x64, 0x6b, 0x2e, 0x69, 0x6f, 0x2f, + 0x61, 0x70, 0x69, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x72, 0x79, 0x70, 0x74, + 0x6f, 0x2f, 0x73, 0x6d, 0x32, 0xa2, 0x02, 0x03, 0x43, 0x43, 0x53, 0xaa, 0x02, 0x11, 0x43, 0x6f, + 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x53, 0x6d, 0x32, 0xca, + 0x02, 0x11, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x5c, + 0x53, 0x6d, 0x32, 0xe2, 0x02, 0x1d, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5c, 0x43, 0x72, 0x79, + 0x70, 0x74, 0x6f, 0x5c, 0x53, 0x6d, 0x32, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, + 0x61, 0x74, 0x61, 0xea, 0x02, 0x13, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x3a, 0x3a, 0x43, 0x72, + 0x79, 0x70, 0x74, 0x6f, 0x3a, 0x3a, 0x53, 0x6d, 0x32, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var ( + file_cosmos_crypto_sm2_keys_proto_rawDescOnce sync.Once + file_cosmos_crypto_sm2_keys_proto_rawDescData = file_cosmos_crypto_sm2_keys_proto_rawDesc +) + +func file_cosmos_crypto_sm2_keys_proto_rawDescGZIP() []byte { + file_cosmos_crypto_sm2_keys_proto_rawDescOnce.Do(func() { + file_cosmos_crypto_sm2_keys_proto_rawDescData = protoimpl.X.CompressGZIP(file_cosmos_crypto_sm2_keys_proto_rawDescData) + }) + return file_cosmos_crypto_sm2_keys_proto_rawDescData +} + +var file_cosmos_crypto_sm2_keys_proto_msgTypes = make([]protoimpl.MessageInfo, 2) +var file_cosmos_crypto_sm2_keys_proto_goTypes = []interface{}{ + (*PubKey)(nil), // 0: cosmos.crypto.sm2.PubKey + (*PrivKey)(nil), // 1: cosmos.crypto.sm2.PrivKey +} +var file_cosmos_crypto_sm2_keys_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_cosmos_crypto_sm2_keys_proto_init() } +func file_cosmos_crypto_sm2_keys_proto_init() { + if File_cosmos_crypto_sm2_keys_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_cosmos_crypto_sm2_keys_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PubKey); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_cosmos_crypto_sm2_keys_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PrivKey); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_cosmos_crypto_sm2_keys_proto_rawDesc, + NumEnums: 0, + NumMessages: 2, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_cosmos_crypto_sm2_keys_proto_goTypes, + DependencyIndexes: file_cosmos_crypto_sm2_keys_proto_depIdxs, + MessageInfos: file_cosmos_crypto_sm2_keys_proto_msgTypes, + }.Build() + File_cosmos_crypto_sm2_keys_proto = out.File + file_cosmos_crypto_sm2_keys_proto_rawDesc = nil + file_cosmos_crypto_sm2_keys_proto_goTypes = nil + file_cosmos_crypto_sm2_keys_proto_depIdxs = nil +} diff --git a/baseapp/abci.go b/baseapp/abci.go index 32a6d8683227..3cacfbd51103 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -133,18 +133,18 @@ func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo { } // FilterPeerByAddrPort filters peers by address/port. -func (app *BaseApp) FilterPeerByAddrPort(info string) abci.ResponseQuery { +func (app *BaseApp) FilterPeerByAddrPort(ctx sdk.Context, info string) abci.ResponseQuery { if app.addrPeerFilter != nil { - return app.addrPeerFilter(info) + return app.addrPeerFilter(ctx, info) } return abci.ResponseQuery{} } // FilterPeerByID filters peers by node ID. -func (app *BaseApp) FilterPeerByID(info string) abci.ResponseQuery { +func (app *BaseApp) FilterPeerByID(ctx sdk.Context, info string) abci.ResponseQuery { if app.idPeerFilter != nil { - return app.idPeerFilter(info) + return app.idPeerFilter(ctx, info) } return abci.ResponseQuery{} @@ -949,6 +949,11 @@ func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery { ), app.trace) } + ctx, err := app.CreateQueryContext(0, false) + if err != nil { + return sdkerrors.QueryResult(err, false) + } + var resp abci.ResponseQuery cmd, typ, arg := path[1], path[2], path[3] @@ -956,10 +961,10 @@ func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery { case "filter": switch typ { case "addr": - resp = app.FilterPeerByAddrPort(arg) + resp = app.FilterPeerByAddrPort(ctx, arg) case "id": - resp = app.FilterPeerByID(arg) + resp = app.FilterPeerByID(ctx, arg) } default: diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 04f419807244..3c5dd14aec6b 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -214,14 +214,14 @@ func TestABCI_GRPCQuery(t *testing.T) { func TestABCI_P2PQuery(t *testing.T) { addrPeerFilterOpt := func(bapp *baseapp.BaseApp) { - bapp.SetAddrPeerFilter(func(addrport string) abci.ResponseQuery { + bapp.SetAddrPeerFilter(func(ctx sdk.Context, addrport string) abci.ResponseQuery { require.Equal(t, "1.1.1.1:8000", addrport) return abci.ResponseQuery{Code: uint32(3)} }) } idPeerFilterOpt := func(bapp *baseapp.BaseApp) { - bapp.SetIDPeerFilter(func(id string) abci.ResponseQuery { + bapp.SetIDPeerFilter(func(ctx sdk.Context, id string) abci.ResponseQuery { require.Equal(t, "testid", id) return abci.ResponseQuery{Code: uint32(4)} }) @@ -229,6 +229,15 @@ func TestABCI_P2PQuery(t *testing.T) { suite := NewBaseAppSuite(t, addrPeerFilterOpt, idPeerFilterOpt) + // As the modified handleQueryP2P needs a query context, the lastBlockHeight + // must not be zero. Therefore, we initiate the chain and commit one block here. + suite.baseApp.InitChain(abci.RequestInitChain{ + ConsensusParams: &tmproto.ConsensusParams{}, + }) + header := tmproto.Header{Height: suite.baseApp.LastBlockHeight() + 1} + suite.baseApp.BeginBlock(abci.RequestBeginBlock{Header: header}) + suite.baseApp.Commit() + addrQuery := abci.RequestQuery{ Path: "/p2p/filter/addr/1.1.1.1:8000", } diff --git a/baseapp/block_gas_test.go b/baseapp/block_gas_test.go index ef5c090fafc3..51520e22d3d9 100644 --- a/baseapp/block_gas_test.go +++ b/baseapp/block_gas_test.go @@ -166,7 +166,7 @@ func TestBaseApp_BlockGas(t *testing.T) { require.Equal(t, []byte("ok"), okValue) } // check block gas is always consumed - baseGas := uint64(51822) // baseGas is the gas consumed before tx msg + baseGas := uint64(51849) // baseGas is the gas consumed before tx msg expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas) if expGasConsumed > txtypes.MaxGasWanted { // capped by gasLimit diff --git a/client/keys/add.go b/client/keys/add.go index 560b29ed3252..70281df11e45 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -78,7 +78,7 @@ Example: f.Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation") f.Uint32(flagAccount, 0, "Account number for HD derivation (less than equal 2147483647)") f.Uint32(flagIndex, 0, "Address index number for HD derivation (less than equal 2147483647)") - f.String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + f.String(flags.FlagKeyType, string(hd.Sm2Type), "Key signing algorithm to generate keys for") // support old flags name for backwards compatibility f.SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { diff --git a/client/tx/legacy.go b/client/tx/legacy.go index c07982a3fe4d..7750aff96e28 100644 --- a/client/tx/legacy.go +++ b/client/tx/legacy.go @@ -2,6 +2,7 @@ package tx import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" @@ -63,3 +64,24 @@ func CopyTx(tx signing.Tx, builder client.TxBuilder, ignoreSignatureError bool) return nil } + +// ConvertAndEncodeStdTx encodes the stdTx as a transaction in the format specified by txConfig +func ConvertAndEncodeStdTx(txConfig client.TxConfig, stdTx legacytx.StdTx) ([]byte, error) { + builder := txConfig.NewTxBuilder() + + var theTx sdk.Tx + + // check if we need a StdTx anyway, in that case don't copy + if _, ok := builder.GetTx().(legacytx.StdTx); ok { + theTx = stdTx + } else { + err := CopyTx(stdTx, builder, false) + if err != nil { + return nil, err + } + + theTx = builder.GetTx() + } + + return txConfig.TxEncoder()(theTx) +} diff --git a/crypto/codec/amino.go b/crypto/codec/amino.go index 27bb10ce46b4..7b43ec743802 100644 --- a/crypto/codec/amino.go +++ b/crypto/codec/amino.go @@ -7,6 +7,7 @@ import ( "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" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) @@ -20,6 +21,8 @@ func RegisterCrypto(cdc *codec.LegacyAmino) { ed25519.PubKeyName, nil) cdc.RegisterConcrete(&secp256k1.PubKey{}, secp256k1.PubKeyName, nil) + cdc.RegisterConcrete(&sm2.PubKey{}, + sm2.PubKeyName, nil) cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{}, kmultisig.PubKeyAminoRoute, nil) @@ -30,4 +33,6 @@ func RegisterCrypto(cdc *codec.LegacyAmino) { ed25519.PrivKeyName, nil) cdc.RegisterConcrete(&secp256k1.PrivKey{}, secp256k1.PrivKeyName, nil) + cdc.RegisterConcrete(&sm2.PrivKey{}, + sm2.PrivKeyName, nil) } diff --git a/crypto/codec/proto.go b/crypto/codec/proto.go index 1340dab03de5..06ec4ae1f431 100644 --- a/crypto/codec/proto.go +++ b/crypto/codec/proto.go @@ -6,6 +6,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) @@ -14,11 +15,13 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { var pk *cryptotypes.PubKey registry.RegisterInterface("cosmos.crypto.PubKey", pk) registry.RegisterImplementations(pk, &ed25519.PubKey{}) + registry.RegisterImplementations(pk, &sm2.PubKey{}) registry.RegisterImplementations(pk, &secp256k1.PubKey{}) registry.RegisterImplementations(pk, &multisig.LegacyAminoPubKey{}) var priv *cryptotypes.PrivKey registry.RegisterInterface("cosmos.crypto.PrivKey", priv) + registry.RegisterImplementations(priv, &sm2.PrivKey{}) registry.RegisterImplementations(priv, &secp256k1.PrivKey{}) registry.RegisterImplementations(priv, &ed25519.PrivKey{}) //nolint secp256r1.RegisterInterfaces(registry) diff --git a/crypto/codec/tm.go b/crypto/codec/tm.go index 4eb365e91c35..9b8eb5bed340 100644 --- a/crypto/codec/tm.go +++ b/crypto/codec/tm.go @@ -7,6 +7,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -22,6 +23,10 @@ func FromTmProtoPublicKey(protoPk tmprotocrypto.PublicKey) (cryptotypes.PubKey, return &secp256k1.PubKey{ Key: protoPk.Secp256K1, }, nil + case *tmprotocrypto.PublicKey_Sm2: + return &sm2.PubKey{ + Key: protoPk.Sm2, + }, nil default: return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from Tendermint public key", protoPk) } @@ -42,6 +47,12 @@ func ToTmProtoPublicKey(pk cryptotypes.PubKey) (tmprotocrypto.PublicKey, error) Secp256K1: pk.Key, }, }, nil + case *sm2.PubKey: + return tmprotocrypto.PublicKey{ + Sum: &tmprotocrypto.PublicKey_Sm2{ + Sm2: pk.Key, + }, + }, nil default: return tmprotocrypto.PublicKey{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk) } diff --git a/crypto/hd/algo.go b/crypto/hd/algo.go index 0feb4ff49b47..bff70ae907ca 100644 --- a/crypto/hd/algo.go +++ b/crypto/hd/algo.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/go-bip39" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" "github.com/cosmos/cosmos-sdk/crypto/types" ) @@ -20,11 +21,15 @@ const ( Ed25519Type = PubKeyType("ed25519") // Sr25519Type represents the Sr25519Type signature system. Sr25519Type = PubKeyType("sr25519") + // Sm2Type represents the Sm2Type signature system. + Sm2Type = PubKeyType("sm2") ) // Secp256k1 uses the Bitcoin secp256k1 ECDSA parameters. var Secp256k1 = secp256k1Algo{} +var Sm2 = sm2Algo{} + type ( DeriveFn func(mnemonic string, bip39Passphrase, hdPath string) ([]byte, error) GenerateFn func(bz []byte) types.PrivKey @@ -68,3 +73,35 @@ func (s secp256k1Algo) Generate() GenerateFn { return &secp256k1.PrivKey{Key: bzArr} } } + +type sm2Algo struct{} + +func (s sm2Algo) Name() PubKeyType { + return Sm2Type +} + +// Derive derives and returns the secp256k1 private key for the given seed and HD path. +func (s sm2Algo) 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 sm2 private key from the given bytes. +func (s sm2Algo) Generate() GenerateFn { + return func(bz []byte) types.PrivKey { + var bzArr [sm2.PrivKeySize]byte + copy(bzArr[:], bz) + return &sm2.PrivKey{Key: bzArr[:]} + } +} diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index af0d4f8aae4f..1a25a6633234 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -214,8 +214,8 @@ func newKeystore(kr keyring.Keyring, cdc codec.Codec, backend string, opts ...Op // Default options for keybase, these can be overwritten using the // Option function options := Options{ - SupportedAlgos: SigningAlgoList{hd.Secp256k1}, - SupportedAlgosLedger: SigningAlgoList{hd.Secp256k1}, + SupportedAlgos: SigningAlgoList{hd.Secp256k1, hd.Sm2}, + SupportedAlgosLedger: SigningAlgoList{hd.Secp256k1, hd.Sm2}, } for _, optionFn := range opts { diff --git a/crypto/keys/sm2/keys.pb.go b/crypto/keys/sm2/keys.pb.go new file mode 100644 index 000000000000..070be77c0bba --- /dev/null +++ b/crypto/keys/sm2/keys.pb.go @@ -0,0 +1,500 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: cosmos/crypto/sm2/keys.proto + +package sm2 + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-sdk/types/tx/amino" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/cosmos/gogoproto/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 secp256k1 public key +// Key is the compressed 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_5b7c2a655929c3c2, []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 secp256k1 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_5b7c2a655929c3c2, []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.sm2.PubKey") + proto.RegisterType((*PrivKey)(nil), "cosmos.crypto.sm2.PrivKey") +} + +func init() { proto.RegisterFile("cosmos/crypto/sm2/keys.proto", fileDescriptor_5b7c2a655929c3c2) } + +var fileDescriptor_5b7c2a655929c3c2 = []byte{ + // 238 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x49, 0xce, 0x2f, 0xce, + 0xcd, 0x2f, 0xd6, 0x4f, 0x2e, 0xaa, 0x2c, 0x28, 0xc9, 0xd7, 0x2f, 0xce, 0x35, 0xd2, 0xcf, 0x4e, + 0xad, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x84, 0xc8, 0xea, 0x41, 0x64, 0xf5, + 0x8a, 0x73, 0x8d, 0xa4, 0x04, 0x13, 0x73, 0x33, 0xf3, 0xf2, 0xf5, 0xc1, 0x24, 0x44, 0x95, 0x94, + 0x48, 0x7a, 0x7e, 0x7a, 0x3e, 0x98, 0xa9, 0x0f, 0x62, 0x41, 0x44, 0x95, 0xdc, 0xb9, 0xd8, 0x02, + 0x4a, 0x93, 0xbc, 0x53, 0x2b, 0x85, 0x04, 0xb8, 0x98, 0xb3, 0x53, 0x2b, 0x25, 0x18, 0x15, 0x18, + 0x35, 0x78, 0x82, 0x40, 0x4c, 0x2b, 0xed, 0x19, 0x0b, 0xe4, 0x19, 0xba, 0x9e, 0x6f, 0xd0, 0x12, + 0x29, 0x49, 0xcd, 0x4b, 0x49, 0x2d, 0xca, 0xcd, 0xcc, 0x2b, 0xd1, 0x87, 0xa8, 0x0e, 0xce, 0x35, + 0x9a, 0xf4, 0x7c, 0x83, 0x16, 0x67, 0x76, 0x6a, 0x65, 0x7c, 0x5a, 0x66, 0x6a, 0x4e, 0x8a, 0x92, + 0x2b, 0x17, 0x7b, 0x40, 0x51, 0x66, 0x19, 0x76, 0x93, 0x34, 0x40, 0xa6, 0x88, 0x22, 0x9b, 0x02, + 0x51, 0x8a, 0x61, 0x8c, 0x93, 0xdb, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, + 0x24, 0xc7, 0x38, 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, + 0xe9, 0xa4, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xc3, 0x82, 0x03, 0x4c, + 0xe9, 0x16, 0xa7, 0x64, 0xc3, 0x42, 0x06, 0x14, 0x2a, 0xa0, 0xe0, 0x49, 0x62, 0x03, 0x7b, 0xcf, + 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xfd, 0xa3, 0x6c, 0x09, 0x3a, 0x01, 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/sm2/sm2.go b/crypto/keys/sm2/sm2.go new file mode 100644 index 000000000000..024e755df3eb --- /dev/null +++ b/crypto/keys/sm2/sm2.go @@ -0,0 +1,227 @@ +package sm2 + +import ( + "crypto/rand" + "crypto/sha256" + "crypto/subtle" + "fmt" + "io" + "math/big" + + "github.com/tjfoc/gmsm/sm2" + + "github.com/cometbft/cometbft/crypto" + ctmsm2 "github.com/cometbft/cometbft/crypto/sm2" + "github.com/cometbft/cometbft/crypto/tmhash" + + "github.com/cosmos/cosmos-sdk/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" + "github.com/cosmos/cosmos-sdk/types/errors" +) + +const ( + PrivKeyName = "cosmos/PrivKeySm2" + PubKeyName = "cosmos/PubKeySm2" + + PrivKeySize = 32 + PubKeySize = 33 + SignatureSize = 64 + + keyType = "sm2" +) + +var ( + _ cryptotypes.PrivKey = &PrivKey{} + _ codec.AminoMarshaler = &PrivKey{} +) + +// -------------------------------------------------------- +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) +} + +func (privKey PrivKey) Bytes() []byte { + return privKey.Key +} + +func (privKey PrivKey) Sign(msg []byte) ([]byte, error) { + priv := privKey.GetPrivateKey() + r, s, err := sm2.Sm2Sign(priv, msg, nil, rand.Reader) + if err != nil { + return nil, err + } + R := r.Bytes() + S := s.Bytes() + sig := make([]byte, 64) + copy(sig[32-len(R):32], R[:]) + copy(sig[64-len(S):64], S[:]) + + return sig, nil +} + +func (privKey PrivKey) PubKey() cryptotypes.PubKey { + priv := privKey.GetPrivateKey() + compPubkey := sm2.Compress(&priv.PublicKey) + + pubkeyBytes := make([]byte, PubKeySize) + copy(pubkeyBytes, compPubkey) + + return &PubKey{Key: pubkeyBytes} +} + +func (privKey PrivKey) Equals(other cryptotypes.LedgerPrivKey) bool { + if privKey.Type() != other.Type() { + return false + } + + return subtle.ConstantTimeCompare(privKey.Bytes(), other.Bytes()) == 1 +} + +func (privKey PrivKey) GetPrivateKey() *sm2.PrivateKey { + k := new(big.Int).SetBytes(privKey.Key[:32]) + c := sm2.P256Sm2() + priv := new(sm2.PrivateKey) + priv.PublicKey.Curve = c + priv.D = k + priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes()) + + return priv +} + +func GenPrivKey() PrivKey { + return genPrivKey(crypto.CReader()) +} + +func genPrivKey(rand io.Reader) PrivKey { + seed := make([]byte, 32) + if _, err := io.ReadFull(rand, seed); err != nil { + panic(err) + } + + privKey, err := sm2.GenerateKey(rand) + if err != nil { + panic(err) + } + + privKeyBytes := make([]byte, PrivKeySize) + copy(privKeyBytes, privKey.D.Bytes()) + + return PrivKey{Key: privKeyBytes} +} + +func GenPrivKeyFromSecret(secret []byte) PrivKey { + one := new(big.Int).SetInt64(1) + secHash := sha256.Sum256(secret) + + k := new(big.Int).SetBytes(secHash[:]) + n := new(big.Int).Sub(sm2.P256Sm2().Params().N, one) + k.Mod(k, n) + k.Add(k, one) + + return PrivKey{Key: k.Bytes()} +} + +var _ cryptotypes.PubKey = &PubKey{} +var _ codec.AminoMarshaler = &PubKey{} + +// -------------------------------------------------------- + +func (pubKey PubKey) Address() crypto.Address { + if len(pubKey.Key) != PubKeySize { + panic("pubkey is incorrect size") + } + return crypto.Address(tmhash.SumTruncated(pubKey.Key)) +} + +func (pubKey PubKey) Bytes() []byte { + return pubKey.Key +} + +func (pubKey *PubKey) VerifySignature(msg []byte, sig []byte) bool { + if len(sig) != SignatureSize { + return false + } + + publicKey := sm2.Decompress(pubKey.Key) + r := new(big.Int).SetBytes(sig[:32]) + s := new(big.Int).SetBytes(sig[32:]) + + return sm2.Sm2Verify(publicKey, msg, nil, r, s) +} + +func (pubKey PubKey) String() string { + return fmt.Sprintf("PubKeySm2{%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) +} + +// AsTmPubKey converts our own PubKey into a Tendermint ED25519 pubkey. +func (pubKey *PubKey) AsTmPubKey() crypto.PubKey { + var pubkey ctmsm2.PubKeySm2 + copy(pubkey[:], pubKey.Key) + return pubkey +} diff --git a/crypto/keys/sm2/sm2_test.go b/crypto/keys/sm2/sm2_test.go new file mode 100644 index 000000000000..77a6a918bb04 --- /dev/null +++ b/crypto/keys/sm2/sm2_test.go @@ -0,0 +1,67 @@ +package sm2_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/cometbft/cometbft/crypto" + + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" +) + +// nolint +func TestSignAndValidate(t *testing.T) { + for i := 0; i < 1000; i++ { + privKey := sm2.GenPrivKey() + pubKey := privKey.PubKey() + + msg := crypto.CRandBytes(128) + sig, err := privKey.Sign(msg) + require.Nil(t, err) + + // Test the signature + if !pubKey.VerifySignature(msg, sig) { + fmt.Printf("# %d: Verify error\n", i) + } + + // Mutate the signature, just one bit. + sig[7] ^= byte(0x01) + + assert.False(t, pubKey.VerifySignature(msg, sig)) + } + +} + +func TestSm2SignAndSm2Validate(t *testing.T) { + for i := 0; i < 1000; i++ { + privKey := sm2.GenPrivKey() + pubKey := privKey.PubKey().(*sm2.PubKey) + + msg := crypto.CRandBytes(128) + sig, err := privKey.Sign(msg) + require.Nil(t, err) + + // Test the signature + if !pubKey.VerifySignature(msg, sig) { + fmt.Printf("# %d: Verify error\n", i) + } + + // Mutate the signature, just one bit. + sig[7] ^= byte(0x01) + + assert.False(t, pubKey.VerifySignature(msg, sig)) + } + +} + +func TestGenPrivKeySm2FromSecret(t *testing.T) { + a := sm2.GenPrivKeyFromSecret([]byte("mySecret1")) + b := sm2.GenPrivKeyFromSecret([]byte("mySecret1")) + c := sm2.GenPrivKeyFromSecret([]byte("mySecret2")) + + require.Equal(t, a, b) + require.NotEqual(t, a, c) +} diff --git a/crypto/ledger/encode_test.go b/crypto/ledger/encode_test.go index 7a60c3d147f5..1af44ac4e033 100644 --- a/crypto/ledger/encode_test.go +++ b/crypto/ledger/encode_test.go @@ -34,10 +34,12 @@ func ExamplePrintRegisteredTypes() { // | Type | Name | Prefix | Length | Notes | // | ---- | ---- | ------ | ----- | ------ | // | PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable | | + // | PubKeySm2 | tendermint/PubKeySm2 | 0xE7CD5A35 | 0x21 | | // | PubKey | tendermint/PubKeyEd25519 | 0x1624DE64 | variable | | // | PubKey | tendermint/PubKeySr25519 | 0x0DFB1005 | variable | | // | PubKey | tendermint/PubKeySecp256k1 | 0xEB5AE987 | variable | | // | PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | | + // | PrivKeySm2 | tendermint/PrivKeySm2 | 0x5D16439B | 0x20 | | // | PrivKey | tendermint/PrivKeyEd25519 | 0xA3288910 | variable | | // | PrivKey | tendermint/PrivKeySr25519 | 0x2F82D78B | variable | | // | PrivKey | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | variable | | diff --git a/go.mod b/go.mod index ca93092ef211..c9bbfe131820 100644 --- a/go.mod +++ b/go.mod @@ -53,6 +53,7 @@ require ( github.com/stretchr/testify v1.8.2 github.com/tendermint/go-amino v0.16.0 github.com/tidwall/btree v1.6.0 + github.com/tjfoc/gmsm v1.4.0 golang.org/x/crypto v0.7.0 golang.org/x/exp v0.0.0-20230321023759-10a507213a29 google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 @@ -167,6 +168,8 @@ require ( replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 + // CometBFT with sm2 crypto + github.com/cometbft/cometbft => github.com/bianjieai/cometbft v0.37.1-irita-230607 // dgrijalva/jwt-go is deprecated and doesn't receive security updates. // TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134 github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2 diff --git a/go.sum b/go.sum index c0b8cce47ab2..9891e6cf6df4 100644 --- a/go.sum +++ b/go.sum @@ -253,6 +253,8 @@ github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d/go.mod h1:6QX/PXZ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 h1:41iFGWnSlI2gVpmOtVTJZNodLdLQLn/KsJqFvXwnd/s= github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= +github.com/bianjieai/cometbft v0.37.1-irita-230607 h1:IwDc1Fr8fEf8mwhvo4/K1RpCDzAiJD84G3wBxy3umOU= +github.com/bianjieai/cometbft v0.37.1-irita-230607/go.mod h1:zbbBRRgCf1TVYNjZ5J/JC9iVa5Et/r2sq/xhsSQvyMQ= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJSRsjzQ= @@ -300,8 +302,6 @@ github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:z github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA= github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c= -github.com/cometbft/cometbft v0.37.1 h1:KLxkQTK2hICXYq21U2hn1W5hOVYUdQgDQ1uB+90xPIg= -github.com/cometbft/cometbft v0.37.1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs= github.com/cometbft/cometbft-db v0.7.0 h1:uBjbrBx4QzU0zOEnU8KxoDl18dMNgDh+zZRUE0ucsbo= github.com/cometbft/cometbft-db v0.7.0/go.mod h1:yiKJIm2WKrt6x8Cyxtq9YTEcIMPcEe4XPxhgX59Fzf0= github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4= @@ -938,6 +938,8 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg= github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= +github.com/tjfoc/gmsm v1.4.0 h1:8nbaiZG+iVdh+fXVw0DZoZZa7a4TGm3Qab+xdrdzj8s= +github.com/tjfoc/gmsm v1.4.0/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= @@ -997,6 +999,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -1080,6 +1083,7 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= diff --git a/proto/cosmos/auth/v1beta1/auth.proto b/proto/cosmos/auth/v1beta1/auth.proto index 0578453ccfbb..938fbb869a44 100644 --- a/proto/cosmos/auth/v1beta1/auth.proto +++ b/proto/cosmos/auth/v1beta1/auth.proto @@ -55,4 +55,5 @@ message Params { uint64 tx_size_cost_per_byte = 3; uint64 sig_verify_cost_ed25519 = 4 [(gogoproto.customname) = "SigVerifyCostED25519"]; uint64 sig_verify_cost_secp256k1 = 5 [(gogoproto.customname) = "SigVerifyCostSecp256k1"]; + uint64 sig_verify_cost_sm2 = 6 [(gogoproto.customname) = "SigVerifyCostSm2"]; } diff --git a/proto/cosmos/crypto/sm2/keys.proto b/proto/cosmos/crypto/sm2/keys.proto new file mode 100644 index 000000000000..9a926279291a --- /dev/null +++ b/proto/cosmos/crypto/sm2/keys.proto @@ -0,0 +1,28 @@ +syntax = "proto3"; +package cosmos.crypto.sm2; + +import "amino/amino.proto"; +import "gogoproto/gogo.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/crypto/keys/sm2"; + +// PubKey defines a secp256k1 public key +// Key is the compressed 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 (amino.name) = "tendermint/PubKeySm2"; + option (amino.message_encoding) = "key_field"; + option (gogoproto.goproto_stringer) = false; + + bytes key = 1; +} + +// PrivKey defines a secp256k1 private key. +message PrivKey { + option (amino.name) = "tendermint/PrivKeySm2"; + option (amino.message_encoding) = "key_field"; + + bytes key = 1; +} \ No newline at end of file diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 58a6223c89aa..9eb0f36bc98b 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -82,7 +82,7 @@ func addTestnetFlagsToCmd(cmd *cobra.Command) { cmd.Flags().StringP(flagOutputDir, "o", "./.testnets", "Directory to store initialization data for the testnet") cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().String(server.FlagMinGasPrices, fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom), "Minimum gas prices to accept for transactions; All fees in a tx must meet this minimum (e.g. 0.01photino,0.001stake)") - cmd.Flags().String(flags.FlagKeyType, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for") + cmd.Flags().String(flags.FlagKeyType, string(hd.Sm2Type), "Key signing algorithm to generate keys for") // support old flags name for backwards compatibility cmd.Flags().SetNormalizeFunc(func(f *pflag.FlagSet, name string) pflag.NormalizedName { diff --git a/testutil/network/network.go b/testutil/network/network.go index 0724a97f3b3a..285ba1f34828 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -131,7 +131,7 @@ func DefaultConfig(factory TestFixtureFactory) Config { BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction), PruningStrategy: pruningtypes.PruningOptionNothing, CleanupDir: true, - SigningAlgo: string(hd.Secp256k1Type), + SigningAlgo: string(hd.Sm2Type), KeyringOptions: []keyring.Option{}, PrintMnemonic: false, } diff --git a/testutil/testdata/tx.go b/testutil/testdata/tx.go index 3d130b398877..3333454d6c80 100644 --- a/testutil/testdata/tx.go +++ b/testutil/testdata/tx.go @@ -59,7 +59,7 @@ func NewTestFeeAmount() sdk.Coins { // NewTestGasLimit is a test fee gas limit. func NewTestGasLimit() uint64 { - return 200000 + return 400000 } // NewTestMsg creates a message for testing with the given signers. diff --git a/types/abci.go b/types/abci.go index ebae95e92c3e..94c34f6aa85c 100644 --- a/types/abci.go +++ b/types/abci.go @@ -20,7 +20,7 @@ type BeginBlocker func(ctx Context, req abci.RequestBeginBlock) abci.ResponseBeg type EndBlocker func(ctx Context, req abci.RequestEndBlock) abci.ResponseEndBlock // PeerFilter responds to p2p filtering queries from Tendermint -type PeerFilter func(info string) abci.ResponseQuery +type PeerFilter func(ctx Context, info string) abci.ResponseQuery // ProcessProposalHandler defines a function type alias for processing a proposer type ProcessProposalHandler func(Context, abci.RequestProcessProposal) abci.ResponseProcessProposal diff --git a/x/auth/ante/ante_test.go b/x/auth/ante/ante_test.go index 0ff3cdaefbf0..1c912a3383ba 100644 --- a/x/auth/ante/ante_test.go +++ b/x/auth/ante/ante_test.go @@ -1457,9 +1457,9 @@ func TestAnteHandlerReCheck(t *testing.T) { name string params authtypes.Params }{ - {"memo size check", authtypes.NewParams(1, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1)}, - {"txsize check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, 10000000, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1)}, - {"sig verify cost check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, 100000000)}, + {"memo size check", authtypes.NewParams(1, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1, authtypes.DefaultSigVerifyCostSm2)}, + {"txsize check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, 10000000, authtypes.DefaultSigVerifyCostED25519, authtypes.DefaultSigVerifyCostSecp256k1, authtypes.DefaultSigVerifyCostSm2)}, + {"sig verify cost check", authtypes.NewParams(authtypes.DefaultMaxMemoCharacters, authtypes.DefaultTxSigLimit, authtypes.DefaultTxSizeCostPerByte, authtypes.DefaultSigVerifyCostED25519, 100000000, authtypes.DefaultSigVerifyCostSm2)}, } for _, tc := range testCases { diff --git a/x/auth/ante/basic.go b/x/auth/ante/basic.go index c14d511e6b09..b26a6cf4ea5d 100644 --- a/x/auth/ante/basic.go +++ b/x/auth/ante/basic.go @@ -114,16 +114,16 @@ func (cgts ConsumeTxSizeGasDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, sim acc := cgts.ak.GetAccount(ctx, signer) - // use placeholder simSecp256k1Pubkey if sig is nil + // use placeholder simSm2Pubkey if sig is nil if acc == nil || acc.GetPubKey() == nil { - pubkey = simSecp256k1Pubkey + pubkey = simSm2Pubkey } else { pubkey = acc.GetPubKey() } // use stdsignature to mock the size of a full signature simSig := legacytx.StdSignature{ //nolint:staticcheck // this will be removed when proto is ready - Signature: simSecp256k1Sig[:], + Signature: simSm2Sig[:], PubKey: pubkey, } diff --git a/x/auth/ante/sigverify.go b/x/auth/ante/sigverify.go index a24af86667e0..8e181e8abac7 100644 --- a/x/auth/ante/sigverify.go +++ b/x/auth/ante/sigverify.go @@ -10,6 +10,7 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" sdk "github.com/cosmos/cosmos-sdk/types" @@ -22,18 +23,16 @@ import ( var ( // simulation signature values used to estimate gas consumption - key = make([]byte, secp256k1.PubKeySize) - simSecp256k1Pubkey = &secp256k1.PubKey{Key: key} - simSecp256k1Sig [64]byte + simSm2Pubkey *sm2.PubKey + simSm2Sig [64]byte _ authsigning.SigVerifiableTx = (*legacytx.StdTx)(nil) // assert StdTx implements SigVerifiableTx ) func init() { - // This decodes a valid hex string into a sepc256k1Pubkey for use in transaction simulation + // This decodes a valid hex string into a simSm2Pubkey for use in transaction simulation bz, _ := hex.DecodeString("035AD6810A47F073553FF30D2FCC7E0D3B1C0B74B61A1AAA2582344037151E143A") - copy(key, bz) - simSecp256k1Pubkey.Key = key + simSm2Pubkey = &sm2.PubKey{Key: bz} } // SignatureVerificationGasConsumer is the type of function that is used to both @@ -72,7 +71,7 @@ func (spkd SetPubKeyDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate b if !simulate { continue } - pk = simSecp256k1Pubkey + pk = simSm2Pubkey } // Only make check if simulate=false if !simulate && !bytes.Equal(pk.Address(), signers[i]) { @@ -175,7 +174,7 @@ func (sgcd SigGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simula // shall consume the largest amount, i.e. it takes more gas to verify // secp256k1 keys than ed25519 ones. if simulate && pubKey == nil { - pubKey = simSecp256k1Pubkey + pubKey = simSm2Pubkey } // make a SignatureV2 with PubKey filled in from above @@ -265,11 +264,18 @@ func (svd SigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simul } // Check account sequence number. - if sig.Sequence != acc.GetSequence() { - return ctx, sdkerrors.Wrapf( - sdkerrors.ErrWrongSequence, - "account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence, - ) + // When using Amino StdSignatures, we actually don't have the Sequence in + // the SignatureV2 struct (it's only in the SignDoc). In this case, we + // cannot check sequence directly, and must do it via signature + // verification (in the VerifySignature call below). + onlyAminoSigners := OnlyLegacyAminoSigners(sig.Data) + if !onlyAminoSigners { + if sig.Sequence != acc.GetSequence() { + return ctx, sdkerrors.Wrapf( + sdkerrors.ErrWrongSequence, + "account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence, + ) + } } // retrieve signer data @@ -392,6 +398,9 @@ func DefaultSigVerificationGasConsumer( ) error { pubkey := sig.PubKey switch pubkey := pubkey.(type) { + case *sm2.PubKey: + meter.ConsumeGas(params.SigVerifyCostSm2, "ante verify: sm2") + return nil case *ed25519.PubKey: meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519") return sdkerrors.Wrap(sdkerrors.ErrInvalidPubKey, "ED25519 public keys are unsupported") diff --git a/x/auth/ante/sigverify_test.go b/x/auth/ante/sigverify_test.go index 9f7a14f5e95f..b9affb99a522 100644 --- a/x/auth/ante/sigverify_test.go +++ b/x/auth/ante/sigverify_test.go @@ -10,6 +10,7 @@ import ( kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" + "github.com/cosmos/cosmos-sdk/crypto/keys/sm2" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/crypto/types/multisig" "github.com/cosmos/cosmos-sdk/testutil/testdata" @@ -98,6 +99,7 @@ func TestConsumeSignatureVerificationGas(t *testing.T) { {"PubKeyEd25519", args{sdk.NewInfiniteGasMeter(), nil, ed25519.GenPrivKey().PubKey(), params}, p.SigVerifyCostED25519, true}, {"PubKeySecp256k1", args{sdk.NewInfiniteGasMeter(), nil, secp256k1.GenPrivKey().PubKey(), params}, p.SigVerifyCostSecp256k1, false}, {"PubKeySecp256r1", args{sdk.NewInfiniteGasMeter(), nil, skR1.PubKey(), params}, p.SigVerifyCostSecp256r1(), false}, + {"PubKeySm2", args{sdk.NewInfiniteGasMeter(), nil, sm2.GenPrivKey().PubKey(), params}, types.DefaultSigVerifyCostSm2, false}, {"Multisig", args{sdk.NewInfiniteGasMeter(), multisignature1, multisigKey1, params}, expectedCost1, false}, {"unknown key", args{sdk.NewInfiniteGasMeter(), nil, nil, params}, 0, true}, } diff --git a/x/auth/keeper/deterministic_test.go b/x/auth/keeper/deterministic_test.go index 83f35c428e2f..a17560b9d147 100644 --- a/x/auth/keeper/deterministic_test.go +++ b/x/auth/keeper/deterministic_test.go @@ -193,6 +193,7 @@ func (suite *DeterministicTestSuite) TestGRPCQueryParameters() { rapid.Uint64Min(1).Draw(t, "tx-size-cost-per-byte"), rapid.Uint64Min(1).Draw(t, "sig-verify-cost-ed25519"), rapid.Uint64Min(1).Draw(t, "sig-verify-cost-Secp256k1"), + rapid.Uint64Min(1).Draw(t, "sig-verify-cost-Sm2"), ) err := suite.accountKeeper.SetParams(suite.ctx, params) suite.Require().NoError(err) @@ -202,13 +203,13 @@ func (suite *DeterministicTestSuite) TestGRPCQueryParameters() { }) // Regression test - params := types.NewParams(15, 167, 100, 1, 21457) + params := types.NewParams(15, 167, 100, 1, 21457, 7850) err := suite.accountKeeper.SetParams(suite.ctx, params) suite.Require().NoError(err) req := &types.QueryParamsRequest{} - testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Params, 1042, false) + testdata.DeterministicIterations(suite.ctx, suite.Require(), req, suite.queryClient.Params, 1051, true) } func (suite *DeterministicTestSuite) TestGRPCQueryAccountInfo() { diff --git a/x/auth/keeper/keeper_test.go b/x/auth/keeper/keeper_test.go index 5c875efa8f5c..68637d8c900f 100644 --- a/x/auth/keeper/keeper_test.go +++ b/x/auth/keeper/keeper_test.go @@ -172,6 +172,7 @@ func (suite *KeeperTestSuite) TestInitGenesis() { TxSizeCostPerByte: types.DefaultTxSizeCostPerByte + 1, SigVerifyCostED25519: types.DefaultSigVerifyCostED25519 + 1, SigVerifyCostSecp256k1: types.DefaultSigVerifyCostSecp256k1 + 1, + SigVerifyCostSm2: types.DefaultSigVerifyCostSm2 + 1, }, } diff --git a/x/auth/keeper/msg_server_test.go b/x/auth/keeper/msg_server_test.go index 03b923d4507c..fd0becb38fce 100644 --- a/x/auth/keeper/msg_server_test.go +++ b/x/auth/keeper/msg_server_test.go @@ -29,6 +29,7 @@ func (s *KeeperTestSuite) TestUpdateParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, }, expectErr: true, @@ -44,6 +45,7 @@ func (s *KeeperTestSuite) TestUpdateParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, }, expectErr: true, @@ -59,6 +61,7 @@ func (s *KeeperTestSuite) TestUpdateParams() { TxSizeCostPerByte: 0, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, }, expectErr: true, @@ -74,6 +77,7 @@ func (s *KeeperTestSuite) TestUpdateParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 0, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, }, expectErr: true, @@ -89,11 +93,28 @@ func (s *KeeperTestSuite) TestUpdateParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 0, + SigVerifyCostSm2: 100, }, }, expectErr: true, expErrMsg: "invalid SECK256k1 signature verification cost", }, + { + name: "set invalid sig verify cost Sm2", + req: &types.MsgUpdateParams{ + Authority: s.accountKeeper.GetAuthority(), + Params: types.Params{ + MaxMemoCharacters: 140, + TxSigLimit: 9, + TxSizeCostPerByte: 5, + SigVerifyCostED25519: 694, + SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 0, + }, + }, + expectErr: true, + expErrMsg: "invalid Sm2 signature verification cost", + }, } for _, tc := range testCases { diff --git a/x/auth/keeper/params_test.go b/x/auth/keeper/params_test.go index 4853fe563482..c06e7f9d36d1 100644 --- a/x/auth/keeper/params_test.go +++ b/x/auth/keeper/params_test.go @@ -19,6 +19,7 @@ func (s *KeeperTestSuite) TestParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, expectErr: true, expErrMsg: "invalid max memo characters", @@ -31,6 +32,7 @@ func (s *KeeperTestSuite) TestParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, expectErr: true, expErrMsg: "invalid tx signature limit", @@ -43,6 +45,7 @@ func (s *KeeperTestSuite) TestParams() { TxSizeCostPerByte: 0, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, expectErr: true, expErrMsg: "invalid tx size cost per byte", @@ -55,6 +58,7 @@ func (s *KeeperTestSuite) TestParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 0, SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 100, }, expectErr: true, expErrMsg: "invalid ED25519 signature verification cost", @@ -67,10 +71,24 @@ func (s *KeeperTestSuite) TestParams() { TxSizeCostPerByte: 5, SigVerifyCostED25519: 694, SigVerifyCostSecp256k1: 0, + SigVerifyCostSm2: 100, }, expectErr: true, expErrMsg: "invalid SECK256k1 signature verification cost", }, + { + name: "set invalid sig verify cost Sm2", + input: types.Params{ + MaxMemoCharacters: 140, + TxSigLimit: 9, + TxSizeCostPerByte: 5, + SigVerifyCostED25519: 694, + SigVerifyCostSecp256k1: 511, + SigVerifyCostSm2: 0, + }, + expectErr: true, + expErrMsg: "invalid Sm2 signature verification cost", + }, } for _, tc := range testCases { tc := tc diff --git a/x/auth/simulation/genesis.go b/x/auth/simulation/genesis.go index 4b31bf7d49e0..3434f7bbdb6f 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" + SigVerifyCostSm2 = "sig_verify_cost_sm2" ) // 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)) } +// GenSigVerifyCostSM2 randomized SigVerifyCostSM2 +func GenSigVerifyCostSM2(r *rand.Rand) uint64 { + return uint64(simulation.RandIntBetween(r, 500, 1000)) +} + // RandomizedGenState generates a random GenesisState for auth func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn types.RandomGenesisAccountsFn) { var maxMemoChars uint64 @@ -119,8 +125,21 @@ func RandomizedGenState(simState *module.SimulationState, randGenAccountsFn type func(r *rand.Rand) { sigVerifyCostSECP256K1 = GenSigVerifyCostSECP256K1(r) }, ) - params := types.NewParams(maxMemoChars, txSigLimit, txSizeCostPerByte, - sigVerifyCostED25519, sigVerifyCostSECP256K1) + var sigVerifyCostSm2 uint64 + simState.AppParams.GetOrGenerate( + simState.Cdc, SigVerifyCostSm2, &sigVerifyCostSECP256K1, simState.Rand, + func(r *rand.Rand) { sigVerifyCostSm2 = GenSigVerifyCostSM2(r) }, + ) + + params := types.NewParams( + maxMemoChars, + txSigLimit, + txSizeCostPerByte, + sigVerifyCostED25519, + sigVerifyCostSECP256K1, + sigVerifyCostSm2, + ) + 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 7c4bbe2cf1d7..50433b065b19 100644 --- a/x/auth/types/auth.pb.go +++ b/x/auth/types/auth.pb.go @@ -174,6 +174,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"` SigVerifyCostED25519 uint64 `protobuf:"varint,4,opt,name=sig_verify_cost_ed25519,json=sigVerifyCostEd25519,proto3" json:"sig_verify_cost_ed25519,omitempty"` SigVerifyCostSecp256k1 uint64 `protobuf:"varint,5,opt,name=sig_verify_cost_secp256k1,json=sigVerifyCostSecp256k1,proto3" json:"sig_verify_cost_secp256k1,omitempty"` + SigVerifyCostSm2 uint64 `protobuf:"varint,6,opt,name=sig_verify_cost_sm2,json=sigVerifyCostSm2,proto3" json:"sig_verify_cost_sm2,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -244,6 +245,13 @@ func (m *Params) GetSigVerifyCostSecp256k1() uint64 { return 0 } +func (m *Params) GetSigVerifyCostSm2() uint64 { + if m != nil { + return m.SigVerifyCostSm2 + } + return 0 +} + func init() { proto.RegisterType((*BaseAccount)(nil), "cosmos.auth.v1beta1.BaseAccount") proto.RegisterType((*ModuleAccount)(nil), "cosmos.auth.v1beta1.ModuleAccount") @@ -254,52 +262,53 @@ func init() { func init() { proto.RegisterFile("cosmos/auth/v1beta1/auth.proto", fileDescriptor_7e1f7e915d020d2d) } var fileDescriptor_7e1f7e915d020d2d = []byte{ - // 707 bytes of a gzipped FileDescriptorProto + // 731 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x54, 0xcf, 0x4e, 0xe3, 0x46, - 0x1c, 0x8e, 0x93, 0x14, 0xca, 0x04, 0x68, 0x31, 0x29, 0x35, 0x51, 0x15, 0xbb, 0x91, 0x2a, 0x52, - 0x54, 0xec, 0x26, 0x15, 0x95, 0xca, 0x8d, 0xa4, 0x55, 0x85, 0x28, 0x14, 0x39, 0x5a, 0x0e, 0xab, - 0x95, 0xac, 0xb1, 0x33, 0x98, 0x11, 0x19, 0x8f, 0xd7, 0x33, 0x46, 0x31, 0x4f, 0x80, 0xf6, 0xb4, - 0x8f, 0xc0, 0xee, 0x13, 0x70, 0xe0, 0x21, 0x56, 0x7b, 0x42, 0x7b, 0xd9, 0xdd, 0x4b, 0xb4, 0x0a, - 0x07, 0xd0, 0x3e, 0xc5, 0xca, 0x33, 0x0e, 0x24, 0x6c, 0x2e, 0x91, 0x7f, 0xdf, 0xf7, 0xfd, 0xfe, - 0x7d, 0xfe, 0xc5, 0xa0, 0xea, 0x51, 0x46, 0x28, 0xb3, 0x60, 0xcc, 0x8f, 0xad, 0xd3, 0x86, 0x8b, - 0x38, 0x6c, 0x88, 0xc0, 0x0c, 0x23, 0xca, 0xa9, 0xba, 0x2c, 0x79, 0x53, 0x40, 0x19, 0x5f, 0x59, - 0x82, 0x04, 0x07, 0xd4, 0x12, 0xbf, 0x52, 0x57, 0x59, 0x95, 0x3a, 0x47, 0x44, 0x56, 0x96, 0x24, - 0xa9, 0xb2, 0x4f, 0x7d, 0x2a, 0xf1, 0xf4, 0x69, 0x94, 0xe0, 0x53, 0xea, 0xf7, 0x90, 0x25, 0x22, - 0x37, 0x3e, 0xb2, 0x60, 0x90, 0x48, 0xaa, 0xf6, 0x2a, 0x0f, 0x4a, 0x2d, 0xc8, 0xd0, 0xb6, 0xe7, - 0xd1, 0x38, 0xe0, 0x6a, 0x13, 0xcc, 0xc2, 0x6e, 0x37, 0x42, 0x8c, 0x69, 0x8a, 0xa1, 0xd4, 0xe7, - 0x5a, 0xda, 0xbb, 0xab, 0x8d, 0x72, 0xd6, 0x63, 0x5b, 0x32, 0x1d, 0x1e, 0xe1, 0xc0, 0xb7, 0x47, - 0x42, 0xf5, 0x10, 0xcc, 0x86, 0xb1, 0xeb, 0x9c, 0xa0, 0x44, 0xcb, 0x1b, 0x4a, 0xbd, 0xd4, 0x2c, - 0x9b, 0xb2, 0xa1, 0x39, 0x6a, 0x68, 0x6e, 0x07, 0x49, 0x6b, 0xed, 0xf3, 0x40, 0x2f, 0x87, 0xb1, - 0xdb, 0xc3, 0x5e, 0xaa, 0xfd, 0x8d, 0x12, 0xcc, 0x11, 0x09, 0x79, 0xf2, 0xfa, 0xf6, 0x72, 0x1d, - 0x3c, 0x10, 0xf6, 0x4c, 0x18, 0xbb, 0xbb, 0x28, 0x51, 0x7f, 0x01, 0x8b, 0x50, 0x8e, 0xe5, 0x04, - 0x31, 0x71, 0x51, 0xa4, 0x15, 0x0c, 0xa5, 0x5e, 0xb4, 0x17, 0x32, 0x74, 0x5f, 0x80, 0x6a, 0x05, - 0x7c, 0xcb, 0xd0, 0xf3, 0x18, 0x05, 0x1e, 0xd2, 0x8a, 0x42, 0x70, 0x1f, 0x6f, 0xb5, 0xcf, 0x2f, - 0xf4, 0xdc, 0xdd, 0x85, 0x9e, 0x7b, 0x7b, 0xb5, 0xf1, 0xd3, 0x14, 0x7b, 0xcd, 0x6c, 0xef, 0x9d, - 0x17, 0xb7, 0x97, 0xeb, 0x2b, 0x52, 0xb0, 0xc1, 0xba, 0x27, 0xd6, 0x98, 0x27, 0xb5, 0x8f, 0x0a, - 0x58, 0xd8, 0xa3, 0xdd, 0xb8, 0x77, 0xef, 0xd2, 0x0e, 0x98, 0x77, 0x21, 0x43, 0x4e, 0x36, 0x88, - 0xb0, 0xaa, 0xd4, 0x34, 0xcc, 0x69, 0x1d, 0xc6, 0x2a, 0xb5, 0x8a, 0xd7, 0x03, 0x5d, 0xb1, 0x4b, - 0xee, 0x98, 0xe1, 0x2a, 0x28, 0x06, 0x90, 0x20, 0xe1, 0xdc, 0x9c, 0x2d, 0x9e, 0x55, 0x03, 0x94, - 0x42, 0x14, 0x11, 0xcc, 0x18, 0xa6, 0x01, 0xd3, 0x0a, 0x46, 0xa1, 0x3e, 0x67, 0x8f, 0x43, 0x5b, - 0xff, 0x9e, 0xcb, 0x9d, 0x6a, 0xd3, 0x3a, 0x4e, 0xcc, 0x2a, 0x36, 0xd3, 0xc6, 0x36, 0x9b, 0x60, - 0x6b, 0xcf, 0xc0, 0xf7, 0x12, 0x68, 0x47, 0xa8, 0x8b, 0x02, 0x8e, 0x61, 0x4f, 0xd5, 0x41, 0x89, - 0x08, 0xcc, 0x11, 0x93, 0x89, 0x3b, 0xb0, 0x81, 0x84, 0xf6, 0xd3, 0xf9, 0xd6, 0xc0, 0x77, 0x5d, - 0x14, 0xe1, 0x53, 0xc8, 0x31, 0x0d, 0xd2, 0x57, 0xc6, 0xb4, 0xbc, 0x51, 0xa8, 0xcf, 0xdb, 0x8b, - 0x0f, 0xf0, 0x2e, 0x4a, 0x58, 0xed, 0x7d, 0x1e, 0xcc, 0x1c, 0xc0, 0x08, 0x12, 0xa6, 0x9a, 0x60, - 0x99, 0xc0, 0xbe, 0x43, 0x10, 0xa1, 0x8e, 0x77, 0x0c, 0x23, 0xe8, 0x71, 0x14, 0xc9, 0x23, 0x2b, - 0xda, 0x4b, 0x04, 0xf6, 0xf7, 0x10, 0xa1, 0xed, 0x7b, 0x42, 0x35, 0xc0, 0x3c, 0xef, 0x3b, 0x0c, - 0xfb, 0x4e, 0x0f, 0x13, 0xcc, 0x85, 0x3f, 0x45, 0x1b, 0xf0, 0x7e, 0x07, 0xfb, 0xff, 0xa5, 0x88, - 0xfa, 0x3b, 0xf8, 0x41, 0x28, 0xce, 0x90, 0xe3, 0x51, 0xc6, 0x9d, 0x10, 0x45, 0x8e, 0x9b, 0x70, - 0x94, 0x5d, 0xc9, 0x52, 0x2a, 0x3d, 0x43, 0x6d, 0xca, 0xf8, 0x01, 0x8a, 0x5a, 0x09, 0x47, 0xea, - 0xff, 0xe0, 0xc7, 0xb4, 0xe0, 0x29, 0x8a, 0xf0, 0x51, 0x22, 0x93, 0x50, 0xb7, 0xb9, 0xb9, 0xd9, - 0xf8, 0x4b, 0x1e, 0x4e, 0x4b, 0x1b, 0x0e, 0xf4, 0x72, 0x07, 0xfb, 0x87, 0x42, 0x91, 0xa6, 0xfe, - 0xf3, 0xb7, 0xe0, 0xed, 0x32, 0x9b, 0x40, 0x65, 0x96, 0xfa, 0x04, 0xac, 0x3e, 0x2e, 0xc8, 0x90, - 0x17, 0x36, 0x37, 0xff, 0x3c, 0x69, 0x68, 0xdf, 0x88, 0x92, 0x95, 0xe1, 0x40, 0x5f, 0x99, 0x28, - 0xd9, 0x19, 0x29, 0xec, 0x15, 0x36, 0x15, 0xdf, 0xfa, 0xf9, 0xee, 0x42, 0x57, 0x1e, 0xbf, 0xb7, - 0xbe, 0xfc, 0x6e, 0x48, 0x3b, 0x5b, 0xed, 0x37, 0xc3, 0xaa, 0x72, 0x3d, 0xac, 0x2a, 0x9f, 0x86, - 0x55, 0xe5, 0xe5, 0x4d, 0x35, 0x77, 0x7d, 0x53, 0xcd, 0x7d, 0xb8, 0xa9, 0xe6, 0x9e, 0xfe, 0xea, - 0x63, 0x7e, 0x1c, 0xbb, 0xa6, 0x47, 0x49, 0xf6, 0x6d, 0xb0, 0xbe, 0xae, 0xc2, 0x93, 0x10, 0x31, - 0x77, 0x46, 0xfc, 0x3f, 0xff, 0xf8, 0x12, 0x00, 0x00, 0xff, 0xff, 0x94, 0x45, 0xcd, 0x40, 0x99, - 0x04, 0x00, 0x00, + 0x1c, 0x8e, 0x49, 0x1a, 0xca, 0x04, 0x28, 0x98, 0x94, 0x9a, 0xa8, 0x8a, 0xdd, 0x48, 0x15, 0x29, + 0x2a, 0x76, 0x93, 0x8a, 0x4a, 0xe5, 0x46, 0xd2, 0xaa, 0x42, 0x14, 0x8a, 0x1c, 0x95, 0x43, 0x55, + 0xc9, 0x1a, 0x3b, 0x83, 0x19, 0x91, 0xf1, 0xb8, 0x9e, 0x31, 0x8a, 0x79, 0x02, 0xd4, 0xd3, 0x3e, + 0xc0, 0x1e, 0xd8, 0x7d, 0x02, 0x0e, 0x3c, 0xc4, 0x6a, 0x4f, 0x68, 0x4f, 0xbb, 0x97, 0x68, 0x15, + 0x0e, 0xa0, 0x7d, 0x8a, 0x95, 0x67, 0x1c, 0x48, 0x20, 0x97, 0xc8, 0xf3, 0x7d, 0xdf, 0xef, 0xfb, + 0xfd, 0x99, 0x5f, 0x06, 0x54, 0x3d, 0xca, 0x08, 0x65, 0x16, 0x8c, 0xf9, 0x89, 0x75, 0xd6, 0x70, + 0x11, 0x87, 0x0d, 0x71, 0x30, 0xc3, 0x88, 0x72, 0xaa, 0xae, 0x48, 0xde, 0x14, 0x50, 0xc6, 0x57, + 0x96, 0x21, 0xc1, 0x01, 0xb5, 0xc4, 0xaf, 0xd4, 0x55, 0xd6, 0xa4, 0xce, 0x11, 0x27, 0x2b, 0x0b, + 0x92, 0x54, 0xd9, 0xa7, 0x3e, 0x95, 0x78, 0xfa, 0x35, 0x0a, 0xf0, 0x29, 0xf5, 0x7b, 0xc8, 0x12, + 0x27, 0x37, 0x3e, 0xb6, 0x60, 0x90, 0x48, 0xaa, 0xf6, 0x6a, 0x06, 0x94, 0x5a, 0x90, 0xa1, 0x1d, + 0xcf, 0xa3, 0x71, 0xc0, 0xd5, 0x26, 0x98, 0x85, 0xdd, 0x6e, 0x84, 0x18, 0xd3, 0x14, 0x43, 0xa9, + 0xcf, 0xb5, 0xb4, 0x77, 0xd7, 0x9b, 0xe5, 0x2c, 0xc7, 0x8e, 0x64, 0x3a, 0x3c, 0xc2, 0x81, 0x6f, + 0x8f, 0x84, 0xea, 0x11, 0x98, 0x0d, 0x63, 0xd7, 0x39, 0x45, 0x89, 0x36, 0x63, 0x28, 0xf5, 0x52, + 0xb3, 0x6c, 0xca, 0x84, 0xe6, 0x28, 0xa1, 0xb9, 0x13, 0x24, 0xad, 0xf5, 0x4f, 0x03, 0xbd, 0x1c, + 0xc6, 0x6e, 0x0f, 0x7b, 0xa9, 0xf6, 0x47, 0x4a, 0x30, 0x47, 0x24, 0xe4, 0xc9, 0xeb, 0xbb, 0xab, + 0x0d, 0xf0, 0x48, 0xd8, 0xc5, 0x30, 0x76, 0xf7, 0x50, 0xa2, 0x7e, 0x0f, 0x16, 0xa1, 0x2c, 0xcb, + 0x09, 0x62, 0xe2, 0xa2, 0x48, 0xcb, 0x1b, 0x4a, 0xbd, 0x60, 0x2f, 0x64, 0xe8, 0x81, 0x00, 0xd5, + 0x0a, 0xf8, 0x92, 0xa1, 0xff, 0x62, 0x14, 0x78, 0x48, 0x2b, 0x08, 0xc1, 0xc3, 0x79, 0xbb, 0x7d, + 0x71, 0xa9, 0xe7, 0xee, 0x2f, 0xf5, 0xdc, 0xdb, 0xeb, 0xcd, 0x6f, 0xa7, 0x8c, 0xd7, 0xcc, 0xfa, + 0xde, 0xfd, 0xff, 0xee, 0x6a, 0x63, 0x55, 0x0a, 0x36, 0x59, 0xf7, 0xd4, 0x1a, 0x9b, 0x49, 0xed, + 0x83, 0x02, 0x16, 0xf6, 0x69, 0x37, 0xee, 0x3d, 0x4c, 0x69, 0x17, 0xcc, 0xbb, 0x90, 0x21, 0x27, + 0x2b, 0x44, 0x8c, 0xaa, 0xd4, 0x34, 0xcc, 0x69, 0x19, 0xc6, 0x9c, 0x5a, 0x85, 0x9b, 0x81, 0xae, + 0xd8, 0x25, 0x77, 0x6c, 0xe0, 0x2a, 0x28, 0x04, 0x90, 0x20, 0x31, 0xb9, 0x39, 0x5b, 0x7c, 0xab, + 0x06, 0x28, 0x85, 0x28, 0x22, 0x98, 0x31, 0x4c, 0x03, 0xa6, 0xe5, 0x8d, 0x7c, 0x7d, 0xce, 0x1e, + 0x87, 0xb6, 0xff, 0xb8, 0x90, 0x3d, 0xd5, 0xa6, 0x65, 0x9c, 0xa8, 0x55, 0x74, 0xa6, 0x8d, 0x75, + 0x36, 0xc1, 0xd6, 0xfe, 0x05, 0x4b, 0x12, 0x68, 0x47, 0xa8, 0x8b, 0x02, 0x8e, 0x61, 0x4f, 0xd5, + 0x41, 0x89, 0x08, 0xcc, 0x11, 0x95, 0x89, 0x3d, 0xb0, 0x81, 0x84, 0x0e, 0xd2, 0xfa, 0xd6, 0xc1, + 0x57, 0x5d, 0x14, 0xe1, 0x33, 0xc8, 0x31, 0x0d, 0xd2, 0x2b, 0x63, 0xda, 0x8c, 0x91, 0xaf, 0xcf, + 0xdb, 0x8b, 0x8f, 0xf0, 0x1e, 0x4a, 0x58, 0xed, 0x65, 0x1e, 0x14, 0x0f, 0x61, 0x04, 0x09, 0x53, + 0x4d, 0xb0, 0x42, 0x60, 0xdf, 0x21, 0x88, 0x50, 0xc7, 0x3b, 0x81, 0x11, 0xf4, 0x38, 0x8a, 0xe4, + 0x92, 0x15, 0xec, 0x65, 0x02, 0xfb, 0xfb, 0x88, 0xd0, 0xf6, 0x03, 0xa1, 0x1a, 0x60, 0x9e, 0xf7, + 0x1d, 0x86, 0x7d, 0xa7, 0x87, 0x09, 0xe6, 0x62, 0x3e, 0x05, 0x1b, 0xf0, 0x7e, 0x07, 0xfb, 0x7f, + 0xa6, 0x88, 0xfa, 0x13, 0xf8, 0x5a, 0x28, 0xce, 0x91, 0xe3, 0x51, 0xc6, 0x9d, 0x10, 0x45, 0x8e, + 0x9b, 0x70, 0x94, 0x6d, 0xc9, 0x72, 0x2a, 0x3d, 0x47, 0x6d, 0xca, 0xf8, 0x21, 0x8a, 0x5a, 0x09, + 0x47, 0xea, 0x5f, 0xe0, 0x9b, 0xd4, 0xf0, 0x0c, 0x45, 0xf8, 0x38, 0x91, 0x41, 0xa8, 0xdb, 0xdc, + 0xda, 0x6a, 0xfc, 0x2a, 0x17, 0xa7, 0xa5, 0x0d, 0x07, 0x7a, 0xb9, 0x83, 0xfd, 0x23, 0xa1, 0x48, + 0x43, 0x7f, 0xff, 0x4d, 0xf0, 0x76, 0x99, 0x4d, 0xa0, 0x32, 0x4a, 0xfd, 0x1b, 0xac, 0x3d, 0x35, + 0x64, 0xc8, 0x0b, 0x9b, 0x5b, 0xbf, 0x9c, 0x36, 0xb4, 0x2f, 0x84, 0x65, 0x65, 0x38, 0xd0, 0x57, + 0x27, 0x2c, 0x3b, 0x23, 0x85, 0xbd, 0xca, 0xa6, 0xe2, 0x6a, 0x1b, 0xac, 0x3c, 0xb3, 0x25, 0x4d, + 0xad, 0x28, 0x0c, 0xcb, 0xc3, 0x81, 0xbe, 0x34, 0x69, 0x48, 0x9a, 0xf6, 0x12, 0x7b, 0x82, 0x6c, + 0x7f, 0x77, 0x7f, 0xa9, 0x2b, 0x4f, 0x2f, 0xbf, 0x2f, 0x1f, 0x1f, 0x79, 0x27, 0xad, 0xf6, 0x9b, + 0x61, 0x55, 0xb9, 0x19, 0x56, 0x95, 0x8f, 0xc3, 0xaa, 0xf2, 0xe2, 0xb6, 0x9a, 0xbb, 0xb9, 0xad, + 0xe6, 0xde, 0xdf, 0x56, 0x73, 0xff, 0xfc, 0xe0, 0x63, 0x7e, 0x12, 0xbb, 0xa6, 0x47, 0x49, 0xf6, + 0xc0, 0x58, 0xcf, 0x5d, 0x78, 0x12, 0x22, 0xe6, 0x16, 0xc5, 0x9f, 0xfc, 0xe7, 0xcf, 0x01, 0x00, + 0x00, 0xff, 0xff, 0x4b, 0x3f, 0x02, 0x6b, 0xde, 0x04, 0x00, 0x00, } func (this *Params) Equal(that interface{}) bool { @@ -336,6 +345,9 @@ func (this *Params) Equal(that interface{}) bool { if this.SigVerifyCostSecp256k1 != that1.SigVerifyCostSecp256k1 { return false } + if this.SigVerifyCostSm2 != that1.SigVerifyCostSm2 { + return false + } return true } func (m *BaseAccount) Marshal() (dAtA []byte, err error) { @@ -500,6 +512,11 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.SigVerifyCostSm2 != 0 { + i = encodeVarintAuth(dAtA, i, uint64(m.SigVerifyCostSm2)) + i-- + dAtA[i] = 0x30 + } if m.SigVerifyCostSecp256k1 != 0 { i = encodeVarintAuth(dAtA, i, uint64(m.SigVerifyCostSecp256k1)) i-- @@ -625,6 +642,9 @@ func (m *Params) Size() (n int) { if m.SigVerifyCostSecp256k1 != 0 { n += 1 + sovAuth(uint64(m.SigVerifyCostSecp256k1)) } + if m.SigVerifyCostSm2 != 0 { + n += 1 + sovAuth(uint64(m.SigVerifyCostSm2)) + } return n } @@ -1178,6 +1198,25 @@ func (m *Params) Unmarshal(dAtA []byte) error { break } } + case 6: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field SigVerifyCostSm2", wireType) + } + m.SigVerifyCostSm2 = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuth + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.SigVerifyCostSm2 |= 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 b157eb220eaf..a7ee7d77021f 100644 --- a/x/auth/types/params.go +++ b/x/auth/types/params.go @@ -11,16 +11,18 @@ const ( DefaultTxSizeCostPerByte uint64 = 10 DefaultSigVerifyCostED25519 uint64 = 590 DefaultSigVerifyCostSecp256k1 uint64 = 1000 + DefaultSigVerifyCostSm2 uint64 = 7850 ) // NewParams creates a new Params object -func NewParams(maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1 uint64) Params { +func NewParams(maxMemoCharacters, txSigLimit, txSizeCostPerByte, sigVerifyCostED25519, sigVerifyCostSecp256k1, sigVerifyCostSm2 uint64) Params { return Params{ MaxMemoCharacters: maxMemoCharacters, TxSigLimit: txSigLimit, TxSizeCostPerByte: txSizeCostPerByte, SigVerifyCostED25519: sigVerifyCostED25519, SigVerifyCostSecp256k1: sigVerifyCostSecp256k1, + SigVerifyCostSm2: sigVerifyCostSm2, } } @@ -32,6 +34,7 @@ func DefaultParams() Params { TxSizeCostPerByte: DefaultTxSizeCostPerByte, SigVerifyCostED25519: DefaultSigVerifyCostED25519, SigVerifyCostSecp256k1: DefaultSigVerifyCostSecp256k1, + SigVerifyCostSm2: DefaultSigVerifyCostSm2, } } @@ -86,6 +89,19 @@ func validateSigVerifyCostSecp256k1(i interface{}) error { return nil } +func validateSigVerifyCostSm2(i interface{}) error { + v, ok := i.(uint64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v == 0 { + return fmt.Errorf("invalid Sm2 signature verification cost: %d", v) + } + + return nil +} + func validateMaxMemoCharacters(i interface{}) error { v, ok := i.(uint64) if !ok { @@ -123,6 +139,9 @@ func (p Params) Validate() error { if err := validateSigVerifyCostSecp256k1(p.SigVerifyCostSecp256k1); err != nil { return err } + if err := validateSigVerifyCostSm2(p.SigVerifyCostSm2); err != nil { + return err + } if err := validateMaxMemoCharacters(p.MaxMemoCharacters); err != nil { return err } diff --git a/x/auth/types/params_legacy.go b/x/auth/types/params_legacy.go index 8800f5822283..c6f46b01aecc 100644 --- a/x/auth/types/params_legacy.go +++ b/x/auth/types/params_legacy.go @@ -16,6 +16,7 @@ var ( KeyTxSizeCostPerByte = []byte("TxSizeCostPerByte") KeySigVerifyCostED25519 = []byte("SigVerifyCostED25519") KeySigVerifyCostSecp256k1 = []byte("SigVerifyCostSecp256k1") + KeySigVerifyCostSm2 = []byte("SigVerifyCostSm2") ) var _ paramtypes.ParamSet = &Params{} diff --git a/x/auth/types/params_test.go b/x/auth/types/params_test.go index fcec36cb833c..63c02c2a794c 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.DefaultSigVerifyCostSm2), 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.DefaultSigVerifyCostSm2), 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.DefaultSigVerifyCostSm2), fmt.Errorf("invalid SECK256k1 signature verification cost: 0")}, + {"invalid SM2 signature verification cost", types.NewParams(types.DefaultMaxMemoCharacters, types.DefaultTxSigLimit, types.DefaultTxSizeCostPerByte, + types.DefaultSigVerifyCostED25519, types.DefaultSigVerifyCostSm2, 0), fmt.Errorf("invalid Sm2 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.DefaultSigVerifyCostSm2), 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.DefaultSigVerifyCostSm2), fmt.Errorf("invalid tx size cost per byte: 0")}, } for _, tt := range tests { tt := tt