Skip to content

Commit

Permalink
Merge pull request #30 from bianjieai/sheldon/v0.34.23-irita
Browse files Browse the repository at this point in the history
v0.34.23-irita :cherry-pick sm2
  • Loading branch information
towerkyoto authored Nov 18, 2022
2 parents e0f68fe + 07e3b27 commit aace5f9
Show file tree
Hide file tree
Showing 25 changed files with 712 additions and 48 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 @@ -208,7 +208,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/tendermint/tendermint/crypto/ed25519"
cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/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
5 changes: 3 additions & 2 deletions cmd/priv_val_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"os"
"time"

"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/algo"

"github.com/tendermint/tendermint/libs/log"
tmnet "github.com/tendermint/tendermint/libs/net"
tmos "github.com/tendermint/tendermint/libs/os"
Expand Down Expand Up @@ -43,7 +44,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/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/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/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sm2"
"github.com/tendermint/tendermint/libs/json"
pc "github.com/tendermint/tendermint/proto/tendermint/crypto"
)
Expand All @@ -20,6 +21,12 @@ func init() {
func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
var kp pc.PublicKey
switch k := k.(type) {
case sm2.PubKeySm2:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Sm2{
Sm2: k[:],
},
}
case ed25519.PubKey:
kp = pc.PublicKey{
Sum: &pc.PublicKey_Ed25519{
Expand All @@ -41,6 +48,14 @@ func PubKeyToProto(k crypto.PubKey) (pc.PublicKey, error) {
// PubKeyFromProto takes a protobuf Pubkey and transforms it to a crypto.Pubkey
func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) {
switch k := k.Sum.(type) {
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
case *pc.PublicKey_Ed25519:
if len(k.Ed25519) != ed25519.PubKeySize {
return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d",
Expand Down
26 changes: 26 additions & 0 deletions crypto/sm2/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sm2

import (
"io"
"testing"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/internal/benchmarking"
)

func BenchmarkKeyGeneration(b *testing.B) {
benchmarkKeygenWrapper := func(reader io.Reader) crypto.PrivKey {
return genPrivKey(reader)
}
benchmarking.BenchmarkKeyGeneration(b, benchmarkKeygenWrapper)
}

func BenchmarkSigning(b *testing.B) {
priv := GenPrivKey()
benchmarking.BenchmarkSigning(b, priv)
}

func BenchmarkVerification(b *testing.B) {
priv := GenPrivKey()
benchmarking.BenchmarkVerification(b, priv)
}
Loading

0 comments on commit aace5f9

Please sign in to comment.