Skip to content

Commit

Permalink
moving two functions from wizardutils to the column package
Browse files Browse the repository at this point in the history
  • Loading branch information
Soleimani193 committed Dec 17, 2024
1 parent 157f641 commit eb9fa7b
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 177 deletions.
73 changes: 73 additions & 0 deletions prover/protocol/column/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package column
import (
"reflect"

"github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/protocol/coin"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/variables"
"github.com/consensys/linea-monorepo/prover/symbolic"
"github.com/consensys/linea-monorepo/prover/utils"
)

Expand Down Expand Up @@ -91,3 +95,72 @@ func NbLeaves(h ifaces.Column) int {
}
panic("unreachable")
}

func EvalExprColumn(run ifaces.Runtime, board symbolic.ExpressionBoard) smartvectors.SmartVector {

var (
metadata = board.ListVariableMetadata()
inputs = make([]smartvectors.SmartVector, len(metadata))
length = ExprIsOnSameLengthHandles(&board)
)

// Attempt to recover the size of the
for i := range inputs {
switch m := metadata[i].(type) {
case ifaces.Column:
inputs[i] = m.GetColAssignment(run)
case coin.Info:
v := run.GetRandomCoinField(m.Name)
inputs[i] = smartvectors.NewConstant(v, length)
case ifaces.Accessor:
v := m.GetVal(run)
inputs[i] = smartvectors.NewConstant(v, length)
case variables.PeriodicSample:
v := m.EvalCoset(length, 0, 1, false)
inputs[i] = v
case variables.X:
v := m.EvalCoset(length, 0, 1, false)
inputs[i] = v
}
}

return board.Evaluate(inputs)
}

