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.

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