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
7`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
8
9### Usage
10- [](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
11 - [](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
12 - [](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
13 - [](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
14
15Example:
16```go
17bn, err := bn128.NewBn128()
18assert.Nil(t, err)
19
20// new Finite Field
21f := fields.NewFq(bn.R)
22
23// new Polynomial Field
24pf := r1csqap.NewPolynomialField(f)
25
26/*
27suppose that we have the following variables with *big.Int elements:
28a = [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
29b = [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
30c = [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
31
32w = [1, 3, 35, 9, 27, 30]
33*/
34
35alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
36
37ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
38
39hx := pf.DivisorPolinomial(px, zx)
40
41// hx==px/zx so px==hx*zx
42assert.Equal(t, px, pf.Mul(hx, zx))
43
44// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
45abc := pf.Sub(pf.Mul(ax, bx), cx)
46assert.Equal(t, abc, px)
47hz := pf.Mul(hx, zx)
48assert.Equal(t, abc, hz)
49
50// calculate trusted setup
51setup, err := GenerateTrustedSetup(bn, len(ax))
52assert.Nil(t, err)
53fmt.Println("trusted setup:")
54fmt.Println(setup.G1T)
55fmt.Println(setup.G2T)
56
57// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
58proof, err := GenerateProofs(bn, f, setup, ax, bx, cx, hx, zx)
59assert.Nil(t, err)
60
61
62// verify the proofs with the bn128 pairing
63verified := VerifyProof(bn, publicSetup, proof)
64assert.True(t, verified)
65```
66
67### Test
68```
69go test ./... -v
70```
71
72---
73
74## Caution
75Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
76
77Thanks 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.