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

feat: add range check selector retrieval #1066

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions frontend/cs/scs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scs

import (
"fmt"
"math/big"
"reflect"
"sort"
Expand Down Expand Up @@ -686,3 +687,63 @@ func (builder *builder) ToCanonicalVariable(v frontend.Variable) frontend.Canoni
return term
}
}

// GetWireConstraints returns the pairs (constraintID, wireLocation) for the
// given wires in the compiled constraint system:
// - constraintID is the index of the constraint in the constraint system.
// - wireLocation is the location of the wire in the constraint (0=xA or 1=xB).
//
// If the argument addMissing is true, then the function will add a new
// constraint for each wire that is not found in the constraint system. This may
// happen when getting the constraint for a witness which is not used in
// constraints. Otherwise, when addMissing is false, the function returns an
// error if a wire is not found in the constraint system.
//
// The method only returns a single pair (constraintID, wireLocation) for every
// unique wire (removing duplicates). The order of the returned pairs is not the
// same as for the given arguments.
func (builder *builder) GetWireConstraints(wires []frontend.Variable, addMissing bool) ([][2]int, error) {
// construct a lookup table table for later quick access when iterating over instructions
lookup := make(map[int]struct{})
for _, w := range wires {
ww, ok := w.(expr.Term)
if !ok {
panic("input wire is not a Term")
}
lookup[ww.WireID()] = struct{}{}
}
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})
delete(lookup, int(c.XA))
continue
}
if _, ok := lookup[int(c.XB)]; ok {
res = append(res, [2]int{constraintIdx, 1})
delete(lookup, int(c.XB))
continue
}
}
if addMissing {
ivokub marked this conversation as resolved.
Show resolved Hide resolved
ivokub marked this conversation as resolved.
Show resolved Hide resolved
nbWitnessWires := builder.cs.GetNbPublicVariables() + builder.cs.GetNbSecretVariables()
for k := range lookup {
if k >= nbWitnessWires {
return nil, fmt.Errorf("addMissing is true, but wire %d is not a witness", k)
}
constraintIdx := builder.cs.AddSparseR1C(constraint.SparseR1C{
XA: uint32(k),
XC: uint32(k),
QL: constraint.CoeffIdOne,
QO: constraint.CoeffIdMinusOne,
}, builder.genericGate)
res = append(res, [2]int{constraintIdx, 0})
delete(lookup, k)
}
}
if len(lookup) > 0 {
return nil, fmt.Errorf("constraint with wire not found in circuit")
}
return res, nil
}
Loading