9

I was reading Vitalik Buterin's post$^\color{magenta}{\star}$ on ZK-snarks and I need some clarification on some points. Since there aren't that many posts and articles on the subject, I had no choice other than turning to Stack Exchange .

First, I need to know what does Vitalik means by the following.

There is a standard way of converting a logic gate into a (a, b, c) triple depending on what the operation is

Is he pointing to a specific topic in mathematics? Why triple? I did google R1CS, but not that many results came up.

Secondly , why use the variable "one". Why assign the variables in a particluar order which is "one" "x" "out", etc? Why $3$ vectors? And how does that exactly work? Especially assigning the variables at the 3rd gate is a bit not-clear.

I know these are too many questions and even though I really appreciate any help that I can get, yet I don't expect you to answer every single question. However, if you could point me to the topics, keywords, etc that describe the methods used therein, perhaps I could work my way to the end of this article. I wish someone would have wrote a complementary post on this article instead of repeating the Sudoku example over and over again. All you can find is either really heavy math papers or the Waldo analogy, but nothing in between.

P.S.: I have read posts on zcash blog. still not clear on R1CS and QAP. And scientific papers are too math-heavy!


$\color{magenta}{\star}$ Vitalik Buterin, Quadratic Arithmetic Programs: from Zero to Hero, December 12, 2016.

Hesaam
  • 115
  • 4

2 Answers2

13

what does he mean by saying " There is a standard way of converting a logic gate into a (a, b, c) triple depending on what the operation is " ?

He means that every "+" operation will follow the same pattern. (As will every "-" operation, "*" operation, and "/" operation)

Example using '+' operation:

Statement:

x + y = z

Witness vector

Let's assume our witness vector corresponds to the following variables:

  • [ ~one, x, y, z (or ~out) ]

The order of these variables is arbitrary, it just has to be kept consistent through out the process

Convert the statement into the A * B - C = 0 format:

(x+y) * 1 - z = 0

This means the following is also true:

A = x + y
B = 1
C = z

Determine the "factors" for each variable in our witness vector

A = (0 * ~one) + (1 * x) + (1 * y) + (0 * z) = x + y
B = (1 * ~one) + (0 * x) + (0 * y) + (0 * z) = ~one
C = (0 * ~one) + (0 * x) + (0 * y) + (1 * z) = z

Our vectors then directly correspond to the factors for each variable in the witness vector

A = [0, 1, 1, 0]
B = [1, 0, 0, 0]
C = [0, 0, 0, 1]

Note that this "pattern" of vector will be used for ANY addition gate where two variables are being added to create a third variable.

  • A:
    • 1 for each value corresponding to the variables being added
    • 0 for everything else
  • B:
    • 1 for the value corresponding to ~one
    • 0 for everything else
  • C:
    • 1 for the value corresponding to the output variable
    • 0 for everything else

I hope this also demonstrates why the ~one variable is necessary.


EDIT:

why 3 vectors ? and HOW does that exactly work ?

The formula A.s * B.s - C.s = 0 allows you to perform any addition/subtraction/multiplication/division operations between values and variables.

The dot product (A.s) allows for scaling and addition of values, and the A*B part allows for multiplication and division of values.

The C vector simply represents the result as a value or variable

ninni21
  • 304
  • 2
  • 5
  • thank you very much .... does sym1 = 2x * y count as a flattened equation or should we go one more step forward and convert the 2x to x+x = k ? also , in some instances such as y = sym1 * x in vitalik's article , it seems that you could define "A" vector based on sym1 and "B" vector based on x , or vice versa . is that correct ? if that is correct , then the stack of "A" vectors and "B" vectors which are made at the end would look a bit different . I mean there could be variations since in each equation its not decided which should be taken as "A" vector . right ? – Hesaam Feb 28 '18 at 12:57
  • any other areas that use the techniques used here ? – Hesaam Feb 28 '18 at 13:35
  • so we introduce a number of intermediate variables and turn a computation into a set of constraints of degree one ( yet called quadratic ! ) , bundle them together , and using their first , second , and ..... elements and arbitrary values! (i dont see why we cant use 5 , 6 , 7 ..etc ) x=1 , x=2 , ..... make some coordinations and polynomials to fit them . please if you can point me to some references . it all seems so vague !!! – Hesaam Feb 28 '18 at 13:40
  • 1
    I should have put a disclaimer that I'm not an expert at this, I am just trying to learn all about it myself (which is how I found your post), but I will try to answer your questions:
    1. No need to break it down further when 2x + y = z, you can just replace the '1' in the A vector corresponding to 'x' with a 2. The values in these vectors aren't limited to 1 and 0, they can be any number
    – ninni21 Mar 01 '18 at 16:49
  • 2
  • ‎Yes, when arranging the formula x+y = z into the A * B - C = 0 format, you can have:
  • A = 1 * z; ‎B = 1 * ~one; ‎C = 1 * x + 1 * y; It can also be the following, swapping A and B like you proposed: A = 1 * ~one; ‎B = 1x + 1y; ‎C = 1 * z; Or you could combine them all into A: A = 1x + 1y + -1z; ‎B = 1~one; ‎C = 0; ‎ These all produce valid solutions, and some may even produce the same 't' vector after a witness is provided. 3. No clue if any other areas use this technique

    – ninni21 Mar 01 '18 at 16:51
  • The steps I outlined in the answer create the R1CS, which is used to derive the QAP (so they aren't considered "quadratic" yet)

  • ‎you seem to have the sequence of steps correct... But I'm not sure what you mean when you say "arbitrary values! (i dont see why we cant use 5 , 6 , 7 ..etc )". What step do you see this occurring?

  • Unfortunately I don't have other sources for this, but what I did was look at the source code he supplied to learn how each function works, and then played around with different input formulas/gates to see what held true

    – ninni21 Mar 01 '18 at 16:51
  • you see , A vectors from all equations are bundled together . and as we see in this post: https://medium.com/@jamesray_56543/for-convenience-could-you-explain-how-to-do-this-as-i-having-a-bit-of-difficulty-figuring-out-how-4c8dfc7d305d first element of first vector with the number 1 make up the coordinate (1,0) . first element of second A vector with the number 2 , make up the coordinate (2,0) and so on ...
    these numbers (1,2,3,4) which along with first elements of the vectors are used to make coordinates is the thing that i meant by arbitrary . why not use 5 and 6 and ..
    – Hesaam Mar 03 '18 at 08:10
  • and the fact that the choice of A and B vector for each equation is interchangable would result on different variations on bundles of A vectors and B vectors (complete R1CS put together) .right ? which in turn will result in production of different coordinates based on which the polynomials are to be made . not to mention the sorting order of equations themselves (in vitalik's example you could assume y = sym_1 * x as the first and sym_1 = x * x as second equation ) . you see all these changes in turn change the resulting bundles of vectors .wouldn't it affect the results ? – Hesaam Mar 03 '18 at 08:28
  • 1
    Ah, I see what you mean. The numbers (1,2,3,4) are not arbitrary, they identify which Gate the coordinates belong to. If you wanted to get the A/B/C vectors for gate 4, you use x=4 for each polynomial to get the corresponding vector value. You are right that swapping A & B and reordering the gates would produce different coordinates and polynomials. However, they are all valid QAPs. I actually had this same question and tested it with the python code that Vitalik provided. I suggest you do the same to see how changing A/B or the Gate order affects each step of the process – ninni21 Mar 03 '18 at 14:19
  • remember how we talked about other areas that might have inspired such a model for zk-snarks? here are a couple of articles . if you had time take a look and let me know what you think : https://rjlipton.wordpress.com/2010/12/13/making-a-heuristic-into-a-theorem/ https://blog.ethereum.org/2014/08/16/secret-sharing-erasure-coding-guide-aspiring-dropbox-decentralizer/ https://innovation.vivint.com/introduction-to-reed-solomon-bc264d0794f8 https://ipfs.io/ipfs/QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco/wiki/Berlekamp%E2%80%93Welch_algorithm.html#cite_note-6 – Hesaam Mar 08 '18 at 15:19
  • interesting articles. might take me some time to actually understand what they are talking about though. thanks for sharing – ninni21 Mar 09 '18 at 16:20
  • @ninni21 I didn't understand why gates are given numbers (1,2,3,4) to generate the equations? Should they be integers or any thing is fine? Can we give numbers like 6,7,8,9 for the 4 gates? – pranay01 Jul 24 '18 at 07:58
  • @godot101 numbers (1,2,3,4) can be arbitrary distinct numbers, or they could be chosen to speed-up producing the proof. – Vadym Fedyukovych Jul 25 '18 at 11:39