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

PR to fix the non determinism of r1cs compilation #95

Merged
merged 4 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions frontend/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"math/big"
"reflect"
"runtime"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -180,7 +181,6 @@ func (cs *ConstraintSystem) LinearExpression(terms ...compiled.Term) compiled.Li
}

// reduces redundancy in a linear expression
// Non deterministic function
func (cs *ConstraintSystem) partialReduce(linExp compiled.LinearExpression, visibility compiled.Visibility) compiled.LinearExpression {

if len(linExp) == 0 {
Expand Down Expand Up @@ -219,10 +219,12 @@ func (cs *ConstraintSystem) partialReduce(linExp compiled.LinearExpression, visi
res = append(res, cs.makeTerm(varRecord[k], &bCoeff))
}

sort.Sort(res)

return res
}

// complete allocate linExp if linExp is empty. If a variable
// completeDanglingVariable allocates linExp if linExp is empty. If a variable
// is created like 'var a Variable', it will be unset but Compile(..)
// will not understand it since a.linExp is empty
func (cs *ConstraintSystem) completeDanglingVariable(v *Variable) {
Expand All @@ -235,21 +237,30 @@ func (cs *ConstraintSystem) completeDanglingVariable(v *Variable) {
}

// reduces redundancy in linear expression
// Non deterministic function
// The reduces linear expression stores the variables as public||secret||internal||unset
// for each visibility, the variables are sorted from lowest ID to highest ID
func (cs *ConstraintSystem) reduce(l compiled.LinearExpression) compiled.LinearExpression {

reducePublic := cs.partialReduce(l, compiled.Public)
reduceSecret := cs.partialReduce(l, compiled.Secret)
reduceInternal := cs.partialReduce(l, compiled.Internal)
reduceUnset := cs.partialReduce(l, compiled.Unset) // we collect also the unset variables so it stays consistant (useful for debugging)

res := make(compiled.LinearExpression, len(reducePublic)+len(reduceSecret)+len(reduceInternal)+len(reduceUnset))

accSize := 0

copy(res[:], reducePublic)
accSize += len(reducePublic)

copy(res[accSize:], reduceSecret)
accSize += len(reduceSecret)

copy(res[accSize:], reduceInternal)
accSize += len(reduceInternal)

copy(res[accSize:], reduceUnset)

return res
}

Expand Down
24 changes: 24 additions & 0 deletions frontend/cs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@ package frontend

import (
"fmt"
"sort"
"testing"

"github.com/consensys/gnark/internal/backend/compiled"
)

func TestQuickSort(t *testing.T) {

toSort := make(compiled.LinearExpression, 12)
rand := 3
for i := 0; i < 12; i++ {
toSort[i].SetVariableVisibility(compiled.Secret)
toSort[i].SetVariableID(rand)
rand += 3
rand = rand % 13
}

sort.Sort(toSort)

for i := 0; i < 10; i++ {
_, _, cur, _ := toSort[i].Unpack()
_, _, next, _ := toSort[i+1].Unpack()
if cur >= next {
t.Fatal("err sorting linear expression")
}
}

}

func TestReduce(t *testing.T) {

cs := newConstraintSystem()
Expand Down
56 changes: 52 additions & 4 deletions internal/backend/bls12-377/cs/r1cs_test.go

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

56 changes: 52 additions & 4 deletions internal/backend/bls12-381/cs/r1cs_test.go

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

56 changes: 52 additions & 4 deletions internal/backend/bn254/cs/r1cs_test.go

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

Loading