go-snark/README.md
README.mdRaw
1# go-snark [](https://goreportcard.com/report/github.com/arnaucube/go-snark)
2
3zkSNARK library implementation in Go
4
5
6- `Succinct Non-Interactive Zero Knowledge for a von Neumann Architecture`, Eli Ben-Sasson, Alessandro Chiesa, Eran Tromer, Madars Virza https://eprint.iacr.org/2013/879.pdf
7- `Pinocchio: Nearly practical verifiable computation`, Bryan Parno, Craig Gentry, Jon Howell, Mariana Raykova https://eprint.iacr.org/2013/279.pdf
8
9## Caution
10Implementation from scratch in Go to understand the concepts. Do not use in production.
11
12Not finished, implementing this in my free time to understand it better, so I don't have much time.
13
14Current implementation status:
15- [x] Finite Fields (1, 2, 6, 12) operations
16- [x] G1 and G2 curve operations
17- [x] BN128 Pairing
18- [x] circuit code compiler
19 - [ ] code to flat code
20 - [x] flat code compiler
21- [x] circuit to R1CS
22- [x] polynomial operations
23- [x] R1CS to QAP
24- [x] generate trusted setup
25- [x] generate proofs
26- [x] verify proofs with BN128 pairing
27
28
29### Usage
30- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
31- [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
32- [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
33- [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
34- [](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
35
36Example:
37```go
38// compile circuit and get the R1CS
39flatCode := `
40func test(x):
41 aux = x*x
42 y = aux*x
43 z = x + y
44 out = z + 5
45`
46
47// parse the code
48parser := circuitcompiler.NewParser(strings.NewReader(flatCode))
49circuit, err := parser.Parse()
50assert.Nil(t, err)
51fmt.Println(circuit)
52
53// witness
54b3 := big.NewInt(int64(3))
55inputs := []*big.Int{b3}
56w := circuit.CalculateWitness(inputs)
57fmt.Println("\nwitness", w)
58/*
59now we have the witness:
60w = [1 3 35 9 27 30]
61*/
62
63// flat code to R1CS
64fmt.Println("generating R1CS from flat code")
65a, b, c := circuit.GenerateR1CS()
66
67/*
68now we have the R1CS from the circuit:
69a == [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
70b == [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
71c == [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
72*/
73
74
75alphas, betas, gammas, zx := snark.Utils.PF.R1CSToQAP(a, b, c)
76
77
78ax, bx, cx, px := snark.Utils.PF.CombinePolynomials(w, alphas, betas, gammas)
79
80hx := snark.Utils.PF.DivisorPolinomial(px, zx)
81
82// hx==px/zx so px==hx*zx
83assert.Equal(t, px, snark.Utils.PF.Mul(hx, zx))
84
85// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
86abc := snark.Utils.PF.Sub(pf.Mul(ax, bx), cx)
87assert.Equal(t, abc, px)
88hz := snark.Utils.PF.Mul(hx, zx)
89assert.Equal(t, abc, hz)
90
91div, rem := snark.Utils.PF.Div(px, zx)
92assert.Equal(t, hx, div)
93assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
94
95// calculate trusted setup
96setup, err := snark.GenerateTrustedSetup(len(w), circuit, alphas, betas, gammas, zx)
97assert.Nil(t, err)
98fmt.Println("t", setup.Toxic.T)
99
100// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
101proof, err := snark.GenerateProofs(circuit, setup, hx, w)
102assert.Nil(t, err)
103
104assert.True(t, snark.VerifyProof(circuit, setup, proof))
105```
106
107### Test
108```
109go test ./... -v
110```
111
112---
113
114
115Thanks to [@jbaylina](https://github.com/jbaylina), [@bellesmarta](https://github.com/bellesmarta), [@adriamb](https://github.com/adriamb) for their explanations that helped to understand this a little bit. Also thanks to [@vbuterin](https://github.com/vbuterin) for all the published articles explaining the zkSNARKs.