Skip to content

Commit 61772b7

Browse files
authored
feat: implement asm debug output (#940)
* assembly debug output operational * support compiling mixed schemas This now puts in support for linking assembly schemas against corset modules being compiled.
1 parent 2a897e0 commit 61772b7

36 files changed

+363
-597
lines changed

pkg/asm/assembler/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func validateInstructions[T io.Instruction[T]](fieldWidth uint, fn io.Function[T
6969
var errors []source.SyntaxError
7070

7171
for _, insn := range fn.Code() {
72-
err := insn.Validate(fieldWidth, fn)
72+
err := insn.Validate(fieldWidth, &fn)
7373
//
7474
if err != nil {
7575
if !srcmaps.Has(insn) {

pkg/asm/io/constraint.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright Consensys Software Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
// the License. You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
// specific language governing permissions and limitations under the License.
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
package io
14+
15+
import (
16+
"github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
17+
sc "github.com/consensys/go-corset/pkg/schema"
18+
"github.com/consensys/go-corset/pkg/trace"
19+
"github.com/consensys/go-corset/pkg/util"
20+
"github.com/consensys/go-corset/pkg/util/collection/bit"
21+
"github.com/consensys/go-corset/pkg/util/source/sexp"
22+
)
23+
24+
// Constraint represents a wrapper around an instruction in order for it to
25+
// conform to the constraint interface.
26+
type Constraint[T Instruction[T]] Function[T]
27+
28+
// Accepts implementation for schema.Constraint interface.
29+
func (p Constraint[T]) Accepts(trace.Trace) (bit.Set, sc.Failure) {
30+
panic("todo")
31+
}
32+
33+
// Bounds implementation for schema.Constraint interface.
34+
func (p Constraint[T]) Bounds(module uint) util.Bounds {
35+
panic("todo")
36+
}
37+
38+
// Consistent implementation for schema.Constraint interface.
39+
func (p Constraint[T]) Consistent(sc.AnySchema) []error {
40+
panic("todo")
41+
}
42+
43+
// Contexts implementation for schema.Constraint interface.
44+
func (p Constraint[T]) Contexts() []sc.ModuleId {
45+
panic("todo")
46+
}
47+
48+
// Name implementation for schema.Constraint interface.
49+
func (p Constraint[T]) Name() string {
50+
panic("todo")
51+
}
52+
53+
// Lisp implementation for schema.Constraint interface.
54+
func (p Constraint[T]) Lisp(schema sc.AnySchema) sexp.SExp {
55+
//
56+
return sexp.NewList([]sexp.SExp{
57+
sexp.NewSymbol("function"),
58+
sexp.NewSymbol(p.name),
59+
})
60+
}
61+
62+
// Substitute implementation for schema.Constraint interface.
63+
func (p Constraint[T]) Substitute(map[string]fr.Element) {
64+
// Do nothing since assembly instructions do not (at the time of writing)
65+
// employ labelled constants.
66+
}

pkg/asm/io/function.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"bytes"
1717
"encoding/gob"
1818
"math"
19+
"math/big"
1920

2021
"github.com/consensys/go-corset/pkg/schema"
2122
"github.com/consensys/go-corset/pkg/util/collection/iter"
@@ -33,14 +34,22 @@ const (
3334
UNUSED_REGISTER = math.MaxUint
3435
)
3536

36-
// Sanity check
37-
var _ schema.Module = &Function[uint]{}
37+
// FunctionInstance represents a specific instance of a function. That is, a
38+
// mapping from input values to expected output values.
39+
type FunctionInstance struct {
40+
// Identifies corresponding function.
41+
Function uint
42+
// Inputs identifies the input arguments
43+
Inputs map[string]big.Int
44+
// Outputs identifies the outputs
45+
Outputs map[string]big.Int
46+
}
3847

3948
// Function defines a distinct functional entity within the system. Functions
4049
// accepts zero or more inputs and produce zero or more outputs. Functions
4150
// declare zero or more internal registers for use, and their interpretation is
4251
// given by a sequence of zero or more instructions.
43-
type Function[T any] struct {
52+
type Function[T Instruction[T]] struct {
4453
// Unique name of this function.
4554
name string
4655
// Registers describes zero or more registers of a given width. Each
@@ -51,7 +60,7 @@ type Function[T any] struct {
5160
}
5261

5362
// NewFunction constructs a new function with the given components.
54-
func NewFunction[T any](name string, registers []Register, code []T) Function[T] {
63+
func NewFunction[T Instruction[T]](name string, registers []Register, code []T) Function[T] {
5564
return Function[T]{name, registers, code}
5665
}
5766

@@ -75,7 +84,9 @@ func (p *Function[T]) Code() []T {
7584
// Constraints provides access to those constraints associated with this
7685
// function.
7786
func (p *Function[T]) Constraints() iter.Iterator[schema.Constraint] {
78-
return iter.NewArrayIterator[schema.Constraint](nil)
87+
var constraint Constraint[T] = Constraint[T]{p.name, p.registers, p.code}
88+
//
89+
return iter.NewUnitIterator[schema.Constraint](constraint)
7990
}
8091

8192
// Consistent applies a number of internal consistency checks. Whilst not

pkg/asm/io/instruction.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package io
1515
import (
1616
"math"
1717
"math/big"
18+
19+
"github.com/consensys/go-corset/pkg/schema"
1820
)
1921

2022
// UNKNOWN_BUS signals a bus which is unknown.
@@ -58,10 +60,10 @@ type Instruction[T any] interface {
5860
// been allocated, etc. The maximum bit capacity of the underlying field is
5961
// needed for this calculation, so as to allow an instruction to check it
6062
// does not overflow the underlying field.
61-
Validate(fieldWidth uint, fn Function[T]) error
63+
Validate(fieldWidth uint, fn schema.Module) error
6264
// Produce a suitable string representation of this instruction. This is
6365
// primarily used for debugging.
64-
String(fn Function[T]) string
66+
String(fn schema.Module) string
6567
}
6668

6769
// InOutInstruction is simply a kind of instruction which performs some kind of I/O

pkg/asm/io/macro/IfGoto.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/consensys/go-corset/pkg/asm/io"
2020
"github.com/consensys/go-corset/pkg/asm/io/micro"
21+
"github.com/consensys/go-corset/pkg/schema"
2122
)
2223

2324
const (
@@ -135,7 +136,7 @@ func (p *IfGoto) RegistersWritten() []io.RegisterId {
135136
return nil
136137
}
137138

138-
func (p *IfGoto) String(fn io.Function[Instruction]) string {
139+
func (p *IfGoto) String(fn schema.Module) string {
139140
var (
140141
l = fn.Register(p.Left).Name
141142
r string
@@ -169,7 +170,7 @@ func (p *IfGoto) String(fn io.Function[Instruction]) string {
169170
}
170171

171172
// Validate checks whether or not this instruction is correctly balanced.
172-
func (p *IfGoto) Validate(fieldWidth uint, fn io.Function[Instruction]) error {
173+
func (p *IfGoto) Validate(fieldWidth uint, fn schema.Module) error {
173174
if p.Left == p.Right {
174175
switch p.Cond {
175176
case EQ, LTEQ, GTEQ:

pkg/asm/io/macro/add.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/consensys/go-corset/pkg/asm/io"
2020
"github.com/consensys/go-corset/pkg/asm/io/micro"
21+
"github.com/consensys/go-corset/pkg/schema"
2122
)
2223

2324
// Add represents a generic operation of the following form:
@@ -84,12 +85,12 @@ func (p *Add) RegistersWritten() []io.RegisterId {
8485
return p.Targets
8586
}
8687

87-
func (p *Add) String(fn io.Function[Instruction]) string {
88+
func (p *Add) String(fn schema.Module) string {
8889
return assignmentToString(p.Targets, p.Sources, p.Constant, fn, zero, " + ")
8990
}
9091

9192
// Validate checks whether or not this instruction is correctly balanced.
92-
func (p *Add) Validate(fieldWidth uint, fn io.Function[Instruction]) error {
93+
func (p *Add) Validate(fieldWidth uint, fn schema.Module) error {
9394
var (
9495
regs = fn.Registers()
9596
lhs_bits = sumTargetBits(p.Targets, regs)

pkg/asm/io/macro/call.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/consensys/go-corset/pkg/asm/io"
2020
"github.com/consensys/go-corset/pkg/asm/io/micro"
21+
"github.com/consensys/go-corset/pkg/schema"
2122
)
2223

2324
// Call represents a function call providing one or more arguments and accepting
@@ -110,7 +111,7 @@ func (p *Call) RegistersWritten() []io.RegisterId {
110111
return p.Targets
111112
}
112113

113-
func (p *Call) String(fn io.Function[Instruction]) string {
114+
func (p *Call) String(fn schema.Module) string {
114115
var (
115116
builder strings.Builder
116117
regs = fn.Registers()
@@ -125,7 +126,7 @@ func (p *Call) String(fn io.Function[Instruction]) string {
125126
}
126127

127128
// Validate checks whether or not this instruction well-formed.
128-
func (p *Call) Validate(fieldWidth uint, fn io.Function[Instruction]) error {
129+
func (p *Call) Validate(fieldWidth uint, fn schema.Module) error {
129130
// Check bus is assigned
130131
if p.bus.IsUnlinked() {
131132
return fmt.Errorf("unknown function")

pkg/asm/io/macro/goto.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/consensys/go-corset/pkg/asm/io"
1919
"github.com/consensys/go-corset/pkg/asm/io/micro"
20+
"github.com/consensys/go-corset/pkg/schema"
2021
)
2122

2223
// Goto provides an unconditional branching instruction to a given instructon.
@@ -53,11 +54,11 @@ func (p *Goto) RegistersWritten() []io.RegisterId {
5354
return nil
5455
}
5556

56-
func (p *Goto) String(fn io.Function[Instruction]) string {
57+
func (p *Goto) String(fn schema.Module) string {
5758
return fmt.Sprintf("goto %d", p.Target)
5859
}
5960

6061
// Validate checks whether or not this instruction is correctly balanced.
61-
func (p *Goto) Validate(fieldWidth uint, fn io.Function[Instruction]) error {
62+
func (p *Goto) Validate(fieldWidth uint, fn schema.Module) error {
6263
return nil
6364
}

pkg/asm/io/macro/insn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/consensys/go-corset/pkg/asm/io"
2121
"github.com/consensys/go-corset/pkg/asm/io/micro"
22+
"github.com/consensys/go-corset/pkg/schema"
2223
)
2324

2425
// Alias for big integer representation of 0.
@@ -56,7 +57,7 @@ type IoInstruction interface {
5657
Link(bus io.Bus)
5758
}
5859

59-
func assignmentToString(dsts []io.RegisterId, srcs []io.RegisterId, constant big.Int, fn io.Function[Instruction],
60+
func assignmentToString(dsts []io.RegisterId, srcs []io.RegisterId, constant big.Int, fn schema.Module,
6061
c big.Int, op string) string {
6162
//
6263
var (

pkg/asm/io/macro/mul.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/consensys/go-corset/pkg/asm/io"
2020
"github.com/consensys/go-corset/pkg/asm/io/micro"
21+
"github.com/consensys/go-corset/pkg/schema"
2122
)
2223

2324
// Mul represents a generic operation of the following form:
@@ -86,12 +87,12 @@ func (p *Mul) RegistersWritten() []io.RegisterId {
8687
return p.Targets
8788
}
8889

89-
func (p *Mul) String(fn io.Function[Instruction]) string {
90+
func (p *Mul) String(fn schema.Module) string {
9091
return assignmentToString(p.Targets, p.Sources, p.Constant, fn, one, " * ")
9192
}
9293

9394
// Validate checks whether or not this instruction is correctly balanced.
94-
func (p *Mul) Validate(fieldWidth uint, fn io.Function[Instruction]) error {
95+
func (p *Mul) Validate(fieldWidth uint, fn schema.Module) error {
9596
var (
9697
regs = fn.Registers()
9798
lhs_bits = sumTargetBits(p.Targets, regs)

0 commit comments

Comments
 (0)