Skip to content

Commit 406ec9f

Browse files
committed
log functions
1 parent 75a7b6e commit 406ec9f

12 files changed

+903
-15
lines changed

Diff for: cli.js

+72-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ setup command
5959
6060
Default: verification_key.json
6161
62-
--protocol [original|groth]
62+
--protocol [original|groth|kimleeoh]
6363
6464
Defines withc variant of snark you want to use
6565
@@ -94,6 +94,23 @@ calculate witness command
9494
9595
Default: witness.json
9696
97+
--lo or --logoutput
98+
99+
Output all the Output signals
100+
101+
--lg or --logget
102+
103+
Output GET access to the signals
104+
105+
--ls or --logset
106+
107+
Output SET access to the signal
108+
109+
--lt or --logtrigger
110+
111+
Output when a subcomponent is triggered and when finished
112+
113+
97114
generate a proof command
98115
========================
99116
@@ -225,8 +242,13 @@ print constraints
225242
.alias("i", "input")
226243
.alias("pub", "public")
227244
.alias("v", "verifier")
245+
.alias("lo", "logoutput")
246+
.alias("lg", "logget")
247+
.alias("ls", "logset")
248+
.alias("lt", "logtrigger")
228249
.help("h")
229250
.alias("h", "help")
251+
230252
.epilogue(`Copyright (C) 2018 0kims association
231253
This program comes with ABSOLUTELY NO WARRANTY;
232254
This is free software, and you are welcome to redistribute it
@@ -283,7 +305,12 @@ try {
283305
const cir = new zkSnark.Circuit(cirDef);
284306
const input = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8")));
285307

286-
const witness = cir.calculateWitness(input);
308+
const witness = cir.calculateWitness(input, {
309+
logOutput: argv.logoutput,
310+
logSet: argv.logset,
311+
logGet: argv.logget,
312+
logTrigger: argv.logtrigger
313+
});
287314

288315
fs.writeFileSync(witnessName, JSON.stringify(stringifyBigInts(witness), null, 1), "utf-8");
289316
process.exit(0);
@@ -324,6 +351,8 @@ try {
324351
verifierCode = generateVerifier_original(verificationKey);
325352
} else if (verificationKey.protocol == "groth") {
326353
verifierCode = generateVerifier_groth(verificationKey);
354+
} else if (verificationKey.protocol == "kimleeoh") {
355+
verifierCode = generateVerifier_kimleeoh(verificationKey);
327356
} else {
328357
throw new Error("InvalidProof");
329358
}
@@ -353,7 +382,7 @@ try {
353382
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
354383
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
355384
`[${inputs}]`;
356-
} else if (proof.protocol == "groth") {
385+
} else if ((proof.protocol == "groth")||(proof.protocol == "kimleeoh")) {
357386
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
358387
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
359388
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
@@ -394,7 +423,7 @@ function generateVerifier_original(verificationKey) {
394423
template = template.replace("<%vk_c%>", vkc_str);
395424

396425
const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
397-
`${verificationKey.vk_g[0][0].toString()}], `+
426+
`${verificationKey.vk_g[0][0].toString()}], `+
398427
`[${verificationKey.vk_g[1][1].toString()},` +
399428
`${verificationKey.vk_g[1][0].toString()}]`;
400429
template = template.replace("<%vk_g%>", vkg_str);
@@ -472,5 +501,44 @@ function generateVerifier_groth(verificationKey) {
472501
return template;
473502
}
474503

504+
function generateVerifier_kimleeoh(verificationKey) {
505+
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8");
506+
507+
508+
const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+
509+
`${verificationKey.vk_alfa_1[1].toString()}`;
510+
template = template.replace("<%vk_alfa1%>", vkalfa1_str);
511+
512+
const vkbeta2_str = `[${verificationKey.vk_beta_2[0][1].toString()},`+
513+
`${verificationKey.vk_beta_2[0][0].toString()}], `+
514+
`[${verificationKey.vk_beta_2[1][1].toString()},` +
515+
`${verificationKey.vk_beta_2[1][0].toString()}]`;
516+
template = template.replace("<%vk_beta2%>", vkbeta2_str);
517+
518+
const vkgamma2_str = `[${verificationKey.vk_gamma_2[0][1].toString()},`+
519+
`${verificationKey.vk_gamma_2[0][0].toString()}], `+
520+
`[${verificationKey.vk_gamma_2[1][1].toString()},` +
521+
`${verificationKey.vk_gamma_2[1][0].toString()}]`;
522+
template = template.replace("<%vk_gamma2%>", vkgamma2_str);
523+
524+
const vkdelta2_str = `[${verificationKey.vk_delta_2[0][1].toString()},`+
525+
`${verificationKey.vk_delta_2[0][0].toString()}], `+
526+
`[${verificationKey.vk_delta_2[1][1].toString()},` +
527+
`${verificationKey.vk_delta_2[1][0].toString()}]`;
528+
template = template.replace("<%vk_delta2%>", vkdelta2_str);
529+
530+
// The points
531+
532+
template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
533+
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
534+
let vi = "";
535+
for (let i=0; i<verificationKey.IC.length; i++) {
536+
if (vi != "") vi = vi + " ";
537+
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
538+
`${verificationKey.IC[i][1].toString()});\n`;
539+
}
540+
template = template.replace("<%vk_ic_pts%>", vi);
475541

542+
return template;
543+
}
476544

Diff for: index.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ exports.groth = {
2828
genProof: require("./src/prover_groth.js"),
2929
isValid: require("./src/verifier_groth.js")
3030
};
31+
exports.kimleeoh = {
32+
setup: require("./src/setup_kimleeoh.js"),
33+
genProof: require("./src/prover_kimleeoh.js"),
34+
isValid: require("./src/verifier_kimleeoh.js")
35+
};
3136
exports.bigInt = require("./src/bigint.js");
3237
exports.ZqField = require("./src/zqfield.js");
3338

Diff for: package-lock.json

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"chai": "^4.2.0",
3333
"escape-string-regexp": "^1.0.5",
3434
"eslint": "^5.16.0",
35+
"keccak": "^2.0.0",
3536
"yargs": "^12.0.5"
3637
},
3738
"devDependencies": {

Diff for: src/bigint.js

+27
Original file line numberDiff line numberDiff line change
@@ -478,5 +478,32 @@ wBigInt.prototype.leInt2Buff = function (len) {
478478
};
479479

480480

481+
wBigInt.beBuff2int = function(buff) {
482+
let res = wBigInt.zero;
483+
for (let i=0; i<buff.length; i++) {
484+
const n = wBigInt(buff[buff.length - i - 1]);
485+
res = res.add(n.shl(i*8));
486+
}
487+
return res;
488+
};
489+
490+
wBigInt.beInt2Buff = function(n, len) {
491+
let r = n;
492+
let o =len-1;
493+
const buff = Buffer.alloc(len);
494+
while ((r.greater(wBigInt.zero))&&(o>=0)) {
495+
let c = Number(r.and(wBigInt("255")));
496+
buff[o] = c;
497+
o--;
498+
r = r.shr(8);
499+
}
500+
if (r.greater(wBigInt.zero)) throw new Error("Number does not feed in buffer");
501+
return buff;
502+
};
503+
504+
wBigInt.prototype.beInt2Buff = function (len) {
505+
return wBigInt.beInt2Buff(this,len);
506+
};
507+
481508
module.exports = wBigInt;
482509

Diff for: src/calculateWitness.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ const bigInt = require("./bigint");
2121

2222
module.exports = calculateWitness;
2323

24-
function calculateWitness(circuit, inputSignals, log) {
25-
log = log || (() => {});
26-
const ctx = new RTCtx(circuit, log);
24+
function calculateWitness(circuit, inputSignals, options) {
25+
options = options || {};
26+
if (!options.logFunction) options.logFunction = console.log;
27+
const ctx = new RTCtx(circuit, options);
2728

2829
function iterateSelector(values, sels, cb) {
2930
if (!Array.isArray(values)) {
@@ -62,15 +63,15 @@ function calculateWitness(circuit, inputSignals, log) {
6263
if (typeof(ctx.witness[i]) == "undefined") {
6364
throw new Error("Signal not assigned: " + circuit.signalNames(i));
6465
}
65-
log(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
66+
if (options.logOutput) options.logFunction(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
6667
}
6768
return ctx.witness.slice(0, circuit.nVars);
6869
// return ctx.witness;
6970
}
7071

7172
class RTCtx {
72-
constructor(circuit, log) {
73-
this.log = log || function() {};
73+
constructor(circuit, options) {
74+
this.options = options;
7475
this.scopes = [];
7576
this.circuit = circuit;
7677
this.witness = new Array(circuit.nSignals);
@@ -104,8 +105,7 @@ class RTCtx {
104105
}
105106

106107
triggerComponent(c) {
107-
this.log("Component Treiggered: " + this.circuit.components[c].name);
108-
// console.log("Start Component Treiggered: " + this.circuit.components[c].name);
108+
if (this.options.logTrigger) this.options.logFunction("Component Treiggered: " + this.circuit.components[c].name);
109109

110110
// Set notInitSignals to -1 to not initialize again
111111
this.notInitSignals[c] --;
@@ -126,7 +126,8 @@ class RTCtx {
126126
this.circuit.templates[template](this);
127127
this.scopes = oldScope;
128128
this.currentComponent = oldComponent;
129-
// console.log("End Component Treiggered: " + this.circuit.components[c].name);
129+
130+
if (this.options.logTrigger) this.options.logFunction("End Component Treiggered: " + this.circuit.components[c].name);
130131
}
131132

132133
callFunction(functionName, params) {
@@ -149,7 +150,7 @@ class RTCtx {
149150
}
150151

151152
setSignalFullName(fullName, value) {
152-
this.log("set " + fullName + " <-- " + value.toString());
153+
if (this.options.logSet) this.options.logFunction("set " + fullName + " <-- " + value.toString());
153154
const sId = this.circuit.getSignalIdx(fullName);
154155
let firstInit =false;
155156
if (typeof(this.witness[sId]) == "undefined") {
@@ -218,7 +219,7 @@ class RTCtx {
218219
if (typeof(this.witness[sId]) == "undefined") {
219220
throw new Error("Signal not initialized: "+fullName);
220221
}
221-
this.log("get --->" + fullName + " = " + this.witness[sId].toString() );
222+
if (this.options.logGet) this.options.logFunction("get --->" + fullName + " = " + this.witness[sId].toString() );
222223
return this.witness[sId];
223224
}
224225

Diff for: src/prover_groth.js

+12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ module.exports = function genProof(vk_proof, witness) {
3535
const r = PolF.F.random();
3636
const s = PolF.F.random();
3737

38+
/* Uncomment to generate a deterministic proof to debug
39+
const r = PolF.F.zero;
40+
const s = PolF.F.zero;
41+
*/
42+
43+
3844
proof.pi_a = G1.zero;
3945
proof.pi_b = G2.zero;
4046
proof.pi_c = G1.zero;
@@ -71,10 +77,16 @@ module.exports = function genProof(vk_proof, witness) {
7177

7278
const h = calculateH(vk_proof, witness);
7379

80+
// proof.pi_c = G1.affine(proof.pi_c);
81+
// console.log("pi_onlyc", proof.pi_c);
82+
7483
for (let i = 0; i < h.length; i++) {
84+
// console.log(i + "->" + h[i].toString());
7585
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.hExps[i], h[i]));
7686
}
7787

88+
// proof.pi_c = G1.affine(proof.pi_c);
89+
// console.log("pi_candh", proof.pi_c);
7890

7991
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( proof.pi_a, s ));
8092
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( pib1, r ));

0 commit comments

Comments
 (0)