Skip to content

Commit 2910b7c

Browse files
committed
zkSnarks working
1 parent 0270cea commit 2910b7c

25 files changed

+945
-180
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,27 @@ const circuit = new zkSnark.Circuit(circuitDef);
3939
circuit.nPublic; // number of public signals (nOutputs + nPublicInputs)
4040
4141
// The array of signals is always sorted in this order:
42-
// [ outputs, publicInputs, 1, privedInputs, internalSignals, constants]
42+
// [ 1, outputs, publicInputs, privedInputs, internalSignals, constants]
4343
4444
// returns a,b and c coeficients of the `signalId` on a given `constrain`
4545
circuit.a(constrain, signalId)
4646
circuit.b(constrain, signalId)
4747
circuit.c(constrain, signalId)
4848
4949
circuit.nOutputs // number of public outputs
50-
circuit.nPublicInputs // number of public inputs
51-
circuit.nPrivateInputs // number of private inputs
50+
circuit.pubInputs // number of public inputs
51+
circuit.nPrvInputs // number of private inputs
5252
circuit.nInputs // number of inputs ( nPublicInputs + nPrivateInputs)
53+
circuit.nVars // number of variables ( not including constants (one is a variable) )
54+
circuit.nSignals // number of signals ( including constants )
5355
5456
circuit.outputIdx(i) // returns the index of the i'th output
5557
circuit.inputIdx(i) // returns the index of the i'th input
56-
circuit.inputPublicIdx(i) // returns the index of the i'th public input
57-
circuit.inputPrivateIdx(i) // returns the index of the i'th private input
58+
circuit.pubInputIdx(i) // returns the index of the i'th public input
59+
circuit.prvInputIdx(i) // returns the index of the i'th private input
60+
circuit.varIdx(i) // returns the index of the i'th variable
61+
circuit.constantIdx(i) // returns the index of the i'th constant
62+
circuit.signalIdx(i) // returns the index of the i'th signal
5863
5964
// returns signal Idx given a signalId
6065
// if the idx >= n , it is a constant

file%3a/Users/jbaylina/git/personal/zksnark/src/polfield.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PolFieldZq {
5555
return this.reduce(res);
5656
}
5757

