arnaucube
arnaucube/go-snarkPublic Self-hosted repository
go-snark/README.md
README.mdRaw
 1# go-snark [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucube/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`Pinocchio: Nearly practical verifiable computation`, Bryan Parno, Craig Gentry, Jon Howell, Mariana Raykova https://eprint.iacr.org/2013/279.pdf
 9
10### Usage
11- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark?status.svg)](https://godoc.org/github.com/arnaucube/go-snark) zkSnark
12- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/bn128?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/bn128) bn128 (more details: https://github.com/arnaucube/go-snark/tree/master/bn128)
13- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/fields?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/fields) Finite Fields operations
14- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/r1csqap?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/r1csqap) R1CS to QAP (more details: https://github.com/arnaucube/go-snark/tree/master/r1csqap)
15- [![GoDoc](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler?status.svg)](https://godoc.org/github.com/arnaucube/go-snark/circuitcompiler) Circuit Compiler
16
17Example:
18```go
19bn, err := bn128.NewBn128()
20assert.Nil(t, err)
21
22// new Finite Field
23fqR := fields.NewFq(bn.R)
24
25// new Polynomial Field
26pf := r1csqap.NewPolynomialField(f)
27
28/*
29suppose that we have the following variables with *big.Int elements:
30a = [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
31b = [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
32c = [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
33
34w = [1, 3, 35, 9, 27, 30]
35*/
36
37alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
38
39// wittness = 1, 3, 35, 9, 27, 30
40w := []*big.Int{b1, b3, b35, b9, b27, b30}
41circuit := compiler.Circuit{
42	NVars:    6,
43	NPublic:  0,
44	NSignals: len(w),
45}
46ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
47
48hx := pf.DivisorPolinomial(px, zx)
49
50// hx==px/zx so px==hx*zx
51assert.Equal(t, px, pf.Mul(hx, zx))
52
53// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
54abc := pf.Sub(pf.Mul(ax, bx), cx)
55assert.Equal(t, abc, px)
56hz := pf.Mul(hx, zx)
57assert.Equal(t, abc, hz)
58	
59div, rem := pf.Div(px, zx)
60assert.Equal(t, hx, div)
61assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
62
63// calculate trusted setup
64setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
65assert.Nil(t, err)
66fmt.Println("t", setup.Toxic.T)
67
68// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
69proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
70assert.Nil(t, err)
71
72assert.True(t, VerifyProof(bn, circuit, setup, proof))
73```
74
75### Test
76```
77go test ./... -v
78```
79
80---
81
82## Caution
83Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
84
85Thanks 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.