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
16Example:
17```go
18bn, err := bn128.NewBn128()
19assert.Nil(t, err)
20
21// new Finite Field
22fqR := fields.NewFq(bn.R)
23
24// new Polynomial Field
25pf := r1csqap.NewPolynomialField(f)
26
27/*
28suppose that we have the following variables with *big.Int elements:
29a = [[0 1 0 0 0 0] [0 0 0 1 0 0] [0 1 0 0 1 0] [5 0 0 0 0 1]]
30b = [[0 1 0 0 0 0] [0 1 0 0 0 0] [1 0 0 0 0 0] [1 0 0 0 0 0]]
31c = [[0 0 0 1 0 0] [0 0 0 0 1 0] [0 0 0 0 0 1] [0 0 1 0 0 0]]
32
33w = [1, 3, 35, 9, 27, 30]
34*/
35
36alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
37
38// wittness = 1, 3, 35, 9, 27, 30
39w := []*big.Int{b1, b3, b35, b9, b27, b30}
40circuit := compiler.Circuit{
41	NVars:    6,
42	NPublic:  0,
43	NSignals: len(w),
44}
45ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
46
47hx := pf.DivisorPolinomial(px, zx)
48
49// hx==px/zx so px==hx*zx
50assert.Equal(t, px, pf.Mul(hx, zx))
51
52// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
53abc := pf.Sub(pf.Mul(ax, bx), cx)
54assert.Equal(t, abc, px)
55hz := pf.Mul(hx, zx)
56assert.Equal(t, abc, hz)
57	
58div, rem := pf.Div(px, zx)
59assert.Equal(t, hx, div)
60assert.Equal(t, rem, r1csqap.ArrayOfBigZeros(4))
61
62// calculate trusted setup
63setup, err := GenerateTrustedSetup(bn, fqR, pf, len(w), circuit, alphas, betas, gammas, zx)
64assert.Nil(t, err)
65fmt.Println("t", setup.Toxic.T)
66
67// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
68proof, err := GenerateProofs(bn, fqR, circuit, setup, hx, w)
69assert.Nil(t, err)
70
71assert.True(t, VerifyProof(bn, circuit, setup, proof))
72```
73
74### Test
75```
76go test ./... -v
77```
78
79---
80
81## Caution
82Not finished, work in progress (implementing this in my free time to understand it better, so I don't have much time).
83
84Thanks 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.