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 simple key-value store to the builders #480

Merged
merged 3 commits into from
Feb 28, 2023
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
4 changes: 4 additions & 0 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/kvstore"
"github.com/consensys/gnark/internal/tinyfield"
"github.com/consensys/gnark/internal/utils"
"github.com/consensys/gnark/logger"
Expand Down Expand Up @@ -63,6 +64,8 @@ type builder struct {
// 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 All @@ -78,6 +81,7 @@ func newBuilder(field *big.Int, config frontend.CompileConfig) *builder {
heap: make(minHeap, 0, 100),
mbuf1: make(expr.LinearExpression, 0, macCapacity),
mbuf2: make(expr.LinearExpression, 0, macCapacity),
Store: kvstore.New(),
}

// by default the circuit is given a public wire equal to 1
Expand Down
4 changes: 4 additions & 0 deletions frontend/cs/scs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/consensys/gnark/frontend/cs"
"github.com/consensys/gnark/frontend/internal/expr"
"github.com/consensys/gnark/frontend/schema"
"github.com/consensys/gnark/internal/kvstore"
"github.com/consensys/gnark/internal/tinyfield"
"github.com/consensys/gnark/internal/utils"
"github.com/consensys/gnark/logger"
Expand Down Expand Up @@ -57,6 +58,8 @@ type scs struct {
mtBooleans map[int]struct{}

q *big.Int

kvstore.Store
}

// initialCapacity has quite some impact on frontend performance, especially on large circuits size
Expand All @@ -67,6 +70,7 @@ func newBuilder(field *big.Int, config frontend.CompileConfig) *scs {
mtBooleans: make(map[int]struct{}),
st: cs.NewCoeffTable(),
config: config,
Store: kvstore.New(),
}

curve := utils.FieldToCurve(field)
Expand Down
38 changes: 38 additions & 0 deletions internal/kvstore/kvstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Package kvstore implements simple key-value store
//
// It is without synchronization and allows any comparable keys. The main use of
// this package is for sharing singletons when building a circuit.
package kvstore

import (
"reflect"
)

type Store interface {
SetKeyValue(key, value any)
GetKeyValue(key any) (value any)
}

type impl struct {
db map[any]any
}

func New() Store {
return &impl{
db: make(map[any]any),
}
}

func (c *impl) SetKeyValue(key, value any) {
if !reflect.TypeOf(key).Comparable() {
panic("key type not comparable")
}
c.db[key] = value
}

func (c *impl) GetKeyValue(key any) any {
if !reflect.TypeOf(key).Comparable() {
panic("key type not comparable")
}
return c.db[key]
}
3 changes: 3 additions & 0 deletions test/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/hint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/internal/kvstore"
"github.com/consensys/gnark/internal/utils"
)

Expand All @@ -50,6 +51,7 @@ type engine struct {
// mHintsFunctions map[hint.ID]hintFunction
constVars bool
apiWrapper ApiWrapper
kvstore.Store
}

// TestEngineOption defines an option for the test engine.
Expand Down Expand Up @@ -103,6 +105,7 @@ func IsSolved(circuit, witness frontend.Circuit, field *big.Int, opts ...TestEng
q: new(big.Int).Set(field),
apiWrapper: func(a frontend.API) frontend.API { return a },
constVars: false,
Store: kvstore.New(),
}
for _, opt := range opts {
if err := opt(e); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions test/solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/frontend/schema"
"github.com/consensys/gnark/internal/backend/circuits"
"github.com/consensys/gnark/internal/kvstore"
"github.com/consensys/gnark/internal/tinyfield"
"github.com/consensys/gnark/internal/utils"
)
Expand Down Expand Up @@ -155,6 +156,7 @@ func isSolvedEngine(c frontend.Circuit, field *big.Int, opts ...TestEngineOption
q: new(big.Int).Set(field),
apiWrapper: func(a frontend.API) frontend.API { return a },
constVars: false,
Store: kvstore.New(),
}
for _, opt := range opts {
if err := opt(e); err != nil {
Expand Down