arnaucube
arnaucube/go-snarkPublic Self-hosted repository
go-snark/zk/zk_test.go
zk/zk_test.goRaw
 1package zk
 2
 3import (
 4	"fmt"
 5	"math/big"
 6	"testing"
 7
 8	"github.com/arnaucube/go-snark/bn128"
 9	"github.com/arnaucube/go-snark/fields"
10	"github.com/arnaucube/go-snark/r1csqap"
11	"github.com/stretchr/testify/assert"
12)
13
14func TestZk(t *testing.T) {
15	bn, err := bn128.NewBn128()
16	assert.Nil(t, err)
17
18	// new Finite Field
19	f := fields.NewFq(bn.R)
20
21	// new Polynomial Field
22	pf := r1csqap.NewPolynomialField(f)
23
24	b0 := big.NewInt(int64(0))
25	b1 := big.NewInt(int64(1))
26	b3 := big.NewInt(int64(3))
27	b5 := big.NewInt(int64(5))
28	b9 := big.NewInt(int64(9))
29	b27 := big.NewInt(int64(27))
30	b30 := big.NewInt(int64(30))
31	b35 := big.NewInt(int64(35))
32	a := [][]*big.Int{
33		[]*big.Int{b0, b1, b0, b0, b0, b0},
34		[]*big.Int{b0, b0, b0, b1, b0, b0},
35		[]*big.Int{b0, b1, b0, b0, b1, b0},
36		[]*big.Int{b5, b0, b0, b0, b0, b1},
37	}
38	b := [][]*big.Int{
39		[]*big.Int{b0, b1, b0, b0, b0, b0},
40		[]*big.Int{b0, b1, b0, b0, b0, b0},
41		[]*big.Int{b1, b0, b0, b0, b0, b0},
42		[]*big.Int{b1, b0, b0, b0, b0, b0},
43	}
44	c := [][]*big.Int{
45		[]*big.Int{b0, b0, b0, b1, b0, b0},
46		[]*big.Int{b0, b0, b0, b0, b1, b0},
47		[]*big.Int{b0, b0, b0, b0, b0, b1},
48		[]*big.Int{b0, b0, b1, b0, b0, b0},
49	}
50	alphas, betas, gammas, zx := pf.R1CSToQAP(a, b, c)
51
52	w := []*big.Int{b1, b3, b35, b9, b27, b30}
53	ax, bx, cx, px := pf.CombinePolynomials(w, alphas, betas, gammas)
54
55	hx := pf.DivisorPolinomial(px, zx)
56
57	// hx==px/zx so px==hx*zx
58	assert.Equal(t, px, pf.Mul(hx, zx))
59
60	// p(x) = a(x) * b(x) - c(x) == h(x) * z(x)
61	abc := pf.Sub(pf.Mul(ax, bx), cx)
62	assert.Equal(t, abc, px)
63	hz := pf.Mul(hx, zx)
64	assert.Equal(t, abc, hz)
65
66	// calculate trusted setup
67	setup, err := GenerateTrustedSetup(bn, len(ax))
68	assert.Nil(t, err)
69	fmt.Println("trusted setup:")
70	fmt.Println(setup.G1T)
71	fmt.Println(setup.G2T)
72
73	// piA = g1 * A(t), piB = g2 * B(t), piC = g1 * C(t), piH = g1 * H(t)
74	proof, err := GenerateProofs(bn, f, setup, ax, bx, cx, hx, zx)
75	assert.Nil(t, err)
76	fmt.Println("proofs:")
77	fmt.Println(proof.PiA)
78	fmt.Println(proof.PiB)
79	fmt.Println(proof.PiC)
80	fmt.Println(proof.PiH)
81	fmt.Println(proof.Vz)
82
83	publicSetup := Setup{
84		G1T: setup.G1T,
85		G2T: setup.G2T,
86	}
87
88	assert.True(t, VerifyProof(bn, publicSetup, proof))
89}