Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/asm/assemble.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ func checkMultiLineLookups(program MicroProgram, externs []hir.Module) {
for _, m := range externs {
for _, c := range m.RawConstraints() {
switch c := c.Unwrap().(type) {
case hir.LookupConstraint:
case *hir.LookupConstraint:
handle := fmt.Sprintf("%s.%s", m.Name(), c.Handle)
checkMultiLineLookup(program, c, handle)
case hir.FunctionCall:
case *hir.FunctionCall:
checkMultiLineFnCall(program, c, m.Name().String())
}
}
}
}

func checkMultiLineLookup(program MicroProgram, c hir.LookupConstraint, handle string) {
func checkMultiLineLookup(program MicroProgram, c *hir.LookupConstraint, handle string) {
for _, src := range c.Sources {
checkMultiLineLookupVector(program, src, handle, "source")
}
Expand All @@ -221,7 +221,7 @@ func checkMultiLineLookupVector(program MicroProgram, c lookup.Vector[word.BigEn
}
}

func checkMultiLineFnCall(program MicroProgram, c hir.FunctionCall, handle string) {
func checkMultiLineFnCall(program MicroProgram, c *hir.FunctionCall, handle string) {
var (
callee = program.Component(c.Callee)
)
Expand Down
4 changes: 2 additions & 2 deletions pkg/asm/io/micro/fail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

// Fail signals a return from the enclosing function.
type Fail struct {
// dummy is included to force Fail structs to be stored in the heap.
// Dummy is included to force Fail structs to be stored in the heap.
//nolint
dummy uint
Dummy uint
}

// Clone this micro code.
Expand Down
42 changes: 21 additions & 21 deletions pkg/asm/io/micro/inout.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

// InOut captures input / output instructions for reading / writing to a bus.
type InOut struct {
// Indicates whether input or output instruction.
input bool
// Local bus
bus io.Bus
// Indicates whether Input or output instruction.
Input bool
// Local DataBus
DataBus io.Bus
}

// NewIoRead constructs an instruction responsible for reading data to a given
Expand All @@ -43,41 +43,41 @@ func NewIoWrite(bus io.Bus) *InOut {
// Bus returns information about the bus. Observe that prior to Link being
// called, this will return an unlinked bus.
func (p *InOut) Bus() io.Bus {
return p.bus
return p.DataBus
}

// Clone this micro code.
func (p *InOut) Clone() Code {
return &InOut{p.input, p.bus}
return &InOut{p.Input, p.DataBus}
}

// MicroExecute a given micro-code, using a given local state. This may update
// the register values, and returns either the number of micro-codes to "skip
// over" when executing the enclosing instruction or, if skip==0, a destination
// program counter (which can signal return of enclosing function).
func (p *InOut) MicroExecute(state io.State) (uint, uint) {
if p.input {
state.In(p.bus)
if p.Input {
state.In(p.DataBus)
} else {
state.Out(p.bus)
state.Out(p.DataBus)
}
//
return 1, 0
}

// RegistersRead returns the set of registers read by this instruction.
func (p *InOut) RegistersRead() []io.RegisterId {
if p.input {
return p.bus.Address()
if p.Input {
return p.DataBus.Address()
}
//
return append(p.bus.Address(), p.bus.Data()...)
return append(p.DataBus.Address(), p.DataBus.Data()...)
}

// RegistersWritten returns the set of registers written by this instruction.
func (p *InOut) RegistersWritten() []io.RegisterId {
if p.input {
return p.bus.Data()
if p.Input {
return p.DataBus.Data()
}
//
return nil
Expand All @@ -87,19 +87,19 @@ func (p *InOut) RegistersWritten() []io.RegisterId {
// micro codes using registers of a fixed maximum width.
func (p *InOut) Split(mapping register.LimbsMap, _ agnostic.RegisterAllocator) []Code {
// Split bus
address := register.ApplyLimbsMap(mapping, p.bus.Address()...)
data := register.ApplyLimbsMap(mapping, p.bus.Data()...)
bus := io.NewBus(p.bus.Name, p.bus.BusId, address, data)
address := register.ApplyLimbsMap(mapping, p.DataBus.Address()...)
data := register.ApplyLimbsMap(mapping, p.DataBus.Data()...)
bus := io.NewBus(p.DataBus.Name, p.DataBus.BusId, address, data)
// Done
return []Code{&InOut{p.input, bus}}
return []Code{&InOut{p.Input, bus}}
}

func (p *InOut) String(fn register.Map) string {
if p.input {
return fmt.Sprintf("in %s", p.bus.Name)
if p.Input {
return fmt.Sprintf("in %s", p.DataBus.Name)
}

return fmt.Sprintf("out %s", p.bus.Name)
return fmt.Sprintf("out %s", p.DataBus.Name)
}

// Validate checks whether or not this instruction is correctly balanced.
Expand Down
13 changes: 13 additions & 0 deletions pkg/asm/io/micro/insn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package micro

import (
"encoding/gob"
"fmt"
"strings"

Expand Down Expand Up @@ -331,3 +332,15 @@ func retargetInsn(oldIndex uint, pktIndex, pktSize uint, code Code, mapping []ui
return code
}
}

func init() {
gob.Register(Code(&Assign{}))
gob.Register(Code(&InOut{}))
gob.Register(Code(&Cast{}))
gob.Register(Code(&Division{}))
gob.Register(Code(&Fail{}))
gob.Register(Code(&Jmp{}))
gob.Register(Code(&Skip{}))
gob.Register(Code(&SkipIf{}))
gob.Register(Code(&Ret{}))
}
4 changes: 2 additions & 2 deletions pkg/asm/io/micro/ret.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

// Ret signals a return from the enclosing function.
type Ret struct {
// dummy is included to force Ret structs to be stored in the heap.
// Dummy is included to force Ret structs to be stored in the heap.
//nolint
dummy uint
Dummy uint
}

// Clone this micro code.
Expand Down
20 changes: 20 additions & 0 deletions pkg/asm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ import (
"math"

"github.com/consensys/go-corset/pkg/asm/io"
"github.com/consensys/go-corset/pkg/asm/io/macro"
"github.com/consensys/go-corset/pkg/asm/io/micro"
"github.com/consensys/go-corset/pkg/asm/program"
"github.com/consensys/go-corset/pkg/schema"
"github.com/consensys/go-corset/pkg/schema/module"
"github.com/consensys/go-corset/pkg/schema/register"
"github.com/consensys/go-corset/pkg/util/collection/array"
"github.com/consensys/go-corset/pkg/util/collection/iter"
"github.com/consensys/go-corset/pkg/util/field"
"github.com/consensys/go-corset/pkg/util/field/bls12_377"
"github.com/consensys/go-corset/pkg/util/field/gf251"
"github.com/consensys/go-corset/pkg/util/field/gf8209"
"github.com/consensys/go-corset/pkg/util/field/koalabear"
"github.com/consensys/go-corset/pkg/util/word"
)

// MixedProgram represents the composition of an assembly program along with
Expand Down Expand Up @@ -189,4 +196,17 @@ func (p *MixedProgram[F, T, M]) GobDecode(data []byte) error {

func init() {
gob.Register(MacroComponent(&MacroFunction{}))
gob.Register(MicroComponent(&MicroFunction{}))
gob.Register(schema.AnySchema[word.BigEndian](&MacroHirProgram{}))
gob.Register(schema.AnySchema[word.BigEndian](&MicroHirProgram{}))
// Field specific initialisation
initForField[bls12_377.Element]()
initForField[koalabear.Element]()
initForField[gf8209.Element]()
initForField[gf251.Element]()
}

func initForField[F field.Element[F]]() {
gob.Register(schema.Assignment[F](&program.Assignment[F, macro.Instruction]{}))
gob.Register(schema.Assignment[F](&program.Assignment[F, micro.Instruction]{}))
}
40 changes: 20 additions & 20 deletions pkg/asm/program/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
// Assignment represents a wrapper around an instruction in order for it to
// conform to the schema.Assignment interface.
type Assignment[F field.Element[F], T io.Instruction] struct {
id sc.ModuleId
fn *io.Function[T]
ModuleId sc.ModuleId
Function *io.Function[T]
}

// NewAssignment constructs a new assignment capable of trace filling for a
Expand All @@ -52,15 +52,15 @@ func (p Assignment[F, T]) Bounds(module uint) util.Bounds {
func (p Assignment[F, T]) Compute(trace tr.Trace[F], schema sc.AnySchema[F]) ([]array.MutArray[F], error) {
//
var (
trModule = trace.Module(p.id)
trModule = trace.Module(p.ModuleId)
states []io.State
iomap = NewTraceIoMap(trace)
numInputs = p.fn.NumInputs()
numOutputs = p.fn.NumOutputs()
numInputs = p.Function.NumInputs()
numOutputs = p.Function.NumOutputs()
)
// Trace given rows
for i := range trModule.Height() {
inputs := extractValues(i, trModule, 0, p.fn.NumInputs())
inputs := extractValues(i, trModule, 0, p.Function.NumInputs())
outputs := extractValues(i, trModule, numInputs, numInputs+numOutputs)
sts := p.trace(inputs, outputs, iomap)
states = append(states, sts...)
Expand All @@ -79,13 +79,13 @@ func (p Assignment[F, T]) Lisp(schema sc.AnySchema[F]) sexp.SExp {
//
var cols []sexp.SExp
//
for _, r := range p.fn.Registers() {
for _, r := range p.Function.Registers() {
cols = append(cols, sexp.NewSymbol(r.Name()))
}
//
return sexp.NewList([]sexp.SExp{
sexp.NewSymbol("compute"),
sexp.NewSymbol(p.fn.Name().String()),
sexp.NewSymbol(p.Function.Name().String()),
sexp.NewList(cols),
})
}
Expand All @@ -99,10 +99,10 @@ func (p Assignment[F, T]) RegistersExpanded() []register.Ref {
func (p Assignment[F, T]) RegistersRead() []register.Ref {
var regs []register.Ref
//
for i, reg := range p.fn.Registers() {
for i, reg := range p.Function.Registers() {
if reg.IsInputOutput() {
rid := register.NewId(uint(i))
regs = append(regs, register.NewRef(p.id, rid))
regs = append(regs, register.NewRef(p.ModuleId, rid))
}
}
//
Expand All @@ -113,8 +113,8 @@ func (p Assignment[F, T]) RegistersRead() []register.Ref {
func (p Assignment[F, T]) RegistersWritten() []register.Ref {
var (
regs []register.Ref
nRegisters = len(p.fn.Registers())
multiLine = len(p.fn.Code()) > 1
nRegisters = len(p.Function.Registers())
multiLine = len(p.Function.Code()) > 1
)
// Include control registers for multi-line functions.
if multiLine {
Expand All @@ -124,7 +124,7 @@ func (p Assignment[F, T]) RegistersWritten() []register.Ref {
// This is because it may expand the I/O registers.
for i := range nRegisters {
rid := register.NewId(uint(i))
regs = append(regs, register.NewRef(p.id, rid))
regs = append(regs, register.NewRef(p.ModuleId, rid))
}
//
return regs
Expand All @@ -143,10 +143,10 @@ func (p Assignment[F, T]) Substitute(map[string]F) {
// ensure internal consistency.
func (p Assignment[F, T]) trace(inputs, outputs []big.Int, iomap io.Map) []io.State {
var (
code = p.fn.Code()
code = p.Function.Code()
states []io.State
// Construct local state
state = io.InitialState(inputs, p.fn.Registers(), p.fn.Buses(), iomap)
state = io.InitialState(inputs, p.Function.Registers(), p.Function.Buses(), iomap)
// Program counter position
pc uint = 0
)
Expand All @@ -169,16 +169,16 @@ func (p Assignment[F, T]) states2columns(width uint, states []io.State, builder
var (
cols = make([]array.MutArray[F], width)
nrows = uint(len(states))
multiLine = len(p.fn.Code()) > 1
multiLine = len(p.Function.Code()) > 1
)
// Initialise register columns
for i, r := range p.fn.Registers() {
for i, r := range p.Function.Registers() {
cols[i] = builder.NewArray(nrows, r.Width())
}
// Initialise control columns (if applicable)
// transcribe values
for row, st := range states {
for i := range p.fn.Registers() {
for i := range p.Function.Registers() {
var (
val F
rid = register.NewId(uint(i))
Expand All @@ -204,10 +204,10 @@ func (p Assignment[F, T]) assignControlRegisters(cols []array.MutArray[F], state
zero = field.Zero[F]()
one = field.One[F]()
nrows = uint(len(states))
pc = uint(len(p.fn.Registers()))
pc = uint(len(p.Function.Registers()))
ret = pc + 1
// Calculate minimum size of PC; NOTE: +1 because PC==0 is reserved for padding.
pcWidth = bit.Width(uint(len(p.fn.Code()) + 1))
pcWidth = bit.Width(uint(len(p.Function.Code()) + 1))
)
// Initialise columns
cols[pc] = builder.NewArray(nrows, pcWidth)
Expand Down
4 changes: 2 additions & 2 deletions pkg/asm/propagate.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func extractExternalCalls[M sc.Module[word.BigEndian]](extern M) []hir.FunctionC
// This should always hold
if hc, ok := c.(hir.Constraint); ok {
// Check whether its a call or not
if call, ok := hc.Unwrap().(hir.FunctionCall); ok {
if call, ok := hc.Unwrap().(*hir.FunctionCall); ok {
// Yes, so record it
calls = append(calls, call)
calls = append(calls, *call)
}
}
}
Expand Down
Loading
Loading