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.

137 lines
3.6 KiB

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