sha256.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { HashMD, Chi, Maj } from './_md.js';
  2. import { rotr, wrapConstructor } from './utils.js';
  3. // SHA2-256 need to try 2^128 hashes to execute birthday attack.
  4. // BTC network is doing 2^67 hashes/sec as per early 2023.
  5. // Round constants:
  6. // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
  7. // prettier-ignore
  8. const SHA256_K = /* @__PURE__ */ new Uint32Array([
  9. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  10. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  11. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  12. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  13. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  14. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  15. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  16. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  17. ]);
  18. // Initial state:
  19. // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
  20. // prettier-ignore
  21. const SHA256_IV = /* @__PURE__ */ new Uint32Array([
  22. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  23. ]);
  24. // Temporary buffer, not used to store anything between runs
  25. // Named this way because it matches specification.
  26. const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
  27. class SHA256 extends HashMD {
  28. constructor() {
  29. super(64, 32, 8, false);
  30. // We cannot use array here since array allows indexing by variable
  31. // which means optimizer/compiler cannot use registers.
  32. this.A = SHA256_IV[0] | 0;
  33. this.B = SHA256_IV[1] | 0;
  34. this.C = SHA256_IV[2] | 0;
  35. this.D = SHA256_IV[3] | 0;
  36. this.E = SHA256_IV[4] | 0;
  37. this.F = SHA256_IV[5] | 0;
  38. this.G = SHA256_IV[6] | 0;
  39. this.H = SHA256_IV[7] | 0;
  40. }
  41. get() {
  42. const { A, B, C, D, E, F, G, H } = this;
  43. return [A, B, C, D, E, F, G, H];
  44. }
  45. // prettier-ignore
  46. set(A, B, C, D, E, F, G, H) {
  47. this.A = A | 0;
  48. this.B = B | 0;
  49. this.C = C | 0;
  50. this.D = D | 0;
  51. this.E = E | 0;
  52. this.F = F | 0;
  53. this.G = G | 0;
  54. this.H = H | 0;
  55. }
  56. process(view, offset) {
  57. // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
  58. for (let i = 0; i < 16; i++, offset += 4)
  59. SHA256_W[i] = view.getUint32(offset, false);
  60. for (let i = 16; i < 64; i++) {
  61. const W15 = SHA256_W[i - 15];
  62. const W2 = SHA256_W[i - 2];
  63. const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
  64. const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
  65. SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
  66. }
  67. // Compression function main loop, 64 rounds
  68. let { A, B, C, D, E, F, G, H } = this;
  69. for (let i = 0; i < 64; i++) {
  70. const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
  71. const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
  72. const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
  73. const T2 = (sigma0 + Maj(A, B, C)) | 0;
  74. H = G;
  75. G = F;
  76. F = E;
  77. E = (D + T1) | 0;
  78. D = C;
  79. C = B;
  80. B = A;
  81. A = (T1 + T2) | 0;
  82. }
  83. // Add the compressed chunk to the current hash value
  84. A = (A + this.A) | 0;
  85. B = (B + this.B) | 0;
  86. C = (C + this.C) | 0;
  87. D = (D + this.D) | 0;
  88. E = (E + this.E) | 0;
  89. F = (F + this.F) | 0;
  90. G = (G + this.G) | 0;
  91. H = (H + this.H) | 0;
  92. this.set(A, B, C, D, E, F, G, H);
  93. }
  94. roundClean() {
  95. SHA256_W.fill(0);
  96. }
  97. destroy() {
  98. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  99. this.buffer.fill(0);
  100. }
  101. }
  102. // Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
  103. class SHA224 extends SHA256 {
  104. constructor() {
  105. super();
  106. this.A = 0xc1059ed8 | 0;
  107. this.B = 0x367cd507 | 0;
  108. this.C = 0x3070dd17 | 0;
  109. this.D = 0xf70e5939 | 0;
  110. this.E = 0xffc00b31 | 0;
  111. this.F = 0x68581511 | 0;
  112. this.G = 0x64f98fa7 | 0;
  113. this.H = 0xbefa4fa4 | 0;
  114. this.outputLen = 28;
  115. }
  116. }
  117. /**
  118. * SHA2-256 hash function
  119. * @param message - data that would be hashed
  120. */
  121. export const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
  122. export const sha224 = /* @__PURE__ */ wrapConstructor(() => new SHA224());
  123. //# sourceMappingURL=sha256.js.map