Skip to content

Commit

Permalink
typos fixed & code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
naveed949 committed Aug 29, 2022
1 parent 382908d commit 659f441
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
34 changes: 8 additions & 26 deletions src/1_generate_keys.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
const snarkjs = require("snarkjs");
const fs = require("fs");

async function run(circuit) {
const input = JSON.parse(fs.readFileSync(`./factor/input.json`, { encoding: 'utf8' }));
// await snarkjs.wtns.calculate(`${JSON.parse(input)}`,`${circuit}.wasm`,`./factor/${circuit}_final_1.wtns`)
const { proof, publicSignals } = await snarkjs.groth16.fullProve(input, `./factor/${circuit}.wasm`, `${circuit}_final_1.zkey`);

console.log("Proof: ");
console.log(JSON.stringify(proof, null, 1));

const vKey = JSON.parse(fs.readFileSync(`${circuit}_verification_key.json`));

const res = await snarkjs.groth16.verify(vKey, publicSignals, proof);

if (res === true) {
console.log("Verification OK");
} else {
console.log("Invalid proof");
}

}
const generator = async (circuit) => {
console.info('Initializing ZKey process...')
await snarkjs.zKey.newZKey(`./factor/${circuit}.r1cs`, "./factor/powerOfTau_final.ptau", `${circuit}_00.zkey`)

await snarkjs.zKey.newZKey(`../circuit_compiled/${circuit}.r1cs`, "../keys/powerOfTau_final.ptau", `../keys/${circuit}_00.zkey`)
console.info('First contribution...')
await snarkjs.zKey.contribute(`${circuit}_00.zkey`,`${circuit}_01.zkey`,'cintribution#1','thisismycontribution')
await snarkjs.zKey.contribute(`../keys/${circuit}_00.zkey`,`../keys/${circuit}_01.zkey`,'cintribution#1','thisismycontribution')
console.info('Beacon phase...')
await snarkjs.zKey.beacon(`${circuit}_01.zkey`,`${circuit}_final.zkey`,'beaconContribution','0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',10)
await snarkjs.zKey.beacon(`../keys/${circuit}_01.zkey`,`../keys/${circuit}_final.zkey`,'beaconContribution','0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',10)
console.info('Verifying the Zkey...')
const flag = await snarkjs.zKey.verifyFromR1cs(`./factor/${circuit}.r1cs`,"./factor/powerOfTau_final.ptau",`${circuit}_final.zkey`)
const flag = await snarkjs.zKey.verifyFromR1cs(`../circuit_compiled/${circuit}.r1cs`,"../keys/powerOfTau_final.ptau",`../keys/${circuit}_final.zkey`)
if (!flag){
console.error('Verification failed, something went wrong!')
process.exit(1)
}

console.info('Verification done! \nExporting the Verification Key...')
const Vk = await snarkjs.zKey.exportVerificationKey(`${circuit}_final_1.zkey`)
fs.writeFileSync(`${circuit}_verification_key.json`, JSON.stringify(Vk))
console.info('ZKeys generated and written successfully at ../keys')
const Vk = await snarkjs.zKey.exportVerificationKey(`../keys/${circuit}_final.zkey`)
fs.writeFileSync(`../keys/${circuit}_verification_key.json`, JSON.stringify(Vk))
console.info('ZKeys generated and written successfully at ../keys/*')
return circuit;
};

Expand Down
5 changes: 2 additions & 3 deletions src/2_generate_proof.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ const fs = require("fs");
async function proof_generator() {
const { proof, publicSignals } = await snarkjs.groth16.fullProve({a: 10, b: 21}, "../circuit_compiled/circuit.wasm", "../keys/circuit_final.zkey");

console.log("Proof Generated!");
fs.writeFileSync("../proof.json",JSON.stringify(proof))
fs.writeFileSync("../publicInput.json",JSON.stringify(publicSignals))
console.log("Proof & public input Generated");
fs.writeFileSync("../publicInputSignals.json",JSON.stringify(publicSignals))
console.log("Proof & publicInput Generated!");

}

Expand Down
12 changes: 6 additions & 6 deletions src/3_verify_proof.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const snarkjs = require("snarkjs");
const fs = require("fs");

async function verifier() {
const vKey = JSON.parse(fs.readFileSync("../keys/verification_key.json"));
const proof = JSON.parse(fs.readFileSync("proof.json"));
const publicSignals = JSON.parse(fs.readFileSync("publicpublicSignals.json"));
async function verifier(circuit) {
const vKey = JSON.parse(fs.readFileSync(`../keys/${circuit}_verification_key.json`));
const proof = JSON.parse(fs.readFileSync('../proof.json'));
const publicSignals = JSON.parse(fs.readFileSync('../publicInputSignals.json'));

const res = await snarkjs.groth16.verify(vKey, publicSignals, proof);

if (res === true) {
console.log("Verification OK");
console.log("Proof verified successfully");
} else {
console.log("Invalid proof");
}

}

verifier().then(() => {
verifier('circuit').then(() => {
process.exit(0);
});

0 comments on commit 659f441

Please sign in to comment.