[ETH Seoul 2023] Halo2 circuit developer experience by Chih-Cheng Liang, Ethereum Foundation

ETH Seoul 2023 · June 2023 · 30:55

Chih-Cheng Liang of the Ethereum Foundation introduces the building blocks of Halo2 circuits for developers used to ordinary programming languages. The talk explains computation traces, constraints and arithmetization, the grid of fixed, advice and instance columns, and gates, copy constraints and lookups, using a Fibonacci circuit as an example. It closes with practical design tricks, including handling branches and guarding against a dishonest prover.

Speakers

Topics

From ETH Seoul 2023, hosted by Kryptoplanet.

Transcript

foreign [Music] thanks for coming to this talk so yeah I did this talk uh uh last year and had a bogota.com and now we are at like June 2023 I felt like this talk is a little bit uh late like after uh half a month like we now have like Nova we have like all these new proving systems and like new languages and um but I I feel like that there's still

need to talk about hello too because if you want to contribute to um uh dkvm or like some low-level language implementation uh some knowledge about how to Works was very helpful and also um the new Provisions uh Nova hyperplunk and um everything are built uh on top of hello too so let's let's begin um so about this talk I will be uh talk about some building blocks of the Halo 2 circuits and also the uh the design mindset for the circuit

implementation like uh most of us are not like cryptographers most of us are developers we are building applications and um we usually write a programming language like a python JavaScript rust but uh hello to is a very different piece and it requires different mindset and thinking uh about uh to to write your uh application and so there might be some challenges and I hope uh some some of uh our tricks and tips can help you navigate through through this

and uh about me uh I've been working on the kvn uh Community version in PSE for for two years and uh the goal is to build uh build a L1 as execution layer proof and so so uh our goal is not uh for for DK row up and we've been working with a scroll and title and they're building the cable apps and where uh we build a whole project on top of Halo 2.

and a quick reminder that we our project is licensed in MIT and Apache 2.0 so feel free to use it for get modified it do whatever you would like to do so uh a little bit primer on the uh CK proving so we have two parties uh one proverb and one verifier they agreed on some algorithms and proverbs at approving time they can claim they have some input and derive to some outputs and the verifier can read the input outputs and approve and then convince

that the output is correct without rerunning the program again and uh that's that's how uh ZK proof allows us to uh compress the computation or you can hide some information in the computations and you can let the verifier with low computation powers to to verify that computation and that's on uh unwrap this a little bit so we have uh two uh scenarios one is setup time and one is proving time so setup time the approver

and the verifier agreed on some algorithm and you the developer write that algorithm and Express in this Grid in in this weird Sudoku game like a spread that algorithm in that grid and then we uh we're attend that Grid in terms of verifying key and proven key and distribute distribute them to the approval and verifier then at approving time the approver can uh can run that algorithm can fill in some inputs all the middle computation traces and outputs

and then the program will create a proof and then send to the verifier and the program used a proving key to generate a proof and the verifier will use the verifying key to verify the proof and determine the proof is valid or not now um the circuit that developers have a very different brand uh brands so usually as a normal developer we work with languages like python we Define our computation in variables we might determine on some conditions and then Branch our programs into

different parts we handle our computation in in terms of CPU runtimes but as a circuit developer you are working with the computation traces which is the uh the input or the intermediate values and the outputs and they're laid out as the as the the values and then we Huawei design is a set of mathematical constraints and then to determine if these traces are valid or not so I would say there are three elements in the uh in the circuit developing one is the computation trace the other are constraints and the third

one like I hope this can go away soon like it's arithmetization so what is the computation Trace it's a step-by-step values uh to derive the computation from the inputs to the output for example if I want you to uh count like uh like one like one like two two numbers multiplication with millions and you might uh like use your textbook algorithm and write all the computations and those middle values are are your computation traces and for the constraints like we Define

like what's the value path of the computation uh Trace like uh there are some uh valid value you can fill in the Sudoku board and there are invalid values so if we uh over constraint or under constraints we will introduce some box um if we constraint too much then there are some valid computation we're not the proverb is not able to perform and then we'll have a complainer spark and if we under constraints then the proper can convince um the verifiers some false statements then that's a sound spark very serious and the last one is the arithmetization

like we have to express those uh computation traces in numbers and the constraints in in math and equations so those are the difficult part so let's uh unwrap this uh three elements one by one the first one is uh we want to express the computation Trace into um in uh into some formats that the computer can pass and understand so first we have a grid like uh Sudoku board we organize it as some columns and some rows and in uh in each cell you can build in a number this number is a finite field

element uh usually uh you can think it as an integer less than P the p a number for example if this p is 3 then you you can input 0 1 2 but if you uh if you need to perform some like uh additions and multiplications for example two plus two then uh this uh you usually get four but like uh since we uh have this like a wrap over uh property then um it will become one so everything in the in a cell is under module of arithmetics and P is usually a

