zkEVM #03 - Devconnect AMS'22

Devconnect Amsterdam 2022 · 27 Jun 2022 · 18:55

A research session from the zkEVM gathering at Devconnect Amsterdam 2022 on non-native, or wrong-field, arithmetic in zero-knowledge circuits. The presenter walks through a guide to constraining equations over a field different from the circuit's native one, as needed for proof aggregation, recursion and ECDSA over curves such as P256. The stack described builds on Halo2, with the original IPA commitments and a fork that supports KZG commitments, and covers limb representation, range checks, addition, subtraction, multiplication through the Chinese remainder theorem, division and inversion.

Topics

From Devconnect Amsterdam 2022, hosted by Ethereum Foundation.

Transcript

Okay, so here's Honor. He's gonna he's gonna talk about MPT. Hi, uh it's Honor. Uh thank you for having me here. Uh I will be talking about um uh non-native arithmetic uh in circuits. Um I tried to prepare some slides, but uh in the end it it uh turned out to be uh something like uh guide uh and I will walk through this document. Um so uh equations here might not be so accurate.

My uh goal is to uh give some intuition uh how it is done. Uh so uh we uh say we have two uh fields. One is uh native and uh other one is wrong. Native field where we constrain uh equations in circuit and wrong field is uh uh which one it which is not equal to the native field. Uh uh I couldn't find how I can do that. Is there a browser?

If we can't figure it out fast, then top right corner, the triangle, right? Triangle. Wait. That's not a triangle. Oh, I don't know how to do this either. Whose laptop is better? Um so there are some applications that we use this wrong field arithmetic. Uh one of them might be uh aggregation or recurs recursion of the same uh circuit.

Uh this is uh where it's uh there's a multiplication A plus the A uh multiplies the point X and Y and X and Y is a is in base field uh which we call uh wrong field here. Uh this is a scalar multiplication and the other uh example might be the E C DSA and uh with the ECDC we use uh that curve, say P256, and uh in that curve scalar field also we uh uh is uh non-native. So in the uh recursion side uh we have the scalar in the uh native field.

So for that purpose we built some stack uh that uses holo 2 as uh backend. Uh uh we in that stack we support uh the original the ZK hello2, which uses the IP commitments and uh our fork uh it's applied ZKP slash hello2 and uh supports KCG commitments. So our stack uh looks like this uh on top of Holo 2 we have a main gate that implements uh standard like uh Plunk gate with uh additions multiplications and stuff. On top of that we have uh integer, uh big integer uh uh crate on top of that.

Finally, we have uh ECC. So uh with that stack, we can implement recursion or ECC uh circuits. So other implementations are we follow uh we mostly follow the Aztec spec. Aztec spec is also implemented in bio time back, and Metal Labs also very similar implementation. Also, scroll has uh other independent implementation of big integer arithmetic. So uh our main gate is uh looks like this.

It might not be the best way to represent a gate, but uh so uh uh I hope this uh is familiar to you. Uh in we have five uh witness columns A, B, C, D, A, B, C, D, and E, and uh two uh multiplication gates here, and we have a one further uh rotation, and in the end, a code there's a cost constant uh uh uh uh fixed code. So we have uh five-bit gate because it is good for ranging uh four limbs which we often use and uh further rotation is used for uh running sums and the two multiplication in a gate is uh good for uh uh applying conditional selections in a single row.

So main gate also implements this a basic arithmetic conditional uh assignments and uh uh equalities between witnesses. Uh we also want this gate to be feasible uh to verify on chain because this uh gate deal with the where we implement uh recursion stuff and uh this gate will be verified in layer one contract so um let me zoom out a bit uh this is how we represent uh integers uh in this example where we see four uh limp representation uh first of them are uh b bits and the last one the most significant one a bit uh shorter than the uh others so we also have another uh special limp that holds the native value which is a combination of uh uh these limbs uh under the native field so we will need uh this native value in multiplication process also in uh synthesis time when we uh synthesizing this circuit we want to keep uh the maximum value uh this integer uh might be uh to um uh defer some reductions so uh we uh apply reductions when this integer goes beyond some specific uh limit so uh when we assign new uh witness uh uh integers we range uh these limbs first uh to be b bits and last one must be b most bits um and then we also constrain the native value under native modulus.

