arnaucube
arnaucube/go-snarkPublic Self-hosted repository
go-snark/snark.go
snark.goRaw
  1package snark
  2
  3import (
  4	"bytes"
  5	"fmt"
  6	"math/big"
  7	"os"
  8
  9	"github.com/arnaucube/go-snark/bn128"
 10	"github.com/arnaucube/go-snark/circuitcompiler"
 11	"github.com/arnaucube/go-snark/fields"
 12	"github.com/arnaucube/go-snark/r1csqap"
 13)
 14
 15// Setup is the data structure holding the Trusted Setup data. The Setup.Toxic sub struct must be destroyed after the GenerateTrustedSetup function is completed
 16type Setup struct {
 17	Toxic struct {
 18		T      *big.Int // trusted setup secret
 19		Ka     *big.Int // prover
 20		Kb     *big.Int // prover
 21		Kc     *big.Int // prover
 22		Kbeta  *big.Int
 23		Kgamma *big.Int
 24		RhoA   *big.Int
 25		RhoB   *big.Int
 26		RhoC   *big.Int
 27	}
 28
 29	// public
 30	G1T [][3]*big.Int    // t encrypted in G1 curve
 31	G2T [][3][2]*big.Int // t encrypted in G2 curve
 32	Pk  struct {         // Proving Key pk:=(pkA, pkB, pkC, pkH)
 33		A  [][3]*big.Int
 34		B  [][3][2]*big.Int
 35		C  [][3]*big.Int
 36		Kp [][3]*big.Int
 37		Ap [][3]*big.Int
 38		Bp [][3]*big.Int
 39		Cp [][3]*big.Int
 40	}
 41	Vk struct {
 42		Vka   [3][2]*big.Int
 43		Vkb   [3]*big.Int
 44		Vkc   [3][2]*big.Int
 45		IC    [][3]*big.Int
 46		G1Kbg [3]*big.Int    // g1 * Kbeta * Kgamma
 47		G2Kbg [3][2]*big.Int // g2 * Kbeta * Kgamma
 48		G2Kg  [3][2]*big.Int // g2 * Kgamma
 49		Vkz   [3][2]*big.Int
 50	}
 51}
 52
 53// Proof contains the parameters to proof the zkSNARK
 54type Proof struct {
 55	PiA  [3]*big.Int
 56	PiAp [3]*big.Int
 57	PiB  [3][2]*big.Int
 58	PiBp [3]*big.Int
 59	PiC  [3]*big.Int
 60	PiCp [3]*big.Int
 61	PiH  [3]*big.Int
 62	PiKp [3]*big.Int
 63	// PublicSignals []*big.Int
 64}
 65
 66type utils struct {
 67	Bn  bn128.Bn128
 68	FqR fields.Fq
 69	PF  r1csqap.PolynomialField
 70}
 71
 72// Utils is the data structure holding the BN128, FqR Finite Field over R, PolynomialField, that will be used inside the snarks operations
 73var Utils = prepareUtils()
 74
 75func prepareUtils() utils {
 76	bn, err := bn128.NewBn128()
 77	if err != nil {
 78		panic(err)
 79	}
 80	// new Finite Field
 81	fqR := fields.NewFq(bn.R)
 82	// new Polynomial Field
 83	pf := r1csqap.NewPolynomialField(fqR)
 84
 85	return utils{
 86		Bn:  bn,
 87		FqR: fqR,
 88		PF:  pf,
 89	}
 90}
 91
 92// GenerateTrustedSetup generates the Trusted Setup from a compiled Circuit. The Setup.Toxic sub data structure must be destroyed
 93func GenerateTrustedSetup(witnessLength int, circuit circuitcompiler.Circuit, alphas, betas, gammas [][]*big.Int, zx []*big.Int) (Setup, error) {
 94	var setup Setup
 95	var err error
 96
 97	// input soundness
 98	for i := 0; i < len(alphas); i++ {
 99		for j := 0; j < len(alphas[i]); j++ {
100			if j <= circuit.NPublic {
101				if bytes.Equal(alphas[i][j].Bytes(), Utils.Bn.Fq1.Zero().Bytes()) {
102					alphas[i][j] = Utils.Bn.Fq1.One()
103				}
104			}
105		}
106	}
107
108	// generate random t value
109	setup.Toxic.T, err = Utils.FqR.Rand()
110	if err != nil {
111		return Setup{}, err
112	}
113
114	// k for calculating pi' and Vk
115	setup.Toxic.Ka, err = Utils.FqR.Rand()
116	if err != nil {
117		return Setup{}, err
118	}
119	setup.Toxic.Kb, err = Utils.FqR.Rand()
120	if err != nil {
121		return Setup{}, err
122	}
123	setup.Toxic.Kc, err = Utils.FqR.Rand()
124	if err != nil {
125		return Setup{}, err
126	}
127
128	// generate Kβ (Kbeta) and Kγ (Kgamma)
129	setup.Toxic.Kbeta, err = Utils.FqR.Rand()
130	if err != nil {
131		return Setup{}, err
132	}
133	setup.Toxic.Kgamma, err = Utils.FqR.Rand()
134	if err != nil {
135		return Setup{}, err
136	}
137
138	// generate ρ (Rho): ρA, ρB, ρC
139	setup.Toxic.RhoA, err = Utils.FqR.Rand()
140	if err != nil {
141		return Setup{}, err
142	}
143	setup.Toxic.RhoB, err = Utils.FqR.Rand()
144	if err != nil {
145		return Setup{}, err
146	}
147	setup.Toxic.RhoC = Utils.FqR.Mul(setup.Toxic.RhoA, setup.Toxic.RhoB)
148
149	// encrypt t values with curve generators
150	var gt1 [][3]*big.Int
151	var gt2 [][3][2]*big.Int
152	gt1 = append(gt1, Utils.Bn.G1.G)
153	tEncr := setup.Toxic.T
154	for i := 1; i < witnessLength; i++ {
155		gt1 = append(gt1, Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tEncr))
156		tEncr = Utils.Bn.Fq1.Mul(tEncr, setup.Toxic.T)
157	}
158	// for i := 0; i < witnessLength; i++ {
159	//         tPow := Utils.FqR.Exp(setup.Toxic.T, big.NewInt(int64(i)))
160	//         tEncr1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, tPow)
161	//         gt1 = append(gt1, tEncr1)
162	//         tEncr2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, tPow)
163	//         gt2 = append(gt2, tEncr2)
164	// }
165	// gt1: g1, g1*t, g1*t^2, g1*t^3, ...
166	// gt2: g2, g2*t, g2*t^2, ...
167	setup.G1T = gt1
168	setup.G2T = gt2
169
170	setup.Vk.Vka = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Ka)
171	setup.Vk.Vkb = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, setup.Toxic.Kb)
172	setup.Vk.Vkc = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kc)
173
174	/*
175		Verification keys:
176		- Vk_betagamma1: setup.G1Kbg = g1 * Kbeta*Kgamma
177		- Vk_betagamma2: setup.G2Kbg = g2 * Kbeta*Kgamma
178		- Vk_gamma: setup.G2Kg = g2 * Kgamma
179	*/
180	kbg := Utils.FqR.Mul(setup.Toxic.Kbeta, setup.Toxic.Kgamma)
181	setup.Vk.G1Kbg = Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kbg)
182	setup.Vk.G2Kbg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, kbg)
183	setup.Vk.G2Kg = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, setup.Toxic.Kgamma)
184
185	for i := 0; i < circuit.NVars; i++ {
186		at := Utils.PF.Eval(alphas[i], setup.Toxic.T)
187		rhoAat := Utils.Bn.Fq1.Mul(setup.Toxic.RhoA, at)
188		a := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoAat)
189		setup.Pk.A = append(setup.Pk.A, a)
190		if i <= circuit.NPublic {
191			setup.Vk.IC = append(setup.Vk.IC, a)
192		}
193
194		bt := Utils.PF.Eval(betas[i], setup.Toxic.T)
195		rhoBbt := Utils.Bn.Fq1.Mul(setup.Toxic.RhoB, bt)
196		bg1 := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoBbt)
197		bg2 := Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoBbt)
198		setup.Pk.B = append(setup.Pk.B, bg2)
199
200		ct := Utils.PF.Eval(gammas[i], setup.Toxic.T)
201		rhoCct := Utils.Bn.Fq1.Mul(setup.Toxic.RhoC, ct)
202		c := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, rhoCct)
203		setup.Pk.C = append(setup.Pk.C, c)
204
205		kt := Utils.FqR.Add(Utils.FqR.Add(rhoAat, rhoBbt), rhoCct)
206		k := Utils.Bn.G1.Affine(Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt))
207
208		ktest := Utils.Bn.G1.Affine(Utils.Bn.G1.Add(Utils.Bn.G1.Add(a, bg1), c))
209		if !Utils.Bn.Fq2.Equal(k, ktest) {
210			os.Exit(1)
211			return setup, err
212		}
213
214		setup.Pk.Ap = append(setup.Pk.Ap, Utils.Bn.G1.MulScalar(a, setup.Toxic.Ka))
215		setup.Pk.Bp = append(setup.Pk.Bp, Utils.Bn.G1.MulScalar(bg1, setup.Toxic.Kb))
216		setup.Pk.Cp = append(setup.Pk.Cp, Utils.Bn.G1.MulScalar(c, setup.Toxic.Kc))
217		k_ := Utils.Bn.G1.MulScalar(Utils.Bn.G1.G, kt)
218		setup.Pk.Kp = append(setup.Pk.Kp, Utils.Bn.G1.MulScalar(k_, setup.Toxic.Kbeta))
219	}
220	zt := Utils.PF.Eval(zx, setup.Toxic.T)
221	rhoCzt := Utils.Bn.Fq1.Mul(setup.Toxic.RhoC, zt)
222	setup.Vk.Vkz = Utils.Bn.G2.MulScalar(Utils.Bn.G2.G, rhoCzt)
223
224	return setup, nil
225}
226
227// GenerateProofs generates all the parameters to proof the zkSNARK from the Circuit, Setup and the Witness
228func GenerateProofs(circuit circuitcompiler.Circuit, setup Setup, hx []*big.Int, w []*big.Int) (Proof, error) {
229	var proof Proof
230	proof.PiA = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
231	proof.PiAp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
232	proof.PiB = Utils.Bn.Fq6.Zero()
233	proof.PiBp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
234	proof.PiC = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
235	proof.PiCp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
236	proof.PiH = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
237	proof.PiKp = [3]*big.Int{Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero(), Utils.Bn.G1.F.Zero()}
238
239	for i := circuit.NPublic + 1; i < circuit.NVars; i++ {
240		proof.PiA = Utils.Bn.G1.Add(proof.PiA, Utils.Bn.G1.MulScalar(setup.Pk.A[i], w[i]))
241		proof.PiAp = Utils.Bn.G1.Add(proof.PiAp, Utils.Bn.G1.MulScalar(setup.Pk.Ap[i], w[i]))
242	}
243
244	for i := 0; i < circuit.NVars; i++ {
245		proof.PiB = Utils.Bn.G2.Add(proof.PiB, Utils.Bn.G2.MulScalar(setup.Pk.B[i], w[i]))
246		proof.PiBp = Utils.Bn.G1.Add(proof.PiBp, Utils.Bn.G1.MulScalar(setup.Pk.Bp[i], w[i]))
247
248		proof.PiC = Utils.Bn.G1.Add(proof.PiC, Utils.Bn.G1.MulScalar(setup.Pk.C[i], w[i]))
249		proof.PiCp = Utils.Bn.G1.Add(proof.PiCp, Utils.Bn.G1.MulScalar(setup.Pk.Cp[i], w[i]))
250
251		proof.PiKp = Utils.Bn.G1.Add(proof.PiKp, Utils.Bn.G1.MulScalar(setup.Pk.Kp[i], w[i]))
252	}
253
254	for i := 0; i < len(hx); i++ {
255		proof.PiH = Utils.Bn.G1.Add(proof.PiH, Utils.Bn.G1.MulScalar(setup.G1T[i], hx[i]))
256	}
257
258	return proof, nil
259}
260
261// VerifyProof verifies over the BN128 the Pairings of the Proof
262func VerifyProof(circuit circuitcompiler.Circuit, setup Setup, proof Proof, publicSignals []*big.Int, printVer bool) bool {
263	// e(piA, Va) == e(piA', g2)
264	pairingPiaVa := Utils.Bn.Pairing(proof.PiA, setup.Vk.Vka)
265	pairingPiapG2 := Utils.Bn.Pairing(proof.PiAp, Utils.Bn.G2.G)
266	if !Utils.Bn.Fq12.Equal(pairingPiaVa, pairingPiapG2) {
267		return false
268	}
269	if printVer {
270		fmt.Println("✓ e(piA, Va) == e(piA', g2), valid knowledge commitment for A")
271	}
272
273	// e(Vb, piB) == e(piB', g2)
274	pairingVbPib := Utils.Bn.Pairing(setup.Vk.Vkb, proof.PiB)
275	pairingPibpG2 := Utils.Bn.Pairing(proof.PiBp, Utils.Bn.G2.G)
276	if !Utils.Bn.Fq12.Equal(pairingVbPib, pairingPibpG2) {
277		return false
278	}
279	if printVer {
280		fmt.Println("✓ e(Vb, piB) == e(piB', g2), valid knowledge commitment for B")
281	}
282
283	// e(piC, Vc) == e(piC', g2)
284	pairingPicVc := Utils.Bn.Pairing(proof.PiC, setup.Vk.Vkc)
285	pairingPicpG2 := Utils.Bn.Pairing(proof.PiCp, Utils.Bn.G2.G)
286	if !Utils.Bn.Fq12.Equal(pairingPicVc, pairingPicpG2) {
287		return false
288	}
289	if printVer {
290		fmt.Println("✓ e(piC, Vc) == e(piC', g2), valid knowledge commitment for C")
291	}
292
293	// Vkx, to then calculate Vkx+piA
294	vkxpia := setup.Vk.IC[0]
295	for i := 0; i < len(publicSignals); i++ {
296		vkxpia = Utils.Bn.G1.Add(vkxpia, Utils.Bn.G1.MulScalar(setup.Vk.IC[i+1], publicSignals[i]))
297	}
298
299	// e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2)
300	if !Utils.Bn.Fq12.Equal(
301		Utils.Bn.Pairing(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiB), // TODO Add(vkxpia, proof.PiA) can go outside in order to save computation, as is reused later
302		Utils.Bn.Fq12.Mul(
303			Utils.Bn.Pairing(proof.PiH, setup.Vk.Vkz),
304			Utils.Bn.Pairing(proof.PiC, Utils.Bn.G2.G))) {
305		return false
306	}
307	if printVer {
308		fmt.Println("✓ e(Vkx+piA, piB) == e(piH, Vkz) * e(piC, g2), QAP disibility checked")
309	}
310
311	// e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB)
312	// == e(piK, g2Kgamma)
313	piApiC := Utils.Bn.G1.Add(Utils.Bn.G1.Add(vkxpia, proof.PiA), proof.PiC)
314	pairingPiACG2Kbg := Utils.Bn.Pairing(piApiC, setup.Vk.G2Kbg)
315	pairingG1KbgPiB := Utils.Bn.Pairing(setup.Vk.G1Kbg, proof.PiB)
316	pairingL := Utils.Bn.Fq12.Mul(pairingPiACG2Kbg, pairingG1KbgPiB)
317	pairingR := Utils.Bn.Pairing(proof.PiKp, setup.Vk.G2Kg)
318	if !Utils.Bn.Fq12.Equal(pairingL, pairingR) {
319		return false
320	}
321	if printVer {
322		fmt.Println("✓ e(Vkx+piA+piC, g2KbetaKgamma) * e(g1KbetaKgamma, piB) == e(piK, g2Kgamma)")
323	}
324
325	return true
326}