From bbace6567582df25c9e3d82e8130b27944d148c8 Mon Sep 17 00:00:00 2001 From: Eagle941 <8973725+Eagle941@users.noreply.github.com> Date: Mon, 25 Sep 2023 22:15:07 +0100 Subject: [PATCH] chore: removed abstractor.API (#41) * Removed abstractor.API * refactor call * moved interface to abstractor * moved interface to abstractor --- README.md | 2 +- abstractor/abstractor.go | 26 +--- abstractor/concretizer.go | 129 ------------------ abstractor/interface.go | 41 ++++++ extractor/extractor.go | 3 +- extractor/interface.go | 43 +----- extractor/test/another_circuit_test.go | 10 +- extractor/test/circuit_with_parameter_test.go | 16 +-- extractor/test/deletion_mbu_circuit_test.go | 10 +- extractor/test/merkle_recover_test.go | 12 +- extractor/test/my_circuit_test.go | 7 +- extractor/test/slices_optimisation_test.go | 28 ++-- extractor/test/to_binary_circuit_test.go | 12 +- extractor/test/two_gadgets_test.go | 14 +- 14 files changed, 88 insertions(+), 265 deletions(-) delete mode 100644 abstractor/concretizer.go create mode 100644 abstractor/interface.go diff --git a/README.md b/README.md index 5dbc80e..84f9ae9 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ type MyCircuit struct { Out frontend.Variable } -func (circuit *MyCircuit) AbsDefine(api abstractor.API) error { +func (circuit *MyCircuit) Define(api abstractor.API) error { sum := api.Add(circuit.In_1, circuit.In_2) api.AssertIsEqual(sum, circuit.Out) return nil diff --git a/abstractor/abstractor.go b/abstractor/abstractor.go index 4b8bb85..46d7395 100644 --- a/abstractor/abstractor.go +++ b/abstractor/abstractor.go @@ -7,33 +7,9 @@ type Gadget interface { } type GadgetDefinition interface { - DefineGadget(api API) interface{} + DefineGadget(api frontend.API) interface{} } type API interface { - frontend.API - DefineGadget(gadget GadgetDefinition) Gadget - - frontend.API Call(gadget GadgetDefinition) interface{} } - -type Circuit interface { - frontend.Circuit - AbsDefine(api API) error -} - -func Concretize(api frontend.API, circuit Circuit) error { - return circuit.AbsDefine(&Concretizer{api}) -} - -func CallGadget(api frontend.API, circuit GadgetDefinition) interface{} { - _, ok := api.(API) - if ok { - // The consequence of calling CallGadget with abstractor.API is that - // the circuit is extracted as a single function instead of - // splitting in sub-circuits - panic("abstractor.CallGadget can't be called with abstractor.API") - } - return circuit.DefineGadget(&Concretizer{api}) -} diff --git a/abstractor/concretizer.go b/abstractor/concretizer.go deleted file mode 100644 index bd76575..0000000 --- a/abstractor/concretizer.go +++ /dev/null @@ -1,129 +0,0 @@ -package abstractor - -import ( - "github.com/consensys/gnark/backend/hint" - "github.com/consensys/gnark/frontend" - "math/big" -) - -type ConcreteGadget struct { - api API -} - -func (g *ConcreteGadget) Call(gadget GadgetDefinition) interface{} { - return gadget.DefineGadget(g.api) -} - -type Concretizer struct { - api frontend.API -} - -func (c *Concretizer) Add(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable { - return c.api.Add(i1, i2, in...) -} - -func (c *Concretizer) MulAcc(a, b, c_ frontend.Variable) frontend.Variable { - return c.api.MulAcc(a, b, c_) -} - -func (c *Concretizer) Neg(i1 frontend.Variable) frontend.Variable { - return c.api.Neg(i1) -} - -func (c *Concretizer) Sub(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable { - return c.api.Sub(i1, i2, in...) -} - -func (c *Concretizer) Mul(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable { - return c.api.Mul(i1, i2, in...) -} - -func (c *Concretizer) DivUnchecked(i1, i2 frontend.Variable) frontend.Variable { - return c.api.DivUnchecked(i1, i2) -} - -func (c *Concretizer) Div(i1, i2 frontend.Variable) frontend.Variable { - return c.api.Div(i1, i2) -} - -func (c *Concretizer) Inverse(i1 frontend.Variable) frontend.Variable { - return c.api.Inverse(i1) -} - -func (c *Concretizer) ToBinary(i1 frontend.Variable, n ...int) []frontend.Variable { - return c.api.ToBinary(i1, n...) -} - -func (c *Concretizer) FromBinary(b ...frontend.Variable) frontend.Variable { - return c.api.FromBinary(b...) -} - -func (c *Concretizer) Xor(a, b frontend.Variable) frontend.Variable { - return c.api.Xor(a, b) -} - -func (c *Concretizer) Or(a, b frontend.Variable) frontend.Variable { - return c.api.Or(a, b) -} - -func (c *Concretizer) And(a, b frontend.Variable) frontend.Variable { - return c.api.And(a, b) -} - -func (c *Concretizer) Select(b frontend.Variable, i1, i2 frontend.Variable) frontend.Variable { - return c.api.Select(b, i1, i2) -} - -func (c *Concretizer) Lookup2(b0, b1 frontend.Variable, i0, i1, i2, i3 frontend.Variable) frontend.Variable { - return c.api.Lookup2(b0, b1, i0, i1, i2, i3) -} - -func (c *Concretizer) IsZero(i1 frontend.Variable) frontend.Variable { - return c.api.IsZero(i1) -} - -func (c *Concretizer) Cmp(i1, i2 frontend.Variable) frontend.Variable { - return c.api.Cmp(i1, i2) -} - -func (c *Concretizer) AssertIsEqual(i1, i2 frontend.Variable) { - c.api.AssertIsEqual(i1, i2) -} - -func (c *Concretizer) AssertIsDifferent(i1, i2 frontend.Variable) { - c.api.AssertIsDifferent(i1, i2) -} - -func (c *Concretizer) AssertIsBoolean(i1 frontend.Variable) { - c.api.AssertIsBoolean(i1) -} - -func (c *Concretizer) AssertIsLessOrEqual(v frontend.Variable, bound frontend.Variable) { - c.api.AssertIsLessOrEqual(v, bound) -} - -func (c *Concretizer) Println(a ...frontend.Variable) { - c.api.Println(a...) -} - -func (c *Concretizer) Compiler() frontend.Compiler { - return c.api.Compiler() -} - -func (c *Concretizer) NewHint(f hint.Function, nbOutputs int, inputs ...frontend.Variable) ([]frontend.Variable, error) { - return c.api.NewHint(f, nbOutputs, inputs...) -} - -func (c *Concretizer) ConstantValue(v frontend.Variable) (*big.Int, bool) { - return c.api.ConstantValue(v) -} - -func (c *Concretizer) DefineGadget(gadget GadgetDefinition) Gadget { - return &ConcreteGadget{c} -} - -func (c *Concretizer) Call(gadget GadgetDefinition) interface{} { - return c.DefineGadget(gadget).Call(gadget) -} - -var _ API = &(Concretizer{}) diff --git a/abstractor/interface.go b/abstractor/interface.go new file mode 100644 index 0000000..7fefa3e --- /dev/null +++ b/abstractor/interface.go @@ -0,0 +1,41 @@ +// This file contains the public API for using the extractor. +// The Call functions are used to call gadgets and get their returnd object. +// These methods are prepared for doing automated casting from interface{}. +// Alternatively it's possible to do manual casting by calling +// abstractor.Call() and casting the result to the needed type. +package abstractor + +import ( + "github.com/consensys/gnark/frontend" +) + +// Call is used to call a Gadget which returns frontend.Variable (i.e. a single element `F` in Lean) +func Call(api frontend.API, gadget GadgetDefinition) frontend.Variable { + if abs, ok := api.(API); ok { + return abs.Call(gadget).(frontend.Variable) + } else { + return gadget.DefineGadget(api).(frontend.Variable) + } +} + +// CallVoid is used to call a Gadget which doesn't return anything +func CallVoid(api frontend.API, gadget GadgetDefinition) { + Call(api, gadget) +} + +// Call1 is used to call a Gadget which returns []frontend.Variable (i.e. `Vector F d` in Lean) +func Call1(api frontend.API, gadget GadgetDefinition) []frontend.Variable { + return Call(api, gadget).([]frontend.Variable) +} + +// Call2 is used to call a Gadget which returns a [][]frontend.Variable +// (i.e. `Vector (Vector F a) b` in Lean) +func Call2(api frontend.API, gadget GadgetDefinition) [][]frontend.Variable { + return Call(api, gadget).([][]frontend.Variable) +} + +// Call3 is used to call a Gadget which returns a [][][]frontend.Variable +// (i.e. `Vector (Vector (Vector F a) b) c` in Lean) +func Call3(api frontend.API, gadget GadgetDefinition) [][][]frontend.Variable { + return Call(api, gadget).([][][]frontend.Variable) +} diff --git a/extractor/extractor.go b/extractor/extractor.go index 638c084..c428219 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -5,12 +5,11 @@ import ( "math/big" "reflect" - "github.com/reilabs/gnark-lean-extractor/v2/abstractor" - "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/backend/hint" "github.com/consensys/gnark/frontend" "github.com/consensys/gnark/frontend/schema" + "github.com/reilabs/gnark-lean-extractor/v2/abstractor" ) type Operand interface { diff --git a/extractor/interface.go b/extractor/interface.go index e483072..b12333a 100644 --- a/extractor/interface.go +++ b/extractor/interface.go @@ -1,8 +1,4 @@ -// This file contains the public API for using the extractor. -// The Call functions are used to call gadgets and get their returnd object. -// These methods are prepared for doing automated casting from interface{}. -// Alternatively it's possible to do manual casting by calling -// abstractor.API.Call() and casting the result to the needed type. +// This file contains the public API for running the extractor. package extractor import ( @@ -15,38 +11,11 @@ import ( "golang.org/x/exp/slices" ) -// CallVoid is used to call a Gadget which doesn't return anything -func CallVoid(api abstractor.API, gadget abstractor.GadgetDefinition) { - api.Call(gadget) -} - -// Call is used to call a Gadget which returns frontend.Variable (i.e. a single element `F` in Lean) -func Call(api abstractor.API, gadget abstractor.GadgetDefinition) frontend.Variable { - return api.Call(gadget).(frontend.Variable) -} - -// Call1 is used to call a Gadget which returns []frontend.Variable (i.e. `Vector F d` in Lean) -func Call1(api abstractor.API, gadget abstractor.GadgetDefinition) []frontend.Variable { - return api.Call(gadget).([]frontend.Variable) -} - -// Call2 is used to call a Gadget which returns a [][]frontend.Variable -// (i.e. `Vector (Vector F a) b` in Lean) -func Call2(api abstractor.API, gadget abstractor.GadgetDefinition) [][]frontend.Variable { - return api.Call(gadget).([][]frontend.Variable) -} - -// Call3 is used to call a Gadget which returns a [][][]frontend.Variable -// (i.e. `Vector (Vector (Vector F a) b) c` in Lean) -func Call3(api abstractor.API, gadget abstractor.GadgetDefinition) [][][]frontend.Variable { - return api.Call(gadget).([][][]frontend.Variable) -} - // CircuitToLeanWithName exports a `circuit` to Lean over a `field` with `namespace` // CircuitToLeanWithName and CircuitToLean aren't joined in a single function // CircuitToLean(circuit abstractor.Circuit, field ecc.ID, namespace ...string) because the long term view // is to add an optional parameter to support custom `set_option` directives in the header. -func CircuitToLeanWithName(circuit abstractor.Circuit, field ecc.ID, namespace string) (out string, err error) { +func CircuitToLeanWithName(circuit frontend.Circuit, field ecc.ID, namespace string) (out string, err error) { defer recoverError() schema, err := getSchema(circuit) @@ -62,7 +31,7 @@ func CircuitToLeanWithName(circuit abstractor.Circuit, field ecc.ID, namespace s FieldID: field, } - err = circuit.AbsDefine(&api) + err = circuit.Define(&api) if err != nil { return "", err } @@ -80,7 +49,7 @@ func CircuitToLeanWithName(circuit abstractor.Circuit, field ecc.ID, namespace s // CircuitToLean exports a `circuit` to Lean over a `field` with the namespace being the // struct name of `circuit` // When the namespace argument is not defined, it uses the name of the struct circuit -func CircuitToLean(circuit abstractor.Circuit, field ecc.ID) (string, error) { +func CircuitToLean(circuit frontend.Circuit, field ecc.ID) (string, error) { name := getStructName(circuit) return CircuitToLeanWithName(circuit, field, name) } @@ -110,7 +79,7 @@ func GadgetToLean(gadget abstractor.GadgetDefinition, field ecc.ID) (string, err } // ExtractCircuits is used to export a series of `circuits` to Lean over a `field` under `namespace`. -func ExtractCircuits(namespace string, field ecc.ID, circuits ...abstractor.Circuit) (out string, err error) { +func ExtractCircuits(namespace string, field ecc.ID, circuits ...frontend.Circuit) (out string, err error) { defer recoverError() api := CodeExtractor{ @@ -142,7 +111,7 @@ func ExtractCircuits(namespace string, field ecc.ID, circuits ...abstractor.Circ past_circuits = append(past_circuits, name) circuitInit(circuit, schema) - err = circuit.AbsDefine(&api) + err = circuit.Define(&api) if err != nil { return "", err } diff --git a/extractor/test/another_circuit_test.go b/extractor/test/another_circuit_test.go index e3ce26b..c825af4 100644 --- a/extractor/test/another_circuit_test.go +++ b/extractor/test/another_circuit_test.go @@ -17,7 +17,7 @@ type IntArrayGadget struct { NestedMatrix [2][2]int } -func (gadget IntArrayGadget) DefineGadget(api abstractor.API) interface{} { +func (gadget IntArrayGadget) DefineGadget(api frontend.API) interface{} { r := api.FromBinary(gadget.In...) api.Mul(gadget.Matrix[0], gadget.Matrix[1]) return []frontend.Variable{r, r, r} @@ -28,8 +28,8 @@ type AnotherCircuit struct { Matrix [2][2]int } -func (circuit *AnotherCircuit) AbsDefine(api abstractor.API) error { - r := extractor.Call1(api, IntArrayGadget{ +func (circuit *AnotherCircuit) Define(api frontend.API) error { + r := abstractor.Call1(api, IntArrayGadget{ circuit.In, circuit.Matrix[0], circuit.Matrix, @@ -41,10 +41,6 @@ func (circuit *AnotherCircuit) AbsDefine(api abstractor.API) error { return nil } -func (circuit AnotherCircuit) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestAnotherCircuit(t *testing.T) { m := [2][2]int{ {0, 36}, diff --git a/extractor/test/circuit_with_parameter_test.go b/extractor/test/circuit_with_parameter_test.go index 22c8cb1..2c70f8c 100644 --- a/extractor/test/circuit_with_parameter_test.go +++ b/extractor/test/circuit_with_parameter_test.go @@ -17,7 +17,7 @@ type ReturnItself struct { Out []frontend.Variable } -func (gadget ReturnItself) DefineGadget(api abstractor.API) interface{} { +func (gadget ReturnItself) DefineGadget(api frontend.API) interface{} { for i := 0; i < len(gadget.In_1); i++ { gadget.Out[i] = api.Mul(gadget.In_1[i], gadget.In_1[i]) } @@ -30,7 +30,7 @@ type SliceGadget struct { In_2 []frontend.Variable } -func (gadget SliceGadget) DefineGadget(api abstractor.API) interface{} { +func (gadget SliceGadget) DefineGadget(api frontend.API) interface{} { for i := 0; i < len(gadget.In_1); i++ { api.Mul(gadget.In_1[i], gadget.In_2[i]) } @@ -46,10 +46,10 @@ type CircuitWithParameter struct { Param int } -func (circuit *CircuitWithParameter) AbsDefine(api abstractor.API) error { +func (circuit *CircuitWithParameter) Define(api frontend.API) error { D := make([]frontend.Variable, 3) for i := 0; i < len(circuit.Path); i++ { - D = extractor.Call1(api, ReturnItself{ + D = abstractor.Call1(api, ReturnItself{ In_1: circuit.Path, Out: D, }) @@ -66,19 +66,15 @@ func (circuit *CircuitWithParameter) AbsDefine(api abstractor.API) error { dec := api.FromBinary(bin...) api.AssertIsEqual(circuit.Param, dec) - extractor.Call(api, SliceGadget{circuit.Path, circuit.Path}) + abstractor.Call(api, SliceGadget{circuit.Path, circuit.Path}) api.Mul(circuit.Path[0], circuit.Path[0]) - extractor.Call(api, SliceGadget{circuit.Tree, circuit.Tree}) + abstractor.Call(api, SliceGadget{circuit.Tree, circuit.Tree}) api.AssertIsEqual(circuit.Param, circuit.In) return nil } -func (circuit CircuitWithParameter) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestCircuitWithParameter(t *testing.T) { paramValue := 20 assignment := CircuitWithParameter{Path: make([]frontend.Variable, 3), Tree: make([]frontend.Variable, 2)} diff --git a/extractor/test/deletion_mbu_circuit_test.go b/extractor/test/deletion_mbu_circuit_test.go index 7ded5c5..8d261cb 100644 --- a/extractor/test/deletion_mbu_circuit_test.go +++ b/extractor/test/deletion_mbu_circuit_test.go @@ -21,7 +21,7 @@ type DeletionProof struct { Depth int } -func (gadget DeletionProof) DefineGadget(api abstractor.API) interface{} { +func (gadget DeletionProof) DefineGadget(api frontend.API) interface{} { return gadget.PreRoot } @@ -42,8 +42,8 @@ type DeletionMbuCircuit struct { Depth int } -func (circuit *DeletionMbuCircuit) AbsDefine(api abstractor.API) error { - root := extractor.Call(api, DeletionProof{ +func (circuit *DeletionMbuCircuit) Define(api frontend.API) error { + root := abstractor.Call(api, DeletionProof{ DeletionIndices: circuit.DeletionIndices, PreRoot: circuit.PreRoot, IdComms: circuit.IdComms, @@ -58,10 +58,6 @@ func (circuit *DeletionMbuCircuit) AbsDefine(api abstractor.API) error { return nil } -func (circuit DeletionMbuCircuit) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestDeletionMbuCircuit(t *testing.T) { batchSize := 2 treeDepth := 3 diff --git a/extractor/test/merkle_recover_test.go b/extractor/test/merkle_recover_test.go index 44f7ebb..b2850bf 100644 --- a/extractor/test/merkle_recover_test.go +++ b/extractor/test/merkle_recover_test.go @@ -16,7 +16,7 @@ type DummyHash struct { In_2 frontend.Variable } -func (gadget DummyHash) DefineGadget(api abstractor.API) interface{} { +func (gadget DummyHash) DefineGadget(api frontend.API) interface{} { r := api.Mul(gadget.In_1, gadget.In_2) return r } @@ -28,11 +28,11 @@ type MerkleRecover struct { Proof [20]frontend.Variable `gnark:",secret"` } -func (circuit *MerkleRecover) AbsDefine(api abstractor.API) error { +func (circuit *MerkleRecover) Define(api frontend.API) error { current := circuit.Element for i := 0; i < len(circuit.Path); i++ { - leftHash := extractor.Call(api, DummyHash{current, circuit.Proof[i]}) - rightHash := extractor.Call(api, DummyHash{circuit.Proof[i], current}) + leftHash := abstractor.Call(api, DummyHash{current, circuit.Proof[i]}) + rightHash := abstractor.Call(api, DummyHash{circuit.Proof[i], current}) current = api.Select(circuit.Path[i], rightHash, leftHash) } api.AssertIsEqual(current, circuit.Root) @@ -40,10 +40,6 @@ func (circuit *MerkleRecover) AbsDefine(api abstractor.API) error { return nil } -func (circuit MerkleRecover) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestMerkleRecover(t *testing.T) { assignment := MerkleRecover{} out, err := extractor.CircuitToLean(&assignment, ecc.BN254) diff --git a/extractor/test/my_circuit_test.go b/extractor/test/my_circuit_test.go index 5726dc7..577317d 100644 --- a/extractor/test/my_circuit_test.go +++ b/extractor/test/my_circuit_test.go @@ -6,7 +6,6 @@ import ( "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/frontend" - "github.com/reilabs/gnark-lean-extractor/v2/abstractor" "github.com/reilabs/gnark-lean-extractor/v2/extractor" ) @@ -17,16 +16,12 @@ type MyCircuit struct { Out frontend.Variable } -func (circuit *MyCircuit) AbsDefine(api abstractor.API) error { +func (circuit *MyCircuit) Define(api frontend.API) error { sum := api.Add(circuit.In_1, circuit.In_2) api.AssertIsEqual(sum, circuit.Out) return nil } -func (circuit MyCircuit) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestMyCircuit(t *testing.T) { assignment := MyCircuit{} out, err := extractor.CircuitToLean(&assignment, ecc.BN254) diff --git a/extractor/test/slices_optimisation_test.go b/extractor/test/slices_optimisation_test.go index 8b5a412..d365ffb 100644 --- a/extractor/test/slices_optimisation_test.go +++ b/extractor/test/slices_optimisation_test.go @@ -15,7 +15,7 @@ type TwoSlices struct { TwoDim [][]frontend.Variable } -func (gadget TwoSlices) DefineGadget(api abstractor.API) interface{} { +func (gadget TwoSlices) DefineGadget(api frontend.API) interface{} { return gadget.TwoDim } @@ -23,7 +23,7 @@ type ThreeSlices struct { ThreeDim [][][]frontend.Variable } -func (gadget ThreeSlices) DefineGadget(api abstractor.API) interface{} { +func (gadget ThreeSlices) DefineGadget(api frontend.API) interface{} { return gadget.ThreeDim } @@ -32,7 +32,7 @@ type SlicesGadget struct { ThreeDim [][][]frontend.Variable } -func (gadget SlicesGadget) DefineGadget(api abstractor.API) interface{} { +func (gadget SlicesGadget) DefineGadget(api frontend.API) interface{} { return append(gadget.ThreeDim[0][0], gadget.TwoDim[0]...) } @@ -43,43 +43,39 @@ type SlicesOptimisation struct { ThreeDim [][][]frontend.Variable } -func (circuit *SlicesOptimisation) AbsDefine(api abstractor.API) error { - extractor.Call1(api, SlicesGadget{ +func (circuit *SlicesOptimisation) Define(api frontend.API) error { + abstractor.Call1(api, SlicesGadget{ TwoDim: circuit.TwoDim, ThreeDim: circuit.ThreeDim, }) - extractor.Call1(api, SlicesGadget{ + abstractor.Call1(api, SlicesGadget{ TwoDim: [][]frontend.Variable{circuit.TwoDim[1], circuit.TwoDim[0]}, ThreeDim: [][][]frontend.Variable{circuit.ThreeDim[1], circuit.ThreeDim[0]}, }) - extractor.Call1(api, SlicesGadget{ + abstractor.Call1(api, SlicesGadget{ TwoDim: [][]frontend.Variable{{circuit.TwoDim[1][1]}, {circuit.TwoDim[1][0]}}, ThreeDim: [][][]frontend.Variable{circuit.ThreeDim[1], circuit.ThreeDim[0], circuit.ThreeDim[1]}, }) - extractor.Call1(api, SlicesGadget{ + abstractor.Call1(api, SlicesGadget{ TwoDim: [][]frontend.Variable{circuit.TwoDim[1], {circuit.TwoDim[1][0], circuit.TwoDim[0][0], circuit.TwoDim[1][1]}}, ThreeDim: circuit.ThreeDim, }) - extractor.Call2(api, TwoSlices{ + abstractor.Call2(api, TwoSlices{ TwoDim: circuit.TwoDim, }) - a := extractor.Call3(api, ThreeSlices{ + a := abstractor.Call3(api, ThreeSlices{ ThreeDim: circuit.ThreeDim, }) - b := extractor.Call3(api, ThreeSlices{ + b := abstractor.Call3(api, ThreeSlices{ ThreeDim: a, }) - extractor.Call3(api, ThreeSlices{ + abstractor.Call3(api, ThreeSlices{ ThreeDim: b, }) return nil } -func (circuit SlicesOptimisation) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestSlicesOptimisation(t *testing.T) { depthOne := 2 depthTwo := 3 diff --git a/extractor/test/to_binary_circuit_test.go b/extractor/test/to_binary_circuit_test.go index b6575bc..9a34f65 100644 --- a/extractor/test/to_binary_circuit_test.go +++ b/extractor/test/to_binary_circuit_test.go @@ -15,7 +15,7 @@ type OptimisedVectorGadget struct { In frontend.Variable } -func (gadget OptimisedVectorGadget) DefineGadget(api abstractor.API) interface{} { +func (gadget OptimisedVectorGadget) DefineGadget(api frontend.API) interface{} { return api.ToBinary(gadget.In, 3) } @@ -26,7 +26,7 @@ type VectorGadget struct { Nested [][]frontend.Variable } -func (gadget VectorGadget) DefineGadget(api abstractor.API) interface{} { +func (gadget VectorGadget) DefineGadget(api frontend.API) interface{} { var sum frontend.Variable for i := 0; i < len(gadget.In_1); i++ { sum = api.Mul(gadget.In_1[i], gadget.In_2[i]) @@ -40,22 +40,18 @@ type ToBinaryCircuit struct { Double [][]frontend.Variable `gnark:",public"` } -func (circuit *ToBinaryCircuit) AbsDefine(api abstractor.API) error { +func (circuit *ToBinaryCircuit) Define(api frontend.API) error { bin := api.ToBinary(circuit.In, 3) bout := api.ToBinary(circuit.Out, 3) api.Add(circuit.Double[2][2], circuit.Double[1][1], circuit.Double[0][0]) api.Mul(bin[1], bout[1]) - d := extractor.Call1(api, VectorGadget{circuit.Double[2][:], circuit.Double[0][:], circuit.Double}) + d := abstractor.Call1(api, VectorGadget{circuit.Double[2][:], circuit.Double[0][:], circuit.Double}) api.Mul(d[2], d[1]) return nil } -func (circuit ToBinaryCircuit) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestGadgetExtraction(t *testing.T) { dim_1 := 3 dim_2 := 3 diff --git a/extractor/test/two_gadgets_test.go b/extractor/test/two_gadgets_test.go index 1451b32..a996cc8 100644 --- a/extractor/test/two_gadgets_test.go +++ b/extractor/test/two_gadgets_test.go @@ -17,7 +17,7 @@ type MyWidget struct { Num uint32 } -func (gadget MyWidget) DefineGadget(api abstractor.API) interface{} { +func (gadget MyWidget) DefineGadget(api frontend.API) interface{} { sum := api.Add(gadget.Test_1, gadget.Test_2) mul := api.Mul(gadget.Test_1, gadget.Test_2) r := api.Div(sum, mul) @@ -31,9 +31,9 @@ type MySecondWidget struct { Num int } -func (gadget MySecondWidget) DefineGadget(api abstractor.API) interface{} { +func (gadget MySecondWidget) DefineGadget(api frontend.API) interface{} { mul := api.Mul(gadget.Test_1, gadget.Test_2) - snd := extractor.Call(api, MyWidget{gadget.Test_1, gadget.Test_2, uint32(gadget.Num)}) + snd := abstractor.Call(api, MyWidget{gadget.Test_1, gadget.Test_2, uint32(gadget.Num)}) api.Mul(mul, snd) return nil } @@ -44,17 +44,13 @@ type TwoGadgets struct { Num int } -func (circuit *TwoGadgets) AbsDefine(api abstractor.API) error { +func (circuit *TwoGadgets) Define(api frontend.API) error { sum := api.Add(circuit.In_1, circuit.In_2) prod := api.Mul(circuit.In_1, circuit.In_2) - extractor.CallVoid(api, MySecondWidget{sum, prod, circuit.Num}) + abstractor.CallVoid(api, MySecondWidget{sum, prod, circuit.Num}) return nil } -func (circuit TwoGadgets) Define(api frontend.API) error { - return abstractor.Concretize(api, &circuit) -} - func TestTwoGadgets(t *testing.T) { assignment := TwoGadgets{Num: 11} out, err := extractor.CircuitToLean(&assignment, ecc.BN254)