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.

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