Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groth16 Prove panic due to incomplete marshalling of ProvingKey #141

Closed
myrade opened this issue Sep 14, 2021 · 2 comments
Closed

groth16 Prove panic due to incomplete marshalling of ProvingKey #141

myrade opened this issue Sep 14, 2021 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@myrade
Copy link

myrade commented Sep 14, 2021

After writing a ProvingKey to a bytes.Buffer and reading it back, groth16.Prove panics when trying to access InfinityA and InfinityB because they aren't handled by the corresponding marshaling functions. The formats []bool and int aren't supported by the gnark-crypto marshaller either.

Here's a minimal example that reproduces the behavior:

import (
	"bytes"
	"testing"

	"github.com/consensys/gnark-crypto/ecc"
	"github.com/consensys/gnark/backend"
	"github.com/consensys/gnark/backend/groth16"
	"github.com/consensys/gnark/frontend"
)

type Circuit struct {
	X frontend.Variable `gnark:"x"`
	Y frontend.Variable `gnark:",public"`
}

func (circuit *Circuit) Define(curveID ecc.ID, cs *frontend.ConstraintSystem) error {
	x3 := cs.Mul(circuit.X, circuit.X, circuit.X)
	cs.AssertIsEqual(circuit.Y, cs.Add(x3, circuit.X, 5))
	return nil
}

func TestCubicEquationMarshalled(t *testing.T) {
	assert := groth16.NewAssert(t)

	var cubicCircuit Circuit

	r1cs, err := frontend.Compile(ecc.BN254, backend.GROTH16, &cubicCircuit)
	assert.NoError(err)

	{
		var witness Circuit
		witness.X.Assign(3)
		witness.Y.Assign(35)

		pk, _, err := groth16.Setup(r1cs)
		assert.NoError(err, "generating public data should not have failed")

		buf := new(bytes.Buffer)
		_, err = pk.WriteTo(buf)
		assert.NoError(err, "writing proving key to buffer should not have failed")

		pkCopy := groth16.NewProvingKey(ecc.BN254)
		_, err = pkCopy.ReadFrom(buf)
		assert.NoError(err, "reading proving key back from buffer should not have failed")

		_, err = groth16.Prove(r1cs, pkCopy, &witness, nil)
		assert.NoError(err, "proving with good witness should not have failed")
	}

}
@gbotrel gbotrel self-assigned this Sep 14, 2021
@gbotrel gbotrel added the bug Something isn't working label Sep 14, 2021
@gbotrel gbotrel added this to the v0.6.0 milestone Sep 14, 2021
@gbotrel
Copy link
Collaborator

gbotrel commented Sep 14, 2021

Thanks for raising this issue, should be fixed with this commit

@myrade
Copy link
Author

myrade commented Sep 14, 2021

Wonderful, thanks, that was unbelievably fast. Working great.

@gbotrel gbotrel closed this as completed Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants