Skip to content

Commit

Permalink
Merge pull request #382 from Consensys/380-support-defpurefun
Browse files Browse the repository at this point in the history
feat: initial support for `corset` syntax
  • Loading branch information
DavePearce authored Nov 24, 2024
2 parents b256e1b + 309e3e1 commit 07162ac
Show file tree
Hide file tree
Showing 81 changed files with 3,614 additions and 1,554 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ linters-settings:
severity: warning
confidence: 0.8
rules:
- name: indent-error-flow
severity: warning
- name: errorf
severity: warning
- name: context-as-argument
Expand Down
6 changes: 5 additions & 1 deletion cmd/testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
cmdutil "github.com/consensys/go-corset/pkg/cmd"
"github.com/consensys/go-corset/pkg/corset"
"github.com/consensys/go-corset/pkg/hir"
sc "github.com/consensys/go-corset/pkg/schema"
"github.com/consensys/go-corset/pkg/sexp"
tr "github.com/consensys/go-corset/pkg/trace"
"github.com/consensys/go-corset/pkg/trace/json"
"github.com/consensys/go-corset/pkg/util"
Expand Down Expand Up @@ -185,8 +187,10 @@ func readSchemaFile(filename string) *hir.Schema {
fmt.Println(err)
os.Exit(1)
}
// Package up as source file
srcfile := sexp.NewSourceFile(filename, bytes)
// Attempt to parse schema
schema, err2 := hir.ParseSchemaString(string(bytes))
schema, err2 := corset.CompileSourceFile(srcfile)
// Check whether parsed successfully or not
if err2 == nil {
// Ok
Expand Down
2 changes: 2 additions & 0 deletions pkg/binfile/computation.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func addInterleavedComputation(c *jsonInterleavedComputation, index uint,
// Update the column type
dst_type = sc.Join(dst_type, src_col.Type())
}
// Update multiplier
ctx = ctx.Multiply(uint(len(sources)))
// Finally, add the sorted permutation assignment
schema.AddAssignment(assignment.NewInterleaving(ctx, dst_hnd.column, sources, dst_type))
// Update allocation information.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var checkCmd = &cobra.Command{
//
stats := util.NewPerfStats()
// Parse constraints
hirSchema = readSchemaFile(args[1])
hirSchema = readSchema(args[1:])
//
stats.Log("Reading constraints file")
// Parse trace file
Expand Down Expand Up @@ -136,7 +136,7 @@ func checkTrace(ir string, cols []tr.RawColumn, schema sc.Schema, cfg checkConfi
for n := cfg.padding.Left; n <= cfg.padding.Right; n++ {
stats := util.NewPerfStats()
trace, errs := builder.Padding(n).Build(cols)

// Log cost of expansion
stats.Log("Expanding trace columns")
// Report any errors
reportErrors(cfg.strict, ir, errs)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ var debugCmd = &cobra.Command{
air := GetFlag(cmd, "air")
stats := GetFlag(cmd, "stats")
// Parse constraints
hirSchema := readSchemaFile(args[0])

hirSchema := readSchema(args)
// Print constraints
if stats {
printStats(hirSchema, hir, mir, air)
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var testCmd = &cobra.Command{
//
stats := util.NewPerfStats()
// Parse constraints
hirSchema = readSchemaFile(args[0])
hirSchema = readSchema(args)
//
stats.Log("Reading constraints file")
//
Expand Down
89 changes: 56 additions & 33 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/consensys/go-corset/pkg/binfile"
"github.com/consensys/go-corset/pkg/corset"
"github.com/consensys/go-corset/pkg/hir"
"github.com/consensys/go-corset/pkg/sexp"
"github.com/consensys/go-corset/pkg/trace"
Expand Down Expand Up @@ -133,54 +134,76 @@ func readTraceFile(filename string) []trace.RawColumn {
return nil
}

// Parse a constraints schema file using a parser based on the extension of the
// filename.
func readSchemaFile(filename string) *hir.Schema {
func readSchema(filenames []string) *hir.Schema {
if len(filenames) == 0 {
fmt.Println("source or binary constraint(s) file required.")
os.Exit(5)
} else if len(filenames) == 1 && path.Ext(filenames[0]) == "bin" {
// Single (binary) file supplied
return readBinaryFile(filenames[0])
}
// Must be source files
return readSourceFiles(filenames)
}

// Read a "bin" file.
func readBinaryFile(filename string) *hir.Schema {
var schema *hir.Schema
// Read schema file
bytes, err := os.ReadFile(filename)
// Handle errors
if err == nil {
// Check file extension
ext := path.Ext(filename)
//
switch ext {
case ".lisp":
// Parse bytes into an S-Expression
schema, err = hir.ParseSchemaString(string(bytes))
if err == nil {
return schema
}
case ".bin":
schema, err = binfile.HirSchemaFromJson(bytes)
if err == nil {
return schema
}
default:
err = fmt.Errorf("Unknown schema file format: %s\n", ext)
// Read the binary file
schema, err = binfile.HirSchemaFromJson(bytes)
if err == nil {
return schema
}
}
// Handle error
if e, ok := err.(*sexp.SyntaxError); ok {
printSyntaxError(filename, e, string(bytes))
} else {
fmt.Println(err)
}

// Handle error & exit
fmt.Println(err)
os.Exit(2)
// unreachable
return nil
}

// Parse a set of source files and compile them into a single schema. This can
// result, for example, in a syntax error, etc.
func readSourceFiles(filenames []string) *hir.Schema {
srcfiles := make([]*sexp.SourceFile, len(filenames))
// Read each file
for i, n := range filenames {
// Read source file
bytes, err := os.ReadFile(n)
// Sanity check for errors
if err != nil {
fmt.Println(err)
os.Exit(3)
}
//
srcfiles[i] = sexp.NewSourceFile(n, bytes)
}
// Parse and compile source files
schema, errs := corset.CompileSourceFiles(srcfiles)
// Check for any errors
if len(errs) == 0 {
return schema
}
// Report errors
for _, err := range errs {
printSyntaxError(&err)
}
// Fail
os.Exit(4)
// unreachable
return nil
}

// Print a syntax error with appropriate highlighting.
func printSyntaxError(filename string, err *sexp.SyntaxError, text string) {
func printSyntaxError(err *sexp.SyntaxError) {
span := err.Span()
// Construct empty source map in order to determine enclosing line.
srcmap := sexp.NewSourceMap[sexp.SExp]([]rune(text))
//
line := srcmap.FindFirstEnclosingLine(span)
line := err.FirstEnclosingLine()
// Print error + line number
fmt.Printf("%s:%d: %s\n", filename, line.Number(), err.Message())
fmt.Printf("%s:%d: %s\n", err.SourceFile().Filename(), line.Number(), err.Message())
// Print separator line
fmt.Println()
// Print line
Expand Down
Loading

0 comments on commit 07162ac

Please sign in to comment.