timing_hash.c 9.8 KB
Newer Older
Enrico Pozzobon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
#ifdef __GNUC__
#ifndef __USE_GNU
#define __USE_GNU
#endif
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h> 
#endif
#include "crypto_hash.h"
#include "api.h"
#include "timing_hash.h"


#define MAX_MESSAGE_LENGTH (1UL << 16UL)
#define MAX_ASSOCIATED_DATA_LENGTH (1UL << 16UL)

#ifdef _MSC_VER
#define DUAL_CORE

#if defined( DUAL_CORE )
#  define WINDOWS_LEAN_AND_MEAN
#  include <windows.h>
#endif
#include <string.h>
#include <math.h>

#include <intrin.h>
#pragma intrinsic( __rdtsc )

__inline unsigned long long read_tsc(void)
{
	return __rdtsc();
}

#if defined( _M_IX86 )
#if _M_IX86 == 500
#define PROCESSOR   "Pentium"
#elif _M_IX86 == 600
#define PROCESSOR   "P2/P3/P4"
#else
#define PROCESSOR   ""
#endif
#elif defined( _M_X64 )
#define PROCESSOR   "AMD64/EMT64"
#else
#define PROCESSOR   ""
#endif

#if defined( _WIN64 )

#define CurrentProcessorNumber GetCurrentProcessorNumber

#else

unsigned long CurrentProcessorNumber(void)
{
    __asm
    {
        mov     eax,1
        cpuid
        shr     ebx,24
        mov     eax, ebx
    }
}

#endif

void setCPUaffinity()
{
#if defined( DUAL_CORE ) && defined( _WIN32 )
    HANDLE ph;
    DWORD_PTR afp;
    DWORD_PTR afs;
    ph = GetCurrentProcess();
    if(GetProcessAffinityMask(ph, &afp, &afs))
    {
        afp &= (1 << CurrentProcessorNumber());
        if(!SetProcessAffinityMask(ph, afp))
        {
            printf("Couldn't set Process Affinity Mask\n\n");
        }
    }
    else
    {
        printf("Couldn't get Process Affinity Mask\n\n");
    }
#endif
}

#else
#ifdef __GNUC__
#include <sched.h>
#include <sys/resource.h>
#include <x86intrin.h>
inline unsigned long long read_tsc(void)
{
#if defined(__i386__)
	unsigned long long cycles;
	__asm__ volatile (".byte 0x0f, 0x31" : "=A"(cycles));
	return cycles;
#else
#if defined(__x86_64__)
	unsigned int hi, lo;
	__asm__ volatile ("rdtsc" : "=a"(lo), "=d"(hi));
	return (((unsigned long long)lo) | ((unsigned long long)(hi)<<32));
#else
#error "Unsupported architecture for counting cycles"
#endif
#endif
}

void setCPUaffinity()
{
	cpu_set_t cpu_mask;
	CPU_SET(0x1, &cpu_mask);
	if(sched_setaffinity(getpid(), sizeof(cpu_mask), &cpu_mask) == -1 )
	{
		printf("Impossible to set CPU affinity...\n");
	}
}
#endif
#endif

#define RAND(a,b) (((a = 36969 * (a & 65535) + (a >> 16)) << 16) + \
	(b = 18000 * (b & 65535) + (b >> 16))  )

void block_rndfill(unsigned char *buf, const size_t len)
{
	static unsigned long a[2], mt = 1, count = 4;
	static unsigned char r[4];
	size_t                  i;

	if(mt) { mt = 0; *(unsigned long long*)a = read_tsc(); }

	for(i = 0; i < len; ++i)
	{
		if(count == 4)
		{
			*(unsigned int*)r = RAND(a[0], a[1]);
			count = 0;
		}

		buf[i] = r[count++];
	}
}

const int loops = 100;  

const int loops_longMessage = 1;

#define SAMPLE1  100
#define SAMPLE2 1000

#define TRUE  1
#define FALSE 0

int timeBase(double *av, double *sig)
{
	volatile int                 i, tol, lcnt, sam_cnt;
	volatile double              cy, av1, sig1;

	tol = 10; lcnt = sam_cnt = 0;
	while(!sam_cnt)
	{
		av1 = sig1 = 0.0;

		for(i = 0; i < SAMPLE1; ++i)
		{
			cy = (volatile double)read_tsc();
			cy = (volatile double)read_tsc() - cy;

			av1 += cy;
			sig1 += cy * cy;
		}

		av1 /= SAMPLE1;
		sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
		sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);

		*av = *sig = 0.0;
		for(i = 0; i < SAMPLE2; ++i)
		{
			cy = (volatile double)read_tsc();
			cy = (volatile double)read_tsc() - cy;

			if(cy > av1 - sig1 && cy < av1 + sig1)
			{
				*av += cy;
				*sig += cy * cy;
				sam_cnt++;
			}
		}

		if(10 * sam_cnt > 9 * SAMPLE2)
		{
			*av /= sam_cnt;
			*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);

			if(*sig > (tol / 100.0) * *av)
				sam_cnt = 0;
		}
		else
		{
			if(lcnt++ == 10)
			{
				lcnt = 0; tol += 5;
				if(tol > 30)
					return FALSE;
			}
			sam_cnt = 0;
		}
	}
	return TRUE;
}

int time_enc16(double *av, double *sig, unsigned long long dataLengthInBytes)
{
	volatile int       i, tol, lcnt, sam_cnt;
	volatile double    cy, av1, sig1;
	unsigned char      pt[4][MAX_MESSAGE_LENGTH];
	unsigned char      digest[4][CRYPTO_BYTES];

	for (i = 0; i < 4; i++)
	{
		block_rndfill(pt[i], dataLengthInBytes);
	}

	tol = 10; lcnt = sam_cnt = 0;
	while(!sam_cnt)
	{
		av1 = sig1 = 0.0;

		for(i = 0; i < SAMPLE1; ++i)
		{
			cy = (double)read_tsc();
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			cy = (double)read_tsc() - cy;

			av1 += cy;
			sig1 += cy * cy;
		}

		av1 /= SAMPLE1;
		sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
		sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);

		*av = *sig = 0.0;
		for(i = 0; i < SAMPLE2; ++i)
		{
			cy = (double)read_tsc();
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			crypto_hash(digest[0], pt[0], dataLengthInBytes);
			crypto_hash(digest[1], pt[1], dataLengthInBytes);
			crypto_hash(digest[2], pt[2], dataLengthInBytes);
			crypto_hash(digest[3], pt[3], dataLengthInBytes);
			cy = (double)read_tsc() - cy;

			if(cy > av1 - sig1 && cy < av1 + sig1)
			{
				*av += cy;
				*sig += cy * cy;
				sam_cnt++;
			}
		}

		if(10 * sam_cnt > 9 * SAMPLE2)
		{
			*av /= sam_cnt;
			*sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
			if(*sig > (tol / 100.0) * *av)
				sam_cnt = 0;
		}
		else
		{
			if(lcnt++ == 10)
			{
				lcnt = 0; tol += 5;
				if(tol > 30)
				{
					return FALSE;
				}
			}
			sam_cnt = 0;
		}
	}
	return TRUE;
}

double enc_cycles(unsigned long long dataLengthInBytes)
{
	volatile double cy1, cy2, c1 = -1, c2 = -1;
	volatile int i;
	unsigned char      pt[MAX_MESSAGE_LENGTH];
    unsigned char      digest[CRYPTO_BYTES];

	block_rndfill(pt, dataLengthInBytes);

    c1 = c2 = 0xffffffffffffffff;
	crypto_hash(digest, pt, dataLengthInBytes);

	for(i = 0; i < loops; ++i)
	{
		cy1 = (volatile double)read_tsc();
		crypto_hash(digest, pt, dataLengthInBytes);
		cy1 = (volatile double)read_tsc() - cy1;

		cy2 = (volatile double)read_tsc();
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		crypto_hash(digest, pt, dataLengthInBytes);
		cy2 = (volatile double)read_tsc() - cy2;

		if(i > (loops / 10))
		{
			c1 = (c1 < cy1 ? c1 : cy1);
			c2 = (c2 < cy2 ? c2 : cy2);
		}
	}
	return ((c2 - c1) + 4.0) / 8.0;
}

static unsigned long ml[] = { 
	0, 
	1UL << 4UL,
	1UL << 5UL,
	1UL << 6UL,
	1UL << 7UL,
	1UL << 8UL,
	1UL << 9UL,
	1UL << 10UL,
	//1UL << 11UL,
	//1UL << 12UL,
	//1UL << 13UL,
	//1UL << 14UL,
	//1UL << 15UL,
	//1UL << 16UL,
	//1UL << 17UL,
	//1UL << 18UL,
	//1UL << 19UL,
	//1UL << 20UL
	};

static double et, dt;

#define MAX_FILE_NAME				256

void timing()
{
	FILE *fp;
	char fileName[MAX_FILE_NAME];
	double   a0, av, sig;
	int ki, i, w;
	unsigned long long pi;
	unsigned long long ai;
	unsigned long long di;

	setCPUaffinity();
	sprintf(fileName, "Hash_Timing_Min_%d.csv", (CRYPTO_BYTES * 8));

	if ((fp = fopen(fileName, "a")) == NULL) {
		fprintf(stderr, "Couldn't open <%s> for write\n", fileName);
		return -1;
	}

	fprintf(fp, "Hash Timing (cycles/byte)\n");
	fprintf(fp, "%20s,", "P_len");

	for (pi = 0; pi < sizeof(ml)/sizeof(unsigned long); pi++)
	{
		fprintf(fp, "%10d,", ml[pi]);
	}
	fprintf(fp, "\n");

	fprintf(fp, "%20s,", "Speed");
	for (pi = 0; pi < sizeof(ml)/sizeof(unsigned long); pi++)
	{		
		et = enc_cycles(ml[pi]);
		if (ml[pi] == 0)
		{
			av = et;
		}
		else
		{
			av = et / (ml[pi]);
		}
		fprintf(fp, "%10.2f,", av);
	}
	fprintf(fp, "\n");
	fclose(fp);

	sprintf(fileName, "Hash_Timing_Avg_%d.csv", (CRYPTO_BYTES * 8));
	if ((fp = fopen(fileName, "a")) == NULL) {
		fprintf(stderr, "Couldn't open <%s> for write\n", fileName);
		return -1;
	}

	while (timeBase(&a0, &sig) != TRUE) {}
	fprintf(fp, "Hash Timing (cycles/byte)\n");
	fprintf(fp, "%20s,", "P_len");
	for (pi = 0; pi < sizeof(ml)/sizeof(unsigned long); pi++)
	{
		fprintf(fp, "%20d,", ml[pi]);
	}
	fprintf(fp, "\n");

	fprintf(fp, "%20s,", "Speed");
	for (pi = 0; pi < sizeof(ml)/sizeof(unsigned long); pi++)
	{
		while (time_enc16(&av, &sig, ml[pi]) != TRUE) {}
		sig *= 100.0 / av;
		if (ml[pi] == 0)
		{
			av = (int)(10.0 * (av - a0) / (16.0)) / 10.0;
			sig = (int)(10 * sig) / 10.0;
		}
		else
		{
			av = (int)(10.0 * (av - a0) / (16.0 * (ml[pi]))) / 10.0;
			sig = (int)(10 * sig) / 10.0;
		}
		fprintf(fp, "%10.2f(%7.2f\%),", av, sig);
	}
	fprintf(fp, "\n");
	fclose(fp);
}

int main()
{
	timing();
	return 0;
}