Commit 22a948ac authored by suyanlong's avatar suyanlong

Add blst Signature

parent 66d7c81d
Pipeline #8191 failed with stages
...@@ -170,6 +170,7 @@ require ( ...@@ -170,6 +170,7 @@ require (
github.com/spf13/jwalterweatherman v1.0.0 // indirect github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect
github.com/supranational/blst v0.3.6 // indirect
github.com/sykesm/zap-logfmt v0.0.3 // indirect github.com/sykesm/zap-logfmt v0.0.3 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 // indirect github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 // indirect
github.com/tidwall/match v1.0.3 // indirect github.com/tidwall/match v1.0.3 // indirect
......
...@@ -932,6 +932,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc ...@@ -932,6 +932,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/supranational/blst v0.3.6 h1:a24cPQB0qYpXPMZx177aapCM50/YrTMt/TKAUa7TzdM=
github.com/supranational/blst v0.3.6/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/sykesm/zap-logfmt v0.0.3 h1:3Wrhf7+I9JEUD8B6KPtDAr9j2jrS0/EPLy7GCE1t/+U= github.com/sykesm/zap-logfmt v0.0.3 h1:3Wrhf7+I9JEUD8B6KPtDAr9j2jrS0/EPLy7GCE1t/+U=
github.com/sykesm/zap-logfmt v0.0.3/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA= github.com/sykesm/zap-logfmt v0.0.3/go.mod h1:AuBd9xQjAe3URrWT1BBDk2v2onAZHkZkWRMiYZXiZWA=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
......
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
crypto "github.com/meshplus/bitxhub-kit/crypto" crypto "github.com/meshplus/bitxhub-kit/crypto"
types "github.com/meshplus/bitxhub-kit/types" types "github.com/meshplus/bitxhub-kit/types"
rpcx "gitlab.33.cn/link33/sidecar/hub/client" rpcx "gitlab.33.cn/link33/sidecar/hub/client"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
peer "github.com/libp2p/go-libp2p-core/peer" peer "github.com/libp2p/go-libp2p-core/peer"
peermgr "gitlab.33.cn/link33/sidecar/internal/peermgr" peermgr "gitlab.33.cn/link33/sidecar/internal/peermgr"
port "gitlab.33.cn/link33/sidecar/internal/port" port "gitlab.33.cn/link33/sidecar/internal/port"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
......
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
port "gitlab.33.cn/link33/sidecar/internal/port" port "gitlab.33.cn/link33/sidecar/internal/port"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
appchain_mgr "github.com/meshplus/bitxhub-core/appchain-mgr" appchain_mgr "github.com/meshplus/bitxhub-core/appchain-mgr"
syncer "gitlab.33.cn/link33/sidecar/internal/syncer" syncer "gitlab.33.cn/link33/sidecar/internal/syncer"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
) )
......
package bls
import (
"crypto/rand"
blst "github.com/supranational/blst/bindings/go"
)
// 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 SecretKey = blst.SecretKey
type Scalar = blst.Scalar
func GenRandomKeyMinSig() *SecretKey {
// Generate 32 bytes of randomness
var ikm [32]byte
_, err := rand.Read(ikm[:])
if err != nil {
return nil
}
return blst.KeyGen(ikm[:])
}
package bls
import (
"fmt"
mrand "math/rand"
"testing"
blst "github.com/supranational/blst/bindings/go"
)
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)
}
// Generate keypairs
for i := 0; i < size; i++ {
priv := GenRandomKeyMinSig()
sks = append(sks, priv)
pks = append(pks, new(PublicKeyMinSig).From(priv))
}
// 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])
}
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)
// Verify aggregated signature and pk
if !afSig.Verify(false, afPk, false, msgs[i], dstMinSig) {
t.Errorf("failed to verify single aggregate size %d", size)
}
}
randFn := func(s *Scalar) {
var rbytes [blst.BLST_SCALAR_BYTES]byte
mrand.Read(rbytes[:])
s.FromBEndian(rbytes[:])
}
// 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)
}
// 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)
}
}
}
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
reflect "reflect" reflect "reflect"
gomock "github.com/golang/mock/gomock" gomock "github.com/golang/mock/gomock"
pb "gitlab.33.cn/link33/sidecar/model/pb" pb "gitlab.33.cn/link33/sidecar/model/pb"
plugins "gitlab.33.cn/link33/sidecar/pkg/plugins" plugins "gitlab.33.cn/link33/sidecar/pkg/plugins"
) )
......
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