From 76543d068bb04d3da4224c58d519174b7714a981 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 4 Mar 2021 13:00:14 +0300 Subject: [PATCH] update snarkjs usage --- contracts/mocks/Pack.sol | 26 +++++++++++++------------- package.json | 3 ++- src/index.js | 9 ++++++--- yarn.lock | 14 ++++++++++++++ 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/contracts/mocks/Pack.sol b/contracts/mocks/Pack.sol index 7c8a4f7..1c3bce0 100644 --- a/contracts/mocks/Pack.sol +++ b/contracts/mocks/Pack.sol @@ -24,11 +24,11 @@ contract Pack { uint256 gasBefore = gasleft(); bytes memory data = new bytes(BYTES_SIZE); for (uint256 i = 0; i < CHUNK_SIZE; i++) { - (bytes32 hash, address instance, uint32 block) = (hashes[i], instances[i], blocks[i]); + (bytes32 _hash, address _instance, uint32 _block) = (hashes[i], instances[i], blocks[i]); assembly { - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), block) - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), instance) - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), hash) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), _block) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), _instance) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), _hash) } } uint256 gasHash = gasleft(); @@ -52,23 +52,23 @@ contract Pack { public view returns ( - uint256 gas1, - uint256 gas2, - bytes32 hash + uint256, + uint256, + bytes32 ) { uint256 gasBefore = gasleft(); bytes memory data = new bytes(BYTES_SIZE); for (uint256 i = 0; i < CHUNK_SIZE; i++) { - (bytes32 hash, address instance, uint32 block) = (hashes[i], instances[i], blocks[i]); + (bytes32 _hash, address _instance, uint32 _block) = (hashes[i], instances[i], blocks[i]); assembly { - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), block) - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), instance) - mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), hash) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x38), _block) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x34), _instance) + mstore(add(add(data, mul(ITEM_SIZE, i)), 0x20), _hash) } } uint256 gasHash = gasleft(); - bytes32 hash = sha256(data); - return (gasleft() - gasHash, gasHash - gasBefore, hash); + bytes32 hash1 = sha256(data); + return (gasleft() - gasHash, gasHash - gasBefore, hash1); } } diff --git a/package.json b/package.json index e6bbc4c..3f4c2e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tornado-trees", - "version": "0.0.7", + "version": "0.0.8", "main": "src/index.js", "repository": "https://github.com/tornadocash/tornado-trees.git", "author": "Tornadocash team ", @@ -46,6 +46,7 @@ "circomlib": "git+https://github.com/tornadocash/circomlib.git#d20d53411d1bef61f38c99a8b36d5d0cc4836aa1", "dotenv": "^8.2.0", "ffiasm": "^0.1.1", + "ffjavascript": "^0.2.35", "fixed-merkle-tree": "^0.5.0", "jssha": "^3.2.0", "snarkjs": "^0.3.57", diff --git a/src/index.js b/src/index.js index 8fc6c9b..bc30643 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,7 @@ const ethers = require('ethers') const BigNumber = ethers.BigNumber +const { wtns } = require('snarkjs') +const { utils } = require('ffjavascript') const { bitsToNumber, toBuffer, toFixedHex, poseidonHash } = require('./utils') @@ -32,16 +34,17 @@ function hashInputs(input) { function prove(input, keyBasePath) { return tmp.dir().then(async (dir) => { dir = dir.path - fs.writeFileSync(`${dir}/input.json`, JSON.stringify(input, null, 2)) let out try { if (fs.existsSync(`${keyBasePath}`)) { // native witness calc + fs.writeFileSync(`${dir}/input.json`, JSON.stringify(input, null, 2)) out = await exec(`${keyBasePath} ${dir}/input.json ${dir}/witness.json`) } else { - out = await exec(`npx snarkjs wd ${keyBasePath}.wasm ${dir}/input.json ${dir}/witness.wtns`) - out = await exec(`npx snarkjs wej ${dir}/witness.wtns ${dir}/witness.json`) + await wtns.calculate(utils.unstringifyBigInts(input), `${keyBasePath}.wasm`, `${dir}/witness.wtns`) + const witness = utils.stringifyBigInts(await wtns.exportJson(`${dir}/witness.wtns`)) + fs.writeFileSync(`${dir}/witness.json`, JSON.stringify(witness, null, 2)) } out = await exec( `zkutil prove -c ${keyBasePath}.r1cs -p ${keyBasePath}.params -w ${dir}/witness.json -r ${dir}/proof.json -o ${dir}/public.json`, diff --git a/yarn.lock b/yarn.lock index 52f4ae4..e1d0373 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3890,6 +3890,15 @@ ffjavascript@0.2.34, ffjavascript@^0.2.30: wasmcurves "0.0.14" worker-threads "^1.0.0" +ffjavascript@^0.2.35: + version "0.2.35" + resolved "https://registry.yarnpkg.com/ffjavascript/-/ffjavascript-0.2.35.tgz#9166d95173b1c0a743b455bb03a72b581922a42e" + integrity sha512-xnC51tWbi0ah4SH+02jEfJyO+P+NiZWnxQrLDLtBYY1Dv3QM5ydxzd+gxnLEfWdT8i1bMM5pIh5P25l6fNCaVQ== + dependencies: + big-integer "^1.6.48" + wasmcurves "0.0.14" + web-worker "^1.0.0" + ffwasm@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/ffwasm/-/ffwasm-0.0.7.tgz#23bb9a3537ecc87c0f24fcfb3a9ddd0e86855fff" @@ -8352,6 +8361,11 @@ wasmcurves@0.0.5: big-integer "^1.6.42" blakejs "^1.1.0" +web-worker@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/web-worker/-/web-worker-1.0.0.tgz#c7ced4e1eb6227636ada35056a9e5a477414e4d0" + integrity sha512-BzuMqeKVkKKwHV6tJuwePFcxYMxvC97D448mXTgh/CxXAB4sRtoV26gRPN+JDxsXRR7QZyioMV9O6NzQaASf7Q== + web3-bzz@1.2.11: version "1.2.11" resolved "https://registry.yarnpkg.com/web3-bzz/-/web3-bzz-1.2.11.tgz#41bc19a77444bd5365744596d778b811880f707f"