sha256.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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<SHA256> {
  28. // We cannot use array here since array allows indexing by variable
  29. // which means optimizer/compiler cannot use registers.
  30. A = SHA256_IV[0] | 0;
  31. B = SHA256_IV[1] | 0;
  32. C = SHA256_IV[2] | 0;
  33. D = SHA256_IV[3] | 0;
  34. E = SHA256_IV[4] | 0;
  35. F = SHA256_IV[5] | 0;
  36. G = SHA256_IV[6] | 0;
  37. H = SHA256_IV[7] | 0;
  38. constructor() {
  39. super(64, 32, 8, false);
  40. }
  41. protected get(): [number, number, number, number, number, number, number, number] {
  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. protected set(
  47. A: number, B: number, C: number, D: number, E: number, F: number, G: number, H: number
  48. ) {
  49. this.A = A | 0;
  50. this.B = B | 0;
  51. this.C = C | 0;
  52. this.D = D | 0;
  53. this.E = E | 0;
  54. this.F = F | 0;
  55. this.G = G | 0;
  56. this.H = H | 0;
  57. }
  58. protected process(view: DataView, offset: number): void {
  59. // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
  60. for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
  61. for (let i = 16; i < 64; i++) {
  62. const W15 = SHA256_W[i - 15];
  63. const W2 = SHA256_W[i - 2];
  64. const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
  65. const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
  66. SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
  67. }
  68. // Compression function main loop, 64 rounds
  69. let { A, B, C, D, E, F, G, H } = this;
  70. for (let i = 0; i < 64; i++) {
  71. const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
  72. const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
  73. const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
  74. const T2 = (sigma0 + Maj(A, B, C)) | 0;
  75. H = G;
  76. G = F;
  77. F = E;
  78. E = (D + T1) | 0;
  79. D = C;
  80. C = B;
  81. B = A;
  82. A = (T1 + T2) | 0;
  83. }
  84. // Add the compressed chunk to the current hash value
  85. A = (A + this.A) | 0;
  86. B = (B + this.B) | 0;
  87. C = (C + this.C) | 0;
  88. D = (D + this.D) | 0;
  89. E = (E + this.E) | 0;
  90. F = (F + this.F) | 0;
  91. G = (G + this.G) | 0;
  92. H = (H + this.H) | 0;
  93. this.set(A, B, C, D, E, F, G, H);
  94. }
  95. protected roundClean() {
  96. SHA256_W.fill(0);
  97. }
  98. destroy() {
  99. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  100. this.buffer.fill(0);
  101. }
  102. }
  103. // Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
  104. class SHA224 extends SHA256 {
  105. A = 0xc1059ed8 | 0;
  106. B = 0x367cd507 | 0;
  107. C = 0x3070dd17 | 0;
  108. D = 0xf70e5939 | 0;
  109. E = 0xffc00b31 | 0;
  110. F = 0x68581511 | 0;
  111. G = 0x64f98fa7 | 0;
  112. H = 0xbefa4fa4 | 0;
  113. constructor() {
  114. super();
  115. this.outputLen = 28;
  116. }
  117. }
  118. /**
  119. * SHA2-256 hash function
  120. * @param message - data that would be hashed
  121. */
  122. export const sha256 = /* @__PURE__ */ wrapConstructor(() => new SHA256());
  123. export const sha224 = /* @__PURE__ */ wrapConstructor(() => new SHA224());