Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
before('Setup Hasher Library', async () => {
hasherContract = await hasherFactory.deploy()
// Load circuits
mtInsertCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeInsert_test.circom'))
)
mtLeafExistsCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeLeafExists_test.circom'))
)
mtCheckRootCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeCheckRoot_test.circom'))
)
})
// Submits it to the smart contract
cmdTree.insert(user2NewLeaf, user2NewMessage)
// Construct circuit inputs
const [cmdTreePathElements, cmdTreePathIndex] = cmdTree.getPathUpdate(cmdTree.nextIndex - 1)
// 1st index because we're getting user 2
const [stateTreePathElements, stateTreePathIndex] = stateTree.getPathUpdate(1)
const ecdhPrivateKey = ecdh(
user2NewSecretKey,
coordinatorPublicKey
)
const circuit = new Circuit(updateStateTreeCircuitDef)
const circuitInput = {
cmd_tree_root: stringifyBigInts(cmdTree.root),
cmd_tree_path_elements: stringifyBigInts(cmdTreePathElements),
cmd_tree_path_index: stringifyBigInts(cmdTreePathIndex),
state_tree_root: stringifyBigInts(stateTree.root),
state_tree_path_elements: stringifyBigInts(stateTreePathElements),
state_tree_path_index: stringifyBigInts(stateTreePathIndex),
encrypted_data: stringifyBigInts(user2NewEncryptedMsg),
existing_public_key: stringifyBigInts(user2PublicKey),
existing_state_tree_leaf: stringifyBigInts(user2Leaf),
ecdh_private_key: stringifyBigInts(ecdhPrivateKey)
}
const witness = circuit.calculateWitness(circuitInput)
assert(circuit.checkWitness(witness))
before('Setup Hasher Library', async () => {
hasherContract = await hasherFactory.deploy()
// Load circuits
mtInsertCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeInsert_test.circom'))
)
mtLeafExistsCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeLeafExists_test.circom'))
)
mtCheckRootCircuit = new Circuit(
await compiler(path.join(__dirname, '../test/merkleTreeCheckRoot_test.circom'))
)
})
before( async () => {
const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname,'../../build/circuit.json')).toString());
circuit = new snarkjs.Circuit(cirDef);
console.log('NConstrains Semaphore: ' + circuit.nConstraints);
});
it('#Verification', async () => {
const circuitDef = await compiler(path.join(__dirname, 'verify_signature_test.circom'))
const circuit = new Circuit(circuitDef)
const sk1 = randomPrivateKey()
const pk1 = privateToPublicKey(sk1)
const msg = [3n, 4n, 5n, 32767n]
const msgHash = multiHash(msg)
const signature = sign(sk1, msgHash)
const circuitInputs = {
'from_x': stringifyBigInts(pk1[0]),
'from_y': stringifyBigInts(pk1[1]),
'R8x': stringifyBigInts(signature.R8[0]),
'R8y': stringifyBigInts(signature.R8[1]),
'S': stringifyBigInts(signature.S),
'preimage': stringifyBigInts(msg)
it('#Decryption', async () => {
const circuitDef = await compiler(path.join(__dirname, 'decrypt_test.circom'))
const circuit = new Circuit(circuitDef)
const sk1 = randomPrivateKey()
const sk2 = randomPrivateKey()
const pk1 = privateToPublicKey(sk1)
const pk2 = privateToPublicKey(sk2)
const sharedKey = ecdh(sk2, pk1)
const msg = [3n, 4n, 5n, 32767n]
const encryptedMsg = encrypt(msg, sk1, pk2)
const circuitInputs = {
'message': stringifyBigInts(encryptedMsg),
'private_key': stringifyBigInts(sharedKey)
it('tests proof', async () => {
const cirDef = JSON.parse(fs.readFileSync(path.join(__dirname,'../../build/circuit.json')).toString());
circuit = new snarkjs.Circuit(cirDef);
const prvKey = Buffer.from('0001020304050607080900010203040506070809000102030405060708090001', 'hex');
const pubKey = eddsa.prv2pub(prvKey);
const external_nullifier = bigInt('12312');
const signal_str = 'hello!';
const signal_hash_raw = crypto.createHash('sha256').update(signal_str, 'utf8').digest();
const signal_hash = beBuff2int(signal_hash_raw.slice(0, 31));
const signal_to_contract = web3.utils.asciiToHex(signal_str);
const msg = mimc7.multiHash([external_nullifier, signal_hash]);
const signature = eddsa.signMiMC(prvKey, msg);
assert(eddsa.verifyMiMC(msg, signature, pubKey));
it('#HashOutput', async () => {
const circuitDef = await compiler(path.join(__dirname, 'hashleftright_test.circom'))
const circuit = new Circuit(circuitDef)
const left = 32767n
const right = 1337n
const circuitInputs = {
'left': stringifyBigInts(left),
'right': stringifyBigInts(right)
}
const witness = circuit.calculateWitness(circuitInputs)
const outputIdx = circuit.getSignalIdx('main.hash')
const output = witness[outputIdx]
const outputJS = hashLeftRight(left, right)
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");
}
}