// ExprIsOnSameLengthHandles checks that all the variables of the expression
// that are [ifaces.Column] have the same size (and panics if it does not), then
// returns the match.
func ExprIsOnSameLengthHandles(board *symbolic.ExpressionBoard) int {

var (
metadatas = board.ListVariableMetadata()
length = 0
)

for _, m := range metadatas {
switch metadata := m.(type) {
case ifaces.Column:
// Initialize the length with the first commitment
if length == 0 {
length = metadata.Size()
}

// Sanity-check the vector should all have the same length
if length != metadata.Size() {
utils.Panic("Inconsistent length for %v (has size %v, but expected %v)", metadata.GetColID(), metadata.Size(), length)
}
// The expression can involve random coins
case coin.Info, variables.X, variables.PeriodicSample, ifaces.Accessor:
// Do nothing
default:
utils.Panic("unknown type %T", metadata)
}
}

// No commitment were found in the metadata, thus this call is broken
if length == 0 {
utils.Panic("declared a handle from an expression which does not contains any handle")
}

return length
}
4 changes: 2 additions & 2 deletions prover/protocol/compiler/globalcs/merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/consensys/linea-monorepo/prover/maths/fft"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/coin"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/query"
"github.com/consensys/linea-monorepo/prover/protocol/variables"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
"github.com/consensys/linea-monorepo/prover/symbolic"
"github.com/consensys/linea-monorepo/prover/utils"
)
Expand Down Expand Up @@ -178,7 +178,7 @@ func getBoundCancelledExpression(cs query.GlobalConstraint) *symbolic.Expression
func getExprRatio(expr *symbolic.Expression) int {
var (
board = expr.Board()
domainSize = wizardutils.ExprIsOnSameLengthHandles(&board)
domainSize = column.ExprIsOnSameLengthHandles(&board)
exprDegree = board.Degree(GetDegree(domainSize))
quotientSize = exprDegree - domainSize + 1
ratio = utils.DivCeil(quotientSize, domainSize)
Expand Down
4 changes: 2 additions & 2 deletions prover/protocol/compiler/innerproduct/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
)

// proverTask implements the [wizard.ProverAction] interface and as such
Expand Down Expand Up @@ -39,7 +39,7 @@ func (ctx *contextForSize) run(run *wizard.ProverRuntime) {

var (
size = ctx.Summation.Size()
collapsed = wizardutils.EvalExprColumn(run, ctx.CollapsedBoard).IntoRegVecSaveAlloc()
collapsed = column.EvalExprColumn(run, ctx.CollapsedBoard).IntoRegVecSaveAlloc()
summation = make([]field.Element, size)
)

Expand Down
5 changes: 3 additions & 2 deletions prover/protocol/compiler/lookup/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sv "github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/maths/common/vector"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
Expand Down Expand Up @@ -308,7 +309,7 @@ func (z zAssignmentTask) run(run *wizard.ProverRuntime) {

var (
numeratorMetadata = z.ZNumeratorBoarded[frag].ListVariableMetadata()
denominator = wizardutils.EvalExprColumn(run, z.ZDenominatorBoarded[frag]).IntoRegVecSaveAlloc()
denominator = column.EvalExprColumn(run, z.ZDenominatorBoarded[frag]).IntoRegVecSaveAlloc()
numerator []field.Element
packedZ = field.BatchInvert(denominator)
)
Expand All @@ -318,7 +319,7 @@ func (z zAssignmentTask) run(run *wizard.ProverRuntime) {
}

if len(numeratorMetadata) > 0 {
numerator = wizardutils.EvalExprColumn(run, z.ZNumeratorBoarded[frag]).IntoRegVecSaveAlloc()
numerator = column.EvalExprColumn(run, z.ZNumeratorBoarded[frag]).IntoRegVecSaveAlloc()
}

for k := range packedZ {
Expand Down
3 changes: 2 additions & 1 deletion prover/protocol/compiler/mimc/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/consensys/linea-monorepo/prover/crypto/mimc"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
Expand Down Expand Up @@ -155,7 +156,7 @@ func mimcExprHandle(comp *wizard.CompiledIOP, expr *symbolic.Expression, name ..

maxRound := wizardutils.LastRoundToEval(expr)
board := expr.Board()
length := wizardutils.ExprIsOnSameLengthHandles(&board)
length := column.ExprIsOnSameLengthHandles(&board)

handleName := fmt.Sprintf("SYMBOLIC_%v", expr.ESHash.String())
if len(name) > 0 {
Expand Down
6 changes: 3 additions & 3 deletions prover/protocol/compiler/permutation/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/maths/common/vector"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
)

// proverTaskAtRound implements the [wizard.ProverAction] interface and is
Expand Down Expand Up @@ -48,13 +48,13 @@ func (z *ZCtx) run(run *wizard.ProverRuntime) {
)

if packingArity*i < len(z.NumeratorFactors) {
numerator = wizardutils.EvalExprColumn(run, z.NumeratorFactorsBoarded[i]).IntoRegVecSaveAlloc()
numerator = column.EvalExprColumn(run, z.NumeratorFactorsBoarded[i]).IntoRegVecSaveAlloc()
} else {
numerator = vector.Repeat(field.One(), z.Size)
}

if packingArity*i < len(z.DenominatorFactors) {
denominator = wizardutils.EvalExprColumn(run, z.DenominatorFactorsBoarded[i]).IntoRegVecSaveAlloc()
denominator = column.EvalExprColumn(run, z.DenominatorFactorsBoarded[i]).IntoRegVecSaveAlloc()
} else {
denominator = vector.Repeat(field.One(), z.Size)
}
Expand Down
3 changes: 2 additions & 1 deletion prover/protocol/dedicated/bigrange/bigrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/consensys/linea-monorepo/prover/maths/fft"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/coin"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/variables"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
Expand Down Expand Up @@ -41,7 +42,7 @@ func BigRange(comp *wizard.CompiledIOP, expr *symbolic.Expression, numLimbs, bit
limbs = make([]ifaces.Column, numLimbs)
round = wizardutils.LastRoundToEval(expr)
boarded = expr.Board()
size = wizardutils.ExprIsOnSameLengthHandles(&boarded)
size = column.ExprIsOnSameLengthHandles(&boarded)
totalNumBits = numLimbs * bitPerLimbs
)

Expand Down
2 changes: 1 addition & 1 deletion prover/protocol/dedicated/byte32cmp/decompose.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (d *decompositionCtx) Run(run *wizard.ProverRuntime) {
numLimbs = len(d.decomposed.Limbs)
bitPerLimbs = d.decomposed.LimbBitSize
totalNumBits = numLimbs * bitPerLimbs
original = wizardutils.EvalExprColumn(run, d.original)
original = column.EvalExprColumn(run, d.original)
limbsWitness = make([][]field.Element, numLimbs)
size = original.Len()
)
Expand Down
3 changes: 2 additions & 1 deletion prover/protocol/dedicated/byte32cmp/multi_limb_cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/dedicated"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
Expand Down Expand Up @@ -192,7 +193,7 @@ func (mCmp *multiLimbCmp) Run(run *wizard.ProverRuntime) {
}()

var (
syndrom = wizardutils.EvalExprColumn(run, mCmp.syndromBoard)
syndrom = column.EvalExprColumn(run, mCmp.syndromBoard)
isGreater = make([]field.Element, mCmp.isGreater.Size())
isLower = make([]field.Element, mCmp.isLower.Size())
nnSyndrom = make([]field.Element, mCmp.isLower.Size())
Expand Down
3 changes: 2 additions & 1 deletion prover/protocol/dedicated/expr_handle/expr_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/consensys/linea-monorepo/prover/maths/fft"
"github.com/consensys/linea-monorepo/prover/maths/field"
"github.com/consensys/linea-monorepo/prover/protocol/coin"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/variables"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
Expand All @@ -25,7 +26,7 @@ func ExprHandle(comp *wizard.CompiledIOP, expr *symbolic.Expression, name ...str
var (
boarded = expr.Board()
maxRound = wizardutils.LastRoundToEval(expr)
length = wizardutils.ExprIsOnSameLengthHandles(&boarded)
length = column.ExprIsOnSameLengthHandles(&boarded)
handleName = fmt.Sprintf("SYMBOLIC_%v", expr.ESHash.String())
)

Expand Down
7 changes: 4 additions & 3 deletions prover/protocol/dedicated/is_zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dedicated

import (
"github.com/consensys/linea-monorepo/prover/maths/common/smartvectors"
"github.com/consensys/linea-monorepo/prover/protocol/column"
"github.com/consensys/linea-monorepo/prover/protocol/ifaces"
"github.com/consensys/linea-monorepo/prover/protocol/wizard"
"github.com/consensys/linea-monorepo/prover/protocol/wizardutils"
Expand Down Expand Up @@ -44,7 +45,7 @@ func IsZero(comp *wizard.CompiledIOP, c any) (ifaces.Column, wizard.ProverAction
case *sym.Expression:
board := c1.Board()
ctx.c = c1
ctx.size = wizardutils.ExprIsOnSameLengthHandles(&board)
ctx.size = column.ExprIsOnSameLengthHandles(&board)
ctx.round = wizardutils.LastRoundToEval(c1)
}

Expand Down Expand Up @@ -123,13 +124,13 @@ func compileIsZeroWithSize(comp *wizard.CompiledIOP, ctx *isZeroCtx) {
// Run implements the [wizard.ProverAction] interface
func (ctx *isZeroCtx) Run(run *wizard.ProverRuntime) {
var (
c = wizardutils.EvalExprColumn(run, ctx.c.Board())
c = column.EvalExprColumn(run, ctx.c.Board())
invOrZero = smartvectors.BatchInvert(c)
isZero = smartvectors.IsZero(c)
)

if ctx.mask != nil {
mask := wizardutils.EvalExprColumn(run, ctx.mask.Board())
mask := column.EvalExprColumn(run, ctx.mask.Board())
invOrZero = smartvectors.Mul(invOrZero, mask)
isZero = smartvectors.Mul(isZero, mask)
}
Expand Down
4 changes: 2 additions & 2 deletions prover/protocol/dedicated/projection/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func InsertProjection(
func (pa projectionProverAction) Run(run *wizard.ProverRuntime) {

var (
a = wizardutils.EvalExprColumn(run, pa.ABoard).IntoRegVecSaveAlloc()
b = wizardutils.EvalExprColumn(run, pa.BBoard).IntoRegVecSaveAlloc()
a = column.EvalExprColumn(run, pa.ABoard).IntoRegVecSaveAlloc()
b = column.EvalExprColumn(run, pa.BBoard).IntoRegVecSaveAlloc()
fA = pa.FilterA.GetColAssignment(run).IntoRegVecSaveAlloc()
fB = pa.FilterB.GetColAssignment(run).IntoRegVecSaveAlloc()
x = run.GetRandomCoinField(pa.EvalCoin.Name)
Expand Down
Loading

0 comments on commit eb9fa7b

Please sign in to comment.