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.

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