Advertisement
Guest User

Untitled

a guest
May 27th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <vector>
  6. #include "cstdlib"
  7. #include <map>
  8. #include <cmath>
  9. #include <stdint.h>
  10. #include <iostream>
  11. #include <cstdlib>
  12. #include <ctime>
  13. #include <windows.h>
  14. #define R operator &(T a, S b);
  15. #define R operator ^(T a, S b);
  16. #define SizeOfVector 4
  17. #define CountOfFunc (pow(2, SizeOfVector))
  18. using namespace std;
  19. uint32_t mask = CountOfFunc - 1;
  20.  
  21. uint32_t SBOXGeneral[] =
  22. { 0x06, 0x00,0x04,0x0d,0x09, 0x0a, 0x0f,0x02,0x05, 0x08, 0x0c,0x0e,0x01, 0x07,   0x03,     0x0b };
  23. uint32_t SBOX[] =
  24. { 0x00, 0x01, 0x02, 0x0c, 0x09, 0x0d, 0x03, 0x0f, 0x05, 0x0e, 0x0a, 0x0b, 0x04, 0x07, 0x06, 0x08 };
  25. int multiVect(uint32_t a) {
  26.     uint32_t i;
  27.     for (i = 0; a; a >>= 1) {
  28.         i += a & 1;
  29.     }
  30.     if (i % 2 == 0) {
  31.         return 0;
  32.     }
  33.     else {
  34.         return 1;
  35.     }
  36. }
  37.  
  38.  
  39. double getPropabilityForLinear(uint32_t alpha, uint32_t beta) {
  40.     int count = 0;
  41.     for (uint32_t x = 0x00; x < CountOfFunc; x++) {
  42.         if (multiVect(alpha & x & mask) == multiVect(beta & SBOX[x] & mask)) {
  43.             count++;
  44.         }
  45.     }
  46.     return  (double)count;
  47. }
  48.  
  49.  
  50. double linearСharacteristic() {
  51.     uint32_t bestAlpha = 0;
  52.     uint32_t bestBeta = 0;
  53.     double bestPropability = 0;
  54.     double temp = 0;
  55.     for (uint32_t alpha = 0x1; alpha < CountOfFunc; alpha++) {
  56.         for (uint32_t beta = 0x1; beta < CountOfFunc; beta++) {
  57.             temp = ((getPropabilityForLinear(alpha & mask, beta & mask)));
  58.             cout << temp / CountOfFunc << " ";
  59.             if (abs(temp) >= bestPropability) {
  60.                 bestPropability = abs(temp);
  61.                 bestAlpha = alpha;
  62.                 bestBeta = beta;
  63.             }
  64.         }
  65.         cout << endl;
  66.     }
  67.  
  68.     return bestPropability * pow(2, SizeOfVector - 1);
  69. }
  70.  
  71. int main() {
  72.     linearСharacteristic();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement