When preparing logic for use in a zkSnark, one needs to first "flatten" the code so it can be written as a series of constraints.

I'm finding it hard to find examples of doing this. For instance, how would I do convert the following logic into a series of constraints:

if ((input_1 && input_2) || input_3) {
  result = input_1 + input_2
} else {
  result = input_3

How would you go about converting this into an arithmetic statement that can then be then re-written as a series of constraints? Or does there exist any examples of this sort of process that someone could learn from?

  • $\begingroup$ In my opinion this is a pure programming question and is that's why off-topic here. $\endgroup$ – mentallurg Apr 16 at 17:28
  • $\begingroup$ Yes, you might be right actually. I'll try posting on Stack Overflow maybe. Thanks. $\endgroup$ – simbro Apr 16 at 19:40

This sort of programming logic for the ZK SNARK systems can be developed into Arithmetic Circuits using Zokrates, a toolbox for zkSNARKs on Ethereum. It helps you use verifiable computation in your Decentralised Applications, from the specification of your program in a high-level language to generating proofs of computation to verifying those proofs in Solidity, the Smart Contract Langauge for Ethereum Blockchain. Please find a sample programming logic written for Zokrates below.

Once you compile the code, you can proceed to the circuit setup. It creates a prover key and verification key. These keys are derived from a source of randomness, commonly referred to as “toxic waste”. Once it is generated, you can proceed to generate a Solidity contract which contains the generated verification key and a public function to verify a solution to the compiled program. Then you can compute a witness for the compiled program. A witness is a valid assignment of the variables, which include the results of the computation.

def main(field a, field b, field c) -> (field):
    field result = 0
    result = if a + b > c  then a + b else 1 fi
    result = if a + b < c  then c else 1 fi
  return result
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.