21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

For many different reasons, it can be fairly difficult to get timing numbers that are<br />

completely accurate. Often, internal clocks that the programmer can read are somewhat<br />

asynchronous from the core processor clock. More significantly, there’s often<br />

significant overhead that can be included in timing results, such as the cost of context<br />

switches and sometimes timing overhead.<br />

Some CPUs, such as AMD’s Athlon, are advertised such that the<br />

actual clock speed is not obvious. For example, the Athlon 2000 runs<br />

at roughly 1666 MHz, significantly less than the 2000 MHz one might<br />

suspect.<br />

Generally, you’ll want to find out how quickly a primitive or algorithm can process a<br />

fixed amount of data, and you’d like to know how well it does that in a real-world<br />

environment. For that reason, you generally shouldn’t worry much about subtracting<br />

out things that aren’t relevant to the underlying algorithm, such as context<br />

switches and procedure call overhead. Instead, we recommend running the algorithm<br />

many times and averaging the total time to give a good indication of overall<br />

performance.<br />

In the following sections we’ll discuss timing basics, then look at the particulars of<br />

timing cryptographic code.<br />

Timing basics<br />

You need to be able to record the current time with as much precision as possible.<br />

On a modern x86 machine, it’s somewhat common to see people using inline assembly<br />

to call the RDTSC instruction directly, which returns the number of clock cycles<br />

since boot as a 64-bit value. For example, here’s some inline assembly for GCC on<br />

32-bit x86 platforms (only!) that reads the counter, placing it into a 64-bit unsigned<br />

long long that you pass in by address:<br />

#define current_stamp(a) asm volatile("rdtsc" : "=a"(((unsigned int *)(a))[0]),\<br />

"=d"(((unsigned int *)a)[1]))<br />

The following program uses the above macro to return the number of ticks since<br />

boot:<br />

#include <br />

int main(int argc, char *argv[ ]) {<br />

spc_uint64_t x;<br />

current_stamp(&x);<br />

printf("%lld ticks since boot (when I read the clock).\n", x);<br />

return 0;<br />

}<br />

RDTSC is fairly accurate, although processor pipelining issues can lead to this technique’s<br />

being a few cycles off, but this is rarely a big deal.<br />

Timing Cryptographic Primitives | 151<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!