mirror of
https://github.com/arnaucube/miksi-core.git
synced 2026-02-06 19:16:40 +01:00
src/miksi.ts works on browser
This commit is contained in:
104
src/miksi.ts
104
src/miksi.ts
@@ -1,6 +1,7 @@
|
||||
const fs = require("fs");
|
||||
const bigInt = require("big-integer");
|
||||
const { groth } = require('snarkjs');
|
||||
const { Fr } = require('ffjavascript').bn128;
|
||||
const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
|
||||
const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
|
||||
const circomlib = require("circomlib");
|
||||
@@ -14,25 +15,38 @@ const coinCode = "0"; // refearing to ETH
|
||||
const ethAmount = '1';
|
||||
const amount = Web3.utils.toWei(ethAmount, 'ether');
|
||||
|
||||
// let bn128;
|
||||
//
|
||||
// exports.init = async () => {
|
||||
// bn128 = await buildBn128();
|
||||
// }
|
||||
exports.randBigInt = () => {
|
||||
return Fr.random();
|
||||
};
|
||||
|
||||
exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
exports.calcCommitment = (secret, nullifier) => {
|
||||
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
|
||||
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
|
||||
return commitment;
|
||||
};
|
||||
|
||||
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
|
||||
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
|
||||
|
||||
// rebuild the tree
|
||||
let tree = await smt.newMemEmptyTrie();
|
||||
await tree.insert(1, 0);
|
||||
for (let i=0; i<commitments.length; i++) {
|
||||
await tree.insert(commitments[i], 0);
|
||||
}
|
||||
|
||||
// old root
|
||||
const rootOld = tree.root;
|
||||
const resOld = await tree.find(commitment);
|
||||
if (resOld.found) {
|
||||
console.error("leaf expect to not exist but exists");
|
||||
let oldKey = "0";
|
||||
if (!resOld.found) {
|
||||
oldKey = resOld.notFoundKey.toString();
|
||||
}
|
||||
console.log("oldKey", oldKey);
|
||||
// if (resOld.found) {
|
||||
// console.error("leaf expect to not exist but exists");
|
||||
// }
|
||||
let siblingsOld = resOld.siblings;
|
||||
while (siblingsOld.length < nLevels) {
|
||||
siblingsOld.push("0");
|
||||
@@ -57,6 +71,7 @@ exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
"amount": amount,
|
||||
"secret": secret,
|
||||
"nullifier": nullifier,
|
||||
"oldKey": oldKey,
|
||||
"siblingsOld": siblingsOld,
|
||||
"siblingsNew": siblingsNew,
|
||||
"rootOld": rootOld,
|
||||
@@ -73,6 +88,70 @@ exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
|
||||
const wBuff = Buffer.allocUnsafe(witness.length*32);
|
||||
|
||||
for (let i=0; i<witness.length; i++) {
|
||||
for (let j=0; j<8; j++) {
|
||||
const bi = witness[i];
|
||||
const v = bigInt(bi).shiftRight(j*32).and(0xFFFFFFFF).toJSNumber();
|
||||
// wBuff.writeUInt32LE(v, i*32 + j*4, 4)
|
||||
wBuff.writeUInt32LE(v, i*32 + j*4)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// const witness = unstringifyBigInts(stringifyBigInts(w));
|
||||
// return wBuff;
|
||||
return {
|
||||
witness: wBuff,
|
||||
publicInputs: {
|
||||
commitment:commitment,
|
||||
root:rootNew
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr) => {
|
||||
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
|
||||
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
|
||||
|
||||
// rebuild the tree
|
||||
let tree = await smt.newMemEmptyTrie();
|
||||
await tree.insert(1, 0);
|
||||
for (let i=0; i<commitments.length; i++) {
|
||||
await tree.insert(commitments[i], 0);
|
||||
}
|
||||
// await tree.insert(commitment, 0);
|
||||
|
||||
// root
|
||||
const root = tree.root;
|
||||
const res = await tree.find(commitment);
|
||||
if (!res.found) {
|
||||
console.error("leaf expect to exist but not exists");
|
||||
}
|
||||
let siblings = res.siblings;
|
||||
while (siblings.length < nLevels) {
|
||||
siblings.push("0");
|
||||
};
|
||||
|
||||
// calculate witness
|
||||
const input = unstringifyBigInts({
|
||||
"coinCode": coinCode,
|
||||
"amount": amount,
|
||||
"secret": secret,
|
||||
"nullifier": nullifier,
|
||||
"siblings": siblings,
|
||||
"root": root,
|
||||
"address": addr
|
||||
});
|
||||
console.log("input", input);
|
||||
// const options = {};
|
||||
// const wc = await WitnessCalculatorBuilder(wasm, options);
|
||||
|
||||
const wc = await WitnessCalculatorBuilder(wasm);
|
||||
|
||||
const witness = await wc.calculateWitness(input, {sanityCheck: true});
|
||||
|
||||
const wBuff = Buffer.allocUnsafe(witness.length*32);
|
||||
|
||||
for (let i=0; i<witness.length; i++) {
|
||||
for (let j=0; j<8; j++) {
|
||||
const bi = witness[i];
|
||||
@@ -83,7 +162,14 @@ exports.calcWitness = async (wasm, secret, nullifier, commitments) => {
|
||||
|
||||
|
||||
// const witness = unstringifyBigInts(stringifyBigInts(w));
|
||||
return wBuff;
|
||||
return {
|
||||
witness: wBuff,
|
||||
publicInputs: {
|
||||
address:addr,
|
||||
nullifier:nullifier
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user