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