scrypt-jane-mix_chacha.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !defined(SCRYPT_CHOOSE_COMPILETIME) || !defined(SCRYPT_CHACHA_INCLUDED)
  2. #undef SCRYPT_MIX
  3. #define SCRYPT_MIX "ChaCha20/8 Ref"
  4. #undef SCRYPT_CHACHA_INCLUDED
  5. #define SCRYPT_CHACHA_INCLUDED
  6. #define SCRYPT_CHACHA_BASIC
  7. static void
  8. chacha_core_basic(uint32_t state[16]) {
  9. size_t rounds = 8;
  10. uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,t;
  11. x0 = state[0];
  12. x1 = state[1];
  13. x2 = state[2];
  14. x3 = state[3];
  15. x4 = state[4];
  16. x5 = state[5];
  17. x6 = state[6];
  18. x7 = state[7];
  19. x8 = state[8];
  20. x9 = state[9];
  21. x10 = state[10];
  22. x11 = state[11];
  23. x12 = state[12];
  24. x13 = state[13];
  25. x14 = state[14];
  26. x15 = state[15];
  27. #define quarter(a,b,c,d) \
  28. a += b; t = d^a; d = ROTL32(t,16); \
  29. c += d; t = b^c; b = ROTL32(t,12); \
  30. a += b; t = d^a; d = ROTL32(t, 8); \
  31. c += d; t = b^c; b = ROTL32(t, 7);
  32. for (; rounds; rounds -= 2) {
  33. quarter( x0, x4, x8,x12)
  34. quarter( x1, x5, x9,x13)
  35. quarter( x2, x6,x10,x14)
  36. quarter( x3, x7,x11,x15)
  37. quarter( x0, x5,x10,x15)
  38. quarter( x1, x6,x11,x12)
  39. quarter( x2, x7, x8,x13)
  40. quarter( x3, x4, x9,x14)
  41. }
  42. state[0] += x0;
  43. state[1] += x1;
  44. state[2] += x2;
  45. state[3] += x3;
  46. state[4] += x4;
  47. state[5] += x5;
  48. state[6] += x6;
  49. state[7] += x7;
  50. state[8] += x8;
  51. state[9] += x9;
  52. state[10] += x10;
  53. state[11] += x11;
  54. state[12] += x12;
  55. state[13] += x13;
  56. state[14] += x14;
  57. state[15] += x15;
  58. #undef quarter
  59. }
  60. #endif