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

bug: R1CS.ReadFrom() panics in cbor code #985

Closed
Scratch-net opened this issue Jan 8, 2024 · 1 comment
Closed

bug: R1CS.ReadFrom() panics in cbor code #985

Scratch-net opened this issue Jan 8, 2024 · 1 comment
Assignees

Comments

@Scratch-net
Copy link

Description

Reading serialized R1CS panics

Expected Behavior

Serialization and deserialization should work as expected

Actual Behavior

panic: reflect: reflect.Value.Set using unaddressable value

Possible Fix

N/A

Steps to Reproduce

import (
	"github.com/consensys/gnark-crypto/ecc"
	"github.com/consensys/gnark/backend/groth16"
	cs "github.com/consensys/gnark/constraint/bn254"
	"github.com/consensys/gnark/frontend"
	"github.com/consensys/gnark/frontend/cs/r1cs"
	"github.com/consensys/gnark/std/math/uints"
)


type Circuit struct {
	Val  uints.U32
	Val1 uints.U32
}

func (c *Circuit) Define(api frontend.API) error {
	uapi, err := uints.New[uints.U32](api)
	if err != nil {
		return err
	}
	uapi.AssertEq(c.Val, c.Val1)
	return nil
}

func main() {
	witness := Circuit{
		Val:  uints.NewU32(10),
		Val1: uints.NewU32(10),
	}

	curve := ecc.BN254.ScalarField()
	r1css, err := frontend.Compile(curve, r1cs.NewBuilder, &witness)
	if err != nil {
		panic(err)
	}

	buf := &bytes.Buffer{}
	r1css.WriteTo(buf)

	r1css = cs.NewR1CS(0)
	r1css.ReadFrom(bytes.NewBuffer(buf.Bytes()))
	fmt.Println(r1css.GetNbConstraints())
}

Context

I was trying to load compiled circuit from file

Your Environment

  • gnark v0.9.1
  • gnark-crypto v0.12.2
  • go1.21.0
@gbotrel gbotrel self-assigned this Jan 9, 2024
@gbotrel
Copy link
Collaborator

gbotrel commented Jan 9, 2024

Hi -- replacing

r1css = cs.NewR1CS(0)

by

// import "github.com/consensys/gnark/backend/groth16"
r1css = groth16.NewCS(ecc.BN254)

Will work. The cs.NewR1CS is indeed confusing now that it is exposed and not documented...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants