The Magic and Power of Recursive STARKs with Eli Ben-Sasson
ETH Seoul 2022 · August 2022 · 27:14
Eli Ben-Sasson of StarkWare explains recursive STARK proofs and why they matter for scaling. The talk reviews how STARK proofs and the Cairo language let an off-chain prover cut on-chain verification costs, how SHARP batches many applications into one proof, and how recursion lets proofs verify other proofs. It announces that recursion goes live in production that day and describes how a verifier written in Cairo makes layer 3 systems on Starknet possible, with lower fees, cheaper interoperability and custom control.
Speakers
Topics
From ETH Seoul 2022, hosted by Kryptoplanet.
Transcript
Thank you. Thanks for having me here. So, yeah. I want to tell you a little bit about recursion and recursive Starks and actually today is a very meaningful day to give this talk because today we're actually turning on what I'm describing here in in our production systems for for basically StarkNet and other systems that go on sharp and I'll explain all of this. So, something I'm describing here is today going live. And in order to explain what recursion is and why we're excited about it, I need to tell you about three things before that, about Stark proofs, about Cairo programming language, and about
shared proving or sharp. So, usually the way things go in our system is that we have a program that is going to be executed for a number of steps and there is an input and then an output. So, most programs tend to look this way. And after that what is happening is that instead of asking Ethereum to execute programs as it would do with let's say if you write Solidity code, smart contracts, you would ask all nodes of Ethereum to run that program and basically naively replay the same computation that you've done. In our system, what you have is someone off chain runs
the computation and then does extra work to create a Stark proof. But what you save from that is that what is send on chain is just the proof, not a request to do the computation again. And this turns out to be much more efficient in terms of gas because the verifier then checks the proof and if the proof passes the checks, then you accept the new state of the system and this turns out to be much more efficient. So, to give an example, let T denote the number of steps of your computation.
Think of T usually I have in my head a million, so like a million steps or a million transactions. Then if you would send the program directly to layer one, all nodes would need to do roughly a million steps or T steps. Instead, what we do is an off-chain prover spends more time, T times roughly log T. So the logarithm of a million is 20. So instead of a million step, it spends something like 20 million steps. But in return, what the verifiers need to do on-chain is not a million step, but logarithm of that roughly, which in the case of a million and taking logarithm, that would be 20.
So instead of a million steps requesting the blockchain to do, now you've gone down to 20. And it also scales very well because if it was a billion steps, the logarithm of a billion is actually 30 to base two. So things scale very nicely. And this is the way that start proofs work and we apply them to the execution of any program written in Cairo. That's why over StarkNet developers write in Cairo, and that's why all of our systems that are running in production for a year or two have been written in Cairo because it turns out that the proofs are very efficient.
So to give an example of how you create or how this applies to a layer two, usually in a layer two, what you have is the main thing that the L1 chain knows, so Ethereum knows, is just the hash of the state of the system. So the state is represented by some Merkle tree. At the leaves there are various accounts and objects, and basically when transactions come in, you operate on them and modify the state. So the input is usually the previous state and the number of transactions, and then you run a Cairo program that updates the state with integrity according, you know, doing the right thing that should
be done, and you have a new state. Um, and this new state, or its hash, is the output. Why? And the transactions could be a variety of transactions. You could have a request to transfer to an ether from Bob to Alice, someone else wants to mint a bored ape NFT or send an oracle price update, and all of these things modify the state of the system, and then you get a new root. So so far I explained the state of the system when you run one program. Now what is SHARP? So first of all, SHARP stands for shared proving.
And today we have already in production several systems running, and all of them need to be proved, right? Because the way our system works is we don't send single individual transactions to the blockchain, we actually batch together many transactions and send them. And we have StarkNet, Immutable X, Diversify, these are all systems that are running either in alpha, StarkNet, or in production for these other folks. And roughly a single proof costs verifying it on chain is roughly 5 million gas. It changes a little bit with the type of the transactions, but it doesn't vary very much. So it's think of it as some constant cost per
proof of roughly 5 million gas. And then if we would just send a single batch, let's suppose that each one of these applications roughly has you know, once per period they have, let's say, 3,000 transactions. So let's do some math here. If we send a create a proof for the StarkNet system, then we have to pay this 5 million gas for verifying it, and it's divided by 3,000 transactions, which comes out to about 1,700 gas per transaction, which is already pretty good.
However, if we can take together many of the of these systems and actually generate a proof that asserts to the correct update to each one of these things in a single proof, then you can get much better savings because if you have 3,000 transactions in each one of them and you send a single proof and it's roughly 5 million gas, then you get around 400 gas per transaction. Now, the actual numbers of gas per transactions that we have in our production systems are pretty much of this nature and often even less. For instance, when we mint NFTs for the Immutable X system in large batches of 600,000 per
per proof, we reach even a cost gas of roughly 10 gas per minting of a single NFT, which is way better than what you can do on chain. So, the more transactions we can batch, the lower the amortized cost per transaction that we get. And this system has been running in production for a very long time. So, I just want to pause here and say that it's something that I also said in my talk at Biddle and I repeat it often, which is that so Starkware Starkware's strength is not necessarily in in marketing and PR, but we are very strong when it comes to engineering and putting things in production after hard
work, things that work very well and rely on a deep engineering and math stack. So, the concept of sharp of taking a single with within a single proof seamlessly proving several different statements or rather more technically the thing we're proving is actually some meta statement. Think of it as some proving that some bootloader program has executed correctly where the bootloader can boot several different programs in advance each of them. This is something that is not done in any other um L2 or validity roll-up system. We've
been doing it for a very long time and it's very powerful and it's just one more innovation coming from StarkWare that again you probably haven't heard about it before this talk because our strength is actually in writing code and making systems work less so in telling the story. So this is something that has been running for a while and is making our customers very happy and saving in gas costs. But now we actually want to increase the scale even more because that's what scalability means in our eyes. It's a technology that you can 10x and 10x again and again and it has very it's not that it doesn't
have any effect but it has very limited effect on the L1 cost. In our case basically you know since the verification time is logarithmic with the size of the throughput every time roughly I mean I'm you know waving hands here but each every time you 10x the throughput the verification cost should go up not by not by times 10 but rather by plus one plus 10. So each time you increase by a multiplicative factor of 10 the work on chain additively goes up by a constant by a small constant factor.
That's a good way to think of it. So you multiply the throughput off chain you just add a little bit of work on chain. However when you reach things like 100k transactions for each one of these then putting them on a single big prover machine starts to become cumbersome because this machine would need very very large memory and a very large number of cores and there's only so much today that you can get on the cloud with a single big machine. So we need to go beyond that. And that's where recursion comes in as being helpful.
So recursion is a very beautiful and deep notion in computer science and mathematics. It's also a very very powerful paradigm. It's tightly related to the concept of induction in mathematics and it goes a very long way. So but it's also a little bit sometimes it's sort of hard to grasp because it gives you this feeling of you know like these infinite mirrors and something that goes on and on and or reverts to itself. So we'll have a little bit of feeling of that in what I described now but I hope you'll appreciate the power of it.
So right now what we have is we start with what I want to emphasize is that we start with programs and we end with programs but that operate with a smaller number of time steps. So we start with some program A and input X and actually what happens we send this it send it to this prover machine and it generates some output which is a proof. What do we do with this proof? Well this proof is fed to another program, the verifier program. So also on the right hand side we generated a different kind of input which is the proof and that proof goes into a verifier.
So this is the proof of program A. Well you're probably already thinking if you can prove the correct execution of any computation you can also prove the correct execution of the computation on the right not just on the left. And that's exactly what we do with recursion. We say okay let's take what came out on the right and let us generate a proof for it. So what would the proof say? The proof would assert that the following claim I received a stark proof of program A and I verified it and it passed the checks.
That's what you would prove now. And now you have on the right hand side you again have input to another program. I mean it's actually the same program, just with a different claim. So part of the beauty of it is that just like well, a very important thing in recursion and induction is that you start from a statement and you go back to exactly the same statement. So as long as our statements are about generic runs of computer programs, which is the case here, you can basically apply recursion or induction and do it again and again. So the statement above was I ran a certain
Cairo program and it executed correctly. That's what happened there. But the program was called A. A was maybe, I don't know, immutable X. But if you have it for the general case of general programs, basically I received some hash of a program and I hash of a basically bytecode of Cairo and I ran Cairo for A steps and received output. That's exactly what you also run on the right-hand side, but just on the left you run it with a program A, and on the right you run it with a program that happens to be a Stark verifier.
And you can do this again and again. So we can generate a proof and then send this proof again to be Stark proof. So it's verification can be Stark proof because maybe I should pause and say another thing and like go a little bit deeper. So Cairo is a Turing complete programming language, which means you can express any program in it. And the way actually the Cairo verifier works, and again this has been going on for a very long time, is that what it receives as its input, as the reference to the program, is actually a hash of the bytecode. And all our systems work the statements that we prove say we ran a certain program
whose bytecode hash is this, you know, this pattern. And we ran it for A many steps and we received a certain output. So once you prove these kinds of statements, the hash of the program can also be the hash of the program that is the Cairo Stark Verifier, which is exactly what we do with this recursion. So if you feel a little bit sort of queasy with this sort of looping and you know or this Escher kind of picture kind of thing, right? You're you're you're getting a grip of it. That's exactly what's happening and that's what we're doing.
And as I said earlier, this thing is turned on in production starting today. And then you can repeat this of course again and again because these are all just cases of programs. So why would you want to do such a thing other than to confuse yourself and feel queasy? So I want to mention a few things that you can get with this. So first of all, you can basically break this barrier of a single machine and parse proofs in a different way using a larger number of smaller machine and thereby get greater capacity and lower cost,
both L2 cost, meaning the cost of the computing devices that you're using to generate the proofs, but also the cost of L1 amortized cost per transaction. So right now we're taking a whole bunch of programs and generating proofs for them. Well, we could take one proof of a program that said I ran Verifier on input A and I ran it on on input B. So it's a new program that takes two programs, two proofs of programs and generates one proof of the program.
So it's a little bit like hashing, right? You take two inputs and you just receive one. So you take here two proofs and you generate one one proof. And now you're probably thinking, oh, so you can do this again and again and generate a tree and compress things further and further just like you do with Merkle hashes. And that's exactly what we do. So, roughly we take two programs. Each one of them runs for T steps. We have two proofs. Each one of them is much much smaller than T steps.
And then we feed this thing which is roughly you know two two log T steps and we get verification time that is much smaller. Now you would think that this verification turns out to be the log of two log T which turns out to be roughly log of log of T. But I'm not writing that because there are some absolute lower bounds that you can't go below. So you get lower but it doesn't go down to like zero or it doesn't really go down you know.
It goes down a lot and makes things much more efficient but there are bounds and I don't want to go into math explanations of where it stops and why. So much much smaller. And you get larger proving capacity from this. And then you send this final proof on chain and it covers the amortized gas cost is now even lower than it would have been previously. It also helps with reducing latency. So first of all it helps with just dealing with larger batches of proofs. It also helps with reducing latency. Why?
Because what we can do is apply this recursion tree. And now what we can do is sorry. We can basically instead of the sharp system for instance which took let's say four programs but then generated one proof which basically placed these transactions then these then these then these and operated on all of these four different batches of transactions and generated one proof which took much more time and also required bigger machine. Now what you can do, you can send each one of these uh different systems in parallel to its own prover.
So you sort of you save twice. One, you don't have to do things sequency uh sequentially. You can do them in parallel. And the other one is that um these are on smaller machines and smaller input sizes, so the proofs come out faster. And then what we do, we have already these objects that are roughly log T size and not T size, and we can uh apply this uh thing of recursion where we prove recursively that we checked two proofs and then have one output and do this again and again. And basically our latency is drastically reduced, and that's another reason we're now deploying this thing in production.
So you save twice. You don't have to wait for many transactions to accumulate. And I mean and you have these different programs for which you compute each one in parallel. And now with this case you just have to do wait for T steps, and if one of them finishes earlier you can start generating its proof. And then also log T in these further steps. Another really powerful thing again that uh I'll repeat this that you know Starkware is uh not that good necessarily with telling and spreading its gospel, but it's very good in innovating and delivering on you know technological innovation, not just in presentations like this but actually in like working code in
production. So L3 is another uh very interesting concept that that we're going to deploy. So everyone's talking today about layer two and how they are the scaling uh solution, right? The solution to scaling problems. Layer two says we can have the exact same security properties of layer one, but sort of they are somehow inductively or recursively magically applied to a next layer. And with Stark proofs the math and cryptography of of Starks assures you that indeed the security of your layer three is exactly the same as that of of layer one.
Now again, the beauty of it when you have these proofs that prove the integrity of a computation is that you you can apply even this notion recursively. So you could have another layer that sends proofs to a verifier that is on layer two. And that would be a layer three system. And now the layer two system would send a proof of transactions including that it verified a proof on a layer three and send that to layer one. And so now the same security that you get from layer one goes inductively to layer two, but also to layer three and you can continue this process, but we'll stop it at layer three.
So by the way, this is something that for instance cannot you you cannot have a layer three on an optimistic roll-up or rather it wouldn't save you anything because the witness data from a layer three and an optimistic roll-up would be needed to be rolled up onto a layer two and from there to a layer one. You don't really get much compression. So with optimistic roll-ups it doesn't make as you could define a layer three, but also the whole arbitration process becomes extremely complicated, right? If there is a fraud proof initiated on a layer three, you need the layer two to intervene in the layer one and also the witness data doesn't get compressed, but it does work beautifully with with uh with Stark systems. And in fact, one of
the things we're going to do over the coming months for all of our uh existing Stark X systems, the one serving Immutable and Sorare and these, we're going to move them to become layer threes onto uh StarkNet. So instead of proving uh we could take the Sharp verifier and uh submit it this we could take a proof generated by the Sharp system and send it to the StarkNet verifier. That would make these systems all a layer three.
And then so you have Ethereum. On top of it you have layer two, which is public StarkNet. And then you can have a whole bunch of different flavors of layer threes. Um you could have them for Validiums, which means that the data availability is not relying on L1 or on L2 in this case. You could have rollups which roll up to layer two and then from there to layer one. And you can even have custom StarkNet systems. So if someone wants let's say if I don't know, city of Seoul wants to have a StarkNet that is dedicated only to you know, local residents here, works at higher speeds um then you could have such a system
that feeds as a layer three into the StarkNet layer two. And then you have all of these different proofs. They are sent. Now they're sent to a layer two. There is a program already existing today. That's the program that uh that's basically the recursive verifier. So to get recursion done, what we did is instead of writing the verifier in Solidity, which is how our layer twos work, they report to a verifier on layer one.
We wrote the verifier in Cairo. So the verifier for a Stark proof of a Cairo run is now written in Cairo itself. And Cairo is the language of layer two. So you can write this you can run this as a smart contract on layer two. And basically you're done. So this way you can actually fit many L3s into StarkNet. And then your L3 input footprint on layer one is even lower than what StarkNet will give you.
It also gives you much cheaper L3 interoperability because the messages are now sent between different L3 systems not through layer one, but actually through layer two. And so you can move very freely between different roll-up systems and so I just want to say that yeah, so okay, there are like many advantages to running an L3 because you could say why would I want an L3? I could just run write smart contracts and put them on Starknet, but you still may want to run your own L3 in which you have full control of for instance the proving
framework. So you could decide maybe you want a dedicated system like Stark X that deals much better but with a very limited use case let's say of payment processing or swap trading. You could deploy your own contracts, you could filter transactions if you want on a layer three, maybe this is something that won't be possible on a layer two, but maybe for regulatory reasons you actually need a system that doesn't allow all transactions to go through. So on a layer three it will be your layer three, you can decide what you let there go. Your block creation time you decide on it, you don't have to wait for the sequencing and proving process of layer two to happen.
The fees are going to be much lower, you can decide if you want to run a centralized system or decentralized. You have better privacy, you can decide on the data availability mode that you want, maybe you want to work with your own data availability providers. So there is a lot of advantages to having a custom layer three. So when recursion as I said today is a very special day today recursion so it's been already we finished the development a very long time ago and it's been running in parallel to the production systems for a while and today we're turning on today we're turning it on in production for all of our sharp systems and so it's a very happy day.
Summary recursion like Sharp and Cairo and Starknet are the results of the maturity and Turing completeness. The Turing completeness of Cairo, the ability to generate proofs for it, and all of this is production grade and humming, and we understand very well the whole stack. Recursion is really great for scaling, lower gas cost, higher proof capacity, lower latency. It enables even higher scale than what you get, things like layer three interoperability. It's in production today.
Saying again, we're not that good with spreading the word. Maybe you can help us, but we're actually pretty damn good when it comes to making breakthroughs and things that seem impossible work in production. Thank you very much.
