mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-07 22:54:17 -05:00
Add Functions for Compressed and Uncompressed HashG2 With Domain (#2833)
* add tests * gaz * lint
This commit is contained in:
@@ -7,6 +7,7 @@ go_library(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"@com_github_phoreproject_bls//:go_default_library",
|
||||
"@com_github_phoreproject_bls//g1pubs:go_default_library",
|
||||
],
|
||||
)
|
||||
@@ -15,5 +16,8 @@ go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["bls_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = ["//shared/bytesutil:go_default_library"],
|
||||
deps = [
|
||||
"//shared/bytesutil:go_default_library",
|
||||
"@com_github_phoreproject_bls//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/phoreproject/bls"
|
||||
g1 "github.com/phoreproject/bls/g1pubs"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
)
|
||||
@@ -125,6 +126,18 @@ func AggregateSignatures(sigs []*Signature) *Signature {
|
||||
return &Signature{val: g1.AggregateSignatures(ss)}
|
||||
}
|
||||
|
||||
// HashG2WithDomainCompressed returns the compressed hash.
|
||||
func HashG2WithDomainCompressed(msg [32]byte, domain uint64) [96]byte {
|
||||
projective := bls.HashG2WithDomain(msg, domain)
|
||||
return bls.CompressG2(projective.ToAffine())
|
||||
}
|
||||
|
||||
// HashG2WithDomainUncompressed returns the uncompressed hash.
|
||||
func HashG2WithDomainUncompressed(msg [32]byte, domain uint64) [192]byte {
|
||||
projective := bls.HashG2WithDomain(msg, domain)
|
||||
return projective.ToAffine().SerializeBytes()
|
||||
}
|
||||
|
||||
// Domain returns the bls domain given by the domain type and the operation 4 byte fork version.
|
||||
//
|
||||
// Spec pseudocode definition:
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/rand"
|
||||
"testing"
|
||||
|
||||
bls2 "github.com/phoreproject/bls"
|
||||
"github.com/prysmaticlabs/prysm/shared/bls"
|
||||
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
||||
)
|
||||
@@ -51,3 +52,20 @@ func TestVerifyAggregate(t *testing.T) {
|
||||
t.Error("Signature did not verify")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashG2WithDomain(t *testing.T) {
|
||||
var msg [32]byte
|
||||
copy(msg[:], []byte{'H', 'E', 'L', 'L', 'O'})
|
||||
domain := uint64(10)
|
||||
compHash := bls.HashG2WithDomainCompressed(msg, domain)
|
||||
unCompHash := bls.HashG2WithDomainUncompressed(msg, domain)
|
||||
g2Affine, err := bls2.DecompressG2(compHash)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if unCompHash != g2Affine.SerializeBytes() {
|
||||
t.Errorf("Uncompressed and Compressed Hash do not point to the same affine point. Expected %#x but got %#x",
|
||||
unCompHash, g2Affine.SerializeBytes())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user