Skip to content

Commit f091605

Browse files
committed
sha256 loop
1 parent 88268e8 commit f091605

3 files changed

Lines changed: 658 additions & 0 deletions

File tree

c_src/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
sha256_avx1:*.asm *.c
2+
gcc -O4 -o $@ main.c $@.o
3+
4+
sha256_sse4:*.asm *.c
5+
gcc -O4 -Dsha256=sha256_sse4 -o $@ main.c $@.o
6+
7+
%.o:%.asm
8+
yasm -f x64 -f elf64 -X gnu -g dwarf2 -D LINUX -o $@ $^

c_src/main.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <assert.h>
4+
#include <sys/time.h>
5+
6+
#ifndef sha256
7+
#define sha256 sha256_avx
8+
#endif
9+
10+
void sha256(void *input_data, uint32_t digest[8], uint32_t num_blks);
11+
int main(int argc, char *argv[]) {
12+
uint32_t ostate[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
13+
uint32_t state[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
14+
uint8_t block[32] = "AnatolyYakovenko11/2/201712pmPST";
15+
uint32_t *blkptr = (void*)block;
16+
uint64_t i;
17+
FILE *f = fopen(argv[1], "a+");
18+
struct timeval start, now;
19+
if(!fseek(f, -40, SEEK_END)) {
20+
assert(8 == fread(&i, 1, 8, f));
21+
i = i<<20;
22+
assert(32 == fread(block, 1, 32, f));
23+
assert(0 == fseek(f, 0, SEEK_END));
24+
}
25+
printf("block %04x%04x%04x%04x\n", blkptr[0], blkptr[1], blkptr[2], blkptr[3]);
26+
printf(" %04x%04x%04x%04x\n", blkptr[4], blkptr[5], blkptr[6], blkptr[7]);
27+
printf("state %04x%04x%04x%04x\n", state[0], state[1], state[2], state[3]);
28+
printf(" %04x%04x%04x%04x\n", state[4], state[5], state[6], state[7]);
29+
assert(!gettimeofday(&start, 0));
30+
for(i; ;++i) {
31+
sha256(block, state, 1);
32+
if(__builtin_expect((i & 0xfffff) == 0, 1)) {
33+
double total;
34+
uint64_t ix = i >> 20;
35+
assert(!gettimeofday(&now, 0));
36+
total = now.tv_usec + (double)now.tv_sec * 1000000 ;
37+
total = total - (start.tv_usec + (double)start.tv_sec * 1000000);
38+
fwrite(&ix, 8, 1, f);
39+
fwrite(blkptr, 4, 8, f);
40+
fflush(f);
41+
printf("block %04x%04x%04x%04x\n", blkptr[0], blkptr[1], blkptr[2], blkptr[3]);
42+
printf(" %04x%04x%04x%04x\n", blkptr[4], blkptr[5], blkptr[6], blkptr[7]);
43+
printf("speed %lu %G %G\n", i, total, i/total);
44+
}
45+
blkptr[0] = state[0];
46+
blkptr[1] = state[1];
47+
blkptr[2] = state[2];
48+
blkptr[3] = state[3];
49+
blkptr[4] = state[4];
50+
blkptr[5] = state[5];
51+
blkptr[6] = state[6];
52+
blkptr[7] = state[7];
53+
state[0] = ostate[0];
54+
state[1] = ostate[1];
55+
state[2] = ostate[2];
56+
state[3] = ostate[3];
57+
state[4] = ostate[4];
58+
state[5] = ostate[5];
59+
state[6] = ostate[6];
60+
state[7] = ostate[7];
61+
62+
}
63+
}
64+

0 commit comments

Comments
 (0)