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: compress linear expression #418

Merged
merged 1 commit into from
Dec 16, 2022
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
21 changes: 21 additions & 0 deletions frontend/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ type CompileConfig struct {
Capacity int
IgnoreUnconstrainedInputs bool
wrapper BuilderWrapper
CompressThreshold int
}

// WithCapacity is a compile option that specifies the estimated capacity needed
Expand Down Expand Up @@ -188,6 +189,26 @@ func WithBuilderWrapper(wrapper BuilderWrapper) CompileOption {
}
}

// WithCompressThreshold is a compile option which enforces automatic variable
// compression if the length of the linear expression in the variable exceeds
// given threshold.
//
// This option is usable in arithmetisations where the variable is a linear
// combination, as for example in R1CS. If variable is not a linear combination,
// then this option does not change the compile behaviour.
//
// This compile option should be used in cases when it is known that there are
// long addition chains and the compile time and memory usage start are growing
// fast. The compression adds some overhead in the number of constraints. The
// overhead and compile performance depends on threshold value, and it should be
// chosen carefully.
func WithCompressThreshold(threshold int) CompileOption {
return func(opt *CompileConfig) error {
opt.CompressThreshold = threshold
return nil
}
}

var tVariable reflect.Type

func init() {
Expand Down
3 changes: 3 additions & 0 deletions frontend/cs/r1cs/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ func (builder *builder) add(vars []expr.LinearExpression, sub bool, capacity int
// keep the linear expression valid (assertIsSet)
res = expr.NewLinearExpression(0, constraint.Coeff{})
}
// if the linear expression LE is too long then record an equality
// constraint LE * 1 = t and return short linear expression instead.
res = builder.compress(res)

return res
}
Expand Down
15 changes: 15 additions & 0 deletions frontend/cs/r1cs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,18 @@ func (builder *builder) newDebugInfo(errName string, in ...interface{}) constrai
return constraint.NewDebugInfo(errName, in...)

}

// compress checks the length of the linear expression le and if it is larger or
// equal than CompressThreshold in the configuration, replaces it with a linear
// expression of one term. In that case it adds an equality constraint enforcing
// the correctness of the returned linear expression.
func (builder *builder) compress(le expr.LinearExpression) expr.LinearExpression {
if builder.config.CompressThreshold <= 0 || len(le) < builder.config.CompressThreshold {
return le
}

one := builder.cstOne()
t := builder.newInternalVariable()
builder.cs.AddConstraint(builder.newR1C(le, one, t))
return t
}
20 changes: 20 additions & 0 deletions frontend/cs/r1cs/r1cs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ func TestReduce(t *testing.T) {

}

func TestCompress(t *testing.T) {
cs := newBuilder(ecc.BN254.ScalarField(), frontend.CompileConfig{CompressThreshold: 3})
vars := make([]frontend.Variable, 4)
for i := range vars {
v := cs.newInternalVariable()
vars[i] = cs.Mul(v, 1<<i)
}

// if add two variables, then should not compress
v1 := cs.Add(vars[0], vars[1])
if vli1 := v1.(expr.LinearExpression); len(vli1) != 2 {
t.Fatalf("expected linear expression length 2, got %d", len(vli1))
}
// if add three vars, then should compress
v2 := cs.Add(vars[0], vars[1], vars[2])
if vli2 := v2.(expr.LinearExpression); len(vli2) != 1 {
t.Fatalf("expected linear expression length 1, got %d", len(vli2))
}
}

func BenchmarkReduce(b *testing.B) {
cs := newBuilder(ecc.BN254.ScalarField(), frontend.CompileConfig{})
// 4 interesting cases;
Expand Down