Consensys / gnark

gnark is a fast zk-SNARK library that offers a high-level API to design circuits. The library is open source and developed under the Apache 2.0 license
https://hackmd.io/@gnark
Apache License 2.0
1.44k stars 369 forks source link

20:16:12 ERR error="constraint #106781 is not satisfied: qL⋅xa + qR⋅xb + qO⋅xc + qM⋅(xaxb) + qC != 0 → 143 + 0 + 0 + 0 + -227 != 0" nbConstraints=980154 #990

Closed mperafan closed 10 months ago

mperafan commented 10 months ago

Hello! How are you? Im getting the 20:16:12 ERR error="constraint #106781 is not satisfied: qL⋅xa + qR⋅xb + qO⋅xc + qM⋅(xaxb) + qC != 0 → 143 + 0 + 0 + 0 + -227 != 0" nbConstraints=980154 error, but i cant find why. I know the part is giving me the error is this:

type Packet struct { AggregatedPacketHash [32]uints.U8 gnark:",public" //Public hash AggregatedPacket []byte gnark:",private" //Packet } func bytetouint(PacketBytes []byte) [32]uints.U8 { //function to convert bytes to uint aux := uints.NewU8Array(PacketBytes) var Packetaux [32]uints.U8 for j := 0; j < min(32, len(aux)); j++ { Packetaux[j] = aux[j] } return Packetaux } func (circuit Packet) Define(api frontend.API) error { uapi, err := uints.Newuints.U32 if err != nil { return err } hpacket := sha256.New() hpacket.Write(circuit.AggregatedPacket) res := bytetouint(hpacket.Sum(make([]byte, 0))) for k := range circuit.AggregatedPacketHash { uapi.ByteAssertEq(circuit.AggregatedPacketHash[k], res[k]) } return nil } func Proof() Packet { //function where i calculate the variable for the proof var Pack Packet Pack.AggregatedPacket = getBytes(big.NewInt(rand.Int63n(int64(1e18)))) shaa2 := sha256.New() shaa2.Write(Pack.AggregatedPacket) Pack.AggregatedPacketHash = bytetouint(shaa2.Sum(make([]byte, 0))) return Pack } func main(){ packetaux := Proof() assignment := &packetaux var myCircuit2 Packet cs, _ := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &myCircuit2) scs := cs.(bn254cs.SparseR1CS) srs, srsLagrange, := unsafekzg.NewSRS(scs) pk, vk, := plonk.Setup(cs, srs, srsLagrange) witness, errNW := frontend.NewWitness(assignment, ecc.BN254.ScalarField()) if errNW != nil { panic(errNW) } proof, errProve := plonk.Prove(cs, pk, witness) if errProve != nil { panic(errProve) } pubWitness, _ := witness.Public() err := plonk.Verify(proof, vk, pubWitness) if err != nil { panic(err) } }

Thank you very much

mperafan commented 10 months ago

the line of uapi, err := uints.Newuints.U32 actually is uapi, err := uints.New [uints.U32] (api) i dont know why github changes it

ivokub commented 10 months ago

The posted example doesn't compile, the function getBytes is not defined.

Please - can you post a minimal repo with your example as main.go? Otherwise there is too much flexibility in dependencies, imports etc. and not worth debugging.

ivokub commented 10 months ago

And - it seems to be like a question about ZK circuit design. We usually try to keep the discussion and issues separated as we use them for planning.