Browse Source

Update src/miksi.ts to last changes for browser usage

pull/2/head
arnaucube 4 years ago
parent
commit
6a513ded87
5 changed files with 68 additions and 58 deletions
  1. +11
    -11
      contracts/deposit-verifier.sol
  2. +10
    -10
      contracts/withdraw-verifier.sol
  3. +25
    -19
      dist/miksi-browser.js
  4. +21
    -17
      src/miksi.ts
  5. +1
    -1
      test/miksi.test.ts

+ 11
- 11
contracts/deposit-verifier.sol

@ -174,18 +174,18 @@ contract DepositVerifier {
DepositPairing.G1Point C;
}
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
vk.alfa1 = DepositPairing.G1Point(18695173155707885922021631283033755535591418672197129614467100440698872763407,1626506100460323286733991395349100178389097370543005429942370444444167817065);
vk.beta2 = DepositPairing.G2Point([7298930152319519678724671205658347486392659569051487272914904218878630422004,20278804400136230600016602729050494456082496665552956593575216136952301465879], [14400234736726653666673256209185515676662403987927981307851255786137918403808,202061819848056105351629960337366504214917264758439266970901420364923813257]);
vk.gamma2 = DepositPairing.G2Point([88902560478033838314004667971746300540170455168011513957934457035035362077,10509682790371697523911222284395334237969554461187168055951064284438196928133], [262372519755819494139123390519431075073514105365767199648231997477378997038,9903995889180920976180540681258851480387134172307619545103102622070424881278]);
vk.delta2 = DepositPairing.G2Point([19749667459894634917871046338432887676262288965549093528649050450724219163245,13756931459128709749559820929305092888983958863551084787825233693015050987062], [20576516057444847821805169435613033091289908065519785381540290141111162328929,15244981401483924012411101359641406633223142876027993598824472316124280834070]);
vk.alfa1 = DepositPairing.G1Point(5430763209380891082112219774590965073812438071785553545512843412766074378589,8483294202769323983386844586172367254189941367220716956010972527412596130486);
vk.beta2 = DepositPairing.G2Point([4461224104052879207048084476396712261168351663641098793015529057949505506609,9544040136419068455629331025948917346266663192627992632868522840394806969434], [16229298110477119094923221927578042298710597226211404598826305561649600112085,3174991076926426123044998896038938058491203360109109874700070586926990826975]);
vk.gamma2 = DepositPairing.G2Point([1763786592992476076824494282837308182336334498741564210939328007472877590979,16095557635581589565592692999561395560887484535347243882788725384363056509081], [20099281874123119981340699313574751066255843720218059754888786799205480145816,4128178975141199585947537534509943631531896570605638201475406072073346159927]);
vk.delta2 = DepositPairing.G2Point([1112445599982071272946700233620225941838681071449287021880935553274281891149,21852125798450609179424969354919823595751481354419855759397333465513526523951], [4650598210775335626309400977090729715816375400747238833715263844489768409735,9506265809207721234821155999410463212069459307612220343268944197887451805661]);
vk.IC = new DepositPairing.G1Point[](7);
vk.IC[0] = DepositPairing.G1Point(2000117758538561750953181437087792909020385452287399464015848728440127592389,275875173722294745606860506515174747141273674463415300932791922897441449811);
vk.IC[1] = DepositPairing.G1Point(4176102879547484253346821261893297661469569413245667250587796973056187219608,1926060395020061418051275655183984440402034736270275535768335121260270447114);
vk.IC[2] = DepositPairing.G1Point(8176255402316361002530601013228562072839156358144426319604813511766758290537,19255649988335327919495695859359269216762287298096174272638221716012315581350);
vk.IC[3] = DepositPairing.G1Point(20944119614073443108911839024077113785116860484550901273175814017528952294710,15941356189224733186425469351312052553223296343229173777927394845664358416828);
vk.IC[4] = DepositPairing.G1Point(16474742653420689694601265238954907093607463298284817604813422283427863653617,9311356062394012318240362122996250765056867942021818814568734196480690247200);
vk.IC[5] = DepositPairing.G1Point(12016269570988993052956789433371861013003194299504564318255744188658097042633,18130086499603245000319550360003638349858422145937471155520725380017515910661);
vk.IC[6] = DepositPairing.G1Point(14992989709522098533999247134336293504761296073753622771564136461571770746835,8331491724814987424742621582227793410739471354578262538342699991611045759814);
vk.IC[0] = DepositPairing.G1Point(10918150886880635497175603837243938291795608025578156706199987503133910943501,3820501207457478037242281915371943430490491637056890884843289615136736661276);
vk.IC[1] = DepositPairing.G1Point(14765455611714785904910505920382976888742011688321426790246638636748913644575,1970327576944768331068346214501841357599829193384396384398512475234228192550);
vk.IC[2] = DepositPairing.G1Point(4575326419654501917969297303051407724371861645581023746738038587958659360147,14512901306272734695398492530853628160305148151587715082363147054390984379306);
vk.IC[3] = DepositPairing.G1Point(10149717886730624284680109616859329799115550276739239226023596802725094047583,16806116621949942919292056712799700002885850181824427798105720780153968785723);
vk.IC[4] = DepositPairing.G1Point(9663169403303410535343713305536179107413370694230955213715276420971109292352,19907010787536566810941780928736434067835793051624423083300240671493191288122);
vk.IC[5] = DepositPairing.G1Point(21069023849778352967265174506861061426513291386845863979546966635979962598349,12327162753516489818246989808695117002665024166967891733423875235072870943654);
vk.IC[6] = DepositPairing.G1Point(21288113989150604365711743161787017824926800547410591057826128522328249297383,13847158925758944149197960029039819643318140529826181314997576920991438276946);
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {

+ 10
- 10
contracts/withdraw-verifier.sol

@ -174,17 +174,17 @@ contract WithdrawVerifier {
WithdrawPairing.G1Point C;
}
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
vk.alfa1 = WithdrawPairing.G1Point(10461708735553443176992732459814962577655306635753492245435095461770754609206,17170070190117362250052226704107651880175572987936980166979072370846071276566);
vk.beta2 = WithdrawPairing.G2Point([11084164661947067228695984420449497565302107485274732922694018877422637260948,14768196651755007408254022498360029528155122429952581228808458847305698486036], [21804606851539124842853838093984466542476538467493989613050132455132951994121,1861720569920484390282049349437713344652903656767554167309156051148865987563]);
vk.gamma2 = WithdrawPairing.G2Point([9706265884453026747036821224300582108519975058422630973848693287593728789837,5514760811545236654037526142877016941373586932537660711868264285004769174888], [14491809303075760609694333332967923981527717776009730567611199650481234125548,12129581260877191822815335682707682434919070096035795484939038493639252524617]);
vk.delta2 = WithdrawPairing.G2Point([16890371933892859364017140187474707868747433731104663205146192533825447514505,21399578739239166683287431604946765321077367409391376717538684282829150225976], [8842639902446623294657089461047982654757172648192937200077646586006331774863,18487165336340965321051036729791768485990613323135528024606788207720211920031]);
vk.alfa1 = WithdrawPairing.G1Point(9956296300690915517874573266154730823221717995215044773324446346162206338992,7255108893541773754529467661076065797486871459164430883084263625121383493568);
vk.beta2 = WithdrawPairing.G2Point([11432672290143715760902579593554637607711856055492856995822332295041301405578,8218828277777042656004498085159220245107199937796005735369331662550836876806], [16363095385614596840077035938593366108864385030444089944032706894707469163509,13118923997328413164477778688868351714210478255695938220137775572511411988900]);
vk.gamma2 = WithdrawPairing.G2Point([6236992641099484591564067220826955511131844106515721496050854402204141122429,19283952549567804533802144868866435128321985987157216867931054245310949679801], [14538630447155596022944429924084950422919358940152142055089384780170329449454,15234331517470019539689603986931890083667647220909628764851302053323116779217]);
vk.delta2 = WithdrawPairing.G2Point([2436545700082826579202863736150815048319840191437072550486817097747625822904,17710485880976382338414619957893783940205882086507522693066355619700507711598], [14266467939815332557299268812959584466785737678641401251537458773418055441300,6807933200389968943957508589084714071952465255570240831171630497718395343844]);
vk.IC = new WithdrawPairing.G1Point[](6);
vk.IC[0] = WithdrawPairing.G1Point(10682120661229048274949364949968842843395989293050982446578990269399782749953,1990964813812662139462300927859607978511478023403957963432885533764658033890);
vk.IC[1] = WithdrawPairing.G1Point(19858805194844516511519307221183024963657395449667106116942848839464106278308,14007050627405719561074645119613087016962000876415739087812187315207916912886);
vk.IC[2] = WithdrawPairing.G1Point(21012689485487519157247991544697647278544236668403782222712353518350961502559,2452121773666466264963717054681755250351950870974950586177089269810623349820);
vk.IC[3] = WithdrawPairing.G1Point(21777835995512453624376343196859713839817688740670494291073772535091042372431,14230131375167768135013466989061198415649711922017749764614481281052630561040);
vk.IC[4] = WithdrawPairing.G1Point(18224309327975275226135927148877460666894066703361312842594398634528579211246,1805137703259464346304717616574612959020983663951806398290027184756994095101);
vk.IC[5] = WithdrawPairing.G1Point(6680422453531132615016449106377746942927034063013504561878935989154670779439,21031511412360362734553233505420357520515690946654736114154923572533964087548);
vk.IC[0] = WithdrawPairing.G1Point(5727939748071835925233666161969432891183030973897843712182471731522640231419,9306839550225504077794511364643952078913298926154035864376514891516707221363);
vk.IC[1] = WithdrawPairing.G1Point(14858203630304801060424827556832267363920729004328211724073257903263494204386,17053286709485422467987496430526114942449253247173021832415394085651252346538);
vk.IC[2] = WithdrawPairing.G1Point(3630464855043668264120602964350694408193307187754886290043709066665836003106,17217015154710940324243151792697651355400365331608661363498939316583119794106);
vk.IC[3] = WithdrawPairing.G1Point(21402637221507761926456097666003799621345236896038353478646641075093395950090,19974148642855666292937519561398036482565655993548932679150207292614120691822);
vk.IC[4] = WithdrawPairing.G1Point(5396502263005748320802553347172416122012071478345448681456826263886252909209,18477840556213682338141497798999631662180749751931174226517158339379873350890);
vk.IC[5] = WithdrawPairing.G1Point(12440750536174971437855964040998743226589802455604053790245538951692229112149,12605904038609683211076647326560338322715812726968936919863510351596025238862);
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {

+ 25
- 19
dist/miksi-browser.js

@ -61,7 +61,7 @@ module.exports={
"_args": [
[
"@web3-js/websocket@1.0.30",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "@web3-js/websocket@1.0.30",
@ -86,7 +86,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/@web3-js/websocket/-/websocket-1.0.30.tgz",
"_spec": "1.0.30",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "Brian McKelvey",
"email": "theturtle32@gmail.com",
@ -27405,7 +27405,7 @@ module.exports={
"_args": [
[
"elliptic@6.5.2",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "elliptic@6.5.2",
@ -27434,7 +27434,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/elliptic/-/elliptic-6.5.2.tgz",
"_spec": "6.5.2",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "Fedor Indutny",
"email": "fedor@indutny.com"
@ -75059,7 +75059,7 @@ module.exports={
"_args": [
[
"web3@1.2.7",
"/home/user/git/miksi/miksi-core"
"/home/nicebox/miksi/miksi-core"
]
],
"_from": "web3@1.2.7",
@ -75084,7 +75084,7 @@ module.exports={
],
"_resolved": "https://registry.npmjs.org/web3/-/web3-1.2.7.tgz",
"_spec": "1.2.7",
"_where": "/home/user/git/miksi/miksi-core",
"_where": "/home/nicebox/miksi/miksi-core",
"author": {
"name": "ethereum.org"
},
@ -76310,25 +76310,28 @@ exports.calcCommitment = (secret, nullifier) => {
return commitment;
};
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments, key) => {
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);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// old root
const rootOld = tree.root;
const resOld = await tree.find(commitment);
let oldKey = "0";
let oldValue = "0";
if (!resOld.found) {
oldKey = resOld.notFoundKey.toString();
oldValue = resOld.notFoundValue.toString();
}
console.log("oldKey", oldKey);
console.log("oldValue", oldValue);
// if (resOld.found) {
// console.error("leaf expect to not exist but exists");
// }
@ -76337,13 +76340,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
siblingsOld.push("0");
};
await tree.insert(commitment, 0);
await tree.insert(key, commitment);
// new root
const rootNew = tree.root;
const resNew = await tree.find(commitment);
const resNew = await tree.find(key);
if (!resNew.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf with the new commitment expect to exist but not exists");
}
let siblingsNew = resNew.siblings;
while (siblingsNew.length < nLevels) {
@ -76357,11 +76360,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
"secret": secret,
"nullifier": nullifier,
"oldKey": oldKey,
"oldValue": oldValue,
"siblingsOld": siblingsOld,
"siblingsNew": siblingsNew,
"rootOld": rootOld,
"rootNew": rootNew,
"commitment": commitment
"commitment": commitment,
"key": key
});
console.log("input", input);
// const options = {};
@ -76394,23 +76399,23 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
};
}
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr) => {
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr, key) => {
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);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// await tree.insert(commitment, 0);
// root
const root = tree.root;
const res = await tree.find(commitment);
const res = await tree.find(key);
if (!res.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf expect to exist but not exists, key:", key);
}
let siblings = res.siblings;
while (siblings.length < nLevels) {
@ -76425,7 +76430,8 @@ exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr)
"nullifier": nullifier,
"siblings": siblings,
"root": root,
"address": addr
"address": addr,
"key": key
});
console.log("input", input);
// const options = {};

+ 21
- 17
src/miksi.ts

@ -25,25 +25,28 @@ exports.calcCommitment = (secret, nullifier) => {
return commitment;
};
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
exports.calcDepositWitness = async (wasm, secret, nullifier, commitments, key) => {
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);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// old root
const rootOld = tree.root;
const resOld = await tree.find(commitment);
let oldKey = "0";
let oldValue = "0";
if (!resOld.found) {
oldKey = resOld.notFoundKey.toString();
oldValue = resOld.notFoundValue.toString();
}
console.log("oldKey", oldKey);
console.log("oldValue", oldValue);
// if (resOld.found) {
// console.error("leaf expect to not exist but exists");
// }
@ -52,13 +55,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
siblingsOld.push("0");
};
await tree.insert(commitment, 0);
await tree.insert(key, commitment);
// new root
const rootNew = tree.root;
const resNew = await tree.find(commitment);
const resNew = await tree.find(key);
if (!resNew.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf with the new commitment expect to exist but not exists");
}
let siblingsNew = resNew.siblings;
while (siblingsNew.length < nLevels) {
@ -72,11 +75,13 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
"secret": secret,
"nullifier": nullifier,
"oldKey": oldKey,
"oldValue": oldValue,
"siblingsOld": siblingsOld,
"siblingsNew": siblingsNew,
"rootOld": rootOld,
"rootNew": rootNew,
"commitment": commitment
"commitment": commitment,
"key": key
});
console.log("input", input);
// const options = {};
@ -109,23 +114,23 @@ exports.calcDepositWitness = async (wasm, secret, nullifier, commitments) => {
};
}
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr) => {
exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr, key) => {
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);
await tree.insert(0, 0);
for (let i=0; i<commitments.length; i++) {
await tree.insert(commitments[i], 0);
await tree.insert(i+1, commitments[i]);
}
// await tree.insert(commitment, 0);
// root
const root = tree.root;
const res = await tree.find(commitment);
const res = await tree.find(key);
if (!res.found) {
console.error("leaf expect to exist but not exists");
console.error("leaf expect to exist but not exists, key:", key);
}
let siblings = res.siblings;
while (siblings.length < nLevels) {
@ -140,7 +145,8 @@ exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr)
"nullifier": nullifier,
"siblings": siblings,
"root": root,
"address": addr
"address": addr,
"key": key
});
console.log("input", input);
// const options = {};
@ -156,7 +162,8 @@ exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr)
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, 4)
wBuff.writeUInt32LE(v, i*32 + j*4)
}
}
@ -170,6 +177,3 @@ exports.calcWithdrawWitness = async (wasm, secret, nullifier, commitments, addr)
}
};
}

+ 1
- 1
test/miksi.test.ts

@ -15,7 +15,7 @@ describe("deposit test", function () {
const wasm = await fs.promises.readFile("./build/deposit.wasm");
console.log("w", wasm.length);
const witness = await miksi.calcDepositWitness(wasm, secret, nullifier, commitments);
const witness = await miksi.calcDepositWitness(wasm, secret, nullifier, commitments, 1);
// console.log("w", witness);

Loading…
Cancel
Save