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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
|
#if defined(__POWERPC__) && defined(__ALTIVEC__) && (!defined(__GNUC__) || !defined(__MACH__))
#include <altivec.h>
#endif
#ifdef __CARBON__
#include <CoreServices/CoreServices.h>
#endif
#if defined(__POWERPC__) && !defined(__MACH__) && !defined(__UNIX__) && !defined(__CARBON__)
#include <Gestalt.h>
#endif
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include "random.h"
#include "sha1.h"
#define BUILD_DLL
#include "libfastmint.h"
/* Index into array of available minters */
static int fastest_minter = -1;
static unsigned int num_minters = 0;
#define MAX_MINTERS 20
static HC_Minter minters[MAX_MINTERS];
const char *encodeAlphabets[] = {
"0123456789ABCDEF",
"0123456789abcdef",
"ABCDEFGHIJKLMNOP",
"abcdefghijklmnop",
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
};
const int EncodeBitRate[] = { 4, 4, 4, 4, 6 };
/* Keep track of what the CPU supports */
static volatile int gIllegalInstructionTrapped = 0;
static jmp_buf gEnv;
int gProcessorSupportFlags = 0;
/* SHA-1 magic gunge */
#define H0 0x67452301
#define H1 0xEFCDAB89
#define H2 0x98BADCFE
#define H3 0x10325476
#define H4 0xC3D2E1F0
static const uInt32 SHA1_IV[ 5 ] = { H0, H1, H2, H3, H4 };
/* SIGILL handler */
static void sig_ill_handler(int sig)
{
sig = sig;
gIllegalInstructionTrapped = 1;
longjmp(gEnv,1);
}
#if defined(__ALTIVEC__) && !defined(__GNUC__)
#pragma dont_inline on
/* Dummy Altivec operation for testing */
static void dummy_altivec_fn(void)
{
volatile vector unsigned int v;
v = vec_or(v,v);
}
#pragma dont_inline reset
#endif
/* Detect whether extended CPU features like Altivec, MMX, etc are supported */
static void hashcash_detect_features(void)
{
#if defined(__POWERPC__) && defined(__ALTIVEC__)
int hasAltivec = 0;
#if defined(__UNIX__) || defined(__MACH__)
/* Use generic SIGILL trap handler */
void *oldhandler;
gIllegalInstructionTrapped = 0;
oldhandler = signal(SIGILL, sig_ill_handler);
if(!setjmp(gEnv)) {
#ifdef __GNUC__
asm volatile ( "vor v0,v0,v0" );
#else
dummy_altivec_fn();
#endif
}
signal(SIGILL, oldhandler);
hasAltivec = !gIllegalInstructionTrapped;
#else
/* Carbon and MacOS Classic */
long cpuAttributes;
OSErr err = Gestalt(gestaltPowerPCProcessorFeatures, &cpuAttributes);
if(err == 0)
hasAltivec = ((1 << gestaltPowerPCHasVectorInstructions) & cpuAttributes) != 0;
#endif
if(hasAltivec)
gProcessorSupportFlags |= HC_CPU_SUPPORTS_ALTIVEC;
else
gProcessorSupportFlags &= ~(HC_CPU_SUPPORTS_ALTIVEC);
#elif defined(__i386__) && defined(__GNUC__)
void *oldhandler;
int hasMMX = 0;
gIllegalInstructionTrapped = 0;
oldhandler = signal(SIGILL, sig_ill_handler);
if(!setjmp(gEnv)) {
asm volatile (
"movl $1, %%eax\n\t"
"push %%ebx\n\t"
"cpuid\n\t"
"andl $0x800000, %%edx\n\t"
"pop %%ebx\n\t"
: "=d" (hasMMX)
: /* no input */
: "eax", "ecx"
);
}
signal(SIGILL, oldhandler);
if(hasMMX && !gIllegalInstructionTrapped)
gProcessorSupportFlags |= HC_CPU_SUPPORTS_MMX;
else
gProcessorSupportFlags &= ~(HC_CPU_SUPPORTS_MMX);
#elif defined(__AMD64__)
gProcessorSupportFlags = HC_CPU_SUPPORTS_MMX;
#else
gProcessorSupportFlags = 0;
#endif
}
/* Statically guesstimate the fastest hashcash minting routine. Takes
* into account only the gross hardware architecture and features
* available. Updates fastest_minter. Also initialises and populates
* the available minter array if necessary.
*/
static const EncodeAlphabet encodings[] = {
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64,
EncodeBase64
};
void hashcash_select_minter()
{
static const HC_Mint_Routine funcs[] = {
minter_library,
minter_ansi_compact_1,
minter_ansi_standard_1,
minter_ansi_ultracompact_1,
minter_ansi_compact_2,
minter_ansi_standard_2,
minter_altivec_standard_1,
minter_altivec_compact_2,
minter_altivec_standard_2,
minter_mmx_compact_1,
minter_mmx_standard_1,
NULL };
static const HC_Mint_Capable_Routine tests[] = {
minter_library_test,
minter_ansi_compact_1_test,
minter_ansi_standard_1_test,
minter_ansi_ultracompact_1_test,
minter_ansi_compact_2_test,
minter_ansi_standard_2_test,
minter_altivec_standard_1_test,
minter_altivec_compact_2_test,
minter_altivec_standard_2_test,
minter_mmx_compact_1_test,
minter_mmx_standard_1_test,
NULL };
static const char *names[] = {
#if defined( OPENSSL )
"SHA1 library (openSSL)",
#else
"SHA1 library (hashcash)",
#endif
"ANSI Compact 1-pipe",
"ANSI Standard 1-pipe",
"ANSI Ultra-Compact 1-pipe",
"ANSI Compact 2-pipe",
"ANSI Standard 2-pipe",
"PowerPC Altivec Standard 1x4-pipe",
"PowerPC Altivec Compact 2x4-pipe",
"PowerPC Altivec Standard 2x4-pipe",
"AMD64/x86 MMX Compact 1x2-pipe",
"AMD64/x86 MMX Standard 1x2-pipe",
NULL };
int i = 0 ;
/* Populate array */
if(!num_minters) {
num_minters = (sizeof(funcs) / sizeof(*funcs)) - 1;
if ( num_minters > MAX_MINTERS ) {
fprintf(stderr,
"INTERNAL ERROR: "
"increase size of MAX_MINTERS\n");
exit(3); /* EXIT_ERROR */
}
for(i=0; i < num_minters; i++) {
minters[i].name = names[i];
minters[i].func = funcs[i];
minters[i].test = tests[i];
minters[i].encoding = encodings[i];
}
}
/* If nothing else works, just use the compact_1 minter on x86
and standard_1 elsewhere */
#ifdef __i386__
fastest_minter = 1;
#elif defined(__M68000__)
fastest_minter = 3;
#else
fastest_minter = 2;
#endif
/* Detect actual CPU capabilities */
hashcash_detect_features();
/* See if any of the vectorised minters work, choose the
highest-numbered one that does */
for(i=6; i < num_minters; i++) {
if(minters[i].test()) { fastest_minter = i; }
}
}
/* Do a quick, silent benchmark of the selected backend. Assumes it
* works. */
unsigned long hashcash_per_sec_calc( void )
{
static const unsigned int test_bits = 64;
static const char *test_string =
"1:32:040404:foo@fnord.gov::0123456789abcdef:00000000";
static const int test_tail = 52;
unsigned long rate = 0, iter_count = 256;
volatile clock_t begin = 0 , end = 0 , tmp = 0 , res = 0 , taken = 0 ;
double elapsed = 0 , multiple = 0 ;
unsigned char block[SHA1_INPUT_BYTES] = {0};
int gotbits = 0;
HC_Mint_Routine best_minter_fp;
/* Ensure a valid minter backend is selected */
if(!num_minters) { hashcash_select_minter(); }
best_minter_fp = minters[fastest_minter].func;
/* Determine clock resolution */
end = clock();
while((begin = clock()) == end)
;
while((end = clock()) == begin)
;
if ( end < begin ) { tmp = begin; begin = end; end = tmp; }
res = end - begin;
/* where there is poor resolution use this */
/* less accurate but faster -- otherwise takes 0.5secs */
if ( res > 1000 ) {
/* Run minter, with clock running */
begin = end;
do {
/* set up SHA-1 block */
strncpy((char*)block, test_string, SHA1_INPUT_BYTES);
block[test_tail] = 0x80;
memset(block+test_tail+1, 0, 59-test_tail);
PUT_WORD(block+60, test_tail << 3);
best_minter_fp(test_bits, &gotbits, block,
SHA1_IV, test_tail, iter_count,
NULL, NULL, 0, 0);
if ( gotbits >= test_bits) {
/* The benchmark will be inaccurate
if we actually find a preimage! */
fprintf(stderr,
"Error in hashcash_quickbench(): found preimage while trying to benchmark!\n");
return 1;
}
rate += iter_count;
end = clock();
if ( end < begin ) { taken = begin-end; }
else { taken = end-begin; }
} while ( taken < 8*res );
multiple = CLOCKS_PER_SEC / (double)(8*res);
rate *= multiple;
return rate;
}
/* Run increasing lengths of minting until we have sufficient
* elapsed time for precision */
while(iter_count) {
/* set up SHA-1 block */
strncpy((char*)block, test_string, SHA1_INPUT_BYTES);
block[test_tail] = 0x80;
memset(block+test_tail+1, 0, 59-test_tail);
PUT_WORD(block+60, test_tail << 3);
/* Run minter, with clock running */
end = clock();
while((begin = clock()) == end) {}
best_minter_fp(test_bits,&gotbits,block,SHA1_IV,test_tail,
iter_count, NULL, NULL, 0, 0);
if ( gotbits >= test_bits) {
/* The benchmark will be inaccurate if we
* actually find a preimage! */
fprintf(stderr, "Error in hashcash_quickbench(): found preimage while trying to benchmark!\n");
return 1;
}
end = clock();
if ( end < begin ) { tmp = begin; begin = end; end = tmp; }
elapsed = (end-begin) / (double) CLOCKS_PER_SEC;
if(end-begin > (res * 16))
break;
iter_count <<= 1;
}
rate = iter_count / elapsed;
return rate;
}
/* version of hashcash_per_sec_calc which caches result, so only doing
* the work once. Note: hashcash_use_core will dirty the cache to
* trigger a recalc
*/
static int cached_per_sec = 0;
unsigned long hashcash_per_sec(void)
{
static unsigned long cache = 0;
if ( !cached_per_sec ) {
cache = hashcash_per_sec_calc();
cached_per_sec = 1;
}
return cache;
}
/* Test and benchmark available hashcash minting backends. Returns
* the speed of the fastest valid routine, and updates fastest_minter
* as appropriate.
*/
unsigned long hashcash_benchtest(int verbose, int core)
{
unsigned long i, a, b;
int best_minter = -1, got_bits = 0;
static const unsigned int test_bits = 22;
static const char *test_string =
"1:22:040404:foo@bar.net::0123456789abcdef:0000000000";
static const int test_tail = 52; /* must be less than 56 */
static const int bit_stats[] = { 8, 10, 16, 20, 22,
24, 26, 28, 30, 0 };
unsigned char block[SHA1_INPUT_BYTES+1] = {0};
volatile clock_t begin = 0, end = 0, tmp = 0;
double elapsed = 0, rate = 0, peak_rate = 0;
SHA1_ctx crypter;
unsigned char hash[SHA1_DIGEST_BYTES] = {0};
const char *p = NULL , *q = NULL ;
int start = 0, stop = 0;
/* If minter list isn't valid, make it so */
hashcash_select_minter();
/* print header */
if(verbose > 0 ) {
printf(" Rate Name (* machine default)\n");
}
if(verbose >= 3) { printf("\n"); }
if ( core >= 0 ) {
start = core; stop = start+1;
} else {
start = 0; stop = num_minters;
}
for(i = start; i < stop; i++) {
/* If the minter can't run... */
if(!minters[i].test()) {
if(verbose >= 2) {
printf(" --- %s (Not available on this machine)\n", minters[i].name);
}
continue;
}
if(verbose) {
printf(" %s\r", minters[i].name);
fflush(stdout);
}
/* set up SHA-1 block */
strncpy((char*)block, test_string, SHA1_INPUT_BYTES);
block[test_tail] = 0x80;
memset(block+test_tail+1, 0, 59-test_tail);
PUT_WORD(block+60, test_tail << 3);
/* Run minter, with clock running */
end = clock();
while((begin = clock()) == end) {}
minters[i].func(test_bits, &got_bits, block, SHA1_IV,
test_tail, 1 << 30, NULL, NULL, 0, 0);
end = clock();
if ( end < begin ) { tmp = begin; begin = end; end = tmp; }
elapsed = (end-begin) / (double) CLOCKS_PER_SEC;
/* Different minter iteration patterns will find
* different solutions */
/* Verify solution correctness first, using reference
* SHA-1 library */
SHA1_Init(&crypter);
SHA1_Update(&crypter, block, test_tail);
SHA1_Final(&crypter, hash);
for(a=0; a < SHA1_DIGEST_BYTES-1 && hash[a] == 0; a++) {}
for(b=0; b < 8 && (hash[a] & 0x80) == 0; b++) {
hash[a] <<= 1;
}
if(got_bits != (a*8)+b || got_bits < test_bits ||
block[test_tail] != (unsigned char) 0x80) {
if(verbose) {
printf("ERROR!\n");
printf(" Wanted %u bits, reported %d bits, got %lu bits.\n", test_bits, got_bits, (a*8)+b);
if(block[test_tail] == (unsigned char) 0x80) {
printf(" End-of-block marker remains intact.\n");
} else {
printf(" End-of-block marker damaged!\n");
}
block[test_tail] = 0;
printf(" \"%s\"\n", block);
printf(" Time taken: %.3f\n\n", elapsed);
}
continue;
}
/* Use knowledge of encoding alphabet to calculate
iterations taken */
a = test_tail-8;
b = 0;
p = encodeAlphabets[minters[i].encoding];
while(a < test_tail && block[a] == '0') {
a++;
}
for( ; a < test_tail; a++) {
q = strchr(p, block[a]);
if(!q)
break;
b = (b * strlen(p)) + (q - p);
}
if(a != test_tail) {
if(verbose) {
printf("ERROR!\n");
printf(" Unable to parse iteration count.\n");
printf(" \"%s\"\n", block);
printf(" \"%s\"\n", p);
}
continue;
}
/* We know the elapsed time and the iteration count,
so calculate the rate */
rate = b / elapsed;
if(verbose) {
printf("%9lu %s %c\n", (unsigned long) rate,
minters[i].name,
(i == fastest_minter) ? '*' : ' ');
}
if(rate > peak_rate) {
peak_rate = rate;
best_minter = i;
}
/* Optionally print out the stats */
if(verbose >= 3) {
block[test_tail] = 0;
printf(" Solution: %s\n", block);
printf(" Iterations: %lu\n", b);
printf(" Time taken: %.3f\n\n", elapsed);
}
}
fastest_minter = best_minter;
if(verbose && best_minter >= 0) {
printf("Best minter: %s (%lu hashes/sec)\n",
minters[best_minter].name, (unsigned long) peak_rate);
}
if(verbose >= 2 && best_minter >= 0) {
printf("Projected average times to mint:\n");
for(i = 0; bit_stats[i]; i++) {
elapsed = (1 << bit_stats[i]) / peak_rate;
printf("%3d bits: %9.3f seconds", bit_stats[i],
elapsed);
if(elapsed > 200000) {
printf(" (%.1f days)", elapsed/(3600*24));
} else if(elapsed > 5000) {
printf(" (%.1f hours)", elapsed/3600);
} else if(elapsed > 100) {
printf(" (%.1f minutes)", elapsed/60);
} else if(elapsed < 0.005) {
printf(" (%.1f microseconds)", elapsed *
1000000);
}
printf("\n");
}
}
return (unsigned long) peak_rate;
}
/* Attempt to mint a hashcash token with a given bit-value.
* Will append a random string to token that produces the required
* preimage, then return a pointer to the resultant string in result.
* Caller must free() result buffer after use.
* Returns the number of bits actually minted (may be more or less
* than requested).
*/
double hashcash_fastmint(const int bits, const char *token, int compress,
char **result, hashcash_callback cb, void* user_args)
{
SHA1_ctx crypter;
unsigned char hash[SHA1_DIGEST_BYTES] = {0};
unsigned int IV[SHA1_DIGEST_WORDS] = {0};
unsigned char *buffer = NULL, *block = NULL, c = 0;
unsigned char *last = NULL;
unsigned int buflen = 0, tail = 0, a = 0, b = 0, save_tail = 0;
unsigned long t = 0, loop = 0, iters = 0, i = 0, first = 1;
HC_Mint_Routine best_minter;
double counter = 0, expected = 0;
int gotBits = 0, bit_rate = 6, chars = 0, blocks = 1, oldblocks = 0;
int prevBits = 0;
int* best = &gotBits; /* NB this is needed for CALLBACK macros */
/* this is to allow this fn to call the same callback macro */
MINTER_CALLBACK_VARS;
/* Make sure list of minters is valid */
if(fastest_minter < 0) { hashcash_select_minter(); }
/* only the library minter can cope with split blocks */
if ( compress > 1 ) { fastest_minter = 0; }
best_minter = minters[fastest_minter].func;
expected = hashcash_expected_tries( bits );
again:
/* Set up string for hashing */
tail = strlen(token);
buflen = (tail - (tail % SHA1_INPUT_BYTES)) + 2*SHA1_INPUT_BYTES;
buffer = malloc(buflen);
memset(buffer, 0, buflen);
strncpy((char*)buffer, token, buflen);
/* Add 96 bits of random data */
t = tail + 16;
for( ; tail < t; tail++) {
random_getbytes(&c, 1);
buffer[tail] = encodeAlphabets[EncodeBase64][c & 0x3f];
}
#if defined( DEBUG )
fprintf( stderr, "tail = \"%s\"\n", buffer+tail-16 );
#endif
/* Add separator and zeroed count field (for v1 hashcash format) */
buffer[tail++] = ':';
save_tail = tail;
bit_rate = EncodeBitRate[encodings[fastest_minter]];
#if defined( DEBUG )
chars = 18/bit_rate;
#else
chars = 31/bit_rate;
#endif
for ( i = compress ? 1 : chars;
i <= chars && (first || gotBits < bits); i++ ) {
first = 0;
tail = save_tail;
t = tail + i;
for( ; tail < t; tail++) { buffer[tail] = '0'; }
switch (compress) {
case 0: /* fast stamps */
/* Align to optimal counting positions */
for( ; (tail % SHA1_INPUT_BYTES) != 32 &&
(tail % SHA1_INPUT_BYTES) != 52; tail++) {
buffer[tail] = '0';
}
break;
case 1: /* produce moderately compact stamps */
/* ensure counting is all within one SHA-1 block */
for( ; (tail % SHA1_INPUT_BYTES) < i ||
(tail % SHA1_INPUT_BYTES) >= 56; tail++) {
buffer[tail] = '0';
}
break;
default: /* produce very compact stamps */
oldblocks = blocks;
if ( (tail % SHA1_INPUT_BYTES) < i ||
(tail % SHA1_INPUT_BYTES) >= 56 ) {
blocks = 2; /* split across block */
}
/* no padding at all! */
}
/* Hash all but the final block, due to invariance */
t = tail - (tail % SHA1_INPUT_BYTES);
if ( blocks > 1 && t >= 64 && (tail % SHA1_INPUT_BYTES) < i ) {
t -= 64;
}
SHA1_Init(&crypter);
SHA1_Update(&crypter, buffer, t);
#if defined(OPENSSL)
IV[0]=crypter.h0;
IV[1]=crypter.h1;
IV[2]=crypter.h2;
IV[3]=crypter.h3;
IV[4]=crypter.h4;
#else
for(a=0; a < 5; a++) { IV[a] = crypter.H[a]; }
#endif
block = buffer + t;
/* Fill in the padding and trailer */
buffer[tail] = 0x80;
/* if number of blocks change get rid of old padding */
if ( blocks > oldblocks) {
/* note only need 7 chars as 1 char wider */
memset(buffer+tail+1,0,7);
}
PUT_WORD(block+(blocks>1?64:0)+60, tail << 3);
tail -= t;
/* Run the minter over the last block */
loop=best_minter(bits, &gotBits, block, IV, tail,
0x1U << (i*bit_rate), cb,
user_args,counter,expected);
if (loop==0) {
free(buffer);
if ( last ) { free( last ); }
if (*best==-1) {return -1;}
else { return 0; }
}
if ( gotBits == 0 || gotBits > prevBits ) {
block[tail] = 0;
prevBits = gotBits;
t = strlen( (char*)buffer );
last = realloc( last, t+1 );
strncpy( (char*)last, (char*)buffer, t+1 );
}
counter += (double)loop;
}
/* if we succeeded call the callback also */
if ( gotBits >= bits ) { MINTER_CALLBACK(); }
/* Verify solution using reference library */
SHA1_Init(&crypter);
SHA1_Update(&crypter, last, strlen( (char*)last ));
SHA1_Final(&crypter, hash);
for(a=0; a < SHA1_DIGEST_BYTES-1 && hash[a] == 0; a++)
;
for(b=0; b < 8 && (hash[a] & 0x80) == 0; b++)
hash[a] <<= 1;
b += a*8;
/* The minter appears to be broken! */
if(b < gotBits) {
fprintf(stderr, "ERROR: requested %d bits, reported %d bits, got %d bits using %s minter: \"%s\"\n",
bits, gotBits, b, minters[fastest_minter].name,
last );
exit(3);
}
/* The minter might not be able to detect unusually large
* (32+) bit counts, so we're allowed to give it another try.
*/
if(b < bits) {
/* fprintf( stderr, "buffer = %s\n", buffer );
fprintf( stderr, "wrapped\n" ); */
free(buffer);
goto again;
}
*result = (char*)buffer;
if ( last ) { free( last ); }
return counter;
}
int hashcash_core(void)
{
if (!num_minters) { hashcash_select_minter(); }
return fastest_minter;
}
int hashcash_use_core(int core)
{
if (!num_minters) { hashcash_select_minter(); }
if ( core < 0 || core >= num_minters ) { return -1; }
if ( !minters[core].test() ) { return 0; }
fastest_minter = core;
/* force recalc */
cached_per_sec = 0;
return 1;
}
const char* hashcash_core_name(int core)
{
if (!num_minters) { hashcash_select_minter(); }
if ( core < 0 || core >= num_minters ) {
return "undefined core";
}
return minters[core].name;
}
|