arnaucube
arnaucube/miksi-corePublic Self-hosted repository
miksi-core/src/miksi.ts
src/miksi.tsRaw
  1const fs = require("fs");
  2const bigInt = require("big-integer");
  3const { groth } = require('snarkjs');
  4const { Fr } = require('ffjavascript').bn128;
  5const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
  6const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  7const circomlib = require("circomlib");
  8const smt = require("circomlib").smt;
  9const Web3 = require("web3");
 10// const buildBn128 = require("wasmsnark").buildBn128;
 11
 12
 13const nLevels = 17;
 14const coinCode = "0"; // refearing to ETH
 15const ethAmount = '1';
 16const amount = Web3.utils.toWei(ethAmount, 'ether');
 17
 18exports.randBigInt = () => {
 19	return Fr.random();
 20};
 21
 22exports.calcCommitment = (secret, nullifier) => {
 23	const poseidon = circomlib.poseidon.createHash(6, 8, 57);
 24	const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
 25	return commitment;
 26};
 27
 28exports.calcDepositWitness = async (wasm, secret, nullifier, commitments, key) => {
 29	const poseidon = circomlib.poseidon.createHash(6, 8, 57);
 30	const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
 31
 32	console.log("PROVA", poseidon([key, commitment]).toString());
 33
 34	// rebuild the tree
 35	let tree = await smt.newMemEmptyTrie();
 36	await tree.insert(0, 0);
 37	for (let i=0; i<commitments.length; i++) {
 38		await tree.insert(i+1, commitments[i]);
 39	}
 40
 41	// old root
 42	const rootOld = tree.root;
 43	const resOld = await tree.find(commitment);
 44	console.log("FIND old", resOld);
 45	let oldKey = "0";
 46	let oldValue = "0";
 47	if (!resOld.found) {
 48		oldKey = resOld.notFoundKey.toString();
 49		oldValue = resOld.notFoundValue.toString();
 50	}
 51	console.log("oldKey", oldKey);
 52	console.log("oldValue", oldValue);
 53	// if (resOld.found) {
 54	//         console.error("leaf expect to not exist but exists");
 55	// }
 56	let siblingsOld = resOld.siblings;
 57	while (siblingsOld.length < nLevels) {
 58		siblingsOld.push("0");
 59	};
 60
 61	await tree.insert(key, commitment);
 62
 63	// new root
 64	const rootNew = tree.root;
 65	const resNew = await tree.find(key);
 66	console.log("FIND new", resNew);
 67	if (!resNew.found) {
 68		console.error("leaf with the new commitment expect to exist but not exists");
 69	}
 70	let siblingsNew = resNew.siblings;
 71	while (siblingsNew.length < nLevels) {
 72		siblingsNew.push("0");
 73	};
 74
 75	// calculate witness
 76	const input = unstringifyBigInts({
 77		"coinCode": coinCode,
 78		"amount": amount,
 79		"secret": secret,
 80		"nullifier": nullifier,
 81		"oldKey": oldKey,
 82		"oldValue": oldValue,
 83		"siblingsOld": siblingsOld,
 84		"siblingsNew": siblingsNew,
 85		"rootOld": rootOld,
 86		"rootNew": rootNew,
 87		"commitment": commitment,
 88		"key": key
 89	});
 90	console.log("input", input);
 91	// const options = {};
 92	// const wc = await WitnessCalculatorBuilder(wasm, options);
 93
 94	const wc = await WitnessCalculatorBuilder(wasm);
 95
 96	const witness = await wc.calculateWitness(input, {sanityCheck: true});
 97
 98	const wBuff = Buffer.allocUnsafe(witness.length*32);
 99
100	for (let i=0; i<witness.length; i++) {
101		for (let j=0; j<8; j++) {
102			const bi = witness[i];
103			const v = bigInt(bi).shiftRight(j*32).and(0xFFFFFFFF).toJSNumber();
104			// wBuff.writeUInt32LE(v, i*32 + j*4, 4)
105			wBuff.writeUInt32LE(v, i*32 + j*4)
106		}
107	}
108
109
110	// const witness = unstringifyBigInts(stringifyBigInts(w));
111	// return wBuff;
112	return {
113		witness: wBuff,
114		publicInputs: {
115			commitment:commitment,
116			root:rootNew
117		}
118	};
119}
120
121exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr, key) => {
122	const poseidon = circomlib.poseidon.createHash(6, 8, 57);
123	const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
124
125	// rebuild the tree
126	let tree = await smt.newMemEmptyTrie();
127	await tree.insert(0, 0);
128	for (let i=0; i<commitments.length; i++) {
129		await tree.insert(i+1, commitments[i]);
130	}
131	// await tree.insert(commitment, 0);
132
133	// root
134	const root = tree.root;
135	const res = await tree.find(key);
136	if (!res.found) {
137		console.error("leaf expect to exist but not exists, key:", key);
138	}
139	let siblings = res.siblings;
140	while (siblings.length < nLevels) {
141		siblings.push("0");
142	};
143
144	// calculate witness
145	const input = unstringifyBigInts({
146		"coinCode": coinCode,
147		"amount": amount,
148		"secret": secret,
149		"nullifier": nullifier,
150		"siblings": siblings,
151		"root": root,
152		"address": addr,
153		"key": key
154	});
155	console.log("input", input);
156	// const options = {};
157	// const wc = await WitnessCalculatorBuilder(wasm, options);
158
159	const wc = await WitnessCalculatorBuilder(wasm);
160
161	const witness = await wc.calculateWitness(input, {sanityCheck: true});
162
163	const wBuff = Buffer.allocUnsafe(witness.length*32);
164
165	for (let i=0; i<witness.length; i++) {
166		for (let j=0; j<8; j++) {
167			const bi = witness[i];
168			const v = bigInt(bi).shiftRight(j*32).and(0xFFFFFFFF).toJSNumber();
169			// wBuff.writeUInt32LE(v, i*32 + j*4, 4)
170			wBuff.writeUInt32LE(v, i*32 + j*4)
171		}
172	}
173
174
175	// const witness = unstringifyBigInts(stringifyBigInts(w));
176	return {
177		witness: wBuff,
178		publicInputs: {
179			address:addr,
180			nullifier:nullifier
181		}
182	};
183}