[OT] Money for nothing, and your bit flips for free?

Steve smorrey at gmail.com
Wed Feb 7 15:25:50 MST 2007


I wanted to do a really simple obfuscation program, because I have
some plaintext data I don't want going out in plaintext.

All it does is bit flip a string of text.

Whats really interesting is the bitflip operations appear to be
occuring (free of charge) as it were.  If I compile with the -O3 flag
in GCC.

Look at these results...

steve at steve-laptop:~/myapp/client$ g++ flip.cpp
steve at steve-laptop:~/myapp/client$ ./a.out "Hello World" 1000000
Started: 0
Ended: 20000
Performed 1000000 bit flips on a 4 character string in 20000 cycles

steve at steve-laptop:~/myapp/client$ g++ -O2 flip.cpp
steve at steve-laptop:~/myapp/client$ ./a.out "Hello World" 1000000
Started: 0
Ended: 0
Performed 1000000 bit flips on a 4 character string in 0 cycles

steve at steve-laptop:~/myapp/client$ g++ -O3 flip.cpp
steve at steve-laptop:~/myapp/client$ ./a.out "Hello World" 1000000
Started: 0
Ended: 0
Performed 1000000 bit flips on a 4 character string in 0 cycles

Something tells me the results are not quite right here.
For one thing, it's telling me I have a 4 character string, which is wrong,
"Hello World" is 12 characters.

Anyways after looking closely, I realized my code which was using a
sizeof(argv[1]) was in fact only flipping the first character which is
evidently 4 bytes wide.

I decided to use a std::string instead of assigning it to a char* and
I got more realistic results.

steve at steve-laptop:~/myapp/client$ g++ flip.cpp
steve at steve-laptop:~/myapp/client$ ./a.out "Hello World" 1000000
Started: 0
Ended: 460000
Performed 1000000 bit flips on a 11 byte string in 460000 cycles

steve at steve-laptop:~/myapp/client$ g++ -O3 flip.cpp
steve at steve-laptop:~/myapp/client$ ./a.out "Hello World" 1000000
Started: 0
Ended: 70000
Performed 1000000 bit flips on a 11 byte string in 70000 cycles


What seems strange though is optimization aside, it still looks to me
like I'm getting something for nothing here.  I mean 1,000,000 * 11
bit flips should take at least 1,000,000 operations to do and in fact
my common sense tells me that I should be looking at 11,000,000 at a
minimum.

Anyways here's the code for the application if anyone is interested in
trying to replicate this.

#include <iostream>
#include <time.h>

int main(int agc, char *argv[]){
	int x = 0,y = 0, count = atoi(argv[2]);
	std::string s = argv[1];
	clock_t start = clock();

	while(x++ < count){
		//std::cout << x << std::endl;
		while(y < s.length()){
			~s[y++];
		}
		y = 0;
	}
	clock_t end = clock();
	std::cout << "Started: " << start << "\nEnded: " << end
	<< "\nPerformed "<< count << " bit flips on a " << s.length()
	<< " byte string in " << difftime(end,start) << " cycles" << std::endl;
	return(0);
}

Thanks in advance for any info!
Regards,



More information about the PLUG mailing list