Guest User

3x+1-toy-cipher

a guest
Dec 10th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdio>
  3. #include <bitset>
  4. #include<iostream>
  5.  
  6. struct xws { __uint128_t x, weyl, s; };
  7.  
  8. struct xws initializer(__uint128_t x_init, __uint128_t weyl_init, const __uint128_t s_init)
  9. {
  10. __uint128_t x = 0;
  11. __uint128_t weyl = 0;
  12. __uint128_t s = 0;
  13.  
  14. for (int i = 0; i < 128; i++)
  15. {
  16. x_init = (-(x_init & 1) & (x_init + ((x_init + 1) >> 1)) | ~- (x_init & 1) & (x_init >> 1)) ^ (weyl_init += s_init);
  17. x += (x & 1) << i;
  18. }
  19. for (int i = 0; i < 128; i++)
  20. {
  21. x_init = (-(x_init & 1) & (x_init + ((x_init + 1) >> 1)) | ~- (x_init & 1) & (x_init >> 1)) ^ (weyl_init += s_init);
  22. weyl += (x & 1) << i;
  23. }
  24. for (int i = 0; i < 128; i++)
  25. {
  26. x_init = (-(x_init & 1) & (x_init + ((x_init + 1) >> 1)) | ~- (x_init & 1) & (x_init >> 1)) ^ (weyl_init += s_init);
  27. s += (x & 1) << i;
  28. }
  29. return xws{x, weyl, s | 1 };
  30. }
  31.  
  32. struct xw { __uint128_t x, weyl; };
  33.  
  34. struct xw skip(__uint128_t x, __uint128_t weyl, const __uint128_t s)
  35. {
  36. for (int i = 0; i < 128; i++)
  37. {
  38. x = (-(x & 1) & (x + ((x + 1) >> 1)) | ~- (x & 1) & (x >> 1)) ^ (weyl += s);
  39. }
  40. return xw{ x, weyl };
  41. }
  42.  
  43. __uint128_t next(__uint128_t& x, __uint128_t& weyl, const __uint128_t& s)
  44. {
  45. __uint128_t v = 0;
  46.  
  47. for (int i = 0; i < 128; i++)
  48. {
  49. x = (-(x & 1) & (x + ((x + 1) >> 1)) | ~-(x & 1) & (x >> 1)) ^ (weyl += s);
  50. v += (x & 1) << i;
  51. }
  52. return v;
  53. }
  54.  
  55. int main()
  56. {
  57. const __uint128_t key = 1234321; //it has to be odd
  58. const __uint128_t x_init = key, weyl_init = key, s_init = key;
  59.  
  60. xws initialization = initializer(x_init, weyl_init, s_init);
  61. __uint128_t x = initialization.x;
  62. __uint128_t weyl = initialization.weyl;
  63. __uint128_t s = initialization.s;
  64.  
  65. xw skipping = skip(x, weyl, s);
  66. x = skipping.x;
  67. weyl = skipping.weyl;
  68.  
  69. uint64_t result = 0;
  70.  
  71. for(int i=0; i<25; i++)
  72. {
  73.  
  74. result = next(x, weyl, s);
  75.  
  76. }
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment