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.

81 lines
2.4 KiB

  1. include "constants.circom";
  2. include "sha256compression.circom";
  3. template Sha256(nBits) {
  4. signal input in[nBits];
  5. signal output out[256];
  6. var i;
  7. var k;
  8. var nBlocks;
  9. var bitsLastBlock;
  10. nBlocks = ((nBits + 64)\512)+1;
  11. signal paddedIn[nBlocks*512];
  12. for (k=0; k<nBits; k++) {
  13. paddedIn[k] <== in[k];
  14. }
  15. paddedIn[nBits] <== 1;
  16. for (k=nBits+1; k<nBlocks*512-64; k++) {
  17. paddedIn[k] <== 0;
  18. }
  19. for (k = 0; k< 64; k++) {
  20. paddedIn[nBlocks*512 - k -1] <== (nBits >> k)&1;
  21. }
  22. component ha0 = H(0);
  23. component hb0 = H(1);
  24. component hc0 = H(2);
  25. component hd0 = H(3);
  26. component he0 = H(4);
  27. component hf0 = H(5);
  28. component hg0 = H(6);
  29. component hh0 = H(7);
  30. component sha256compression[nBlocks];
  31. for (i=0; i<nBlocks; i++) {
  32. sha256compression[i] = Sha256compression() ;
  33. if (i==0) {
  34. for (k=0; k<32; k++ ) {
  35. sha256compression[i].hin[0*32+k] <== ha0.out[k];
  36. sha256compression[i].hin[1*32+k] <== hb0.out[k];
  37. sha256compression[i].hin[2*32+k] <== hc0.out[k];
  38. sha256compression[i].hin[3*32+k] <== hd0.out[k];
  39. sha256compression[i].hin[4*32+k] <== he0.out[k];
  40. sha256compression[i].hin[5*32+k] <== hf0.out[k];
  41. sha256compression[i].hin[6*32+k] <== hg0.out[k];
  42. sha256compression[i].hin[7*32+k] <== hh0.out[k];
  43. }
  44. } else {
  45. for (k=0; k<32; k++ ) {
  46. sha256compression[i].hin[32*0+k] <== sha256compression[i-1].out[32*0+31-k];
  47. sha256compression[i].hin[32*1+k] <== sha256compression[i-1].out[32*1+31-k];
  48. sha256compression[i].hin[32*2+k] <== sha256compression[i-1].out[32*2+31-k];
  49. sha256compression[i].hin[32*3+k] <== sha256compression[i-1].out[32*3+31-k];
  50. sha256compression[i].hin[32*4+k] <== sha256compression[i-1].out[32*4+31-k];
  51. sha256compression[i].hin[32*5+k] <== sha256compression[i-1].out[32*5+31-k];
  52. sha256compression[i].hin[32*6+k] <== sha256compression[i-1].out[32*6+31-k];
  53. sha256compression[i].hin[32*7+k] <== sha256compression[i-1].out[32*7+31-k];
  54. }
  55. }
  56. for (k=0; k<512; k++) {
  57. sha256compression[i].inp[k] <== paddedIn[i*512+k];
  58. }
  59. }
  60. for (k=0; k<256; k++) {
  61. out[k] <== sha256compression[nBlocks-1].out[k];
  62. }
  63. }