Skip to content

Commit

Permalink
Merge branch 'develop' into groth16-setup-filter-inf
Browse files Browse the repository at this point in the history
  • Loading branch information
gbotrel committed Sep 1, 2021
2 parents 4c424c8 + 5aaf531 commit d105e48
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 62 deletions.
9 changes: 5 additions & 4 deletions frontend/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ type ConstraintSystem struct {
coeffsIDs map[string]int // map to fast check existence of a coefficient (key = coeff.Text(16))

// debug info
logs []logEntry // list of logs to be printed when solving a circuit. The logs are called with the method Println
debugInfo []logEntry // list of logs storing information about assertions. If an assertion fails, it prints it in a friendly format
unsetVariables []logEntry // unset variables. If a variable is unset, the error is caught when compiling the circuit
logs []logEntry // list of logs to be printed when solving a circuit. The logs are called with the method Println
debugInfoComputation []logEntry // list of logs storing information about computations (e.g. division by 0).If an computation fails, it prints it in a friendly format
debugInfoAssertion []logEntry // list of logs storing information about assertions. If an assertion fails, it prints it in a friendly format
unsetVariables []logEntry // unset variables. If a variable is unset, the error is caught when compiling the circuit

}

Expand Down Expand Up @@ -211,7 +212,7 @@ func (cs *ConstraintSystem) reduce(l compiled.LinearExpression) compiled.LinearE

func (cs *ConstraintSystem) addAssertion(constraint compiled.R1C, debugInfo logEntry) {
cs.assertions = append(cs.assertions, constraint)
cs.debugInfo = append(cs.debugInfo, debugInfo)
cs.debugInfoAssertion = append(cs.debugInfoAssertion, debugInfo)
}

// coeffID tries to fetch the entry where b is if it exits, otherwise appends b to
Expand Down
38 changes: 37 additions & 1 deletion frontend/cs_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ func (cs *ConstraintSystem) Mul(i1, i2 interface{}, in ...interface{}) Variable
}

// Inverse returns res = inverse(v)
// TODO the function should take an interface
func (cs *ConstraintSystem) Inverse(v Variable) Variable {

v.assertIsSet()
Expand All @@ -205,9 +204,30 @@ func (cs *ConstraintSystem) Inverse(v Variable) Variable {

cs.constraints = append(cs.constraints, newR1C(v, res, cs.one()))

// prepare debug info to be displayed in case the constraint is not solved
debugInfo := logEntry{
toResolve: nil,
}
var sbb strings.Builder
sbb.WriteString("couldn't solve computational constraint (inversion by zero ?)")
stack := getCallStack()
for i := 0; i < len(stack); i++ {
sbb.WriteByte('\n')
sbb.WriteString(stack[i])
}
debugInfo.format = sbb.String()

// add it to the logs record
cs.debugInfoComputation = append(cs.debugInfoComputation, debugInfo)

return res
}

// AssertIsDifferent constrain i1 and i2 to be different
func (cs *ConstraintSystem) AssertIsDifferent(i1, i2 interface{}) {
cs.Inverse(cs.Sub(i1, i2))
}

// Div returns res = i1 / i2
func (cs *ConstraintSystem) Div(i1, i2 interface{}) Variable {

Expand Down Expand Up @@ -239,6 +259,22 @@ func (cs *ConstraintSystem) Div(i1, i2 interface{}) Variable {
}
}

// prepare debug info to be displayed in case the constraint is not solved
debugInfo := logEntry{
toResolve: nil,
}
var sbb strings.Builder
sbb.WriteString("couldn't solve computational constraint (inversion by zero ?)")
stack := getCallStack()
for i := 0; i < len(stack); i++ {
sbb.WriteByte('\n')
sbb.WriteString(stack[i])
}
debugInfo.format = sbb.String()

// add it to the logs record
cs.debugInfoComputation = append(cs.debugInfoComputation, debugInfo)

return res
}

