Add miksi js lib for build witness calculation

This commit is contained in:
arnaucube
2020-05-13 21:08:47 +02:00
parent ee84a28c17
commit c3b5165006
9 changed files with 77535 additions and 6 deletions

76368
dist/miksi-browser.js vendored Normal file

File diff suppressed because one or more lines are too long

11
dist/miksi.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
declare const fs: any;
declare const groth: any;
declare const stringifyBigInts: any, unstringifyBigInts: any;
declare const WitnessCalculatorBuilder: any;
declare const circomlib: any;
declare const smt: any;
declare const Web3: any;
declare const nLevels = 5;
declare const coinCode = "0";
declare const ethAmount = "1";
declare const amount: any;

67
dist/miksi.js vendored Normal file
View File

@@ -0,0 +1,67 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const fs = require("fs");
const { groth } = require('snarkjs');
const { stringifyBigInts, unstringifyBigInts } = require('ffjavascript').utils;
const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
const circomlib = require("circomlib");
const smt = require("circomlib").smt;
const Web3 = require("web3");
const nLevels = 5;
const coinCode = "0"; // refearing to ETH
const ethAmount = '1';
const amount = Web3.utils.toWei(ethAmount, 'ether');
exports.calcWitness = (wasm, secret, nullifier, commitments) => __awaiter(this, void 0, void 0, function* () {
const poseidon = circomlib.poseidon.createHash(6, 8, 57);
const commitment = poseidon([coinCode, amount, secret, nullifier]).toString();
let tree = yield smt.newMemEmptyTrie();
yield tree.insert(1, 0);
// old root
const rootOld = tree.root;
const resOld = yield tree.find(commitment);
if (resOld.found) {
console.error("leaf expect to not exist but exists");
}
let siblingsOld = resOld.siblings;
while (siblingsOld.length < nLevels) {
siblingsOld.push("0");
}
;
yield tree.insert(commitment, 0);
// new root
const rootNew = tree.root;
const resNew = yield tree.find(commitment);
if (!resNew.found) {
console.error("leaf expect to exist but not exists");
}
let siblingsNew = resNew.siblings;
while (siblingsNew.length < nLevels) {
siblingsNew.push("0");
}
;
// calculate witness
const input = unstringifyBigInts({
"coinCode": coinCode,
"amount": amount,
"secret": secret,
"nullifier": nullifier,
"siblingsOld": siblingsOld,
"siblingsNew": siblingsNew,
"rootOld": rootOld,
"rootNew": rootNew,
"commitment": commitment
});
const options = {};
const wc = yield WitnessCalculatorBuilder(wasm, options);
const w = yield wc.calculateWitness(input);
const witness = unstringifyBigInts(stringifyBigInts(w));
return witness;
});
//# sourceMappingURL=miksi.js.map

1
dist/miksi.js.map vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"miksi.js","sourceRoot":"","sources":["../src/miksi.ts"],"names":[],"mappings":";;;;;;;;;AAAA,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AACzB,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;AACrC,MAAM,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC;AAC/E,MAAM,wBAAwB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,wBAAwB,CAAC;AACpF,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;AACvC,MAAM,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,GAAG,CAAC;AACrC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAG7B,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,QAAQ,GAAG,GAAG,CAAC,CAAC,mBAAmB;AACzC,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAA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