Skip to content
This repository was archived by the owner on Apr 11, 2022. It is now read-only.

Commit c66797a

Browse files
committed
update SHA1
1 parent 3a8f9f4 commit c66797a

File tree

5 files changed

+224
-199
lines changed

5 files changed

+224
-199
lines changed

benchmark/proofofwork-sha1.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ extern uint64_t pow_sha1_count;
66
int main(int argc, char **argv) {
77
uint8_t mask[pow_sha1_digest_length] = { 0xff, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
88
uint8_t target[pow_sha1_digest_length] = { 0xde, 0xad, 0xbe, 0xaf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
9-
uint8_t buffer[pow_sha1_block_length];
10-
uint64_t size = 15;
11-
memcpy(buffer, "libproofofwork:", size);
12-
pow_sha1_mine(mask, target, buffer, &size);
9+
uint8_t buffer[pow_sha1_block_length] = { 'f', 'l', 'a', 'g', '{', 0, 0, 0, 0, 0, 0, 0, 0, '}' };
10+
int32_t indices[pow_indices_length] = { 5,6,7,8,9,10,11,12 };
11+
uint64_t size = 14;
12+
pow_set_num_threads(0);
13+
pow_sha1_mine(mask, target, buffer, size, indices);
1314
printf("sha1\n");
1415
printf("deadbeaf\n");
1516
printf("%s\n", buffer);

bindings/python/tests/main.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ def test_md5(self):
1616
self.snippet_random_test(proofofwork.md5, hashlib.md5)
1717

1818
def test_sha1(self):
19-
pass# self.snippet_random_test(proofofwork.sha1, hashlib.sha1)
19+
self.snippet_random_test(proofofwork.sha1, hashlib.sha1)
2020

2121
def snippet_random_test_full(self, mine, answer):
2222
for _ in range(3):
2323
for len_s in range(1, 6):
2424
hash = ''.join([ random.choice('0123456789abcdef?') for _ in range(len_s) ])
25-
text = bytes(bytearray([ random.choice(bytearray(b'0123456789abcdef?')) for _ in range(20) ] + [ ord('?') ] * 8))
25+
text = bytes(bytearray([ random.choice(bytearray(b'0123456789abcdef?')) for _ in range(20) ] + [ ord('?') ] * 4))
2626
alphabet = bytes(bytearray([ random.randrange(256) for _ in range(64) ]))
2727
result = mine(hash, text=text, alphabet=alphabet)
2828
self.assertIsInstance(result, bytes)
@@ -37,7 +37,7 @@ def test_md5_full(self):
3737
self.snippet_random_test_full(proofofwork.md5, hashlib.md5)
3838

3939
def test_sha1_full(self):
40-
pass# self.snippet_random_test_full(proofofwork.sha1, hashlib.sha1)
40+
self.snippet_random_test_full(proofofwork.sha1, hashlib.sha1)
4141

4242

4343
if __name__ == '__main__':

include/proofofwork.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ bool pow_md5_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, u
3737

3838
enum { pow_sha1_block_length = 64 };
3939
enum { pow_sha1_digest_length = 20 };
40-
bool pow_sha1_mine(uint8_t *mask, uint8_t *target, uint8_t *buffer, uint64_t *size);
40+
bool pow_sha1_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, uint64_t size, int32_t const *indices);
4141

4242
#endif

src/md5-avx2.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ bool pow_md5_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, u
3232
static_assert (__BYTE_ORDER == __LITTLE_ENDIAN, "");
3333
if (mask == NULL) return false;
3434
if (target == NULL) return false;
35-
if (target == NULL) return false;
36-
if (indices == NULL) return false;
3735
if (buffer == NULL) return false;
36+
if (indices == NULL) return false;
3837
for (int i = 0; i < pow_indices_length; ++ i) {
39-
if (indices[i] < -1 or size <= indices[i]) return false;
38+
if (indices[i] < -1 or (int64_t)size <= indices[i]) return false;
4039
}
41-
if (size > pow_md5_block_length - 9) return false;
40+
if (indices[0] == -1) return false;
41+
if (size > pow_sha1_block_length - sizeof(uint64_t) / CHAR_BIT - 1) return false;
4242

4343
// load hash
4444
const uint32_t mask_a = ((uint32_t *)mask)[0];
@@ -70,7 +70,9 @@ bool pow_md5_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, u
7070
const int index7 = indices[7];
7171
static_assert (pow_indices_length == 8, "");
7272
repeat (i,pow_indices_length) {
73-
local[indices[i]] = 0;
73+
if (indices[i] != -1) {
74+
local[indices[i]] = 0;
75+
}
7476
}
7577
uint32_t *padded_alphabet = malloc(alphabet_size * sizeof(uint32_t));
7678
repeat (i,alphabet_size) {
@@ -89,7 +91,7 @@ bool pow_md5_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, u
8991
repeat (i4, alphabet_size) { if (index4 != -1) local[index4] = alphabet[i4];
9092
repeat (i3, alphabet_size) { if (index3 != -1) local[index3] = alphabet[i3];
9193
repeat (i2, alphabet_size) { if (index2 != -1) local[index2] = alphabet[i2];
92-
cnt += alphabet_size * alphabet_size;
94+
cnt += alphabet_size * (alphabet_size / vector_width * vector_width);
9395
repeat (i1, alphabet_size) { if (index1 != -1) local[index1] = alphabet[i1];
9496
__m256i y0 = _mm256_set1_epi32(((uint32_t *)local)[0 ]);
9597
__m256i y1 = _mm256_set1_epi32(((uint32_t *)local)[1 ]);
@@ -226,7 +228,7 @@ bool pow_md5_mine(uint8_t const *mask, uint8_t const *target, uint8_t *buffer, u
226228
}
227229
}
228230

229-
// break if unnecessary
231+
// break
230232
} if (index1 == -1 or found) break;
231233
} if (index2 == -1 or found) break;
232234
} if (index3 == -1 or found) break;

0 commit comments

Comments
 (0)