very very big numbers is 254 bits uh that usually that's very uh enough for for most of applications but like for example if you are dealing with uh evm words with which is uh 256 bits then you have to split the word into like uh two two parts the high parts or low parts so that it doesn't overflow um so follow rows um that was a fixed height uh UJ is uh 216 um or 220 rows like um you uh that the space is free but it's capped and uh you

you can like feel free to use as much for as many rows as possible but uh you are not able to to increase it to uh to to many roles and for the columns uh you are free to add many columns with some additional cost for approval and verifiers so um yes so so so this is the grid that we can lay a lot of computation traces and now like um like we now need to define the uh there are different types of columns a column is a polynomial and like it kind of

compress the all the the value in that uh in that columns and into a relative curve element and uh but there there uh there are some like different types of columns uh we need to pay attention to first one is the uh fixed fixed column and this one uh is uh created at a setup time you assign the value at a set of time it's visible to the verifier and also to the approver you can sync it as part of the circuit constraint yeah the the uh the printed local values that uh it it might not satisfy and the other column is the advice

column uh this is uh the the program assigns value in this cells in the uh proving time so uh usually uh the actual value is only visible to the approver and um the verifier only has access to the commitment or the curve element of that column but they don't see the actual values in there time and the last one is the instance column this is when you have some public inputs and then you want to share uh between like a program verifier for example the

uh the the uh the medical rule of tornado cash and you you put this value in the instance column and this is also determine approving time and visible to verifier so uh that's uh give a recap um so at approving time we filled agreed with the proper values and the program use the province key to turn this grid into a grid of computation traces into a snap proof and then send to the verifier and verify using varying key uh the verifiers sees the proofs and all

all the columns and and the the values visible to them and verifying the proof with them now um we've talked about the computation traces now uh we we uh we can talk about how to how do we express uh constraints uh in in Halo 2. so constraints Define uh what computation traces are valid and they uh they they are defined by relative uh positions so usually you have a selector that that used to enable or disable a constraint and then uh this selector is a fixed

column and you have an expression like uh the equation there uh it it says the selector times a minus B minus B next equals to zero and uh and this cells you can you can put it in any relative positions you can put it in like 20 rows later or or like three rows later but or or like some columns on the right columns on the left but they have to be in relative position so we can give an example to to show that for example this is uh a Fibonacci circuit uh so we want to uh prove that the the

ends turn of the Fibonacci numbers and for example one one two three five and we can lay down this computation trace and the intermediate values in this grid and we have the white one oh that yeah sorry the Y one should be the fixed calendar I I did the wrong coloring and uh so and I uh so we Define the gate the gate as a constraint it has the selectors uh on the left and then left value right value and the sum and then how much satisfy the equation uh if the selector is on then left plus

right minus sum equals to zero then uh we checked it row by row like this one selector is on and so they must satisfy the right equation and then uh one plus one minus two equals to zero and this gate will scan through the whole rows and then um if it's on then the concern must be satisfied and uh scan down scan down and to the last last row the selector is disabled so uh the the right the left right and

some that doesn't have to satisfy this expression um so this is another way you can write your constraint with saved one column and move the constraint cells to the uh to the right next and then we can do the scanning and and check that uh this uh this Trace is actually valid uh there are three type three types of checks in Halogen so one is using the gate to constrain the computation Trace uh the other one is the uh equality track equality checks so equality checks can let you close to

cells together but they are defined in absolute position so for example we can glue this uh 10 to the 10 um 10 to the 10 with with this copy constraint also the 100 to the 100 with the uh with it called copy constraint so the when they are glued the the self uh the value in the cell must equal to each other and yeah uh sorry yeah and and there's also a lookup constraint but I I didn't include here um uh because that includes some complexity

um yeah and as uh where uh the snark is a verifying uh computation uh technique and so we're designing some like uh can't be can't be evil applications and to do that we must assume that our actors poorer is always evil they they are able to do uh anything evil and we can we must argue that if that they are evil we can prevent them from doing evil things so um so that's the that's all the uh rule we have in how to now we can uh look in

some tricks the first uh Trace is uh of course limiting what kind of uh values you can input in a cell so uh the the simplest one is to multiply that uh the for example you you only allow the value 1 and 2 and 3 inside the cell then if the cell is X and then you you create an expression x minus 1 times x minus 2 times x minus 3 equals to value so if you input value 3 it satisfy the expression 2 is all Satisfied by 100 does not satisfy the expression the

output is not zero zero that's not satisfy the expression too uh so yep that's limiting the set of options now the other trick is to convert uh if else so in in your application if else is very uh basic uh basic building blocks for your application let's give an example like you have a algorithm Foo and then you have two inputs add and B and another input

