_blake.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Internal helpers for blake hash.
  3. * @module
  4. */
  5. import { rotr } from './utils.ts';
  6. /**
  7. * Internal blake variable.
  8. * For BLAKE2b, the two extra permutations for rounds 10 and 11 are SIGMA[10..11] = SIGMA[0..1].
  9. */
  10. // prettier-ignore
  11. export const BSIGMA: Uint8Array = /* @__PURE__ */ Uint8Array.from([
  12. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  13. 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
  14. 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
  15. 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
  16. 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
  17. 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
  18. 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
  19. 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
  20. 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
  21. 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
  22. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  23. 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
  24. // Blake1, unused in others
  25. 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
  26. 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
  27. 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
  28. 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
  29. ]);
  30. // prettier-ignore
  31. export type Num4 = { a: number; b: number; c: number; d: number; };
  32. // Mixing function G splitted in two halfs
  33. export function G1s(a: number, b: number, c: number, d: number, x: number): Num4 {
  34. a = (a + b + x) | 0;
  35. d = rotr(d ^ a, 16);
  36. c = (c + d) | 0;
  37. b = rotr(b ^ c, 12);
  38. return { a, b, c, d };
  39. }
  40. export function G2s(a: number, b: number, c: number, d: number, x: number): Num4 {
  41. a = (a + b + x) | 0;
  42. d = rotr(d ^ a, 8);
  43. c = (c + d) | 0;
  44. b = rotr(b ^ c, 7);
  45. return { a, b, c, d };
  46. }