Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. typedef std::vector<std::vector<std::vector<bool>>> state;
  2.  
  3. half_byte hexa_to_binary(char c) {
  4. static std::unordered_map<char, half_byte> mp;
  5. mp['0'] = half_byte("0000");
  6. mp['1'] = half_byte("0001");
  7. mp['2'] = half_byte("0010");
  8. mp['3'] = half_byte("0011");
  9. mp['4'] = half_byte("0100");
  10. mp['5'] = half_byte("0101");
  11. mp['6'] = half_byte("0110");
  12. mp['7'] = half_byte("0111");
  13. mp['8'] = half_byte("1000");
  14. mp['9'] = half_byte("1001");
  15. mp['A'] = half_byte("1010");
  16. mp['B'] = half_byte("1011");
  17. mp['C'] = half_byte("1100");
  18. mp['D'] = half_byte("1101");
  19. mp['E'] = half_byte("1110");
  20. mp['F'] = half_byte("1111");
  21. return mp[c];
  22. }
  23. std::vector<bool> string_vec_of_bool(std::string s) {
  24. s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end());
  25. // std::cout << s << 'n';
  26. int vec_size = s.size() * 4;
  27. std::vector<bool> vec(vec_size);
  28. int j = 0;
  29. for(auto&& c : s) {
  30. auto hb = hexa_to_binary(c);
  31. for(int i = 3; i >= 0; i--) {
  32. vec[j] = hb[i];
  33. j++;
  34. }
  35. }
  36. return vec;
  37. }
  38.  
  39. state convert_string_to_state(std::vector<bool> s, int w) {
  40. state A(5, std::vector<std::vector<bool>>(5, std::vector<bool>(w)));
  41. for(int y = 0; y < 5; ++y) {
  42. int y_slice = to_index(y);
  43. for(int x = 0; x < 5; ++x) {
  44. int x_slice = to_index(x);
  45. for(int z = 0; z < w; ++z) {
  46. //std::cout << x << " ---> " << x_slice << 'n';
  47. A[x_slice][y_slice][z] = s[w * (5 * y + x) + z];
  48. }
  49. }
  50. }
  51. return A;
  52. }
  53.  
  54. std::vector<std::vector<bool>> theta_C(const state& A, int w) {
  55. std::vector<std::vector<bool>> C(5, std::vector<bool>(w, false));
  56. for(int x = 0; x < 5; ++x) {
  57. int x_slice = to_index(x);
  58. for(int z = 0; z < w; ++z) {
  59. C[x_slice][z] = A[x][0][z] ^ A[x][1][z]
  60. ^ A[x][2][z] ^ A[x][3][z]
  61. ^ A[x][4][z];
  62. }
  63. }
  64. return C;
  65. }
  66.  
  67. std::vector<std::vector<bool>> theta_D(std::vector<std::vector<bool>> &C, int w) {
  68. std::vector<std::vector<bool>> D(5, std::vector<bool>(w, false));
  69. for(int x = 0; x < 5; ++x) {
  70. int x_slice = to_index(x);
  71. for(int z = 0; z < w; ++z) {
  72. int cx1 = (x - 1 + 5) % 5;
  73. int cx2 = (x + 1) % 5;
  74. int cz = (z - 1 + w) % w;
  75. D[x][z] = C[cx1][z] ^ C[cx2][cz];
  76. }
  77. }
  78. return D;
  79. }
  80.  
  81. state theta(state &A) {
  82. int w = A[0][0].size();
  83. auto C = theta_C(A, w);
  84. auto D = theta_D(C, w);
  85. state A_prime(5, std::vector<std::vector<bool>>(5, std::vector<bool>(w, false)));
  86. for(int x = 0; x < 5; ++x) {
  87. int x_slice = to_index(x);
  88. for(int y = 0; y < 5; ++y) {
  89. int y_slice = to_index(y);
  90. for(int z = 0; z < w; ++z) {
  91. A_prime[x][y][z] = A[x][y][z] ^ D[x][z];
  92. }
  93. }
  94. }
  95. return A_prime;
  96. }
  97.  
  98. int to_index(int slice_index) { return (slice_index + 2) % 5;}
  99.  
  100. std::vector<bool> convert_state_to_string(state& A) {
  101. int w = A[0][0].size();
  102. int b = slize_size * w;
  103. std::vector<bool> S(b);
  104. int index = 0;
  105. for(int y = 0; y < 5; ++y) {
  106. int y_slice = to_index(y);
  107. for(int x = 0; x < 5; ++x) {
  108. int x_slice = to_index(x);
  109. for(int z = 0; z < w; z++) {
  110. S[index++] = A[x_slice][y_slice][z];
  111. }
  112. }
  113. }
  114. return S;
  115. }
  116.  
  117. std::string binary_to_hexa(std::string b) {
  118. static std::unordered_map<std::string, std::string> mp;
  119. mp["0000"] = "0";
  120. mp["0001"] = "1";
  121. mp["0010"] = "2";
  122. mp["0011"] = "3";
  123. mp["0100"] = "4";
  124. mp["0101"] = "5";
  125. mp["0110"] = "6";
  126. mp["0111"] = "7";
  127. mp["1000"] = "8";
  128. mp["1001"] = "9";
  129. mp["1010"] = "A";
  130. mp["1011"] = "B";
  131. mp["1100"] = "C";
  132. mp["1101"] = "D";
  133. mp["1110"] = "E";
  134. mp["1111"] = "F";
  135. return mp[b];
  136. }
  137.  
  138. std::string vec_of_bool_string(std::vector<bool> &v) {
  139. std::ostringstream o;
  140. std::ostringstream aux;
  141. std::bitset<4> hb;
  142. for(int i = 0; i < v.size(); i += 4) {
  143. aux << v[i] << v[i + 1] << v[i + 2] << v[i + 3];
  144. o << binary_to_hexa(aux.str());
  145. aux.str(std::string());
  146. }
  147. return o.str();
  148. }
Add Comment
Please, Sign In to add comment