Skip to content

Commit

Permalink
Merge branch 'main' into feat/issue-156/mmap
Browse files Browse the repository at this point in the history
  • Loading branch information
powerslider authored Jun 13, 2024
2 parents ae5868f + 8a12829 commit 0500dd5
Show file tree
Hide file tree
Showing 75 changed files with 594 additions and 504 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '>=1.20'
- run: make test

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '>=1.20'
# Setup environment
- run: make install
# Do the linting
- run: make lint
File renamed without changes.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ repos:
rev: v1.57.1
hooks:
- id: golangci-lint
args: [--config=.golangci-lint.yml]
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.17
hooks:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ all: clean lint test build
.PHONY: lint
lint:
@echo ">>> Performing golang code linting.."
golangci-lint run --config=.golangci-lint.yml
golangci-lint run --config=.golangci.yml

.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion pkg/air/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *Mul) Equate(other Expr) Expr { return &Sub{Args: []Expr{p, other}} }
// Constant represents a constant value within an expression.
type Constant struct{ Value *fr.Element }

// NewConstant construct an AIR expression representing a given constant.
// NewConst construct an AIR expression representing a given constant.
func NewConst(val *fr.Element) Expr {
return &Constant{val}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/air/gadgets/normalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"github.com/consensys/go-corset/pkg/table"
)

// Norm constructs an expression representing the normalised value of e. That is,
// an expression which is 0 when e is 0, and 1 when e is non-zero. This is done
// by introducing a synthetic column to hold the (pseudo) mutliplicative inverse
// of e.
// Normalise constructs an expression representing the normalised value of e.
// That is, an expression which is 0 when e is 0, and 1 when e is non-zero.
// This is done by introducing a synthetic column to hold the (pseudo)
// mutliplicative inverse of e.
func Normalise(e air.Expr, tbl *air.Schema) air.Expr {
// Construct pseudo multiplicative inverse of e.
ie := ApplyPseudoInverseGadget(e, tbl)
Expand Down
3 changes: 3 additions & 0 deletions pkg/air/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func (p *Schema) Accepts(trace table.Trace) error {
// columns. Observe that computed columns have to be computed in the correct
// order.
func (p *Schema) ExpandTrace(tr table.Trace) error {
// Insert initial padding row
table.PadTrace(1, tr)
// Execute all computations
for _, c := range p.computations {
err := c.ExpandTrace(tr)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/hir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func (p *Schema) Accepts(trace table.Trace) error {

// ExpandTrace expands a given mmap according to this schema.
func (p *Schema) ExpandTrace(tr table.Trace) error {
// Insert initial padding row
table.PadTrace(1, tr)
// Expand all the permutation columns
for _, perm := range p.permutations {
err := perm.ExpandTrace(tr)
Expand Down
2 changes: 2 additions & 0 deletions pkg/mir/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ func lowerPermutationToAir(c Permutation, mirSchema *Schema, airSchema *air.Sche

// ExpandTrace expands a given mmap according to this schema.
func (p *Schema) ExpandTrace(tr table.Trace) error {
// Insert initial padding row
table.PadTrace(1, tr)
// Expand all the permutation columns
for _, perm := range p.permutations {
err := perm.ExpandTrace(tr)
Expand Down
1 change: 1 addition & 0 deletions pkg/table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (c *DataColumn[T]) Accepts(tr Trace) error {
return nil
}

//nolint:revive
func (c *DataColumn[T]) String() string {
if c.Type.AsField() != nil {
return fmt.Sprintf("(column %s)", c.Name)
Expand Down
6 changes: 6 additions & 0 deletions pkg/table/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (p ZeroTest[E]) TestAt(row int, tr Trace) bool {
return val == nil || val.IsZero()
}

// String generates a human-readble string.
//
//nolint:revive
func (p ZeroTest[E]) String() string {
return fmt.Sprintf("%s", any(p.Expr))
}
Expand Down Expand Up @@ -130,6 +133,9 @@ func HoldsLocally[T Testable](k int, handle string, constraint T, tr Trace) erro
return nil
}

// String generates a human-readble string.
//
//nolint:revive
func (p *RowConstraint[T]) String() string {
if p.Domain == nil {
return fmt.Sprintf("(vanish %s %s)", p.Handle, any(p.Constraint))
Expand Down
95 changes: 80 additions & 15 deletions pkg/table/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ type Acceptable interface {
// Trace describes a set of named columns. Columns are not required to have the
// same height and can be either "data" columns or "computed" columns.
type Trace interface {
// Determine the height of this table, which is defined as the
// height of the largest column.
Height() int
// Get the number of columns in this trace.
Width() int
// Add a new column of data
AddColumn(name string, data []*fr.Element)
// Get the name of the ith column in this trace.
ColumnName(int) string
// Duplicate the first row of this trace n times, whilst placing the
// duplicates at the beginning of the trace. This can be used, for example,
// to apply padding to an existing trace. Note it is an error to call this
// on an empty trace.
DuplicateFront(n int)
// ColumnByName returns the data of a given column in order that it can be
// inspected. If the given column does not exist, then nil is returned.
ColumnByName(name string) []*fr.Element
// Check whether this trace contains data for the given column.
HasColumn(name string) bool
// Get the value of a given column by its name. If the column
// does not exist or if the index is out-of-bounds then an
// error is returned.
Expand All @@ -39,13 +46,14 @@ type Trace interface {
// does not exist or if the index is out-of-bounds then an
// error is returned.
GetByIndex(col int, row int) *fr.Element
// Check whether this trace contains data for the given column.
HasColumn(name string) bool
// ColumnByName returns the data of a given column in order that it can be
// inspected. If the given column does not exist, then nil is returned.
ColumnByName(name string) []*fr.Element
// Add a new column of data
AddColumn(name string, data []*fr.Element)
// Insert n copies of a given row at the front of this trace using a given
// mapping function to initialise each column. Note, n cannot be negative.
InsertFront(n int, mapping func(int) *fr.Element)
// Determine the height of this table, which is defined as the
// height of the largest column.
Height() int
// Get the number of columns in this trace.
Width() int
}

// ConstraintsAcceptTrace determines whether or not one or more groups of
Expand All @@ -62,6 +70,15 @@ func ConstraintsAcceptTrace[T Acceptable](trace Trace, constraints []T) error {
return nil
}

// PadTrace adds n rows of padding to the given trace by duplicating the first
// row n times. This requires that a first row exists. Furthermore, we cannot
// pad a negative number of rows (i.e. when n < 0).
func PadTrace(n int, tr Trace) {
var zero fr.Element = fr.NewElement((0))
// Insert initial padding row
tr.InsertFront(n, func(index int) *fr.Element { return &zero })
}

// ===================================================================
// Array Trace
// ===================================================================
Expand Down Expand Up @@ -143,9 +160,14 @@ func (p *ArrayTrace) Columns() []*ArrayTraceColumn {
return p.columns
}

// Height determines the maximum height of any column within this trace.
func (p *ArrayTrace) Height() int {
return p.height
// DuplicateFront inserts n duplicates of the first row at the front of this
// trace.
func (p *ArrayTrace) DuplicateFront(n int) {
for _, c := range p.columns {
c.DuplicateFront(n)
}
// Increment height
p.height += n
}

// GetByName gets the value of a given column (as identified by its name) at a
Expand Down Expand Up @@ -198,6 +220,21 @@ func (p *ArrayTrace) getColumnByName(name string) *ArrayTraceColumn {
return nil
}

// Height determines the maximum height of any column within this trace.
func (p *ArrayTrace) Height() int {
return p.height
}

// InsertFront inserts n duplicates of a given row at the beginning of this
// trace.
func (p *ArrayTrace) InsertFront(n int, mapping func(int) *fr.Element) {
for i, c := range p.columns {
c.InsertFront(n, mapping(i))
}
// Increment height
p.height += n
}

func (p *ArrayTrace) String() string {
// Use string builder to try and make this vaguely efficient.
var id strings.Builder
Expand Down Expand Up @@ -259,6 +296,21 @@ func (p *ArrayTraceColumn) Clone() *ArrayTraceColumn {
return clone
}

// DuplicateFront the first row of this column n times.
func (p *ArrayTraceColumn) DuplicateFront(n int) {
ndata := make([]*fr.Element, len(p.data)+n)
// Copy items from existing data over
copy(ndata[n:], p.data)
// Copy front
front := p.data[0]
// Duplicate front
for i := 0; i < n; i++ {
ndata[i] = front
}
// Copy over
p.data = ndata
}

// Get the value at the given row of this column.
func (p *ArrayTraceColumn) Get(row int) *fr.Element {
if row < 0 || row >= len(p.data) {
Expand All @@ -268,6 +320,19 @@ func (p *ArrayTraceColumn) Get(row int) *fr.Element {
return p.data[row]
}

// InsertFront inserts a given item at the front of this column.
func (p *ArrayTraceColumn) InsertFront(n int, item *fr.Element) {
ndata := make([]*fr.Element, len(p.data)+n)
// Copy items from existing data over
copy(ndata[n:], p.data)
// Insert new items
for i := 0; i < n; i++ {
ndata[i] = item
}
// Copy over
p.data = ndata
}

// ===================================================================
// JSON Parser
// ===================================================================
Expand Down
66 changes: 40 additions & 26 deletions pkg/test/ir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,18 @@ func TestSlow_Wcp(t *testing.T) {
Check(t, "wcp")
}

func Test_Mxp(t *testing.T) {
func TestSlow_Mxp(t *testing.T) {
Check(t, "mxp")
}

// ===================================================================
// Test Helpers
// ===================================================================

// Determines the maximum amount of padding to use when testing. Specifically,
// every trace is tested with varying amounts of padding upto this value.
const MAX_PADDING int = 0

// For a given set of constraints, check that all traces which we
// expect to be accepted are accepted, and all traces that we expect
// to be rejected are rejected.
Expand Down Expand Up @@ -335,29 +339,31 @@ func Check(t *testing.T, test string) {
func CheckTraces(t *testing.T, test string, expected bool, traces []*table.ArrayTrace, hirSchema *hir.Schema) {
for i, tr := range traces {
if tr != nil {
// Lower HIR => MIR
mirSchema := hirSchema.LowerToMir()
// Lower MIR => AIR
airSchema := mirSchema.LowerToAir()
// Check HIR/MIR trace (if applicable)
if airSchema.IsInputTrace(tr) == nil {
// This is an unexpanded input trace.
checkInputTrace(t, tr, traceId{"HIR", test, expected, i + 1}, hirSchema)
checkInputTrace(t, tr, traceId{"MIR", test, expected, i + 1}, mirSchema)
checkInputTrace(t, tr, traceId{"AIR", test, expected, i + 1}, airSchema)
} else if airSchema.IsOutputTrace(tr) == nil {
// This is an already expanded input trace. Therefore, no need
// to perform expansion.
checkExpandedTrace(t, tr, traceId{"AIR", test, expected, i + 1}, airSchema)
} else {
// Trace appears to be malformed.
err1 := airSchema.IsInputTrace(tr)
err2 := airSchema.IsOutputTrace(tr)

if expected {
t.Errorf("Trace malformed (%s.accepts, line %d): [%s][%s]", test, i+1, err1, err2)
for padding := 0; padding <= MAX_PADDING; padding++ {
// Lower HIR => MIR
mirSchema := hirSchema.LowerToMir()
// Lower MIR => AIR
airSchema := mirSchema.LowerToAir()
// Check HIR/MIR trace (if applicable)
if airSchema.IsInputTrace(tr) == nil {
// This is an unexpanded input trace.
checkInputTrace(t, tr, traceId{"HIR", test, expected, i + 1, padding}, hirSchema)
checkInputTrace(t, tr, traceId{"MIR", test, expected, i + 1, padding}, mirSchema)
checkInputTrace(t, tr, traceId{"AIR", test, expected, i + 1, padding}, airSchema)
} else if airSchema.IsOutputTrace(tr) == nil {
// This is an already expanded input trace. Therefore, no need
// to perform expansion.
checkExpandedTrace(t, tr, traceId{"AIR", test, expected, i + 1, 0}, airSchema)
} else {
t.Errorf("Trace malformed (%s.rejects, line %d): [%s][%s]", test, i+1, err1, err2)
// Trace appears to be malformed.
err1 := airSchema.IsInputTrace(tr)
err2 := airSchema.IsOutputTrace(tr)

if expected {
t.Errorf("Trace malformed (%s.accepts, line %d): [%s][%s]", test, i+1, err1, err2)
} else {
t.Errorf("Trace malformed (%s.rejects, line %d): [%s][%s]", test, i+1, err1, err2)
}
}
}
}
Expand All @@ -369,7 +375,7 @@ func checkInputTrace(t *testing.T, tr *table.ArrayTrace, id traceId, schema tabl
etr := tr.Clone()
// Expand trace
err := schema.ExpandTrace(etr)

// Check
if err != nil {
t.Error(err)
} else {
Expand All @@ -378,18 +384,24 @@ func checkInputTrace(t *testing.T, tr *table.ArrayTrace, id traceId, schema tabl
}

func checkExpandedTrace(t *testing.T, tr table.Trace, id traceId, schema table.Schema) {
// Apply padding
table.PadTrace(id.padding, tr)
// Check
err := schema.Accepts(tr)
// Determine whether trace accepted or not.
accepted := (err == nil)
// Process what happened versus what was supposed to happen.
if !accepted && id.expected {
//printTrace(tr)
msg := fmt.Sprintf("Trace rejected incorrectly (%s, %s.accepts, line %d): %s", id.ir, id.test, id.line, err)
msg := fmt.Sprintf("Trace rejected incorrectly (%s, %s.accepts, %d padding, line %d): %s",
id.ir, id.test, id.padding, id.line, err)
t.Errorf(msg)
} else if accepted && !id.expected {
printTrace(tr)

msg := fmt.Sprintf("Trace accepted incorrectly (%s, %s.rejects, line %d)", id.ir, id.test, id.line)
msg := fmt.Sprintf("Trace accepted incorrectly (%s, %s.rejects, %d padding, line %d)",
id.ir, id.test, id.padding, id.line)

t.Errorf(msg)
}
}
Expand All @@ -409,6 +421,8 @@ type traceId struct {
// Identifies the line number within the test file that the failing trace
// original.
line int
// Identifies how much padding has been added to the original trace.
padding int
}

// ReadTracesFile reads a file containing zero or more traces expressed as JSON, where
Expand Down
Loading

0 comments on commit 0500dd5

Please sign in to comment.