-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.c
112 lines (88 loc) · 2.08 KB
/
main.c
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
/*
Implementation of SHA256 for hashing single blocks or 8x single blocks in
parallel using AVX2.
*/
#include "stdio.h"
#include "stdlib.h"
#include <string.h>
#include "sha256avx.h"
#include "sha256.h"
#include "timing.h"
void checkTestvectorAvx() {
unsigned char *in, *out;
int i, j;
in = malloc(64 * 8);
out = malloc(32 * 8);
memset(in, 0, 64 * 8);
// Test Vector "abc" for all 8 blocks
for (i = 0; i < 8; i++) {
in[64 * i + 0] = 0x80;
in[64 * i + 1] = 0x63;
in[64 * i + 2] = 0x62;
in[64 * i + 3] = 0x61;
in[64 * i + 60] = 0x18;
}
sha256_8x(out, in);
for (i = 0; i < 32; i++) {
for (j = 0; j < 8; j++) {
if (out[32 * j + i] != testvector_abc[i]) {
printf("ERROR SHA256AVX: Output does not match with testvector.\n");
}
}
}
free(in);
free(out);
}
void checkTestvector() {
//TODO:
unsigned char *in, *out;
int i;
in = malloc(64);
out = malloc(32);
memset(in, 0, 64);
// Test Vector "abc" from
// http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
in[0] = 0x80;
in[1] = 0x63;
in[2] = 0x62;
in[3] = 0x61;
in[60] = 0x18;
sha256(out, in);
for (i = 0; i < 32; i++) {
if (out[i] != testvector_abc[i]) {
printf("ERROR SHA256: Output does not match with testvector.\n");
}
}
free(in);
free(out);
}
int main() {
unsigned char *in, *out;
unsigned long long inlen;
unsigned long long timer = 0;
double timings[NUM_TIMINGS];
int i, j;
checkTestvector();
checkTestvectorAvx();
srand(0);
inlen = 64 * 8; // Hash 8 * 512 bits
in = malloc(inlen);
out = malloc(inlen / 2);
for (i = -100; i < NUM_TIMINGS; i++) {
//Get random input
for (j = 0; j < inlen; j++) {
in[j] = rand() & 0xff;
}
timer = startTimer();
sha256_8x(out, in);
timer = endTimer() - timer;
if (i >= 0 && i < NUM_TIMINGS) {
timings[i] = ((double)timer) / inlen;
}
}
//Get Median
qsort(timings, NUM_TIMINGS, sizeof(double), compareDouble);
printf("Sha256_8x: %f cycles per byte\n", timings[NUM_TIMINGS / 2]);
free(out);
free(in);
}