diff options
author | MITSUNARI Shigeo <herumi@nifty.com> | 2019-01-09 13:06:14 +0800 |
---|---|---|
committer | MITSUNARI Shigeo <herumi@nifty.com> | 2019-01-09 13:20:27 +0800 |
commit | 0693d342069b100cf53867c39c7d4e837bcdc366 (patch) | |
tree | 3a6625cef6e49f49f91da9ba7b34306d94d9d036 /ffi/go | |
parent | ddd2989d3e681aed4c03c6223978d2d6d0cfe206 (diff) | |
download | dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar.gz dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar.bz2 dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar.lz dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar.xz dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.tar.zst dexon-bls-0693d342069b100cf53867c39c7d4e837bcdc366.zip |
add VerifyAggregateHashes for cgo
Diffstat (limited to 'ffi/go')
-rw-r--r-- | ffi/go/bls/bls.go | 19 | ||||
-rw-r--r-- | ffi/go/bls/bls_test.go | 50 |
2 files changed, 58 insertions, 11 deletions
diff --git a/ffi/go/bls/bls.go b/ffi/go/bls/bls.go index ff4f951..a5c657c 100644 --- a/ffi/go/bls/bls.go +++ b/ffi/go/bls/bls.go @@ -365,3 +365,22 @@ func (sign *Sign) VerifyHash(pub *PublicKey, hash []byte) bool { // #nosec return C.blsVerifyHash(sign.getPointer(), pub.getPointer(), unsafe.Pointer(&hash[0]), C.size_t(len(hash))) == 1 } + +func Min(x, y int) int { + if x < y { + return x + } + return y +} + +// VerifyAggregateHashes -- +func (sign *Sign) VerifyAggregateHashes(pubVec []PublicKey, hash [][]byte) bool { + hashByte := GetOpUnitSize() * 8 + n := len(hash) + h := make([]byte, n*hashByte) + for i := 0; i < n; i++ { + hn := len(hash[i]) + copy(h[i*hashByte:(i+1)*hashByte], hash[i][0:Min(hn, hashByte)]) + } + return C.blsVerifyAggregatedHashes(sign.getPointer(), pubVec[0].getPointer(), unsafe.Pointer(&h[0]), C.size_t(hashByte), C.size_t(n)) == 1 +} diff --git a/ffi/go/bls/bls_test.go b/ffi/go/bls/bls_test.go index 72f1838..84c8be2 100644 --- a/ffi/go/bls/bls_test.go +++ b/ffi/go/bls/bls_test.go @@ -4,6 +4,7 @@ import "testing" import "strconv" import "crypto/sha256" import "crypto/sha512" +import "fmt" var unitN = 0 @@ -370,7 +371,7 @@ func testPairing(t *testing.T) { } } -func testAggregate2(t *testing.T) { +func testAggregate(t *testing.T) { var sec SecretKey sec.SetByCSPRNG() pub := sec.GetPublicKey() @@ -394,20 +395,23 @@ func testAggregate2(t *testing.T) { } } +func Hash(buf []byte) []byte { + if GetOpUnitSize() == 4 { + d := sha256.Sum256([]byte(buf)) + return d[:] + } else { + // use SHA512 if bitSize > 256 + d := sha512.Sum512([]byte(buf)) + return d[:] + } +} + func testHash(t *testing.T) { var sec SecretKey sec.SetByCSPRNG() pub := sec.GetPublicKey() m := "abc" - var h []byte - if GetOpUnitSize() == 4 { - d := sha256.Sum256([]byte(m)) - h = d[:] - } else { - // use SHA512 if bitSize > 256 - d := sha512.Sum512([]byte(m)) - h = d[:] - } + h := Hash([]byte(m)) sig1 := sec.Sign(m) sig2 := sec.SignHash(h) if !sig1.IsEqual(sig2) { @@ -421,6 +425,29 @@ func testHash(t *testing.T) { } } +func testAggregateHashes(t *testing.T) { + n := 1000 + pubVec := make([]PublicKey, n) + sigVec := make([]*Sign, n) + h := make([][]byte, n) + for i := 0; i < n; i++ { + sec := new(SecretKey) + sec.SetByCSPRNG() + pubVec[i] = *sec.GetPublicKey() + m := fmt.Sprintf("abc-%d", i) + h[i] = Hash([]byte(m)) + sigVec[i] = sec.SignHash(h[i]) + } + // aggregate sig + sig := sigVec[0] + for i := 1; i < n; i++ { + sig.Add(sigVec[i]) + } + if !sig.VerifyAggregateHashes(pubVec, h) { + t.Errorf("sig.VerifyAggregateHashes") + } +} + func test(t *testing.T, c int) { err := Init(c) if err != nil { @@ -439,8 +466,9 @@ func test(t *testing.T, c int) { testDHKeyExchange(t) testSerializeToHexStr(t) testPairing(t) - testAggregate2(t) + testAggregate(t) testHash(t) + testAggregateHashes(t) } func TestMain(t *testing.T) { |