mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 21:38:05 -05:00
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:
@@ -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.
|
||||
|
||||
@@ -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())
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user