Commit 7662dced authored by madengji's avatar madengji Committed by 33cn

take c++ bls lib in use and save 30ms

parent 3f99a0b4
......@@ -7,9 +7,8 @@ package para
import (
"encoding/hex"
"fmt"
"sync"
"sort"
"sync"
log "github.com/33cn/chain33/common/log/log15"
......@@ -27,6 +26,7 @@ import (
"github.com/33cn/chain33/types"
paracross "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"
)
......@@ -210,6 +210,8 @@ func (client *client) SetQueueClient(c queue.Client) {
func (client *client) InitBlock() {
var err error
//only init once for bls sign
bls.Init(bls.BLS12_381)
client.execAPI = api.New(client.BaseClient.GetAPI(), client.grpcClient)
cfg := client.GetAPI().GetConfig()
......
......@@ -16,7 +16,8 @@ import (
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/types"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/phoreproject/bls/g2pubs"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
)
......@@ -38,10 +39,10 @@ const (
type blsClient struct {
paraClient *client
selfID string
blsPriKey *g2pubs.SecretKey
blsPubKey *g2pubs.PublicKey
blsPriKey *bls.SecretKey
blsPubKey *bls.PublicKey
peers map[string]bool
peersBlsPubKey map[string]*g2pubs.PublicKey
peersBlsPubKey map[string]*bls.PublicKey
commitsPool map[int64]*pt.ParaBlsSignSumDetails
rcvCommitTxCh chan []*pt.ParacrossCommitAction
leaderOffset int32
......@@ -55,7 +56,7 @@ func newBlsClient(para *client, cfg *subConfig) *blsClient {
b := &blsClient{paraClient: para}
b.selfID = cfg.AuthAccount
b.peers = make(map[string]bool)
b.peersBlsPubKey = make(map[string]*g2pubs.PublicKey)
b.peersBlsPubKey = make(map[string]*bls.PublicKey)
b.commitsPool = make(map[int64]*pt.ParaBlsSignSumDetails)
b.rcvCommitTxCh = make(chan []*pt.ParacrossCommitAction, maxRcvTxCount)
b.quit = make(chan struct{})
......@@ -280,7 +281,7 @@ func (b *blsClient) checkCommitTx(txs []*types.Transaction) ([]*pt.ParacrossComm
if tx.From() != commit.Bls.Addrs[0] {
return nil, errors.Wrapf(types.ErrFromAddr, "from=%s,bls addr=%s", tx.From(), commit.Bls.Addrs[0])
}
//验证bls 签名,大概40ms, 是secp 80倍
//验证bls 签名
err = b.verifyBlsSign(tx.From(), commit)
if err != nil {
return nil, errors.Wrapf(pt.ErrBlsSignVerify, "from=%s", tx.From())
......@@ -381,7 +382,6 @@ func filterDoneCommits(peers int, pool map[int64]*pt.ParaBlsSignSumDetails) []*p
//聚合多个签名为一个签名,并设置地址bitmap
func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails) ([]*pt.ParacrossCommitAction, error) {
var notify []*pt.ParacrossCommitAction
for _, v := range commits {
a := &pt.ParacrossCommitAction{Bls: &pt.ParacrossCommitBlsInfo{}}
s := &pt.ParacrossNodeStatus{}
......@@ -392,8 +392,7 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails)
if err != nil {
return nil, errors.Wrapf(err, "bls aggregate=%s", v.Addrs)
}
signData := sign.Serialize()
a.Bls.Sign = append(a.Bls.Sign, signData[:]...)
a.Bls.Sign = sign.Serialize()
bits, remains := setAddrsBitMap(nodes, v.Addrs)
plog.Debug("AggregateCommit2Action", "nodes", nodes, "addr", v.Addrs, "bits", common.ToHex(bits), "height", v.Height)
if len(remains) > 0 {
......@@ -405,19 +404,16 @@ func aggregateCommit2Action(nodes []string, commits []*pt.ParaBlsSignSumDetails)
return notify, nil
}
func aggregateSigns(signs [][]byte) (*g2pubs.Signature, error) {
var signatures []*g2pubs.Signature
func aggregateSigns(signs [][]byte) (*bls.Sign, error) {
var sum bls.Sign
var signatures []bls.Sign
for _, data := range signs {
var s [48]byte
copy(s[:], data)
signKey, err := g2pubs.DeserializeSignature(s)
if err != nil {
return nil, errors.Wrap(err, "aggregateSigns")
}
signatures = append(signatures, signKey)
var si bls.Sign
si.Deserialize(data)
signatures = append(signatures, si)
}
return g2pubs.AggregateSignatures(signatures), nil
sum.Aggregate(signatures)
return &sum, nil
}
func (b *blsClient) updatePeers(id string, add bool) {
......@@ -438,26 +434,25 @@ func (b *blsClient) updatePeers(id string, add bool) {
func (b *blsClient) setBlsPriKey(secpPrkKey []byte) {
b.blsPriKey = getBlsPriKey(secpPrkKey)
b.blsPubKey = g2pubs.PrivToPub(b.blsPriKey)
b.blsPubKey = b.blsPriKey.GetPublicKey()
serial := b.blsPubKey.Serialize()
plog.Info("para commit get pub bls", "pubkey", common.ToHex(serial[:]))
}
//to repeat get prikey's hash until in range of bls's private key
func getBlsPriKey(key []byte) *g2pubs.SecretKey {
func getBlsPriKey(key []byte) *bls.SecretKey {
var newKey [common.Sha256Len]byte
copy(newKey[:], key)
for {
plog.Info("para commit getBlsPriKey", "keys", common.ToHex(newKey[:]))
secret := g2pubs.DeserializeSecretKey(newKey)
if nil != secret.GetFRElement() {
serial := secret.Serialize()
plog.Info("para commit getBlsPriKey", "final keys", common.ToHex(serial[:]), "string", secret.String())
return secret
plog.Info("para commit getBlsPriKey try", "key", common.ToHex(newKey[:]))
var secret bls.SecretKey
err := secret.Deserialize(newKey[:])
if nil != err {
copy(newKey[:], common.Sha256(newKey[:]))
continue
}
copy(newKey[:], common.Sha256(newKey[:]))
plog.Info("para commit getBlsPriKey", "final key", secret.SerializeToHexStr())
return &secret
}
}
//transfer secp Private key to bls pub key
......@@ -468,7 +463,7 @@ func secpPrikey2BlsPub(key string) (string, error) {
return "", err
}
blsPriKey := getBlsPriKey(secpPrkKey.Bytes())
blsPubKey := g2pubs.PrivToPub(blsPriKey)
blsPubKey := blsPriKey.GetPublicKey()
serial := blsPubKey.Serialize()
return common.ToHex(serial[:]), nil
}
......@@ -478,11 +473,12 @@ func (b *blsClient) blsSign(commits []*pt.ParacrossCommitAction) error {
data := types.Encode(cmt.Status)
cmt.Bls = &pt.ParacrossCommitBlsInfo{Addrs: []string{b.selfID}}
sign := g2pubs.Sign(data, b.blsPriKey).Serialize()
sig := b.blsPriKey.SignByte(data)
sign := sig.Serialize()
if len(sign) <= 0 {
return errors.Wrapf(types.ErrInvalidParam, "addr=%s,prikey=%d,height=%d", b.selfID, len(b.blsPriKey.Serialize()), cmt.Status.Height)
}
cmt.Bls.Sign = append(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))
}
return nil
......@@ -530,10 +526,7 @@ func isCommitDone(nodes, mostSame int) bool {
return 3*mostSame > 2*nodes
}
func (b *blsClient) getBlsPubKey(addr string) (*g2pubs.PublicKey, error) {
b.mutex.Lock()
defer b.mutex.Unlock()
func (b *blsClient) getBlsPubKey(addr string) (*bls.PublicKey, error) {
//先从缓存中获取
if v, ok := b.peersBlsPubKey[addr]; ok {
return v, nil
......@@ -556,23 +549,16 @@ func (b *blsClient) getBlsPubKey(addr string) (*g2pubs.PublicKey, error) {
return nil, err
}
//pubKeys := make([]*g2pubs.PublicKey, 0)
val, err := common.FromHex(resp.BlsPubKey)
if err != nil {
plog.Error("verifyBlsSign.fromhex", "p", addr)
return nil, err
}
k := [96]byte{}
copy(k[:], val)
pubKey, err := g2pubs.DeserializePublicKey(k)
var pubKey bls.PublicKey
err = pubKey.DeserializeHexStr(resp.BlsPubKey)
if err != nil {
plog.Error("verifyBlsSign.DeserializePublicKey", "key", addr)
return nil, err
}
plog.Info("getBlsPubKey", "addr", addr, "pub", resp.BlsPubKey, "serial", pubKey.SerializeToHexStr())
b.peersBlsPubKey[addr] = &pubKey
b.peersBlsPubKey[addr] = pubKey
return pubKey, nil
return &pubKey, nil
}
func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction) error {
......@@ -581,11 +567,9 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction)
if err != nil {
return errors.Wrapf(err, "pub key not exist to addr=%s", addr)
}
//2. 获取bls签名
signkey := [48]byte{}
copy(signkey[:], commit.Bls.Sign)
sign, err := g2pubs.DeserializeSignature(signkey)
var sig bls.Sign
sig.Deserialize(commit.Bls.Sign)
if err != nil {
return errors.Wrapf(err, "DeserializeSignature key=%s", common.ToHex(commit.Bls.Sign))
}
......@@ -593,10 +577,12 @@ func (b *blsClient) verifyBlsSign(addr string, commit *pt.ParacrossCommitAction)
//3. 获取签名前原始msg
msg := types.Encode(commit.Status)
if !g2pubs.Verify(msg, pubKey, sign) {
//4. 验证bls 签名
if !sig.VerifyByte(pubKey, msg) {
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)
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()))
return pt.ErrBlsSignVerify
}
return nil
......
......@@ -21,6 +21,8 @@
1. BLS签名需要的私钥比SECP256小一倍,采用SECP256私钥不断取hash直到满足BLS范围为止作为BLS私钥,然后确定BLS公钥
1. BLS公钥注册到主链nodegroup里面,和聚合签名一起验证BLS签名,同时防止BLS leader节点作弊
1. 对同一高度,每个节点签名的共识消息是一样的,只需要保留一份,签名聚合成一个,公钥信息压缩到一个bitmap,作为一个交易发送
1. BLS签名有两种paring曲线,G1和G2,G1产生msg较短,G2的较长,一般公钥放G2上,签名消息放G1上,ETH采用公钥放G1,签名放G2,公钥较短,消息较长,
我们由于公钥静态配置在数据库里,主链验证,签名经过消息发送,占用空间,和ETH相反比较好,但是目前静态库似乎不支持翻转,将来支持了可以改
......
......@@ -7,7 +7,11 @@ package para
import (
"testing"
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/evm/executor/vm/common"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/magiconair/properties/assert"
)
......@@ -55,13 +59,81 @@ func TestIntegrateCommits(t *testing.T) {
}
func TestSecpPrikey2BlsPub(t *testing.T) {
func TestBlsSignMain(t *testing.T) {
//只初始化一次,多次初始化会并行产生冲突
bls.Init(bls.BLS12_381)
testSecpPrikey2BlsPub(t)
testBlsSign(t)
testVerifyBlsSign(t)
}
func testSecpPrikey2BlsPub(t *testing.T) {
key := ""
ret, _ := secpPrikey2BlsPub(key)
assert.Equal(t, "", ret)
//real prikey="1626b254a75e5c44de9500a0c7897643e7736c09a7270b807546acb7cf7c94c9"
key = "0xcacb1f5d51700aea07fca2246ab43b0917d70405c65edea9b5063d72eb5c6b71"
q := "0x87c58bb6cce41842462a0030335bb95948dcfba77e47e2d8ee893c0b2c34ac20d08c9e98a883ef2a6492d0ad808ace9a1730e8bae5d3b0861aaf743449df5de510073e2991c7274cab47f327e48d7eacf300e4b24174dae2e8603d1904b8a015"
q := "0x980287e26d4d44f8c57944ffc096f7d98a460c97dadbffaed14ff0de901fa7f8afc59fcb1805a0b031e5eae5601df1c2"
ret, _ = secpPrikey2BlsPub(key)
assert.Equal(t, q, ret)
}
func testBlsSign(t *testing.T) {
status := &pt.ParacrossNodeStatus{}
status.Height = 0
status.Title = "user.p.para."
KS := "1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4"
PubKS := "a3d97d4186c80268fe6d3689dd574599e25df2dffdcff03f7d8ef64a3bd483241b7d0985958990de2d373d5604caf805"
PriKS := "6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b"
commit := &pt.ParacrossCommitAction{Status: status}
client := &blsClient{}
client.peersBlsPubKey = make(map[string]*bls.PublicKey)
var prikey bls.SecretKey
prikey.DeserializeHexStr(PriKS)
t.Log("pri", prikey.SerializeToHexStr())
client.blsPriKey = &prikey
err := client.blsSign([]*pt.ParacrossCommitAction{commit})
assert.Equal(t, err, nil)
var pub bls.PublicKey
pub.DeserializeHexStr(PubKS)
client.peersBlsPubKey[KS] = &pub
t.Log("pubks", pub.SerializeToHexStr())
var sign bls.Sign
sign.Deserialize(commit.Bls.Sign)
msg := types.Encode(status)
ret := sign.VerifyByte(&pub, msg)
assert.Equal(t, ret, true)
err = client.verifyBlsSign(KS, commit)
assert.Equal(t, err, nil)
}
func testVerifyBlsSign(t *testing.T) {
client := &blsClient{}
client.peersBlsPubKey = make(map[string]*bls.PublicKey)
KS := "1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4"
PubKS := "a3d97d4186c80268fe6d3689dd574599e25df2dffdcff03f7d8ef64a3bd483241b7d0985958990de2d373d5604caf805"
var pub bls.PublicKey
pub.DeserializeHexStr(PubKS)
client.peersBlsPubKey[KS] = &pub
commit := &pt.ParacrossCommitAction{}
blsInfo := &pt.ParacrossCommitBlsInfo{}
signData := "0x82753675393576758571cbbaefada498614b4a0a967ca2dd5724eb46ecfd1c89f1e49792ebbe1866c1d6d6ceaf3054c7189751477a5b7312218eb77dcab1bfb6287c6fbf2e1c6cf8fe2ade7c17596b081dc98be785a34db5b45a5cca08e7e744"
blsInfo.Sign = common.FromHex(signData)
status := &pt.ParacrossNodeStatus{}
data := "0x1a0c757365722e702e706172612e322097162f9d4a888121fdba2fb1ab402596acdbcb602121bd12284adb739d85f225"
msg := common.FromHex(data)
types.Decode(msg, status)
commit.Status = status
commit.Bls = blsInfo
err := client.verifyBlsSign(KS, commit)
assert.Equal(t, err, nil)
}
......@@ -26,7 +26,7 @@ import (
func init() {
//types.Init("user.p.para.", nil)
log.SetLogLevel("error")
log.SetLogLevel("info")
}
func getPrivKey(t *testing.T) crypto.PrivKey {
......
......@@ -12,6 +12,12 @@ PARANAME_GAME="game"
PARA_COIN_FROZEN="5.0000"
MainLoopCheckForkHeight="60"
BLSPUB_E5="8920442cf306fccd11e7bde3cfffe183a138a941f471df0818edff5580b3ad7df42850a5cec15e09aef0fdd4489f7c12"
BLSPUB_KS="a3d97d4186c80268fe6d3689dd574599e25df2dffdcff03f7d8ef64a3bd483241b7d0985958990de2d373d5604caf805"
BLSPUB_JR="81307df1fdde8f0e846ed1542c859c1e9daba2553e62e48db0877329c5c63fb86e70b9e2e83263da0eb7fcad275857f8"
BLSPUB_NL="ad1d9ff67d790581fa3659c1817985eeec7c65206e8a873147cd5b6bfe1356d5cd4ed1089462bd11e51705e100c95a6b"
BLSPUB_MC="980287e26d4d44f8c57944ffc096f7d98a460c97dadbffaed14ff0de901fa7f8afc59fcb1805a0b031e5eae5601df1c2"
xsedfix=""
if [ "$(uname)" == "Darwin" ]; then
xsedfix=".bak"
......@@ -496,8 +502,7 @@ function para_cross_transfer_withdraw_for_token() {
function para_create_nodegroup_gamechain() {
echo "=========== # game para chain create node group test ============="
##apply
local KS="0x8293f1e8eab2919910c2d347348d1d344a86e0dd10610ff06211f85c8cd3dfc99d81c36ef0f6ad6ba1db931d1ffbe7321411d80ce76269463301af5cce4128b196e48abced00c536f7be557fd5940ef5a0740c85a871fe81fe940aca9ed329e7"
txhash=$(${CLI} --paraName user.p.game. send para nodegroup apply -a "1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4" -p "$KS" -c 5 -k 0xd165c84ed37c2a427fea487470ee671b7a0495d68d82607cafbc6348bf23bec5)
txhash=$(${CLI} --paraName user.p.game. send para nodegroup apply -a "1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4" -p "$BLSPUB_KS" -c 5 -k 0xd165c84ed37c2a427fea487470ee671b7a0495d68d82607cafbc6348bf23bec5)
echo "tx=$txhash"
query_tx "${PARA_CLI5}" "${txhash}"
id=$txhash
......@@ -654,12 +659,7 @@ function para_create_nodegroup() {
echo "=========== # para chain create node group again ============="
##apply
local E5="0x9293f1e8eab2919910c2d347348d1d344a86e0dd10610ff06211f85c8cd3dfc99d81c36ef0f6ad6ba1db931d1ffbe7321411d80ce76269463301af5cce4128b196e48abced00c536f7be557fd5940ef5a0740c85a871fe81fe940aca9ed329e7"
local KS="0x8293f1e8eab2919910c2d347348d1d344a86e0dd10610ff06211f85c8cd3dfc99d81c36ef0f6ad6ba1db931d1ffbe7321411d80ce76269463301af5cce4128b196e48abced00c536f7be557fd5940ef5a0740c85a871fe81fe940aca9ed329e7"
local JR="0x8ed5ba075c27015e2c6da399b42da4cd272d4082b55f05c85d84b1308ec87bdb4aeea70dbef3e754eae99a6be0c0e49512d7e9197712f8538ce3d57c1b2d88e17b37f0e419f55333f6e841261a8d3151552fd7d4fd8e19f4f38a413395aab26e"
local NL="0x872e3ac07998deb12045ee48c52a8ba5d2538dc85123866fb330112eb0b805ce23f31bfde3a485cd89fac48eab48560005d12f714ca3786c7f47fe3b5edb1dc7838677c041c89cee4caf9225c1d68346bfcde3365ada0a627fbd77bc72e9b356"
local MC="0x87c58bb6cce41842462a0030335bb95948dcfba77e47e2d8ee893c0b2c34ac20d08c9e98a883ef2a6492d0ad808ace9a1730e8bae5d3b0861aaf743449df5de510073e2991c7274cab47f327e48d7eacf300e4b24174dae2e8603d1904b8a015"
local blspubs=$E5,$KS,$JR,$NL,$MC
local blspubs=$BLSPUB_E5,$BLSPUB_KS,$BLSPUB_JR,$BLSPUB_NL,$BLSPUB_MC
txhash=$(${PARA_CLI} send para nodegroup apply -a "1E5saiXVb9mW8wcWUUZjsHJPZs5GmdzuSY,1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4,1JRNjdEqp4LJ5fqycUBm9ayCKSeeskgMKR,1NLHPEcbTWWxxU3dGUZBhayjrCHD3psX7k,1MCftFynyvG2F4ED5mdHYgziDxx6vDrScs" -p "$blspubs" -c 6 -k 0xd165c84ed37c2a427fea487470ee671b7a0495d68d82607cafbc6348bf23bec5)
echo "tx=$txhash"
query_tx "${PARA_CLI}" "${txhash}"
......
......@@ -18,7 +18,7 @@ import (
"github.com/33cn/chain33/util"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/golang/protobuf/proto"
"github.com/phoreproject/bls/g2pubs"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/pkg/errors"
)
......@@ -424,44 +424,45 @@ func (a *action) verifyBlsSign(nodesArry []string, commit *pt.ParacrossCommitAct
}
pubs = append(pubs, pub)
}
//单独deserial 5ms, g2pubs的公钥结构不好整合到protobuf,就不好压缩到数据库直接读取
pubKeys := make([]*g2pubs.PublicKey, 0)
err := verifyBlsSignPlus(pubs, commit)
if err != nil {
clog.Error("paracross.Commit bls sign verify", "addr", signAddrs, "nodes", nodesArry, "from", a.fromaddr)
return nil, err
}
return signAddrs, nil
}
func verifyBlsSignPlus(pubs []string, commit *pt.ParacrossCommitAction) error {
t1 := types.Now()
//单独deserial 90us, g2pubs的公钥结构不好整合到protobuf,就不好压缩到数据库直接读取
pubKeys := make([]bls.PublicKey, 0)
for _, p := range pubs {
k := [96]byte{}
val, err := common.FromHex(p)
if err != nil {
return nil, errors.Wrapf(err, "fromhex.p=%s", p)
}
copy(k[:], val)
key, err := g2pubs.DeserializePublicKey(k)
var pub bls.PublicKey
err := pub.DeserializeHexStr(p)
if err != nil {
return nil, errors.Wrapf(err, "DeserializePublicKey=%s", p)
return errors.Wrapf(err, "DeserializePublicKey=%s", p)
}
pubKeys = append(pubKeys, key)
pubKeys = append(pubKeys, pub)
}
//2. 聚合公钥 单独耗时200us
aPub := g2pubs.AggregatePublicKeys(pubKeys)
//3. 获取聚合的签名, deserial 5ms
signkey := [48]byte{}
copy(signkey[:], commit.Bls.Sign)
sign, err := g2pubs.DeserializeSignature(signkey)
//3. 获取聚合的签名, deserial 300us
var sign bls.Sign
err := sign.Deserialize(commit.Bls.Sign)
if err != nil {
return nil, errors.Wrapf(err, "DeserializeSignature,key=%s", common.ToHex(commit.Bls.Sign))
return errors.Wrapf(err, "DeserializeSignature,key=%s", common.ToHex(commit.Bls.Sign))
}
//4. 获取签名前原始msg
msg := types.Encode(commit.Status)
if !g2pubs.Verify(msg, aPub, sign) {
//verify 1ms, total 2ms
if !sign.FastAggregateVerify(pubKeys, msg) {
clog.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", signAddrs, "nodes", nodesArry)
clog.Error("paracross.commit bls sign verify", "data", common.ToHex(msg), "height", commit.Status.Height, "from", a.fromaddr)
return nil, pt.ErrBlsSignVerify
"addrsMap", common.ToHex(commit.Bls.AddrsMap), "sign", common.ToHex(commit.Bls.Sign), "data", common.ToHex(msg))
return pt.ErrBlsSignVerify
}
return signAddrs, nil
clog.Info("paracross verifyBlsSign success", "title", commit.Status.Title, "height", commit.Status.Height, "time", types.Since(t1))
return nil
}
//共识commit msg 处理
......
......@@ -14,6 +14,7 @@ import (
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/herumi/bls-eth-go-binary/bls"
)
var (
......@@ -32,6 +33,7 @@ func Init(name string, cfg *types.Chain33Config, sub []byte) {
drivers.Register(cfg, GetName(), newParacross, cfg.GetDappFork(driverName, "Enable"))
InitExecType()
setPrefix()
bls.Init(bls.BLS12_381)
}
func InitExecType() {
......
......@@ -6,9 +6,8 @@ package executor
import (
"bytes"
"testing"
"strings"
"testing"
apimock "github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/common"
......@@ -21,6 +20,7 @@ import (
"github.com/33cn/chain33/types"
"github.com/33cn/plugin/plugin/dapp/paracross/testnode"
pt "github.com/33cn/plugin/plugin/dapp/paracross/types"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
......@@ -826,3 +826,47 @@ func TestValidParaCrossExec(t *testing.T) {
valid = types.IsParaExecName(string(exec))
assert.Equal(t, true, valid)
}
func TestVerifyBlsSign(t *testing.T) {
bls.Init(bls.BLS12_381)
status := &pt.ParacrossNodeStatus{}
status.Height = 0
status.Title = "user.p.para."
msg := types.Encode(status)
blsInfo := &pt.ParacrossCommitBlsInfo{}
commit := &pt.ParacrossCommitAction{Status: status, Bls: blsInfo}
PriKS := "0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b"
PriJR := "0x19c069234f9d3e61135fefbeb7791b149cdf6af536f26bebb310d4cd22c3fee4"
var priKeyKs bls.SecretKey
var prikeyJr bls.SecretKey
//set hex 支持有0x前缀的,unserial支持无前缀的
priKeyKs.SetHexString(PriKS)
prikeyJr.SetHexString(PriJR)
signKs := priKeyKs.SignByte(msg)
signJr := prikeyJr.SignByte(msg)
pubKs := priKeyKs.GetPublicKey()
pubJr := prikeyJr.GetPublicKey()
var si bls.Sign
si.Aggregate([]bls.Sign{*signKs, *signJr})
pubs := []bls.PublicKey{*pubKs, *pubJr}
ret := si.FastAggregateVerify(pubs, msg)
assert.Equal(t, true, ret)
blsInfo.Sign = append(blsInfo.Sign, si.Serialize()...)
blsInfo.AddrsMap = []byte{0x3}
PubKS := "a3d97d4186c80268fe6d3689dd574599e25df2dffdcff03f7d8ef64a3bd483241b7d0985958990de2d373d5604caf805"
PubJR := "81307df1fdde8f0e846ed1542c859c1e9daba2553e62e48db0877329c5c63fb86e70b9e2e83263da0eb7fcad275857f8"
pubKeys := []string{PubJR, PubKS}
err := verifyBlsSignPlus(pubKeys, commit)
assert.Equal(t, nil, err)
blsInfo.Sign = signKs.Serialize()
blsInfo.AddrsMap = []byte{0x3}
pubKeys = []string{PubKS}
err = verifyBlsSignPlus(pubKeys, commit)
assert.Equal(t, nil, err)
}
......@@ -93,7 +93,8 @@ func (c *Jrpc) GetParaCmtTxInfo(in *types.ReqNil, result *interface{}) error {
// GetParaBlsPubKey query para chain bls pubkey
func (c *channelClient) GetParaBlsPubKey(ctx context.Context, in *types.ReqString) (*pt.BlsPubKey, error) {
data, err := c.QueryConsensusFunc("para", "BlsPubKey", in)
ins := *in
data, err := c.QueryConsensusFunc("para", "BlsPubKey", &ins)
if err != nil {
return nil, err
}
......
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