Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef std::vector<std::vector<std::vector<bool>>> state;
- half_byte hexa_to_binary(char c) {
- static std::unordered_map<char, half_byte> mp;
- mp['0'] = half_byte("0000");
- mp['1'] = half_byte("0001");
- mp['2'] = half_byte("0010");
- mp['3'] = half_byte("0011");
- mp['4'] = half_byte("0100");
- mp['5'] = half_byte("0101");
- mp['6'] = half_byte("0110");
- mp['7'] = half_byte("0111");
- mp['8'] = half_byte("1000");
- mp['9'] = half_byte("1001");
- mp['A'] = half_byte("1010");
- mp['B'] = half_byte("1011");
- mp['C'] = half_byte("1100");
- mp['D'] = half_byte("1101");
- mp['E'] = half_byte("1110");
- mp['F'] = half_byte("1111");
- return mp[c];
- }
- std::vector<bool> string_vec_of_bool(std::string s) {
- s.erase(std::remove_if(s.begin(), s.end(), isspace), s.end());
- // std::cout << s << 'n';
- int vec_size = s.size() * 4;
- std::vector<bool> vec(vec_size);
- int j = 0;
- for(auto&& c : s) {
- auto hb = hexa_to_binary(c);
- for(int i = 3; i >= 0; i--) {
- vec[j] = hb[i];
- j++;
- }
- }
- return vec;
- }
- state convert_string_to_state(std::vector<bool> s, int w) {
- state A(5, std::vector<std::vector<bool>>(5, std::vector<bool>(w)));
- for(int y = 0; y < 5; ++y) {
- int y_slice = to_index(y);
- for(int x = 0; x < 5; ++x) {
- int x_slice = to_index(x);
- for(int z = 0; z < w; ++z) {
- //std::cout << x << " ---> " << x_slice << 'n';
- A[x_slice][y_slice][z] = s[w * (5 * y + x) + z];
- }
- }
- }
- return A;
- }
- std::vector<std::vector<bool>> theta_C(const state& A, int w) {
- std::vector<std::vector<bool>> C(5, std::vector<bool>(w, false));
- for(int x = 0; x < 5; ++x) {
- int x_slice = to_index(x);
- for(int z = 0; z < w; ++z) {
- C[x_slice][z] = A[x][0][z] ^ A[x][1][z]
- ^ A[x][2][z] ^ A[x][3][z]
- ^ A[x][4][z];
- }
- }
- return C;
- }
- std::vector<std::vector<bool>> theta_D(std::vector<std::vector<bool>> &C, int w) {
- std::vector<std::vector<bool>> D(5, std::vector<bool>(w, false));
- for(int x = 0; x < 5; ++x) {
- int x_slice = to_index(x);
- for(int z = 0; z < w; ++z) {
- int cx1 = (x - 1 + 5) % 5;
- int cx2 = (x + 1) % 5;
- int cz = (z - 1 + w) % w;
- D[x][z] = C[cx1][z] ^ C[cx2][cz];
- }
- }
- return D;
- }
- state theta(state &A) {
- int w = A[0][0].size();
- auto C = theta_C(A, w);
- auto D = theta_D(C, w);
- state A_prime(5, std::vector<std::vector<bool>>(5, std::vector<bool>(w, false)));
- for(int x = 0; x < 5; ++x) {
- int x_slice = to_index(x);
- for(int y = 0; y < 5; ++y) {
- int y_slice = to_index(y);
- for(int z = 0; z < w; ++z) {
- A_prime[x][y][z] = A[x][y][z] ^ D[x][z];
- }
- }
- }
- return A_prime;
- }
- int to_index(int slice_index) { return (slice_index + 2) % 5;}
- std::vector<bool> convert_state_to_string(state& A) {
- int w = A[0][0].size();
- int b = slize_size * w;
- std::vector<bool> S(b);
- int index = 0;
- for(int y = 0; y < 5; ++y) {
- int y_slice = to_index(y);
- for(int x = 0; x < 5; ++x) {
- int x_slice = to_index(x);
- for(int z = 0; z < w; z++) {
- S[index++] = A[x_slice][y_slice][z];
- }
- }
- }
- return S;
- }
- std::string binary_to_hexa(std::string b) {
- static std::unordered_map<std::string, std::string> mp;
- mp["0000"] = "0";
- mp["0001"] = "1";
- mp["0010"] = "2";
- mp["0011"] = "3";
- mp["0100"] = "4";
- mp["0101"] = "5";
- mp["0110"] = "6";
- mp["0111"] = "7";
- mp["1000"] = "8";
- mp["1001"] = "9";
- mp["1010"] = "A";
- mp["1011"] = "B";
- mp["1100"] = "C";
- mp["1101"] = "D";
- mp["1110"] = "E";
- mp["1111"] = "F";
- return mp[b];
- }
- std::string vec_of_bool_string(std::vector<bool> &v) {
- std::ostringstream o;
- std::ostringstream aux;
- std::bitset<4> hb;
- for(int i = 0; i < v.size(); i += 4) {
- aux << v[i] << v[i + 1] << v[i + 2] << v[i + 3];
- o << binary_to_hexa(aux.str());
- aux.str(std::string());
- }
- return o.str();
- }
Add Comment
Please, Sign In to add comment