variable happy if it's uh happy then we perform addition if it's not happy then we perform multiplication and for this uh circuit we laid out some computation traces and we um input the computations trace to the to the grid and then we Define some copy constraints so that uh let's say we have a is 5 and B is 6. uh Happy is 10 and the output uh is 11 if happy is one and if it's zero then the output is 30.

so we can Define the we can Define the expression like this so you have happy times a plus b one times happy uh is uh emulated by by B and minus output must be equal to zero and if uh as you can see if the happy is one then the last constraint is enabled and the right one the eighth multiplied by B is disabled and uh if happy is zero then the left one is disabled and the right one is enabled

uh but there's a small problem in uh this equation um do anyone see any any problem okay I'll get to the point uh so uh the program can witness uh the value happy to the uh to three so that uh it gets a he has a very invalid output of -27 that you cannot ever derive from that algorithm and why is that because we didn't constrain the possible value for happy happy is the Boolean value so we need to add another constraint to constraint

heavy to be zero or one okay now uh we have uh if else and we convert it to the math expressions now we can come converting some for loops um for a simple follow-up if the uh if the number of loop is deterministic then you can just uh lay it out the computation traces and and make it uh make it uh fixed numbered um of sorry Base number of grids uh so this this is very easy

but uh if the uh if the range like if if if you don't know how many how many uh Loops are like this uh this number of Loops is determined by proving time then we have a problem like we cannot um we cannot grow them in with the copy conditions uh we don't know how how big uh the the table is so uh so what we have to do here um so so one one uh one principle is that like uh we are not able to prove infinite Loops like

there's only your uh when you determine number of columns of your circuit there's only so many computations you can prove it's a fixed amount it it is it's kept so we're not able to prove infinite number of computations um so so we have to define the max possible of an and then uh for um so um sorry yeah um so so one strategy to do this is to um to to have up so

one strategy to have a a dynamic uh input is to uh is to let the program witness the number of n and uh divide it as uh defined it as a variable uh r and then uh but uh if we don't uh limit the uh the number of uh the art the program can witness the wrong value and then uh give you a wrong output um so we can we can see some valid valid computation

Trace like so uh the the left one uh is like if the N is zero then we should have selectors of zero but if the N is three then then we should have uh three uh three of uh selectors are continuously one and then uh zero to the end and the the last one if the uh the the end is five then we should have like five of selectors uh enabled and the last one is zero and then so we can observe this a state machine here so if uh if we start our algorithm um

we can uh we can go to the path to to add more add more values or we could uh go to the padding no values but we are not able to go through the padding to the ad so when you um when when a selector is one your next one can be one or zero but once you enter zero there's no way you can enter to the one again so uh so Define the state transition you can uh you can make sure this uh computations always run correctly there is no way the approver can witness uh zero one zero one zero one and like um

mess up your computation uh in in this algorithm um so yeah these are a bunch of details like uh to add some missing constraint to the to the uh to to this algorithm and the takeaway here is the um in um in the in a civil computation when we do a branch uh we we just ignore uh like if you select one branch in the if else then you we just ignore the uh else Branch or the the other Branch we didn't

go there won't be the overhead of your computation but in the circuit like um if we have if else Branch we must leave the space for the approver to uh to to compute them because you don't know and uh you write a program at the same time but when you're in approving time you don't know uh which path the program will take so you need to leave the space for the both branches inside the circuit and uh the last one last one trick is we learned in uh the this much in the Vietnam residency we have a

we have our boot camp and uh we realized that like if uh we can express um our computations in the recursive function since we are verifying a computation Trace we don't have to we don't have to really verify this from the beginning to the end of of the real computation we can actually verify from the end to the beginning as long as the the relationships and the structure of the computation is correct so uh expressing the the function in recursion has some benefits that it's very easy to

argue the correctness uh of the of the function like uh if you express in for Loops or like like uh you're no more integrated uh incremental computation it's easy to mess up uh or forget or or do it wrong with some variables but expressing uh recursions is is very uh easy to see if it's correct or not um so yeah I still have uh 40 seconds so some take away for this talk um so so yeah for a challenge for a circuit developers

um we are doing with computation traces instead of the the actual real uh execution so we need to flatten the computation path and leave uh all the space for the both uh uh both if else branches and all the branches of your computation and we are working with the uh Mass arithmetics instead of bytes so we have to deal with the finite field elements and math equations and the last one is the uh we are working with the uh approval that could be uh evil so so we need to prevent them from uh cheating the computation let's

be careful with the uh every possible embedded computation the program can witness and convince you the wrong output and some trick is that uh if we want to limit some uh if we want to do the X if else branches then uh use the uh this type of uh equations to do if else branch and for complicated rules uh try to Define your application as a state transition and Define constraints for them so yeah it's my talk thank you very much

Related talks