From 9a8121d049d3beb7f553bbc3e966e2c640c1cc76 Mon Sep 17 00:00:00 2001 From: Antoine Rondelet Date: Wed, 22 May 2019 12:23:57 +0100 Subject: [PATCH 1/3] Added checks for nil pointers in Marshal functions --- bn256.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bn256.go b/bn256.go index 3a07377..f48a7a5 100644 --- a/bn256.go +++ b/bn256.go @@ -105,6 +105,10 @@ func (e *G1) Marshal() []byte { // Each value is a 256-bit number. const numBytes = 256 / 8 + if e.p == nil { + e.p = &curvePoint{} + } + e.p.MakeAffine() ret := make([]byte, numBytes*2) if e.p.IsInfinity() { @@ -391,6 +395,10 @@ func (e *GT) Marshal() []byte { // Each value is a 256-bit number. const numBytes = 256 / 8 + if e.p == nil { + e.p = &gfP12{} + } + ret := make([]byte, numBytes*12) temp := &gfP{} From a8faec7df68ce8b4869ac5cf0c7bcfaab883fd38 Mon Sep 17 00:00:00 2001 From: armfazh Date: Wed, 22 May 2019 11:02:01 -0700 Subject: [PATCH 2/3] Set identities on marshaling functions for G1,G2,GT. - Adds tests for verifying the issue pointed at #8. --- bn256.go | 3 +++ bn256_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/bn256.go b/bn256.go index f48a7a5..d307e60 100644 --- a/bn256.go +++ b/bn256.go @@ -107,6 +107,7 @@ func (e *G1) Marshal() []byte { if e.p == nil { e.p = &curvePoint{} + e.p.SetInfinity() } e.p.MakeAffine() @@ -236,6 +237,7 @@ func (e *G2) Marshal() []byte { if e.p == nil { e.p = &twistPoint{} + e.p.SetInfinity() } e.p.MakeAffine() @@ -397,6 +399,7 @@ func (e *GT) Marshal() []byte { if e.p == nil { e.p = &gfP12{} + e.p.SetOne() } ret := make([]byte, numBytes*12) diff --git a/bn256_test.go b/bn256_test.go index f2a6223..ad168fa 100644 --- a/bn256_test.go +++ b/bn256_test.go @@ -41,6 +41,18 @@ func TestG1Marshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } + + Ga = new(G1) + ma = Ga.Marshal() + + one := &curvePoint{} + one.SetInfinity() + Gb = &G1{p: one} + mb = Gb.Marshal() + + if !bytes.Equal(ma, mb) { + t.Fatal("bytes are different") + } } func TestG2(t *testing.T) { @@ -76,6 +88,18 @@ func TestG2Marshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } + + Ga = new(G2) + ma = Ga.Marshal() + + one := &twistPoint{} + one.SetInfinity() + Gb = &G2{p: one} + mb = Gb.Marshal() + + if !bytes.Equal(ma, mb) { + t.Fatal("bytes are different") + } } func TestGT(t *testing.T) { @@ -114,6 +138,18 @@ func TestGTMarshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } + + Ga = new(GT) + ma = Ga.Marshal() + + one := &gfP12{} + one.SetOne() + Gb = >{p: one} + mb = Gb.Marshal() + + if !bytes.Equal(ma, mb) { + t.Fatal("bytes are different") + } } func TestBilinearity(t *testing.T) { From 0a352d728d8f703090a99402e7ae750c3cabc2f6 Mon Sep 17 00:00:00 2001 From: armfazh Date: Thu, 23 May 2019 10:22:55 -0700 Subject: [PATCH 3/3] Removing extra tests and SetInfinity calls. --- bn256.go | 2 -- bn256_test.go | 36 ------------------------------------ 2 files changed, 38 deletions(-) diff --git a/bn256.go b/bn256.go index d307e60..9d4cc39 100644 --- a/bn256.go +++ b/bn256.go @@ -107,7 +107,6 @@ func (e *G1) Marshal() []byte { if e.p == nil { e.p = &curvePoint{} - e.p.SetInfinity() } e.p.MakeAffine() @@ -237,7 +236,6 @@ func (e *G2) Marshal() []byte { if e.p == nil { e.p = &twistPoint{} - e.p.SetInfinity() } e.p.MakeAffine() diff --git a/bn256_test.go b/bn256_test.go index ad168fa..f2a6223 100644 --- a/bn256_test.go +++ b/bn256_test.go @@ -41,18 +41,6 @@ func TestG1Marshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } - - Ga = new(G1) - ma = Ga.Marshal() - - one := &curvePoint{} - one.SetInfinity() - Gb = &G1{p: one} - mb = Gb.Marshal() - - if !bytes.Equal(ma, mb) { - t.Fatal("bytes are different") - } } func TestG2(t *testing.T) { @@ -88,18 +76,6 @@ func TestG2Marshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } - - Ga = new(G2) - ma = Ga.Marshal() - - one := &twistPoint{} - one.SetInfinity() - Gb = &G2{p: one} - mb = Gb.Marshal() - - if !bytes.Equal(ma, mb) { - t.Fatal("bytes are different") - } } func TestGT(t *testing.T) { @@ -138,18 +114,6 @@ func TestGTMarshal(t *testing.T) { if !bytes.Equal(ma, mb) { t.Fatal("bytes are different") } - - Ga = new(GT) - ma = Ga.Marshal() - - one := &gfP12{} - one.SetOne() - Gb = >{p: one} - mb = Gb.Marshal() - - if !bytes.Equal(ma, mb) { - t.Fatal("bytes are different") - } } func TestBilinearity(t *testing.T) {