Skip to content

Commit

Permalink
log functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaylina committed Jun 15, 2019
1 parent 75a7b6e commit 406ec9f
Show file tree
Hide file tree
Showing 12 changed files with 903 additions and 15 deletions.
76 changes: 72 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ setup command
Default: verification_key.json
--protocol [original|groth]
--protocol [original|groth|kimleeoh]
Defines withc variant of snark you want to use
Expand Down Expand Up @@ -94,6 +94,23 @@ calculate witness command
Default: witness.json
--lo or --logoutput
Output all the Output signals
--lg or --logget
Output GET access to the signals
--ls or --logset
Output SET access to the signal
--lt or --logtrigger
Output when a subcomponent is triggered and when finished
generate a proof command
========================
Expand Down Expand Up @@ -225,8 +242,13 @@ print constraints
.alias("i", "input")
.alias("pub", "public")
.alias("v", "verifier")
.alias("lo", "logoutput")
.alias("lg", "logget")
.alias("ls", "logset")
.alias("lt", "logtrigger")
.help("h")
.alias("h", "help")

.epilogue(`Copyright (C) 2018 0kims association
This program comes with ABSOLUTELY NO WARRANTY;
This is free software, and you are welcome to redistribute it
Expand Down Expand Up @@ -283,7 +305,12 @@ try {
const cir = new zkSnark.Circuit(cirDef);
const input = unstringifyBigInts(JSON.parse(fs.readFileSync(inputName, "utf8")));

const witness = cir.calculateWitness(input);
const witness = cir.calculateWitness(input, {
logOutput: argv.logoutput,
logSet: argv.logset,
logGet: argv.logget,
logTrigger: argv.logtrigger
});

fs.writeFileSync(witnessName, JSON.stringify(stringifyBigInts(witness), null, 1), "utf-8");
process.exit(0);
Expand Down Expand Up @@ -324,6 +351,8 @@ try {
verifierCode = generateVerifier_original(verificationKey);
} else if (verificationKey.protocol == "groth") {
verifierCode = generateVerifier_groth(verificationKey);
} else if (verificationKey.protocol == "kimleeoh") {
verifierCode = generateVerifier_kimleeoh(verificationKey);
} else {
throw new Error("InvalidProof");
}
Expand Down Expand Up @@ -353,7 +382,7 @@ try {
`[${p256(proof.pi_h[0])}, ${p256(proof.pi_h[1])}],` +
`[${p256(proof.pi_kp[0])}, ${p256(proof.pi_kp[1])}],` +
`[${inputs}]`;
} else if (proof.protocol == "groth") {
} else if ((proof.protocol == "groth")||(proof.protocol == "kimleeoh")) {
S=`[${p256(proof.pi_a[0])}, ${p256(proof.pi_a[1])}],` +
`[[${p256(proof.pi_b[0][1])}, ${p256(proof.pi_b[0][0])}],[${p256(proof.pi_b[1][1])}, ${p256(proof.pi_b[1][0])}]],` +
`[${p256(proof.pi_c[0])}, ${p256(proof.pi_c[1])}],` +
Expand Down Expand Up @@ -394,7 +423,7 @@ function generateVerifier_original(verificationKey) {
template = template.replace("<%vk_c%>", vkc_str);

const vkg_str = `[${verificationKey.vk_g[0][1].toString()},`+
`${verificationKey.vk_g[0][0].toString()}], `+
`${verificationKey.vk_g[0][0].toString()}], `+
`[${verificationKey.vk_g[1][1].toString()},` +
`${verificationKey.vk_g[1][0].toString()}]`;
template = template.replace("<%vk_g%>", vkg_str);
Expand Down Expand Up @@ -472,5 +501,44 @@ function generateVerifier_groth(verificationKey) {
return template;
}

function generateVerifier_kimleeoh(verificationKey) {
let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8");


const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+
`${verificationKey.vk_alfa_1[1].toString()}`;
template = template.replace("<%vk_alfa1%>", vkalfa1_str);

const vkbeta2_str = `[${verificationKey.vk_beta_2[0][1].toString()},`+
`${verificationKey.vk_beta_2[0][0].toString()}], `+
`[${verificationKey.vk_beta_2[1][1].toString()},` +
`${verificationKey.vk_beta_2[1][0].toString()}]`;
template = template.replace("<%vk_beta2%>", vkbeta2_str);

const vkgamma2_str = `[${verificationKey.vk_gamma_2[0][1].toString()},`+
`${verificationKey.vk_gamma_2[0][0].toString()}], `+
`[${verificationKey.vk_gamma_2[1][1].toString()},` +
`${verificationKey.vk_gamma_2[1][0].toString()}]`;
template = template.replace("<%vk_gamma2%>", vkgamma2_str);

const vkdelta2_str = `[${verificationKey.vk_delta_2[0][1].toString()},`+
`${verificationKey.vk_delta_2[0][0].toString()}], `+
`[${verificationKey.vk_delta_2[1][1].toString()},` +
`${verificationKey.vk_delta_2[1][0].toString()}]`;
template = template.replace("<%vk_delta2%>", vkdelta2_str);

// The points

template = template.replace("<%vk_input_length%>", (verificationKey.IC.length-1).toString());
template = template.replace("<%vk_ic_length%>", verificationKey.IC.length.toString());
let vi = "";
for (let i=0; i<verificationKey.IC.length; i++) {
if (vi != "") vi = vi + " ";
vi = vi + `vk.IC[${i}] = Pairing.G1Point(${verificationKey.IC[i][0].toString()},`+
`${verificationKey.IC[i][1].toString()});\n`;
}
template = template.replace("<%vk_ic_pts%>", vi);

return template;
}

5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ exports.groth = {
genProof: require("./src/prover_groth.js"),
isValid: require("./src/verifier_groth.js")
};
exports.kimleeoh = {
setup: require("./src/setup_kimleeoh.js"),
genProof: require("./src/prover_kimleeoh.js"),
isValid: require("./src/verifier_kimleeoh.js")
};
exports.bigInt = require("./src/bigint.js");
exports.ZqField = require("./src/zqfield.js");

Expand Down
34 changes: 34 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"chai": "^4.2.0",
"escape-string-regexp": "^1.0.5",
"eslint": "^5.16.0",
"keccak": "^2.0.0",
"yargs": "^12.0.5"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions src/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,5 +478,32 @@ wBigInt.prototype.leInt2Buff = function (len) {
};


wBigInt.beBuff2int = function(buff) {
let res = wBigInt.zero;
for (let i=0; i<buff.length; i++) {
const n = wBigInt(buff[buff.length - i - 1]);
res = res.add(n.shl(i*8));
}
return res;
};

wBigInt.beInt2Buff = function(n, len) {
let r = n;
let o =len-1;
const buff = Buffer.alloc(len);
while ((r.greater(wBigInt.zero))&&(o>=0)) {
let c = Number(r.and(wBigInt("255")));
buff[o] = c;
o--;
r = r.shr(8);
}
if (r.greater(wBigInt.zero)) throw new Error("Number does not feed in buffer");
return buff;
};

wBigInt.prototype.beInt2Buff = function (len) {
return wBigInt.beInt2Buff(this,len);
};

module.exports = wBigInt;

23 changes: 12 additions & 11 deletions src/calculateWitness.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const bigInt = require("./bigint");

module.exports = calculateWitness;

function calculateWitness(circuit, inputSignals, log) {
log = log || (() => {});
const ctx = new RTCtx(circuit, log);
function calculateWitness(circuit, inputSignals, options) {
options = options || {};
if (!options.logFunction) options.logFunction = console.log;
const ctx = new RTCtx(circuit, options);

function iterateSelector(values, sels, cb) {
if (!Array.isArray(values)) {
Expand Down Expand Up @@ -62,15 +63,15 @@ function calculateWitness(circuit, inputSignals, log) {
if (typeof(ctx.witness[i]) == "undefined") {
throw new Error("Signal not assigned: " + circuit.signalNames(i));
}
log(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
if (options.logOutput) options.logFunction(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
}
return ctx.witness.slice(0, circuit.nVars);
// return ctx.witness;
}

class RTCtx {
constructor(circuit, log) {
this.log = log || function() {};
constructor(circuit, options) {
this.options = options;
this.scopes = [];
this.circuit = circuit;
this.witness = new Array(circuit.nSignals);
Expand Down Expand Up @@ -104,8 +105,7 @@ class RTCtx {
}

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

// Set notInitSignals to -1 to not initialize again
this.notInitSignals[c] --;
Expand All @@ -126,7 +126,8 @@ class RTCtx {
this.circuit.templates[template](this);
this.scopes = oldScope;
this.currentComponent = oldComponent;
// console.log("End Component Treiggered: " + this.circuit.components[c].name);

if (this.options.logTrigger) this.options.logFunction("End Component Treiggered: " + this.circuit.components[c].name);
}

callFunction(functionName, params) {
Expand All @@ -149,7 +150,7 @@ class RTCtx {
}

setSignalFullName(fullName, value) {
this.log("set " + fullName + " <-- " + value.toString());
if (this.options.logSet) this.options.logFunction("set " + fullName + " <-- " + value.toString());
const sId = this.circuit.getSignalIdx(fullName);
let firstInit =false;
if (typeof(this.witness[sId]) == "undefined") {
Expand Down Expand Up @@ -218,7 +219,7 @@ class RTCtx {
if (typeof(this.witness[sId]) == "undefined") {
throw new Error("Signal not initialized: "+fullName);
}
this.log("get --->" + fullName + " = " + this.witness[sId].toString() );
if (this.options.logGet) this.options.logFunction("get --->" + fullName + " = " + this.witness[sId].toString() );
return this.witness[sId];
}

Expand Down
12 changes: 12 additions & 0 deletions src/prover_groth.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ module.exports = function genProof(vk_proof, witness) {
const r = PolF.F.random();
const s = PolF.F.random();

/* Uncomment to generate a deterministic proof to debug
const r = PolF.F.zero;
const s = PolF.F.zero;
*/


proof.pi_a = G1.zero;
proof.pi_b = G2.zero;
proof.pi_c = G1.zero;
Expand Down Expand Up @@ -71,10 +77,16 @@ module.exports = function genProof(vk_proof, witness) {

const h = calculateH(vk_proof, witness);

// proof.pi_c = G1.affine(proof.pi_c);
// console.log("pi_onlyc", proof.pi_c);

for (let i = 0; i < h.length; i++) {
// console.log(i + "->" + h[i].toString());
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.hExps[i], h[i]));
}

// proof.pi_c = G1.affine(proof.pi_c);
// console.log("pi_candh", proof.pi_c);

proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( proof.pi_a, s ));
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( pib1, r ));
Expand Down
Loading

0 comments on commit 406ec9f

Please sign in to comment.