From 803e9ce9028c61b94a3adf43b369275a6307ab4f Mon Sep 17 00:00:00 2001 From: Thomas Piellard Date: Fri, 20 Sep 2024 10:17:27 +0200 Subject: [PATCH] feat: add round trip serialisation test --- ecc/bls12-377/shplonk/marshal.go | 2 +- ecc/bls12-377/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bls12-378/shplonk/marshal.go | 2 +- ecc/bls12-378/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bls12-381/shplonk/marshal.go | 2 +- ecc/bls12-381/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bls24-315/shplonk/marshal.go | 2 +- ecc/bls24-315/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bls24-317/shplonk/marshal.go | 2 +- ecc/bls24-317/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bn254/shplonk/marshal.go | 2 +- ecc/bn254/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bw6-633/shplonk/marshal.go | 2 +- ecc/bw6-633/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bw6-756/shplonk/marshal.go | 2 +- ecc/bw6-756/shplonk/shplonk_test.go | 20 +++++++++++++++++++ ecc/bw6-761/shplonk/marshal.go | 2 +- ecc/bw6-761/shplonk/shplonk_test.go | 20 +++++++++++++++++++ .../shplonk/template/marshal.go.tmpl | 2 +- .../shplonk/template/shplonk.test.go.tmpl | 20 +++++++++++++++++++ 20 files changed, 210 insertions(+), 10 deletions(-) diff --git a/ecc/bls12-377/shplonk/marshal.go b/ecc/bls12-377/shplonk/marshal.go index 9e03a86d06..c2b805a8b5 100644 --- a/ecc/bls12-377/shplonk/marshal.go +++ b/ecc/bls12-377/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bls12-377/shplonk/shplonk_test.go b/ecc/bls12-377/shplonk/shplonk_test.go index 602ee3f9d0..9333499557 100644 --- a/ecc/bls12-377/shplonk/shplonk_test.go +++ b/ecc/bls12-377/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bls12-377" "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" "github.com/consensys/gnark-crypto/ecc/bls12-377/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bls12377.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bls12-378/shplonk/marshal.go b/ecc/bls12-378/shplonk/marshal.go index 6774448de5..bbe0b8d9e1 100644 --- a/ecc/bls12-378/shplonk/marshal.go +++ b/ecc/bls12-378/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bls12-378/shplonk/shplonk_test.go b/ecc/bls12-378/shplonk/shplonk_test.go index 34e4127cc0..4b73e50e4c 100644 --- a/ecc/bls12-378/shplonk/shplonk_test.go +++ b/ecc/bls12-378/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bls12-378" "github.com/consensys/gnark-crypto/ecc/bls12-378/fr" "github.com/consensys/gnark-crypto/ecc/bls12-378/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bls12378.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bls12-381/shplonk/marshal.go b/ecc/bls12-381/shplonk/marshal.go index b609b73266..e013c2d3ef 100644 --- a/ecc/bls12-381/shplonk/marshal.go +++ b/ecc/bls12-381/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bls12-381/shplonk/shplonk_test.go b/ecc/bls12-381/shplonk/shplonk_test.go index 05fb93a3f6..b007b75590 100644 --- a/ecc/bls12-381/shplonk/shplonk_test.go +++ b/ecc/bls12-381/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bls12-381" "github.com/consensys/gnark-crypto/ecc/bls12-381/fr" "github.com/consensys/gnark-crypto/ecc/bls12-381/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bls12381.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bls24-315/shplonk/marshal.go b/ecc/bls24-315/shplonk/marshal.go index a57a90679f..a936f40bbe 100644 --- a/ecc/bls24-315/shplonk/marshal.go +++ b/ecc/bls24-315/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bls24-315/shplonk/shplonk_test.go b/ecc/bls24-315/shplonk/shplonk_test.go index a8d35f5c00..c97b6dda88 100644 --- a/ecc/bls24-315/shplonk/shplonk_test.go +++ b/ecc/bls24-315/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bls24-315" "github.com/consensys/gnark-crypto/ecc/bls24-315/fr" "github.com/consensys/gnark-crypto/ecc/bls24-315/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bls24315.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bls24-317/shplonk/marshal.go b/ecc/bls24-317/shplonk/marshal.go index b69c0946ca..f3f4520ac2 100644 --- a/ecc/bls24-317/shplonk/marshal.go +++ b/ecc/bls24-317/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bls24-317/shplonk/shplonk_test.go b/ecc/bls24-317/shplonk/shplonk_test.go index 9e6d76ded0..bd89089b78 100644 --- a/ecc/bls24-317/shplonk/shplonk_test.go +++ b/ecc/bls24-317/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bls24-317" "github.com/consensys/gnark-crypto/ecc/bls24-317/fr" "github.com/consensys/gnark-crypto/ecc/bls24-317/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bls24317.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bn254/shplonk/marshal.go b/ecc/bn254/shplonk/marshal.go index 050e1d6fdf..5da8c732a5 100644 --- a/ecc/bn254/shplonk/marshal.go +++ b/ecc/bn254/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bn254/shplonk/shplonk_test.go b/ecc/bn254/shplonk/shplonk_test.go index d0e4ce0695..88fa11fc0a 100644 --- a/ecc/bn254/shplonk/shplonk_test.go +++ b/ecc/bn254/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254/fr" "github.com/consensys/gnark-crypto/ecc/bn254/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bn254.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bw6-633/shplonk/marshal.go b/ecc/bw6-633/shplonk/marshal.go index 2c44c28a65..80661c53db 100644 --- a/ecc/bw6-633/shplonk/marshal.go +++ b/ecc/bw6-633/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bw6-633/shplonk/shplonk_test.go b/ecc/bw6-633/shplonk/shplonk_test.go index ea171cbefb..dcb46ffeea 100644 --- a/ecc/bw6-633/shplonk/shplonk_test.go +++ b/ecc/bw6-633/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bw6-633" "github.com/consensys/gnark-crypto/ecc/bw6-633/fr" "github.com/consensys/gnark-crypto/ecc/bw6-633/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bw6633.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bw6-756/shplonk/marshal.go b/ecc/bw6-756/shplonk/marshal.go index e7b0bf4ed8..8977a57254 100644 --- a/ecc/bw6-756/shplonk/marshal.go +++ b/ecc/bw6-756/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bw6-756/shplonk/shplonk_test.go b/ecc/bw6-756/shplonk/shplonk_test.go index 4643d87606..ce1c480464 100644 --- a/ecc/bw6-756/shplonk/shplonk_test.go +++ b/ecc/bw6-756/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bw6-756" "github.com/consensys/gnark-crypto/ecc/bw6-756/fr" "github.com/consensys/gnark-crypto/ecc/bw6-756/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bw6756.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/ecc/bw6-761/shplonk/marshal.go b/ecc/bw6-761/shplonk/marshal.go index f931b1dd51..6e03808eeb 100644 --- a/ecc/bw6-761/shplonk/marshal.go +++ b/ecc/bw6-761/shplonk/marshal.go @@ -29,7 +29,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/ecc/bw6-761/shplonk/shplonk_test.go b/ecc/bw6-761/shplonk/shplonk_test.go index ca08b62e53..1e4ecc2d81 100644 --- a/ecc/bw6-761/shplonk/shplonk_test.go +++ b/ecc/bw6-761/shplonk/shplonk_test.go @@ -23,8 +23,10 @@ import ( "testing" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/bw6-761" "github.com/consensys/gnark-crypto/ecc/bw6-761/fr" "github.com/consensys/gnark-crypto/ecc/bw6-761/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -45,6 +47,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := bw6761.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t) diff --git a/internal/generator/shplonk/template/marshal.go.tmpl b/internal/generator/shplonk/template/marshal.go.tmpl index 316f852ef5..98f11f61a6 100644 --- a/internal/generator/shplonk/template/marshal.go.tmpl +++ b/internal/generator/shplonk/template/marshal.go.tmpl @@ -11,7 +11,7 @@ func (proof *OpeningProof) ReadFrom(r io.Reader) (int64, error) { toDecode := []interface{}{ &proof.W, &proof.WPrime, - proof.ClaimedValues, + &proof.ClaimedValues, } for _, v := range toDecode { diff --git a/internal/generator/shplonk/template/shplonk.test.go.tmpl b/internal/generator/shplonk/template/shplonk.test.go.tmpl index 7d642ed8fa..775995c032 100644 --- a/internal/generator/shplonk/template/shplonk.test.go.tmpl +++ b/internal/generator/shplonk/template/shplonk.test.go.tmpl @@ -5,8 +5,10 @@ import ( "math/rand" "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark-crypto/ecc/{{ .Name }}" "github.com/consensys/gnark-crypto/ecc/{{ .Name }}/fr" "github.com/consensys/gnark-crypto/ecc/{{ .Name }}/kzg" + "github.com/consensys/gnark-crypto/utils/testutils" "github.com/stretchr/testify/require" ) @@ -28,6 +30,24 @@ func init() { } } +func TestSerialization(t *testing.T) { + + _, _, g, _ := {{ .CurvePackage }}.Generators() + var proof OpeningProof + proof.W.Set(&g) + proof.WPrime.Set(&g) + nbClaimedValues := 10 + proof.ClaimedValues = make([][]fr.Element, nbClaimedValues) + for i := 0; i < nbClaimedValues; i++ { + proof.ClaimedValues[i] = make([]fr.Element, i+2) + for j := 0; j < i+2; j++ { + proof.ClaimedValues[i][j].SetRandom() + } + } + + t.Run("opening proof round trip", testutils.SerializationRoundTrip(&proof)) +} + func TestOpening(t *testing.T) { assert := require.New(t)