Skip to content

Commit cb4a9b1

Browse files
committed
make top-level cmds and testing framework generic
This makes the various top-level commands for the CLI, along with the testing framework field agnostic. At some level, this means we are now fully field agnostic. However, there remains a number of things needing to be tidied up before this is fully realised.
1 parent 9eb873d commit cb4a9b1

File tree

23 files changed

+617
-349
lines changed

23 files changed

+617
-349
lines changed

pkg/cmd/check.go

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,55 @@ var checkCmd = &cobra.Command{
4747
Traces can be given either as JSON or binary lt files.
4848
Constraints can be given either as lisp or bin files.`,
4949
Run: func(cmd *cobra.Command, args []string) {
50-
var cfg checkConfig
51-
52-
if len(args) < 2 {
53-
fmt.Println(cmd.UsageString())
54-
os.Exit(1)
55-
}
56-
// Configure log level
57-
if GetFlag(cmd, "verbose") {
58-
log.SetLevel(log.DebugLevel)
59-
}
60-
// Configure CPU profiling (if requested)
61-
startCpuProfiling(cmd)
62-
//
63-
batched := GetFlag(cmd, "batched")
64-
//
65-
cfg.padding.Right = GetUint(cmd, "padding")
66-
cfg.report = GetFlag(cmd, "report")
67-
cfg.reportPadding = GetUint(cmd, "report-context")
68-
cfg.reportCellWidth = GetUint(cmd, "report-cellwidth")
69-
cfg.reportTitleWidth = GetUint(cmd, "report-titlewidth")
70-
cfg.ansiEscapes = GetFlag(cmd, "ansi-escapes")
71-
// TODO: support true ranges
72-
cfg.padding.Left = cfg.padding.Right
73-
// Read in constraint files
74-
schemas := *getSchemaStack(cmd, SCHEMA_DEFAULT_AIR, args[1:]...)
75-
// enable / disable coverage
76-
if covfile := GetString(cmd, "coverage"); covfile != "" {
77-
cfg.coverage = util.Some(covfile)
78-
}
79-
//
80-
tracefile := args[0]
81-
//
82-
checkWithLegacyPipeline(cfg, batched, tracefile, schemas)
83-
// Write memory profiling (if requested)
84-
writeMemProfile(cmd)
85-
// Stop cpu profiling (if was requested)
86-
stopCpuProfiling(cmd)
50+
runFieldAgnosticCmd(cmd, args, checkCmds)
8751
},
8852
}
8953

54+
// Available instances
55+
var checkCmds = []FieldAgnosticCmd{
56+
FieldAgnosticCmd{sc.BLS12_377, runCheckCmd[bls12_377.Element]},
57+
}
58+
59+
func runCheckCmd[F field.Element[F]](cmd *cobra.Command, args []string) {
60+
var cfg checkConfig
61+
62+
if len(args) < 2 {
63+
fmt.Println(cmd.UsageString())
64+
os.Exit(1)
65+
}
66+
// Configure log level
67+
if GetFlag(cmd, "verbose") {
68+
log.SetLevel(log.DebugLevel)
69+
}
70+
// Configure CPU profiling (if requested)
71+
startCpuProfiling(cmd)
72+
//
73+
batched := GetFlag(cmd, "batched")
74+
//
75+
cfg.padding.Right = GetUint(cmd, "padding")
76+
cfg.report = GetFlag(cmd, "report")
77+
cfg.reportPadding = GetUint(cmd, "report-context")
78+
cfg.reportCellWidth = GetUint(cmd, "report-cellwidth")
79+
cfg.reportTitleWidth = GetUint(cmd, "report-titlewidth")
80+
cfg.ansiEscapes = GetFlag(cmd, "ansi-escapes")
81+
// TODO: support true ranges
82+
cfg.padding.Left = cfg.padding.Right
83+
// Read in constraint files
84+
schemas := *getSchemaStack[F](cmd, SCHEMA_DEFAULT_AIR, args[1:]...)
85+
// enable / disable coverage
86+
if covfile := GetString(cmd, "coverage"); covfile != "" {
87+
cfg.coverage = util.Some(covfile)
88+
}
89+
//
90+
tracefile := args[0]
91+
//
92+
checkWithLegacyPipeline(cfg, batched, tracefile, schemas)
93+
// Write memory profiling (if requested)
94+
writeMemProfile(cmd)
95+
// Stop cpu profiling (if was requested)
96+
stopCpuProfiling(cmd)
97+
}
98+
9099
func startCpuProfiling(cmd *cobra.Command) {
91100
if filename := GetString(cmd, "cpuprof"); filename != "" {
92101
f, err := os.Create(filename)
@@ -148,8 +157,8 @@ type checkConfig struct {
148157
}
149158

150159
// Check raw constraints using the legacy pipeline.
151-
func checkWithLegacyPipeline(cfg checkConfig, batched bool, tracefile string,
152-
schemas cmd_util.SchemaStack[bls12_377.Element]) {
160+
func checkWithLegacyPipeline[F field.Element[F]](cfg checkConfig, batched bool, tracefile string,
161+
schemas cmd_util.SchemaStack[F]) {
153162
//
154163
var (
155164
traces []lt.TraceFile
@@ -182,8 +191,8 @@ func checkWithLegacyPipeline(cfg checkConfig, batched bool, tracefile string,
182191
}
183192
}
184193

185-
func checkTrace(ir string, traces []lt.TraceFile, schema sc.AnySchema[bls12_377.Element],
186-
builder ir.TraceBuilder[bls12_377.Element], cfg checkConfig) bool {
194+
func checkTrace[F field.Element[F]](ir string, traces []lt.TraceFile, schema sc.AnySchema[F],
195+
builder ir.TraceBuilder[F], cfg checkConfig) bool {
187196
//
188197
for _, tf := range traces {
189198
for n := cfg.padding.Left; n <= cfg.padding.Right; n++ {
@@ -214,7 +223,7 @@ func checkTrace(ir string, traces []lt.TraceFile, schema sc.AnySchema[bls12_377.
214223
}
215224

216225
// Report constraint failures, whilst providing contextual information (when requested).
217-
func reportFailures(ir string, failures []sc.Failure, trace tr.Trace[bls12_377.Element], cfg checkConfig) {
226+
func reportFailures[F field.Element[F]](ir string, failures []sc.Failure, trace tr.Trace[F], cfg checkConfig) {
218227
errs := make([]error, len(failures))
219228
for i, f := range failures {
220229
errs[i] = errors.New(f.Message())

pkg/cmd/compile.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import (
1717
"os"
1818
"strings"
1919

20+
sc "github.com/consensys/go-corset/pkg/schema"
2021
"github.com/consensys/go-corset/pkg/util/collection/typed"
22+
"github.com/consensys/go-corset/pkg/util/field"
23+
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
2124
log "github.com/sirupsen/logrus"
2225
"github.com/spf13/cobra"
2326
)
@@ -28,24 +31,34 @@ var compileCmd = &cobra.Command{
2831
Long: `Compile a given set of constraint file(s) into a single binary package which can
2932
be subsequently used without requiring a full compilation step.`,
3033
Run: func(cmd *cobra.Command, args []string) {
31-
// Configure log level
32-
if GetFlag(cmd, "verbose") {
33-
log.SetLevel(log.DebugLevel)
34-
}
35-
output := GetString(cmd, "output")
36-
defines := GetStringArray(cmd, "define")
37-
// Parse constraints
38-
binfile := getSchemaStack(cmd, SCHEMA_DEFAULT_MIR, args...).BinaryFile()
39-
// Write metadata
40-
if err := binfile.Header.SetMetaData(buildMetadata(defines)); err != nil {
41-
fmt.Printf("error writing metadata: %s\n", err.Error())
42-
os.Exit(1)
43-
}
44-
// Serialise as a gob file.
45-
WriteBinaryFile(binfile, output)
34+
runFieldAgnosticCmd(cmd, args, compileCmds)
4635
},
4736
}
4837

38+
// Available instances
39+
var compileCmds = []FieldAgnosticCmd{
40+
FieldAgnosticCmd{sc.BLS12_377, runCompileCmd[bls12_377.Element]},
41+
}
42+
43+
func runCompileCmd[F field.Element[F]](cmd *cobra.Command, args []string) {
44+
// Configure log level
45+
if GetFlag(cmd, "verbose") {
46+
log.SetLevel(log.DebugLevel)
47+
}
48+
49+
output := GetString(cmd, "output")
50+
defines := GetStringArray(cmd, "define")
51+
// Parse constraints
52+
binfile := getSchemaStack[F](cmd, SCHEMA_DEFAULT_MIR, args...).BinaryFile()
53+
// Write metadata
54+
if err := binfile.Header.SetMetaData(buildMetadata(defines)); err != nil {
55+
fmt.Printf("error writing metadata: %s\n", err.Error())
56+
os.Exit(1)
57+
}
58+
// Serialise as a gob file.
59+
WriteBinaryFile(binfile, output)
60+
}
61+
4962
func buildMetadata(items []string) typed.Map {
5063
metadata := make(map[string]any)
5164
//

pkg/cmd/debug.go

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
"github.com/consensys/go-corset/pkg/cmd/debug"
2020
cmd_util "github.com/consensys/go-corset/pkg/cmd/util"
2121
"github.com/consensys/go-corset/pkg/corset"
22+
sc "github.com/consensys/go-corset/pkg/schema"
23+
"github.com/consensys/go-corset/pkg/util/field"
2224
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
2325
log "github.com/sirupsen/logrus"
2426
"github.com/spf13/cobra"
@@ -31,50 +33,59 @@ var debugCmd = &cobra.Command{
3133
expansion in order to debug them. Constraints can be given
3234
either as lisp or bin files.`,
3335
Run: func(cmd *cobra.Command, args []string) {
34-
//
35-
if len(args) < 1 {
36-
fmt.Println(cmd.UsageString())
37-
os.Exit(1)
38-
}
39-
// Configure log level
40-
if GetFlag(cmd, "verbose") {
41-
log.SetLevel(log.DebugLevel)
42-
}
43-
stats := GetFlag(cmd, "stats")
44-
attrs := GetFlag(cmd, "attributes")
45-
metadata := GetFlag(cmd, "metadata")
46-
constants := GetFlag(cmd, "constants")
47-
spillage := GetFlag(cmd, "spillage")
48-
textWidth := GetUint(cmd, "textwidth")
49-
// Read in constraint files
50-
schemas := *getSchemaStack(cmd, SCHEMA_DEFAULT_MIR, args...)
51-
// Print constant info (if requested)
52-
if constants {
53-
debug.PrintExternalisedConstants(schemas)
54-
}
55-
// Print spillage info (if requested)
56-
if spillage {
57-
printSpillage(schemas, true)
58-
}
59-
// Print meta-data (if requested)
60-
if metadata {
61-
printBinaryFileHeader(schemas)
62-
}
63-
// Print stats (if requested)
64-
if stats {
65-
debug.PrintStats(schemas)
66-
}
67-
// Print embedded attributes (if requested
68-
if attrs {
69-
printAttributes(schemas)
70-
}
71-
//
72-
if !stats && !attrs {
73-
debug.PrintSchemas(schemas, textWidth)
74-
}
36+
runFieldAgnosticCmd(cmd, args, debugCmds)
7537
},
7638
}
7739

