mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-06-25 22:50:29 -04:00
merge
This commit is contained in:
commit
1194e76b9a
5 changed files with 76 additions and 3 deletions
66
scripts/test_snark.js
Normal file
66
scripts/test_snark.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
const fs = require('fs');
|
||||
const assert = require('assert');
|
||||
const circom = require("circom");
|
||||
const snarkjs = require("snarkjs");
|
||||
const circomlib = require('circomlib');
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const stringifyBigInts = require("websnark/tools/stringifybigint").stringifyBigInts;
|
||||
const unstringifyBigInts = require("websnark/tools/stringifybigint").unstringifyBigInts;
|
||||
const utils = require("./utils");
|
||||
const merkleTree = require('../lib/MerkleTree');
|
||||
const jsStorage = require("../lib/Storage");
|
||||
const mimcHasher = require("../lib/MiMC");
|
||||
|
||||
function generateDeposit() {
|
||||
let deposit = {
|
||||
secret: utils.rbigint(31),
|
||||
nullifier: utils.rbigint(31),
|
||||
};
|
||||
const preimage = Buffer.concat([deposit.nullifier.leInt2Buff(32), deposit.secret.leInt2Buff(32)]);
|
||||
deposit.commitment = utils.pedersenHash(preimage);
|
||||
return deposit;
|
||||
}
|
||||
|
||||
(async () => {
|
||||
const dep1 = generateDeposit();
|
||||
const dep2 = generateDeposit();
|
||||
const dep3 = generateDeposit();
|
||||
|
||||
const tree = new merkleTree("", new jsStorage(), new mimcHasher(), 16, 0);
|
||||
|
||||
await tree.insert(dep1.commitment);
|
||||
await tree.insert(dep2.commitment);
|
||||
await tree.insert(dep3.commitment);
|
||||
|
||||
const {root, path_elements, path_index} = await tree.path(1);
|
||||
|
||||
// Circuit input
|
||||
const input = {
|
||||
// public
|
||||
root: root,
|
||||
nullifier: dep2.nullifier,
|
||||
receiver: utils.rbigint(20),
|
||||
fee: bigInt(1e17),
|
||||
|
||||
// private
|
||||
secret: dep2.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndex: path_index,
|
||||
};
|
||||
|
||||
console.log("Input:\n", input);
|
||||
console.time("Time");
|
||||
const proof = await utils.snarkProof(input);
|
||||
console.log("Proof:\n", proof);
|
||||
console.timeEnd("Time");
|
||||
|
||||
const verify = await utils.snarkVerify(proof);
|
||||
assert(verify);
|
||||
|
||||
// try to cheat with recipient
|
||||
proof.publicSignals[2] = '0x000000000000000000000000000000000000000000000000000000000000beef';
|
||||
const verifyScam = await utils.snarkVerify(proof);
|
||||
assert(!verifyScam);
|
||||
|
||||
console.log("Done.");
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue