github.com/consensys/gnark@v0.11.0/backend/groth16/bn254/icicle/marshal_test.go (about)

     1  package icicle_bn254_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/consensys/gnark-crypto/ecc"
     8  	"github.com/consensys/gnark/backend/groth16"
     9  	groth16_bn254 "github.com/consensys/gnark/backend/groth16/bn254"
    10  	icicle_bn254 "github.com/consensys/gnark/backend/groth16/bn254/icicle"
    11  	cs_bn254 "github.com/consensys/gnark/constraint/bn254"
    12  	"github.com/consensys/gnark/frontend"
    13  	"github.com/consensys/gnark/frontend/cs/r1cs"
    14  	"github.com/consensys/gnark/test"
    15  )
    16  
    17  type circuit struct {
    18  	A, B frontend.Variable `gnark:",public"`
    19  	Res  frontend.Variable
    20  }
    21  
    22  func (c *circuit) Define(api frontend.API) error {
    23  	api.AssertIsEqual(api.Mul(c.A, c.B), c.Res)
    24  	return nil
    25  }
    26  
    27  func TestMarshal(t *testing.T) {
    28  	assert := test.NewAssert(t)
    29  	ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit{})
    30  	assert.NoError(err)
    31  	tCcs := ccs.(*cs_bn254.R1CS)
    32  	nativePK := groth16_bn254.ProvingKey{}
    33  	nativeVK := groth16_bn254.VerifyingKey{}
    34  	err = groth16_bn254.Setup(tCcs, &nativePK, &nativeVK)
    35  	assert.NoError(err)
    36  
    37  	pk := groth16.NewProvingKey(ecc.BN254)
    38  	buf := new(bytes.Buffer)
    39  	_, err = nativePK.WriteTo(buf)
    40  	assert.NoError(err)
    41  	_, err = pk.ReadFrom(buf)
    42  	assert.NoError(err)
    43  	if pk.IsDifferent(&nativePK) {
    44  		t.Error("marshal output difference")
    45  	}
    46  }
    47  
    48  func TestMarshal2(t *testing.T) {
    49  	assert := test.NewAssert(t)
    50  	ccs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &circuit{})
    51  	assert.NoError(err)
    52  	tCcs := ccs.(*cs_bn254.R1CS)
    53  	iciPK := icicle_bn254.ProvingKey{}
    54  	iciVK := groth16_bn254.VerifyingKey{}
    55  	err = icicle_bn254.Setup(tCcs, &iciPK, &iciVK)
    56  	assert.NoError(err)
    57  
    58  	nativePK := groth16_bn254.ProvingKey{}
    59  	buf := new(bytes.Buffer)
    60  	_, err = iciPK.WriteTo(buf)
    61  	assert.NoError(err)
    62  	_, err = nativePK.ReadFrom(buf)
    63  	assert.NoError(err)
    64  	if iciPK.IsDifferent(&nativePK) {
    65  		t.Error("marshal output difference")
    66  	}
    67  }