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

clean: remove deadcode and kill api.Tag and api.Counter #353

Merged
merged 12 commits into from
Aug 3, 2022
25 changes: 1 addition & 24 deletions debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,12 @@
package debug

import (
"errors"
"path/filepath"
"runtime"
"strconv"
"strings"
)

type StackLine struct {
Line uint32
File string
}

// ParseStack parses a stack as stored in a log entry and return readable data
func ParseStack(stack []uint64, stackPaths map[uint32]string) ([]StackLine, error) {
r := make([]StackLine, len(stack))

for i, s := range stack {
pID := uint32(s >> 32)
line := uint32(s)
path, ok := stackPaths[pID]
if !ok {
return nil, errors.New("missing stack path in stackPaths map")
}
r[i] = StackLine{Line: line, File: path}
}

return r, nil
}

func Stack() string {
var sbb strings.Builder
WriteStack(&sbb)
Expand All @@ -41,7 +18,7 @@ func WriteStack(sbb *strings.Builder, forceClean ...bool) {
// we stop when func name == Define as it is where the gnark circuit code should start

// Ask runtime.Callers for up to 10 pcs
pc := make([]uintptr, 10)
pc := make([]uintptr, 20)
n := runtime.Callers(3, pc)
if n == 0 {
// No pcs available. Stop now.
Expand Down
46 changes: 0 additions & 46 deletions debug/debug_test.go

This file was deleted.

8 changes: 0 additions & 8 deletions frontend/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,6 @@ type API interface {
// Deprecated: use api.Compiler().NewHint() instead
NewHint(f hint.Function, nbOutputs int, inputs ...Variable) ([]Variable, error)

// Tag is a shorcut to api.Compiler().Tag()
// Deprecated: use api.Compiler().Tag() instead
Tag(name string) Tag

// AddCounter is a shorcut to api.Compiler().AddCounter()
// Deprecated: use api.Compiler().AddCounter() instead
AddCounter(from, to Tag)

// ConstantValue is a shorcut to api.Compiler().ConstantValue()
// Deprecated: use api.Compiler().ConstantValue() instead
ConstantValue(v Variable) (*big.Int, bool)
Expand Down
9 changes: 0 additions & 9 deletions frontend/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ type Compiler interface {
// If nbOutputs is specified, it must be >= 1 and <= f.NbOutputs
NewHint(f hint.Function, nbOutputs int, inputs ...Variable) ([]Variable, error)

// Tag creates a tag at a given place in a circuit. The state of the tag may contain informations needed to
// measure constraints, variables and coefficients creations through AddCounter
Tag(name string) Tag

// AddCounter measures the number of constraints, variables and coefficients created between two tags
// note that the PlonK statistics are contextual since there is a post-compile phase where linear expressions
// are factorized. That is, measuring 2 times the "repeating" piece of circuit may give less constraints the second time
AddCounter(from, to Tag)

// ConstantValue returns the big.Int value of v and true if op is a success.
// nil and false if failure. This API returns a boolean to allow for future refactoring
// replacing *big.Int with fr.Element
Expand Down
10 changes: 0 additions & 10 deletions frontend/ccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/frontend/compiled"
"github.com/consensys/gnark/frontend/schema"
)

Expand All @@ -40,17 +39,8 @@ type CompiledConstraintSystem interface {

CurveID() ecc.ID

// GetCounters return the collected constraint counters, if any
GetCounters() []compiled.Counter

GetSchema() *schema.Schema

// GetConstraints return a human readable representation of the constraints
GetConstraints() [][]string

// GetDebugInfo return the list of debug info per constraints and the map to restore file path
// from the debug info. When compiled with -tags=debug, each constraint has an associated
// stack encoded as a []uint64. The uint64 pack uint32(fileID) | uint32(lineNumber).
// The file string can be retrieved from associated map.
GetDebugInfo() ([][]uint64, map[uint32]string)
}
95 changes: 2 additions & 93 deletions frontend/compiled/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ package compiled
import (
"fmt"
"math/big"
"runtime"
"strings"

"github.com/blang/semver/v4"
"github.com/consensys/gnark"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/hint"
"github.com/consensys/gnark/debug"
"github.com/consensys/gnark/frontend/schema"
Expand Down Expand Up @@ -41,18 +39,11 @@ type ConstraintSystem struct {
// debug info contains stack trace (including line number) of a call to a cs.API that
// results in an unsolved constraint
DebugInfo []LogEntry
// maps unique id to a path (optimized for reading debug info stacks)
DebugStackPaths map[uint32]string
// maps a path to an id (optimized for storing debug info stacks)
debugPathsIds map[string]uint32 `cbor:"-"`
debugPathId uint32 `cbor:"-"`

// maps constraint id to debugInfo id
// several constraints may point to the same debug info
MDebug map[int]int

Counters []Counter `cbor:"-"`

MHints map[int]*Hint // maps wireID to hint
MHintsDependencies map[hint.ID]string // maps hintID to hint string identifier

Expand All @@ -74,8 +65,6 @@ func NewConstraintSystem(scalarField *big.Int) ConstraintSystem {
MDebug: make(map[int]int),
MHints: make(map[int]*Hint),
MHintsDependencies: make(map[hint.ID]string),
DebugStackPaths: make(map[uint32]string),
debugPathsIds: make(map[string]uint32),
q: new(big.Int).Set(scalarField),
bitLen: scalarField.BitLen(),
}
Expand Down Expand Up @@ -123,36 +112,12 @@ func (cs *ConstraintSystem) Field() *big.Int {
return new(big.Int).Set(cs.q)
}

// GetCounters return the collected constraint counters, if any
func (cs *ConstraintSystem) GetCounters() []Counter { return cs.Counters }

func (cs *ConstraintSystem) GetSchema() *schema.Schema { return cs.Schema }

// Counter contains measurements of useful statistics between two Tag
type Counter struct {
From, To string
NbVariables int
NbConstraints int
BackendID backend.ID
}

func (c Counter) String() string {
return fmt.Sprintf("%s %s - %s: %d variables, %d constraints", c.BackendID, c.From, c.To, c.NbVariables, c.NbConstraints)
}

func (cs *ConstraintSystem) AddDebugInfo(errName string, i ...interface{}) int {

var l LogEntry

// add the stack info
// TODO @gbotrel duplicate with Debug.stack below
l.Stack = cs.stack()

if errName == "" {
cs.DebugInfo = append(cs.DebugInfo, l)
return len(cs.DebugInfo) - 1
}

const minLogSize = 500
var sbb strings.Builder
sbb.Grow(minLogSize)
Expand Down Expand Up @@ -180,6 +145,8 @@ func (cs *ConstraintSystem) AddDebugInfo(errName string, i ...interface{}) int {
}
}
sbb.WriteByte('\n')
// TODO this stack should not be stored as string, but as a slice of locations
// to avoid overloading with lots of str duplicate the serialized constraint system
debug.WriteStack(&sbb)
l.Format = sbb.String()

Expand All @@ -192,61 +159,3 @@ func (cs *ConstraintSystem) AddDebugInfo(errName string, i ...interface{}) int {
func (cs *ConstraintSystem) FieldBitLen() int {
return cs.bitLen
}

func (cs *ConstraintSystem) GetDebugInfo() ([][]uint64, map[uint32]string) {
r := make([][]uint64, len(cs.DebugInfo))
for _, l := range cs.DebugInfo {
r = append(r, l.Stack)
}
return r, cs.DebugStackPaths
}

func (cs *ConstraintSystem) stack() (r []uint64) {
if !debug.Debug {
return
}
// derived from: https://golang.org/pkg/runtime/#example_Frames
// we stop when func name == Define as it is where the gnark circuit code should start

// Ask runtime.Callers for up to 10 pcs
pc := make([]uintptr, 10)
n := runtime.Callers(3, pc)
if n == 0 {
// No pcs available. Stop now.
// This can happen if the first argument to runtime.Callers is large.
return
}
pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
frames := runtime.CallersFrames(pc)
// Loop to get frames.
// A fixed number of pcs can expand to an indefinite number of Frames.
for {
frame, more := frames.Next()
fe := strings.Split(frame.Function, "/")
function := fe[len(fe)-1]
file := frame.File

if strings.Contains(frame.File, "gnark/frontend") {
continue
}

// TODO @gbotrel this stores an absolute path, so will work only locally
id, ok := cs.debugPathsIds[file]
if !ok {
id = cs.debugPathId
cs.debugPathId++
cs.debugPathsIds[file] = id
cs.DebugStackPaths[id] = file
}
r = append(r, ((uint64(id) << 32) | uint64(frame.Line)))
if !more {
break
}
if strings.HasSuffix(function, "Define") {
break
}
}

return

}
5 changes: 0 additions & 5 deletions frontend/compiled/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ type LogEntry struct {
Caller string
Format string
ToResolve []Term

// When compiled with -tags=debug, each constraint has an associated
// stack encoded as a []uint64. The uint64 pack uint32(fileID) | uint32(lineNumber).
// The actual string describing the file is stored in a map in the compiled.ConstraintSystem.
Stack []uint64
}

func (l *LogEntry) WriteVariable(le LinearExpression, sbb *strings.Builder) {
Expand Down
30 changes: 0 additions & 30 deletions frontend/counter.go

This file was deleted.

26 changes: 0 additions & 26 deletions frontend/cs/r1cs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@ import (
"errors"
"fmt"
"math/big"
"path/filepath"
"reflect"
"runtime"
"sort"
"strconv"
"strings"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/hint"
"github.com/consensys/gnark/debug"
"github.com/consensys/gnark/frontend"
Expand Down Expand Up @@ -572,29 +569,6 @@ func (system *r1cs) toVariables(in ...frontend.Variable) ([]compiled.LinearExpre
return r, s
}

// Tag creates a tag at a given place in a circuit. The state of the tag may contain informations needed to
// measure constraints, variables and coefficients creations through AddCounter
func (system *r1cs) Tag(name string) frontend.Tag {
_, file, line, _ := runtime.Caller(1)

return frontend.Tag{
Name: fmt.Sprintf("%s[%s:%d]", name, filepath.Base(file), line),
VID: system.NbInternalVariables,
CID: len(system.Constraints),
}
}

// AddCounter measures the number of constraints, variables and coefficients created between two tags
func (system *r1cs) AddCounter(from, to frontend.Tag) {
system.Counters = append(system.Counters, compiled.Counter{
From: from.Name,
To: to.Name,
NbVariables: to.VID - from.VID,
NbConstraints: to.CID - from.CID,
BackendID: backend.GROTH16,
})
}

// NewHint initializes internal variables whose value will be evaluated using
// the provided hint function at run time from the inputs. Inputs must be either
// variables or convertible to *big.Int. The function returns an error if the
Expand Down
Loading