From ce2961d2c40a99884194ff99f08be4f2f92abaa3 Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Fri, 3 Mar 2023 18:17:15 +0100 Subject: [PATCH 1/4] feat: add callDeferred method to test engine --- test/engine.go | 15 +++++++++++---- test/solver_test.go | 7 ++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/test/engine.go b/test/engine.go index ece99f8bad..e72ff5f8e7 100644 --- a/test/engine.go +++ b/test/engine.go @@ -125,10 +125,8 @@ func IsSolved(circuit, witness frontend.Circuit, field *big.Int, opts ...TestEng if err = c.Define(e); err != nil { return fmt.Errorf("define: %w", err) } - for i, cb := range circuitdefer.GetAll[func(frontend.API) error](e) { - if err = cb(e); err != nil { - return fmt.Errorf("defer %d: %w", i, err) - } + if err = callDeferred(e); err != nil { + return fmt.Errorf("deferred: %w", err) } log.Debug().Uint64("add", cptAdd). @@ -141,6 +139,15 @@ func IsSolved(circuit, witness frontend.Circuit, field *big.Int, opts ...TestEng return } +func callDeferred(builder *engine) error { + for i, cb := range circuitdefer.GetAll[func(frontend.API) error](builder) { + if err := cb(builder); err != nil { + return fmt.Errorf("defer fn %d: %w", i, err) + } + } + return nil +} + var cptAdd, cptMul, cptSub, cptToBinary, cptFromBinary, cptAssertIsEqual uint64 func (e *engine) Add(i1, i2 frontend.Variable, in ...frontend.Variable) frontend.Variable { diff --git a/test/solver_test.go b/test/solver_test.go index 9fb88da92d..af8adbe9c1 100644 --- a/test/solver_test.go +++ b/test/solver_test.go @@ -18,7 +18,6 @@ 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/circuitdefer" "github.com/consensys/gnark/internal/kvstore" "github.com/consensys/gnark/internal/tinyfield" "github.com/consensys/gnark/internal/utils" @@ -209,10 +208,8 @@ func isSolvedEngine(c frontend.Circuit, field *big.Int, opts ...TestEngineOption if err = c.Define(e); err != nil { return fmt.Errorf("define: %w", err) } - for i, cb := range circuitdefer.GetAll[func(frontend.API) error](e) { - if err = cb(e); err != nil { - return fmt.Errorf("defer %d: %w", i, err) - } + if err = callDeferred(e); err != nil { + return fmt.Errorf("") } return From d1e9239579ca9e7e8e6353831c19e9353f1e6382 Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Fri, 3 Mar 2023 18:17:35 +0100 Subject: [PATCH 2/4] fix: return error when deferred function fails --- frontend/compile.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/compile.go b/frontend/compile.go index 439b5802a5..02f0ec384f 100644 --- a/frontend/compile.go +++ b/frontend/compile.go @@ -124,7 +124,7 @@ func parseCircuit(builder Builder, circuit Circuit) (err error) { return fmt.Errorf("define circuit: %w", err) } if err = callDeferred(builder); err != nil { - return fmt.Errorf("") + return fmt.Errorf("deferred: %w", err) } return From ca090d2b386f4f9b945e76cd2414077f504a5884 Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Sat, 4 Mar 2023 00:11:15 +0100 Subject: [PATCH 3/4] fix: always indent witness doctest output witness.ToJSON indents output when run in debug mode and not if in normal mode. But the doctest always expected non-indented output. --- backend/witness/witness_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/backend/witness/witness_test.go b/backend/witness/witness_test.go index ba127b3240..d7feca17a4 100644 --- a/backend/witness/witness_test.go +++ b/backend/witness/witness_test.go @@ -1,6 +1,8 @@ package witness_test import ( + "bytes" + "encoding/json" "fmt" "reflect" "testing" @@ -47,11 +49,17 @@ func ExampleWitness() { // first get the circuit expected schema schema, _ := frontend.NewSchema(assignment) - json, _ := reconstructed.ToJSON(schema) + ret, _ := reconstructed.ToJSON(schema) - fmt.Println(string(json)) + var b bytes.Buffer + json.Indent(&b, ret, "", "\t") + fmt.Println(b.String()) // Output: - // {"X":42,"Y":8000,"E":1} + // { + // "X": 42, + // "Y": 8000, + // "E": 1 + // } } From 44fcb4044ce707d234a87ffc9272bf80f58145a4 Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Sat, 4 Mar 2023 00:12:27 +0100 Subject: [PATCH 4/4] fix: further specify skipped stack frames in normal mode The function `callDeferred` is in `gnark/frontend`. We do not want to skip it as it is a break condition. --- debug/debug.go | 2 +- debug/symbol_table.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/debug/debug.go b/debug/debug.go index 95e1079768..32b891c38a 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -56,7 +56,7 @@ func writeStack(sbb *strings.Builder, forceClean ...bool) { if strings.Contains(frame.File, "test/engine.go") { continue } - if strings.Contains(frame.File, "gnark/frontend") { + if strings.Contains(frame.File, "gnark/frontend/cs") { continue } file = filepath.Base(file) diff --git a/debug/symbol_table.go b/debug/symbol_table.go index 6ce2f68bb3..dc25c3e8d8 100644 --- a/debug/symbol_table.go +++ b/debug/symbol_table.go @@ -61,7 +61,7 @@ func (st *SymbolTable) CollectStack() []int { if strings.Contains(frame.File, "test/engine.go") { continue } - if strings.Contains(frame.File, "gnark/frontend") { + if strings.Contains(frame.File, "gnark/frontend/cs") { continue } frame.File = filepath.Base(frame.File)