diff --git a/examples/lookup/lookup.go b/examples/lookup/lookup.go new file mode 100644 index 0000000000..323d29b4e2 --- /dev/null +++ b/examples/lookup/lookup.go @@ -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 +} diff --git a/examples/lookup/lookup_test.go b/examples/lookup/lookup_test.go new file mode 100644 index 0000000000..bbc62e6ade --- /dev/null +++ b/examples/lookup/lookup_test.go @@ -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}, + }, + ) +}