From ca9dbb7f987b83deaa5ef207f66b46f339ddd612 Mon Sep 17 00:00:00 2001 From: Koh Wei Jie Date: Wed, 31 Jul 2019 17:22:11 +0800 Subject: [PATCH] added require revert strings to contracts; modified semaphore contract test to check for said strings (#21) upgraded snarkjs commit hash added revert reason test for verifier-gte-snark-scalar-field updated package-lock.json and circleci snark file cache --- .circleci/config.yml | 8 +- semaphorejs/contracts/MerkleTreeLib.sol | 2 +- semaphorejs/contracts/Semaphore.sol | 19 +- semaphorejs/package-lock.json | 485 +++++++++++++++++++++++- semaphorejs/package.json | 2 +- semaphorejs/test/contracts/semaphore.js | 6 + 6 files changed, 512 insertions(+), 10 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6228d75d..d7d46ebb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -19,14 +19,14 @@ jobs: - restore_cache: name: restore-npm-cache keys: - - v1.6-dependencies-{{ checksum "package-lock.json" }} + - v1.7-dependencies-{{ checksum "package-lock.json" }} - run: npm install - save_cache: paths: - node_modules - key: v1.6-dependencies-{{ checksum "package-lock.json" }} + key: v1.7-dependencies-{{ checksum "package-lock.json" }} # checksum the snarks definitions - run: @@ -37,7 +37,7 @@ jobs: - restore_cache: name: restore-snark-cache keys: - - v1.6-dependencies-{{ checksum "build/.snark_checksum" }} + - v1.7-dependencies-{{ checksum "build/.snark_checksum" }} # build snarks - run: @@ -47,7 +47,7 @@ jobs: # cache generated snark circuit and keys - save_cache: - key: v1.6-dependencies-{{ checksum "build/.snark_checksum" }} + key: v1.7-dependencies-{{ checksum "build/.snark_checksum" }} paths: - build/circuit.json - build/proving_key.bin diff --git a/semaphorejs/contracts/MerkleTreeLib.sol b/semaphorejs/contracts/MerkleTreeLib.sol index 04feb57f..7256f467 100644 --- a/semaphorejs/contracts/MerkleTreeLib.sol +++ b/semaphorejs/contracts/MerkleTreeLib.sol @@ -129,7 +129,7 @@ contract MultipleMerkleTree { current_index /= 2; } - require(tree_roots[tree_index] == current_level_hash); + require(tree_roots[tree_index] == current_level_hash, "MultipleMerkleTree: tree root / current level hash mismatch"); current_index = leaf_index; diff --git a/semaphorejs/contracts/Semaphore.sol b/semaphorejs/contracts/Semaphore.sol index 98f93874..ec0388ad 100644 --- a/semaphorejs/contracts/Semaphore.sol +++ b/semaphorejs/contracts/Semaphore.sol @@ -82,6 +82,20 @@ contract Semaphore is Verifier, MultipleMerkleTree, Ownable { verifyProof(a, b, c, input); } + function preBroadcastRequire ( + uint[2] memory a, + uint[2][2] memory b, + uint[2] memory c, + uint[5] memory input, + uint256 signal_hash + ) public { + require(hasNullifier(input[1]) == false, "Semaphore: nullifier already seen"); + require(signal_hash == input[2], "Semaphore: signal hash mismatch"); + require(external_nullifier == input[3], "Semaphore: external nullifier mismatch"); + require(isInRootHistory(input[0]), "Semaphore: root not seen"); + require(verifyProof(a, b, c, input), "Semaphore: invalid proof"); + } + function broadcastSignal( bytes memory signal, uint[2] memory a, @@ -89,14 +103,15 @@ contract Semaphore is Verifier, MultipleMerkleTree, Ownable { uint[2] memory c, uint[5] memory input // (root, nullifiers_hash, signal_hash, external_nullifier, broadcaster_address) ) public { + // Hash the signal uint256 signal_hash = uint256(sha256(signal)) >> 8; // Check the inputs - require(preBroadcastCheck(a, b, c, input, signal_hash) == true); + preBroadcastRequire(a, b, c, input, signal_hash); // Verify the broadcaster's address address broadcaster = address(input[4]); - require(broadcaster == msg.sender); + require(broadcaster == msg.sender, "Semaphore: wrong broadcaster's address"); signals[current_signal_index++] = signal; nullifiers_set[input[1]] = true; diff --git a/semaphorejs/package-lock.json b/semaphorejs/package-lock.json index 39124ef5..6cc0f357 100644 --- a/semaphorejs/package-lock.json +++ b/semaphorejs/package-lock.json @@ -9840,6 +9840,487 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, + "fsevents": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.7.tgz", + "integrity": "sha512-Pxm6sI2MeBD7RdD12RYsqaP0nMiwx8eZBXCa6z2L+mRHm2DYrOYwihmhjpkdjUHwQhslWQjRpEgNq4XvBmaAuw==", + "optional": true, + "requires": { + "nan": "^2.9.2", + "node-pre-gyp": "^0.10.0" + }, + "dependencies": { + "abbrev": { + "version": "1.1.1", + "bundled": true, + "optional": true + }, + "ansi-regex": { + "version": "2.1.1", + "bundled": true, + "optional": true + }, + "aproba": { + "version": "1.2.0", + "bundled": true, + "optional": true + }, + "are-we-there-yet": { + "version": "1.1.5", + "bundled": true, + "optional": true, + "requires": { + "delegates": "^1.0.0", + "readable-stream": "^2.0.6" + } + }, + "balanced-match": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "brace-expansion": { + "version": "1.1.11", + "bundled": true, + "optional": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "chownr": { + "version": "1.1.1", + "bundled": true, + "optional": true + }, + "code-point-at": { + "version": "1.1.0", + "bundled": true, + "optional": true + }, + "concat-map": { + "version": "0.0.1", + "bundled": true, + "optional": true + }, + "console-control-strings": { + "version": "1.1.0", + "bundled": true, + "optional": true + }, + "core-util-is": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "debug": { + "version": "2.6.9", + "bundled": true, + "optional": true, + "requires": { + "ms": "2.0.0" + } + }, + "deep-extend": { + "version": "0.6.0", + "bundled": true, + "optional": true + }, + "delegates": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "detect-libc": { + "version": "1.0.3", + "bundled": true, + "optional": true + }, + "fs-minipass": { + "version": "1.2.5", + "bundled": true, + "optional": true, + "requires": { + "minipass": "^2.2.1" + } + }, + "fs.realpath": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "gauge": { + "version": "2.7.4", + "bundled": true, + "optional": true, + "requires": { + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" + } + }, + "glob": { + "version": "7.1.3", + "bundled": true, + "optional": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "has-unicode": { + "version": "2.0.1", + "bundled": true, + "optional": true + }, + "iconv-lite": { + "version": "0.4.24", + "bundled": true, + "optional": true, + "requires": { + "safer-buffer": ">= 2.1.2 < 3" + } + }, + "ignore-walk": { + "version": "3.0.1", + "bundled": true, + "optional": true, + "requires": { + "minimatch": "^3.0.4" + } + }, + "inflight": { + "version": "1.0.6", + "bundled": true, + "optional": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.3", + "bundled": true, + "optional": true + }, + "ini": { + "version": "1.3.5", + "bundled": true, + "optional": true + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "bundled": true, + "optional": true, + "requires": { + "number-is-nan": "^1.0.0" + } + }, + "isarray": { + "version": "1.0.0", + "bundled": true, + "optional": true + }, + "minimatch": { + "version": "3.0.4", + "bundled": true, + "optional": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "minimist": { + "version": "0.0.8", + "bundled": true, + "optional": true + }, + "minipass": { + "version": "2.3.5", + "bundled": true, + "optional": true, + "requires": { + "safe-buffer": "^5.1.2", + "yallist": "^3.0.0" + } + }, + "minizlib": { + "version": "1.2.1", + "bundled": true, + "optional": true, + "requires": { + "minipass": "^2.2.1" + } + }, + "mkdirp": { + "version": "0.5.1", + "bundled": true, + "optional": true, + "requires": { + "minimist": "0.0.8" + } + }, + "ms": { + "version": "2.0.0", + "bundled": true, + "optional": true + }, + "needle": { + "version": "2.2.4", + "bundled": true, + "optional": true, + "requires": { + "debug": "^2.1.2", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" + } + }, + "node-pre-gyp": { + "version": "0.10.3", + "bundled": true, + "optional": true, + "requires": { + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.1", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.2.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4" + } + }, + "nopt": { + "version": "4.0.1", + "bundled": true, + "optional": true, + "requires": { + "abbrev": "1", + "osenv": "^0.1.4" + } + }, + "npm-bundled": { + "version": "1.0.5", + "bundled": true, + "optional": true + }, + "npm-packlist": { + "version": "1.2.0", + "bundled": true, + "optional": true, + "requires": { + "ignore-walk": "^3.0.1", + "npm-bundled": "^1.0.1" + } + }, + "npmlog": { + "version": "4.1.2", + "bundled": true, + "optional": true, + "requires": { + "are-we-there-yet": "~1.1.2", + "console-control-strings": "~1.1.0", + "gauge": "~2.7.3", + "set-blocking": "~2.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "bundled": true, + "optional": true + }, + "object-assign": { + "version": "4.1.1", + "bundled": true, + "optional": true + }, + "once": { + "version": "1.4.0", + "bundled": true, + "optional": true, + "requires": { + "wrappy": "1" + } + }, + "os-homedir": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "os-tmpdir": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "osenv": { + "version": "0.1.5", + "bundled": true, + "optional": true, + "requires": { + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "bundled": true, + "optional": true + }, + "process-nextick-args": { + "version": "2.0.0", + "bundled": true, + "optional": true + }, + "rc": { + "version": "1.2.8", + "bundled": true, + "optional": true, + "requires": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "bundled": true, + "optional": true + } + } + }, + "readable-stream": { + "version": "2.3.6", + "bundled": true, + "optional": true, + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "rimraf": { + "version": "2.6.3", + "bundled": true, + "optional": true, + "requires": { + "glob": "^7.1.3" + } + }, + "safe-buffer": { + "version": "5.1.2", + "bundled": true, + "optional": true + }, + "safer-buffer": { + "version": "2.1.2", + "bundled": true, + "optional": true + }, + "sax": { + "version": "1.2.4", + "bundled": true, + "optional": true + }, + "semver": { + "version": "5.6.0", + "bundled": true, + "optional": true + }, + "set-blocking": { + "version": "2.0.0", + "bundled": true, + "optional": true + }, + "signal-exit": { + "version": "3.0.2", + "bundled": true, + "optional": true + }, + "string-width": { + "version": "1.0.2", + "bundled": true, + "optional": true, + "requires": { + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" + } + }, + "string_decoder": { + "version": "1.1.1", + "bundled": true, + "optional": true, + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "strip-ansi": { + "version": "3.0.1", + "bundled": true, + "optional": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "strip-json-comments": { + "version": "2.0.1", + "bundled": true, + "optional": true + }, + "tar": { + "version": "4.4.8", + "bundled": true, + "optional": true, + "requires": { + "chownr": "^1.1.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.3.4", + "minizlib": "^1.1.1", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.2", + "yallist": "^3.0.2" + } + }, + "util-deprecate": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "wide-align": { + "version": "1.1.3", + "bundled": true, + "optional": true, + "requires": { + "string-width": "^1.0.2 || 2" + } + }, + "wrappy": { + "version": "1.0.2", + "bundled": true, + "optional": true + }, + "yallist": { + "version": "3.0.3", + "bundled": true, + "optional": true + } + } + }, "fstream": { "version": "1.0.12", "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.12.tgz", @@ -20356,8 +20837,8 @@ } }, "snarkjs": { - "version": "git+https://github.com/iden3/snarkjs.git#f8ba7cbfa7d526e186fd3a706be00f63b65812d8", - "from": "git+https://github.com/iden3/snarkjs.git#f8ba7cbfa7d526e186fd3a706be00f63b65812d8", + "version": "git+https://github.com/iden3/snarkjs.git#c428706ef69930e378c31199ff8d66ee13fada85", + "from": "git+https://github.com/iden3/snarkjs.git#c428706ef69930e378c31199ff8d66ee13fada85", "requires": { "big-integer": "^1.6.43", "chai": "^4.2.0", diff --git a/semaphorejs/package.json b/semaphorejs/package.json index 82d9e06a..cfc5d7a5 100644 --- a/semaphorejs/package.json +++ b/semaphorejs/package.json @@ -33,7 +33,7 @@ "node-fetch": "^2.3.0", "require-nocache": "^1.0.0", "semaphore-merkle-tree": "^1.0.4", - "snarkjs": "git+https://github.com/iden3/snarkjs.git#f8ba7cbfa7d526e186fd3a706be00f63b65812d8", + "snarkjs": "git+https://github.com/iden3/snarkjs.git#c428706ef69930e378c31199ff8d66ee13fada85", "truffle": "^5.0.10", "truffle-artifactor": "^4.0.10", "truffle-contract": "^4.0.11", diff --git a/semaphorejs/test/contracts/semaphore.js b/semaphorejs/test/contracts/semaphore.js index 50271ad2..185004d0 100644 --- a/semaphorejs/test/contracts/semaphore.js +++ b/semaphorejs/test/contracts/semaphore.js @@ -190,6 +190,8 @@ contract('Semaphore', function (accounts) { const publicSignals = w.slice(1, circuit.nPubInputs + circuit.nOutputs+1); const proof = await proof_util.prove(witness_bin.buffer, vk_proof.buffer); let failed = false; + let reason = ''; + try { await semaphore.broadcastSignal( signal_to_contract, @@ -200,8 +202,10 @@ contract('Semaphore', function (accounts) { ); } catch(e) { failed = true; + reason = e.reason } assert.equal(failed, true); + assert.equal(reason, 'Semaphore: root not seen'); failed = false; try { @@ -214,8 +218,10 @@ contract('Semaphore', function (accounts) { ); } catch(e) { failed = true; + reason = e.reason } assert.equal(failed, true); + assert.equal(reason, 'verifier-gte-snark-scalar-field'); const a = [ proof.pi_a[0].toString(), proof.pi_a[1].toString() ] const b = [ [ proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString() ], [ proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString() ] ]