Update BLS with @protolambda's improvements (#3152)

* Add @protolambda's fork until https://github.com/phoreproject/bls/pull/11

* update workspace
This commit is contained in:
Preston Van Loon
2019-08-07 22:54:33 -04:00
committed by GitHub
parent 4e041c852b
commit ccc7d8d7b7
5 changed files with 29 additions and 6 deletions

View File

@@ -4,6 +4,7 @@
package bls
import (
"encoding/binary"
"fmt"
"io"
@@ -72,7 +73,9 @@ func (s *SecretKey) PublicKey() *PublicKey {
// Sign a message using a secret key - in a beacon/validator client,
func (s *SecretKey) Sign(msg []byte, domain uint64) *Signature {
sig := g1.SignWithDomain(bytesutil.ToBytes32(msg), s.val, domain)
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, domain)
sig := g1.SignWithDomain(bytesutil.ToBytes32(msg), s.val, bytesutil.ToBytes8(b))
return &Signature{val: sig}
}
@@ -97,7 +100,9 @@ func (p *PublicKey) Aggregate(p2 *PublicKey) *PublicKey {
// Verify a bls signature given a public key, a message, and a domain.
func (s *Signature) Verify(msg []byte, pub *PublicKey, domain uint64) bool {
return g1.VerifyWithDomain(bytesutil.ToBytes32(msg), pub.val, s.val, domain)
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, domain)
return g1.VerifyWithDomain(bytesutil.ToBytes32(msg), pub.val, s.val, bytesutil.ToBytes8(b))
}
// VerifyAggregate verifies each public key against a message.
@@ -111,7 +116,9 @@ func (s *Signature) VerifyAggregate(pubKeys []*PublicKey, msg []byte, domain uin
for _, v := range pubKeys {
keys = append(keys, v.val)
}
return s.val.VerifyAggregateCommonWithDomain(keys, bytesutil.ToBytes32(msg), domain)
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, domain)
return s.val.VerifyAggregateCommonWithDomain(keys, bytesutil.ToBytes32(msg), bytesutil.ToBytes8(b))
}
// Marshal a signature into a byte slice.

View File

@@ -2,6 +2,7 @@ package spectest
import (
"bytes"
"encoding/binary"
"fmt"
"testing"
@@ -25,10 +26,12 @@ func TestG2CompressedHash(t *testing.T) {
for i, tt := range test.TestCases {
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, tt.Input.Domain)
projective := bls.HashG2WithDomain(
bytesutil.ToBytes32(tt.Input.Message),
tt.Input.Domain,
bytesutil.ToBytes8(b),
)
hash := bls.CompressG2(projective.ToAffine())

View File

@@ -2,6 +2,7 @@ package spectest
import (
"bytes"
"encoding/binary"
"fmt"
"testing"
@@ -26,9 +27,12 @@ func TestG2UncompressedHash(t *testing.T) {
for i, tt := range test.TestCases {
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, tt.Input.Domain)
projective := bls.HashG2WithDomain(
bytesutil.ToBytes32(tt.Input.Message),
tt.Input.Domain,
bytesutil.ToBytes8(b),
)
hash := projective.ToAffine().SerializeBytes()

View File

@@ -86,6 +86,15 @@ func LowerThan(x []byte, y []byte) bool {
return true
}
// ToBytes8 is a convenience method for converting a byte slice to a fix
// sized 8 byte array. This method will truncate the input if it is larger
// than 8 bytes.
func ToBytes8(x []byte) [8]byte {
var y [8]byte
copy(y[:], x)
return y
}
// ToBytes32 is a convenience method for converting a byte slice to a fix
// sized 32 byte array. This method will truncate the input if it is larger
// than 32 bytes.