Commit 27aab59b authored by madengji's avatar madengji Committed by 33cn

bls take crypto interface

parent 9ebf9348
...@@ -14,9 +14,9 @@ import ( ...@@ -14,9 +14,9 @@ import (
"time" "time"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types" pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
...@@ -33,10 +33,11 @@ const ( ...@@ -33,10 +33,11 @@ const (
type blsClient struct { type blsClient struct {
paraClient *client paraClient *client
selfID string selfID string
blsPriKey *bls.SecretKey cryptoCli crypto.Crypto
blsPubKey *bls.PublicKey blsPriKey crypto.PrivKey
blsPubKey crypto.PubKey
peers map[string]bool peers map[string]bool
peersBlsPubKey map[string]*bls.PublicKey peersBlsPubKey map[string]crypto.PubKey
commitsPool map[int64]*pt.ParaBlsSignSumDetails commitsPool map[int64]*pt.ParaBlsSignSumDetails
rcvCommitTxCh chan []*pt.ParacrossCommitAction rcvCommitTxCh chan []*pt.ParacrossCommitAction
leaderOffset int32 leaderOffset int32
...@@ -50,7 +51,12 @@ func newBlsClient(para *client, cfg *subConfig) *blsClient { ...@@ -50,7 +51,12 @@ func newBlsClient(para *client, cfg *subConfig) *blsClient {
b := &blsClient{paraClient: para} b := &blsClient{paraClient: para}
b.selfID = cfg.AuthAccount b.selfID = cfg.AuthAccount
b.peers = make(map[string]bool) b.peers = make(map[string]bool)
b.peersBlsPubKey = make(map[string]*bls.PublicKey) cli, err := crypto.New("bls")
if err != nil {
panic("new bls crypto fail")
}
b.cryptoCli = cli
b.peersBlsPubKey = make(map[string]crypto.PubKey)
b.commitsPool = make(map[int64]*pt.ParaBlsSignSumDetails) b.commitsPool = make(map[int64]*pt.ParaBlsSignSumDetails)
b.rcvCommitTxCh = make(chan []*pt.ParacrossCommitAction, maxRcvTxCount) b.rcvCommitTxCh = make(chan []*pt.ParacrossCommitAction, maxRcvTxCount)
b.quit = make(chan struct{}) b.quit = make(chan struct{})
...@@ -214,7 +220,7 @@ func (b *blsClient) sendAggregateTx(nodes []string) error { ...@@ -214,7 +220,7 @@ func (b *blsClient) sendAggregateTx(nodes []string) error {
if len(dones) <= 0 { if len(dones) <= 0 {
return nil return nil
} }
acts, err := aggregateCommit2Action(nodes, dones) acts, err := b.aggregateCommit2Action(nodes, dones)
if err != nil { if err != nil {
plog.Error("sendAggregateTx AggregateCommit2Action", "err", err) plog.Error("sendAggregateTx AggregateCommit2Action", "err", err)
return err return err
...@@ -374,7 +380,7 @@ func filterDoneCommits(peers int, pool map[int64]*pt.ParaBlsSignSumDetails) []*p ...@@ -374,7 +380,7 @@ func filterDoneCommits(peers int, pool map[int64]*pt.ParaBlsSignSumDetails) []*p
} }
//聚合多个签名为一个签名,并设置地址bitmap //聚合多个签名为一个签名,并设置地址bitmap
func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails) ([]*pt.ParacrossCommitAction, error) { func (b *blsClient) aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails) ([]*pt.ParacrossCommitAction, error) {
var notify []*pt.ParacrossCommitAction var notify []*pt.ParacrossCommitAction
for _, v := range commits { for _, v := range commits {
a := &pt.ParacrossCommitAction{Bls: &pt.ParacrossCommitBlsInfo{}} a := &pt.ParacrossCommitAction{Bls: &pt.ParacrossCommitBlsInfo{}}
...@@ -382,11 +388,11 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails) ...@@ -382,11 +388,11 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails)
types.Decode(v.Msgs[0], s) types.Decode(v.Msgs[0], s)
a.Status = s a.Status = s
sign, err := aggregateSigns(v.Signs) sign, err := b.aggregateSigns(v.Signs)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "bls aggregate=%s", v.Addrs) return nil, errors.Wrapf(err, "bls aggregate=%s", v.Addrs)
} }
a.Bls.Sign = sign.Serialize() a.Bls.Sign = sign.Bytes()
bits, remains := setAddrsBitMap(nodes, v.Addrs) bits, remains := setAddrsBitMap(nodes, v.Addrs)
plog.Debug("AggregateCommit2Action", "nodes", nodes, "addr", v.Addrs, "bits", common.ToHex(bits), "height", v.Height) plog.Debug("AggregateCommit2Action", "nodes", nodes, "addr", v.Addrs, "bits", common.ToHex(bits), "height", v.Height)
if len(remains) > 0 { if len(remains) > 0 {
...@@ -398,16 +404,16 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails) ...@@ -398,16 +404,16 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails)
return notify, nil return notify, nil
} }
func aggregateSigns(signs [][]byte) (*bls.Sign, error) { func (b *blsClient) aggregateSigns(signs [][]byte) (crypto.Signature, error) {
var sum bls.Sign var signatures []crypto.Signature
var signatures []bls.Sign
for _, data := range signs { for _, data := range signs {
var si bls.Sign si, err := b.cryptoCli.SignatureFromBytes(data)
si.Deserialize(data) if err != nil {
return nil, err
}
signatures = append(signatures, si) signatures = append(signatures, si)
} }
sum.Aggregate(signatures) return b.cryptoCli.Aggregate(signatures)
return &sum, nil
} }
func (b *blsClient) updatePeers(id string, add bool) { func (b *blsClient) updatePeers(id string, add bool) {
...@@ -427,38 +433,36 @@ func (b *blsClient) updatePeers(id string, add bool) { ...@@ -427,38 +433,36 @@ func (b *blsClient) updatePeers(id string, add bool) {
} }
func (b *blsClient) setBlsPriKey(secpPrkKey []byte) { func (b *blsClient) setBlsPriKey(secpPrkKey []byte) {
b.blsPriKey = getBlsPriKey(secpPrkKey) b.blsPriKey = b.getBlsPriKey(secpPrkKey)
b.blsPubKey = b.blsPriKey.GetPublicKey() b.blsPubKey = b.blsPriKey.PubKey()
serial := b.blsPubKey.Serialize() serial := b.blsPubKey.Bytes()
plog.Info("para commit get pub bls", "pubkey", common.ToHex(serial[:])) plog.Debug("para commit get pub bls", "pubkey", common.ToHex(serial[:]))
} }
func getBlsPriKey(key []byte) *bls.SecretKey { func (b *blsClient) getBlsPriKey(key []byte) crypto.PrivKey {
var newKey [common.Sha256Len]byte var newKey [common.Sha256Len]byte
copy(newKey[:], key) copy(newKey[:], key)
for { for {
plog.Info("para commit getBlsPriKey try", "key", common.ToHex(newKey[:])) plog.Info("para commit getBlsPriKey try", "key", common.ToHex(newKey[:]))
var secret bls.SecretKey pri, err := b.cryptoCli.PrivKeyFromBytes(newKey[:])
err := secret.Deserialize(newKey[:])
if nil != err { if nil != err {
copy(newKey[:], common.Sha256(newKey[:])) copy(newKey[:], common.Sha256(newKey[:]))
continue continue
} }
plog.Info("para commit getBlsPriKey", "final key", secret.SerializeToHexStr()) return pri
return &secret
} }
} }
//transfer secp Private key to bls pub key //transfer secp256 Private key to bls pub key
func secpPrikey2BlsPub(key string) (string, error) { func (b *blsClient) secp256Prikey2BlsPub(key string) (string, error) {
secpPrkKey, err := getSecpPriKey(key) secpPrkKey, err := getSecpPriKey(key)
if err != nil { if err != nil {
plog.Error("getSecpPriKey", "err", err) plog.Error("getSecpPriKey", "err", err)
return "", err return "", err
} }
blsPriKey := getBlsPriKey(secpPrkKey.Bytes()) blsPriKey := b.getBlsPriKey(secpPrkKey.Bytes())
blsPubKey := blsPriKey.GetPublicKey() blsPubKey := blsPriKey.PubKey()
serial := blsPubKey.Serialize() serial := blsPubKey.Bytes()
return common.ToHex(serial[:]), nil return common.ToHex(serial[:]), nil
} }
...@@ -467,10 +471,10 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error { ...@@ -467,10 +471,10 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error {
data := types.Encode(cmt.Status) data := types.Encode(cmt.Status)
cmt.Bls = &pt.ParacrossCommitBlsInfo{Addrs: []string{b.selfID}} cmt.Bls = &pt.ParacrossCommitBlsInfo{Addrs: []string{b.selfID}}
sig := b.blsPriKey.SignByte(data) sig := b.blsPriKey.Sign(data)
sign := sig.Serialize() sign := sig.Bytes()
if len(sign) <= 0 { if len(sign) <= 0 {
return errors.Wrapf(types.ErrInvalidParam, "addr=%s,prikey=%d,height=%d", b.selfID, len(b.blsPriKey.Serialize()), cmt.Status.Height) return errors.Wrapf(types.ErrInvalidParam, "addr=%s,height=%d", b.selfID, cmt.Status.Height)
} }
cmt.Bls.Sign = sign cmt.Bls.Sign = sign
plog.Debug("blsign msg", "data", common.ToHex(data), "height", cmt.Status.Height, "sign", len(cmt.Bls.Sign), "src", len(sign)) plog.Debug("blsign msg", "data", common.ToHex(data), "height", cmt.Status.Height, "sign", len(cmt.Bls.Sign), "src", len(sign))
...@@ -520,7 +524,7 @@ func isCommitDone(nodes, mostSame int) bool { ...@@ -520,7 +524,7 @@ func isCommitDone(nodes, mostSame int) bool {
return 3*mostSame > 2*nodes return 3*mostSame > 2*nodes
} }
func (b *blsClient) getBlsPubKey(addr string) (*bls.PublicKey, error) { func (b *blsClient) getBlsPubKey(addr string) (crypto.PubKey, error) {
//先从缓存中获取 //先从缓存中获取
if v, ok := b.peersBlsPubKey[addr]; ok { if v, ok := b.peersBlsPubKey[addr]; ok {
return v, nil return v, nil
...@@ -543,16 +547,20 @@ func (b *blsClient) getBlsPubKey(addr string) (*bls.PublicKey, error) { ...@@ -543,16 +547,20 @@ func (b *blsClient) getBlsPubKey(addr string) (*bls.PublicKey, error) {
return nil, err return nil, err
} }
var pubKey bls.PublicKey s, err := common.FromHex(resp.BlsPubKey)
err = pubKey.DeserializeHexStr(resp.BlsPubKey) if err != nil {
plog.Error("commitmsg.getNode pubkey nok", "pubkey", resp.BlsPubKey)
return nil, err
}
pubKey, err := b.cryptoCli.PubKeyFromBytes(s)
if err != nil { if err != nil {
plog.Error("verifyBlsSign.DeserializePublicKey", "key", addr) plog.Error("verifyBlsSign.DeserializePublicKey", "key", addr)
return nil, err return nil, err
} }
plog.Info("getBlsPubKey", "addr", addr, "pub", resp.BlsPubKey, "serial", pubKey.SerializeToHexStr()) plog.Info("getBlsPubKey", "addr", addr, "pub", resp.BlsPubKey, "serial", pubKey.Bytes())
b.peersBlsPubKey[addr] = &pubKey b.peersBlsPubKey[addr] = pubKey
return &pubKey, nil return pubKey, nil
} }
func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) error { func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) error {
...@@ -562,8 +570,7 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) ...@@ -562,8 +570,7 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction)
return errors.Wrapf(err, "pub key not exist to addr=%s", addr) return errors.Wrapf(err, "pub key not exist to addr=%s", addr)
} }
//2. 获取bls签名 //2. 获取bls签名
var sig bls.Sign sig, err := b.cryptoCli.SignatureFromBytes(commit.Bls.Sign)
sig.Deserialize(commit.Bls.Sign)
if err != nil { if err != nil {
return errors.Wrapf(err, "DeserializeSignature key=%s", common.ToHex(commit.Bls.Sign)) return errors.Wrapf(err, "DeserializeSignature key=%s", common.ToHex(commit.Bls.Sign))
} }
...@@ -572,11 +579,11 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) ...@@ -572,11 +579,11 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction)
msg := types.Encode(commit.Status) msg := types.Encode(commit.Status)
//4. 验证bls 签名 //4. 验证bls 签名
if !sig.VerifyByte(pubKey, msg) { if !pubKey.VerifyBytes(msg, sig) {
plog.Error("paracross.Commit bls sign verify", "title", commit.Status.Title, "height", commit.Status.Height, plog.Error("paracross.Commit bls sign verify", "title", commit.Status.Title, "height", commit.Status.Height,
"addrsMap", common.ToHex(commit.Bls.AddrsMap), "sign", common.ToHex(commit.Bls.Sign), "addr", addr) "addrsMap", common.ToHex(commit.Bls.AddrsMap), "sign", common.ToHex(commit.Bls.Sign), "addr", addr)
plog.Error("paracross.commit bls sign verify", "data", common.ToHex(msg), "height", commit.Status.Height, plog.Error("paracross.commit bls sign verify", "data", common.ToHex(msg), "height", commit.Status.Height,
"pub", common.ToHex(pubKey.Serialize())) "pub", common.ToHex(pubKey.Bytes()))
return pt.ErrBlsSignVerify return pt.ErrBlsSignVerify
} }
return nil return nil
......
...@@ -70,13 +70,13 @@ func TestBlsSignMain(t *testing.T) { ...@@ -70,13 +70,13 @@ func TestBlsSignMain(t *testing.T) {
func testSecpPrikey2BlsPub(t *testing.T) { func testSecpPrikey2BlsPub(t *testing.T) {
key := "" key := ""
ret, _ := secpPrikey2BlsPub(key) ret, _ := secp256Prikey2BlsPub(key)
assert.Equal(t, "", ret) assert.Equal(t, "", ret)
//real prikey="1626b254a75e5c44de9500a0c7897643e7736c09a7270b807546acb7cf7c94c9" //real prikey="1626b254a75e5c44de9500a0c7897643e7736c09a7270b807546acb7cf7c94c9"
key = "0xcacb1f5d51700aea07fca2246ab43b0917d70405c65edea9b5063d72eb5c6b71" key = "0xcacb1f5d51700aea07fca2246ab43b0917d70405c65edea9b5063d72eb5c6b71"
q := "0x980287e26d4d44f8c57944ffc096f7d98a460c97dadbffaed14ff0de901fa7f8afc59fcb1805a0b031e5eae5601df1c2" q := "0x980287e26d4d44f8c57944ffc096f7d98a460c97dadbffaed14ff0de901fa7f8afc59fcb1805a0b031e5eae5601df1c2"
ret, _ = secpPrikey2BlsPub(key) ret, _ = secp256Prikey2BlsPub(key)
assert.Equal(t, q, ret) assert.Equal(t, q, ret)
} }
......
...@@ -80,7 +80,7 @@ func (client *client) Query_BlsPubKey(req *types.ReqString) (types.Message, erro ...@@ -80,7 +80,7 @@ func (client *client) Query_BlsPubKey(req *types.ReqString) (types.Message, erro
var pub pt.BlsPubKey var pub pt.BlsPubKey
if len(req.Data) > 0 { if len(req.Data) > 0 {
p, err := secpPrikey2BlsPub(req.Data) p, err := client.blsSignCli.secp256Prikey2BlsPub(req.Data)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -89,7 +89,7 @@ func (client *client) Query_BlsPubKey(req *types.ReqString) (types.Message, erro ...@@ -89,7 +89,7 @@ func (client *client) Query_BlsPubKey(req *types.ReqString) (types.Message, erro
} }
//缺省获取钱包的 //缺省获取钱包的
if nil != client.blsSignCli.blsPubKey { if nil != client.blsSignCli.blsPubKey {
t := client.blsSignCli.blsPubKey.Serialize() t := client.blsSignCli.blsPubKey.Bytes()
pub.Key = common.ToHex(t[:]) pub.Key = common.ToHex(t[:])
return &pub, nil return &pub, nil
} }
......
...@@ -6,11 +6,11 @@ strapp=${strcmd%/cmd*} ...@@ -6,11 +6,11 @@ strapp=${strcmd%/cmd*}
OUT_DIR="${1}/$strapp" OUT_DIR="${1}/$strapp"
PARACLI="${OUT_DIR}/chain33-para-cli" #PARACLI="${OUT_DIR}/chain33-para-cli"
PARANAME=para #PARANAME=para
SRC_CLI=github.com/33cn/plugin/cli #SRC_CLI=github.com/33cn/plugin/cli
#go build -v -o "${PARACLI}" -ldflags "-X ${SRC_CLI}/buildflags.ParaName=user.p.${PARANAME}. -X ${SRC_CLI}/buildflags.RPCAddr=http://localhost:8901" "${SRC_CLI}"
go build -v -o "${PARACLI}" -ldflags "-X ${SRC_CLI}/buildflags.ParaName=user.p.${PARANAME}. -X ${SRC_CLI}/buildflags.RPCAddr=http://localhost:8901" "${SRC_CLI}"
# shellcheck disable=SC2086 # shellcheck disable=SC2086
cp ./build/* "${OUT_DIR}" cp ./build/* "${OUT_DIR}"
......
#!/usr/bin/env bash #!/usr/bin/env bash
PARA_CLI="docker exec ${NODE3} /root/chain33-para-cli" PARA_CLI="docker exec ${NODE3} /root/chain33-cli --paraName user.p.para. --rpc_laddr http://localhost:8901"
PARA_CLI2="docker exec ${NODE2} /root/chain33-para-cli" PARA_CLI2="docker exec ${NODE2} /root/chain33-cli --paraName user.p.para. --rpc_laddr http://localhost:8901"
PARA_CLI1="docker exec ${NODE1} /root/chain33-para-cli" PARA_CLI1="docker exec ${NODE1} /root/chain33-cli --paraName user.p.para. --rpc_laddr http://localhost:8901"
PARA_CLI4="docker exec ${NODE4} /root/chain33-para-cli" PARA_CLI4="docker exec ${NODE4} /root/chain33-cli --paraName user.p.para. --rpc_laddr http://localhost:8901"
PARA_CLI5="docker exec ${NODE5} /root/chain33-para-cli --paraName user.p.game." PARA_CLI5="docker exec ${NODE5} /root/chain33-cli --paraName user.p.game. --rpc_laddr http://localhost:8901"
MAIN_CLI="docker exec ${NODE3} /root/chain33-cli" MAIN_CLI="docker exec ${NODE3} /root/chain33-cli"
PARANAME="para" PARANAME="para"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment