zkFold / zkfold-base

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

Optimize `instance SemiEuclidean (UInt n r c)` using new `MonadCircuit` interface #356

Open TurtlePU opened 1 week ago

TurtlePU commented 1 week ago

Currently, the divMod operation performs bitwise division. Under deterministic computational model, this is the only approach; with the new MonadCircuit interface, we can access the full power of circuit computational model to reduce the number of constraints dramatically. The idea is: instead of "computing" divMod x y, we can "guess" the result d and m s.t. x = d * y + m and m < y. Outline of the algorithm:

  1. compute witnesses for d and m;
  2. range-constrain them so that values inside fit registers;
  3. constrain witnesses of m s.t. m < y;
  4. constrain witnesses s.t. x = d * y + m.