Commit a022cf95 authored by suyanlong's avatar suyanlong

update bls package for bls-eth-go-binary

parent 22a948ac
......@@ -81,6 +81,7 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/herumi/bls-eth-go-binary v0.0.0-20210917013441-d37c07cfda4e // indirect
github.com/huin/goupnp v1.0.0 // indirect
github.com/hyperledger/fabric v2.0.1+incompatible // indirect
github.com/hyperledger/fabric-amcl v0.0.0-20200424173818-327c9e2cf77a // indirect
......
......@@ -347,6 +347,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/herumi/bls-eth-go-binary v0.0.0-20210917013441-d37c07cfda4e h1:wCMygKUQhmcQAjlk2Gquzq6dLmyMv2kF+llRspoRgrk=
github.com/herumi/bls-eth-go-binary v0.0.0-20210917013441-d37c07cfda4e/go.mod h1:luAnRm3OsMQeokhGzpYmc0ZKwawY7o87PUEP11Z7r7U=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/go-assert v1.1.5 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c=
github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U=
......@@ -676,6 +678,9 @@ github.com/meshplus/bitxhub-kit v1.0.0/go.mod h1:7cWyhXWZfrQ3+EaxkRoXfuiG3Y5R9DX
github.com/meshplus/bitxhub-kit v1.1.1/go.mod h1:r4l4iqn0RPJreb/OmoYKfjCjQJrXpZX++6Qc31VG/1k=
github.com/meshplus/bitxhub-kit v1.2.1-0.20210524063043-9afae78ac098 h1:/E1isgGcUXQhTlKgVZ7GULTqw+0mj/WZ1a+Yd5N7Bmg=
github.com/meshplus/bitxhub-kit v1.2.1-0.20210524063043-9afae78ac098/go.mod h1:wrEdhHp1tktzdwcWb4bOxYsVc+KkcrYL18IYWYeumPQ=
github.com/meshplus/bitxhub-model v1.0.0-rc3/go.mod h1:ZCctQIYTlE3vJ8Lhkrgs9bWwNA+Dw4JzojOSIzLVU6E=
github.com/meshplus/bitxhub-model v1.1.1/go.mod h1:lUl9vPZXM9tP+B0ABRW/2eOW/6KCmjFTdoiTj5Vut/A=
github.com/meshplus/bitxhub-model v1.2.1-0.20210524063354-5d48e2fee178/go.mod h1:vwJ+sHPUyA2JELmUUDBol+7zA+7GcqutxzqXjsN0QLA=
github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a h1:c4ESPDa60Jd4zfzZIGGTyzhfaVM3vKN+xV2G9BwIDGQ=
github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a/go.mod h1:vAldSRfDe2Qo7exsSTbchVmZWXPY7fhWQrRw18QJHho=
github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54 h1:5Ip5AB7SxxQHg5SRtf2cCOI2wy1p75MQB12soPtPyf8=
......
package bls
import (
"crypto/rand"
blst "github.com/supranational/blst/bindings/go"
"github.com/herumi/bls-eth-go-binary/bls"
)
// Names in this file must be unique to support min-sig so we can't use 'dst'
// here.
var dstMinSig = []byte("BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_")
//minimal-signature-size operations:
type PublicKeyMinSig = blst.P2Affine
type SignatureMinSig = blst.P1Affine
type AggregateSignatureMinSig = blst.P1Aggregate
type AggregatePublicKeyMinSig = blst.P2Aggregate
type Message = blst.Message
type Sign = bls.Sign
type SecretKey = blst.SecretKey
type SecretKey = bls.SecretKey
type Scalar = blst.Scalar
type PublicKey = bls.PublicKey
func GenRandomKeyMinSig() *SecretKey {
// Generate 32 bytes of randomness
var ikm [32]byte
_, err := rand.Read(ikm[:])
type PublicKeys = bls.PublicKeys
if err != nil {
return nil
}
return blst.KeyGen(ikm[:])
}
type ID = bls.ID
package bls
import (
"fmt"
mrand "math/rand"
"encoding/hex"
"testing"
blst "github.com/supranational/blst/bindings/go"
"github.com/herumi/bls-eth-go-binary/bls"
)
func TestSignMultipleVerifyAggregateMinSig(t *testing.T) {
msgCount := 5
for size := 1; size < 20; size++ {
msgs := make([]Message, 0)
sks := make([]*SecretKey, 0)
pks := make([]*PublicKeyMinSig, 0)
// Generate messages
for i := 0; i < msgCount; i++ {
msg := Message(fmt.Sprintf("blst is a blast!! %d %d", i, size))
msgs = append(msgs, msg)
func TestGetSafePublicKey(t *testing.T) {
if bls.Init(bls.BLS12_381) != nil {
t.Fatalf("Init")
}
// Generate keypairs
for i := 0; i < size; i++ {
priv := GenRandomKeyMinSig()
sks = append(sks, priv)
pks = append(pks, new(PublicKeyMinSig).From(priv))
var sec SecretKey
pub, err := sec.GetSafePublicKey()
if pub != nil || err == nil {
t.Fatalf("sec must be zero")
}
// All signers sign each message
aggSigs := make([]*SignatureMinSig, 0)
aggPks := make([]*PublicKeyMinSig, 0)
for i := 0; i < msgCount; i++ {
sigsToAgg := make([]*SignatureMinSig, 0)
pksToAgg := make([]*PublicKeyMinSig, 0)
for j := 0; j < size; j++ {
sigsToAgg = append(sigsToAgg,
new(SignatureMinSig).Sign(sks[j], msgs[i],
dstMinSig))
pksToAgg = append(pksToAgg, pks[j])
sec.SetByCSPRNG()
pub, err = sec.GetSafePublicKey()
if pub == nil || err != nil {
t.Fatalf("sec must be non-zero")
}
}
agSig := new(AggregateSignatureMinSig)
if !agSig.Aggregate(sigsToAgg, true) {
t.Errorf("failed to aggregate")
}
afSig := agSig.ToAffine()
agPk := new(AggregatePublicKeyMinSig)
agPk.Aggregate(pksToAgg, false)
afPk := agPk.ToAffine()
aggSigs = append(aggSigs, afSig)
aggPks = append(aggPks, afPk)
func TestEthDraft07(t *testing.T) {
secHex := "0000000000000000000000000000000000000000000000000000000000000001"
msgHex := "61736466"
sigHex := "b45a264e0d6f8614c4640ea97bae13effd3c74c4e200e3b1596d6830debc952602a7d210eca122dc4f596fa01d7f6299106933abd29477606f64588595e18349afe22ecf2aeeeb63753e88a42ef85b24140847e05620a28422f8c30f1d33b9aa"
ethSignOneTest(t, secHex, msgHex, sigHex)
}
// Verify aggregated signature and pk
if !afSig.Verify(false, afPk, false, msgs[i], dstMinSig) {
t.Errorf("failed to verify single aggregate size %d", size)
func ethSignOneTest(t *testing.T, secHex string, msgHex string, sigHex string) {
var sec SecretKey
if sec.DeserializeHexStr(secHex) != nil {
t.Fatalf("bad sec")
}
var sig Sign
if sig.DeserializeHexStr(sigHex) != nil {
t.Logf("bad sig %v\n", sigHex)
return
}
randFn := func(s *Scalar) {
var rbytes [blst.BLST_SCALAR_BYTES]byte
mrand.Read(rbytes[:])
s.FromBEndian(rbytes[:])
pub := sec.GetPublicKey()
msg, _ := hex.DecodeString(msgHex)
sig = *sec.SignByte(msg)
if !sig.VerifyByte(pub, msg) {
t.Fatalf("bad verify %v %v", secHex, msgHex)
}
s := sig.SerializeToHexStr()
if s != sigHex {
t.Fatalf("bad sign\nL=%v\nR=%v\nsec=%v\nmsg=%v", s, sigHex, secHex, msgHex)
}
}
// Verify
randBits := 64
if !new(SignatureMinSig).MultipleAggregateVerify(aggSigs, true,
aggPks, false,
msgs, dstMinSig,
randFn, randBits) {
t.Errorf("failed to verify multiple aggregate size %d", size)
func blsAggregateVerifyNoCheckTest(t *testing.T) {
nTbl := []int{1, 2, 15, 16, 17, 50}
for i := 0; i < len(nTbl); i++ {
blsAggregateVerifyNoCheckTestOne(t, nTbl[i])
}
}
// Negative test
if new(SignatureMinSig).MultipleAggregateVerify(aggSigs, true,
aggPks, false,
msgs, dstMinSig[1:],
randFn, randBits) {
t.Errorf("failed to not verify multiple aggregate size %d", size)
func blsAggregateVerifyNoCheckTestOne(t *testing.T, n int) {
t.Logf("blsAggregateVerifyNoCheckTestOne %v\n", n)
pubs, sigs, msgs := makeMultiSig(n)
if !bls.AreAllMsgDifferent(msgs) {
t.Fatalf("bad msgs")
}
var aggSig Sign
aggSig.Aggregate(sigs)
if !aggSig.AggregateVerifyNoCheck(pubs, msgs) {
t.Fatalf("bad AggregateVerifyNoCheck 1")
}
msgs[1] = 1
if aggSig.AggregateVerifyNoCheck(pubs, msgs) {
t.Fatalf("bad AggregateVerifyNoCheck 2")
}
}
func makeMultiSig(n int) (pubs []PublicKey, sigs []Sign, msgs []byte) {
msgSize := 32
pubs = make([]PublicKey, n)
sigs = make([]Sign, n)
msgs = make([]byte, n*msgSize)
for i := 0; i < n; i++ {
var sec SecretKey
sec.SetByCSPRNG()
pubs[i] = *sec.GetPublicKey()
msgs[msgSize*i] = byte(i)
sigs[i] = *sec.SignByte(msgs[msgSize*i : msgSize*(i+1)])
}
return pubs, sigs, msgs
}
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