Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const createTrustedSetup = async (compiledCircuitFiles) => {
console.log('===== Trusted Setup Started =====');
console.log('===== Generating pk and vk =====');
for (compiledCircuit of compiledCircuitFiles) {
let extension = path.extname(compiledCircuit, 'json');
let nameOfFile = path.basename(compiledCircuit, extension);
const file = require(`${process.cwd()}/${compiledCircuit}`);
let circuit = new zkSnark.Circuit(file);
let setup = zkSnark.original.setup(circuit);
fs.writeFileSync(`${trustedSetup}/${nameOfFile}_proving_key.json`, JSON.stringify(zkSnark.stringifyBigInts(setup.vk_proof), null, 1), "utf8");
fs.writeFileSync(`${trustedSetup}/${nameOfFile}_verification_key.json`, JSON.stringify(zkSnark.stringifyBigInts(setup.vk_verifier), null, 1), "utf8");
}
}
const createTrustedSetup = async (compiledCircuitFiles) => {
console.log('===== Trusted Setup Started =====');
console.log('===== Generating pk and vk =====');
for (compiledCircuit of compiledCircuitFiles) {
let extension = path.extname(compiledCircuit, 'json');
let nameOfFile = path.basename(compiledCircuit, extension);
const file = require(`${process.cwd()}/${compiledCircuit}`);
let circuit = new zkSnark.Circuit(file);
let setup = zkSnark.original.setup(circuit);
fs.writeFileSync(`${trustedSetup}/${nameOfFile}_proving_key.json`, JSON.stringify(zkSnark.stringifyBigInts(setup.vk_proof), null, 1), "utf8");
fs.writeFileSync(`${trustedSetup}/${nameOfFile}_verification_key.json`, JSON.stringify(zkSnark.stringifyBigInts(setup.vk_verifier), null, 1), "utf8");
}
}
const generateProof = (provingKey, witness, circuit) => {
console.log('===== Generating Proof =====');
const proofObject = zkSnark.original.genProof(provingKey, witness);
let extension = path.extname(circuit, 'json');
let nameOfFile = path.basename(circuit, extension);
fs.writeFileSync(`${generatedProofPath}/${nameOfFile}_proof.json`, JSON.stringify(zkSnark.stringifyBigInts(proofObject.proof), null, 1), "utf8");
fs.writeFileSync(`${generatedProofPath}/${nameOfFile}_public_signals.json`, JSON.stringify(zkSnark.stringifyBigInts(proofObject.publicSignals), null, 1), "utf8");
}
const generateProof = (provingKey, witness, circuit) => {
console.log('===== Generating Proof =====');
const proofObject = zkSnark.original.genProof(provingKey, witness);
let extension = path.extname(circuit, 'json');
let nameOfFile = path.basename(circuit, extension);
fs.writeFileSync(`${generatedProofPath}/${nameOfFile}_proof.json`, JSON.stringify(zkSnark.stringifyBigInts(proofObject.proof), null, 1), "utf8");
fs.writeFileSync(`${generatedProofPath}/${nameOfFile}_public_signals.json`, JSON.stringify(zkSnark.stringifyBigInts(proofObject.publicSignals), null, 1), "utf8");
}
const run = async (signals, proof) => {
const publicSignals = require(`${process.cwd()}/${generatedProofPath}/${signals}`);
const generatedProof = zkSnark.unstringifyBigInts((require(`${process.cwd()}/${generatedProofPath}/${proof}`)));
createGenerateCallFolder(generatedCallPath);
const generatedCall = await zkSnark.generateCall(publicSignals, generatedProof);
fs.writeFileSync(`${generatedCallPath}/generatedCall.json`, zkSnark.stringifyBigInts(generatedCall));
console.log('===== Generated Call Complete! =====');
console.log('===== Generated Call: =====')
console.log(generatedCall)
};
const inputs = {
'identity_pk[0]': pubKey[0],
'identity_pk[1]': pubKey[1],
'auth_sig_r[0]': signature.R8[0],
'auth_sig_r[1]': signature.R8[1],
auth_sig_s: signature.S,
signal_hash,
external_nullifier,
identity_nullifier,
identity_trapdoor,
identity_path_elements,
identity_path_index,
fake_zero: bigInt(0),
};
const w = this.circuit.calculateWitness(inputs);
const witness_bin = proof_util.convertWitness(snarkjs.stringifyBigInts(w));
const publicSignals = w.slice(1, this.circuit.nPubInputs + this.circuit.nOutputs+1);
logger.info(`calculating witness (ended at ${Date.now()})`);
const root = w[this.circuit.getSignalIdx('main.root')];
const nullifiers_hash = w[this.circuit.getSignalIdx('main.nullifiers_hash')];
assert(this.circuit.checkWitness(w));
logger.info(`identity commitment from proof: ${w[this.circuit.getSignalIdx('main.identity_commitment.out[0]')].toString()}`);
assert.equal(w[this.circuit.getSignalIdx('main.root')].toString(), identity_path.root);
logger.info(`generating proof (started at ${Date.now()})`);
const proof = await proof_util.prove(witness_bin.buffer, this.vk_proof.buffer);
logger.info(`proof: ${JSON.stringify(stringifyBigInts(proof))}`);
assert(snarkjs.groth.isValid(this.verifier_key, proof, publicSignals));
logger.info(`generating proof (ended at ${Date.now()})`);