aboutsummaryrefslogtreecommitdiffstats
path: root/benchmark.c
blob: eec02baa58a8b458d4deecdf95d8ef7ab15d8879 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "libkeccak.h"

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>


#ifndef MESSAGE_FILE
# define MESSAGE_FILE      "benchfile"
#endif
#ifndef MESSAGE_LEN
# define MESSAGE_LEN       50000
#endif


#ifndef L
# define L                 6
#endif
#ifndef STATE
# define STATE             (25 << (L))
#endif

#ifndef BITRATE
# define BITRATE           (16 << (L))
#endif
#ifndef CAPACITY
# define CAPACITY          ((STATE) - (BITRATE))
#endif
#ifndef OUTPUT
# define OUTPUT            ((BITRATE) / 2)
#endif

#ifndef UPDATE_RUNS
# define UPDATE_RUNS       100
#endif
#ifndef FAST_SQUEEZE_RUNS
# define FAST_SQUEEZE_RUNS 100
#endif
#ifndef SLOW_SQUEEZE_RUNS
# define SLOW_SQUEEZE_RUNS 100
#endif
#ifndef RERUNS
# define RERUNS            50
#endif



/**
 * Benchmark, will print the number of nanoseconds
 * spent with hashing algorithms and representation
 * conversion from binary to hexadecimal. The latter
 * can be compiled out by compiling with -DIGNORE_BEHEXING.
 * 
 * @return  Zero on success, 1 on error
 */
int
main(void)
{
	char message[MESSAGE_LEN];
	struct libkeccak_spec spec;
	struct libkeccak_state state;
	char hashsum[OUTPUT / 8];
#ifndef IGNORE_BEHEXING
	char hexsum[OUTPUT / 8 * 2 + 1];
#endif
	struct timespec start, end;
	long int i, r;
	int fd;
	ssize_t got;
	size_t ptr;

	/* Fill message with content from the file. */
	fd = open(MESSAGE_FILE, O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}
	for (ptr = 0; ptr < MESSAGE_LEN; ptr += (size_t)got) {
		got = read(fd, message, MESSAGE_LEN - ptr);
		if (got <= 0) {
			perror("read");
			close(fd);
			return 1;
		}
	}
	close(fd);

	/* Initialise state. */
	spec.bitrate = BITRATE;
	spec.capacity = CAPACITY;
	spec.output = OUTPUT;
	if (libkeccak_state_initialise(&state, &spec)) {
		perror("libkeccak_state_initialise");
		return 1;
	}

	/* Get start-time. */
	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) < 0) {
		perror("clock_gettime");
		return 1;
	}

	/* Run benchmarking loop. */
	for (r = 0; r < RERUNS; r++) {
		/* Updates. */
#if UPDATE_RUNS > 0
		for (i = 0; i < UPDATE_RUNS; i++) {
			if (libkeccak_fast_update(&state, message, MESSAGE_LEN) < 0) {
				perror("libkeccak_update");
				return 1;
			}
		}
#endif

		/* Digest. */
		if (libkeccak_fast_digest(&state, NULL, 0, 0, NULL, hashsum) < 0) {
			perror("libkeccak_digest");
			return 1;
		}
#ifndef IGNORE_BEHEXING
		libkeccak_behex_lower(hexsum, hashsum, OUTPUT / 8);
#endif

		/* Fast squeezes. */
#if FAST_SQUEEZE_RUNS > 0
		libkeccak_fast_squeeze(&state, FAST_SQUEEZE_RUNS);
#endif

		/* Slow squeezes. */
#if SLOW_SQUEEZE_RUNS > 0
		for (i = 0; i < SLOW_SQUEEZE_RUNS; i++) {
			libkeccak_squeeze(&state, hashsum);
# ifndef IGNORE_BEHEXING
			libkeccak_behex_lower(hexsum, hashsum, OUTPUT / 8);
# endif
		}
#endif
	}

	/* Get end-time. */
	if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0) {
		perror("clock_gettime");
		return -1;
	}

	/* Print execution-time. */
	end.tv_sec -= start.tv_sec;
	end.tv_nsec -= start.tv_nsec;
	if (end.tv_nsec < 0) {
		end.tv_sec--;
		end.tv_nsec += 1000000000L;
	}
	printf("%03li%09li\n", (long)(end.tv_sec), end.tv_nsec);

	/* Release resources and exit. */
	libkeccak_state_fast_destroy(&state);
	return 0;

#if (UPDATE_RUNS == 0) && (SLOW_SQUEEZE_RUNS == 0)
	(void) i;
#endif
}