zkFold / zkfold-base

ZkFold's Base library
https://zkfold.io
MIT License
17 stars 7 forks source link

Deeper constant optimization #270

Open TurtlePU opened 2 months ago

TurtlePU commented 2 months ago

For now, only "explicit constants" are optimized away -- that is, created with fromConstant :: a -> var in MonadCircuit and with embed :: Symbolic c => f (BaseField c) -> c f. More can be optimized:

vlasin commented 2 months ago

It seems, the first item can be solved by modifying the constraint function instance for ArithmeticCircuit.

TurtlePU commented 2 months ago

Not the constraint, but the newAssigned function, I guess, as constraint is not able to change any variable

But yeah, the first point is doable without running any specific optimization passes

vlasin commented 1 month ago

As for the second item, we could do more if we make a reasonable assumption about the set of polynomial equations that are admissible constraints. Specifically, if we assume that any substitution of the form $x \rightarrow a_1 x_1 + a_2 x_2 + b$ does not make a valid constraint invalid, then we could eliminate all equations of the form $a_1 x_1 + a_2 x_2 + a_3 x_3 + b = 0$.

It holds true for Plonk and PlonkUp and, in general, is a pretty reasonable assumption, but it should be possible to turn it off if needed, I think. And, we should be careful with input variables.