verifenc.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "ecgadget.hpp"
  5. #include "scalarmul.hpp"
  6. using namespace libsnark;
  7. using namespace std;
  8. typedef enum {
  9. MODE_NONE,
  10. MODE_PRIV,
  11. MODE_PUB,
  12. MODE_CONST
  13. } Mode;
  14. template<typename FieldT>
  15. class verified_encryption_gadget : public gadget<FieldT> {
  16. private:
  17. const size_t numbits;
  18. FieldT curve_b, Gx, Gy, Hx, Hy;
  19. pb_variable<FieldT> r;
  20. pb_variable<FieldT> xsquared, ysquared;
  21. pb_variable_array<FieldT> kbits, rbits;
  22. pb_variable<FieldT> elgx, elgy;
  23. pb_linear_combination<FieldT> x;
  24. pb_variable<FieldT> s, y;
  25. vector<packing_gadget<FieldT> > packers;
  26. vector<ec_constant_scalarmul_vec_gadget<FieldT> > constmuls;
  27. vector<ec_scalarmul_vec_gadget<FieldT> > muls;
  28. vector<ec_add_gadget<FieldT> > adders;
  29. public:
  30. const Mode mode;
  31. const pb_variable<FieldT> C1x, C1y, C2x, C2y, Kx, Ky;
  32. const pb_variable<FieldT> Px, Py;
  33. const pb_variable_array<FieldT> Ptable;
  34. const pb_variable<FieldT> k;
  35. verified_encryption_gadget(protoboard<FieldT> &pb,
  36. Mode mode,
  37. const pb_variable<FieldT> &C1x,
  38. const pb_variable<FieldT> &C1y,
  39. const pb_variable<FieldT> &C2x,
  40. const pb_variable<FieldT> &C2y,
  41. const pb_variable<FieldT> &Kx,
  42. const pb_variable<FieldT> &Ky,
  43. const pb_variable<FieldT> &Px,
  44. const pb_variable<FieldT> &Py,
  45. const pb_variable_array<FieldT> &Ptable,
  46. const pb_variable<FieldT> &k) :
  47. gadget<FieldT>(pb, "verified_encryption_gadget"),
  48. // Curve parameters and generators
  49. numbits(FieldT::num_bits),
  50. curve_b("7950939520449436327800262930799465135910802758673292356620796789196167463969"),
  51. Gx(0), Gy("11977228949870389393715360594190192321220966033310912010610740966317727761886"),
  52. Hx(1), Hy("21803877843449984883423225223478944275188924769286999517937427649571474907279"),
  53. mode(mode), C1x(C1x), C1y(C1y), C2x(C2x), C2y(C2y),
  54. Kx(Kx), Ky(Ky), Px(Px), Py(Py), Ptable(Ptable), k(k)
  55. {
  56. s.allocate(pb, "s");
  57. y.allocate(pb, "y");
  58. r.allocate(pb, "r");
  59. xsquared.allocate(pb, "xsquared");
  60. ysquared.allocate(pb, "ysquared");
  61. kbits.allocate(pb, numbits-8, "kbits");
  62. rbits.allocate(pb, numbits, "rbits");
  63. // The unpacking gadgets to turn k and r into bits
  64. packers.emplace_back(pb, kbits, k);
  65. packers.emplace_back(pb, rbits, r);
  66. // The El Gamal first component r*G
  67. constmuls.emplace_back(pb, C1x, C1y, rbits, Gx, Gy);
  68. // The El Gamal intermediate value r*P
  69. elgx.allocate(pb, "elgx");
  70. elgy.allocate(pb, "elgy");
  71. if (mode == MODE_CONST) {
  72. constmuls.emplace_back(pb, elgx, elgy, rbits, Hx, Hy);
  73. } else {
  74. muls.emplace_back(pb, elgx, elgy, rbits, Px, Py, Ptable, mode == MODE_PRIV, true);
  75. }
  76. // The El Gamal second component r*P + M
  77. x.assign(pb, k * 256 + s);
  78. adders.emplace_back(pb, C2x, C2y, elgx, elgy, x, y);
  79. // The generated public key k*G
  80. constmuls.emplace_back(pb, Kx, Ky, kbits, Gx, Gy);
  81. }
  82. void generate_r1cs_constraints()
  83. {
  84. // Prove (256*k+s,y) is on the curve
  85. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(y, y, ysquared));
  86. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(k * 256 + s, k * 256 + s, xsquared));
  87. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(xsquared - 3, k * 256 + s, ysquared - curve_b));
  88. for (auto&& gadget : packers) {
  89. gadget.generate_r1cs_constraints(true);
  90. }
  91. for (auto&& gadget : constmuls) {
  92. gadget.generate_r1cs_constraints();
  93. }
  94. for (auto&& gadget : muls) {
  95. gadget.generate_r1cs_constraints();
  96. }
  97. for (auto&& gadget : adders) {
  98. gadget.generate_r1cs_constraints();
  99. }
  100. }
  101. void find_s_y(const FieldT &kval, FieldT &sval, FieldT &yval)
  102. {
  103. // A modification of the Tonelli-Shanks implementation from libff
  104. // to catch the case when you encounter a nonresidue
  105. const FieldT one = FieldT::one();
  106. size_t v = FieldT::s;
  107. FieldT z = FieldT::nqr_to_t;
  108. FieldT s_candidate = 0;
  109. bool found = false;
  110. while (!found) {
  111. FieldT x_candidate = kval*256+s_candidate;
  112. FieldT ysq_candidate = (x_candidate.squared() - 3)*x_candidate + curve_b;
  113. FieldT w = ysq_candidate^FieldT::t_minus_1_over_2;
  114. FieldT x = ysq_candidate * w;
  115. FieldT b = x * w; // b = ysq_candidate^t
  116. bool nonresidue = false;
  117. while (b != one && !nonresidue)
  118. {
  119. size_t m = 0;
  120. FieldT b2m = b;
  121. while (b2m != one)
  122. {
  123. /* invariant: b2m = b^(2^m) after entering this loop */
  124. b2m = b2m.squared();
  125. m += 1;
  126. }
  127. if (m == v) {
  128. // Not a quadratic residue
  129. s_candidate += 1;
  130. nonresidue = true;
  131. break;
  132. }
  133. int j = v-m-1;
  134. w = z;
  135. while (j > 0)
  136. {
  137. w = w.squared();
  138. --j;
  139. } // w = z^2^(v-m-1)
  140. z = w.squared();
  141. b = b * z;
  142. x = x * w;
  143. v = m;
  144. }
  145. if (!nonresidue) {
  146. found = true;
  147. yval = x;
  148. sval = s_candidate;
  149. }
  150. }
  151. }
  152. void generate_r1cs_witness()
  153. {
  154. // Find an s and y such that x^3 - 3*x + b = y^2, where x = 256*k + s
  155. FieldT sval, yval;
  156. find_s_y(this->pb.val(k), sval, yval);
  157. this->pb.val(s) = sval;
  158. this->pb.val(y) = yval;
  159. this->pb.val(r) = FieldT::random_element();
  160. this->pb.val(xsquared) = (this->pb.val(k) * 256 + this->pb.val(s)).squared();
  161. this->pb.val(ysquared) = this->pb.val(y).squared();
  162. x.evaluate(this->pb);
  163. for (auto&& gadget : packers) {
  164. gadget.generate_r1cs_witness_from_packed();
  165. }
  166. for (auto&& gadget : constmuls) {
  167. gadget.generate_r1cs_witness();
  168. }
  169. for (auto&& gadget : muls) {
  170. gadget.generate_r1cs_witness();
  171. }
  172. for (auto&& gadget : adders) {
  173. gadget.generate_r1cs_witness();
  174. }
  175. }
  176. };
  177. int main(int argc, char **argv)
  178. {
  179. Mode mode = MODE_NONE;
  180. size_t numverifencs = 1;
  181. if (argc == 2 || argc == 3) {
  182. if (!strcmp(argv[1], "priv")) {
  183. mode = MODE_PRIV;
  184. } else if (!strcmp(argv[1], "pub")) {
  185. mode = MODE_PUB;
  186. } else if (!strcmp(argv[1], "const")) {
  187. mode = MODE_CONST;
  188. }
  189. if (argc == 3) {
  190. numverifencs = atoi(argv[2]);
  191. }
  192. }
  193. if (mode == MODE_NONE || numverifencs < 1) {
  194. cerr << "Usage: " << argv[0] << " mode n" << endl << endl;
  195. cerr << "Where mode is one of:" << endl;
  196. cerr << " priv: use private Ptable" << endl;
  197. cerr << " pub: use public Ptable" << endl;
  198. cerr << " const: use constant public key (no Ptable)" << endl << endl;
  199. cerr << "and where n is the number of verifencs in the circuit" << endl;
  200. exit(1);
  201. }
  202. // Initialize the curve parameters
  203. default_r1cs_gg_ppzksnark_pp::init_public_params();
  204. typedef libff::Fr<default_r1cs_gg_ppzksnark_pp> FieldT;
  205. // Create protoboard
  206. libff::start_profiling();
  207. cout << "Keypair" << endl;
  208. protoboard<FieldT> pb;
  209. pb_variable<FieldT> C1x[numverifencs], C1y[numverifencs];
  210. pb_variable<FieldT> C2x[numverifencs], C2y[numverifencs];
  211. pb_variable<FieldT> Kx[numverifencs], Ky[numverifencs];
  212. pb_variable<FieldT> Px[numverifencs], Py[numverifencs];
  213. pb_variable_array<FieldT> Ptable[numverifencs];
  214. pb_variable<FieldT> k[numverifencs];
  215. const size_t numbits = FieldT::num_bits;
  216. // Allocate variables
  217. // Public outputs:
  218. for (size_t i = 0; i < numverifencs; ++i) {
  219. // El Gamal encryption of k under public key P (or H if MODE_CONST)
  220. // C1 = r*G, C2 = r*P + M (where M=(256*k+s,y))
  221. C1x[i].allocate(pb, "C1x");
  222. C1y[i].allocate(pb, "C1y");
  223. C2x[i].allocate(pb, "C2x");
  224. C2y[i].allocate(pb, "C2y");
  225. // Public key corresponding to private key k
  226. // K = k*G
  227. Kx[i].allocate(pb, "Kx");
  228. Ky[i].allocate(pb, "Ky");
  229. // Public inputs:
  230. // The public key P (if not MODE_CONST)
  231. if (mode != MODE_CONST) {
  232. Px[i].allocate(pb, "Px");
  233. Py[i].allocate(pb, "Py");
  234. }
  235. }
  236. if (mode != MODE_CONST) {
  237. for (size_t i = 0; i < numverifencs; ++i) {
  238. // The Ptable might be public or private, according to the mode
  239. Ptable[i].allocate(pb, 2*numbits, "Ptable");
  240. }
  241. }
  242. for (size_t i = 0; i < numverifencs; ++i) {
  243. // Private inputs:
  244. // k is a 246-bit random number
  245. k[i].allocate(pb, "k");
  246. }
  247. // This sets up the protoboard variables so that the first n of them
  248. // represent the public input and the rest is private input
  249. if (mode == MODE_PRIV) {
  250. pb.set_input_sizes(8*numverifencs);
  251. } else if (mode == MODE_PUB) {
  252. pb.set_input_sizes(8*numverifencs+2*numbits*numverifencs);
  253. } else if (mode == MODE_CONST) {
  254. pb.set_input_sizes(6*numverifencs);
  255. }
  256. // Initialize the gadgets
  257. vector<verified_encryption_gadget<FieldT> > vencs;
  258. for (size_t i = 0; i < numverifencs; ++i) {
  259. vencs.emplace_back(pb, mode, C1x[i], C1y[i], C2x[i], C2y[i], Kx[i], Ky[i], Px[i], Py[i], Ptable[i], k[i]);
  260. }
  261. for (auto&& gadget : vencs) {
  262. gadget.generate_r1cs_constraints();
  263. }
  264. const r1cs_constraint_system<FieldT> constraint_system = pb.get_constraint_system();
  265. const r1cs_gg_ppzksnark_keypair<default_r1cs_gg_ppzksnark_pp> keypair = r1cs_gg_ppzksnark_generator<default_r1cs_gg_ppzksnark_pp>(constraint_system);
  266. // Add witness values
  267. cout << "Prover" << endl;
  268. if (mode != MODE_CONST) {
  269. for (size_t i = 0; i < numverifencs; ++i) {
  270. // A variable base point P
  271. pb.val(Px[i]) = FieldT("1095194319010475832867263440470707690447963461907735667341232728633587089702");
  272. pb.val(Py[i]) = FieldT("9185463202887631101218413269806857706246311016297504828581985913021301344974");
  273. }
  274. }
  275. gmp_randstate_t randstate;
  276. gmp_randinit_default(randstate);
  277. FieldT seed = FieldT::random_element();
  278. mpz_t seed_mpz;
  279. mpz_init(seed_mpz);
  280. seed.mont_repr.to_mpz(seed_mpz);
  281. gmp_randseed(randstate, seed_mpz);
  282. mpz_clear(seed_mpz);
  283. for (size_t i = 0; i < numverifencs; ++i) {
  284. mpz_t kval;
  285. mpz_init(kval);
  286. mpz_urandomb(kval, randstate, 246);
  287. pb.val(k[i]) = FieldT(kval);
  288. mpz_clear(kval);
  289. }
  290. libff::enter_block("PROVER TIME");
  291. for (auto&& gadget : vencs) {
  292. gadget.generate_r1cs_witness();
  293. }
  294. const r1cs_gg_ppzksnark_proof<default_r1cs_gg_ppzksnark_pp> proof = r1cs_gg_ppzksnark_prover<default_r1cs_gg_ppzksnark_pp>(keypair.pk, pb.primary_input(), pb.auxiliary_input());
  295. libff::leave_block("PROVER TIME");
  296. cout << "Verifier" << endl;
  297. libff::enter_block("VERIFIER TIME");
  298. bool verified = r1cs_gg_ppzksnark_verifier_strong_IC<default_r1cs_gg_ppzksnark_pp>(keypair.vk, pb.primary_input(), proof);
  299. libff::leave_block("VERIFIER TIME");
  300. cout << "Number of R1CS constraints: " << constraint_system.num_constraints() << endl;
  301. cout << "Primary (public) input length: " << pb.primary_input().size() << endl;
  302. // cout << "Primary (public) input: " << pb.primary_input() << endl;
  303. cout << "Auxiliary (private) input length: " << pb.auxiliary_input().size() << endl;
  304. // cout << "Auxiliary (private) input: " << pb.auxiliary_input() << endl;
  305. cout << "Verification status: " << verified << endl;
  306. ofstream pkfile(string("pk_verifenc_") + argv[1]);
  307. pkfile << keypair.pk;
  308. pkfile.close();
  309. ofstream vkfile(string("vk_verifenc_") + argv[1]);
  310. vkfile << keypair.vk;
  311. vkfile.close();
  312. ofstream pffile(string("proof_verifenc_") + argv[1]);
  313. pffile << proof;
  314. pffile.close();
  315. return 0;
  316. }