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.

243 lines
9.6 KiB

  1. //
  2. // Copyright 2017 Christian Reitwiessner
  3. // 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:
  4. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. // 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.
  6. pragma solidity ^0.4.14;
  7. library Pairing {
  8. struct G1Point {
  9. uint X;
  10. uint Y;
  11. }
  12. // Encoding of field elements is: X[0] * z + X[1]
  13. struct G2Point {
  14. uint[2] X;
  15. uint[2] Y;
  16. }
  17. /// @return the generator of G1
  18. function P1() pure internal returns (G1Point) {
  19. return G1Point(1, 2);
  20. }
  21. /// @return the generator of G2
  22. function P2() pure internal returns (G2Point) {
  23. // Original code point
  24. return G2Point(
  25. [11559732032986387107991004021392285783925812861821192530917403151452391805634,
  26. 10857046999023057135944570762232829481370756359578518086990519993285655852781],
  27. [4082367875863433681332203403145435568316851327593401208105741076214120093531,
  28. 8495653923123431417604973247489272438418190587263600148770280649306958101930]
  29. );
  30. /*
  31. // Changed by Jordi point
  32. return G2Point(
  33. [10857046999023057135944570762232829481370756359578518086990519993285655852781,
  34. 11559732032986387107991004021392285783925812861821192530917403151452391805634],
  35. [8495653923123431417604973247489272438418190587263600148770280649306958101930,
  36. 4082367875863433681332203403145435568316851327593401208105741076214120093531]
  37. );
  38. */
  39. }
  40. /// @return the negation of p, i.e. p.addition(p.negate()) should be zero.
  41. function negate(G1Point p) pure internal returns (G1Point) {
  42. // The prime q in the base field F_q for G1
  43. uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
  44. if (p.X == 0 && p.Y == 0)
  45. return G1Point(0, 0);
  46. return G1Point(p.X, q - (p.Y % q));
  47. }
  48. /// @return the sum of two points of G1
  49. function addition(G1Point p1, G1Point p2) view internal returns (G1Point r) {
  50. uint[4] memory input;
  51. input[0] = p1.X;
  52. input[1] = p1.Y;
  53. input[2] = p2.X;
  54. input[3] = p2.Y;
  55. bool success;
  56. assembly {
  57. success := staticcall(sub(gas, 2000), 6, input, 0xc0, r, 0x60)
  58. // Use "invalid" to make gas estimation work
  59. switch success case 0 { invalid() }
  60. }
  61. require(success);
  62. }
  63. /// @return the product of a point on G1 and a scalar, i.e.
  64. /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p.
  65. function scalar_mul(G1Point p, uint s) view internal returns (G1Point r) {
  66. uint[3] memory input;
  67. input[0] = p.X;
  68. input[1] = p.Y;
  69. input[2] = s;
  70. bool success;
  71. assembly {
  72. success := staticcall(sub(gas, 2000), 7, input, 0x80, r, 0x60)
  73. // Use "invalid" to make gas estimation work
  74. switch success case 0 { invalid() }
  75. }
  76. require (success);
  77. }
  78. /// @return the result of computing the pairing check
  79. /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1
  80. /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should
  81. /// return true.
  82. function pairing(G1Point[] p1, G2Point[] p2) view internal returns (bool) {
  83. require(p1.length == p2.length);
  84. uint elements = p1.length;
  85. uint inputSize = elements * 6;
  86. uint[] memory input = new uint[](inputSize);
  87. for (uint i = 0; i < elements; i++)
  88. {
  89. input[i * 6 + 0] = p1[i].X;
  90. input[i * 6 + 1] = p1[i].Y;
  91. input[i * 6 + 2] = p2[i].X[0];
  92. input[i * 6 + 3] = p2[i].X[1];
  93. input[i * 6 + 4] = p2[i].Y[0];
  94. input[i * 6 + 5] = p2[i].Y[1];
  95. }
  96. uint[1] memory out;
  97. bool success;
  98. assembly {
  99. success := staticcall(sub(gas, 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
  100. // Use "invalid" to make gas estimation work
  101. switch success case 0 { invalid() }
  102. }
  103. require(success);
  104. return out[0] != 0;
  105. }
  106. /// Convenience method for a pairing check for two pairs.
  107. function pairingProd2(G1Point a1, G2Point a2, G1Point b1, G2Point b2) view internal returns (bool) {
  108. G1Point[] memory p1 = new G1Point[](2);
  109. G2Point[] memory p2 = new G2Point[](2);
  110. p1[0] = a1;
  111. p1[1] = b1;
  112. p2[0] = a2;
  113. p2[1] = b2;
  114. return pairing(p1, p2);
  115. }
  116. /// Convenience method for a pairing check for three pairs.
  117. function pairingProd3(
  118. G1Point a1, G2Point a2,
  119. G1Point b1, G2Point b2,
  120. G1Point c1, G2Point c2
  121. ) view internal returns (bool) {
  122. G1Point[] memory p1 = new G1Point[](3);
  123. G2Point[] memory p2 = new G2Point[](3);
  124. p1[0] = a1;
  125. p1[1] = b1;
  126. p1[2] = c1;
  127. p2[0] = a2;
  128. p2[1] = b2;
  129. p2[2] = c2;
  130. return pairing(p1, p2);
  131. }
  132. /// Convenience method for a pairing check for four pairs.
  133. function pairingProd4(
  134. G1Point a1, G2Point a2,
  135. G1Point b1, G2Point b2,
  136. G1Point c1, G2Point c2,
  137. G1Point d1, G2Point d2
  138. ) view internal returns (bool) {
  139. G1Point[] memory p1 = new G1Point[](4);
  140. G2Point[] memory p2 = new G2Point[](4);
  141. p1[0] = a1;
  142. p1[1] = b1;
  143. p1[2] = c1;
  144. p1[3] = d1;
  145. p2[0] = a2;
  146. p2[1] = b2;
  147. p2[2] = c2;
  148. p2[3] = d2;
  149. return pairing(p1, p2);
  150. }
  151. }
  152. contract Verifier {
  153. using Pairing for *;
  154. struct VerifyingKey {
  155. Pairing.G2Point A;
  156. Pairing.G1Point B;
  157. Pairing.G2Point C;
  158. Pairing.G2Point gamma;
  159. Pairing.G1Point gammaBeta1;
  160. Pairing.G2Point gammaBeta2;
  161. Pairing.G2Point Z;
  162. Pairing.G1Point[] IC;
  163. }
  164. struct Proof {
  165. Pairing.G1Point A;
  166. Pairing.G1Point A_p;
  167. Pairing.G2Point B;
  168. Pairing.G1Point B_p;
  169. Pairing.G1Point C;
  170. Pairing.G1Point C_p;
  171. Pairing.G1Point K;
  172. Pairing.G1Point H;
  173. }
  174. function verifyingKey() pure internal returns (VerifyingKey vk) {
  175. vk.A = Pairing.G2Point(<%vk_a%>);
  176. vk.B = Pairing.G1Point(<%vk_b%>);
  177. vk.C = Pairing.G2Point(<%vk_c%>);
  178. vk.gamma = Pairing.G2Point(<%vk_g%>);
  179. vk.gammaBeta1 = Pairing.G1Point(<%vk_gb1%>);
  180. vk.gammaBeta2 = Pairing.G2Point(<%vk_gb2%>);
  181. vk.Z = Pairing.G2Point(<%vk_z%>);
  182. vk.IC = new Pairing.G1Point[](<%vk_ic_length%>);
  183. <%vk_ic_pts%>
  184. }
  185. function verify(uint[] input, Proof proof) view internal returns (uint) {
  186. VerifyingKey memory vk = verifyingKey();
  187. require(input.length + 1 == vk.IC.length);
  188. // Compute the linear combination vk_x
  189. Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
  190. for (uint i = 0; i < input.length; i++)
  191. vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i]));
  192. vk_x = Pairing.addition(vk_x, vk.IC[0]);
  193. if (!Pairing.pairingProd2(proof.A, vk.A, Pairing.negate(proof.A_p), Pairing.P2())) return 1;
  194. if (!Pairing.pairingProd2(vk.B, proof.B, Pairing.negate(proof.B_p), Pairing.P2())) return 2;
  195. if (!Pairing.pairingProd2(proof.C, vk.C, Pairing.negate(proof.C_p), Pairing.P2())) return 3;
  196. if (!Pairing.pairingProd3(
  197. proof.K, vk.gamma,
  198. Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.A, proof.C))), vk.gammaBeta2,
  199. Pairing.negate(vk.gammaBeta1), proof.B
  200. )) return 4;
  201. if (!Pairing.pairingProd3(
  202. Pairing.addition(vk_x, proof.A), proof.B,
  203. Pairing.negate(proof.H), vk.Z,
  204. Pairing.negate(proof.C), Pairing.P2()
  205. )) return 5;
  206. return 0;
  207. }
  208. function verifyProof(
  209. uint[2] a,
  210. uint[2] a_p,
  211. uint[2][2] b,
  212. uint[2] b_p,
  213. uint[2] c,
  214. uint[2] c_p,
  215. uint[2] h,
  216. uint[2] k,
  217. uint[<%vk_input_length%>] input
  218. ) view public returns (bool r) {
  219. Proof memory proof;
  220. proof.A = Pairing.G1Point(a[0], a[1]);
  221. proof.A_p = Pairing.G1Point(a_p[0], a_p[1]);
  222. proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
  223. proof.B_p = Pairing.G1Point(b_p[0], b_p[1]);
  224. proof.C = Pairing.G1Point(c[0], c[1]);
  225. proof.C_p = Pairing.G1Point(c_p[0], c_p[1]);
  226. proof.H = Pairing.G1Point(h[0], h[1]);
  227. proof.K = Pairing.G1Point(k[0], k[1]);
  228. uint[] memory inputValues = new uint[](input.length);
  229. for(uint i = 0; i < input.length; i++){
  230. inputValues[i] = input[i];
  231. }
  232. if (verify(inputValues, proof) == 0) {
  233. return true;
  234. } else {
  235. return false;
  236. }
  237. }
  238. }