Skip to content

Commit

Permalink
Merge pull request #2 from bianjieai/yuandu/v0.37.1-irita
Browse files Browse the repository at this point in the history
feat: cherry-pick sm2
  • Loading branch information
aofengli authored Jun 7, 2023
2 parents 2af25ae + f96e616 commit 9a0e094
Show file tree
Hide file tree
Showing 22 changed files with 691 additions and 32 deletions.
2 changes: 1 addition & 1 deletion abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func isValidatorTx(tx []byte) bool {
}

// format is "val:pubkey!power"
// pubkey is a base64-encoded 32-byte ed25519 key
// pubkey is a base64-encoded 32-byte ed25519 key or sm2 key
func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
tx = tx[len(ValidatorSetChangePrefix):]

Expand Down
13 changes: 13 additions & 0 deletions abci/types/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cometbft/cometbft/crypto/ed25519"
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
"github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/crypto/sm2"
)

func Ed25519ValidatorUpdate(pk []byte, power int64) ValidatorUpdate {
Expand Down Expand Up @@ -38,6 +39,18 @@ func UpdateValidator(pk []byte, power int64, keyType string) ValidatorUpdate {
PubKey: pkp,
Power: power,
}
case sm2.KeyType:
var pke sm2.PubKeySm2
copy(pke[:], pk)
pkp, err := cryptoenc.PubKeyToProto(pke)
if err != nil {
panic(err)
}
return ValidatorUpdate{
// Address:
PubKey: pkp,
Power: power,
}
default:
panic(fmt.Sprintf("key type %s not supported", keyType))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/priv_val_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"time"

"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/algo"
"github.com/cometbft/cometbft/libs/log"
cmtnet "github.com/cometbft/cometbft/libs/net"
cmtos "github.com/cometbft/cometbft/libs/os"
Expand Down Expand Up @@ -43,7 +43,7 @@ func main() {
dialer = privval.DialUnixFn(address)
case "tcp":
connTimeout := 3 * time.Second // TODO
dialer = privval.DialTCPFn(address, connTimeout, ed25519.GenPrivKey())
dialer = privval.DialTCPFn(address, connTimeout, algo.GenPrivKey())
default:
logger.Error("Unknown protocol", "protocol", protocol)
os.Exit(1)
Expand Down
175 changes: 175 additions & 0 deletions crypto/algo/algo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package algo

import (
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/sm2"
)

const (
ED25519 = "ed25519"
SM2 = "sm2"
)

var Algo = "ed25519"

func GetPubKeyType() string {
switch Algo {
case ED25519:
return ed25519.KeyType

case SM2:
return sm2.KeyType

default:
return ed25519.KeyType
}
}

func GetPrivKeyBytes(privKey crypto.PrivKey) []byte {
switch Algo {
case ED25519:
key := privKey.(ed25519.PrivKey)
return key[:]

case SM2:
key := privKey.(sm2.PrivKeySm2)
return key[:]

default:
key := privKey.(ed25519.PrivKey)
return key[:]
}
}

func GetPubKeyBytes(pubKey crypto.PubKey) []byte {
switch Algo {
case ED25519:
key := pubKey.(ed25519.PubKey)
return key[:]

case SM2:
key := pubKey.(sm2.PubKeySm2)
return key[:]

default:
key := pubKey.(ed25519.PubKey)
return key[:]
}
}

func GetPubKeyFromData(keyType string, keyData []byte) crypto.PubKey {
switch Algo {
case ED25519:
pubkey := ed25519.PubKey{}
copy(pubkey[:], keyData)
return pubkey

case SM2:
pubkey := sm2.PubKeySm2{}
copy(pubkey[:], keyData)
return pubkey

default:
pubkey := ed25519.PubKey{}
copy(pubkey[:], keyData)
return pubkey
}
}

func GenPrivKey() crypto.PrivKey {
switch Algo {
case ED25519:
return ed25519.GenPrivKey()

case SM2:
return sm2.GenPrivKey()

default:
return ed25519.GenPrivKey()
}
}

func GenPrivKeyFromSecret(secret []byte) crypto.PrivKey {
switch Algo {
case ED25519:
return ed25519.GenPrivKeyFromSecret(secret)

case SM2:
return sm2.GenPrivKeySm2FromSecret(secret)

default:
return ed25519.GenPrivKeyFromSecret(secret)
}
}

func GetPrivKeySize() int {
switch Algo {
case ED25519:
return 64

case SM2:
return sm2.PrivKeySize

default:
return 64
}
}

func GetPubKeySize() int {
switch Algo {
case ED25519:
return ed25519.PubKeySize

case SM2:
return sm2.PubKeySize

default:
return ed25519.PubKeySize
}
}

func GetSignatureSize() int {
switch Algo {
case ED25519:
return ed25519.SignatureSize

case SM2:
return sm2.SignatureSize

default:
return ed25519.SignatureSize
}
}

func VerifyPubKeyType(pubKey crypto.PubKey) bool {
switch Algo {
case ED25519:
if _, ok := pubKey.(ed25519.PubKey); ok {
return true
}

case SM2:
if _, ok := pubKey.(sm2.PubKeySm2); ok {
return true
}
}

return false
}

func VerifyPrivKeyType(privKey crypto.PrivKey) bool {
switch Algo {
case ED25519:
if _, ok := privKey.(ed25519.PrivKey); ok {
return true
}

case SM2:
if _, ok := privKey.(sm2.PrivKeySm2); ok {
return true
}
}

return false
}
15 changes: 15 additions & 0 deletions crypto/encoding/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/crypto/ed25519"
"github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cometbft/cometbft/crypto/sm2"
"github.com/cometbft/cometbft/libs/json"
pc "github.com/cometbft/cometbft/proto/tendermint/crypto"
)
Expand All @@ -32,6 +33,12 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
Secp256K1: k,
},
}
case sm2.PubKeySm2:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Sm2{
Sm2: k[:],
},
}
default:
return kp, fmt.Errorf("toproto: key type %v is not supported", k)
}
Expand All @@ -57,6 +64,14 @@ func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
pk := make(secp256k1.PubKey, secp256k1.PubKeySize)
copy(pk, k.Secp256K1)
return pk, nil
case *pc.PublicKey_Sm2:
if len(k.Sm2) != sm2.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeySm2. Got %d, expected %d",
len(k.Sm2), sm2.PubKeySize)
}
pk := sm2.PubKeySm2{}
copy(pk[:], k.Sm2)
return pk, nil
default:
return nil, fmt.Errorf("fromproto: key type %v is not supported", k)
}
Expand Down
Loading

0 comments on commit 9a0e094

Please sign in to comment.