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.

271 lines
7.5 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];
  116. component cadders[nWindows];
  117. for (i=0; i<nWindows; i++) {
  118. windows[i] = WindowMulFix();
  119. cadders[i] = MontgomeryAdd();
  120. if (i==0) {
  121. windows[i].base[0] <== e2m.out[0];
  122. windows[i].base[1] <== e2m.out[1];
  123. cadders[i].in1[0] <== e2m.out[0];
  124. cadders[i].in1[1] <== e2m.out[1];
  125. } else {
  126. windows[i].base[0] <== windows[i-1].out8[0];
  127. windows[i].base[1] <== windows[i-1].out8[1];
  128. cadders[i].in1[0] <== cadders[i-1].out[0];
  129. cadders[i].in1[1] <== cadders[i-1].out[1];
  130. }
  131. cadders[i].in2[0] <== windows[i].out8[0];
  132. cadders[i].in2[1] <== windows[i].out8[1];
  133. for (j=0; j<3; j++) {
  134. windows[i].in[j] <== e[3*i+j];
  135. }
  136. }
  137. for (i=0; i<nWindows; i++) {
  138. adders[i] = MontgomeryAdd();
  139. if (i==0) {
  140. adders[i].in1[0] <== windows[nWindows-1].out8[0];
  141. adders[i].in1[1] <== windows[nWindows-1].out8[1];
  142. } else {
  143. adders[i].in1[0] <== adders[i-1].out[0];
  144. adders[i].in1[1] <== adders[i-1].out[1];
  145. }
  146. adders[i].in2[0] <== windows[i].out[0];
  147. adders[i].in2[1] <== windows[i].out[1];
  148. }
  149. component m2e = Montgomery2Edwards();
  150. component cm2e = Montgomery2Edwards();
  151. m2e.in[0] <== adders[nWindows-1].out[0];
  152. m2e.in[1] <== adders[nWindows-1].out[1];
  153. cm2e.in[0] <== cadders[nWindows-1].out[0];
  154. cm2e.in[1] <== cadders[nWindows-1].out[1];
  155. component cAdd = BabyAdd();
  156. cAdd.x1 <== m2e.out[0];
  157. cAdd.y1 <== m2e.out[1];
  158. cAdd.x2 <== -cm2e.out[0];
  159. cAdd.y2 <== cm2e.out[1];
  160. cAdd.xout ==> out[0];
  161. cAdd.yout ==> out[1];
  162. windows[nWindows-1].out8[0] ==> dbl[0];
  163. windows[nWindows-1].out8[1] ==> dbl[1];
  164. }
  165. /*
  166. This component multiplies a escalar times a fixed point BASE (twisted edwards format)
  167. Signals
  168. e: The escalar in binary format
  169. out: The output point in twisted edwards
  170. */
  171. template EscalarMulFix(n, BASE) {
  172. signal input e[n]; // Input in binary format
  173. signal output out[2]; // Point (Twisted format)
  174. var nsegments = (n-1)\246 +1; // 249 probably would work. But I'm not sure and for security I keep 246
  175. var nlastsegment = n - (nsegments-1)*249;
  176. component segments[nsegments];
  177. component m2e[nsegments-1];
  178. component adders[nsegments-1];
  179. var s;
  180. var i;
  181. var nseg;
  182. var nWindows
  183. for (s=0; s<nsegments; s++) {
  184. nseg = (s < nsegments-1) ? 249 : nlastsegment;
  185. nWindows = ((nseg - 1)\3)+1;
  186. segments[s] = SegmentMulFix(nWindows);
  187. for (i=0; i<nseg; i++) {
  188. segments[s].e[i] <== e[s*249+i];
  189. }
  190. for (i = nseg; i<nWindows*3; i++) {
  191. segments[s].e[i] <== 0;
  192. }
  193. if (s==0) {
  194. segments[s].base[0] <== BASE[0];
  195. segments[s].base[1] <== BASE[1];
  196. } else {
  197. m2e[s-1] = Montgomery2Edwards();
  198. adders[s-1] = BabyAdd();
  199. segments[s-1].dbl[0] ==> m2e[s-1].in[0];
  200. segments[s-1].dbl[1] ==> m2e[s-1].in[1];
  201. m2e[s-1].out[0] ==> segments[s].base[0];
  202. m2e[s-1].out[1] ==> segments[s].base[1];
  203. if (s==1) {
  204. segments[s-1].out[0] ==> adders[s-1].x1;
  205. segments[s-1].out[1] ==> adders[s-1].y1;
  206. } else {
  207. adders[s-2].xout ==> adders[s-1].x1;
  208. adders[s-2].yout ==> adders[s-1].y1;
  209. }
  210. segments[s].out[0] ==> adders[s-1].x2;
  211. segments[s].out[1] ==> adders[s-1].y2;
  212. }
  213. }
  214. if (nsegments == 1) {
  215. segments[0].out[0] ==> out[0];
  216. segments[0].out[1] ==> out[1];
  217. } else {
  218. adders[nsegments-2].xout ==> out[0];
  219. adders[nsegments-2].yout ==> out[1];
  220. }
  221. }