From e822c1579ce56553ce2f2c635f0e75491572690b Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Tue, 14 May 2024 06:42:09 +0200 Subject: [PATCH] fix: shift constraint indices by nb of public vars (#1128) * fix: shift constraint indices by nb of public vars * perf: break early if all found --- frontend/cs/scs/builder.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/frontend/cs/scs/builder.go b/frontend/cs/scs/builder.go index 7023b40596..deec03d143 100644 --- a/frontend/cs/scs/builder.go +++ b/frontend/cs/scs/builder.go @@ -722,19 +722,24 @@ func (builder *builder) GetWireConstraints(wires []frontend.Variable, addMissing } lookup[ww.WireID()] = struct{}{} } + nbPub := builder.cs.GetNbPublicVariables() res := make([][2]int, 0, len(wires)) iterator := builder.cs.GetSparseR1CIterator() for c, constraintIdx := iterator.Next(), 0; c != nil; c, constraintIdx = iterator.Next(), constraintIdx+1 { if _, ok := lookup[int(c.XA)]; ok { - res = append(res, [2]int{constraintIdx, 0}) + res = append(res, [2]int{nbPub + constraintIdx, 0}) delete(lookup, int(c.XA)) continue } if _, ok := lookup[int(c.XB)]; ok { - res = append(res, [2]int{constraintIdx, 1}) + res = append(res, [2]int{nbPub + constraintIdx, 1}) delete(lookup, int(c.XB)) continue } + if len(lookup) == 0 { + // we can break early if we found constraints for all the wires + break + } } if addMissing { nbWitnessWires := builder.cs.GetNbPublicVariables() + builder.cs.GetNbSecretVariables() @@ -748,7 +753,7 @@ func (builder *builder) GetWireConstraints(wires []frontend.Variable, addMissing QL: constraint.CoeffIdOne, QO: constraint.CoeffIdMinusOne, }, builder.genericGate) - res = append(res, [2]int{constraintIdx, 0}) + res = append(res, [2]int{nbPub + constraintIdx, 0}) delete(lookup, k) } }