ecgadget.hpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. #include "libsnark_headers.hpp"
  2. using namespace libsnark;
  3. // There are two types of values:
  4. // _constants_ are values known at circuit generation time; they
  5. // are global constants known to everyone
  6. // _variables_ are values that change in each use of the circuit;
  7. // they have two subtypes:
  8. //
  9. // _public variables_ are values known to both the prover
  10. // and verifier but change in each use of the circuit
  11. // _private variables_ are values known only to the prover
  12. // and change in each use of the circuit
  13. // Double a constant EC point (inx,iny) to yield (outx,outy). The input
  14. // point must not be the point at infinity.
  15. template<typename FieldT>
  16. static void ec_double_point(FieldT &outx, FieldT &outy,
  17. const FieldT &inx, const FieldT &iny)
  18. {
  19. FieldT xsq = inx.squared();
  20. FieldT lambda = (xsq * 3 - 3) * (iny * 2).inverse();
  21. outx = lambda.squared() - inx * 2;
  22. outy = lambda * (inx - outx) - iny;
  23. }
  24. // Add constant EC points (inx, iny) and (addx, addy) to yield (outx, outy).
  25. // inx and addx must not be equal.
  26. template<typename FieldT>
  27. static void ec_add_points(FieldT &outx, FieldT &outy,
  28. const FieldT &inx, const FieldT &iny,
  29. const FieldT &addx, const FieldT &addy)
  30. {
  31. FieldT lambda = (addy - iny) * (addx - inx).inverse();
  32. outx = lambda.squared() - (addx + inx);
  33. outy = lambda * (inx - outx) - iny;
  34. }
  35. // Double the variable EC point (inx,iny) to yield (outx,outy). The
  36. // input point must not be the point at infinity.
  37. template<typename FieldT>
  38. class ec_double_gadget : public gadget<FieldT> {
  39. private:
  40. pb_variable<FieldT> lambda, inxsq;
  41. public:
  42. const pb_variable<FieldT> outx, outy, inx, iny;
  43. ec_double_gadget(protoboard<FieldT> &pb,
  44. const pb_variable<FieldT> &outx,
  45. const pb_variable<FieldT> &outy,
  46. const pb_linear_combination<FieldT> &inx,
  47. const pb_linear_combination<FieldT> &iny) :
  48. gadget<FieldT>(pb, "ec_double_gadget"), outx(outx), outy(outy),
  49. inx(inx), iny(iny)
  50. {
  51. // Allocate variables to protoboard
  52. // The strings (like "x") are only for debugging purposes
  53. lambda.allocate(this->pb, "lambda");
  54. inxsq.allocate(this->pb, "inxsq");
  55. }
  56. void generate_r1cs_constraints()
  57. {
  58. // inxsq = inx * inx
  59. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(inx, inx, inxsq));
  60. // 2 * iny * lambda = 3 * inxsq - 3 (a = -3 on our curve)
  61. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(2 * iny, lambda, 3 * inxsq - 3));
  62. // outx = lambda^2 - 2 * inx
  63. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, lambda, outx + 2 * inx));
  64. // outy = lambda * (inx - outx) - iny
  65. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, inx - outx, outy + iny));
  66. }
  67. void generate_r1cs_witness()
  68. {
  69. this->pb.val(inxsq) = this->pb.lc_val(inx) * this->pb.lc_val(inx);
  70. this->pb.val(lambda) = (this->pb.val(inxsq) * 3 - 3) * (this->pb.lc_val(iny) * 2).inverse();
  71. this->pb.val(outx) = this->pb.val(lambda).squared() - this->pb.lc_val(inx) * 2;
  72. this->pb.val(outy) = this->pb.val(lambda) * (this->pb.lc_val(inx) - this->pb.val(outx)) - this->pb.lc_val(iny);
  73. }
  74. };
  75. // Add the variable EC point (addx,addy) to the variable EC point
  76. // (inx,iny) to yield (outx,outy). The input point must not be the
  77. // point at infinity.
  78. template<typename FieldT>
  79. class ec_add_gadget : public gadget<FieldT> {
  80. private:
  81. pb_variable<FieldT> lambda;
  82. public:
  83. const pb_variable<FieldT> outx, outy;
  84. const pb_linear_combination<FieldT> inx, iny, addx, addy;
  85. ec_add_gadget(protoboard<FieldT> &pb,
  86. const pb_variable<FieldT> &outx,
  87. const pb_variable<FieldT> &outy,
  88. const pb_linear_combination<FieldT> &inx,
  89. const pb_linear_combination<FieldT> &iny,
  90. const pb_linear_combination<FieldT> &addx,
  91. const pb_linear_combination<FieldT> &addy) :
  92. gadget<FieldT>(pb, "ec_add_gadget"),
  93. outx(outx), outy(outy), inx(inx), iny(iny), addx(addx), addy(addy)
  94. {
  95. // Allocate variables to protoboard
  96. // The strings (like "x") are only for debugging purposes
  97. lambda.allocate(this->pb, "lambda");
  98. }
  99. void generate_r1cs_constraints()
  100. {
  101. // (addx - inx) * lambda = addy - iny
  102. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(addx - inx, lambda, addy - iny));
  103. // outx = lambda^2 - (addx + inx)
  104. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, lambda, outx + addx + inx));
  105. // outy = lambda * (inx - outx) - iny
  106. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, inx - outx, outy + iny));
  107. }
  108. void generate_r1cs_witness()
  109. {
  110. this->pb.val(lambda) = (this->pb.lc_val(addy) - this->pb.lc_val(iny)) * (this->pb.lc_val(addx) - this->pb.lc_val(inx)).inverse();
  111. this->pb.val(outx) = this->pb.val(lambda).squared() - (this->pb.lc_val(addx) + this->pb.lc_val(inx));
  112. this->pb.val(outy) = this->pb.val(lambda) * (this->pb.lc_val(inx) - this->pb.val(outx)) - this->pb.lc_val(iny);
  113. }
  114. };
  115. // Add the variable EC point P to the constant EC point (inx,iny) to
  116. // yield (outx,outy). The input point must not be the point at
  117. // infinity.
  118. template<typename FieldT>
  119. class ec_constant_add_gadget : public gadget<FieldT> {
  120. private:
  121. pb_variable<FieldT> lambda;
  122. public:
  123. const pb_variable<FieldT> outx, outy;
  124. const pb_linear_combination<FieldT> inx, iny;
  125. const FieldT Px, Py;
  126. ec_constant_add_gadget(protoboard<FieldT> &pb,
  127. const pb_variable<FieldT> &outx,
  128. const pb_variable<FieldT> &outy,
  129. const pb_linear_combination<FieldT> &inx,
  130. const pb_linear_combination<FieldT> &iny,
  131. const FieldT &Px, const FieldT &Py) :
  132. gadget<FieldT>(pb, "ec_constant_add_gadget"),
  133. outx(outx), outy(outy), inx(inx), iny(iny), Px(Px), Py(Py)
  134. {
  135. // Allocate variables to protoboard
  136. // The strings (like "x") are only for debugging purposes
  137. lambda.allocate(this->pb, "lambda");
  138. }
  139. void generate_r1cs_constraints()
  140. {
  141. // (Px - inx) * lambda = Py - iny
  142. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(Px - inx, lambda, Py - iny));
  143. // outx = lambda^2 - (Px + inx)
  144. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, lambda, outx + Px + inx));
  145. // outy = lambda * (inx - outx) - iny
  146. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, inx - outx, outy + iny));
  147. }
  148. void generate_r1cs_witness()
  149. {
  150. this->pb.val(lambda) = (Py - this->pb.lc_val(iny)) * (Px - this->pb.lc_val(inx)).inverse();
  151. this->pb.val(outx) = this->pb.val(lambda).squared() - (Px + this->pb.lc_val(inx));
  152. this->pb.val(outy) = this->pb.val(lambda) * (this->pb.lc_val(inx) - this->pb.val(outx)) - this->pb.lc_val(iny);
  153. }
  154. };
  155. // Add the constant EC point P0 or the constant EC point P1 to the
  156. // variable EC point (inx,iny) to yield (outx,outy). The input point
  157. // must not be the point at infinity. The input bit which controls
  158. // which addition is done.
  159. template<typename FieldT>
  160. class ec_2_constant_add_gadget : public gadget<FieldT> {
  161. private:
  162. pb_variable<FieldT> sumx, sumy;
  163. pb_linear_combination<FieldT> addx, addy;
  164. std::vector<ec_add_gadget<FieldT> > adder;
  165. public:
  166. const pb_variable<FieldT> outx, outy;
  167. const pb_linear_combination<FieldT> inx, iny;
  168. const pb_variable<FieldT> which;
  169. const FieldT P0x, P0y, P1x, P1y;
  170. ec_2_constant_add_gadget(protoboard<FieldT> &pb,
  171. const pb_variable<FieldT> &outx,
  172. const pb_variable<FieldT> &outy,
  173. const pb_linear_combination<FieldT> &inx,
  174. const pb_linear_combination<FieldT> &iny,
  175. const pb_variable<FieldT> &which,
  176. const FieldT &P0x, const FieldT &P0y,
  177. const FieldT &P1x, const FieldT &P1y) :
  178. gadget<FieldT>(pb, "ec_2_constant_add_gadget"),
  179. outx(outx), outy(outy), inx(inx), iny(iny), which(which),
  180. P0x(P0x), P0y(P0y), P1x(P1x), P1y(P1y)
  181. {
  182. // Allocate variables to protoboard
  183. addx.assign(pb, which * (P1x-P0x) + P0x);
  184. addy.assign(pb, which * (P1y-P0y) + P0y);
  185. adder.emplace_back(this->pb, outx, outy, inx, iny, addx, addy);
  186. }
  187. void generate_r1cs_constraints()
  188. {
  189. adder[0].generate_r1cs_constraints();
  190. }
  191. void generate_r1cs_witness()
  192. {
  193. addx.evaluate(this->pb);
  194. addy.evaluate(this->pb);
  195. adder[0].generate_r1cs_witness();
  196. }
  197. };
  198. // Add the constant EC point P0 or the variable EC point P1 to the
  199. // variable EC point (inx,iny) to yield (outx,outy). The input point
  200. // must not be the point at infinity. The input bit which controls
  201. // which addition is done.
  202. template<typename FieldT>
  203. class ec_2_1constant_add_gadget : public gadget<FieldT> {
  204. private:
  205. pb_variable<FieldT> sumx, sumy;
  206. pb_variable<FieldT> addx, addy;
  207. std::vector<ec_add_gadget<FieldT> > adder;
  208. public:
  209. const pb_variable<FieldT> outx, outy;
  210. const pb_linear_combination<FieldT> inx, iny;
  211. const pb_variable<FieldT> which;
  212. const FieldT P0x, P0y;
  213. const pb_variable<FieldT> P1x, P1y;
  214. ec_2_1constant_add_gadget(protoboard<FieldT> &pb,
  215. const pb_variable<FieldT> &outx,
  216. const pb_variable<FieldT> &outy,
  217. const pb_linear_combination<FieldT> &inx,
  218. const pb_linear_combination<FieldT> &iny,
  219. const pb_variable<FieldT> &which,
  220. const FieldT &P0x,
  221. const FieldT &P0y,
  222. const pb_variable<FieldT> &P1x,
  223. const pb_variable<FieldT> &P1y) :
  224. gadget<FieldT>(pb, "ec_2_1constant_add_gadget"),
  225. outx(outx), outy(outy), inx(inx), iny(iny), which(which),
  226. P0x(P0x), P0y(P0y), P1x(P1x), P1y(P1y)
  227. {
  228. // Allocate variables to protoboard
  229. addx.allocate(this->pb, "addx");
  230. addy.allocate(this->pb, "addy");
  231. adder.emplace_back(this->pb, outx, outy, inx, iny, addx, addy);
  232. }
  233. void generate_r1cs_constraints()
  234. {
  235. // Set (addx,addy) = which ? (P0x, P0y) : (P1x, P1y)
  236. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(P1x - P0x, which, addx - P0x));
  237. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(P1y - P0y, which, addy - P0y));
  238. adder[0].generate_r1cs_constraints();
  239. }
  240. void generate_r1cs_witness()
  241. {
  242. bool whichb = this->pb.val(which) != FieldT(0);
  243. this->pb.val(addx) = whichb ? this->pb.val(P1x) : P0x;
  244. this->pb.val(addy) = whichb ? this->pb.val(P1y) : P0y;
  245. adder[0].generate_r1cs_witness();
  246. }
  247. };
  248. // Add the variable EC point P0 or the variable EC point P1 to the
  249. // variable EC point (inx,iny) to yield (outx,outy). The input point
  250. // must not be the point at infinity. The input bit which controls
  251. // which addition is done.
  252. template<typename FieldT>
  253. class ec_2_add_gadget : public gadget<FieldT> {
  254. private:
  255. pb_variable<FieldT> sumx, sumy;
  256. pb_variable<FieldT> addx, addy;
  257. std::vector<ec_add_gadget<FieldT> > adder;
  258. public:
  259. const pb_variable<FieldT> outx, outy;
  260. const pb_linear_combination<FieldT> inx, iny;
  261. const pb_variable<FieldT> which;
  262. const pb_variable<FieldT> P0x, P0y, P1x, P1y;
  263. ec_2_add_gadget(protoboard<FieldT> &pb,
  264. const pb_variable<FieldT> &outx,
  265. const pb_variable<FieldT> &outy,
  266. const pb_linear_combination<FieldT> &inx,
  267. const pb_linear_combination<FieldT> &iny,
  268. const pb_variable<FieldT> &which,
  269. const pb_variable<FieldT> &P0x,
  270. const pb_variable<FieldT> &P0y,
  271. const pb_variable<FieldT> &P1x,
  272. const pb_variable<FieldT> &P1y) :
  273. gadget<FieldT>(pb, "ec_2_add_gadget"),
  274. outx(outx), outy(outy), inx(inx), iny(iny), which(which),
  275. P0x(P0x), P0y(P0y), P1x(P1x), P1y(P1y)
  276. {
  277. // Allocate variables to protoboard
  278. addx.allocate(this->pb, "addx");
  279. addy.allocate(this->pb, "addy");
  280. adder.emplace_back(this->pb, outx, outy, inx, iny, addx, addy);
  281. }
  282. void generate_r1cs_constraints()
  283. {
  284. // Set (addx,addy) = which ? (P0x, P0y) : (P1x, P1y)
  285. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(P1x - P0x, which, addx - P0x));
  286. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(P1y - P0y, which, addy - P0y));
  287. adder[0].generate_r1cs_constraints();
  288. }
  289. void generate_r1cs_witness()
  290. {
  291. bool whichb = this->pb.val(which) != FieldT(0);
  292. this->pb.val(addx) = whichb ? this->pb.val(P1x) : this->pb.val(P0x);
  293. this->pb.val(addy) = whichb ? this->pb.val(P1y) : this->pb.val(P0y);
  294. adder[0].generate_r1cs_witness();
  295. }
  296. };
  297. #if 0
  298. // Add nothing, or one of the constant EC points P1, P2, or P3 to the EC
  299. // point (inx,iny) to yield (outx,outy). The input point must not be
  300. // the point at infinity. The two input bits add1 and add2 control what
  301. // is added. Typically, P3 will equal P1+P2, in which case this gadget
  302. // does two conditional constant adds simultaneously in just 6 constraints.
  303. template<typename FieldT>
  304. class ec_add_P123_gadget : public gadget<FieldT> {
  305. private:
  306. pb_variable<FieldT> lambda, sumx, sumy, move;
  307. public:
  308. const pb_variable<FieldT> outx, outy;
  309. const pb_linear_combination<FieldT> inx, iny;
  310. const pb_variable<FieldT> add1, add2;
  311. const FieldT P1x, P1y, P2x, P2y, P3x, P3y;
  312. ec_add_P123_gadget(protoboard<FieldT> &pb,
  313. const pb_variable<FieldT> &outx,
  314. const pb_variable<FieldT> &outy,
  315. const pb_linear_combination<FieldT> &inx,
  316. const pb_linear_combination<FieldT> &iny,
  317. const pb_variable<FieldT> &add1,
  318. const pb_variable<FieldT> &add2,
  319. const FieldT &P1x, const FieldT &P1y,
  320. const FieldT &P2x, const FieldT &P2y,
  321. const FieldT &P3x, const FieldT &P3y) :
  322. gadget<FieldT>(pb, "ec_add_P123_gadget"),
  323. outx(outx), outy(outy), inx(inx), iny(iny), add1(add1), add2(add2),
  324. P1x(P1x), P1y(P1y), P2x(P2x), P2y(P2y), P3x(P3x), P3y(P3y)
  325. {
  326. // Allocate variables to protoboard
  327. // The strings (like "x") are only for debugging purposes
  328. lambda.allocate(this->pb, "lambda");
  329. sumx.allocate(this->pb, "sumx");
  330. sumy.allocate(this->pb, "sumy");
  331. move.allocate(this->pb, "move");
  332. }
  333. void generate_r1cs_constraints()
  334. {
  335. // Strategy: if add1 = add2 = 0, we compute some nonsense but throw
  336. // it away later. Otherwise, the coordinates of the point to add
  337. // are a _linear_ function of add1 and add2 (since P1, P2, and P3
  338. // are public constants)
  339. // In particular, the point to add is ( (P3x - P2x) * add1 + (P3x -
  340. // P1x) * add2 + (P1x + P2x - P3x), (P3y - P2y) * add1 + (P3y - P1y) *
  341. // add2 + (P1y + P2y - P3y))
  342. // (addx - inx) * lambda = addy - iny
  343. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>((P3x - P2x) * add1 + (P3x - P1x) * add2 + (P1x + P2x - P3x) - inx, lambda, (P3y - P2y) * add1 + (P3y - P1y) * add2 + (P1y + P2y - P3y) - iny));
  344. // sumx = lambda^2 - (addx + inx)
  345. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, lambda, sumx + (P3x - P2x) * add1 + (P3x - P1x) * add2 + (P1x + P2x - P3x) + inx));
  346. // sumy = lambda * (inx - sumx) - iny
  347. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(lambda, inx - sumx, sumy + iny));
  348. // Now we want to conditionally move the sum. We want that
  349. // outx = (add1 || add2) ? sumx : inx
  350. // outy = (add1 || add2) ? sumy : iny
  351. // so we compute move = add1 || add2, and then
  352. // outx = inx + (sumx - inx) * move
  353. // outy = iny + (sumy - iny) * move
  354. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(1 - add1, 1 - add2, 1 - move));
  355. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sumx - inx, move, outx - inx));
  356. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(sumy - iny, move, outy - iny));
  357. }
  358. void generate_r1cs_witness()
  359. {
  360. FieldT addxval = (P3x - P2x) * this->pb.val(add1) + (P3x - P1x) * this->pb.val(add2) + (P1x + P2x - P3x);
  361. FieldT addyval = (P3y - P2y) * this->pb.val(add1) + (P3y - P1y) * this->pb.val(add2) + (P1y + P2y - P3y);
  362. this->pb.val(lambda) = (addyval - this->pb.lc_val(iny)) * (addxval - this->pb.lc_val(inx)).inverse();
  363. this->pb.val(sumx) = this->pb.val(lambda).squared() - (addxval + this->pb.lc_val(inx));
  364. this->pb.val(sumy) = this->pb.val(lambda) * (this->pb.lc_val(inx) - this->pb.val(sumx)) - this->pb.lc_val(iny);
  365. bool a1 = this->pb.val(add1) != FieldT(0);
  366. bool a2 = this->pb.val(add2) != FieldT(0);
  367. this->pb.val(move) = a1 || a2;
  368. this->pb.val(outx) = (a1 || a2) ? this->pb.val(sumx) : this->pb.lc_val(inx);
  369. this->pb.val(outy) = (a1 || a2) ? this->pb.val(sumy) : this->pb.lc_val(iny);
  370. }
  371. };
  372. #endif
  373. // Compute A + s*P as (outx, outy) for an accumulator A, a given
  374. // constant point P, and s given as a bit vector. The _caller_ is
  375. // responsible for proving that the elements of svec are bits. The
  376. // (constant) accumulator excess (AXS) will be updated; when all the
  377. // computations are complete, AXS should be subtracted from the
  378. // accumulator A.
  379. template<typename FieldT>
  380. class ec_constant_scalarmul_vec_accum_gadget : public gadget<FieldT> {
  381. private:
  382. FieldT Cx, Cy;
  383. pb_variable_array<FieldT> accumx, accumy;
  384. std::vector<ec_2_constant_add_gadget<FieldT> > twoadders;
  385. public:
  386. const pb_variable<FieldT> outx, outy;
  387. const pb_variable<FieldT> Ax, Ay;
  388. const pb_variable_array<FieldT> svec;
  389. const FieldT Px, Py;
  390. // Strategy: We compute (as compile-time constants) (powers of 2)
  391. // times P. Based on each bit of s, we add one of the constant points
  392. // C or (2^i * P) + C to the accumulator, and regardless of s, add C
  393. // to the excess.
  394. ec_constant_scalarmul_vec_accum_gadget(protoboard<FieldT> &pb,
  395. const pb_variable<FieldT> &outx,
  396. const pb_variable<FieldT> &outy,
  397. const pb_variable<FieldT> &Ax,
  398. const pb_variable<FieldT> &Ay,
  399. const pb_variable_array<FieldT> &svec,
  400. const FieldT &Px, const FieldT &Py,
  401. FieldT &AXSx, FieldT &AXSy) :
  402. gadget<FieldT>(pb, "ec_constant_scalarmul_vec_accum_gadget"),
  403. // Precomputed coordinates of C
  404. Cx(2),
  405. Cy("4950745124018817972378217179409499695353526031437053848725554590521829916331"),
  406. outx(outx), outy(outy), Ax(Ax), Ay(Ay), svec(svec), Px(Px), Py(Py)
  407. {
  408. size_t numbits = svec.size();
  409. accumx.allocate(this->pb, numbits-1, "accumx");
  410. accumy.allocate(this->pb, numbits-1, "accumy");
  411. FieldT twoiPx = Px, twoiPy = Py;
  412. size_t i = 0;
  413. while(i < numbits) {
  414. // Invariant: twoiP = 2^i * P
  415. FieldT twoiPCx, twoiPCy;
  416. ec_add_points(twoiPCx, twoiPCy, twoiPx, twoiPy, Cx, Cy);
  417. twoadders.emplace_back(this->pb,
  418. (i == numbits-1 ? outx : accumx[i]),
  419. (i == numbits-1 ? outy : accumy[i]),
  420. (i == 0 ? Ax : accumx[i-1]),
  421. (i == 0 ? Ay : accumy[i-1]),
  422. svec[i], Cx, Cy, twoiPCx, twoiPCy);
  423. FieldT newtwoiPx, newtwoiPy, newAXSx, newAXSy;
  424. ec_double_point(newtwoiPx, newtwoiPy, twoiPx, twoiPy);
  425. twoiPx = newtwoiPx;
  426. twoiPy = newtwoiPy;
  427. i += 1;
  428. ec_add_points(newAXSx, newAXSy, AXSx, AXSy, Cx, Cy);
  429. AXSx = newAXSx;
  430. AXSy = newAXSy;
  431. }
  432. }
  433. void generate_r1cs_constraints()
  434. {
  435. for (auto&& gadget : twoadders) {
  436. gadget.generate_r1cs_constraints();
  437. }
  438. }
  439. void generate_r1cs_witness()
  440. {
  441. for (auto&& gadget : twoadders) {
  442. gadget.generate_r1cs_witness();
  443. }
  444. }
  445. };
  446. // Compute A + s*P as (outx, outy) for an accumulator A, a given
  447. // constant point P, and s given as a field element. The (constant)
  448. // accumulator excess (AXS) will be updated; when all the computations
  449. // are complete, AXS should be subtracted from the accumulator A.
  450. template<typename FieldT>
  451. class ec_constant_scalarmul_accum_gadget : public gadget<FieldT> {
  452. private:
  453. pb_variable_array<FieldT> svec;
  454. std::vector<packing_gadget<FieldT> > packers;
  455. std::vector<ec_constant_scalarmul_vec_accum_gadget<FieldT> > vecgadget;
  456. public:
  457. const pb_variable<FieldT> outx, outy;
  458. const pb_variable<FieldT> Ax, Ay;
  459. const pb_variable<FieldT> s;
  460. const FieldT Px, Py;
  461. ec_constant_scalarmul_accum_gadget(protoboard<FieldT> &pb,
  462. const pb_variable<FieldT> &outx,
  463. const pb_variable<FieldT> &outy,
  464. const pb_variable<FieldT> &Ax,
  465. const pb_variable<FieldT> &Ay,
  466. const pb_variable<FieldT> &s,
  467. const FieldT &Px, const FieldT &Py,
  468. FieldT &AXSx, FieldT &AXSy) :
  469. gadget<FieldT>(pb, "ec_constant_scalarmul_accum_gadget"),
  470. outx(outx), outy(outy), Ax(Ax), Ay(Ay), s(s), Px(Px), Py(Py)
  471. {
  472. // Allocate variables to protoboard
  473. // The strings (like "x") are only for debugging purposes
  474. size_t numbits = FieldT::num_bits;
  475. svec.allocate(this->pb, numbits, "svec");
  476. packers.emplace_back(this->pb, svec, s);
  477. vecgadget.emplace_back(this->pb, outx, outy, Ax, Ay, svec, Px, Py, AXSx, AXSy);
  478. }
  479. void generate_r1cs_constraints()
  480. {
  481. packers[0].generate_r1cs_constraints(true);
  482. vecgadget[0].generate_r1cs_constraints();
  483. }
  484. void generate_r1cs_witness()
  485. {
  486. packers[0].generate_r1cs_witness_from_packed();
  487. vecgadget[0].generate_r1cs_witness();
  488. }
  489. };
  490. // Compute s*P as (outx, outy) for a given constant point P, and s given
  491. // as a bit vector. The _caller_ is responsible for proving that the
  492. // elements of svec are bits.
  493. template<typename FieldT>
  494. class ec_constant_scalarmul_vec_gadget : public gadget<FieldT> {
  495. private:
  496. FieldT Cx, Cy, Ax, Ay, AXSx, AXSy;
  497. pb_variable<FieldT> accinx, acciny, accoutx, accouty;
  498. std::vector<ec_constant_scalarmul_vec_accum_gadget<FieldT> > scalarmuls;
  499. std::vector<ec_constant_add_gadget<FieldT> > adders;
  500. public:
  501. const pb_variable<FieldT> outx, outy;
  502. const pb_variable_array<FieldT> svec;
  503. const FieldT Px, Py;
  504. ec_constant_scalarmul_vec_gadget(protoboard<FieldT> &pb,
  505. const pb_variable<FieldT> &outx,
  506. const pb_variable<FieldT> &outy,
  507. const pb_variable_array<FieldT> &svec,
  508. const FieldT &Px, const FieldT &Py) :
  509. gadget<FieldT>(pb, "ec_constant_scalarmul_vec_gadget"),
  510. // Precomputed coordinates of C and A
  511. Cx(2),
  512. Cy("4950745124018817972378217179409499695353526031437053848725554590521829916331"),
  513. Ax("7536839002660211356286040193441766649532044555061394833845553337792579131020"),
  514. Ay("11391058648720923807988142436733355540810929560298907319389650598553246451302"),
  515. outx(outx), outy(outy), svec(svec), Px(Px), Py(Py)
  516. {
  517. AXSx = Ax;
  518. AXSy = Ay;
  519. accinx.allocate(this->pb, "accinx");
  520. acciny.allocate(this->pb, "acciny");
  521. accoutx.allocate(this->pb, "accoutx");
  522. accouty.allocate(this->pb, "accouty");
  523. scalarmuls.emplace_back(pb, accoutx, accouty, accinx, acciny, svec, Px, Py, AXSx, AXSy);
  524. adders.emplace_back(pb, outx, outy, accoutx, accouty, AXSx, -AXSy);
  525. }
  526. void generate_r1cs_constraints()
  527. {
  528. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(accinx, 1, Ax));
  529. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(acciny, 1, Ay));
  530. scalarmuls[0].generate_r1cs_constraints();
  531. adders[0].generate_r1cs_constraints();
  532. }
  533. void generate_r1cs_witness()
  534. {
  535. this->pb.val(accinx) = Ax;
  536. this->pb.val(acciny) = Ay;
  537. scalarmuls[0].generate_r1cs_witness();
  538. adders[0].generate_r1cs_witness();
  539. }
  540. };
  541. // Compute s*P as (outx, outy) for a given constant point P, and s given
  542. // as a field element.
  543. template<typename FieldT>
  544. class ec_constant_scalarmul_gadget : public gadget<FieldT> {
  545. private:
  546. pb_variable_array<FieldT> svec;
  547. std::vector<packing_gadget<FieldT> > packers;
  548. std::vector<ec_constant_scalarmul_vec_gadget<FieldT> > vecgadget;
  549. public:
  550. const pb_variable<FieldT> outx, outy;
  551. const pb_variable<FieldT> s;
  552. const FieldT Px, Py;
  553. ec_constant_scalarmul_gadget(protoboard<FieldT> &pb,
  554. const pb_variable<FieldT> &outx,
  555. const pb_variable<FieldT> &outy,
  556. const pb_variable<FieldT> &s,
  557. const FieldT &Px, const FieldT &Py) :
  558. gadget<FieldT>(pb, "ec_constant_scalarmul_gadget"),
  559. outx(outx), outy(outy), s(s), Px(Px), Py(Py)
  560. {
  561. // Allocate variables to protoboard
  562. // The strings (like "x") are only for debugging purposes
  563. size_t numbits = FieldT::num_bits;
  564. svec.allocate(this->pb, numbits, "svec");
  565. packers.emplace_back(this->pb, svec, s);
  566. vecgadget.emplace_back(this->pb, outx, outy, svec, Px, Py);
  567. }
  568. void generate_r1cs_constraints()
  569. {
  570. packers[0].generate_r1cs_constraints(true);
  571. vecgadget[0].generate_r1cs_constraints();
  572. }
  573. void generate_r1cs_witness()
  574. {
  575. packers[0].generate_r1cs_witness_from_packed();
  576. vecgadget[0].generate_r1cs_witness();
  577. }
  578. };
  579. // Compute A + s*P as (outx, outy) for an accumulator A, a precomputed
  580. // addition table Ptable for a variable point P, and s given as a bit
  581. // vector. The _caller_ is responsible for proving that the elements of
  582. // svec are bits. The (constant) accumulator excess (AXS) will be
  583. // updated; when all the computations are complete, AXS should be
  584. // subtracted from the accumulator A. The addition table is a variable
  585. // array of length 2*numbits (where numbits is the length of svec) such
  586. // that Ptable[2*i] and Ptable[2*i+1] are the (x,y) coordinates of
  587. // 2^i * P + C.
  588. template<typename FieldT>
  589. class ec_scalarmul_vec_accum_gadget : public gadget<FieldT> {
  590. private:
  591. FieldT Cx, Cy;
  592. pb_variable_array<FieldT> accumx, accumy;
  593. std::vector<ec_2_1constant_add_gadget<FieldT> > twoadders;
  594. public:
  595. const pb_variable<FieldT> outx, outy;
  596. const pb_variable<FieldT> Ax, Ay;
  597. const pb_variable_array<FieldT> svec, Ptable;
  598. ec_scalarmul_vec_accum_gadget(protoboard<FieldT> &pb,
  599. const pb_variable<FieldT> &outx,
  600. const pb_variable<FieldT> &outy,
  601. const pb_variable<FieldT> &Ax,
  602. const pb_variable<FieldT> &Ay,
  603. const pb_variable_array<FieldT> &svec,
  604. const pb_variable_array<FieldT> &Ptable,
  605. FieldT &AXSx, FieldT &AXSy) :
  606. gadget<FieldT>(pb, "ec_scalarmul_vec_accum_gadget"),
  607. // Precomputed coordinates of C
  608. Cx(2),
  609. Cy("4950745124018817972378217179409499695353526031437053848725554590521829916331"),
  610. outx(outx), outy(outy), Ax(Ax), Ay(Ay), svec(svec), Ptable(Ptable)
  611. {
  612. size_t numbits = svec.size();
  613. assert(Ptable.size() == 2*numbits);
  614. accumx.allocate(this->pb, numbits-1, "accumx");
  615. accumy.allocate(this->pb, numbits-1, "accumy");
  616. for (size_t i = 0; i < numbits; ++i) {
  617. twoadders.emplace_back(this->pb,
  618. (i == numbits-1 ? outx : accumx[i]),
  619. (i == numbits-1 ? outy : accumy[i]),
  620. (i == 0 ? Ax : accumx[i-1]),
  621. (i == 0 ? Ay : accumy[i-1]),
  622. svec[i], Cx, Cy, Ptable[2*i], Ptable[2*i+1]);
  623. FieldT newAXSx, newAXSy;
  624. ec_add_points(newAXSx, newAXSy, AXSx, AXSy, Cx, Cy);
  625. AXSx = newAXSx;
  626. AXSy = newAXSy;
  627. }
  628. }
  629. void generate_r1cs_constraints()
  630. {
  631. for (auto&& gadget : twoadders) {
  632. gadget.generate_r1cs_constraints();
  633. }
  634. }
  635. void generate_r1cs_witness()
  636. {
  637. for (auto&& gadget : twoadders) {
  638. gadget.generate_r1cs_witness();
  639. }
  640. }
  641. };
  642. // Compute A + s*P as (outx, outy) for an accumulator A, a precomputed
  643. // addition table Ptable for a variable point P, and s given as a field
  644. // element. The _caller_ is responsible for proving that the elements
  645. // of svec are bits. The (constant) accumulator excess (AXS) will be
  646. // updated; when all the computations are complete, AXS should be
  647. // subtracted from the accumulator A. The addition table is a variable
  648. // array of length 2*numbits (where numbits is the length of the FieldT
  649. // size) such that Ptable[2*i] and Ptable[2*i+1] are the (x,y)
  650. // coordinates of 2^i * P + C.
  651. template<typename FieldT>
  652. class ec_scalarmul_accum_gadget : public gadget<FieldT> {
  653. private:
  654. pb_variable_array<FieldT> svec;
  655. std::vector<packing_gadget<FieldT> > packers;
  656. std::vector<ec_scalarmul_vec_accum_gadget<FieldT> > vecgadget;
  657. public:
  658. const pb_variable<FieldT> outx, outy;
  659. const pb_variable<FieldT> Ax, Ay;
  660. const pb_variable<FieldT> s;
  661. const pb_variable_array<FieldT> Ptable;
  662. ec_scalarmul_accum_gadget(protoboard<FieldT> &pb,
  663. const pb_variable<FieldT> &outx,
  664. const pb_variable<FieldT> &outy,
  665. const pb_variable<FieldT> &Ax,
  666. const pb_variable<FieldT> &Ay,
  667. const pb_variable<FieldT> &s,
  668. const pb_variable_array<FieldT> &Ptable,
  669. FieldT &AXSx, FieldT &AXSy) :
  670. gadget<FieldT>(pb, "ec_scalarmul_accum_gadget"),
  671. outx(outx), outy(outy), Ax(Ax), Ay(Ay), s(s), Ptable(Ptable)
  672. {
  673. // Allocate variables to protoboard
  674. // The strings (like "x") are only for debugging purposes
  675. size_t numbits = FieldT::num_bits;
  676. svec.allocate(this->pb, numbits, "svec");
  677. packers.emplace_back(this->pb, svec, s);
  678. vecgadget.emplace_back(this->pb, outx, outy, Ax, Ay, svec, Ptable, AXSx, AXSy);
  679. }
  680. void generate_r1cs_constraints()
  681. {
  682. packers[0].generate_r1cs_constraints(true);
  683. vecgadget[0].generate_r1cs_constraints();
  684. }
  685. void generate_r1cs_witness()
  686. {
  687. packers[0].generate_r1cs_witness_from_packed();
  688. vecgadget[0].generate_r1cs_witness();
  689. }
  690. };
  691. // Compute s*P as (outx, outy) for a precomputed addition table Ptable
  692. // for a variable point P, and s given as a bit vector. The _caller_ is
  693. // responsible for proving that the elements of svec are bits.
  694. // The addition table is a variable array of length 2*numbits (where
  695. // numbits is the length of svec) such that Ptable[2*i] and
  696. // Ptable[2*i+1] are the (x,y) coordinates of 2^i * P + C.
  697. template<typename FieldT>
  698. class ec_scalarmul_vec_gadget : public gadget<FieldT> {
  699. private:
  700. FieldT Cx, Cy, Ax, Ay, AXSx, AXSy;
  701. pb_variable<FieldT> accinx, acciny, accoutx, accouty;
  702. std::vector<ec_scalarmul_vec_accum_gadget<FieldT> > scalarmuls;
  703. std::vector<ec_constant_add_gadget<FieldT> > adders;
  704. public:
  705. const pb_variable<FieldT> outx, outy;
  706. const pb_variable_array<FieldT> svec;
  707. const pb_variable_array<FieldT> Ptable;
  708. ec_scalarmul_vec_gadget(protoboard<FieldT> &pb,
  709. const pb_variable<FieldT> &outx,
  710. const pb_variable<FieldT> &outy,
  711. const pb_variable_array<FieldT> &svec,
  712. const pb_variable_array<FieldT> &Ptable) :
  713. gadget<FieldT>(pb, "ec_scalarmul_vec_gadget"),
  714. // Precomputed coordinates of C and A
  715. Cx(2),
  716. Cy("4950745124018817972378217179409499695353526031437053848725554590521829916331"),
  717. Ax("7536839002660211356286040193441766649532044555061394833845553337792579131020"),
  718. Ay("11391058648720923807988142436733355540810929560298907319389650598553246451302"),
  719. outx(outx), outy(outy), svec(svec), Ptable(Ptable)
  720. {
  721. AXSx = Ax;
  722. AXSy = Ay;
  723. accinx.allocate(this->pb, "accinx");
  724. acciny.allocate(this->pb, "acciny");
  725. accoutx.allocate(this->pb, "accoutx");
  726. accouty.allocate(this->pb, "accouty");
  727. scalarmuls.emplace_back(pb, accoutx, accouty, accinx, acciny, svec, Ptable, AXSx, AXSy);
  728. adders.emplace_back(pb, outx, outy, accoutx, accouty, AXSx, -AXSy);
  729. }
  730. void generate_r1cs_constraints()
  731. {
  732. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(accinx, 1, Ax));
  733. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(acciny, 1, Ay));
  734. scalarmuls[0].generate_r1cs_constraints();
  735. adders[0].generate_r1cs_constraints();
  736. }
  737. void generate_r1cs_witness()
  738. {
  739. this->pb.val(accinx) = Ax;
  740. this->pb.val(acciny) = Ay;
  741. scalarmuls[0].generate_r1cs_witness();
  742. adders[0].generate_r1cs_witness();
  743. }
  744. };
  745. // Compute s*P as (outx, outy) for a precomputed addition table Ptable
  746. // for a variable point P, and s given as a field element. The addition
  747. // table is a variable array of length 2*numbits (where numbits is the
  748. // length of the FieldT size) such that Ptable[2*i] and Ptable[2*i+1]
  749. // are the (x,y) coordinates of 2^i * P + C.
  750. template<typename FieldT>
  751. class ec_scalarmul_gadget : public gadget<FieldT> {
  752. private:
  753. pb_variable_array<FieldT> svec;
  754. std::vector<packing_gadget<FieldT> > packers;
  755. std::vector<ec_scalarmul_vec_gadget<FieldT> > vecgadget;
  756. public:
  757. const pb_variable<FieldT> outx, outy;
  758. const pb_variable<FieldT> s;
  759. const pb_variable_array<FieldT> Ptable;
  760. ec_scalarmul_gadget(protoboard<FieldT> &pb,
  761. const pb_variable<FieldT> &outx,
  762. const pb_variable<FieldT> &outy,
  763. const pb_variable<FieldT> &s,
  764. const pb_variable_array<FieldT> &Ptable) :
  765. gadget<FieldT>(pb, "ec_scalarmul_gadget"),
  766. outx(outx), outy(outy), s(s), Ptable(Ptable)
  767. {
  768. // Allocate variables to protoboard
  769. // The strings (like "x") are only for debugging purposes
  770. size_t numbits = FieldT::num_bits;
  771. svec.allocate(this->pb, numbits, "svec");
  772. packers.emplace_back(this->pb, svec, s);
  773. vecgadget.emplace_back(this->pb, outx, outy, svec, Ptable);
  774. }
  775. void generate_r1cs_constraints()
  776. {
  777. packers[0].generate_r1cs_constraints(true);
  778. vecgadget[0].generate_r1cs_constraints();
  779. }
  780. void generate_r1cs_witness()
  781. {
  782. packers[0].generate_r1cs_witness_from_packed();
  783. vecgadget[0].generate_r1cs_witness();
  784. }
  785. // Compute the addition table. The addition table is a variable array
  786. // of length 2*numbits such that Ptable[2*i] and Ptable[2*i+1] are the
  787. // (x,y) coordinates of 2^i * P + C.
  788. static void compute_Ptable(protoboard<FieldT> &pb,
  789. pb_variable_array<FieldT> &Ptable,
  790. const FieldT &Px,
  791. const FieldT &Py)
  792. {
  793. const FieldT Cx(2);
  794. const FieldT Cy("4950745124018817972378217179409499695353526031437053848725554590521829916331");
  795. assert(Ptable.size() % 2 == 0);
  796. size_t numbits = Ptable.size() / 2;
  797. FieldT twoiPx = Px;
  798. FieldT twoiPy = Py;
  799. for (size_t i = 0; i < numbits; ++i) {
  800. // Invariant: (twoiPx, twoiPy) = 2^i * P
  801. // Compute 2^i * P + C
  802. FieldT twoiPCx, twoiPCy;
  803. ec_add_points(twoiPCx, twoiPCy, twoiPx, twoiPy, Cx, Cy);
  804. pb.val(Ptable[2*i]) = twoiPCx;
  805. pb.val(Ptable[2*i+1]) = twoiPCy;
  806. // Compute 2^{i+1} * P
  807. FieldT twoi1Px, twoi1Py;
  808. ec_double_point(twoi1Px, twoi1Py, twoiPx, twoiPy);
  809. twoiPx = twoi1Px;
  810. twoiPy = twoi1Py;
  811. }
  812. }
  813. };
  814. // Compute a*G + b*H as (outx, outy), given a and b as field elements.
  815. template<typename FieldT>
  816. class ec_pedersen_gadget : public gadget<FieldT> {
  817. private:
  818. pb_variable<FieldT> accinx, acciny, accmidx, accmidy, accoutx, accouty;
  819. std::vector<ec_constant_scalarmul_accum_gadget<FieldT> > mulgadgets;
  820. std::vector<ec_constant_add_gadget<FieldT> > addgadget;
  821. const FieldT Gx, Gy, Hx, Hy, Ax, Ay;
  822. public:
  823. const pb_variable<FieldT> outx, outy, a, b;
  824. ec_pedersen_gadget(protoboard<FieldT> &pb,
  825. const pb_variable<FieldT> &outx,
  826. const pb_variable<FieldT> &outy,
  827. const pb_variable<FieldT> &a,
  828. const pb_variable<FieldT> &b) :
  829. gadget<FieldT>(pb, "ec_pedersen_gadget"),
  830. outx(outx), outy(outy), a(a), b(b),
  831. // Precomputed coordinates of G, H, and A
  832. Gx(0),
  833. Gy("11977228949870389393715360594190192321220966033310912010610740966317727761886"),
  834. Hx(1),
  835. Hy("21803877843449984883423225223478944275188924769286999517937427649571474907279"),
  836. Ax("7536839002660211356286040193441766649532044555061394833845553337792579131020"),
  837. Ay("11391058648720923807988142436733355540810929560298907319389650598553246451302")
  838. {
  839. // Allocate variables to protoboard
  840. // The strings (like "x") are only for debugging purposes
  841. accinx.allocate(this->pb, "accinx");
  842. acciny.allocate(this->pb, "acciny");
  843. accmidx.allocate(this->pb, "accmidx");
  844. accmidy.allocate(this->pb, "accmidy");
  845. accoutx.allocate(this->pb, "accoutx");
  846. accouty.allocate(this->pb, "accouty");
  847. // Initialize the accumulator
  848. FieldT AXSx = Ax;
  849. FieldT AXSy = Ay;
  850. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(accinx, 1, Ax));
  851. this->pb.add_r1cs_constraint(r1cs_constraint<FieldT>(acciny, 1, Ay));
  852. // Initialize the gadgets
  853. mulgadgets.emplace_back(this->pb, accmidx, accmidy, accinx, acciny, a, Gx, Gy, AXSx, AXSy);
  854. mulgadgets.emplace_back(this->pb, accoutx, accouty, accmidx, accmidy, b, Hx, Hy, AXSx, AXSy);
  855. // Subtract the accumulator excess to get the result
  856. addgadget.emplace_back(this->pb, outx, outy, accoutx, accouty, AXSx, -AXSy);
  857. }
  858. void generate_r1cs_constraints()
  859. {
  860. this->pb.val(accinx) = Ax;
  861. this->pb.val(acciny) = Ay;
  862. mulgadgets[0].generate_r1cs_constraints();
  863. mulgadgets[1].generate_r1cs_constraints();
  864. addgadget[0].generate_r1cs_constraints();
  865. }
  866. void generate_r1cs_witness()
  867. {
  868. mulgadgets[0].generate_r1cs_witness();
  869. mulgadgets[1].generate_r1cs_witness();
  870. addgadget[0].generate_r1cs_witness();
  871. }
  872. };