Optimize zkEVM throughput
ETHCC 7 · 11 Jul 2024 · 1:04:59
Carlos Matallana and Ignasi Ramos of the Polygon zkEVM protocol team lead a workshop on optimizing the zkEVM by tuning its ROM, written in ZK assembly. They explain the language's registers, counters and instructions such as rotate C, and why Keccak is costly in a batch (about 2,000 per batch against 265,000 Poseidon hashes). They then optimize two functions: verify Merkle proof, by unrolling the loop and letting the prover supply the path bits, which the code then checks against the key, and getLenBytes, where they say steps fall by roughly ten times.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Welcome to the last talk. Last workshop, sorry. Hello for the CC. After this, we we have the closing ceremony, so let's start. Yep. So the slice. Okay, welcome. We are Carlos and Ignazi. We are both part of the protocol team of the ZKBM at Polygon. Today we are going to do a talk about optimizing the ZKBM throughput.
It will take a little bit less than an hour, so it's a workshop. If you have any questions, please uh interrupt us and we will solve it. First of all, we it's you don't really have to to code anything. We will be explaining what we are doing, we will be showing code examples and explaining them. But if you want to have access to those files that we're gonna talk about, you can check this repository on this QR. Okay.
So um there are different ways of optimizing the ZKBM, but the one that we are going to talk about now is to optimize the code of the ZKVM itself. This means optimizing the code of the ROM. Um the ROM that it's like the code that is consumed by the executor for fulfilling the the execution trace, it's done in a language that is called GK assembly. So most of the part, probably like half of the of the workshop, we will be talking about the GK assembly. And it's important to understand this language and the nuances that it has to understand the optimization that we that we will do.
Because after explaining the ZK assembly language, we will be going deep in explaining the optimization of two different functions: verify Merkel proof and getLen bytes. So let's start talking about this GK assembly language. Sorry for my voice, it's a bit a bit broken, like it's the last day. So the GK assembly language is a language that it's been developed in in-house in the Polygon CKBM team. And the particularity that it has is that when it is compiled compiled with the with another in-house library, another in-house compiler called GK compiler.
It outputs a build, uh a JSON file that can be then be consumed by the executor so that it can completely define the execution trace of a batch when it's being processed, and it can generate a proof with all the all the constraints that follow the EVM so that the ZKBM behaves exactly the same way as the EVM. Here it's just an image of how this assembly language uh works. Probably now when you see this, you don't understand anything.
Our objective is that after the workshop of today, you at least understand a little bit what this instruction does. As I said before, we input a ZK assembly file with the ZK assembly code to the compiler, and we have that instruction uh instruction file set that I will show and I will explain now. Let's check a little bit the code, just as an introduction of this language. This is what I will do now, it's like a very high level, and then we will go a bit deep on the instructions. So it's not my laptop.
Okay. It starts at the main CKSM file. Here we have uh we can see how other files are included. It's like an import in other languages. Um or you can also import like libraries, and here you can see this is what we call a label. This is for two reasons. One, well, for yes, the the objective of labels are two. One is like to organize your code, but also for control flow um conditions that we also will see later.
Now I will explain these two lines. After I will compile this code. This is the ZKVM. So I will be compiling all the code of the CKBM, and I will show you the build, how does it look for particularly these two lines? So you will can understand how the executor interprets the compiled code. So the first line, this is a register called step. This register evolves among all the execution, and the value of a step is the current step where we are in the execution. And now, as we have just started, this is the first line of the execution.
The step, the value is zero, and the step value increase on each line. So here it's zero, one, two, three, and so on. And what we're doing here is an assignation. We're assigning the value of a step to the register A. So after executing this, I think it's easy, but A will be zero. Because we are assigning the step value to A. On the second line, we're using a function called assert.
This function compares the value you have in the left, we call it op zero, with the value of register A. And it checks that they are exactly equal. In case they are not equal, the execution will fail, and the proof will call will cannot be generated. And if they are equal, the execution will continue. This will be like a health check. We do this here because we want to be sure that this part of the code is only executed at the very beginning of the execution. Now I will show you how to compile.
Okay. We have this script of JavaScript that looks like looks like this, this is the important part. As you can see, we have a library called GKIASM, well, a script that is the compiler. We select the file that we want to compile. And with the O flag, we select the file, the file name where we want to get the output. We run this.
And now we will go check how does it look compiled. This is a huge JSON where each one of the of these objects is one step of the ZK assembly of the execution. And we will check now the two first steps that are the ones that I've just commented. This is like the JSON file that is consumed by the executor to process the batch. As we can see, the first step has two levels, the inner step with value one, which means that we are getting the value from the step register, and the set A is also set to one.
Means that we get the value of a step of a step and we set it to A. These other three levels, line file name and line string, are more like metadata for the bag. And the second step we have a constant which is zero, that is that zero that we put here in the main. KBM define it so that it behaves exactly the same as the ZKBM.
As you can see, we have, I mean, we've only seen the first two lines, but there is a lot of code, for example, we have done all the opcodes of the EVM with ZK assembly, the add, mul, soup, um, log zero, log1, so SHA3 that it's used for ketchups, and so on. Also, not only the opcodes, but also all the logic that there is behind a create, context, depth, um, gas consumption, whatever. Okay, now Carlos will go a bit deep in all the on all the resistors and all the functions, it's like all the tools that we have available to do this.
Yeah, hi. So basically, you can think about this language, the zero knowledge assembly, ZK assembly, like an assembly language. If you have coded in the university an assembly language, uh probably you are very familiar with registers and instructions. So this language is an assembly language, I think basically the same. It's about registers and instructions. So uh we're gonna I'm going to introduce uh at the very first all the registers that are available in this language and later on all the instructions. So there are at the very first, there are two kinds of registers.
We have the very first register, it's a register that it's an array of elements. And each element is a Goldilog prime field number. This Goldilocks, we use this Goldilocks prime number because it fits very much into the ZK wall. It's very um effective over that. So this is basically uh the prime field number is 2264 minus 22 plus one, which is basically 63.99 bits. And since it is 63.99 bits, we are only using the less significant bits, the 32 less significant bits. So if we join the less negative bits of all the array elements, we will end having 256 bits.
And the other type of the register, it's a register that only have one element, and this element, of course, is the Goldilocks prime field number. So now we are going to see all the registers available that we have in the language. Okay, so we have this kind of uh generic purpose registers. Um we call it A, B, C, D, and E. We have the state root register, and we have the rotator left C register. These are very special one. Uh Ignazi will explain in his uh function better how to use it because it's very tricky, I would say, and very special to the ZKBM. All those registers are this type of register which is made by eight field elements.
And then the rest of the registers are made by just one element. And these registers are basically what we know uh what we call it the ZK counters, which are which are the resources of the zero knowledge wall of the batch. We have the context, stack pointer, program counter, gas, zero knowledge program counter, return stack, max meme, and hash box. You can see here that we have designed this language in order to fit the ZK EBM. This is why we have, for example, this uh smart contract prompt counter, which basically checks at which line the smart contract is executed.
And we have also the zero knowledge program counter, which is the prompt counter of the ZK assembly itself. Okay, so it's designed to fit the ZKBM. Okay. Okay, I have explained it a little bit, all the registers that they are used. Now I'm going to explain very quickly all the instructions available that basically the instructions take some resistors and do something to those registers. We will get into the details of the instructions that we'll use later on in the verify market control function and the getLem bytes. But all the instructions, I will explain all of them a little bit.
So first of all, uh I would say typical instruction from assembly language, which is memory load, memory store. We have three different hashes, KAC, SHA and Poseidon. Later on we have the I would say standard also control flow instructions, which is uh the jump instruction will have conditional jumps. Some of them are a little bit uh special uh because that they are more uh efficient, which is the jump negative, which is uh at the very end it's a conditional jump if the value to check if it's zero is if it's negative or not. Then we have the jump carry, which basically depends on the carry if you do a binary or genetic operation.
We have these uh call and and return, which are they are basically kind of a conditional jam because it's more efficient uh because when you do you just do the instruction of the call, you just save the zero knowledge program counter, and when you call the return, you can return to the previous point. The assert operation, this is very important. Uh Ignazzi already mentioned, and and basically, this assert operation is kind of asserting that uh two values has exactly the same value. Otherwise, and this is very important, the proof cannot be generated.
And we'll see why this one is very important in the verified microproof. We have two instructions in order to interact with the Merkle tree with the storage, which is SLOAT and Store. We have the arithmetic instruction, which is the addit. This basically mostly uses for doing uh multiplications and divisions, and then we have what we call the binary instructions, which is the add subtraction less than less than equal and or an exha. As you may know, uh the AVM, the memory of the ABM is aligned to eight bits, but uh we are currently working the ZKBM with 256 bits.
So when you want to retrieve some value from the ZKBM, uh you need to kind of query two different memory storage slots and join them all together. This is I would say not easy in the zero knowledge uh wall. Uh so we build this kind of instructions in order to do it quite straightforward. So we have this memory aligned reading, memory writing, and memory writing eight. You can see here that the memory aligned write eight, it matches the opcode of the EVM, which is the memory write eight. That's why we we did it.
Okay, now Ignazzi will explain some examples and later on I will explain mine. Yes, thank you. I will explain some examples of the instructions that after we will see when we show um the performance in the approaches of the of the functions. This one uh it's a bit tricky like to understand, but I think it's very important to understand how really the CKE assembly language works, which are the the resistors itself. As Carlos said, we have five registers of each element of Goldiloc prime file number each one.
So we have eight field, well eight elements of 32 bits. This is like 256 bits, 32 bytes. And imagining them like an array, we can like sort them from A7 to A0 for the case of A. So A0 has one element of 32 that we call 7, one element 32 that we call 6, and so on. So how we assign values to these registers. For example, the most straightforward way, it's this first one, which we're assigning 5 to A.
It's very important that when we handle registers without using binary instructions, for example, the ones that Carlos said, like equal or or the add or the sub, when you use these instructions, you are operating to all the elements. But when you are doing assignments or for example, here in the bottom, this addition without using a binary, you are only operating on the last element. So in this case, this 5 to A assignment, it will not alter A7 to A1, that they were zero, so they will continue being zero, yes, this will alter all the value of five.
We'll put a7 to c7, a6 to c6, so that at the end, after executing this line, a and c will be exactly equal. Now let's check like a more like extreme case where we are assigning 32 bits to B. We are assigning this big number, which is 2 to the 32 minus 1 to B. So B7 to B1 will not be altered, and B0 will get all the value. As you can see, it's all F because it's 2 to the 32 minus 1.
So what happens now if we add iA, that if you remember it has a 5 and B, and we set it to B. As we are not using a binary instruction for this addition, the only element that will be affected will be B0. So B7 and B1 will remain unaltered, and B0 will have this big value, which is this F plus 5. So it's more than 32 bits. This will, in our mind, it's like an overflow, but this will compile because we have uh we also have symbols, and so we can have negative values in the elements. So it really is not 32 bit but 64.
But you really need to understand this behavior to know what you're doing and don't fall into issues or um like unexpected behaviors of the ABM, of the Zig ABM. So this value will be interpreted like a minus 4, I think. So a negative value. Now let's talk about rotate C because if we want to interact with the other fields, not only A0 without using binaries, the rotate C instruction is very useful. Imagine we have a super big number that we put at C.
This number is a 32 bytes number without zeros, all full filled with all the elements. Here I have like split it in the eight elements. This is like C7, C6, C5, until C0, and all of them have values of less than 32 bits. Imagine that each uh I put it here like an array from C 7 to 0. So when C, the value of C is this one, the value of rotate C is exactly the same, but moving the most significant element to the least significant element. So this blue element will go to the N.
That makes us that if we use rotate C like this with operations of additions or subtraction without binaries, we will be using the value that is in the most significant byte element. In the other way, it's not possible. If we can if we don't do it with the rotate C instruction, we could not do this. So that is very useful, and we will see that it really improves a lot the performance on the function that we will explain later, the getLen bytes. More instructions, these are I will go fast, I think are very straightforward.
These are control flow instructions for jam jumping from one line of the execution to another depending on some condition. And we have, and they this jump n means jump negative. So if the value that you put on the left in this case is A, we call it up zero. It's negative, it will jump to level the level that you put here inside. But it's important to understand that this is only affecting to a zero. So it's only affecting to the last element.
And the same for the other instructions. Jam Z means jump zero, so it will jump if A is zero, jump no zero if A it's not zero. And these ones, jam carry and jam no carry, they always go with another instruction. For example, this one, Lt, that means lower than. So in case A is lower than B, you will be jumping to uh to the function to the function called well to the level called carry. Else to level call no carry. The lower than instruction consumes a binary.
This means that in this case we are comparing all the elements of the resistor. It's not the same as as jam no zero or jam zero. These are like nuances that you need to really have in mind when you are developing on this language. They sometimes make it more tricky, but on the other hand, they give you some performance advantages. Now Carlos will explain others. Yeah, I will explain the following ones which are very related to the verify market. So the first one would be the hash k, which is basically a kickak.
And basically those instructions are at the very end, you just add bytes to a buffer, and this buffer, then you kind of close the buffer, and then you get the digest. Then you do the hashing. And basically we have three different instructions: hash k, hashcalan, hashcala gen. Hash K, the very first one, it's that you adding bytes. How do you add bytes? Well, basically you need to specify the offset from which point do you want to add the bytes? How many bytes do you want to add?
And imagine that you have a lot of buffer, so somehow you need to address at which buffer do you want to add the bytes? This is done basically at the time to select the buffer where to add the bytes. This is done by the register E. The hash post register we determine where to add those bytes, basically the offset, and the register D we determine how many bytes are we going to add, which is basically the size. Then when we have all the bytes that we wanted, uh they are added to the buffer, then we kind of close this uh buffer of bytes with the hashkalen instruction, and then in order to get the digest of the hash, we will use the hashka digest. So here it's a trivial example of the code.
So basically we want to add 32 bytes at the 32 position of the buffer, determined by the register E. So here we are setting at the very first, we are setting where do we want to add the bytes? Or from which point? Okay, hash post, we said 32 to the hash post. How many bytes do we want to add? So we set 32 to D. Which bytes do we want to add? Those bytes are placed in the register A. And in which buffer uh do we want to add those bytes? So it depends on the register E, basically.
Okay, I already explained the asset, but uh I explained it uh again because it's very important. Um this is the the assert instruction, and the assert basically takes the register A and takes its input, which we call it OP, and then it compares to each other. So here basically these two lines of code um will never be verified because you are assigning one to A and you are doing an assert A against, I mean one against two, so this will be never be true, so the proof will not be generated never. This is very important, and we will see why in the verify Merkel proof.
And the very last one, because now in the code that I will show, there are some uh sync tax that uh we didn't explain, and which is this syntax is basically what we call a free input. A free input is a value that the prover provides to the to the system to the language that it's not uh verified, so it's selected by the prover. That's why we call free input, because it's it's an input that is selected by the prover, but it's not verified at all. Okay, and basically the syntax is like this kind of dollar, and in in the middle, you can use kind of a very basic syntax language in order to tell the prover what to put over there, but in reality the prover can put whatever he wants.
And here the basically the main question is why are we doing that? What are we doing with those optimizations? And and at the very end, we have limited resources in the ZK wall. Um we have this concept of a batch, and we can fit a lot of transactions into the batch. But those transactions are limited. Why are limited? Because the batch has limited zero knowledge resources. And this zero, what are these zero knowledge resources? This is basically all the zero knowledge resources that we have and the code that we have in the ZK assembly in order to compute them.
But these are basically the same, but in a better slide. The arithmetic, binary, memory aligns, Kikax, Poseidon, Poseidon padding, Poseidon, and Shadow 15056. And these values over here are all the resources available in the batch. Once I mean if the batch reaches any of these limits, you cannot finish the batch.
So you use all the resources. And here I think it's important to mention that you may hear about that we use some specific hash functions because they are very zero knowledge friendly. So for example, here you can see that the Poseidon hash function, we have uh 265k available. So we have a lot of positives. We can do a lot of positions into the batch because it's a hash function that it's ZK friendly. But instead, if we look at the maximum key card that we can do in the batch, we have only 2k available kickers. Because kickards because Kikak is not a hash function that is thought to be used in ZK.
And it consumes a lot of clocks. It consumes a lot of zero knowledge resources. That's why we can only do 2K kickax and we can do a lot of Poseidon. Okay. So time to see some code. I hope you are prepared. I'm going to explain a little bit how we code at the very first time this functionality, which is the verify Merkle proof, and how we optimize it using two techniques. Okay, what is a verify Merkle proof function?
Basically, you provide a key, you provide a value, you provide an array of siblings, and then you navigate through the Merkle tree to the top, and then you compute the root. And probably this route you want to check it against like a trusted root. If it matches, it means that the verifier the Merkel tree proof is okay. So it means that the key and the value are indeed contained in the Merkle tree. Here, the important thing to mention is that the key that we are using in the Merkle tree defines the navigation path from the less significant bit to the most significant bit. So for example, if we got we we have a key of three, which is 011, so the less significant bit, it's a one, so we go to the right.
So we go to the right, and the last level is a zero, so we go to the left. So the key, uh the binary representation of the key defines the navigation path of the Merkle tree. And here what I want to show you is how this function looks like in standard language. So this is the function in JavaScript. One, two, three, four, five. Six lines of code. Not bad. And basically, we just take the index is the key, by the way. So we take the index, we took the less significant bit.
If it's zero, then we hash to the left. If it's one, we hash uh to the right. The left means the values on the left, the sibling is on the right. And if it's one, the other way around. That's it. Then we iterate 32 times until reaching the top of the tree. And the very last line is what I mentioned. You have a reference root, so when you compute your top the Merkel truth, the Merkle proof, and you go to the top, then you compare the root that you have been computed with the root that is trusted root. So this basically this function returns true or false.
Which I straightforward. Let's check that function into Solidity. This is the solidity function, and again, one, two, three, four, five, six, yeah. Eight lines of code. Straightforward. It does exactly the same. It goes from zero to 31, depending if the binary representation of the key is one or zero, it hashes to the right or it hash to the left. It reaches the top and then it compares. Exactly the same as the JavaScript. Okay, so let's see how this uh function looks in the GK assembly, the very first implementation of the ZK assembly.
this is in the utility. Okay, this is where this function starts in GK assembly. There are two sections of this function at the very beginning that I'm not going into the details because are very specific to the zero knowledge language, and I don't want to explain them because it's not the purpose of this workshop. This is basically checking the ZK counters.
Okay. And this is computing the leaf value. Okay, also. The important thing here, we can start this function over here. So we have here as I mentioned, we have the siblings, the trusted root, and we have the key and the value. So here we have the value, which is in the register C. Okay. This piece of code also we don't care about it, which is a handling of uh special um case. And then we enter the loop.
No. Here is the loop. This is how a loop looks like in zero knowledge in the ZK assembly. So basically, this is the D, this is uh how many iterations uh are uh in which iterations uh in which iteration uh we are in the loop. This is and this is the maximum iteration, this is a constant. So at the very in the very first loop, we will have this a constant 32 minus zero, which is the very first iteration. And basically, this is like uh this is breaking the loop.
Okay, so the D is um incremental, so zero, one, two, three, and when we are at the very last iteration, the D will be 32, 32 minus 32 will be zero, and here we have a jump zero, and then we kind of break the loop. And here in this piece of code, what we are doing in this piece of code. Basically, in this piece of code, we are getting this is the key of the tree. It set it to A, to A, register A. And then here we do kind of uh we have a table in order to get the values two to zero, two to one, two to two, two to three, and so on, and then we just do a bitwise operation and and operation.
So we know that if the bit zero is one or zero, if the bit one is one or zero, depending on the loop. Then depending on the B, if it's zero or one, we will hash to the left, to the left or to the right. Exactly the same as the JavaScript and Solidity. So if here's basically this is kind of the the decision to make. If it's zero, we will hash the left, so we hash the value and the sibling. Uh if it's not zero, it will not jump here, so it will continue the execution, so it will hash the sibling and the value. And then when you finish hashing right or left, then you go again, you increment D by one, and then you go again to the verify Mercury proof loop.
Level one. It's zero or it's one or it's zero. Let's hash left or hash right. Level two, blah blah blah, blah blah blah, until level 32. When the level 32 is reached, then we basically compare. This is the last line in JavaScript and insolity. So you are basically comparing the trusted root with the one that you have been computed. And here an important thing to mention is that here we are not returning true block files. Here when doing an assert.
What did that mean? It means that the inputs of the Merkle proof are deterministic. So the inputs of the values should be deterministic. Otherwise, the proof cannot be generated and it should always be exist. Okay, so here. And here is the probably the most uh difficult part. How to optimize that.
Well, let's see first. Let's see first how many resources this uh consumes, this implementation consumes. So here I will just run that. Okay. So this function to compute uh the verify Merkle proof, it consumes 33 binaries, 33 kickax, and hasn't an 11 steps. Okay, so now it's time to think about how to optimize this function. And basically we came with uh two different optimizations over here. The very first one is uh what we call um rolling.
So instead of having a loop, you just have the same code repeated 32 times. So with that, the code looks very ugly, of course, but you don't need to maintain this incremental the you know e0, one, two, three, you need to to keep track of that. Yeah. Like couldn't the compiler, for example, like control it on itself because uh like the iteration count uh the iteration indices that they're constants anyway, uh like they would be the compiler could do that, but it's not straightforward. So basically, here what we do is that this could be done by the compiler, but uh it's very difficult.
I think it's very difficult to detect when it's a constant, when it comes from, and and so on. But it maybe it could be possible, good idea. Here we think about the this uh so this optimization makes the code very ugly, but it saves steps, it's because you don't need to increment the loop and you don't need to to check if the loop is at the final in order to break the loop and so on. This one of the optimizations, and the second one is like okay, uh why if I have the the key of the Merkle tree and the key defines the navigation path, why computing the navigation path? If you have the key 0, 1 1 in the example in the slides, I'm taking the the less significant bit, which is one, and uh in order to do that in the zero knowledge assembly, what we did is this uh bitwise operation.
That let's go to the next one. Let's go to the next one, and then you get all the bits. So instead of doing that, why not let the prover do whatever the prover wants? Let's make them free inputs. So let the prover put the values that he wants. And at the very end, I will verify that what the prover has told me is true. So instead of computing, we are verifying. This kind of a shifting the way of thinking how to code. It's not, I would say it's not straightforward.
But it's a trick. It's a trick. And here we will see the code right now. So at the very beginning, this is we don't care about that, is uh checking the second counters, computing the leaf value. We have the value again, we have the value in the register C. And here we are starting starting the unrolling. And you can see that we are we are going to do exactly the same for all the bits from the bit zero until the bit 31. And here, as I mentioned, this is a free input.
So the prover could set the value that he wants over here. What we're doing is that basically we are asking the prover, hey, tell me where to go, to the left or to the right, whatever you want. If you go to the left, I will hash it like uh value and sibling, and I will do nothing more than that. But if I go to the right, it means that I have a one. So if I have a one, it means that I need to accumulate. Because at the very end, the key could be represented as a linear combination of all the bits.
And basically, if the prover wants to go to the right, I will accumulate that bit. If the prover goes to the left, I will not accumulate. And this is basically at the very end, at the very end of all the unrolling, I will check that all the navigation path, all the accumulating values that has been accumulated during all the loop matches the trusted key, the key at the beginning. So here, for example, so the prover will choose left or right, depending if it's left, it hash value and sibling. If it's right, it hashes sibling and value, and it accumulates the value.
And this is basically two to zero. And blah blah blah. Okay. Exactly the same until we finish the loop. And when we finish the loop, and this is the important, this is the important uh line here. Once we finish the loop, we are telling, hey prover, you have told me this navigation path, and I have accumulated the value. And this value must match the one that I trust, the one that was already in the in the ZTBM.
And this is why we put an assert over here. So there are deterministic values that the prover must provide in order to pass this assert. And then when we finish, then we go to the verify Mercury proof, and this is exactly the same as the previous one. Let's see the performance. So here you can see now that the performance gains are quite a lot. This function is used for every block that we change inside the batch.
So it's very user. And you can see that we have gone from 33 binaries to just one binary with this trick of let the prover use whatever the prover wants. Okay.
I think that now Ignazi will explain his function. Okay, we are reaching the the end of the workshop. Let's talk about the getLetBytes function. This function is far more I think easy to understand and simple than the verify Merkel proof, but I really think it's very interesting to understand the mindset that you need to have when you are trying to optimize the ZKBM. So first of all, um as the name says, getNet bytes, it's easy, it receives a number, and it returns the length in bytes of this number.
Here it's not like in JavaScript that we have a dot length instruction, and that's all. We have like to compute it manually. Well, we'll check this is the performance. Okay, let's see the. You have to remember that a resistor is up to 32 bytes, so the maximum length will be 32.
But how to compute it without with the tools that we have, you don't have this dot length instruction. So I put here, as you can see, this is the function, the input in the number is on register B, and we expect to have uh a return of the length in the register A. How we do it, well, at least this could be probably like the first approach that can come to the mind of someone. It's imagine I have this number in B. This is the input. A way to know the length of this is write shifting.
If you shift write one byte, you will have this number. If you shift write again, you will have this one, again this one, and after every shift, you check if the result is zero. If the result is zero, you just count how many times you shift it, so you will have the length of the byte. And this is how it's done here. As you can see, we have a loop. Every time you see a loop in CKAssembly, I'm sure that there will be a way to optimize it. So in this loop, we have a counter, which is how many times we've done the loop and we have like a a return function to leave the loop which is the get length and first of all we have the the value at B but we set it to A because we need to use the equal instruction.
The equal instruction compares A and B. So first of all we check if the input number is zero. If it's zero the length is in by zero so we are done. If it's not zero we shift write and we increase the counter and we do exactly the same again and we just count how many times we did the loop and we have the length the C was the counter we set it to A because we define it as we are returning the the length in A and just we return. As we can see in this function we're using we're using an equal equal it's very useful because you are comparing as I explain it I'm thinking I'm gonna go to this yes this one the equal instruction it's comparing each one of the elements between two registers but the disadvantage is that it consumes a binary counter which is something that we really want to have as much as we can available so we can fit more transactions in a batch.
At least improving the performance of the ZKVM is improving the counters and in the shift write um function we use also binary operations and also arithmetic operations. At least we are like dividing by two the this number. This is how you shift write so let's see now. If we run we have a some tests that they use this function in different scenarios so we can get the the counters that are consumed. I think I'll have it here.
So in terms of counter consumption, okay, it's sensible. Um we are using it's not this one. Just do a clear okay. We're using um 27 AD, 96 binaries, and more than one hundred one thousand two hundred steps. Like this is this is a lot. So now we have to think a way of get the same results with less counters, knowing the tools we have and how the ZKBM works, and we have a new approach here that I will explain now.
In terms of line of code, it's far more large, but in terms of counter, it's far better, so it's worth to understand this approach. Um you really have to have in mind what I explained before about the thing of the resistors and that you can operate with resistors but only with the least significant byte, sorry, the less significant element without consuming binaries. And with the rotate, we can like choose which of the elements we want to operate with. So imagine we we had this big number and we know we want to know the length.
Um, just watching it, we know it is 32 bytes, but imagine we have to code it. First of all, you have to check this uh element. If this element is zero, you then check this one. If it's zero, check this one, and so on. If all are zero, then it is zero. Okay. But let's see how can we see it in the code. First we have the input, which it's like this, and the first we think we do that, sorry, the first thing we do is that we rotate because we want to operate, we start operating with the most significant element. When we have the most significant element at the least significant position, we check if it's zero.
In case it's zero, we will rotate again and we will check element six and so on. In case it's not zero, let's check. If this number is not zero, the length of this input can only be, I think 29, 30, 31, or 32. Let's see which one is. And now, as we are operating with just one element, we can do um this subtraction operations within without the spending a binary. And the first thing we check if is if this field, this element, element 7, that now it's in the least significant position, it's lower than this number.
So it means that the value is the value is the value is this number or lower. If it's this number, I mean if the if this element is this number or lower, it means that we have indeed 29 bytes. If it's not, we check the next one.
Which will be, I'll do it again so that it's more visible. Here we are checking if the value is lower than this. It means that it's this number or lower. If it's this number or lower, it means that the number is between this and this, because I already checked this one. So this means that the length is 30. And you go doing this, so on. So you can then you check it with the next byte, so it will be 31. And if it's greater than this number, it means that the length is 32.
And you do this checking all of the elements rotating. In case we have in case slot six is not zero, we are talking of a number that has a length of uh 25, 26, 27, or 28 bytes, and so on. Let's see the results in terms of counters of this exactly. And well, we have a function returning each one of the numbers as it's just 32, so you can just type it. Are these ones?
As you can see, we are only spending steps. This is a huge, a huge optimization because steps is one of the counters that we have more. And we have and anyway, we have optimized it so far as you can see. Here I'll show both of the results. We move from 27 AD, 96 minories to 00, and the steps divided by 10. So it's a very very good optimization. I think this one is very easy to understand and also very good to understand how the ZKE assembly works and what do you need to have in mind when you are doing like this kind of logics? The thing of the counters, it's really uh important in terms of performance and in terms of the price of the batch, and it really affects a lot.
So I think that this will be everything from our side. Finally, if you have any questions, you can ask now. I don't know if we have time, or we will be here around. Thank you. Oh, question. Thank you for the presentation. Uh maybe I missed something. Why don't you use a sub registers like it like it can be done in X64? Like sub registers. Why don't you use subregisters for like you have A, B, C D? And why don't you use sub registers for uh A uh A1, A2?
Like it can be done in X64. You know, you have a EAX, which is a subregister of error X, and work with that. You want to for example, having A and then also A1, A2, A3. So that will make it. As subregisters, yes sense. Yes, I think, well, I think that this is a matter of uh making the compiler uh less complex. Like well, basically we are working with the positives and with the with a prime file number, which is the Holdy logs. And basically we want to build a register of 2005 bits.
And we are taking only the 32 bits, less bits, because the last bit is problematic. Because in the prime field number that you cannot rely on the last bit because it can overflow. Because at the very end the prime field number it has 63.99 bits. It has no 64. So we need to build a 256 bits from the less significant 32 bytes of each prime field element. Um yeah, so i i for uh several operations if you want to extract just one uh like uh I don't know one batch of six bits. If you have a sub registers, you can you could directly access them without having to rotate.
And this is how the state machines work internally. So the the binary state machines, it's it does some execution. Um and basically this as we done it's it's divided in what we call steps or clocks. So it takes the eight bytes of the eight bits, and it I think the binary it works it in eight bits and eight bits, eight bits, eight bits. So the the state machine that we have not talking about um anything over here, um it takes it it does what what you want, uh what you're thinking about. I think.
But I mean I think you could have like a one, a two, and when you update a one, you update the first opposition in terms of compiler, the first opposition of A. I think I'm maybe I think this but is this this what you're saying? Like you want to mention. Having registers in the CK also in the ZK processor is quite expensive. Mainly because when you have a register, if you are using a register maybe once every uh twenty instructions, this register is worth worthless. I mean it's uh it's you need the polynomial, I mean you need you it it occupates the cells and you are not using.
So here one of the things that you are trying to minimize when you are doing a CK processor is minimizing the number of registers. The other point is um I mean uh maybe why you join them together. I mean the the the other thing that's expensive is uh when you have a register, it's not only having the register, you need to be able to move the register, uh to set registers to so to put something in the register to remove something from the register to me, all the logic that goes around the register. So that's why we are trying to minimize uh the number of registers that we are using, and that I mean that's general in the ZK and that's that's very much why a sub register is not a new register, it's just a sub register. I don't know, I call it a sub register because it's sub yes sub, yeah, sorry, French accent.
Uh specific register for a sub register, but sub register. Like in X sixty four, um EAX is a sub register of uh R A X, like so the the register is A and yeah, no, like yeah, yeah. Uh the uh actually uh yeah, I mean uh so For example, see this is I mean I mean if uh so we already have subregisters because we have a zero one or two, okay. But having instructions that work with sub registers, so each instruction, it's different instruction requires like another another specific uh uh polynomial.
So I mean this would increase the having the I mean having instructions for subregisters, this would increase the number of polynomials in the in the ZK machine, and uh this would be much more expensive. So because I mean this sub-register instructions, I mean you are paying for all the instructions. So and sometimes it's better to pay a little bit more for some specific registers, even if you have to do some more complex things, that having uh full subset of instructions in this case for subregisters for uh uh doing some specific operation that's useful. I mean, maybe it's useful for this, but you are paying when you need to pay for all uh state machine.
So that's mean this is always uh compromises, and this is the I mean this is a little bit the science of building processors uh for ZK. I mean that the costs are not exactly the same that when you are doing electronics. I mean when you are doing uh polynomials, the the costs are uh different, but it's the are always trade-off, and uh cool thing is that I mean all these things can be always get better improvements uh here yeah, uh just a small question, like a bit dumb, but uh is it you who is building the ZK wasm uh format, or is it like the W3C or some uh institutions like uh developing Wasm regularly? ZK Wasm?
Yeah. I mean well we uh started to look into kind of interpreting um I mean having in the in the ZK assembly in the ZK assembly trying to interpret the ZK Wasm, but uh we realize that uh this is very specific this language is very specific to the ZK VM. So probably there are other uh architectures that fits better now to the GK Wasm, for example uh ZKBM, not EBM, because it's more uh I mean uh as as as you can see there are some uh registers and it's built it in a way that is very focused on the ZKBM. Um I'm curious with your get len bytes optimized version, will it use fewer steps if the bytes are longer?
Um yes, yes, the number of steps it's variable depending on the input. So not constant. Did you make a sort of strategic choice that you thought you'd be measuring more long bytes than short bytes? Uh what do you mean? Can you repeat please? Did you make a strategic design choice that you thought you'd measure more long bytes than short bytes, so you'd make it faster, uh use fewer steps for short for long bytes than for short bytes, all right? Uh yes, like doing two specific functions depending on its long bytes or yeah, or I suppose you could have rotated right instead of rotated left and built the same function and it would have been faster for short than long. Uh yes, yes, you could be this um, but I um with the rotate we have you could do like a new rotate uh register, but the one we have you can only rotate one side.
I I would also say that this is a very general function. And for example, is if a function is used a lot, for example, by the Uniswap swap, then we we can do what what you propose, no? Uh do a specific get length bytes for shorter bytes, longer bytes or whatever. So um you can squeeze no uh uh the steps or doing another optimization over there. But this one it's was for the purpose of a general you know implementation. I kinda have a follow-up question that we can have you guys thought about you know the future and uh in case you guys need to update the instruction set and you know, because I just thought about you know the X eighty six at the very beginning where they had to uh double the register size and the bench become you know X eighty six sixty-four, which is not really backwards compatible.
Uh in c just high level thinking if in case you guys need to add some other uh instruction or update the current ones. Have you thought about like how's how's that gonna look like? Yes. Good question. So yeah, um in fact we have uh we have a branch in the in the there is the ZK assembly repository, which is basically the compiler. Um basically we are using here a a version of the compiler, but in the previous ones uh we didn't have the MEMA line, for example. Um we have more uh binaries, but then we the we decided to remove them because uh we can't reuse the length.
I mean, there is not a greater than because it consumes you need more polynomials and then you decrease the efficiency. And basically we are iterating. And at the very end of the function, you need again to recover those registers. And basically it is consumer steps.
Okay. So Feli, one guy that worked with us, came up with kind of hey, we could add an extraction that basically it's a safe, safe instruction. And this safe extraction, it's one step. But it will save a lot of steps. And this instruction basically what it does is it saves all the steps in a polynomial, and later on you can retrieve it. This is just an example, but and this is in the very last version of the compiler. But yes, I mean, since it's a new language, a new compiler, we are realizing things, you are realizing which are the bottlenecks, which are the top offenders, which are.
I mean, while you are iterating and you are moving forward, um the language is evolving basically. Thank you. Thank you for thinking for being here. This is the last workshop of the HCC. What are you doing here? Thank you, Brandon. I didn't expect too much people.
Related talks
- zkEVM Vs EVM: Full Equivalence? by Ignasi Ramos | Devcon Bogotá
- Top opcode offenders in the zkEVM
- ZK-EVM
- Lightning Talk: Native Rollups by Luca Donno, L2BEAT
- Kakarot zkEVM: beyond ethereum compatibility
- zkSync 2.0 update, Exciting Ethereum things and more - The Daily Gwei Refuel #411 - Ethereum Updates
- How to Scale a Blockchain Motivating the Ethereum Rollup Centric Roadmap
- IRL Workshop: Build on Ethereum, Hanoi Day 2
- Optimize zkEVM throughput: Series II