|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.4; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/utils/Strings.sol"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @notice This library is needed to simplify the interaction with autogenerated contracts that use [snarkjs](https://www.npmjs.com/package/snarkjs) to verify ZK proofs |
| 8 | + * The main problem with these contracts is that the verification function always has the same signature, except for one parameter. |
| 9 | + * The `input` parameter is a static array `uint256`, the size of which depends on the number of public outputs of ZK proof, |
| 10 | + * therefore the signatures of the verification functions may be different for different schemes. |
| 11 | + * |
| 12 | + * With this library there is no need to create many different interfaces for each circuit. |
| 13 | + * Also, the library functions accept dynamic arrays of public signals, so you don't need to convert them manually to static ones. |
| 14 | + */ |
| 15 | +library VerifierHelper { |
| 16 | + using Strings for uint256; |
| 17 | + |
| 18 | + struct ProofPoints { |
| 19 | + uint256[2] a; |
| 20 | + uint256[2][2] b; |
| 21 | + uint256[2] c; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @notice Function to call the `verifyProof` function on the `verifier` contract. |
| 26 | + * The ZK proof points are wrapped in a structure for convenience |
| 27 | + * @param verifier_ the address of the autogenerated `Verifier` contract |
| 28 | + * @param pubSignals_ the array of the ZK proof public signals |
| 29 | + * @param proofPoints_ the ProofPoints struct with ZK proof points |
| 30 | + * @return true if the proof is valid, false - otherwise |
| 31 | + */ |
| 32 | + function verifyProof( |
| 33 | + address verifier_, |
| 34 | + uint256[] memory pubSignals_, |
| 35 | + ProofPoints memory proofPoints_ |
| 36 | + ) internal view returns (bool) { |
| 37 | + return |
| 38 | + _verifyProof( |
| 39 | + verifier_, |
| 40 | + proofPoints_.a, |
| 41 | + proofPoints_.b, |
| 42 | + proofPoints_.c, |
| 43 | + pubSignals_, |
| 44 | + pubSignals_.length |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @notice Function to call the `verifyProof` function on the `verifier` contract |
| 50 | + * @param verifier_ the address of the autogenerated `Verifier` contract |
| 51 | + * @param pubSignals_ the array of the ZK proof public signals |
| 52 | + * @param a_ the A point of the ZK proof |
| 53 | + * @param b_ the B point of the ZK proof |
| 54 | + * @param c_ the C point of the ZK proof |
| 55 | + * @return true if the proof is valid, false - otherwise |
| 56 | + */ |
| 57 | + function verifyProof( |
| 58 | + address verifier_, |
| 59 | + uint256[] memory pubSignals_, |
| 60 | + uint256[2] memory a_, |
| 61 | + uint256[2][2] memory b_, |
| 62 | + uint256[2] memory c_ |
| 63 | + ) internal view returns (bool) { |
| 64 | + return _verifyProof(verifier_, a_, b_, c_, pubSignals_, pubSignals_.length); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @notice Function to call the `verifyProof` function on the `verifier` contract. |
| 69 | + * The ZK proof points are wrapped in a structure for convenience |
| 70 | + * The length of the `pubSignals_` arr must be strictly equal to `pubSignalsCount_` |
| 71 | + * @param verifier_ the address of the autogenerated `Verifier` contract |
| 72 | + * @param pubSignals_ the array of the ZK proof public signals |
| 73 | + * @param proofPoints_ the ProofPoints struct with ZK proof points |
| 74 | + * @param pubSignalsCount_ the number of public signals |
| 75 | + * @return true if the proof is valid, false - otherwise |
| 76 | + */ |
| 77 | + function verifyProofSafe( |
| 78 | + address verifier_, |
| 79 | + uint256[] memory pubSignals_, |
| 80 | + ProofPoints memory proofPoints_, |
| 81 | + uint256 pubSignalsCount_ |
| 82 | + ) internal view returns (bool) { |
| 83 | + require( |
| 84 | + pubSignals_.length == pubSignalsCount_, |
| 85 | + "VerifierHelper: invalid public signals count" |
| 86 | + ); |
| 87 | + |
| 88 | + return |
| 89 | + _verifyProof( |
| 90 | + verifier_, |
| 91 | + proofPoints_.a, |
| 92 | + proofPoints_.b, |
| 93 | + proofPoints_.c, |
| 94 | + pubSignals_, |
| 95 | + pubSignalsCount_ |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @notice Function to call the `verifyProof` function on the `verifier` contract |
| 101 | + * The length of the `pubSignals_` arr must be strictly equal to `pubSignalsCount_` |
| 102 | + * @param verifier_ the address of the autogenerated `Verifier` contract |
| 103 | + * @param pubSignals_ the array of the ZK proof public signals |
| 104 | + * @param a_ the A point of the ZK proof |
| 105 | + * @param b_ the B point of the ZK proof |
| 106 | + * @param c_ the C point of the ZK proof |
| 107 | + * @param pubSignalsCount_ the number of public signals |
| 108 | + * @return true if the proof is valid, false - otherwise |
| 109 | + */ |
| 110 | + function verifyProofSafe( |
| 111 | + address verifier_, |
| 112 | + uint256[] memory pubSignals_, |
| 113 | + uint256[2] memory a_, |
| 114 | + uint256[2][2] memory b_, |
| 115 | + uint256[2] memory c_, |
| 116 | + uint256 pubSignalsCount_ |
| 117 | + ) internal view returns (bool) { |
| 118 | + require( |
| 119 | + pubSignals_.length == pubSignalsCount_, |
| 120 | + "VerifierHelper: invalid public signals count" |
| 121 | + ); |
| 122 | + |
| 123 | + return _verifyProof(verifier_, a_, b_, c_, pubSignals_, pubSignalsCount_); |
| 124 | + } |
| 125 | + |
| 126 | + function _verifyProof( |
| 127 | + address verifier_, |
| 128 | + uint256[2] memory a_, |
| 129 | + uint256[2][2] memory b_, |
| 130 | + uint256[2] memory c_, |
| 131 | + uint256[] memory pubSignals_, |
| 132 | + uint256 pubSignalsCount_ |
| 133 | + ) private view returns (bool) { |
| 134 | + string memory funcSign_ = string( |
| 135 | + abi.encodePacked( |
| 136 | + "verifyProof(uint256[2],uint256[2][2],uint256[2],uint256[", |
| 137 | + pubSignalsCount_.toString(), |
| 138 | + "])" |
| 139 | + ) |
| 140 | + ); |
| 141 | + |
| 142 | + (bool success_, bytes memory returnData_) = verifier_.staticcall( |
| 143 | + abi.encodeWithSignature(funcSign_, a_, b_, c_, pubSignals_) |
| 144 | + ); |
| 145 | + |
| 146 | + require(success_, "VerifierHelper: failed to call verifyProof function"); |
| 147 | + |
| 148 | + return abi.decode(returnData_, (bool)); |
| 149 | + } |
| 150 | +} |
0 commit comments