Commit a022cf95 authored by suyanlong's avatar suyanlong

update bls package for bls-eth-go-binary

parent 22a948ac
...@@ -81,6 +81,7 @@ require ( ...@@ -81,6 +81,7 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // 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/huin/goupnp v1.0.0 // indirect
github.com/hyperledger/fabric v2.0.1+incompatible // indirect github.com/hyperledger/fabric v2.0.1+incompatible // indirect
github.com/hyperledger/fabric-amcl v0.0.0-20200424173818-327c9e2cf77a // 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 ...@@ -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/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 h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= 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/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 h1:fjemmA7sSfYHJD7CUqs9qTwwfdNAx7/j2/ZlHXzNB3c=
github.com/huandu/go-assert v1.1.5/go.mod h1:yOLvuqZwmcHIC5rIzrBhT7D3Q9c3GFnd0JrPVhn/06U= 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 ...@@ -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.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 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-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 h1:c4ESPDa60Jd4zfzZIGGTyzhfaVM3vKN+xV2G9BwIDGQ=
github.com/meshplus/bitxid v0.0.0-20210412025850-e0eaf0f9063a/go.mod h1:vAldSRfDe2Qo7exsSTbchVmZWXPY7fhWQrRw18QJHho= 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= github.com/meshplus/go-lightp2p v0.0.0-20200817105923-6b3aee40fa54 h1:5Ip5AB7SxxQHg5SRtf2cCOI2wy1p75MQB12soPtPyf8=
......
package bls package bls
import ( import (
"crypto/rand" "github.com/herumi/bls-eth-go-binary/bls"
blst "github.com/supranational/blst/bindings/go"
) )
// Names in this file must be unique to support min-sig so we can't use 'dst' type Sign = bls.Sign
// 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 SecretKey = blst.SecretKey type SecretKey = bls.SecretKey
type Scalar = blst.Scalar type PublicKey = bls.PublicKey
func GenRandomKeyMinSig() *SecretKey { type PublicKeys = bls.PublicKeys
// Generate 32 bytes of randomness
var ikm [32]byte
_, err := rand.Read(ikm[:])
if err != nil { type ID = bls.ID
return nil
}
return blst.KeyGen(ikm[:])
}
package bls package bls
import ( import (
"fmt" "encoding/hex"
mrand "math/rand"
"testing" "testing"
blst "github.com/supranational/blst/bindings/go" "github.com/herumi/bls-eth-go-binary/bls"
) )
func TestSignMultipleVerifyAggregateMinSig(t *testing.T) { func TestGetSafePublicKey(t *testing.T) {
msgCount := 5 if bls.Init(bls.BLS12_381) != nil {
for size := 1; size < 20; size++ { t.Fatalf("Init")
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)
} }
// Generate keypairs var sec SecretKey
for i := 0; i < size; i++ { pub, err := sec.GetSafePublicKey()
priv := GenRandomKeyMinSig() if pub != nil || err == nil {
sks = append(sks, priv) t.Fatalf("sec must be zero")
pks = append(pks, new(PublicKeyMinSig).From(priv))
} }
sec.SetByCSPRNG()
// All signers sign each message pub, err = sec.GetSafePublicKey()
aggSigs := make([]*SignatureMinSig, 0) if pub == nil || err != nil {
aggPks := make([]*PublicKeyMinSig, 0) t.Fatalf("sec must be non-zero")
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])
} }
}
agSig := new(AggregateSignatureMinSig) func TestEthDraft07(t *testing.T) {
if !agSig.Aggregate(sigsToAgg, true) { secHex := "0000000000000000000000000000000000000000000000000000000000000001"
t.Errorf("failed to aggregate") msgHex := "61736466"
} sigHex := "b45a264e0d6f8614c4640ea97bae13effd3c74c4e200e3b1596d6830debc952602a7d210eca122dc4f596fa01d7f6299106933abd29477606f64588595e18349afe22ecf2aeeeb63753e88a42ef85b24140847e05620a28422f8c30f1d33b9aa"
afSig := agSig.ToAffine() ethSignOneTest(t, secHex, msgHex, sigHex)
agPk := new(AggregatePublicKeyMinSig) }
agPk.Aggregate(pksToAgg, false)
afPk := agPk.ToAffine()
aggSigs = append(aggSigs, afSig)
aggPks = append(aggPks, afPk)
// Verify aggregated signature and pk func ethSignOneTest(t *testing.T, secHex string, msgHex string, sigHex string) {
if !afSig.Verify(false, afPk, false, msgs[i], dstMinSig) { var sec SecretKey
t.Errorf("failed to verify single aggregate size %d", size) 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
} }
pub := sec.GetPublicKey()
randFn := func(s *Scalar) { msg, _ := hex.DecodeString(msgHex)
var rbytes [blst.BLST_SCALAR_BYTES]byte sig = *sec.SignByte(msg)
mrand.Read(rbytes[:]) if !sig.VerifyByte(pub, msg) {
s.FromBEndian(rbytes[:]) 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 func blsAggregateVerifyNoCheckTest(t *testing.T) {
randBits := 64 nTbl := []int{1, 2, 15, 16, 17, 50}
if !new(SignatureMinSig).MultipleAggregateVerify(aggSigs, true, for i := 0; i < len(nTbl); i++ {
aggPks, false, blsAggregateVerifyNoCheckTestOne(t, nTbl[i])
msgs, dstMinSig,
randFn, randBits) {
t.Errorf("failed to verify multiple aggregate size %d", size)
} }
}
// Negative test func blsAggregateVerifyNoCheckTestOne(t *testing.T, n int) {
if new(SignatureMinSig).MultipleAggregateVerify(aggSigs, true, t.Logf("blsAggregateVerifyNoCheckTestOne %v\n", n)
aggPks, false, pubs, sigs, msgs := makeMultiSig(n)
msgs, dstMinSig[1:], if !bls.AreAllMsgDifferent(msgs) {
randFn, randBits) { t.Fatalf("bad msgs")
t.Errorf("failed to not verify multiple aggregate size %d", size) }
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