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.

255 lines
8.3 KiB

5 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 "montgomery.circom";
  16. include "mux3.circom";
  17. include "babyjub.circom";
  18. template Window4() {
  19. signal input in[4];
  20. signal input base[2];
  21. signal output out[2];
  22. signal output out8[2]; // Returns 8*Base (To be linked)
  23. component mux = MultiMux3(2);
  24. mux.s[0] <== in[0];
  25. mux.s[1] <== in[1];
  26. mux.s[2] <== in[2];
  27. component dbl2 = MontgomeryDouble();
  28. component adr3 = MontgomeryAdd();
  29. component adr4 = MontgomeryAdd();
  30. component adr5 = MontgomeryAdd();
  31. component adr6 = MontgomeryAdd();
  32. component adr7 = MontgomeryAdd();
  33. component adr8 = MontgomeryAdd();
  34. // in[0] -> 1*BASE
  35. mux.c[0][0] <== base[0];
  36. mux.c[1][0] <== base[1];
  37. // in[1] -> 2*BASE
  38. dbl2.in[0] <== base[0];
  39. dbl2.in[1] <== base[1];
  40. mux.c[0][1] <== dbl2.out[0];
  41. mux.c[1][1] <== dbl2.out[1];
  42. // in[2] -> 3*BASE
  43. adr3.in1[0] <== base[0];
  44. adr3.in1[1] <== base[1];
  45. adr3.in2[0] <== dbl2.out[0];
  46. adr3.in2[1] <== dbl2.out[1];
  47. mux.c[0][2] <== adr3.out[0];
  48. mux.c[1][2] <== adr3.out[1];
  49. // in[3] -> 4*BASE
  50. adr4.in1[0] <== base[0];
  51. adr4.in1[1] <== base[1];
  52. adr4.in2[0] <== adr3.out[0];
  53. adr4.in2[1] <== adr3.out[1];
  54. mux.c[0][3] <== adr4.out[0];
  55. mux.c[1][3] <== adr4.out[1];
  56. // in[4] -> 5*BASE
  57. adr5.in1[0] <== base[0];
  58. adr5.in1[1] <== base[1];
  59. adr5.in2[0] <== adr4.out[0];
  60. adr5.in2[1] <== adr4.out[1];
  61. mux.c[0][4] <== adr5.out[0];
  62. mux.c[1][4] <== adr5.out[1];
  63. // in[5] -> 6*BASE
  64. adr6.in1[0] <== base[0];
  65. adr6.in1[1] <== base[1];
  66. adr6.in2[0] <== adr5.out[0];
  67. adr6.in2[1] <== adr5.out[1];
  68. mux.c[0][5] <== adr6.out[0];
  69. mux.c[1][5] <== adr6.out[1];
  70. // in[6] -> 7*BASE
  71. adr7.in1[0] <== base[0];
  72. adr7.in1[1] <== base[1];
  73. adr7.in2[0] <== adr6.out[0];
  74. adr7.in2[1] <== adr6.out[1];
  75. mux.c[0][6] <== adr7.out[0];
  76. mux.c[1][6] <== adr7.out[1];
  77. // in[7] -> 8*BASE
  78. adr8.in1[0] <== base[0];
  79. adr8.in1[1] <== base[1];
  80. adr8.in2[0] <== adr7.out[0];
  81. adr8.in2[1] <== adr7.out[1];
  82. mux.c[0][7] <== adr8.out[0];
  83. mux.c[1][7] <== adr8.out[1];
  84. out8[0] <== adr8.out[0];
  85. out8[1] <== adr8.out[1];
  86. out[0] <== mux.out[0];
  87. out[1] <== - mux.out[1]*2*in[3] + mux.out[1]; // Negate y if in[3] is one
  88. }
  89. template Segment(nWindows) {
  90. signal input in[nWindows*4];
  91. signal input base[2];
  92. signal output out[2];
  93. var i;
  94. var j;
  95. // Convert the base to montgomery
  96. component e2m = Edwards2Montgomery();
  97. e2m.in[0] <== base[0];
  98. e2m.in[1] <== base[1];
  99. component windows[nWindows];
  100. component doublers1[nWindows-1];
  101. component doublers2[nWindows-1];
  102. component adders[nWindows-1];
  103. for (i=0; i<nWindows; i++) {
  104. windows[i] = Window4();
  105. for (j=0; j<4; j++) {
  106. windows[i].in[j] <== in[4*i+j];
  107. }
  108. if (i==0) {
  109. windows[i].base[0] <== e2m.out[0];
  110. windows[i].base[1] <== e2m.out[1];
  111. } else {
  112. doublers1[i-1] = MontgomeryDouble();
  113. doublers2[i-1] = MontgomeryDouble();
  114. doublers1[i-1].in[0] <== windows[i-1].out8[0];
  115. doublers1[i-1].in[1] <== windows[i-1].out8[1];
  116. doublers2[i-1].in[0] <== doublers1[i-1].out[0];
  117. doublers2[i-1].in[1] <== doublers1[i-1].out[1];
  118. windows[i].base[0] <== doublers2[i-1].out[0];
  119. windows[i].base[1] <== doublers2[i-1].out[1];
  120. adders[i-1] = MontgomeryAdd();
  121. if (i==1) {
  122. adders[i-1].in1[0] <== windows[0].out[0];
  123. adders[i-1].in1[1] <== windows[0].out[1];
  124. } else {
  125. adders[i-1].in1[0] <== adders[i-2].out[0];
  126. adders[i-1].in1[1] <== adders[i-2].out[1];
  127. }
  128. adders[i-1].in2[0] <== windows[i].out[0];
  129. adders[i-1].in2[1] <== windows[i].out[1];
  130. }
  131. }
  132. component m2e = Montgomery2Edwards();
  133. if (nWindows > 1) {
  134. m2e.in[0] <== adders[nWindows-2].out[0];
  135. m2e.in[1] <== adders[nWindows-2].out[1];
  136. } else {
  137. m2e.in[0] <== windows[0].out[0];
  138. m2e.in[1] <== windows[0].out[1];
  139. }
  140. out[0] <== m2e.out[0];
  141. out[1] <== m2e.out[1];
  142. }
  143. template Pedersen(n) {
  144. signal input in[n];
  145. signal output out[2];
  146. var BASE[10][2] = [
  147. [7688621503272331394947188562469131124099290577812125474996268020905176040083,6637287939860384587467947982369268811366630904563077767287326262235485629411],
  148. [11549681895645637778324638856880330712650895608496649854094912415387988201330,5771732722784528537721081267383956005090479808901717812009343940574217488577],
  149. [18790245153471844934157747708238883966079935875787657036767664036124524381945,18300275459419441151064576487317481499516933849631632883767173501999997278432],
  150. [16301069151422548986850494139112207641738464387919729729324473657161689764196,8215273507373494014441104012907835625670941526105528197815397741007626226499],
  151. [12597665704678284488008395353749282149622295037737374782196049599390683534185,4072455241781501621593714139281767473040087753548015968773801065193764079468],
  152. [4729410576230735258214831208080552588881894465489299233097088872252465832672,14367731890670510422926552586486424937476635415639602730590517235570020260326],
  153. [7546420686025050869200393054526306477146836870617678274607971529534032974471,8663210466512842901413293603100781938253817808912549776944118491282484711929],
  154. [6544653022506992755201027646251976600601201151329001772892901529509137954387,5932506509962692832681604586561215780097326378431958035490245111470435106811],
  155. [12376274813795671622507230443130412169480807188767687554607910279743333852725,10116389110458158800073166533660211332390835019644001845057351607297889034557],
  156. [18268098112071835140361074835791174816144587762778386397940339415400583397725,8120955462199046866292537174552276799123029303901205157708576578886090835495]
  157. ];
  158. var nSegments = ((n-1)\200)+1;
  159. component segments[nSegments];
  160. var i;
  161. var j;
  162. var nBits;
  163. var nWindows;
  164. for (i=0; i<nSegments; i++) {
  165. nBits = (i == (nSegments-1)) ? n - (nSegments-1)*200 : 200;
  166. nWindows = ((nBits - 1)\4)+1;
  167. segments[i] = Segment(nWindows);
  168. segments[i].base[0] <== BASE[i][0];
  169. segments[i].base[1] <== BASE[i][1];
  170. for (j = 0; j<nBits; j++) {
  171. segments[i].in[j] <== in[i*200+j];
  172. }
  173. // Fill padding bits
  174. for (j = nBits; j < nWindows*4; j++) {
  175. segments[i].in[j] <== 0;
  176. }
  177. }
  178. component adders[nSegments-1];
  179. for (i=0; i<nSegments-1; i++) {
  180. adders[i] = BabyAdd();
  181. if (i==0) {
  182. adders[i].x1 <== segments[0].out[0];
  183. adders[i].y1 <== segments[0].out[1];
  184. adders[i].x2 <== segments[1].out[0];
  185. adders[i].y2 <== segments[1].out[1];
  186. } else {
  187. adders[i].x1 <== adders[i-1].xout;
  188. adders[i].y1 <== adders[i-1].yout;
  189. adders[i].x2 <== segments[i+1].out[0];
  190. adders[i].y2 <== segments[i+1].out[1];
  191. }
  192. }
  193. /*
  194. coponent packPoint = PackPoint();
  195. if (nSegments>1) {
  196. packPoint.in[0] <== adders[nSegments-2].xout;
  197. packPoint.in[1] <== adders[nSegments-2].yout;
  198. } else {
  199. packPoint.in[0] <== segments[0].out[0];
  200. packPoint.in[1] <== segments[0].out[1];
  201. }
  202. out[0] <== packPoint.out[0];
  203. out[1] <== packPoint.out[1];
  204. */
  205. if (nSegments>1) {
  206. out[0] <== adders[nSegments-2].xout;
  207. out[1] <== adders[nSegments-2].yout;
  208. } else {
  209. out[0] <== segments[0].out[0];
  210. out[1] <== segments[0].out[1];
  211. }
  212. }