func (pubKey PubKeySM2) KeyString() string {
	return fmt.Sprintf("%X", pubKey[:])
}
KeyString Must return the full bytes in hex.
Used for map keying, etc.
package sm2
import (
	"bytes"
	"crypto/elliptic"
	"errors"
	"fmt"
	"math/big"
	cert "github.com/33cn/chain33/system/crypto/common"
	"github.com/33cn/chain33/system/crypto/common/authority"
	"github.com/33cn/chain33/system/crypto/common/authority/utils"
	"github.com/golang/protobuf/proto"
	"github.com/33cn/chain33/common/crypto"
	"github.com/tjfoc/gmsm/sm2"
)
const (
	SM2PrivateKeyLength    = 32
	SM2PublicKeyLength     = 65
	SM2PublicKeyCompressed = 33
	SM2SignatureMinLength  = 72
)
var SM2Author = authority.Authority{}
type Driver struct{}
func (d Driver) GenKey() (crypto.PrivKey, error) {
	privKeyBytes := [SM2PrivateKeyLength]byte{}
	copy(privKeyBytes[:], crypto.CRandBytes(SM2PrivateKeyLength))
	priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:])
	copy(privKeyBytes[:], SerializePrivateKey(priv))
	return PrivKeySM2(privKeyBytes), nil
}
func (d Driver) PrivKeyFromBytes(b []byte) (privKey crypto.PrivKey, err error) {
	if len(b) != SM2PrivateKeyLength {
		return nil, errors.New("invalid priv key byte")
	}
	privKeyBytes := new([SM2PrivateKeyLength]byte)
	copy(privKeyBytes[:], b[:SM2PrivateKeyLength])
	priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKeyBytes[:])
	copy(privKeyBytes[:], SerializePrivateKey(priv))
	return PrivKeySM2(*privKeyBytes), nil
}
func (d Driver) PubKeyFromBytes(b []byte) (pubKey crypto.PubKey, err error) {
	if len(b) != SM2PublicKeyLength && len(b) != SM2PublicKeyCompressed {
		return nil, errors.New("invalid pub key byte")
	}
	pubKeyBytes := new([SM2PublicKeyLength]byte)
	copy(pubKeyBytes[:], b[:])
	return PubKeySM2(*pubKeyBytes), nil
}
func (d Driver) SignatureFromBytes(b []byte) (sig crypto.Signature, err error) {
	var certSignature cert.CertSignature
	err = proto.Unmarshal(b, &certSignature)
	if err != nil {
		return SignatureSM2(b), nil
	}
	return &SignatureS{
		Signature: SignatureSM2(certSignature.Signature),
		uid:       certSignature.Uid,
	}, nil
}
func (d Driver) Validate(msg, pub, sig []byte) error {
	err := crypto.BasicValidation(d, msg, pub, sig)
	if err != nil {
		return err
	}
	if SM2Author.IsInit {
		err = SM2Author.Validate(pub, sig)
	}
	return err
}
type PrivKeySM2 [SM2PrivateKeyLength]byte
func (privKey PrivKeySM2) Bytes() []byte {
	s := make([]byte, SM2PrivateKeyLength)
	copy(s, privKey[:])
	return s
}
func (privKey PrivKeySM2) Sign(msg []byte) crypto.Signature {
	priv, _ := privKeyFromBytes(sm2.P256Sm2(), privKey[:])
	r, s, err := sm2.Sm2Sign(priv, msg, nil)
	if err != nil {
		return nil
	}
	return SignatureSM2(Serialize(r, s))
}
func (privKey PrivKeySM2) PubKey() crypto.PubKey {
	_, pub := privKeyFromBytes(sm2.P256Sm2(), privKey[:])
	var pubSM2 PubKeySM2
	copy(pubSM2[:], sm2.Compress(pub))
	return pubSM2
}
func (privKey PrivKeySM2) Equals(other crypto.PrivKey) bool {
	if otherSecp, ok := other.(PrivKeySM2); ok {
		return bytes.Equal(privKey[:], otherSecp[:])
	}
	return false
}
func (privKey PrivKeySM2) String() string {
	return fmt.Sprintf("PrivKeySM2{*****}")
}
type PubKeySM2 [SM2PublicKeyLength]byte
func (pubKey PubKeySM2) Bytes() []byte {
	length := SM2PublicKeyLength
	if pubKey.isCompressed() {
		length = SM2PublicKeyCompressed
	}
	s := make([]byte, length)
	copy(s, pubKey[0:length])
	return s
}
func (pubKey PubKeySM2) isCompressed() bool {
	return pubKey[0] != pubkeyUncompressed
}
func (pubKey PubKeySM2) VerifyBytes(msg []byte, sig crypto.Signature) bool {
	var uid []byte
	if wrap, ok := sig.(*SignatureS); ok {
		sig = wrap.Signature
		uid = wrap.uid
	}
	sigSM2, ok := sig.(SignatureSM2)
	if !ok {
		return false
	}
	if !pubKey.isCompressed() {
		return false
	}
	pub := sm2.Decompress(pubKey[0:SM2PublicKeyCompressed])
	r, s, err := Deserialize(sigSM2)
	if err != nil {
		fmt.Printf("unmarshal sign failed")
		return false
	}
	return sm2.Sm2Verify(pub, msg, uid, r, s)
}
func (pubKey PubKeySM2) String() string {
	return fmt.Sprintf("PubKeySM2{%X}", pubKey[:])
}