40+
// Available instances
41+
var debugCmds = []FieldAgnosticCmd{
42+
FieldAgnosticCmd{sc.BLS12_377, runDebugCmd[bls12_377.Element]},
43+
}
44+
45+
func runDebugCmd[F field.Element[F]](cmd *cobra.Command, args []string) {
46+
if len(args) < 1 {
47+
fmt.Println(cmd.UsageString())
48+
os.Exit(1)
49+
}
50+
// Configure log level
51+
if GetFlag(cmd, "verbose") {
52+
log.SetLevel(log.DebugLevel)
53+
}
54+
55+
stats := GetFlag(cmd, "stats")
56+
attrs := GetFlag(cmd, "attributes")
57+
metadata := GetFlag(cmd, "metadata")
58+
constants := GetFlag(cmd, "constants")
59+
spillage := GetFlag(cmd, "spillage")
60+
textWidth := GetUint(cmd, "textwidth")
61+
// Read in constraint files
62+
schemas := *getSchemaStack[F](cmd, SCHEMA_DEFAULT_MIR, args...)
63+
// Print constant info (if requested)
64+
if constants {
65+
debug.PrintExternalisedConstants(schemas)
66+
}
67+
// Print spillage info (if requested)
68+
if spillage {
69+
printSpillage(schemas, true)
70+
}
71+
// Print meta-data (if requested)
72+
if metadata {
73+
printBinaryFileHeader(schemas)
74+
}
75+
// Print stats (if requested)
76+
if stats {
77+
debug.PrintStats(schemas)
78+
}
79+
// Print embedded attributes (if requested
80+
if attrs {
81+
printAttributes(schemas)
82+
}
83+
//
84+
if !stats && !attrs {
85+
debug.PrintSchemas(schemas, textWidth)
86+
}
87+
}
88+
7889
func init() {
7990
rootCmd.AddCommand(debugCmd)
8091
debugCmd.Flags().Bool("attributes", false, "Print attribute information")
@@ -85,7 +96,7 @@ func init() {
8596
debugCmd.Flags().Uint("textwidth", 130, "Set maximum textwidth to use")
8697
}
8798

88-
func printAttributes(schemas cmd_util.SchemaStack[bls12_377.Element]) {
99+
func printAttributes[F field.Element[F]](schemas cmd_util.SchemaStack[F]) {
89100
binfile := schemas.BinaryFile()
90101
// Print attributes
91102
for _, attr := range binfile.Attributes {
@@ -97,7 +108,7 @@ func printAttributes(schemas cmd_util.SchemaStack[bls12_377.Element]) {
97108
}
98109
}
99110

100-
func printSpillage(schemas cmd_util.SchemaStack[bls12_377.Element], defensive bool) {
111+
func printSpillage[F field.Element[F]](schemas cmd_util.SchemaStack[F], defensive bool) {
101112
// fmt.Println("Spillage:")
102113
// // Compute spillage for optimisation level
103114
// spillage := determineSpillage(&binf.Schema, defensive, optConfig)
@@ -118,7 +129,7 @@ func printSpillage(schemas cmd_util.SchemaStack[bls12_377.Element], defensive bo
118129
panic("todo")
119130
}
120131

121-
func printBinaryFileHeader(schemas cmd_util.SchemaStack[bls12_377.Element]) {
132+
func printBinaryFileHeader[F field.Element[F]](schemas cmd_util.SchemaStack[F]) {
122133
header := schemas.BinaryFile().Header
123134
//
124135
fmt.Printf("Format: %d.%d\n", header.MajorVersion, header.MinorVersion)

pkg/cmd/debug/constants.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
"github.com/consensys/go-corset/pkg/binfile"
2020
cmd_util "github.com/consensys/go-corset/pkg/cmd/util"
2121
"github.com/consensys/go-corset/pkg/corset"
22-
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
22+
"github.com/consensys/go-corset/pkg/util/field"
2323
)
2424

2525
// PrintExternalisedConstants is responsible for printing any externalised
2626
// constants contained within the given binary file.
27-
func PrintExternalisedConstants(schemas cmd_util.SchemaStack[bls12_377.Element]) {
27+
func PrintExternalisedConstants[F field.Element[F]](schemas cmd_util.SchemaStack[F]) {
2828
binf := schemas.BinaryFile()
2929
//
3030
fmt.Println("External constants:")

pkg/cmd/debug/schema.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ import (
2222
"github.com/consensys/go-corset/pkg/ir/mir"
2323
"github.com/consensys/go-corset/pkg/schema"
2424
"github.com/consensys/go-corset/pkg/util/field"
25-
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
2625
"github.com/consensys/go-corset/pkg/util/source/sexp"
2726
)
2827

2928
// PrintSchemas is responsible for printing out a human-readable description of
3029
// a given schema.
31-
func PrintSchemas(stack cmd_util.SchemaStack[bls12_377.Element], textwidth uint) {
30+
func PrintSchemas[F field.Element[F]](stack cmd_util.SchemaStack[F], textwidth uint) {
3231
//
3332
for _, schema := range stack.AbstractSchemas() {
3433
printSchema(schema, textwidth)

pkg/cmd/debug/stats.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ import (
2323
"github.com/consensys/go-corset/pkg/schema"
2424
sc "github.com/consensys/go-corset/pkg/schema"
2525
"github.com/consensys/go-corset/pkg/util/field"
26-
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
2726
"github.com/consensys/go-corset/pkg/util/termio"
2827
)
2928

3029
// PrintStats is used for printing summary information about a constraint set,
3130
// such as the number and type of constraints, etc.
32-
func PrintStats(stack cmd_util.SchemaStack[bls12_377.Element]) {
31+
func PrintStats[F field.Element[F]](stack cmd_util.SchemaStack[F]) {
3332
var (
3433
schemas = stack.ConcreteSchemas()
35-
summarisers = getSummerisers[bls12_377.Element]()
34+
summarisers = getSummerisers[F]()
3635
//
3736
n = 1 + uint(len(schemas))
3837
m = uint(len(summarisers))

0 commit comments

Comments
 (0)