You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
9.9 KiB

3 years ago
  1. // THIS FILE IS GENERATED BY HARDHAT-CIRCOM. DO NOT EDIT THIS FILE.
  2. //
  3. // Copyright 2017 Christian Reitwiessner
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  5. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  7. //
  8. // 2019 OKIMS
  9. // ported to solidity 0.5
  10. // fixed linter warnings
  11. // added requiere error messages
  12. //
  13. pragma solidity ^0.7.6;
  14. pragma abicoder v2;
  15. contract TestVerifier{
  16. constructor() {}
  17. function verify(
  18. uint256[] memory input,
  19. Verifier.Proof memory proof,
  20. Verifier.VerifyingKey memory vk
  21. ) public view returns (bool) {
  22. uint256 err = Verifier.verify(
  23. input,
  24. proof,
  25. vk
  26. );
  27. if (err == 0) {
  28. return true;
  29. } else {
  30. return false;
  31. }
  32. }
  33. }
  34. library Verifier {
  35. using Pairing for *;
  36. struct VerifyingKey {
  37. Pairing.G1Point alfa1;
  38. Pairing.G2Point beta2;
  39. Pairing.G2Point gamma2;
  40. Pairing.G2Point delta2;
  41. Pairing.G1Point[] IC;
  42. }
  43. struct Proof {
  44. Pairing.G1Point A;
  45. Pairing.G2Point B;
  46. Pairing.G1Point C;
  47. }
  48. function verify(
  49. uint256[] memory input,
  50. Proof memory proof,
  51. VerifyingKey memory vk
  52. ) internal view returns (uint256) {
  53. uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
  54. require(input.length + 1 == vk.IC.length, "verifier-bad-input");
  55. // Compute the linear combination vk_x
  56. Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
  57. for (uint256 i = 0; i < input.length; i++) {
  58. require(input[i] < snark_scalar_field, "verifier-gte-snark-scalar-field");
  59. vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i]));
  60. }
  61. vk_x = Pairing.addition(vk_x, vk.IC[0]);
  62. if (
  63. !Pairing.pairingProd4(
  64. Pairing.negate(proof.A),
  65. proof.B,
  66. vk.alfa1,
  67. vk.beta2,
  68. vk_x,
  69. vk.gamma2,
  70. proof.C,
  71. vk.delta2
  72. )
  73. ) return 1;
  74. return 0;
  75. }
  76. function verifyProof(
  77. uint256[2] memory a,
  78. uint256[2][2] memory b,
  79. uint256[2] memory c,
  80. uint256[] memory input,
  81. VerifyingKey memory vk
  82. ) internal view returns (bool) {
  83. Proof memory proof;
  84. proof.A = Pairing.G1Point(a[0], a[1]);
  85. proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
  86. proof.C = Pairing.G1Point(c[0], c[1]);
  87. if (verify(input, proof, vk) == 0) {
  88. return true;
  89. } else {
  90. return false;
  91. }
  92. }
  93. }
  94. library Pairing {
  95. struct G1Point {
  96. uint256 X;
  97. uint256 Y;
  98. }
  99. // Encoding of field elements is: X[0] * z + X[1]
  100. struct G2Point {
  101. uint256[2] X;
  102. uint256[2] Y;
  103. }
  104. /// @return the generator of G1
  105. function P1() internal pure returns (G1Point memory) {
  106. return G1Point(1, 2);
  107. }
  108. /// @return the generator of G2
  109. function P2() internal pure returns (G2Point memory) {
  110. // Original code point
  111. return
  112. G2Point(
  113. [
  114. 11559732032986387107991004021392285783925812861821192530917403151452391805634,
  115. 10857046999023057135944570762232829481370756359578518086990519993285655852781
  116. ],
  117. [
  118. 4082367875863433681332203403145435568316851327593401208105741076214120093531,
  119. 8495653923123431417604973247489272438418190587263600148770280649306958101930
  120. ]
  121. );
  122. /*
  123. // Changed by Jordi point
  124. return G2Point(
  125. [10857046999023057135944570762232829481370756359578518086990519993285655852781,
  126. 11559732032986387107991004021392285783925812861821192530917403151452391805634],
  127. [8495653923123431417604973247489272438418190587263600148770280649306958101930,
  128. 4082367875863433681332203403145435568316851327593401208105741076214120093531]
  129. );
  130. */
  131. }
  132. /// @return r the negation of p, i.e. p.addition(p.negate()) should be zero.
  133. function negate(G1Point memory p) internal pure returns (G1Point memory r) {
  134. // The prime q in the base field F_q for G1
  135. uint256 q
  136. = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
  137. if (p.X == 0 && p.Y == 0) return G1Point(0, 0);
  138. return G1Point(p.X, q - (p.Y % q));
  139. }
  140. /// @return r the sum of two points of G1
  141. function addition(G1Point memory p1, G1Point memory p2)
  142. internal
  143. view
  144. returns (G1Point memory r)
  145. {
  146. uint256[4] memory input;
  147. input[0] = p1.X;
  148. input[1] = p1.Y;
  149. input[2] = p2.X;
  150. input[3] = p2.Y;
  151. bool success;
  152. // solium-disable-next-line security/no-inline-assembly
  153. assembly {
  154. success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60)
  155. // Use "invalid" to make gas estimation work
  156. switch success
  157. case 0 {
  158. invalid()
  159. }
  160. }
  161. require(success, "pairing-add-failed");
  162. }
  163. /// @return r the product of a point on G1 and a scalar, i.e.
  164. /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p.
  165. function scalar_mul(G1Point memory p, uint256 s)
  166. internal
  167. view
  168. returns (G1Point memory r)
  169. {
  170. uint256[3] memory input;
  171. input[0] = p.X;
  172. input[1] = p.Y;
  173. input[2] = s;
  174. bool success;
  175. // solium-disable-next-line security/no-inline-assembly
  176. assembly {
  177. success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60)
  178. // Use "invalid" to make gas estimation work
  179. switch success
  180. case 0 {
  181. invalid()
  182. }
  183. }
  184. require(success, "pairing-mul-failed");
  185. }
  186. /// @return the result of computing the pairing check
  187. /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1
  188. /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should
  189. /// return true.
  190. function pairing(G1Point[] memory p1, G2Point[] memory p2)
  191. internal
  192. view
  193. returns (bool)
  194. {
  195. require(p1.length == p2.length, "pairing-lengths-failed");
  196. uint256 elements = p1.length;
  197. uint256 inputSize = elements * 6;
  198. uint256[] memory input = new uint256[](inputSize);
  199. for (uint256 i = 0; i < elements; i++) {
  200. input[i * 6 + 0] = p1[i].X;
  201. input[i * 6 + 1] = p1[i].Y;
  202. input[i * 6 + 2] = p2[i].X[0];
  203. input[i * 6 + 3] = p2[i].X[1];
  204. input[i * 6 + 4] = p2[i].Y[0];
  205. input[i * 6 + 5] = p2[i].Y[1];
  206. }
  207. uint256[1] memory out;
  208. bool success;
  209. // solium-disable-next-line security/no-inline-assembly
  210. assembly {
  211. success := staticcall(
  212. sub(gas(), 2000),
  213. 8,
  214. add(input, 0x20),
  215. mul(inputSize, 0x20),
  216. out,
  217. 0x20
  218. )
  219. // Use "invalid" to make gas estimation work
  220. switch success
  221. case 0 {
  222. invalid()
  223. }
  224. }
  225. require(success, "pairing-opcode-failed");
  226. return out[0] != 0;
  227. }
  228. /// Convenience method for a pairing check for two pairs.
  229. function pairingProd2(
  230. G1Point memory a1,
  231. G2Point memory a2,
  232. G1Point memory b1,
  233. G2Point memory b2
  234. ) internal view returns (bool) {
  235. G1Point[] memory p1 = new G1Point[](2);
  236. G2Point[] memory p2 = new G2Point[](2);
  237. p1[0] = a1;
  238. p1[1] = b1;
  239. p2[0] = a2;
  240. p2[1] = b2;
  241. return pairing(p1, p2);
  242. }
  243. /// Convenience method for a pairing check for three pairs.
  244. function pairingProd3(
  245. G1Point memory a1,
  246. G2Point memory a2,
  247. G1Point memory b1,
  248. G2Point memory b2,
  249. G1Point memory c1,
  250. G2Point memory c2
  251. ) internal view returns (bool) {
  252. G1Point[] memory p1 = new G1Point[](3);
  253. G2Point[] memory p2 = new G2Point[](3);
  254. p1[0] = a1;
  255. p1[1] = b1;
  256. p1[2] = c1;
  257. p2[0] = a2;
  258. p2[1] = b2;
  259. p2[2] = c2;
  260. return pairing(p1, p2);
  261. }
  262. /// Convenience method for a pairing check for four pairs.
  263. function pairingProd4(
  264. G1Point memory a1,
  265. G2Point memory a2,
  266. G1Point memory b1,
  267. G2Point memory b2,
  268. G1Point memory c1,
  269. G2Point memory c2,
  270. G1Point memory d1,
  271. G2Point memory d2
  272. ) internal view returns (bool) {
  273. G1Point[] memory p1 = new G1Point[](4);
  274. G2Point[] memory p2 = new G2Point[](4);
  275. p1[0] = a1;
  276. p1[1] = b1;
  277. p1[2] = c1;
  278. p1[3] = d1;
  279. p2[0] = a2;
  280. p2[1] = b2;
  281. p2[2] = c2;
  282. p2[3] = d2;
  283. return pairing(p1, p2);
  284. }
  285. }