forked from monkins1010/ccminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverus_clhash.h
148 lines (114 loc) · 4.08 KB
/
verus_clhash.h
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
/*
* This uses veriations of the clhash algorithm for Verus Coin, licensed
* with the Apache-2.0 open source license.
*
* Copyright (c) 2018 Michael Toutonghi
* Distributed under the Apache 2.0 software license, available in the original form for clhash
* here: https://github.com/lemire/clhash/commit/934da700a2a54d8202929a826e2763831bd43cf7#diff-9879d6db96fd29134fc802214163b95a
*
* CLHash is a very fast hashing function that uses the
* carry-less multiplication and SSE instructions.
*
* Original CLHash code (C) 2017, 2018 Daniel Lemire and Owen Kaser
* Faster 64-bit universal hashing
* using carry-less multiplications, Journal of Cryptographic Engineering (to appear)
*
* Best used on recent x64 processors (Haswell or better).
*
**/
#ifndef INCLUDE_VERUS_CLHASH_H
#define INCLUDE_VERUS_CLHASH_H
//#include <intrin.h>
#ifndef _WIN32
#include <cpuid.h>
#else
#include <intrin.h>
#endif // !WIN32
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <assert.h>
//#include <boost/thread.hpp>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#define posix_memalign(p, a, s) (((*(p)) = _aligned_malloc((s), (a))), *(p) ?0 :errno)
typedef unsigned char u_char;
typedef unsigned char u_char;
#endif
#include "haraka.h"
#include "haraka_portable.h"
enum {
// Verus Key size must include the equivalent size of a Haraka key
// after the first part.
// Any excess over a power of 2 will not get mutated, and any excess over
// power of 2 + Haraka sized key will not be used
VERUSKEYSIZE = 1024 * 8 + (40 * 16),
VERUSHHASH_SOLUTION_VERSION = 1
};
extern int __cpuverusoptimized;
inline bool IsCPUVerusOptimized()
{
#ifndef _WIN32
unsigned int eax, ebx, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
{
return false;
}
return ((ecx & (bit_AVX | bit_AES)) == (bit_AVX | bit_AES));
#else
// https://github.com/gcc-mirror/gcc/blob/master/gcc/config/i386/cpuid.h
#define bit_AVX (1 << 28)
#define bit_AES (1 << 25)
// https://insufficientlycomplicated.wordpress.com/2011/11/07/detecting-intel-advanced-vector-extensions-avx-in-visual-studio/
// bool cpuAVXSuport = cpuInfo[2] & (1 << 28) || false;
int cpuInfo[4];
__cpuid(cpuInfo, 1);
return ((cpuInfo[2] & (bit_AVX | bit_AES)) == (bit_AVX | bit_AES));
#endif
if (__cpuverusoptimized & 0x80)
{
#ifdef _WIN32
#define bit_AVX (1 << 28)
#define bit_AES (1 << 25)
#define bit_PCLMUL (1 << 1)
// https://insufficientlycomplicated.wordpress.com/2011/11/07/detecting-intel-advanced-vector-extensions-avx-in-visual-studio/
// bool cpuAVXSuport = cpuInfo[2] & (1 << 28) || false;
int cpuInfo[4];
__cpuid(cpuInfo, 1);
__cpuverusoptimized = ((cpuInfo[2] & (bit_AVX | bit_AES | bit_PCLMUL)) == (bit_AVX | bit_AES | bit_PCLMUL));
#else
unsigned int eax,ebx,ecx,edx;
if (!__get_cpuid(1,&eax,&ebx,&ecx,&edx))
{
__cpuverusoptimized = false;
}
else
{
__cpuverusoptimized = ((ecx & (bit_AVX | bit_AES | bit_PCLMUL)) == (bit_AVX | bit_AES | bit_PCLMUL));
}
#endif //WIN32
}
return __cpuverusoptimized;
};
inline void ForceCPUVerusOptimized(bool trueorfalse)
{
__cpuverusoptimized = trueorfalse;
};
uint64_t verusclhashv2_1(void * random, const unsigned char buf[64], uint64_t keyMask, uint32_t *fixrand, uint32_t *fixrandex,
u128 *g_prand, u128 *g_prandex);
uint64_t verusclhashv2_2(void * random, const unsigned char buf[64], uint64_t keyMask, uint32_t *fixrand, uint32_t *fixrandex,
u128 *g_prand, u128 *g_prandex);
uint64_t verusclhash_port(void * random, const unsigned char buf[64], uint64_t keyMask, uint32_t *fixrand, uint32_t *fixrandex,
u128 *g_prand, u128 *g_prandex);
void *alloc_aligned_buffer(uint64_t bufSize);
#ifdef __cplusplus
} // extern "C"
#endif
#ifdef __cplusplus
#include <vector>
#include <string>
// special high speed hasher for VerusHash 2.0
#endif // #ifdef __cplusplus
#endif // INCLUDE_VERUS_CLHASH_H