58-
mulEscalar(a, b) {
58+
mulScalar(a, b) {
5959
if (this.F.isZero(b)) return [];
6060
const res = new Array(a.length);
6161
for (let i=0; i<a.length; i++) {
@@ -67,8 +67,8 @@ class PolFieldZq {
6767
mul(a, b) {
6868
if (a.length == 0) return [];
6969
if (b.length == 0) return [];
70-
if (a.length == 1) return this.mulEscalar(b, a[0]);
71-
if (b.length == 1) return this.mulEscalar(a, b[0]);
70+
if (a.length == 1) return this.mulScalar(b, a[0]);
71+
if (b.length == 1) return this.mulScalar(a, b[0]);
7272

7373
const longestN = Math.max(a.length, b.length);
7474
const bitsResult = log2(longestN-1)+2;

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports.Circuit = require "./src/circuit.js";
2-
exports.setup = require "./src/setup.js";
3-
exports.genProof = require "./src/prover.js";
4-
exports.isValid = require "./src/verifier.js";
1+
exports.Circuit = require("./src/circuit.js");
2+
exports.setup = require("./src/setup.js");
3+
exports.genProof = require("./src/prover.js");
4+
exports.isValid = require("./src/verifier.js");

src/bigint.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ if (typeof(BigInt) != "undefined") {
8585
}
8686
};
8787

88+
// Shr
89+
wBigInt.genShr = () => {
90+
return (a,b) => a >> wBigInt(b);
91+
};
92+
93+
// Shl
94+
wBigInt.genShl = (q) => {
95+
if (q) {
96+
return (a,b) => (a << wBigInt(b)) % q;
97+
} else {
98+
return (a,b) => a << wBigInt(b);
99+
}
100+
};
101+
88102
// Equals
89103
wBigInt.genEquals = (q) => {
90104
if (q) {
@@ -132,18 +146,40 @@ if (typeof(BigInt) != "undefined") {
132146
return this < wBigInt.zero;
133147
};
134148

135-
wBigInt.prototype.shiftRight = function(f) {
136-
return this >> wBigInt(f);
149+
wBigInt.prototype.and = function(m) {
150+
return this & m;
151+
};
152+
153+
wBigInt.prototype.mod = function(c) {
154+
return this % c;
155+
};
156+
157+
wBigInt.prototype.modPow = function(e, m) {
158+
return this ** e % m;
137159
};
138160

139161
wBigInt.prototype.greaterOrEquals = function(b) {
140162
return this >= b;
141163
};
142164

165+
wBigInt.prototype.greater = function(b) {
166+
return this > b;
167+
};
168+
wBigInt.prototype.gt = wBigInt.prototype.greater;
169+
143170
wBigInt.prototype.lesserOrEquals = function(b) {
144171
return this <= b;
145172
};
146173

174+
wBigInt.prototype.lesser = function(b) {
175+
return this < b;
176+
};
177+
wBigInt.prototype.lt = wBigInt.prototype.lesser;
178+
179+
wBigInt.prototype.equals = function(b) {
180+
return this.valueOf == b.valueOf;
181+
};
182+
wBigInt.prototype.eq = wBigInt.prototype.equals;
147183

148184
} else {
149185

@@ -214,6 +250,20 @@ if (typeof(BigInt) != "undefined") {
214250
}
215251
};
216252

253+
// Shr
254+
wBigInt.genShr = () => {
255+
return (a,b) => a.shiftRight(wBigInt(b).value);
256+
};
257+
258+
// Shr
259+
wBigInt.genShl = (q) => {
260+
if (q) {
261+
return (a,b) => a.shiftLeft(wBigInt(b).value).mod(q);
262+
} else {
263+
return (a,b) => a.shiftLeft(wBigInt(b).value);
264+
}
265+
};
266+
217267
// Square
218268
wBigInt.genSquare = (q) => {
219269
if (q) {
@@ -301,6 +351,22 @@ wBigInt.prototype.mul = function (a, q) {
301351
return wBigInt.genMul(q)(this, a);
302352
};
303353

354+
wBigInt.shr = function(a, b, q) {
355+
return wBigInt.genShr(q)(a,b);
356+
};
357+
358+
wBigInt.prototype.shr = function (a, q) {
359+
return wBigInt.genShr(q)(this, a);
360+
};
361+
362+
wBigInt.shl = function(a, b, q) {
363+
return wBigInt.genShl(q)(a,b);
364+
};
365+
366+
wBigInt.prototype.shl = function (a, q) {
367+
return wBigInt.genShl(q)(this, a);
368+
};
369+
304370
wBigInt.equals = function(a, b, q) {
305371
return wBigInt.genEquals(q)(a,b);
306372
};

src/bn128.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ class BN128 {
5858
this.loop_count_bits = []; // Constant
5959
while (!lc.isZero()) {
6060
this.loop_count_bits.push( lc.isOdd() );
61-
lc = lc.shiftRight(1);
61+
lc = lc.shr(1);
6262
}
6363

6464
this.two_inv = this.F1.inverse(bigInt(2));
6565

6666
this.coef_b = bigInt(3);
6767
this.twist = [bigInt(9) , bigInt(1)];
68-
this.twist_coeff_b = this.F2.mulEscalar( this.F2.inverse(this.twist), this.coef_b );
68+
this.twist_coeff_b = this.F2.mulScalar( this.F2.inverse(this.twist), this.coef_b );
6969

7070
this.frobenius_coeffs_c1_1 = bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208582");
7171
this.twist_mul_by_q_X =
@@ -179,17 +179,17 @@ class BN128 {
179179
f = this._mul_by_024(
180180
f,
181181
c.ell_0,
182-
this.F2.mulEscalar(c.ell_VW , pre1.PY),
183-
this.F2.mulEscalar(c.ell_VV , pre1.PX, ));
182+
this.F2.mulScalar(c.ell_VW , pre1.PY),
183+
this.F2.mulScalar(c.ell_VV , pre1.PX, ));
184184

185185
if (bit)
186186
{
187187
c = pre2.coeffs[idx++];
188188
f = this._mul_by_024(
189189
f,
190190
c.ell_0,
191-
this.F2.mulEscalar(c.ell_VW, pre1.PY, ),
192-
this.F2.mulEscalar(c.ell_VV, pre1.PX, ));
191+
this.F2.mulScalar(c.ell_VW, pre1.PY, ),
192+
this.F2.mulScalar(c.ell_VV, pre1.PX, ));
193193
}
194194

195195
}
@@ -203,15 +203,15 @@ class BN128 {
203203
f = this._mul_by_024(
204204
f,
205205
c.ell_0,
206-
this.F2.mulEscalar(c.ell_VW, pre1.PY),
207-
this.F2.mulEscalar(c.ell_VV, pre1.PX));
206+
this.F2.mulScalar(c.ell_VW, pre1.PY),
207+
this.F2.mulScalar(c.ell_VV, pre1.PX));
208208

209209
c = pre2.coeffs[idx++];
210210
f = this._mul_by_024(
211211
f,
212212
c.ell_0,
213-
this.F2.mulEscalar(c.ell_VW, pre1.PY, ),
214-
this.F2.mulEscalar(c.ell_VV, pre1.PX));
213+
this.F2.mulScalar(c.ell_VW, pre1.PY, ),
214+
this.F2.mulScalar(c.ell_VV, pre1.PX));
215215

216216
return f;
217217
}
@@ -229,14 +229,14 @@ class BN128 {
229229
const Y = current.Y;
230230
const Z = current.Z;
231231

232-
const A = this.F2.mulEscalar(this.F2.mul(X,Y), this.two_inv); // A = X1 * Y1 / 2
232+
const A = this.F2.mulScalar(this.F2.mul(X,Y), this.two_inv); // A = X1 * Y1 / 2
233233
const B = this.F2.square(Y); // B = Y1^2
234234
const C = this.F2.square(Z); // C = Z1^2
235235
const D = this.F2.add(C, this.F2.add(C,C)); // D = 3 * C
236236
const E = this.F2.mul(this.twist_coeff_b, D); // E = twist_b * D
237237
const F = this.F2.add(E, this.F2.add(E,E)); // F = 3 * E
238238
const G =
239-
this.F2.mulEscalar(
239+
this.F2.mulScalar(
240240
this.F2.add( B , F ),
241241
this.two_inv); // G = (B+F)/2
242242
const H =

0 commit comments

Comments
 (0)