mirror of
https://github.com/tornadocash/tornado-core.git
synced 2025-05-02 06:16:03 -04:00
tests
This commit is contained in:
parent
24f15277bc
commit
335a79d2ae
5 changed files with 80 additions and 7 deletions
|
@ -7,7 +7,25 @@ const { toWei, toBN, fromWei } = require('web3-utils')
|
|||
const { takeSnapshot, revertSnapshot, increaseTime } = require('../scripts/ganacheHelper');
|
||||
|
||||
const Mixer = artifacts.require('./Mixer.sol')
|
||||
const { AMOUNT } = process.env
|
||||
|
||||
const utils = require("../scripts/utils")
|
||||
const stringifyBigInts = require("websnark/tools/stringifybigint").stringifyBigInts
|
||||
const snarkjs = require("snarkjs");
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const JsStorage = require('../lib/Storage')
|
||||
const MerkleTree = require('../lib/MerkleTree')
|
||||
const MimcHacher = 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;
|
||||
}
|
||||
|
||||
contract('Mixer', async accounts => {
|
||||
let mixer
|
||||
|
@ -22,18 +40,69 @@ contract('Mixer', async accounts => {
|
|||
let hasher
|
||||
|
||||
before(async () => {
|
||||
const storage = new JsStorage()
|
||||
hasher = new MimcHacher()
|
||||
tree = new MerkleTree(
|
||||
prefix,
|
||||
storage,
|
||||
hasher,
|
||||
levels,
|
||||
zeroValue,
|
||||
)
|
||||
mixer = await Mixer.deployed()
|
||||
snapshotId = await takeSnapshot()
|
||||
})
|
||||
|
||||
describe('#constructor', async () => {
|
||||
it('should initialize', async () => {
|
||||
const { AMOUNT } = process.env
|
||||
const transferValue = await mixer.transferValue()
|
||||
transferValue.should.be.eq.BN(toBN(AMOUNT))
|
||||
})
|
||||
})
|
||||
|
||||
describe('#deposit', async () => {
|
||||
it('should emit event', async () => {
|
||||
const commitment = 42
|
||||
const { logs } = await mixer.deposit(commitment, { value: AMOUNT, from: sender })
|
||||
logs[0].event.should.be.equal('LeafAdded')
|
||||
logs[0].args.leaf.should.be.eq.BN(toBN(commitment))
|
||||
logs[0].args.leaf_index.should.be.eq.BN(toBN(0))
|
||||
|
||||
logs[1].event.should.be.equal('Deposit')
|
||||
logs[1].args.from.should.be.equal(sender)
|
||||
logs[1].args.commitment.should.be.eq.BN(toBN(commitment))
|
||||
})
|
||||
})
|
||||
|
||||
describe('#withdraw', async () => {
|
||||
it.skip('should work', async () => {
|
||||
const deposit = generateDeposit()
|
||||
await tree.insert(deposit.commitment)
|
||||
await mixer.deposit(toBN(deposit.commitment.toString()), { value: AMOUNT, from: sender })
|
||||
|
||||
const {root, path_elements, path_index} = await tree.path(0);
|
||||
|
||||
// Circuit input
|
||||
const input = stringifyBigInts({
|
||||
// public
|
||||
root: root,
|
||||
nullifier: deposit.nullifier,
|
||||
receiver: utils.rbigint(20),
|
||||
fee: bigInt(1e17),
|
||||
|
||||
// private
|
||||
secret: deposit.secret,
|
||||
pathElements: path_elements,
|
||||
pathIndex: path_index,
|
||||
})
|
||||
|
||||
const { pi_a, pi_b, pi_c, publicSignals } = await utils.snarkProof(input)
|
||||
console.log('proof', pi_a, pi_b, pi_c, publicSignals)
|
||||
const { logs } = await mixer.withdraw(pi_a, pi_b, pi_c, publicSignals, { from: sender })
|
||||
console.log('logs', logs)
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await revertSnapshot(snapshotId.result)
|
||||
snapshotId = await takeSnapshot()
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const circom = require("circom");
|
||||
const snarkjs = require("snarkjs");
|
||||
const circomlib = require('circomlib');
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const stringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").stringifyBigInts;
|
||||
const unstringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").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 = stringifyBigInts({
|
||||
// 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");
|
||||
})();
|
|
@ -1,87 +0,0 @@
|
|||
const fs = require('fs');
|
||||
const circom = require("circom");
|
||||
const snarkjs = require("snarkjs");
|
||||
const groth = snarkjs["groth"];
|
||||
const crypto = require("crypto");
|
||||
const circomlib = require('circomlib');
|
||||
const pedersen = circomlib.pedersenHash;
|
||||
const babyjub = circomlib.babyJub;
|
||||
const mimcsponge = circomlib.mimcsponge;
|
||||
const bigInt = snarkjs.bigInt;
|
||||
const buildGroth16 = require('../node_modules/websnark/src/groth16.js');
|
||||
const stringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").stringifyBigInts;
|
||||
const unstringifyBigInts = require("../node_modules/websnark/tools/stringifybigint.js").unstringifyBigInts;
|
||||
|
||||
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes));
|
||||
|
||||
function pedersenHash(data) {
|
||||
return babyjub.unpackPoint(pedersen.hash(data))[0];
|
||||
}
|
||||
|
||||
|
||||
function mimcHash(left, right) {
|
||||
return mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString();
|
||||
}
|
||||
|
||||
function p256(o) {
|
||||
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
|
||||
let nstr = o.toString(16);
|
||||
while (nstr.length < 64) nstr = "0"+nstr;
|
||||
nstr = "0x"+nstr;
|
||||
return nstr;
|
||||
} else if (Array.isArray(o)) {
|
||||
return o.map(p256);
|
||||
} else if (typeof o == "object") {
|
||||
const res = {};
|
||||
for (let k in o) {
|
||||
if (k === "value") {
|
||||
return p256(o[k]);
|
||||
}
|
||||
res[k] = p256(o[k]);
|
||||
}
|
||||
return res;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
}
|
||||
|
||||
function convertWitness(witness) {
|
||||
const buffLen = witness.length * 32;
|
||||
const buff = new ArrayBuffer(buffLen);
|
||||
const h = {
|
||||
dataView: new DataView(buff),
|
||||
offset: 0
|
||||
};
|
||||
for (let i=0; i<witness.length; i++) {
|
||||
for (let i=0; i<8; i++) {
|
||||
//const v = witness[i].shiftRight(i*32).and(0xFFFFFFFF).toJSNumber();
|
||||
const v = Number(witness[i].shr(i * 32).and(BigInt(0xFFFFFFFF)));
|
||||
h.dataView.setUint32(h.offset, v, true);
|
||||
h.offset += 4;
|
||||
}
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
function toArrayBuffer(b) {
|
||||
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
||||
}
|
||||
|
||||
async function snarkProof(input) {
|
||||
const circuit = new snarkjs.Circuit(unstringifyBigInts(require("../build/circuits/withdraw.json")));
|
||||
const witnessArray = circuit.calculateWitness(input);
|
||||
const witness = convertWitness(witnessArray);
|
||||
const publicSignals = witnessArray.slice(1, circuit.nPubInputs + circuit.nOutputs + 1);
|
||||
const key = toArrayBuffer(fs.readFileSync("../build/circuits/withdraw_proving_key.bin"));
|
||||
const groth16 = await buildGroth16();
|
||||
let proof = await groth16.proof(witness, key);
|
||||
proof = unstringifyBigInts(proof);
|
||||
return p256({
|
||||
pi_a: [proof.pi_a[0], proof.pi_a[1]],
|
||||
pi_b: [[proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]]],
|
||||
pi_c: [proof.pi_c[0], proof.pi_c[1]],
|
||||
publicSignals: publicSignals,
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {rbigint, pedersenHash, snarkProof, mimcHash};
|
Loading…
Add table
Add a link
Reference in a new issue