Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Adding testing for deserialization of G1 and G2 points #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions g1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ package bls12381
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

var (
deserializationG1Tests = filepath.Join(testDir, "deserialization_G1/*")
)

func (g *G1) one() *PointG1 {
Expand Down Expand Up @@ -358,6 +368,41 @@ func TestZKCryptoVectorsG1CompressedValid(t *testing.T) {
}
}

func TestZKCryptoVectorsG1Compressed(t *testing.T) {
type Test struct {
Input struct {
PubKeyHexStr string `yaml:"pubkey"`
}
IsValidPredicate *bool `yaml:"output"`
}
tests, err := filepath.Glob(deserializationG1Tests)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.IsValidPredicate != nil
byts, err := hex.DecodeString(test.Input.PubKeyHexStr)
if err != nil && testCaseValid {
panic(err)
}

g := NewG1()
_, err = g.FromCompressed(byts)
if err == nil && !testCaseValid {
panic("err should not be nil")
}
if err != nil && testCaseValid {
panic("err should be nil")
}
})
}
}

func TestG1MultiExpExpected(t *testing.T) {
g := NewG1()
one := g.one()
Expand Down
46 changes: 46 additions & 0 deletions g2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package bls12381
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)

var (
testDir = "tests/bls"
deserializationG2Tests = filepath.Join(testDir, "deserialization_G2/*")
)

func (g *G2) one() *PointG2 {
Expand Down Expand Up @@ -359,6 +370,41 @@ func TestZKCryptoVectorsG2CompressedValid(t *testing.T) {
}
}

func TestZKCryptoVectorsG2Compressed(t *testing.T) {
type Test struct {
Input struct {
SignatureHexStr string `yaml:"signature"`
}
IsValidPredicate *bool `yaml:"output"`
}
tests, err := filepath.Glob(deserializationG2Tests)
require.NoError(t, err)
for _, testPath := range tests {
t.Run(testPath, func(t *testing.T) {
testFile, err := os.Open(testPath)
require.NoError(t, err)
test := Test{}
err = yaml.NewDecoder(testFile).Decode(&test)
require.NoError(t, testFile.Close())
require.NoError(t, err)
testCaseValid := test.IsValidPredicate != nil
byts, err := hex.DecodeString(test.Input.SignatureHexStr)
if err != nil && testCaseValid {
panic(err)
}

g := NewG2()
_, err = g.FromCompressed(byts)
if err == nil && !testCaseValid {
panic("err should not be nil")
}
if err != nil && testCaseValid {
panic("err should be nil")
}
})
}
}

func TestG2MultiExpExpected(t *testing.T) {
g := NewG2()
one := g.one()
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/kilic/bls12-381

go 1.13

require golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
require (
github.com/stretchr/testify v1.8.2 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c
gopkg.in/yaml.v2 v2.4.0 // indirect
)
18 changes: 18 additions & 0 deletions go.sum

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kilic It might be good to consider moving to a more recent Go version in the Go mod too, to get the dep pruning goodies from go 1.17 or higher 👼🏻

Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Binary file added tests/bls/.DS_Store

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't meant to be committed. You might want to consider adding it to your global gitignore ;)

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: '800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaa}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaa900}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: c123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 2491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 6491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: e491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79a}
output: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {pubkey: c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: '800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefff}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: c123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 32cc74bc9f089ed9764bbceac5edba416bef5e73701288977b9cac1ccb6964269d4ebf78b4e8aa7792ba09d3e49c8e6a1351bdf582971f796bbaf6320e81251c9d28f674d720cca07ed14596b96697cf18238e0e03ebd7fc1353d885a39407e0}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 72cc74bc9f089ed9764bbceac5edba416bef5e73701288977b9cac1ccb6964269d4ebf78b4e8aa7792ba09d3e49c8e6a1351bdf582971f796bbaf6320e81251c9d28f674d720cca07ed14596b96697cf18238e0e03ebd7fc1353d885a39407e0}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: f2cc74bc9f089ed9764bbceac5edba416bef5e73701288977b9cac1ccb6964269d4ebf78b4e8aa7792ba09d3e49c8e6a1351bdf582971f796bbaf6320e81251c9d28f674d720cca07ed14596b96697cf18238e0e03ebd7fc1353d885a39407e0}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: 8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac}
output: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: b2cc74bc9f089ed9764bbceac5edba416bef5e73701288977b9cac1ccb6964269d4ebf78b4e8aa7792ba09d3e49c8e6a1351bdf582971f796bbaf6320e81251c9d28f674d720cca07ed14596b96697cf18238e0e03ebd7fc1353d885a39407e0}
output: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
input: {signature: c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}
output: true