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.

156 lines
4.5 KiB

  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 "constants.circom";
  16. include "t1.circom";
  17. include "t2.circom";
  18. include "../binsum.circom";
  19. include "sigmaplus.circom";
  20. template Sha256compression() {
  21. signal input hin[256];
  22. signal input inp[512];
  23. signal output out[256];
  24. signal a[65][32];
  25. signal b[65][32];
  26. signal c[65][32];
  27. signal d[65][32];
  28. signal e[65][32];
  29. signal f[65][32];
  30. signal g[65][32];
  31. signal h[65][32];
  32. signal w[64][32];
  33. var i;
  34. component sigmaPlus[48];
  35. for (i=0; i<48; i++) sigmaPlus[i] = SigmaPlus();
  36. component ct_k[64];
  37. for (i=0; i<64; i++) ct_k[i] = K(i);
  38. component t1[64];
  39. for (i=0; i<64; i++) t1[i] = T1();
  40. component t2[64];
  41. for (i=0; i<64; i++) t2[i] = T2();
  42. component suma[64];
  43. for (i=0; i<64; i++) suma[i] = BinSum(32, 2);
  44. component sume[64];
  45. for (i=0; i<64; i++) sume[i] = BinSum(32, 2);
  46. component fsum[8];
  47. for (i=0; i<8; i++) fsum[i] = BinSum(32, 2);
  48. var k;
  49. var t;
  50. for (t=0; t<64; t++) {
  51. if (t<16) {
  52. for (k=0; k<32; k++) {
  53. w[t][k] <== inp[t*32+31-k];
  54. }
  55. } else {
  56. for (k=0; k<32; k++) {
  57. sigmaPlus[t-16].in2[k] <== w[t-2][k];
  58. sigmaPlus[t-16].in7[k] <== w[t-7][k];
  59. sigmaPlus[t-16].in15[k] <== w[t-15][k];
  60. sigmaPlus[t-16].in16[k] <== w[t-16][k];
  61. w[t][k] <== sigmaPlus[t-16].out[k];
  62. }
  63. }
  64. }
  65. for (k=0; k<32; k++ ) {
  66. a[0][k] <== hin[k];
  67. b[0][k] <== hin[32*1 + k];
  68. c[0][k] <== hin[32*2 + k];
  69. d[0][k] <== hin[32*3 + k];
  70. e[0][k] <== hin[32*4 + k];
  71. f[0][k] <== hin[32*5 + k];
  72. g[0][k] <== hin[32*6 + k];
  73. h[0][k] <== hin[32*7 + k];
  74. }
  75. for (t = 0; t<64; t++) {
  76. for (k=0; k<32; k++) {
  77. t1[t].h[k] <== h[t][k];
  78. t1[t].e[k] <== e[t][k];
  79. t1[t].f[k] <== f[t][k];
  80. t1[t].g[k] <== g[t][k];
  81. t1[t].k[k] <== ct_k[t].out[k];
  82. t1[t].w[k] <== w[t][k];
  83. t2[t].a[k] <== a[t][k];
  84. t2[t].b[k] <== b[t][k];
  85. t2[t].c[k] <== c[t][k];
  86. }
  87. for (k=0; k<32; k++) {
  88. sume[t].in[0][k] <== d[t][k];
  89. sume[t].in[1][k] <== t1[t].out[k];
  90. suma[t].in[0][k] <== t1[t].out[k];
  91. suma[t].in[1][k] <== t2[t].out[k];
  92. }
  93. for (k=0; k<32; k++) {
  94. h[t+1][k] <== g[t][k];
  95. g[t+1][k] <== f[t][k];
  96. f[t+1][k] <== e[t][k];
  97. e[t+1][k] <== sume[t].out[k];
  98. d[t+1][k] <== c[t][k];
  99. c[t+1][k] <== b[t][k];
  100. b[t+1][k] <== a[t][k];
  101. a[t+1][k] <== suma[t].out[k];
  102. }
  103. }
  104. for (k=0; k<32; k++) {
  105. fsum[0].in[0][k] <== hin[32*0+k];
  106. fsum[0].in[1][k] <== a[64][k];
  107. fsum[1].in[0][k] <== hin[32*1+k];
  108. fsum[1].in[1][k] <== b[64][k];
  109. fsum[2].in[0][k] <== hin[32*2+k];
  110. fsum[2].in[1][k] <== c[64][k];
  111. fsum[3].in[0][k] <== hin[32*3+k];
  112. fsum[3].in[1][k] <== d[64][k];
  113. fsum[4].in[0][k] <== hin[32*4+k];
  114. fsum[4].in[1][k] <== e[64][k];
  115. fsum[5].in[0][k] <== hin[32*5+k];
  116. fsum[5].in[1][k] <== f[64][k];
  117. fsum[6].in[0][k] <== hin[32*6+k];
  118. fsum[6].in[1][k] <== g[64][k];
  119. fsum[7].in[0][k] <== hin[32*7+k];
  120. fsum[7].in[1][k] <== h[64][k];
  121. }
  122. for (k=0; k<32; k++) {
  123. out[31-k] <== fsum[0].out[k];
  124. out[32+31-k] <== fsum[1].out[k];
  125. out[64+31-k] <== fsum[2].out[k];
  126. out[96+31-k] <== fsum[3].out[k];
  127. out[128+31-k] <== fsum[4].out[k];
  128. out[160+31-k] <== fsum[5].out[k];
  129. out[192+31-k] <== fsum[6].out[k];
  130. out[224+31-k] <== fsum[7].out[k];
  131. }
  132. }