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