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.

236 lines
7.5 KiB

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