Skip to content

Commit

Permalink
docs: add lookup example
Browse files Browse the repository at this point in the history
  • Loading branch information
ivokub committed Oct 25, 2022
1 parent 68b9f49 commit 6da29b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/lookup/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lookup

import (
"fmt"

"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/lookup"
)

type LookupExampleCircuit struct {
Entries []frontend.Variable
Queries []frontend.Variable
Results []frontend.Variable
}

func (c *LookupExampleCircuit) Define(api frontend.API) error {
table := lookup.New()
for i := range c.Entries {
table.Insert(c.Entries[i])
}
results := table.Lookup(api, c.Queries...)
if len(results) != len(c.Results) {
return fmt.Errorf("result length %d expected %d", len(results), len(c.Results))
}
for i := range results {
api.AssertIsEqual(results[i], c.Results[i])
}
table.Commit(api)
return nil
}
25 changes: 25 additions & 0 deletions examples/lookup/lookup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lookup

import (
"testing"

"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/test"
)

func TestLookup(t *testing.T) {
assert := test.NewAssert(t)

assert.ProverSucceeded(
&LookupExampleCircuit{
Entries: make([]frontend.Variable, 6),
Queries: make([]frontend.Variable, 2),
Results: make([]frontend.Variable, 2),
},
&LookupExampleCircuit{
Entries: []frontend.Variable{10, 20, 30, 40, 50, 60},
Queries: []frontend.Variable{2, 4},
Results: []frontend.Variable{30, 50},
},
)
}

0 comments on commit 6da29b2

Please sign in to comment.