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

refactor: plonk uses constraint/ and couple of fixes closes #467 #493

Merged
merged 13 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from 18 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
3 changes: 3 additions & 0 deletions debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@ func writeStack(sbb *strings.Builder, forceClean ...bool) {
if strings.HasSuffix(function, "Define") {
break
}
if strings.HasSuffix(function, "callDeferred") {
break
}
}
}
3 changes: 3 additions & 0 deletions debug/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func (st *SymbolTable) CollectStack() []int {
if strings.HasSuffix(function, "Define") {
break
}
if strings.HasSuffix(function, "callDeferred") {
break
}
}
return r
}
Expand Down
2 changes: 1 addition & 1 deletion debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestTraceNotEqual(t *testing.T) {
{
_, err := getPlonkTrace(&circuit, &witness)
assert.Error(err)
assert.Contains(err.Error(), "constraint #1 is not satisfied: [assertIsEqual] 1 + -66 == 0")
assert.Contains(err.Error(), "constraint #1 is not satisfied: [assertIsEqual] 1 == 66")
assert.Contains(err.Error(), "(*notEqualTrace).Define")
assert.Contains(err.Error(), "debug_test.go:")
}
Expand Down
17 changes: 11 additions & 6 deletions frontend/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ type Compiler interface {
// FieldBitLen returns the number of bits needed to represent an element in the scalar field
FieldBitLen() int

// Commit returns a commitment to the given variables, to be used as initial randomness in
// Fiat-Shamir when the statement to prove is particularly large.
// TODO cite paper
// ! Experimental
// TENTATIVE: Functions regarding fiat-shamir-ed proofs over enormous statements TODO finalize
Commit(...Variable) (Variable, error)
// Defer is called after circuit.Define() and before Compile(). This method
// allows for the circuits to register callbacks which finalize batching
// operations etc. Unlike Go defer, it is not locally scoped.
Defer(cb func(api API) error)
}

// Builder represents a constraint system builder
Expand All @@ -73,3 +71,10 @@ type Builder interface {
// called inside circuit.Define()
SecretVariable(schema.LeafInfo) Variable
}

// Committer allows to commit to the variables and returns the commitment. The
// commitment can be used as a challenge using Fiat-Shamir heuristic.
type Committer interface {
// Commit commits to the variables and returns the commitment.
Commit(toCommit ...Variable) (commitment Variable, err error)
}
13 changes: 13 additions & 0 deletions frontend/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/debug"
"github.com/consensys/gnark/frontend/schema"
"github.com/consensys/gnark/internal/circuitdefer"
"github.com/consensys/gnark/logger"
)

Expand Down Expand Up @@ -122,10 +123,22 @@ func parseCircuit(builder Builder, circuit Circuit) (err error) {
if err = circuit.Define(builder); err != nil {
return fmt.Errorf("define circuit: %w", err)
}
if err = callDeferred(builder); err != nil {
return fmt.Errorf("")
}

return
}

func callDeferred(builder Builder) error {
for i, cb := range circuitdefer.GetAll[func(API) error](builder) {
if err := cb(builder); err != nil {
return fmt.Errorf("defer fn %d: %w", i, err)
}
}
return nil
}

// CompileOption defines option for altering the behaviour of the Compile
// method. See the descriptions of the functions returning instances of this
// type for available options.
Expand Down
22 changes: 11 additions & 11 deletions frontend/cs/r1cs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/internal/expr"
"github.com/consensys/gnark/frontend/schema"
"github.com/consensys/gnark/internal/circuitdefer"
"github.com/consensys/gnark/internal/kvstore"
"github.com/consensys/gnark/internal/tinyfield"
"github.com/consensys/gnark/internal/utils"
Expand All @@ -45,27 +46,27 @@ import (
)

// NewBuilder returns a new R1CS builder which implements frontend.API.
// Additionally, this builder also implements [frontend.Committer].
func NewBuilder(field *big.Int, config frontend.CompileConfig) (frontend.Builder, error) {
return newBuilder(field, config), nil
}

type builder struct {
cs constraint.R1CS

cs constraint.R1CS
config frontend.CompileConfig
kvstore.Store

// map for recording boolean constrained variables (to not constrain them twice)
mtBooleans map[uint64][]expr.LinearExpression

q *big.Int
tOne constraint.Coeff
heap minHeap // helps merge k sorted linear expressions

// helps merge k sorted linear expressions
heap minHeap

// buffers used to do in place api.MAC
mbuf1 expr.LinearExpression
mbuf2 expr.LinearExpression

kvstore.Store
}

// initialCapacity has quite some impact on frontend performance, especially on large circuits size
Expand Down Expand Up @@ -114,11 +115,6 @@ func newBuilder(field *big.Int, config frontend.CompileConfig) *builder {
builder.tOne = builder.cs.One()
builder.cs.AddPublicVariable("1")

builder.q = builder.cs.Field()
if builder.q.Cmp(field) != 0 {
panic("invalid modulus on cs impl") // sanity check
}

return &builder
}

Expand Down Expand Up @@ -452,3 +448,7 @@ func (builder *builder) compress(le expr.LinearExpression) expr.LinearExpression
builder.cs.AddConstraint(builder.newR1C(le, one, t))
return t
}

func (builder *builder) Defer(cb func(frontend.API) error) {
circuitdefer.Put(builder, cb)
}
25 changes: 25 additions & 0 deletions frontend/cs/r1cs/r1cs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,28 @@ func BenchmarkReduce(b *testing.B) {
}
})
}

type EmptyCircuit struct {
X frontend.Variable
cb func(frontend.API) error
}

func (c *EmptyCircuit) Define(api frontend.API) error {
api.AssertIsEqual(c.X, 0)
api.Compiler().Defer(c.cb)
return nil
}

func TestPreCompileHook(t *testing.T) {
var called bool
c := &EmptyCircuit{
cb: func(a frontend.API) error { called = true; return nil },
}
_, err := frontend.Compile(ecc.BN254.ScalarField(), NewBuilder, c)
if err != nil {
t.Fatal(err)
}
if !called {
t.Error("callback not called")
}
}
Loading