Expand Down
54 changes: 39 additions & 15 deletions frontend/cs_to_r1cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ func (cs *ConstraintSystem) toR1CS(curveID ecc.ID) (CompiledConstraintSystem, er

// setting up the result
res := compiled.R1CS{
NbInternalVariables: len(cs.internal.variables),
NbPublicVariables: len(cs.public.variables),
NbSecretVariables: len(cs.secret.variables),
NbConstraints: len(cs.constraints) + len(cs.assertions),
NbCOConstraints: len(cs.constraints),
Constraints: make([]compiled.R1C, len(cs.constraints)+len(cs.assertions)),
Logs: make([]compiled.LogEntry, len(cs.logs)),
DebugInfo: make([]compiled.LogEntry, len(cs.debugInfo)),
NbInternalVariables: len(cs.internal.variables),
NbPublicVariables: len(cs.public.variables),
NbSecretVariables: len(cs.secret.variables),
NbConstraints: len(cs.constraints) + len(cs.assertions),
NbCOConstraints: len(cs.constraints),
Constraints: make([]compiled.R1C, len(cs.constraints)+len(cs.assertions)),
Logs: make([]compiled.LogEntry, len(cs.logs)),
DebugInfoComputation: make([]compiled.LogEntry, len(cs.debugInfoComputation)),
DebugInfoAssertion: make([]compiled.LogEntry, len(cs.debugInfoAssertion)),
}

// computational constraints (= gates)
Expand Down Expand Up @@ -68,7 +69,7 @@ func (cs *ConstraintSystem) toR1CS(curveID ecc.ID) (CompiledConstraintSystem, er
}
}

// we need to offset the ids in logs too
// we need to offset the ids in logs
for i := 0; i < len(cs.logs); i++ {
entry := compiled.LogEntry{
Format: cs.logs[i].format,
Expand All @@ -91,13 +92,13 @@ func (cs *ConstraintSystem) toR1CS(curveID ecc.ID) (CompiledConstraintSystem, er
res.Logs[i] = entry
}

// offset ids in the debugInfo
for i := 0; i < len(cs.debugInfo); i++ {
// offset ids in the debugInfoComputation
for i := 0; i < len(cs.debugInfoComputation); i++ {
entry := compiled.LogEntry{
Format: cs.debugInfo[i].format,
Format: cs.debugInfoComputation[i].format,
}
for j := 0; j < len(cs.debugInfo[i].toResolve); j++ {
_, cID, cVisibility := cs.debugInfo[i].toResolve[j].Unpack()
for j := 0; j < len(cs.debugInfoComputation[i].toResolve); j++ {
_, cID, cVisibility := cs.debugInfoComputation[i].toResolve[j].Unpack()
switch cVisibility {
case compiled.Internal:
cID += len(cs.public.variables) + len(cs.secret.variables)
Expand All @@ -111,7 +112,30 @@ func (cs *ConstraintSystem) toR1CS(curveID ecc.ID) (CompiledConstraintSystem, er
entry.ToResolve = append(entry.ToResolve, cID)
}

res.DebugInfo[i] = entry
res.DebugInfoComputation[i] = entry
}

// offset ids in the debugInfoAssertion
for i := 0; i < len(cs.debugInfoAssertion); i++ {
entry := compiled.LogEntry{
Format: cs.debugInfoAssertion[i].format,
}
for j := 0; j < len(cs.debugInfoAssertion[i].toResolve); j++ {
_, cID, cVisibility := cs.debugInfoAssertion[i].toResolve[j].Unpack()
switch cVisibility {
case compiled.Internal:
cID += len(cs.public.variables) + len(cs.secret.variables)
case compiled.Public:
// cID += len(cs.internal.variables) + len(cs.secret.variables)
case compiled.Secret:
cID += len(cs.public.variables)
case compiled.Unset:
panic("encountered unset visibility on a variable in debugInfo id offset routine")
}
entry.ToResolve = append(entry.ToResolve, cID)
}

res.DebugInfoAssertion[i] = entry
}

switch curveID {
Expand Down
6 changes: 4 additions & 2 deletions frontend/cs_to_r1cs_sparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ func popInternalVariable(l compiled.LinearExpression, id int) (compiled.LinearEx

// pops the constant associated to the one_wire in the cs, which will become
// a constant in a PLONK constraint.
// returns the reduced linear expression and the ID of the coeff corresponding to the constant term (in cs.coeffs).
//
// Returns the reduced linear expression and the ID of the coeff corresponding to the constant term (in cs.coeffs).
// If there is no constant term, the id is 0 (the 0-th entry is reserved for this purpose).
//
// ex: if l = <expr> + k1*ONE_WIRE the function returns <expr>, k1.
func (scs *sparseR1CS) popConstantTerm(l compiled.LinearExpression) (compiled.LinearExpression, big.Int) {

const idOneWire = 0

// TODO @thomas can "1 public" appear only once?
for i := 0; i < len(l); i++ {
if l[i].VariableID() == idOneWire && l[i].VariableVisibility() == compiled.Public {
lCopy := make(compiled.LinearExpression, len(l)-1)
Expand Down
32 changes: 27 additions & 5 deletions internal/backend/bls12-377/cs/r1cs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions internal/backend/bls12-381/cs/r1cs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d105e48

Please sign in to comment.