Skip to content

Commit

Permalink
Merge pull request #377 from Consensys/334-support-columns-with-expli…
Browse files Browse the repository at this point in the history
…cit-length-multiplliers

feat: support user-defined length multipliers
  • Loading branch information
DavePearce authored Nov 5, 2024
2 parents c637f5b + 6efa856 commit 452d062
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
9 changes: 6 additions & 3 deletions pkg/binfile/constraint_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type column struct {
// information about the length of this column (e.g. its a
// multiple of two). This seems only relevant for computed
// columns.
IntrinsicSizeFactor string `json:"intrinsic_size_factor"`
IntrinsicSizeFactor uint `json:"intrinsic_size_factor"`
// Indicates this is a computed column. For binfiles being
// compiled without expansion, this should always be false.
Computed bool
Expand Down Expand Up @@ -82,6 +82,10 @@ type register struct {
// the original binfile format. Instead, this field is determined from
// parsing the binfile format.
MustProve bool
// LengthMultiplier indicates the length multiplier for this column. This
// must be a factor of the number of rows in the column. For example, a
// column with length multiplier of 2 must have an even number of rows, etc.
LengthMultiplier uint `json:"length_multiplier"`
}

type columnSet struct {
Expand Down Expand Up @@ -168,8 +172,7 @@ func allocateRegisters(cs *constraintSet, schema *hir.Schema) map[uint]uint {
if !c.Computed {
handle := asHandle(c.Handle)
mid := registerModule(schema, handle.module)
// NOTE: assumption here that length multiplier is always one.
ctx := trace.NewContext(mid, 1)
ctx := trace.NewContext(mid, c.LengthMultiplier)
col_type := c.Type.toHir()
// Add column for this
cid := schema.AddDataColumn(ctx, handle.column, col_type)
Expand Down
12 changes: 4 additions & 8 deletions pkg/schema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func (tb TraceBuilder) Build(columns []trace.RawColumn) (trace.Trace, []error) {

// A column key is used as a key for the column map
type columnKey struct {
context trace.Context
column string
module uint
column string
}

func (tb TraceBuilder) initialiseTrace(cols []trace.RawColumn) (*trace.ArrayTrace, []error) {
Expand Down Expand Up @@ -150,7 +150,7 @@ func (tb TraceBuilder) initialiseTraceColumns() ([]trace.ArrayColumn, map[column
for i, iter := uint(0), tb.schema.Columns(); iter.HasNext(); i++ {
c := iter.Next()
// Construct an appropriate key for this column
colkey := columnKey{c.Context(), c.Name()}
colkey := columnKey{c.Context().Module(), c.Name()}
// Initially column data and padding are nil. In some cases, we will
// populate this information from the cols array. However, in other
// cases, it will need to be populated during trace expansion.
Expand Down Expand Up @@ -180,12 +180,8 @@ func fillTraceColumns(modmap map[string]uint, colmap map[columnKey]uint,
if !ok {
errs = append(errs, fmt.Errorf("unknown module '%s' in trace", c.Module))
} else {
// We assume (for now) that user-provided columns always have a length
// multiplier of 1. In general, this will be true. However, in situations
// where we are importing expanded traces, then this might not be true.
context := trace.NewContext(mid, 1)
// Determine enclosiong module height
cid, ok := colmap[columnKey{context, c.Name}]
cid, ok := colmap[columnKey{mid, c.Name}]
// More sanity checks
if !ok {
errs = append(errs, fmt.Errorf("unknown column '%s' in trace", c.QualifiedName()))
Expand Down
4 changes: 2 additions & 2 deletions pkg/trace/array_trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func (p *ArrayTrace) FillColumn(cid uint, data util.FrArray, padding fr.Element)
// Sanity check this column has not already been filled.
if data.Len()%multiplier != 0 {
colname := QualifiedColumnName(mod.name, col.name)
panic(fmt.Sprintf("computed column %s has invalid length multiplier (%d indivisible by %d)",
panic(fmt.Sprintf("column %s has invalid length multiplier (%d indivisible by %d)",
colname, data.Len(), multiplier))
} else if mod.height == math.MaxUint {
// Initialise column height
mod.height = data.Len() / col.context.multiplier
} else if data.Len() != p.Height(col.Context()) {
colname := QualifiedColumnName(mod.name, col.name)
panic(fmt.Sprintf("computed column %s has invalid height (%d but expected %d)", colname, data.Len(), mod.height))
panic(fmt.Sprintf("column %s has invalid height (%d but expected %d)", colname, data.Len(), mod.height*multiplier))
}
// Fill the column
col.fill(data, padding)
Expand Down
2 changes: 1 addition & 1 deletion pkg/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p Context) Module() uint {
panic("void context has no module")
}

panic("conflicted context has no module")
panic("conflicted context")
}

// LengthMultiplier returns the length multiplier for this context. Note,
Expand Down

0 comments on commit 452d062

Please sign in to comment.