Browse Source

bls signatures aggregation verification working

master
arnaucube 5 years ago
parent
commit
b14d5447a8
3 changed files with 17 additions and 18 deletions
  1. +13
    -11
      bls/bls.go
  2. +4
    -6
      bls/bls_test.go
  3. +0
    -1
      go.mod

+ 13
- 11
bls/bls.go

@ -3,7 +3,6 @@ package bls
import (
"crypto/rand"
"crypto/sha256"
"fmt"
"math/big"
"github.com/arnaucube/go-snark/bn128"
@ -81,34 +80,37 @@ func (bls BLS) Verify(m []byte, sig [3][2]*big.Int, pubK [3]*big.Int) bool {
return bls.Bn.Fq12.Equal(p1, p2)
}
// AggregateSignatures
// s = s0 + s1 + s2 ...
func (bls BLS) AggregateSignatures(signatures ...[3][2]*big.Int) [3][2]*big.Int {
aggr := signatures[0]
for _, sig := range signatures {
aggr = bls.Bn.G2.Add(aggr, sig)
for i := 1; i < len(signatures); i++ {
aggr = bls.Bn.G2.Add(aggr, signatures[i])
}
return aggr
}
func (bls BLS) VerifyAggregatedSignatures(aggrsig [3][2]*big.Int, pubKArray [][3]*big.Int, mArray [][]byte) bool {
if len(pubKArray) != len(mArray) {
fmt.Println("pubK array and msg array not with the same number of elements")
return false
}
// VerifyAggregatedSignatures
// ê(G,S) == ê(P, H(m))
// ê(G, s0+s1+s2...) == ê(p0, H(m)) x ê(p1, H(m)) x ê(p2, H(m)) ...
func (bls BLS) VerifyAggregatedSignatures(aggrsig [3][2]*big.Int, pubKArray [][3]*big.Int, m []byte) bool {
pairingGS, err := bls.Bn.Pairing(bls.Bn.G1.G, aggrsig)
if err != nil {
return false
}
pairingsMul, err := bls.Bn.Pairing(pubKArray[0], bls.Hash(mArray[0]))
pairingsMul, err := bls.Bn.Pairing(pubKArray[0], bls.Hash(m))
if err != nil {
return false
}
for i := 1; i < len(pubKArray); i++ {
e, err := bls.Bn.Pairing(pubKArray[i], bls.Hash(mArray[i]))
e, err := bls.Bn.Pairing(pubKArray[i], bls.Hash(m))
if err != nil {
return false
}
pairingsMul = bls.Bn.Fq12.Mul(pairingsMul, e)
}
if !bls.Bn.Fq12.Equal(pairingGS, pairingsMul) {
return false
}

+ 4
- 6
bls/bls_test.go

@ -28,19 +28,17 @@ func TestBls(t *testing.T) {
// signature aggregation
keys1, err := bls.NewKeys()
assert.Nil(t, err)
m1 := []byte("message1")
sig1 := bls.Sign(keys1.PrivK, m1)
sig1 := bls.Sign(keys1.PrivK, m0)
assert.True(t, bls.Verify(m0, sig1, keys1.PubK))
keys2, err := bls.NewKeys()
assert.Nil(t, err)
m2 := []byte("message2")
sig2 := bls.Sign(keys2.PrivK, m2)
sig2 := bls.Sign(keys2.PrivK, m0)
aggr := bls.AggregateSignatures(sig0, sig1, sig2)
pubKArray := [][3]*big.Int{keys0.PubK, keys1.PubK, keys2.PubK}
mArray := [][]byte{m0, m1, m2}
verified = bls.VerifyAggregatedSignatures(aggr, pubKArray, mArray)
verified = bls.VerifyAggregatedSignatures(aggr, pubKArray, m0)
fmt.Println("signature aggregation verified:", verified)
assert.True(t, verified)

+ 0
- 1
go.mod

@ -1,7 +1,6 @@
module github.com/arnaucube/cryptofun
require (
github.com/arnaucube/bn128 v0.0.0-20181124004642-3bb6b68ddbe4
github.com/arnaucube/go-snark v0.0.0-20181207210027-19f7216d0e3d
github.com/stretchr/testify v1.2.2
)

Loading…
Cancel
Save