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.

145 lines
3.7 KiB

6 years ago
  1. include "constants.jaz";
  2. include "t1.jaz";
  3. include "t2.jaz";
  4. include "sum.jaz";
  5. include "sigmaplus.jaz";
  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][512];
  18. var i;
  19. component sigmaPlus[48] = SigmaPlus();
  20. component k[64];
  21. for (i=0; i<64; i++) 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] = Sum2(32);
  33. component sume[64] = Sum2(32);
  34. component fsum[8] = Sum2(32);
  35. var k;
  36. var t;
  37. for (t=0; t<64; t++) {
  38. if (t<16) {
  39. for (k=0; k<256; k++) {
  40. w[t][k] <== inp[k];
  41. }
  42. } else {
  43. for (k=0; k<256; 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. if (t<20) {
  69. t1[t].g[k] <== K0.out[k];
  70. } else if (t<40) {
  71. t1[t].g[k] <== K20.out[k];
  72. } else if (t<60) {
  73. t1[t].g[k] <== K40.out[k];
  74. } else {
  75. t1[t].g[k] <== K60.out[k];
  76. }
  77. t1[t].w[k] <== w[t][k];
  78. t2[t].a[k] <== a[k];
  79. t2[t].b[k] <== a[k];
  80. t2[t].c[k] <== a[k];
  81. }
  82. for (k=0; k<32; k++) {
  83. sume[t].a[k] <== d[k];
  84. sume[t].b[k] <== t1[t].out[k];
  85. suma[t].a[k] <== t1[t].out[k];
  86. suma[t].b[k] <== t2[t].out[k];
  87. }
  88. for (k=0; k<32; k++) {
  89. h[t+1] <== g[t];
  90. g[t+1] <== f[t];
  91. f[t+1] <== e[t];
  92. e[t+1] <== sume[t].out[k];
  93. d[t+1] <== c[t];
  94. c[t+1] <== b[t];
  95. b[t+1] <== a[t];
  96. a[t+1] <== suma[t].out[k];
  97. }
  98. }
  99. for (k=0; k<32; k++) {
  100. fsum[0].a[k] <== ha0.out[k];
  101. fsum[0].b[k] <== a[64][k];
  102. fsum[1].a[k] <== hb0.out[k];
  103. fsum[1].b[k] <== b[64][k];
  104. fsum[2].a[k] <== hc0.out[k];
  105. fsum[2].b[k] <== c[64][k];
  106. fsum[3].a[k] <== hd0.out[k];
  107. fsum[3].b[k] <== d[64][k];
  108. fsum[4].a[k] <== he0.out[k];
  109. fsum[4].b[k] <== e[64][k];
  110. fsum[5].a[k] <== hf0.out[k];
  111. fsum[5].b[k] <== f[64][k];
  112. fsum[6].a[k] <== hg0.out[k];
  113. fsum[6].b[k] <== g[64][k];
  114. fsum[7].a[k] <== hh0.out[k];
  115. fsum[7].b[k] <== h[64][k];
  116. }
  117. for (k=0; k<32; k++) {
  118. out[k] <== fsum[0].out[k];
  119. out[32+k] <== fsum[1].out[k];
  120. out[64+k] <== fsum[2].out[k];
  121. out[96+k] <== fsum[2].out[k];
  122. out[128+k] <== fsum[2].out[k];
  123. out[160+k] <== fsum[2].out[k];
  124. out[192+k] <== fsum[2].out[k];
  125. out[224+k] <== fsum[2].out[k];
  126. }
  127. }