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.

277 lines
7.6 KiB

6 years ago
  1. /*
  2. Copyright 2018 0KIMS association.
  3. This file is part of circom (Zero Knowledge Circuit Compiler).
  4. circom is a free software: you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. circom is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with circom. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. include "mux3.circom";
  16. include "montgomery.circom";
  17. include "babyjub.circom";
  18. /*
  19. Window of 3 elements, it calculates
  20. out = base + base*in[0] + 2*base*in[1] + 4*base*in[2]
  21. out4 = 4*base
  22. The result should be compensated.
  23. */
  24. template WindowMulFix() {
  25. signal input in[3];
  26. signal input base[2];
  27. signal output out[2];
  28. signal output out8[2]; // Returns 8*Base (To be linked)
  29. component mux = MultiMux3(2);
  30. mux.s[0] <== in[0];
  31. mux.s[1] <== in[1];
  32. mux.s[2] <== in[2];
  33. component dbl2 = MontgomeryDouble();
  34. component adr3 = MontgomeryAdd();
  35. component adr4 = MontgomeryAdd();
  36. component adr5 = MontgomeryAdd();
  37. component adr6 = MontgomeryAdd();
  38. component adr7 = MontgomeryAdd();
  39. component adr8 = MontgomeryAdd();
  40. // in[0] -> 1*BASE
  41. mux.c[0][0] <== base[0];
  42. mux.c[1][0] <== base[1];
  43. // in[1] -> 2*BASE
  44. dbl2.in[0] <== base[0];
  45. dbl2.in[1] <== base[1];
  46. mux.c[0][1] <== dbl2.out[0];
  47. mux.c[1][1] <== dbl2.out[1];
  48. // in[2] -> 3*BASE
  49. adr3.in1[0] <== base[0];
  50. adr3.in1[1] <== base[1];
  51. adr3.in2[0] <== dbl2.out[0];
  52. adr3.in2[1] <== dbl2.out[1];
  53. mux.c[0][2] <== adr3.out[0];
  54. mux.c[1][2] <== adr3.out[1];
  55. // in[3] -> 4*BASE
  56. adr4.in1[0] <== base[0];
  57. adr4.in1[1] <== base[1];
  58. adr4.in2[0] <== adr3.out[0];
  59. adr4.in2[1] <== adr3.out[1];
  60. mux.c[0][3] <== adr4.out[0];
  61. mux.c[1][3] <== adr4.out[1];
  62. // in[4] -> 5*BASE
  63. adr5.in1[0] <== base[0];
  64. adr5.in1[1] <== base[1];
  65. adr5.in2[0] <== adr4.out[0];
  66. adr5.in2[1] <== adr4.out[1];
  67. mux.c[0][4] <== adr5.out[0];
  68. mux.c[1][4] <== adr5.out[1];
  69. // in[5] -> 6*BASE
  70. adr6.in1[0] <== base[0];
  71. adr6.in1[1] <== base[1];
  72. adr6.in2[0] <== adr5.out[0];
  73. adr6.in2[1] <== adr5.out[1];
  74. mux.c[0][5] <== adr6.out[0];
  75. mux.c[1][5] <== adr6.out[1];
  76. // in[6] -> 7*BASE
  77. adr7.in1[0] <== base[0];
  78. adr7.in1[1] <== base[1];
  79. adr7.in2[0] <== adr6.out[0];
  80. adr7.in2[1] <== adr6.out[1];
  81. mux.c[0][6] <== adr7.out[0];
  82. mux.c[1][6] <== adr7.out[1];
  83. // in[7] -> 8*BASE
  84. adr8.in1[0] <== base[0];
  85. adr8.in1[1] <== base[1];
  86. adr8.in2[0] <== adr7.out[0];
  87. adr8.in2[1] <== adr7.out[1];
  88. mux.c[0][7] <== adr8.out[0];
  89. mux.c[1][7] <== adr8.out[1];
  90. out8[0] <== adr8.out[0];
  91. out8[1] <== adr8.out[1];
  92. out[0] <== mux.out[0];
  93. out[1] <== mux.out[1];
  94. }
  95. /*
  96. This component does a multiplication of a escalar times a fix base
  97. Signals:
  98. e: The scalar in bits
  99. base: the base point in edwards format
  100. out: The result
  101. dbl: Point in Edwards to be linked to the next segment.
  102. */
  103. template SegmentMulFix(nWindows) {
  104. signal input e[nWindows*3];
  105. signal input base[2];
  106. signal output out[2];
  107. signal output dbl[2];
  108. var i;
  109. var j;
  110. // Convert the base to montgomery
  111. component e2m = Edwards2Montgomery();
  112. e2m.in[0] <== base[0];
  113. e2m.in[1] <== base[1];
  114. component windows[nWindows];
  115. component adders[nWindows-1];
  116. component cadders[nWindows-1];
  117. for (i=0; i<nWindows; i++) {
  118. windows[i] = WindowMulFix();
  119. if (i==0) {
  120. windows[i].base[0] <== e2m.out[0];
  121. windows[i].base[1] <== e2m.out[1];
  122. } else {
  123. windows[i].base[0] <== windows[i-1].out8[0];
  124. windows[i].base[1] <== windows[i-1].out8[1];
  125. adders[i-1] = MontgomeryAdd();
  126. cadders[i-1] = MontgomeryAdd();
  127. if (i==1) {
  128. adders[i-1].in1[0] <== windows[0].out[0];
  129. adders[i-1].in1[1] <== windows[0].out[1];
  130. cadders[i-1].in1[0] <== e2m.out[0];
  131. cadders[i-1].in1[1] <== e2m.out[1];
  132. } else {
  133. adders[i-1].in1[0] <== adders[i-2].out[0];
  134. adders[i-1].in1[1] <== adders[i-2].out[1];
  135. cadders[i-1].in1[0] <== cadders[i-2].out[0];
  136. cadders[i-1].in1[1] <== cadders[i-2].out[1];
  137. }
  138. adders[i-1].in2[0] <== windows[i].out[0];
  139. adders[i-1].in2[1] <== windows[i].out[1];
  140. cadders[i-1].in2[0] <== windows[i-1].out8[0];
  141. cadders[i-1].in2[1] <== windows[i-1].out8[1];
  142. }
  143. for (j=0; j<3; j++) {
  144. windows[i].in[j] <== e[3*i+j];
  145. }
  146. }
  147. component m2e = Montgomery2Edwards();
  148. component cm2e = Montgomery2Edwards();
  149. if (nWindows > 1) {
  150. m2e.in[0] <== adders[nWindows-2].out[0];
  151. m2e.in[1] <== adders[nWindows-2].out[1];
  152. cm2e.in[0] <== cadders[nWindows-2].out[0];
  153. cm2e.in[1] <== cadders[nWindows-2].out[1];
  154. } else {
  155. m2e.in[0] <== windows[0].out[0];
  156. m2e.in[1] <== windows[0].out[1];
  157. cm2e.in[0] <== e2m.out[0];
  158. cm2e.in[1] <== e2m.out[1];
  159. }
  160. component cAdd = BabyAdd();
  161. cAdd.x1 <== m2e.out[0];
  162. cAdd.y1 <== m2e.out[1];
  163. cAdd.x2 <== -cm2e.out[0];
  164. cAdd.y2 <== cm2e.out[1];
  165. cAdd.xout ==> out[0];
  166. cAdd.yout ==> out[1];
  167. windows[nWindows-1].out8[0] ==> dbl[0];
  168. windows[nWindows-1].out8[1] ==> dbl[1];
  169. }
  170. /*
  171. This component multiplies a escalar times a fixed point BASE (twisted edwards format)
  172. Signals
  173. e: The escalar in binary format
  174. out: The output point in twisted edwards
  175. */
  176. template EscalarMulFix(n, BASE) {
  177. signal input e[n]; // Input in binary format
  178. signal output out[2]; // Point (Twisted format)
  179. var nsegments = (n-1)\249 +1;
  180. var nlastsegment = n - (nsegments-1)*249;
  181. component segments[nsegments];
  182. component m2e[nsegments-1];
  183. component adders[nsegments-1];
  184. var s;
  185. var i;
  186. var nseg;
  187. var nWindows
  188. for (s=0; s<nsegments; s++) {
  189. nseg = (s < nsegments-1) ? 249 : nlastsegment;
  190. nWindows = ((nseg - 1)\3)+1;
  191. segments[s] = SegmentMulFix(nWindows);
  192. for (i=0; i<nseg; i++) {
  193. segments[s].e[i] <== e[s*249+i];
  194. }
  195. for (i = nseg; i<nWindows*3; i++) {
  196. segments[s].e[i] <== 0;
  197. }
  198. if (s==0) {
  199. segments[s].base[0] <== BASE[0];
  200. segments[s].base[1] <== BASE[1];
  201. } else {
  202. m2e[s-1] = Montgomery2Edwards();
  203. adders[s-1] = BabyAdd();
  204. segments[s-1].dbl[0] ==> m2e[s-1].in[0];
  205. segments[s-1].dbl[1] ==> m2e[s-1].in[1];
  206. m2e[s-1].out[0] ==> segments[s].base[0];
  207. m2e[s-1].out[1] ==> segments[s].base[1];
  208. if (s==1) {
  209. segments[s-1].out[0] ==> adders[s-1].x1;
  210. segments[s-1].out[1] ==> adders[s-1].y1;
  211. } else {
  212. adders[s-2].xout ==> adders[s-1].x1;
  213. adders[s-2].yout ==> adders[s-1].y1;
  214. }
  215. segments[s].out[0] ==> adders[s-1].x2;
  216. segments[s].out[1] ==> adders[s-1].y2;
  217. }
  218. }
  219. if (nsegments == 1) {
  220. segments[0].out[0] ==> out[0];
  221. segments[0].out[1] ==> out[1];
  222. } else {
  223. adders[nsegments-2].xout ==> out[0];
  224. adders[nsegments-2].yout ==> out[1];
  225. }
  226. }