This is uh trivial to compute because it is under their native field. So we also in synthesis time a maximum value will be assigned to 2 to the power M, which is the next power of 2 of native value. For example, we have native field. For example, we have uh 11 for the native field modulus, and then uh this value should be um 16 minus 1. Right?

16 is the next power of 2 this uh uh modules. So how we range these integers when we are assigning uh we as we said we have two columns, witness columns, we just uh split a limp into a sub-limps. Uh in our implementation this uh in our implementation a limp uh is uh 68 bit here uh and b most will be 18. But in the sub-limps we divide this integer uh into four uh sorry uh four uh sub-limps will be um each of them will be eighteen bits, so we just combine them together and uh see if it's equal to the uh actual limp.

And we also range A, B, C, D, E, A, B, C, D, uh, this four uh columns uh to be in this specific limit. Uh for the most significant bit, we have a special range for uh B column, which also ranges the overflow which is shorter than the actual table. So we also combine them to see if it is uh equal to the uh uh E, column A. So addition is uh pretty straightforward, uh we just add uh limbs together and uh don't apply reduction uh until it's uh strictly necessary then uh until it's go beyond uh some limits.

And in the synthesis time we also track the maximum value of the near witness. Uh subtraction if we apply subtraction uh uh in a way that we apply it uh addition, uh this might this probably uh this limbs resulting limbs probably uh uh underflow. So we want to stay above zero. Uh this if this underflow it can be a pretty big uh number, just less than um modulus. Uh to avoid uh underflow we pick an auxiliary value and add to the E beforehand.

Uh thisiliary value must uh equal to the uh wrong modulus, lineative modulus, but uh it should also uh move left hand side operand uh uh uh left hand side operand above B. How we can do that? Because we uh tracked uh uh what maximum value B uh right hand side can be and we pick an auxiliary value. This auxiliary value will be constant and can be take place in a fixed column.

So uh this will be these auxiliary will be assigned in synthesis time. Uh multiplication a bit uh will be a bit trickier uh but uh in the end we want to satisfy this uh equation uh two operands and the right hand side coefficient uh and remainder will be the our uh NIM witnesses and uh as they are NIV they will be assigned and uh they must be uh uh subject to two range checks uh we will constrain this equation in two uh uh fields or uh rings. First one will be uh under native field uh which is pretty uh straightforward and a single uh row equation uh we remember we keep all integers native value uh just after this uh limp so we can just uh compute or constrain uh uh this stuff uh pretty straightforward.

So uh we will also do this thing under modulo T. If they uh probably if they uh likely exceed, so we uh want to apply reduction just before the multiplication.

Uh reduction is uh also very uh um uh is actually very similar with uh uh multiplication uh but some uh simplifications. Uh quotient uh we keep quotient uh uh uh as single limb. Okay. Uh so in quotient is not a big integer, just uh uh for example sixty eight-bit uh number. Uh so yes, this is the uh simplified equation which is very similar with multiplication.

Uh equality, we say the value can exceed the modulus because we arrange it uh with uh uh next power of two of the uh wrong field modulus. So um uh if we need to really check uh this value should be under uh wrong field, we should be in a uh field, so we have a cert less than modulus constraint, so uh it can be used uh when we compare with uh some value comes from public inputs. Uh I think that's all and uh there is some uh appendix here uh about uh this uh mod T constraints uh if you want to look at uh link is uh here in uh thank you.

Um any questions for for Andrew? Okay. So is that ways to do additions, subtractions and multiplications? Do you do like all the store? Oh yes, we support uh division and uh inversion. Uh actually they are uh how we do it in the native field, just check the if a multiplication is one. Uh yes, but uh in the base we use for example multiplication.

It would this multiplication cluster to make division. Thank you. Okay, thank you all now.

Related talks