| 1 | #if !defined( _libfastmint_h ) |
| 2 | #define _libfastmint_h |
| 3 | |
| 4 | #if defined(WIN32) |
| 5 | #include <windows.h> |
| 6 | #undef small |
| 7 | #else |
| 8 | #include <sys/time.h> |
| 9 | #endif |
| 10 | #include "hashcash.h" |
| 11 | |
| 12 | #if defined(WIN32) |
| 13 | #define MILLISEC 1 |
| 14 | #define TIMETYPE DWORD |
| 15 | #define TIME0 0 |
| 16 | #define timer(x) (*(x)=GetTickCount()) |
| 17 | #else |
| 18 | #define MILLISEC 1000 |
| 19 | #define TIME0 {0,0} |
| 20 | #define TIMETYPE struct timeval |
| 21 | #define timer(x) gettimeofday(x,NULL) |
| 22 | #endif |
| 23 | |
| 24 | #define MINTER_CALLBACK_ARGS hashcash_callback cb, void* user_args, \ |
| 25 | double counter, double expected |
| 26 | |
| 27 | #define MINTER_CALLBACK_VARS double percent; \ |
| 28 | TIMETYPE prev=TIME0, curr; \ |
| 29 | int lastBits = *best |
| 30 | |
| 31 | /* in minter if have to do something for acces to floating point |
| 32 | * operations override this */ |
| 33 | |
| 34 | /* eg mmx minter sets to this: __builtin_ia32_emms() */ |
| 35 | |
| 36 | #define MINTER_CALLBACK_CLEANUP_FP /**/ |
| 37 | |
| 38 | |
| 39 | #if defined(WIN32) |
| 40 | #define small_time_diff_gt( c, p, intv ) \ |
| 41 | ((c>p?c-p:(c+(((TIMETYPE)-1)-p)))>(intv)) |
| 42 | #else |
| 43 | /* works for intervals < 1 second */ |
| 44 | #define small_time_diff_gt( c, p, intv ) \ |
| 45 | (c.tv_sec-p.tv_sec>1 || \ |
| 46 | (c.tv_usec+((c.tv_sec - p.tv_sec)?1000000:0))-p.tv_usec>(intv)) |
| 47 | #endif |
| 48 | |
| 49 | #define MINTER_CALLBACK() \ |
| 50 | do { \ |
| 51 | if (cb != NULL && (iters & 0xFFFF) == 0) { \ |
| 52 | timer(&curr); \ |
| 53 | if (gotBits > lastBits || \ |
| 54 | small_time_diff_gt(curr,prev,100*MILLISEC)) { \ |
| 55 | MINTER_CALLBACK_CLEANUP_FP; \ |
| 56 | percent = (int)(((counter+iters)/ \ |
| 57 | expected*100)+0.5); \ |
| 58 | if (!cb(percent,*best,bits, \ |
| 59 | counter+iters,expected,user_args)) { \ |
| 60 | *best = -1; \ |
| 61 | return 0; \ |
| 62 | } \ |
| 63 | prev = curr; \ |
| 64 | lastBits = gotBits; \ |
| 65 | } \ |
| 66 | } \ |
| 67 | } while (0) |
| 68 | |
| 69 | typedef unsigned int uInt32; |
| 70 | typedef unsigned long (* HC_Mint_Routine)(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 71 | typedef int (* HC_Mint_Capable_Routine)(void); |
| 72 | |
| 73 | typedef enum { |
| 74 | EncodeHexUpper, /* 0123456789ABCDEF */ |
| 75 | EncodeHexLower, /* 0123456789abcdef */ |
| 76 | EncodeAlpha16Upper, /* ABCDEFGHIJKLMNOP */ |
| 77 | EncodeAlpha16Lower, /* abcdefghijklmnop */ |
| 78 | EncodeBase64 /* 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/ */ |
| 79 | } EncodeAlphabet; |
| 80 | |
| 81 | |
| 82 | typedef struct { |
| 83 | const char *name; |
| 84 | EncodeAlphabet encoding; |
| 85 | HC_Mint_Routine func; |
| 86 | HC_Mint_Capable_Routine test; |
| 87 | } HC_Minter; |
| 88 | |
| 89 | #define HC_CPU_SUPPORTS_ALTIVEC 0x01 |
| 90 | #define HC_CPU_SUPPORTS_MMX 0x02 |
| 91 | extern int gProcessorSupportFlags; |
| 92 | |
| 93 | extern const char *encodeAlphabets[]; |
| 94 | extern const int EncodeBitRate[]; |
| 95 | |
| 96 | extern void hashcash_select_minter(); |
| 97 | |
| 98 | /* Portably write a word into a byte array */ |
| 99 | #define PUT_WORD(_dst, _src) { \ |
| 100 | *((unsigned char*)(_dst)+0) = ((_src) >> 24) & 0xFF; \ |
| 101 | *((unsigned char*)(_dst)+1) = ((_src) >> 16) & 0xFF; \ |
| 102 | *((unsigned char*)(_dst)+2) = ((_src) >> 8) & 0xFF; \ |
| 103 | *((unsigned char*)(_dst)+3) = ((_src) ) & 0xFF; \ |
| 104 | } |
| 105 | |
| 106 | /* Portably load bytes into a word */ |
| 107 | #define GET_WORD(_src) ( \ |
| 108 | (*((unsigned char*)(_src)+0) << 24) | \ |
| 109 | (*((unsigned char*)(_src)+1) << 16) | \ |
| 110 | (*((unsigned char*)(_src)+2) << 8) | \ |
| 111 | (*((unsigned char*)(_src)+3) ) ) |
| 112 | |
| 113 | /* Attempt to mint a hashcash token with a given bit-value. |
| 114 | * Will append a random string to token that produces the required preimage, |
| 115 | * then return a pointer to the resultant string in result. Caller must free() |
| 116 | * result buffer after use. |
| 117 | * Returns the number of bits actually minted (may be more or less than requested). |
| 118 | */ |
| 119 | extern double hashcash_fastmint(const int bits, const char *token, int small, char **result, hashcash_callback cb, void* user_arg); |
| 120 | |
| 121 | /* Perform a quick benchmark of the selected minting backend. Returns speed. */ |
| 122 | extern unsigned long hashcash_per_sec(void); |
| 123 | |
| 124 | /* Test and benchmark available hashcash minting backends. Returns the speed |
| 125 | * of the fastest valid routine, and updates fastest_minter as appropriate. |
| 126 | * Uses one or more known solutions to gauge both speed and accuracy. |
| 127 | * Optionally displays status and benchmark results on console. |
| 128 | */ |
| 129 | extern unsigned long hashcash_benchtest(int verbose, int core); |
| 130 | |
| 131 | /* Minting backend routines. |
| 132 | * These are in two parts: |
| 133 | * - HC_Mint_Routine is the actual backend. Attempts to produce at least N-bit |
| 134 | * preimage by incrementing the tail section of the block. Block need not |
| 135 | * be initial block, as custom IV is provided, but must be terminal block. |
| 136 | * Tail index points just after last character - ie. to beginning of SHA-1 |
| 137 | * padding - do not overwrite padding. At least 8 characters will be present. |
| 138 | * Will bail out after maxIter attempts, within some reasonable tolerance. |
| 139 | * - HC_Mint_Capable_Routine is a trivial test to see if the current machine |
| 140 | * supports necessary hardware features for the backend. Returns boolean. |
| 141 | */ |
| 142 | |
| 143 | /* Standard ANSI-C 1-pipe "compact" implementation - for use on x86 and other |
| 144 | * register-limited architectures. |
| 145 | */ |
| 146 | extern unsigned long minter_ansi_compact_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 147 | extern int minter_ansi_compact_1_test(void); |
| 148 | |
| 149 | /* Standard ANSI-C 1-pipe "Method B" implementation - for use on register-rich architectures. |
| 150 | */ |
| 151 | extern unsigned long minter_ansi_standard_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 152 | extern int minter_ansi_standard_1_test(void); |
| 153 | |
| 154 | /* Standard ANSI-C 1-pipe "ultra-compact" implementation - for use on architectures with |
| 155 | * severely limited L1 caches (eg. M68K). |
| 156 | */ |
| 157 | extern unsigned long minter_ansi_ultracompact_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 158 | extern int minter_ansi_ultracompact_1_test(void); |
| 159 | |
| 160 | /* Standard ANSI-C 2-pipe "compact" implementation - for use on register-rich architectures. |
| 161 | */ |
| 162 | extern unsigned long minter_ansi_compact_2(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 163 | extern int minter_ansi_compact_2_test(void); |
| 164 | |
| 165 | /* Standard ANSI-C 2-pipe "Method B" implementation - for use on register-rich architectures. |
| 166 | */ |
| 167 | extern unsigned long minter_ansi_standard_2(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 168 | extern int minter_ansi_standard_2_test(void); |
| 169 | |
| 170 | /* PowerPC Altivec 1x4-pipe "Method B" implementation - for use on G4 and higher systems. |
| 171 | */ |
| 172 | extern unsigned long minter_altivec_standard_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 173 | extern int minter_altivec_standard_1_test(void); |
| 174 | |
| 175 | /* PowerPC Altivec 2x4-pipe "Method B" implementation - for use on G4 and higher systems. Hand-optimised. |
| 176 | */ |
| 177 | extern unsigned long minter_altivec_standard_2(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 178 | extern int minter_altivec_standard_2_test(void); |
| 179 | |
| 180 | /* PowerPC Altivec 2x4-pipe "compact" implementation - for use on G4 and higher systems. Hand-optimised. |
| 181 | */ |
| 182 | extern unsigned long minter_altivec_compact_2(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 183 | extern int minter_altivec_compact_2_test(void); |
| 184 | |
| 185 | /* AMD64/x86 MMX 1x2-pipe "Method B" implementation - for use on Pentium-MMX/2/3/4, K6-2/3, |
| 186 | * Athlon, Athlon64, Opteron, and compatible systems. Hand-optimised. |
| 187 | */ |
| 188 | extern unsigned long minter_mmx_standard_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 189 | |
| 190 | extern int minter_mmx_standard_1_test(void); |
| 191 | |
| 192 | /* AMD64/x86 MMX 1x2-pipe "compact" implementation - for use on Pentium-MMX/2/3/4, K6-2/3, |
| 193 | * Athlon, Athlon64, Opteron, and compatible systems. Hand-optimised. |
| 194 | */ |
| 195 | extern unsigned long minter_mmx_compact_1(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 196 | extern int minter_mmx_compact_1_test(void); |
| 197 | |
| 198 | /* use SHA1 library (integrated or openSSL depending on how compiled) */ |
| 199 | |
| 200 | extern int minter_library_test(void); |
| 201 | |
| 202 | extern unsigned long minter_library(int bits, int* best, unsigned char *block, const uInt32 IV[5], int tailIndex, unsigned long maxIter, MINTER_CALLBACK_ARGS); |
| 203 | |
| 204 | |
| 205 | #endif |