0
int main(){
  double one, zero, bias;
  double max = -2.0;
  string plaintext, ciphertext, firstFourStr, lastFourStr;
  bitset<16> V;
  bitset<16> base("0000110000001011");
  bitset<16> targetKey;
  for(int k=0; k<16; k++){
    ifstream plaintexts ("plaintexts.txt");
    ifstream ciphertexts ("ciphertext07.txt");
    bitset<12> tmpBs(k);
    bitset<16> add(tmpBs.to_string()+"0000");
    bitset<16> guessedKey1(add^base);
    for(int j=0; j<16; j++){
      bitset<4> tmpBs2(j);
      bitset<16> add2(tmpBs2.to_string()+"000000000000");
      bitset<16> guessedKey(guessedKey1^add2);
      one = zero = 0.0;
      for(int i=0; i<20000; i++){
        getline(plaintexts, plaintext);
        getline(ciphertexts, ciphertext);
        bitset<16> pTxt(plaintext);
        bitset<16> cTxt(ciphertext);
        V = guessedKey^cTxt;
        bitset<4> firstFour((V.to_string()).substr(0,4));
        bitset<4> secondFour((V.to_string()).substr(4,4));
        bitset<4> thirdFour((V.to_string()).substr(8,4));
        bitset<4> lastFour((V.to_string()).substr(12,4));
        bitset<16> U(sBoxInverse(firstFour)+sBoxInverse(secondFour)+sBoxInverse(thirdFour)+sBoxInverse(lastFour));
        if((U[2]^U[6]^U[10]^U[14]^pTxt[4]^pTxt[7]^pTxt[12]^pTxt[15]) == 0) zero++;
        else one++;
      }
      plaintexts.close();
      ciphertexts.close();
      bias = zero/(zero+one)-0.5;
      if(bias < 0) bias *= -1;
      if(max <= bias){
        max = bias;
        targetKey = guessedKey;
      }
      cout << bias << endl;
    }
  }
  cout << targetKey << ": " << max << endl;
}

I was doing my cryptography assignment and this is the program I wrote to get the key. But it runs pretty slow. I tried to replace some lines of code with simpler ones to check if operations like to_string() or set() caused the problem but it seems that they have nothing to do with the problem. So what causes the program to run slow?

2
  • If you are on windows, you can use Visual Studio Profile Analyzer (or something like that, don't recall how it's called). If you are on linux you can use valgrind with callgrind. There are profilers and will show you where your code spends most time.
    – bolov
    Commented Feb 1, 2015 at 17:38
  • 3
    my feeling is that the IO here is the most taxing here. I mean you have 16 x 16 x 20.000 x 2 = 10.240.000 calls to getline.
    – bolov
    Commented Feb 1, 2015 at 17:41

1 Answer 1

5

Probably the thing which is slowest in that code is not the std::bitsets, but rather the extensive (and unnecessary) use of std::strings to manipulate them. For example:

 bitset<16> add2(tmpBs2.to_string()+"000000000000");

requires creating a std::string from a bitset, making a new std::string by concatenating that with a constant C string, and then reinterpreting the result as a std::bitset.

You'd be much better off manipulating the std::bitsets as unsigned integers and using bitwise operators and shifts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.