Commit 47379749 authored by caopingcp's avatar caopingcp Committed by vipwzw

update vrf usage

parent be6fe665
......@@ -6,6 +6,7 @@ package ticket
import (
"bytes"
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
......@@ -20,13 +21,14 @@ import (
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/difficulty"
"github.com/33cn/chain33/common/log/log15"
vrf "github.com/33cn/chain33/common/vrf/p256"
vrf "github.com/33cn/chain33/common/vrf/secp256k1"
"github.com/33cn/chain33/queue"
drivers "github.com/33cn/chain33/system/consensus"
driver "github.com/33cn/chain33/system/dapp"
cty "github.com/33cn/chain33/system/dapp/coins/types"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/golang/protobuf/proto"
)
......@@ -377,11 +379,12 @@ func (client *Client) CheckBlock(parent *types.Block, current *types.BlockDetail
if input == nil {
input = miner.PrivHash
}
if err = vrfVerify(miner.PubKey, input, miner.VrfProof, miner.VrfHash); err != nil {
minerTx := current.Block.Txs[0]
if err = vrfVerify(minerTx.Signature.Pubkey, input, miner.VrfProof, miner.VrfHash); err != nil {
return err
}
} else {
if len(miner.PubKey) != 0 || len(miner.VrfHash) != 0 || len(miner.VrfProof) != 0 {
if len(miner.VrfHash) != 0 || len(miner.VrfProof) != 0 {
tlog.Error("block error: not yet add vrf")
return ty.ErrNoVrf
}
......@@ -390,11 +393,12 @@ func (client *Client) CheckBlock(parent *types.Block, current *types.BlockDetail
}
func vrfVerify(pub []byte, input []byte, proof []byte, hash []byte) error {
vrfPub, err := vrf.ParseVrfPubKey(pub)
pubKey, err := secp256k1.ParsePubKey(pub, secp256k1.S256())
if err != nil {
tlog.Error("vrfVerify", "err", err)
return ty.ErrVrfVerify
}
vrfPub := &vrf.PublicKey{PublicKey: (*ecdsa.PublicKey)(pubKey)}
vrfHash, err := vrfPub.ProofToHash(input, proof)
if err != nil {
tlog.Error("vrfVerify", "err", err)
......@@ -653,9 +657,9 @@ func (client *Client) addMinerTx(parent, block *types.Block, diff *big.Int, priv
if input == nil {
input = miner.PrivHash
}
vrfPriv, _, pubKey := vrf.GenVrfKey(priv)
privKey, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), priv.Bytes())
vrfPriv := &vrf.PrivateKey{PrivateKey: (*ecdsa.PrivateKey)(privKey)}
vrfHash, vrfProof := vrfPriv.Evaluate(input)
miner.PubKey = pubKey
miner.VrfHash = vrfHash[:]
miner.VrfProof = vrfProof
}
......
......@@ -5,17 +5,19 @@
package ticket
import (
"crypto/ecdsa"
"fmt"
"testing"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common/crypto"
vrf "github.com/33cn/chain33/common/vrf/p256"
vrf "github.com/33cn/chain33/common/vrf/secp256k1"
"github.com/33cn/chain33/queue"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/util"
"github.com/33cn/chain33/util/testnode"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
secp256k1 "github.com/btcsuite/btcd/btcec"
"github.com/stretchr/testify/assert"
_ "github.com/33cn/chain33/system"
......@@ -170,8 +172,11 @@ func Test_vrfVerify(t *testing.T) {
assert.NoError(t, err)
priv, err := c.GenKey()
assert.NoError(t, err)
pub := priv.PubKey().Bytes()
privKey, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), priv.Bytes())
vpriv := &vrf.PrivateKey{PrivateKey: (*ecdsa.PrivateKey)(privKey)}
vpriv, _, pubKey := vrf.GenVrfKey(priv)
m1 := []byte("data1")
m2 := []byte("data2")
m3 := []byte("data2")
......@@ -191,7 +196,7 @@ func Test_vrfVerify(t *testing.T) {
{m3, hash3, proof1, ty.ErrVrfVerify},
{m3, hash1, proof3, ty.ErrVrfVerify},
} {
err := vrfVerify(pubKey, tc.m, tc.proof, tc.hash[:])
err := vrfVerify(pub, tc.m, tc.proof, tc.hash[:])
if got, want := err, tc.err; got != want {
t.Errorf("vrfVerify(%s, %x): %v, want %v", tc.m, tc.proof, got, want)
}
......
......@@ -45,7 +45,6 @@ func (ticket *Ticket) GetRandNum(blockHash []byte, blockNum int64) (types.Messag
var ticketIds string
var privHashs []byte
var vrfHashs []byte
var vrfProofs []byte
for _, ticketAction := range txActions {
//tlog.Debug("GetRandNum", "modify", ticketAction.GetMiner().GetModify(), "bits", ticketAction.GetMiner().GetBits(), "ticketId", ticketAction.GetMiner().GetTicketId(), "PrivHash", ticketAction.GetMiner().GetPrivHash())
......@@ -54,12 +53,11 @@ func (ticket *Ticket) GetRandNum(blockHash []byte, blockNum int64) (types.Messag
ticketIds += ticketAction.GetMiner().GetTicketId()
privHashs = append(privHashs, ticketAction.GetMiner().GetPrivHash()...)
vrfHashs = append(vrfHashs, ticketAction.GetMiner().GetVrfHash()...)
vrfProofs = append(vrfProofs, ticketAction.GetMiner().GetVrfProof()...)
}
newmodify := fmt.Sprintf("%s:%s:%d:%s", string(modifies), ticketIds, bits, string(privHashs))
if len(vrfHashs) != 0 {
newmodify = newmodify + ":" + fmt.Sprintf("%x:%x", vrfHashs, vrfProofs)
newmodify = fmt.Sprintf("%s:%x", newmodify, vrfHashs)
}
modify := common.Sha256([]byte(newmodify))
......
......@@ -41,12 +41,10 @@ message TicketMiner {
bytes modify = 4;
//挖到区块时公开
bytes privHash = 5;
//VRF公钥
bytes pubKey = 6;
//VRF计算得到的hash
bytes vrfHash = 7;
bytes vrfHash = 6;
//VRF计算得到的proof
bytes vrfProof = 8;
bytes vrfProof = 7;
}
message TicketMinerOld {
......
This diff is collapsed.
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