From 7fb2ebf3f12f043d7bd12f43500dc2bd133df573 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Fri, 21 Jun 2019 08:43:03 +0530 Subject: [PATCH] Add Functions for Compressed and Uncompressed HashG2 With Domain (#2833) * add tests * gaz * lint --- shared/bls/BUILD.bazel | 6 +++++- shared/bls/bls.go | 13 +++++++++++++ shared/bls/bls_test.go | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/shared/bls/BUILD.bazel b/shared/bls/BUILD.bazel index 686f227230..4006f09086 100644 --- a/shared/bls/BUILD.bazel +++ b/shared/bls/BUILD.bazel @@ -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", + ], ) diff --git a/shared/bls/bls.go b/shared/bls/bls.go index 859f80c1e5..da6541b512 100644 --- a/shared/bls/bls.go +++ b/shared/bls/bls.go @@ -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: diff --git a/shared/bls/bls_test.go b/shared/bls/bls_test.go index ce5fb1e4c0..08185f3022 100644 --- a/shared/bls/bls_test.go +++ b/shared/bls/bls_test.go @@ -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()) + } +}