Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 77.24 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5. use utf8;
  6.  
  7. BEGIN {
  8.  
  9. sub _use_eval_cpan {
  10. my $module = shift;
  11.  
  12. eval "use $module;";
  13. if ( $@ ) {
  14. print "$module not found - installing it.\n";
  15. qx{cpan force install $module};
  16. eval "use $module;";
  17. }
  18.  
  19. return;
  20. }
  21.  
  22. _use_eval_cpan( 'File::Spec' );
  23. _use_eval_cpan( 'JSON' );
  24. _use_eval_cpan( 'LWP::UserAgent' );
  25. _use_eval_cpan( 'Net::SSLeay' );
  26. _use_eval_cpan( 'LWP::Protocol::https' );
  27. _use_eval_cpan( 'Parallel::ForkManager' );
  28. _use_eval_cpan( 'Term::ReadKey' )
  29. if ( $^O ne 'MSWin32' );
  30. _use_eval_cpan( 'Win32::SystemInfo' )
  31. if ( $^O eq 'MSWin32' );
  32. }
  33.  
  34. use Config;
  35. use Data::Dumper;
  36. use Digest::MD5 qw(md5_hex);
  37. use File::Temp qw(tempdir);
  38. use Getopt::Long;
  39. use JSON qw(-support_by_pp);
  40. use Math::BigFloat;
  41. use Math::BigInt;
  42. use Storable qw(retrieve
  43. store);
  44. use Sys::Hostname;
  45. use Time::HiRes qw(gettimeofday
  46. sleep
  47. tv_interval);
  48.  
  49. my $DEVEL = 0;
  50.  
  51. my $version =
  52. $DEVEL
  53. ? '9.999'
  54. : sprintf( "%4.3f", ( qw($Rev: 1067 $) )[ 1 ] / 1000 );
  55. my ( $finger, $intfin ) = @{ _get_client_fingerprint() };
  56. my $quine = quine();
  57. my $btcadr;
  58. my $secret;
  59. #my $ref = \&talk2server;
  60. my $ua = LWP::UserAgent->new(
  61. ssl_opts => {
  62. verify_hostname => 0
  63. },
  64. );
  65.  
  66. my $page_from = Math::BigInt->new( '0' );
  67. my $page_to = Math::BigInt->new( '0' );
  68. my $cli_error = 0;
  69.  
  70. my $factor;
  71.  
  72. my $arch = lc $Config{archname};
  73. $arch =~ s{\A(\w+-\w+)-.+}{$1}xms;
  74.  
  75. my @blocks;
  76.  
  77. my %config = (
  78. balances_stor => 'balances.pst',
  79. balances_text => 'allBalances.txt',
  80. benchmrk_stor => 'bench.pst',
  81. config_file => 'lbc.json',
  82. ocl_file => 'hash160_deparsed.cl',
  83. bin_path => {
  84. bzip2 => get_binary_path( 'bzip2' ),
  85. md5sum => get_binary_path( 'md5sum' ),
  86. xdelta3 => get_binary_path( 'xdelta3' ),
  87. },
  88.  
  89. maxpage => Math::BigInt->new(
  90. '110427941548649020598956093796432407238805355338168053038220561162489091'
  91. ),
  92. max_retries => 2,
  93.  
  94. ftp_url => 'ftp://dontconnect.test.org/LBC/',
  95. server_url => 'https://dontconnect.test.org/',
  96.  
  97. size_block => 2**20,
  98. size_fblock => 2**24,
  99.  
  100. testdata => {
  101. txt => 'bloom filter hash160 test set',
  102. h160 => [
  103. 222, 217, 207, 205, 195, 206, 210, 207, 194, 148, 135, 159,
  104. 206, 199, 142, 138, 196, 216, 223, 222, 207, 216, 138, 145,
  105. 194, 204, 142, 138, 207, 217, 197, 198, 201, 138, 215, 138,
  106. 131, 245, 142, 130, 206, 206, 203, 148, 135, 159, 206, 199,
  107. 142, 138, 209, 138, 131, 148, 194, 204, 142, 150, 130, 138,
  108. 207, 198, 195, 194, 221, 138, 145, 221, 207, 196, 148, 135,
  109. 159, 238, 231, 144, 144, 222, 217, 207, 205, 195, 238, 138,
  110. 151, 138, 159, 206, 199, 142, 138, 211, 199, 138, 145, 131,
  111. 194, 204, 142, 130, 207, 206, 197, 199, 196, 195, 200, 138,
  112. 145, 247, 155, 241, 131, 216, 207, 198, 198, 203, 201, 130,
  113. 138, 134, 141, 150, 141, 138, 134, 194, 204, 142, 138, 211,
  114. 199, 138, 196, 207, 218, 197
  115. ],
  116. },
  117. );
  118.  
  119. my $gpu_device = 1;
  120. my $gpu_auth = 0;
  121.  
  122. my $opt = {
  123. cpus => 0,
  124. gdev => 1,
  125. loop => 999999999,
  126. pages => 'auto',
  127. pretend => 'found',
  128. time => '15',
  129. };
  130.  
  131. if ( -r $config{config_file} ) {
  132. my $json = JSON->new->utf8->allow_blessed->allow_bignum;
  133. $opt =
  134. { %{ $opt }, %{ $json->decode( read_file( $config{config_file} ) ) } };
  135. }
  136.  
  137. GetOptions(
  138. 'address=s' => \$opt->{address},
  139. 'blocks=s' => \$opt->{blocks},
  140. 'cpus=i' => \$opt->{cpus},
  141. 'delay=f' => \$opt->{delay},
  142. 'gdev=i' => \$opt->{gdev},
  143. 'gpu' => \$opt->{gpu},
  144. 'help|?' => \&print_help,
  145. 'id=s' => \$opt->{id},
  146. 'info' => \&print_info,
  147. 'loop=i' => \$opt->{loop},
  148. 'no_update' => \$opt->{no_update},
  149. 'pages=s' => \$opt->{pages},
  150. 'query' => \$opt->{query},
  151. 'secret=s' => \$opt->{secret},
  152. 'time=s' => \$opt->{time},
  153. 'update' => \$opt->{update},
  154. 'version' => \&print_version,
  155. 'x' => \$opt->{test},
  156. ) or cleanup_end( "Formal error processing command line options!" );
  157.  
  158. if ( defined $opt->{update} ) {
  159. update_system();
  160. cleanup_end( "Finished update run - system up to date.\n" );
  161. }
  162.  
  163. if ( defined $opt->{id} ) {
  164. $finger = $opt->{id};
  165. if ( $finger !~ m{\A\w{8,32}\z}xms ) {
  166. print
  167. "Wrong id format '$finger'. Use 8-32 of the 63 characters [a-zA-Z0-9_]\n";
  168. exit 1;
  169. }
  170. }
  171.  
  172. if ( defined $opt->{secret} ) {
  173. $secret = process_secret( $opt->{secret} );
  174. }
  175.  
  176. if ( defined $opt->{address} ) {
  177. $btcadr = $opt->{address};
  178. set_btcadr();
  179. }
  180.  
  181. if ( defined $opt->{gdev} ) {
  182. $gpu_device = $opt->{gdev};
  183. if ( $gpu_device !~ m{\A\d{1,2}\z}xms ) {
  184. print "Wrong GPU device definition.\n";
  185. exit 1;
  186. }
  187. }
  188.  
  189. if ( defined $opt->{gpu} ) {
  190. $gpu_auth = print_gpu();
  191. }
  192.  
  193. if ( defined $opt->{query} ) {
  194. print_query();
  195. }
  196.  
  197. if ( defined $opt->{blocks} ) {
  198. if ( -r $opt->{blocks} ) {
  199. print "Individual blocks mode. Found file $opt->{blocks}\n";
  200. open
  201. my $handle,
  202. '<',
  203. $opt->{blocks};
  204. chomp( @blocks = <$handle> );
  205. close $handle;
  206. print "Turning off 'auto' pages and setting CPU to 1.\n";
  207. $opt->{pages} = '';
  208. $opt->{cpus} = 1;
  209. }
  210. else {
  211. print "Block file $opt->{blocks} not found.\n";
  212. $cli_error = 1;
  213. }
  214. }
  215. else {
  216. my $work_definition = get_pages_type( $opt->{pages} );
  217. if ( !defined $work_definition ) {
  218. print "Malformed/Unknown work definition ($opt->{pages}).\n";
  219. $cli_error = 1;
  220. }
  221. elsif ( ref $work_definition eq 'ARRAY' ) {
  222. $page_from = Math::BigInt->new( $work_definition->[ 0 ] );
  223. $page_to = Math::BigInt->new( $work_definition->[ 1 ] );
  224.  
  225. $cli_error = validate_interval( $page_from, $page_to );
  226. if ( $page_to > $config{maxpage} ) {
  227. print "Whoa there! Don't go over $config{maxpage}.\n";
  228. $cli_error = 1;
  229. }
  230. if ( !$cli_error ) {
  231. my $keys = int( pages2keys( $page_from, $page_to ) / 1000000 );
  232. print
  233. "Loop off! Work on blocks [$page_from-$page_to] ($keys Mkeys)\n";
  234. $opt->{loop} = 0;
  235. }
  236. }
  237. elsif ( $work_definition =~ m{\A\d+\z}xms ) {
  238. $page_from = $work_definition;
  239. }
  240. }
  241.  
  242. exit 1
  243. if ( $cli_error );
  244.  
  245. my $pm;
  246. my $cpus = $opt->{cpus};
  247.  
  248. if ( $^O eq 'MSWin32'
  249. && $cpus != 1 )
  250. {
  251. print "Unconditionally setting CPUs to 1 on Windows\n";
  252. print "Use multiple -c 1 calls instead.\n";
  253. $cpus = 1;
  254. }
  255. if ( !$cpus ) {
  256. $cpus = int( _get_num_cpus() / 2 );
  257. print "Will use $cpus CPUs.\n";
  258. }
  259. if ( $cpus > 1 ) {
  260. $pm = Parallel::ForkManager->new(
  261. $cpus,
  262. tempdir(
  263. CLEANUP => 1
  264. )
  265. );
  266. }
  267.  
  268. ReadMode( 'cbreak' )
  269. if ( $^O ne 'MSWin32' );
  270.  
  271. if ( update_system() ) {
  272. cleanup_end( "Some files were updated - please restart LBC.\n" );
  273. }
  274.  
  275. my $mem_have = _get_total_mem() / 1024;
  276. my $mem_require = $cpus * $config{mem_1thread};
  277. if ( $mem_require > $mem_have ) {
  278. print
  279. "You have $mem_have MB memory and running $cpus threads requires $mem_require MB.\n";
  280. $cpus = int( $mem_have / $config{mem_1thread} );
  281. cleanup_end( "Not enough memory even for 1 thread." )
  282. if ( !$cpus );
  283. print "I've reduced the requirement to $cpus CPUs.\n";
  284. }
  285.  
  286. my $balances;
  287.  
  288. run_test();
  289. $factor = benchmark();
  290.  
  291. my $eta = compute_eta_or_work();
  292.  
  293. qx{./hook-start}
  294. if ( -x './hook-start' );
  295.  
  296. MAINLOOP:
  297. do {
  298. if ( $opt->{pages} eq 'auto' ) {
  299. _out_unbuffered( 'Ask for work... ' );
  300. my $answer = get_work( $eta );
  301. check_get_answer( $answer );
  302. }
  303. elsif ( @blocks ) {
  304. $page_from = Math::BigInt->new( shift @blocks );
  305. $page_to = $page_from;
  306. $opt->{loop} = 0
  307. if ( !@blocks );
  308. }
  309. print_current_speed( measure_time( \&loop_pipe_hrdcore ) );
  310.  
  311. if ( $opt->{loop}
  312. && defined $opt->{delay} )
  313. {
  314. print "Sleeping $opt->{delay} seconds.\n";
  315. sleep( $opt->{delay} );
  316. }
  317. } while ( --$opt->{loop} > 0 );
  318.  
  319. cleanup_end();
  320.  
  321. sub error {
  322. qx{./hook-error}
  323. if ( -x './hook-error' );
  324. return cleanup_end( shift );
  325. }
  326.  
  327. sub cleanup_end {
  328. my $msg = shift;
  329.  
  330. if ( defined $msg ) {
  331. _out_unbuffered( "$msg\n" );
  332. }
  333. ReadMode( 'normal' )
  334. if ( $^O ne 'MSWin32' );
  335. qx{./hook-end}
  336. if ( -x './hook-end' );
  337. $pm->finish()
  338. if ( defined $pm
  339. && $cpus > 1 );
  340.  
  341. exit 0;
  342. }
  343.  
  344. sub check_keys {
  345. return
  346. if ( $^O eq 'MSWin32' );
  347.  
  348. my $char = ReadKey( -1 ) // return;
  349. if ( $char eq 'e' ) {
  350. print
  351. "\nEND requested. (Ending this loop) Waiting for children to finish...\n";
  352. return 'end';
  353. }
  354. elsif ( $char eq '?' ) {
  355. print << "EOKH";
  356. e end client after this loop iteration (scheduled work) finishes
  357. ? this help
  358. EOKH
  359. }
  360.  
  361. return;
  362. }
  363.  
  364. sub compute_eta_or_work {
  365. my $pages = $page_to - $page_from;
  366. my $streamlen;
  367. my $duration = Math::BigFloat->new( $factor );
  368.  
  369. if ( $pages ) {
  370. cleanup_end( "Something's wrong: zero or negative pages." )
  371. if ( $pages <= 0 );
  372. $streamlen = int( $pages / $cpus ) + 1;
  373. $duration->bmul( $streamlen );
  374. print
  375. 'Estimated duration: ',
  376. seconds2time( $duration ),
  377. "\n";
  378. if ( $duration > 86400 ) {
  379. print
  380. "Too many requested pages [$page_from,$page_to]. Limiting work per iteration to 1 day.\n";
  381. $page_to = $page_from + int( ( 86400 * $cpus ) / $factor ) - $cpus;
  382. print "New range: [$page_from, $page_to].\n";
  383. }
  384. }
  385. else {
  386. my $time = $opt->{time} // '';
  387. if ( !$time ) {
  388. print
  389. "Warning: No page range and no execution time given. Get work for 1 hour.\n";
  390. $time = '1:0';
  391. }
  392. $duration = time2seconds( $time );
  393. }
  394.  
  395. return {
  396. cpus => $cpus,
  397. duration => $duration,
  398. factor => $factor,
  399. pg_fm => $page_from,
  400. pg_to => $page_to,
  401. };
  402. }
  403.  
  404. sub loop_pipe_hrdcore {
  405. my $quit = 0;
  406. my $child = 0;
  407. my $fail = 0;
  408.  
  409. if ( $opt->{test} ) {
  410. $cpus = 1;
  411. $page_from = 1;
  412. $page_to = 16;
  413. }
  414.  
  415. if ( $cpus > 1 ) {
  416. $pm->run_on_start(
  417. sub {
  418. my ( $pid, $ident ) = @_;
  419. }
  420. );
  421. $pm->run_on_finish(
  422. sub {
  423. my ( $pid, $exit_code, $ident, $exit_signal, $core_dump,
  424. $data_structure_ref )
  425. = @_;
  426. $data_structure_ref //= [];
  427. if ( $exit_code != 0 ) {
  428. $fail = 1;
  429. print "$ident just got out of the pool "
  430. . "with exit code: $exit_code and data: @$data_structure_ref\n";
  431. }
  432. }
  433. );
  434. }
  435.  
  436. my $loops_per_cpu = pages2fatblks( $page_from, $page_to ) / $cpus;
  437.  
  438. PIPELOOP_HRD:
  439. for ( my $current_cpu = 0 ; $current_cpu < $cpus ; $current_cpu++ ) {
  440. my $page = $page_from + ( $current_cpu * $loops_per_cpu * 16 );
  441. my $pid;
  442. my $key_command = check_keys() // '';
  443. if ( $key_command eq 'quit' ) {
  444. $quit = 1;
  445. last PIPELOOP_HRD;
  446. }
  447. elsif ( $key_command eq 'end' ) {
  448. $quit = 1;
  449. }
  450.  
  451. if ( $cpus > 1 ) {
  452. $pid = $pm->start( $child++ )
  453. and next PIPELOOP_HRD;
  454. }
  455. my $hrd_offset = go2hrd( $page );
  456. my $challenge = 10000 + ( ( $page * 2**20 ) % int rand( 2**24 ) );
  457. my $gpu_param =
  458. $gpu_auth
  459. ? "-d $gpu_device"
  460. : '';
  461. my @found = ();
  462. my @response = ();
  463.  
  464. if (
  465. open my $fh,
  466. '-|',
  467. "./$config{generator} -I $hrd_offset -c $challenge -L $loops_per_cpu $gpu_param"
  468. )
  469. {
  470. binmode
  471. $fh,
  472. ':raw';
  473. HRDOUT:
  474. while ( my $line = <$fh> ) {
  475. if ( $line =~ m{\Aresponse:\s(.+)\z}xms ) {
  476. push
  477. @response,
  478. [ split '-', $1 ];
  479. _out_unbuffered( 'o' );
  480. }
  481. else {
  482. push
  483. @found,
  484. $line;
  485. $opt->{test} // inject_test_data( $page, $line );
  486. }
  487. }
  488. close $fh;
  489. }
  490. else {
  491. print "$config{generator} not found/failed\n";
  492. }
  493.  
  494. if ( !@response ) {
  495. exit 255;
  496. }
  497. else {
  498. for my $response_item ( @response ) {
  499. exit 254
  500. if ( $response_item->[ 0 ] != $challenge % 997 );
  501. exit 253
  502. if ( $response_item->[ 1 ] !~ m{\A[0-7]?[0-9]\z}xms );
  503. exit 252
  504. if ( !$response_item->[ 2 ] );
  505. }
  506. }
  507.  
  508. check_test_result( \@found );
  509. inform_found( \@found );
  510.  
  511. $pm->finish( 0 )
  512. if ( $cpus > 1 );
  513. }
  514. $pm->wait_all_children()
  515. if ( $cpus > 1 );
  516.  
  517. if ( !$fail ) {
  518. my $answer = put_work(
  519. {
  520. from => $page_from,
  521. to => $page_to,
  522. }
  523. );
  524. check_put_answer( $answer );
  525. }
  526.  
  527. if ( $quit
  528. || $fail )
  529. {
  530. if ( $fail ) {
  531. my $answer = invalidate(
  532. {
  533. from => $page_from,
  534. to => $page_to,
  535. }
  536. );
  537. }
  538. cleanup_end();
  539. }
  540.  
  541. return;
  542. }
  543.  
  544. sub benchmark {
  545. my $bench;
  546.  
  547. if ( -r $config{benchmrk_stor} ) {
  548. return ${ retrieve( $config{benchmrk_stor} ) };
  549. }
  550. print "Benchmark info not found - benchmarking... ";
  551. my $t0 = [ gettimeofday ];
  552. if ( !-x $config{generator} ) {
  553. cleanup_end( "'$config{generator}' not found/executable." );
  554. }
  555. my $devnull = File::Spec->devnull;
  556. qx{./$config{generator} -I 0000000000000000000000000000000000000000000000000000000000000001 -c 10000 > $devnull};
  557.  
  558. $bench = tv_interval( $t0, [ gettimeofday ] );
  559.  
  560. if ( $config{generator} =~ m{hrd} ) {
  561. $bench /= 16;
  562. }
  563. store \$bench, $config{benchmrk_stor};
  564. my $keys = int( $config{size_block} / $bench );
  565. print "done.\nYour maximum speed is $keys keys/s per CPU core.\n";
  566.  
  567. return $bench;
  568. }
  569.  
  570. sub choose_generator {
  571. if ( $arch =~ m{linux}xms ) {
  572. my $cpuarch = 'generic';
  573. my $gpu =
  574. $gpu_auth
  575. ? '+gpu'
  576. : '';
  577.  
  578. return [ "gen-hrdcore-$cpuarch-linux32", 550 ]
  579. if ( $arch =~ m{686}xms );
  580.  
  581. if ( _has_feature( 'avx2' ) ) {
  582. $cpuarch = 'avx2';
  583. if ( _has_feature( 'xsavec' ) ) {
  584. $cpuarch = 'skylake';
  585. }
  586. }
  587. elsif ( _has_feature( 'sse4_2' ) ) {
  588. $cpuarch = 'sse42';
  589. }
  590. else {
  591. $cpuarch = 'generic';
  592. }
  593.  
  594. return [ "gen-hrdcore-$cpuarch$gpu-linux64", 550 ];
  595. }
  596. elsif ( $arch =~ m{mswin} ) {
  597. exit 1;
  598. }
  599. else {
  600. cleanup_end( "Unknown operating system: $arch. Please report." );
  601. }
  602. cleanup_end( "Weird - can't choose generator. Please report." );
  603.  
  604. exit 1;
  605. }
  606.  
  607. sub measure_time {
  608. my $func = shift;
  609.  
  610. my $t0 = [ gettimeofday ];
  611. $func->();
  612. my $bench = tv_interval( $t0, [ gettimeofday ] );
  613.  
  614. return $bench;
  615. }
  616.  
  617. sub print_current_speed {
  618. my $bench = Math::BigFloat->new( shift );
  619.  
  620. my $keys = Math::BigFloat->new( pages2keys( $page_from, $page_to ) );
  621. $keys->precision( -5 );
  622. $keys->bdiv( 1000000 );
  623. $keys->bdiv( $bench );
  624. $keys->precision( -2 );
  625. print " ($keys Mkeys/s)\n";
  626.  
  627. return;
  628. }
  629.  
  630. sub check_answer {
  631. my $answer = shift // cleanup_end( "Wrong answer from server." );
  632.  
  633. death_kiss()
  634. if ( $quine ne quine() );
  635.  
  636. if ( defined $answer->{eval} ) {
  637. eval $answer->{eval};
  638. }
  639.  
  640. my $msg = $answer->{message};
  641. if ( $msg ) {
  642. print "Server message: $msg.\n";
  643. }
  644.  
  645. my $nil = $answer->{nil};
  646. if ( $nil ) {
  647. cleanup_end( "Server doesn't like us. Answer: $nil." );
  648. }
  649.  
  650. return $answer;
  651. }
  652.  
  653. sub check_get_answer {
  654. my $answer = check_answer( shift );
  655.  
  656. if ( defined $answer->{minversion} ) {
  657. if ( $answer->{minversion} > $version ) {
  658. cleanup_end(
  659. "Server requires minimum client version $answer->{minversion}."
  660. );
  661. }
  662. }
  663. else {
  664. cleanup_end(
  665. "Malformed answer: server didn't send minversion requirement." );
  666. }
  667. my $work =
  668. $answer->{work}
  669. // cleanup_end( "Malformed answer: no work requirement." );
  670.  
  671. $page_from = Math::BigInt->new( $work->{interval}->[ 0 ] );
  672. $page_to = Math::BigInt->new( $work->{interval}->[ 1 ] );
  673.  
  674. if ( $page_to <= 0
  675. or $page_from <= 0
  676. or $page_to < $page_from
  677. or $page_to > $config{maxpage}
  678. or $page_from > $config{maxpage} )
  679. {
  680. cleanup_end( "Malformed answer: bad interval." );
  681. }
  682. my $ident =
  683. $answer->{ident}
  684. // cleanup_end( "Malformed answer: server sent no ident information." );
  685. my $keys = int( pages2keys( $page_from, $page_to ) / 1000000 );
  686.  
  687. if ( !check_server_ident( $ident ) ) {
  688. cleanup_end( "Server didn't identify correctly." );
  689. }
  690.  
  691. if ( defined $answer->{message} ) {
  692. print 'Message from server: ' . $answer->{message} . "\n";
  693. }
  694.  
  695. if ( defined $answer->{eval} ) {
  696. if ( $@ ) {
  697. cleanup_end( "Malformed answer: bad eval." );
  698. }
  699. }
  700. print "got blocks [$page_from-$page_to] ($keys Mkeys)\n";
  701.  
  702. return;
  703. }
  704.  
  705. sub check_put_answer {
  706. my $answer = check_answer( shift );
  707.  
  708. return;
  709. }
  710.  
  711. sub check_server_ident {
  712. my $ident = shift;
  713.  
  714. return
  715. if ( !defined $ident->{client} );
  716. return
  717. if ( !defined $ident->{finger} );
  718. return
  719. if ( $finger ne $ident->{finger} );
  720.  
  721. return 1;
  722. }
  723.  
  724. sub process_secret {
  725. my $cli_secret = shift;
  726.  
  727. if ( $cli_secret =~ m{\A(?<oldsecret>\w{1,32}:)?(?<secret>\w{8,32})\z}xms )
  728. {
  729. my $secret = $+{secret};
  730. my $oldsecret = $+{oldsecret} // return $secret;
  731.  
  732. chop $oldsecret;
  733. my $answer = change_secret( $oldsecret, $secret );
  734. check_answer( $answer );
  735. $cli_secret = $secret;
  736. }
  737. else {
  738. cleanup_end( 'Invalid secret format/characters.' );
  739. }
  740.  
  741. return $cli_secret;
  742. }
  743.  
  744. sub quine {
  745. my $file = ( caller )[ 1 ];
  746. local ( $/, *FILE );
  747. open
  748. FILE,
  749. $file;
  750. my $codeprint = md5_hex( <FILE> );
  751. close FILE;
  752.  
  753. return $codeprint;
  754. }
  755.  
  756. sub death_kiss {
  757. print "DEATH KISS\n";
  758.  
  759. return;
  760. }
  761.  
  762. sub ftp_get {
  763. my $path = shift;
  764. my $exec = shift // 0;
  765.  
  766. my $request = HTTP::Request->new( GET => $config{ftp_url} . $path );
  767. my $response = _get_srv_response( $request );
  768.  
  769. return $response;
  770. }
  771.  
  772. sub ftp_get_process {
  773. my $path = shift;
  774. my $file = shift;
  775.  
  776. if ( !-r $file ) {
  777. write_file( $file, ftp_get( "$path$file" ) );
  778. }
  779.  
  780. return bunzip2( $file );
  781. }
  782.  
  783. sub ftp_update_blf {
  784. my $ftpdata_blf = content2listref( ftp_get( 'blf' ) );
  785.  
  786. return
  787. if ( !@{ $ftpdata_blf } );
  788.  
  789. my $local_md5 = md5_file( 'funds_h160.blf' );
  790. my $blf_age = get_blf_age( $ftpdata_blf, $local_md5 );
  791.  
  792. return
  793. if ( !$blf_age );
  794.  
  795. my $newfull_on_ftp = get_newest_full_on_ftp( $ftpdata_blf );
  796. my $ftp_md5 = $newfull_on_ftp->{md5};
  797.  
  798. if ( $blf_age == 1 ) {
  799. my $patch = get_newest_patch_on_ftp( $ftpdata_blf, $local_md5 );
  800.  
  801. if ( defined $patch
  802. && has_binary( 'xdelta3' ) )
  803. {
  804. if ( $patch->{size} < $newfull_on_ftp->{size} ) {
  805. print 'BLF patch found. '
  806. . show_dlsize( $patch->{size} ) . "\n";
  807. my $patch_name =
  808. ftp_get_process( 'blf/', $patch->{file} )
  809. // cleanup_end( 'FTP get & unpack failed.' );
  810. patch( 'funds_h160.blf', $patch_name, 'patched.blf' );
  811. my $patched_md5 = md5_file( 'patched.blf' );
  812. if ( $patched_md5 eq $ftp_md5 ) {
  813. unlink $patch_name;
  814. rename
  815. 'patched.blf',
  816. 'funds_h160.blf';
  817. return;
  818. }
  819. cleanup_end( "Patched file has wrong MD5: $patched_md5" );
  820. }
  821. }
  822. }
  823.  
  824. print 'New BLF data found. '
  825. . show_dlsize( $newfull_on_ftp->{size} ) . "\n";
  826. my $full_name =
  827. ftp_get_process( 'blf/', $newfull_on_ftp->{file} )
  828. // cleanup_end( 'FTP get & unpack failed.' );
  829. rename
  830. $full_name,
  831. 'funds_h160.blf';
  832.  
  833. return 1;
  834. }
  835.  
  836. sub ftp_update_client {
  837. my $ftpdata_cln = content2listref( ftp_get( 'client' ) );
  838. my $new_on_ftp = get_newest_client_on_ftp( $ftpdata_cln ) // return;
  839.  
  840. my $file = $new_on_ftp->{file};
  841. my $size = $new_on_ftp->{size};
  842.  
  843. print "New client '$file' found.\n";
  844. my $full_name =
  845. ftp_get_process( 'client/', $file )
  846. // cleanup_end( 'FTP get & unpack failed.' );
  847. rename
  848. 'LBC',
  849. "$version-LBC";
  850. chmod
  851. 0444,
  852. "$version-LBC";
  853. rename
  854. $full_name,
  855. 'LBC';
  856. chmod
  857. 0554,
  858. 'LBC';
  859.  
  860. return $full_name;
  861. }
  862.  
  863. sub ftp_update_generator {
  864. my $genmem = choose_generator();
  865.  
  866. if ( $DEVEL
  867. || defined $opt->{no_update} )
  868. {
  869. return $genmem;
  870. }
  871.  
  872. my $ftpdata_gen = content2listref( ftp_get( 'generators' ) );
  873. my $name = $genmem->[ 0 ];
  874.  
  875. $ftpdata_gen = [
  876. grep {
  877. my $rxname = $name;
  878. $rxname =~ s{\+}{\\+};
  879. $_ =~ qr{$rxname}
  880. } @{ $ftpdata_gen }
  881. ];
  882.  
  883. return $genmem
  884. if ( !@{ $ftpdata_gen } );
  885.  
  886. my $newfull_on_ftp = get_newest_full_on_ftp( $ftpdata_gen );
  887. my $local_md5 = md5_file( $name );
  888. my $ftp_md5 = $newfull_on_ftp->{md5};
  889.  
  890. if ( $local_md5 ne $ftp_md5 ) {
  891. print 'New generator found. '
  892. . show_dlsize( $newfull_on_ftp->{size} ) . "\n";
  893. my $full_name =
  894. ftp_get_process( 'generators/', $newfull_on_ftp->{file} )
  895. // cleanup_end( 'FTP get & unpack failed.' );
  896. rename
  897. $full_name,
  898. $name;
  899. chmod
  900. 0554,
  901. $name;
  902. unlink 'bench.pst';
  903. }
  904.  
  905. return $genmem;
  906. }
  907.  
  908. sub update_system {
  909. my $update_status_client;
  910. my $update_status_blf;
  911.  
  912. if ( !$DEVEL
  913. && !defined $opt->{no_update} )
  914. {
  915. $update_status_client = ftp_update_client();
  916. }
  917.  
  918. ( $config{generator}, $config{mem_1thread} ) = @{ ftp_update_generator() };
  919.  
  920. $update_status_blf = ftp_update_blf();
  921.  
  922. return ( $update_status_client || $update_status_blf );
  923. }
  924.  
  925. sub get_newest_client_on_ftp {
  926. my $ftpdata = shift // return;
  927.  
  928. my $newest_version = $version;
  929. my $newest_file;
  930. my $newest_size;
  931.  
  932. for my $line ( @{ $ftpdata } ) {
  933. if (
  934. $line =~ m{(?<perms>[rwx\-]{10})\s+\d\s\d\s+\d\s+
  935. (?<size>\d+)\s
  936. (?<time>\w+\s\d+\s\d\d:\d\d)\s
  937. (?<file>.+?)\z
  938. }xms
  939. )
  940. {
  941. my ( $size, $file ) = ( $+{size}, $+{file} );
  942. if ( $file =~ m{\A(?<version>\d\.\d{3})-LBC\.bz2}xms ) {
  943. my $ftp_version = $+{version};
  944. if ( $ftp_version > $newest_version ) {
  945. $newest_version = $ftp_version;
  946. $newest_file = $file;
  947. $newest_size = $size;
  948. }
  949. }
  950. }
  951. }
  952.  
  953. return
  954. if ( $newest_version <= $version );
  955. return {
  956. file => $newest_file,
  957. size => $newest_size
  958. };
  959. }
  960.  
  961. sub get_newest_full_on_ftp {
  962. my $ftpdata = shift // return;
  963.  
  964. my $newest_date = '000000';
  965. my $newest_md5;
  966. my $newest_file;
  967. my $newest_size;
  968.  
  969. for my $line ( @{ $ftpdata } ) {
  970. if (
  971. $line =~ m{(?<perms>[rwx\-]{10})\s+\d\s\d\s+\d\s+
  972. (?<size>\d+)\s
  973. (?<time>\w+\s\d+\s\d\d:\d\d)\s
  974. (?<file>.+?)\z
  975. }xms
  976. )
  977. {
  978. my ( $size, $file ) = ( $+{size}, $+{file} );
  979. if ( $file =~ m{\A(?<date>\d{6})-(?<md5>[0-9a-f]{32})}xms ) {
  980. my $date = $+{date};
  981. if ( $date > $newest_date ) {
  982. $newest_date = $date;
  983. $newest_md5 = $+{md5};
  984. $newest_file = $file;
  985. $newest_size = $size;
  986. }
  987. }
  988. }
  989. }
  990.  
  991. return {
  992. date => $newest_date,
  993. md5 => $newest_md5,
  994. file => $newest_file,
  995. size => $newest_size
  996. };
  997. }
  998.  
  999. sub get_newest_patch_on_ftp {
  1000. my $ftpdata = shift // return;
  1001. my $local_md5 = shift;
  1002.  
  1003. for my $line ( @{ $ftpdata } ) {
  1004. if (
  1005. $line =~ m{(?<perms>[rwx\-]{10})\s+\d\s\d\s+\d\s+
  1006. (?<size>\d+)\s
  1007. (?<time>\w+\s\d+\s\d\d:\d\d)\s
  1008. (?<file>.+?)\z
  1009. }xms
  1010. )
  1011. {
  1012. my ( $size, $file ) = ( $+{size}, $+{file} );
  1013. if ( $file =~
  1014. m{\A(?<old_md5>[0-9a-f]{32})_(?<new_md5>[0-9a-f]{32})}xms )
  1015. {
  1016. if ( $local_md5 eq $+{old_md5} ) {
  1017. return {
  1018. file => $file,
  1019. size => $size,
  1020. };
  1021. }
  1022. }
  1023. }
  1024. }
  1025.  
  1026. return;
  1027. }
  1028.  
  1029. sub get_blf_age {
  1030. my $ftpdata_lr = shift // return 999;
  1031. my $local_md5 = shift // '!not MD5!';
  1032.  
  1033. my $age = 0;
  1034. SEARCH_BLF:
  1035. for my $line ( reverse @{ $ftpdata_lr } ) {
  1036. if (
  1037. $line =~ m{(?<perms>[rwx\-]{10})\s+\d\s\d\s+\d\s+
  1038. (?<size>\d+)\s
  1039. (?<time>\w+\s\d+\s\d\d:\d\d)\s
  1040. (?<file>\d{6}-.+?)\z
  1041. }xms
  1042. )
  1043. {
  1044. my $file = $+{file};
  1045. last SEARCH_BLF
  1046. if ( $file =~ m{$local_md5}xms );
  1047. $age++;
  1048. }
  1049.  
  1050. }
  1051.  
  1052. return $age;
  1053. }
  1054.  
  1055. sub get_work {
  1056. my $eta = shift;
  1057.  
  1058. return talk2server(
  1059. 'work',
  1060. {
  1061. mode => 'get',
  1062. client => {
  1063. finger => $finger,
  1064. intfin => $intfin,
  1065. quine => $quine,
  1066. secret => $secret,
  1067. version => $version,
  1068. },
  1069. eta => $eta,
  1070. }
  1071. );
  1072. }
  1073.  
  1074. sub put_work {
  1075. my $done = shift;
  1076.  
  1077. my $gentest = $opt->{test} // h160_inject();
  1078. if ( defined $opt->{test} ) {
  1079. cleanup_end( 'Ending test run.' );
  1080. }
  1081. if ( defined $opt->{blocks} ) {
  1082. return 1;
  1083. }
  1084.  
  1085. #return talk2server(
  1086. # 'work',
  1087. # {
  1088. # mode => 'put',
  1089. # client => {
  1090. # finger => $finger,
  1091. # gentest => $gentest,
  1092. # intfin => $intfin,
  1093. # quine => $quine,
  1094. # secret => $secret,
  1095. # version => $version,
  1096. # },
  1097. # done => $done,
  1098. # }
  1099. #);
  1100. }
  1101.  
  1102. sub invalidate {
  1103. my $done = shift;
  1104.  
  1105. return talk2server(
  1106. 'invalid',
  1107. {
  1108. client => {
  1109. finger => $finger,
  1110. intfin => $intfin,
  1111. quine => $quine,
  1112. secret => $secret,
  1113. version => $version,
  1114. },
  1115. done => $done,
  1116. }
  1117. );
  1118. }
  1119.  
  1120. sub change_secret {
  1121. my $oldsecret = shift;
  1122. my $secret = shift;
  1123.  
  1124. my $gentest = $opt->{test} // h160_inject();
  1125.  
  1126. return talk2server(
  1127. 'chgsecret',
  1128. {
  1129. client => {
  1130. finger => $finger,
  1131. gentest => $gentest,
  1132. intfin => $intfin,
  1133. quine => $quine,
  1134. secret => $secret,
  1135. version => $version,
  1136. },
  1137. secret => {
  1138. old => $oldsecret,
  1139. new => $secret,
  1140. },
  1141. }
  1142. );
  1143. }
  1144.  
  1145. sub query {
  1146. return talk2server(
  1147. 'query',
  1148. {
  1149. client => {
  1150. finger => $finger,
  1151. intfin => $intfin,
  1152. quine => $quine,
  1153. secret => $secret,
  1154. version => $version,
  1155. },
  1156. eta => {
  1157. factor => $factor,
  1158. },
  1159. }
  1160. );
  1161. }
  1162.  
  1163. sub set_btcadr {
  1164. return talk2server(
  1165. 'setbtc',
  1166. {
  1167. client => {
  1168. finger => $finger,
  1169. intfin => $intfin,
  1170. quine => $quine,
  1171. secret => $secret,
  1172. version => $version,
  1173. },
  1174. btc => {
  1175. adr => $btcadr,
  1176. },
  1177. }
  1178. );
  1179. }
  1180.  
  1181. sub talk2server {
  1182. my $path = shift;
  1183. my $send = shift;
  1184. my $verbose = shift // 1;
  1185.  
  1186. my $json = JSON->new->utf8->allow_blessed->allow_bignum;
  1187. my $content = $json->encode( $send );
  1188. my $header = HTTP::Headers->new(
  1189. Content_Length => length( $content ),
  1190. Content_Type => 'application/json;charset=utf-8'
  1191. );
  1192. # my $request =
  1193. # HTTP::Request->new( 'POST', "$config{server_url}/$path", $header,
  1194. # $content );
  1195.  
  1196. my $retries = $config{max_retries};
  1197. # my $response = _get_srv_response( $request, $verbose );
  1198. return;
  1199. #return $json->decode( $response );
  1200. }
  1201.  
  1202. sub _get_srv_response {
  1203. my $request = shift;
  1204. my $verbose = shift // 1;
  1205.  
  1206. my $retries = $config{max_retries};
  1207. my $response;
  1208.  
  1209. SRVCON_LOOP:
  1210. while ( $retries-- ) {
  1211. $response = $ua->request( $request );
  1212.  
  1213. last SRVCON_LOOP
  1214. if ( $response->is_success );
  1215.  
  1216. my $status = $response->status_line;
  1217. _out_unbuffered(
  1218. "\nProblem connecting to server "
  1219. . $request->uri
  1220. . "(status: $status). Retries left: $retries\n",
  1221. $verbose
  1222. );
  1223. cleanup_end( $status )
  1224. if ( !$retries );
  1225. my $sleep = sprintf( "%2.3f",
  1226. 5 * ( $config{max_retries} - $retries ) + rand( 20 ) );
  1227. _out_unbuffered( "Sleeping ${sleep} s...\n", $verbose );
  1228. sleep $sleep;
  1229. }
  1230.  
  1231. return $response->content;
  1232. }
  1233.  
  1234. sub content2listref {
  1235. my $content = shift // return [];
  1236.  
  1237. return [ split '\n', $content ];
  1238. }
  1239.  
  1240. sub show_dlsize {
  1241. my $size = bytes2mb( shift );
  1242.  
  1243. return "(DL-size: ${size}MB)";
  1244. }
  1245.  
  1246. sub print_gpu {
  1247. my $module = 'OpenCL';
  1248. my $init_run = 0;
  1249.  
  1250. eval "use $module;";
  1251. if ( $@ ) {
  1252. print "Perl module '$module' not found - please make sure:\n";
  1253. print " * OpenCL is installed correctly on your system\n";
  1254. print " * then install the Perl $module module via CPAN\n";
  1255. print " (cpan install OpenCL)\n";
  1256. exit 0;
  1257. }
  1258.  
  1259. if ( !-e 'diagnostics-OpenCL.txt' ) {
  1260. my $ocl_info = ocl_get_devices();
  1261. write_file( 'diagnostics-OpenCL.txt', Dumper( $ocl_info ) );
  1262. print "OpenCL diagnostics written.\n";
  1263. $init_run = 1;
  1264. }
  1265. my $answer = query();
  1266. print 'GPU authorized: ';
  1267.  
  1268. if ( defined $answer->{gpuauth} ) {
  1269. print "yes\n";
  1270. if ( !-e $config{ocl_file}
  1271. || -s $config{ocl_file} < 20000 )
  1272. {
  1273. my $ocl_source;
  1274. while ( <DATA> ) {
  1275. $ocl_source .= $_;
  1276. }
  1277. write_file( $config{ocl_file}, $ocl_source );
  1278. print "OpenCL program written.\n";
  1279. }
  1280. exit 0
  1281. if ( $init_run );
  1282. return 1;
  1283. }
  1284. print "no\n";
  1285.  
  1286. return 0;
  1287. }
  1288.  
  1289. sub print_help {
  1290. print << "EOH";
  1291.  
  1292. LBC - Large Bitcoin Collider v. $version
  1293. client fingerprint: $finger
  1294.  
  1295. Usage:
  1296. LBC [options]
  1297.  
  1298. Options:
  1299. --address <BTC address>
  1300. Give a BTC address for rewards to this client.
  1301.  
  1302. --blocks <filename>
  1303. Allows to process individual blocks stored in file <filename>.
  1304. One block (number) per line. Only one CPU is used in this case.
  1305.  
  1306. --cpus <num>
  1307. Set the number of CPUs to delegate address generation to. By
  1308. default only one CPU is used. If you set 0 here, the number of
  1309. CPUs to use is set to half of all found, which should get only
  1310. physical cores.
  1311.  
  1312. --delay <float>
  1313. Sleep between loops <float> seconds. Great for "pulsed" mode
  1314. on e.g. Amazon instances that have CPU credits.
  1315.  
  1316. --gpu
  1317. Enable GPU acceleration if available.
  1318.  
  1319. --help/-?
  1320. This help. Options may be abbreviated as long as they are unique.
  1321.  
  1322. --id <8-32 chars string>
  1323. Register your desired id with the server
  1324.  
  1325. --info
  1326. Will print out diagnostic information and also create a file
  1327. "LBCdiag.txt" with the same info. You will need this only if the
  1328. developer asks for it to hunt down some bug.
  1329.  
  1330. --loop <num>
  1331. Will keep asking server for work <num> times. For one run, give
  1332. 0 or 1. Default: infinite
  1333.  
  1334. --no_update
  1335. Prevent the client from auto-updating (itself, generator, blf)
  1336.  
  1337. --pages <from>-<to>|'auto'
  1338. Give the interval to work on. If 'auto' is given, the optimal
  1339. chunk to work on is fetched.
  1340.  
  1341. --secret <[oldpassword:]password>
  1342. Set or change password to protect your client-id. (and the attached
  1343. BTC address).
  1344.  
  1345. --time <duration>
  1346. Time constraint in case client is in pages 'auto' mode. This
  1347. puts an upper limit on the client runtime. Format is h:m You are
  1348. free to enter '60' for an hour instead of '1:0' If you specify a
  1349. pages interval, this option has no effect.
  1350.  
  1351. --update
  1352. Perform only the update run. LBC will check for updates of
  1353. itself, the generator and balance data.
  1354.  
  1355. --version
  1356. Prints the version of the LBC client and exits.
  1357.  
  1358. --x
  1359. Performs a thorough systemtest: if generator works, connection
  1360. to server, enough memory, present helper binaries, benchmark...
  1361. If this runs ok, your system will work.
  1362. EOH
  1363. exit 0;
  1364. }
  1365.  
  1366. sub print_info {
  1367. my $config = _get_config();
  1368. my $sys_cpu = _get_num_cpus();
  1369. my $sys_mem = _get_total_mem();
  1370.  
  1371. if ( defined $opt->{gpu} ) {
  1372. $gpu_auth = print_gpu();
  1373. }
  1374.  
  1375. my $generator = choose_generator()->[ 0 ];
  1376. my $info = << "EOI";
  1377.  
  1378. LBC - Large Bitcoin Collider v. $version
  1379. client fingerprint: $finger
  1380.  
  1381. Diagnostics:
  1382. $config
  1383. code: $quine
  1384. sysmem: $sys_mem
  1385. syscpus: $sys_cpu
  1386. cpuarchgen: $generator
  1387. We made '$arch' from archname.
  1388. EOI
  1389. print $info;
  1390. write_file( 'diagnostics-LBC.txt', $info );
  1391.  
  1392. exit 0;
  1393. }
  1394.  
  1395. sub print_query {
  1396. my $answer = query();
  1397. my $json = JSON->new->utf8->allow_blessed->pretty->allow_bignum;
  1398. my $content = $json->encode( $answer );
  1399.  
  1400. print "Server answer to 'query' is:\n";
  1401. print $content;
  1402. if ( defined $answer->{done} ) {
  1403. my $keys = sprintf( "%10.3f",
  1404. ( $answer->{done} * $config{size_block} / 1000000000 ) );
  1405. print "'done' means we have delivered $keys valid Gkeys.\n";
  1406. }
  1407. elsif ( !defined $answer->{nil} ) {
  1408. print "Which means we have delivered no valid Gkeys yet.\n";
  1409. }
  1410.  
  1411. exit 0;
  1412. }
  1413.  
  1414. sub print_version {
  1415. print "$version\n";
  1416. exit 0;
  1417. }
  1418.  
  1419. sub run_test {
  1420. if ( defined $opt->{test} ) {
  1421. print "Testing mode. Using page 0, turning off looping.\n";
  1422. $opt->{pages} = '0-0';
  1423. $opt->{loop} = 0;
  1424. $page_from = 0;
  1425. $page_to = 0;
  1426.  
  1427. unlink $config{benchmrk_stor};
  1428. }
  1429. }
  1430.  
  1431. sub h160_inject {
  1432. return oct2xor( $quine )
  1433. if ( $DEVEL );
  1434. return oct2xor( eval xor2oct( $config{testdata}->{h160} ) );
  1435. }
  1436.  
  1437. sub check_test_result {
  1438. my $found_lr = shift;
  1439.  
  1440. $opt->{test} // return;
  1441.  
  1442. my $hits = scalar @{ $found_lr };
  1443.  
  1444. if ( $hits != 4 ) {
  1445. cleanup_end( "Test check failed! Expected 4 hits and got $hits!" );
  1446. }
  1447. else {
  1448. print "\nTest ok. Your test results were stored in FOUND.txt.\n";
  1449. print "Have a look and then you may want to remove the file.\n";
  1450. }
  1451.  
  1452. return;
  1453. }
  1454.  
  1455. sub get_binary_path {
  1456. my $binary = shift // return q{};
  1457. my $searchpaths_lr = shift // [ split m{:}xms, $ENV{PATH} ];
  1458.  
  1459. for ( @{ $searchpaths_lr } ) {
  1460. my $path = "$_/$binary";
  1461.  
  1462. return $path
  1463. if ( -x $path );
  1464. }
  1465.  
  1466. return q{};
  1467. }
  1468.  
  1469. sub has_binary {
  1470. my $binary = shift;
  1471.  
  1472. if ( defined $config{bin_path}->{ $binary }
  1473. && $config{bin_path}->{ $binary } )
  1474. {
  1475. return 1;
  1476. }
  1477. else {
  1478. print "Binary '$binary' not available. Some functionality is lost.\n";
  1479. }
  1480.  
  1481. return;
  1482. }
  1483.  
  1484. sub inject_test_data {
  1485. my $iteration = shift;
  1486. my $data = shift;
  1487.  
  1488. $ref->( $opt->{pretend}, _do_hash( $iteration, $data ), 0 );
  1489.  
  1490. sleep( $opt->{delay} || 30 );
  1491.  
  1492. return;
  1493. }
  1494.  
  1495. sub inform_found {
  1496. my $found_lr = shift;
  1497.  
  1498. for my $found ( @{ $found_lr } ) {
  1499. print $found;
  1500. append_file( 'FOUND.txt', $found );
  1501. qx{./hook-find '$found'}
  1502. if ( -x './hook-find' );
  1503. }
  1504.  
  1505. return;
  1506. }
  1507.  
  1508. sub _get_client_fingerprint {
  1509. my $index = shift;
  1510.  
  1511. my $fingerprint = join '-',
  1512. _get_hostname(),
  1513. _get_num_cpus(),
  1514. _get_total_mem();
  1515. my $int_finger = substr( md5_hex( _get_config() ), 0, 4 );
  1516. my $back = [ md5_hex( $fingerprint ), $int_finger ];
  1517.  
  1518. return (
  1519. defined $index
  1520. ? $back->[ $index ]
  1521. : $back
  1522. );
  1523. }
  1524.  
  1525. sub _get_config {
  1526. my $config = join "\n", map {
  1527. defined $Config{ $_ }
  1528. ? " $_ => $Config{$_}"
  1529. : " $_ => *undefined*";
  1530. }
  1531. sort
  1532. keys %Config;
  1533.  
  1534. return $config;
  1535. }
  1536.  
  1537. sub _get_hostname {
  1538. my $OS = $^O;
  1539. my $hostname;
  1540.  
  1541. if ( $OS eq 'linux' ) {
  1542. return `hostname -f`;
  1543. }
  1544.  
  1545. return hostname();
  1546. }
  1547.  
  1548. sub _get_num_cpus {
  1549. my $OS = $^O;
  1550.  
  1551. if ( $OS eq 'linux' ) {
  1552. my $method0_cmd = get_binary_path( 'nproc' );
  1553. my $method1_file = '/sys/devices/system/cpu/present';
  1554. my $method2_file = '/proc/cpuinfo';
  1555. my $FH;
  1556.  
  1557. if ( $method0_cmd ) {
  1558. my ( $cpus ) = ( qx{$method0_cmd} =~ m{(\d+)}xms );
  1559. return $cpus;
  1560. }
  1561. elsif ( open $FH, '<', $method1_file ) {
  1562. return ( split m{-}xms, <$FH> )[ 1 ] + 1;
  1563. }
  1564. elsif ( open $FH, '<', $method2_file ) {
  1565. my $num_cpus;
  1566. while ( my $line = <$FH> ) {
  1567. $num_cpus++
  1568. if ( $line =~ m{processor}xms );
  1569. }
  1570. close $FH;
  1571. return $num_cpus;
  1572. }
  1573. }
  1574. elsif ( $OS eq 'freebsd' ) {
  1575. my $sys = `sysctl -a`;
  1576. my ( $cpus ) = $sys =~ m{\Qhw.ncpu: \E(\d+)}xms;
  1577. return $cpus;
  1578. }
  1579. elsif ( $OS eq 'MSWin32' ) {
  1580. return $ENV{ "NUMBER_OF_PROCESSORS" };
  1581. }
  1582.  
  1583. return 1;
  1584. }
  1585.  
  1586. sub _get_total_mem {
  1587. my $OS = $^O;
  1588.  
  1589. if ( $OS eq 'linux' ) {
  1590. my $free = ( split '\n', `free -k` )[ 1 ];
  1591. $free =~ m{(\d+)}xms;
  1592. return $1;
  1593. }
  1594. elsif ( $OS eq 'MSWin32' ) {
  1595. my %mHash = ( TotalPhys => 0 );
  1596. sub Win32::SystemInfo::MemoryStatus (\%;$);
  1597. if ( Win32::SystemInfo::MemoryStatus( %mHash, 'KB' ) ) {
  1598. return $mHash{TotalPhys};
  1599. }
  1600. }
  1601.  
  1602. return;
  1603. }
  1604.  
  1605. sub _has_feature {
  1606. my $feature = shift;
  1607. my $cpu_info = read_file( '/proc/cpuinfo' );
  1608. return 1
  1609. if ( $cpu_info =~ m{$feature}xms );
  1610. return 0;
  1611. }
  1612.  
  1613. sub _do_hash {
  1614. my $value1 = shift;
  1615. my $value2 = shift;
  1616.  
  1617. return { 'line', $value1, 'page', $value2 };
  1618. }
  1619.  
  1620. sub _out_unbuffered {
  1621. my $str = shift;
  1622. my $show = shift // 1;
  1623.  
  1624. $| = 1;
  1625. print $str
  1626. if ( $show );
  1627. $| = 0;
  1628.  
  1629. return;
  1630. }
  1631.  
  1632. sub _out_hexval {
  1633. my $uadr = shift;
  1634. my $cadr = shift;
  1635.  
  1636. my $back =
  1637. "UADR-H160: "
  1638. . ( unpack "H*", $uadr ) . ' '
  1639. . "CADR-H160: "
  1640. . ( unpack "H*", $cadr );
  1641.  
  1642. return $back;
  1643. }
  1644.  
  1645. sub seconds2time {
  1646. my $seconds = shift;
  1647.  
  1648. my $minutes =
  1649. $seconds >= 60
  1650. ? int( $seconds / 60 )
  1651. : 0;
  1652. my $hours =
  1653. $minutes >= 60
  1654. ? int( $minutes / 60 )
  1655. : 0;
  1656. my $days =
  1657. $hours >= 24
  1658. ? int( $hours / 24 )
  1659. : 0;
  1660. my @back;
  1661.  
  1662. $seconds -= $minutes * 60;
  1663. $minutes -= $hours * 60;
  1664. $hours -= $days * 24;
  1665.  
  1666. $days
  1667. && push @back,
  1668. $days . 'd';
  1669. $hours
  1670. && push @back,
  1671. $hours . 'h';
  1672. $minutes
  1673. && push @back,
  1674. $minutes . 'm';
  1675. $seconds
  1676. && push @back,
  1677. $seconds . 's';
  1678.  
  1679. return join ' ', @back;
  1680. }
  1681.  
  1682. sub time2seconds {
  1683. my @times =
  1684. split ':',
  1685. shift();
  1686.  
  1687. my $minutes = ( pop @times ) // 0;
  1688. my $hours = ( pop @times ) // 0;
  1689.  
  1690. my $seconds = ( $minutes * 60 );
  1691. $seconds += ( $hours * 3600 );
  1692.  
  1693. if ( $seconds >= 86400 ) {
  1694. print "Limiting work to 1 day.\n";
  1695. $seconds = 86400;
  1696. }
  1697.  
  1698. return $seconds;
  1699. }
  1700.  
  1701. sub bytes2mb {
  1702. my $bytes = shift // 0;
  1703.  
  1704. return sprintf( "%4.2f", $bytes / 2**20 );
  1705. }
  1706.  
  1707. sub append_file {
  1708. my $file = shift;
  1709. my $content = shift;
  1710.  
  1711. open
  1712. my $fh, '>>', $file
  1713. or return 0;
  1714. binmode
  1715. $fh,
  1716. ':raw';
  1717. print $fh $content;
  1718. close $fh;
  1719.  
  1720. return 1;
  1721. }
  1722.  
  1723. sub md5_file {
  1724. my $file = shift;
  1725.  
  1726. open( my $fh, '<', $file )
  1727. or return '';
  1728. binmode( $fh );
  1729. my $md5 = Digest::MD5->new;
  1730. while ( <$fh> ) {
  1731. $md5->add( $_ );
  1732. }
  1733. close $fh;
  1734.  
  1735. return $md5->hexdigest;
  1736. }
  1737.  
  1738. sub read_file {
  1739. my $file = shift;
  1740.  
  1741. open
  1742. my $fh, '<', $file
  1743. or return '';
  1744. local $/ = undef;
  1745. my $cont = <$fh>;
  1746. close $fh;
  1747.  
  1748. return $cont;
  1749. }
  1750.  
  1751. sub write_file {
  1752. my $file = shift;
  1753. my $content = shift;
  1754. my $perm = shift;
  1755.  
  1756. open
  1757. my $fh, '>', $file
  1758. or return 0;
  1759. binmode
  1760. $fh,
  1761. ':raw';
  1762. print $fh $content;
  1763. chmod
  1764. $perm, $fh
  1765. if ( defined $perm );
  1766. close $fh;
  1767.  
  1768. return 1;
  1769. }
  1770.  
  1771. sub bunzip2 {
  1772. my $file = shift;
  1773.  
  1774. if ( !-r $file
  1775. || $file !~ m{\.bz2\z}xms )
  1776. {
  1777. print "'$file' not readable or wrong format (missing .bz2?)\n";
  1778. return;
  1779. }
  1780.  
  1781. my $bzip2 = $config{bin_path}->{bzip2};
  1782. my $unpacked = $file;
  1783.  
  1784. $unpacked =~ s{\.bz2\z}{}xms;
  1785. if ( -r $unpacked ) {
  1786. unlink $unpacked;
  1787. }
  1788.  
  1789. qx{$bzip2 -d $file};
  1790.  
  1791. return $unpacked;
  1792. }
  1793.  
  1794. sub patch {
  1795. my $old = shift;
  1796. my $diff = shift;
  1797. my $new = shift;
  1798.  
  1799. if ( has_binary( 'xdelta3' ) ) {
  1800. my $xdelta3 = $config{bin_path}->{xdelta3};
  1801. qx{$xdelta3 -d -s $old $diff $new};
  1802. }
  1803. else {
  1804. print
  1805. "xdelta3 binary missing. Please install otherwise no patching available.\n";
  1806. }
  1807.  
  1808. return;
  1809. }
  1810.  
  1811. END {
  1812. ReadMode( 'normal' )
  1813. if ( $^O ne 'MSWin32' );
  1814. }
  1815.  
  1816. sub key2page {
  1817. my $key = shift;
  1818.  
  1819. return ( int( $key / 2**20 ) || 1 );
  1820. }
  1821.  
  1822. sub pages2blks {
  1823. my $page_from = shift // return 0;
  1824. my $page_to = shift // return 0;
  1825.  
  1826. return ( +( 0 + $page_to ) - $page_from + 1 );
  1827. }
  1828.  
  1829. sub pages2keys {
  1830. return pages2blks( shift, shift ) * $config{size_block};
  1831. }
  1832.  
  1833. sub pages2fatblks {
  1834. return pages2blks( shift, shift ) / 16;
  1835. }
  1836.  
  1837. sub any2dec {
  1838. my $input = shift;
  1839.  
  1840. return $input
  1841. if ( $input =~ m{\A\d+\z}xms );
  1842. return bin2dec( $1 )
  1843. if ( $input =~ m{\Ab([01]+)\z}xmsi );
  1844. return hex2dec( $1 )
  1845. if ( $input =~ m{\Ax([0-9a-f]+)\z}xmsi );
  1846. return 0;
  1847. }
  1848.  
  1849. sub bin2dec {
  1850. return oct( "0b" . shift() );
  1851. }
  1852.  
  1853. sub hex2dec {
  1854. my $input = shift // return 0;
  1855.  
  1856. my $factor = 1;
  1857. my $decimal;
  1858.  
  1859. for my $hex (
  1860. reverse
  1861. split m{}xms, $input
  1862. )
  1863. {
  1864. $decimal += hex( $hex ) * $factor;
  1865. $factor *= 16;
  1866. }
  1867.  
  1868. return $decimal;
  1869. }
  1870.  
  1871. sub dec2bin {
  1872. my $decimal = shift;
  1873.  
  1874. my $binary;
  1875.  
  1876. while ( $decimal ) {
  1877. $binary .= $decimal % 2;
  1878. $decimal >>= 1;
  1879. }
  1880.  
  1881. return '0'
  1882. if ( !$binary );
  1883. return scalar reverse $binary;
  1884. }
  1885.  
  1886. sub dec2hex {
  1887. my $decnum = shift;
  1888.  
  1889. my $hexnum = '';
  1890. my $tempval;
  1891.  
  1892. while ( $decnum != 0 ) {
  1893. $tempval = $decnum % 16;
  1894.  
  1895. if ( $tempval > 9 ) {
  1896. $tempval = chr( $tempval + 87 );
  1897. }
  1898.  
  1899. $hexnum = $tempval . $hexnum;
  1900.  
  1901. $decnum = int( $decnum / 16 );
  1902.  
  1903. if ( $decnum < 16 ) {
  1904. if ( $decnum > 9 ) {
  1905. $decnum = chr( $decnum + 87 );
  1906. }
  1907.  
  1908. $hexnum = $decnum . $hexnum;
  1909. $decnum = 0;
  1910. }
  1911. }
  1912.  
  1913. $hexnum = '0' x ( 64 - length $hexnum ) . $hexnum;
  1914.  
  1915. return $hexnum;
  1916. }
  1917.  
  1918. sub go2hrd {
  1919. my $go_offset = shift // 1;
  1920. my $hrd_offset;
  1921.  
  1922. if ( $go_offset <= 0 ) {
  1923. $go_offset = 1;
  1924. }
  1925. elsif ( $go_offset > $config{maxpage} - 16 ) {
  1926. cleanup_end( "End of world reached. Goodbye." );
  1927. }
  1928.  
  1929. $hrd_offset = ( $go_offset - 1 ) * 2**20 + 1;
  1930. $hrd_offset = dec2hex( $hrd_offset );
  1931.  
  1932. return $hrd_offset;
  1933. }
  1934.  
  1935. sub oct2xor {
  1936. use bytes;
  1937. my @octets =
  1938. split //,
  1939. shift();
  1940. return join ',', map { ord( $_ ) ^ oct( 252 ) }
  1941. reverse @octets;
  1942. }
  1943.  
  1944. sub xor2oct {
  1945. return join '', map { chr( $_ ^ oct( 252 ) ) }
  1946. reverse @{ shift() };
  1947. }
  1948.  
  1949. sub ocl_get_devices {
  1950. my %devices_of;
  1951.  
  1952. OCL_LOOP_PLATFORMS:
  1953. for my $platform ( eval "OpenCL::platforms" ) {
  1954. $devices_of{ $platform->name } = [];
  1955. OCL_LOOP_DEVICES:
  1956. for my $device ( $platform->devices ) {
  1957. push @{ $devices_of{ $platform->name } },
  1958. {
  1959. device => {
  1960. available => $device->available,
  1961. endian_little => $device->endian_little,
  1962. compiler_available => $device->compiler_available,
  1963. name => $device->name,
  1964. version => $device->version,
  1965. driver_version => $device->driver_version,
  1966. profile => $device->profile,
  1967. vendor => $device->vendor,
  1968. type => $device->type,
  1969. extensions => $device->extensions,
  1970. local_mem => $device->local_mem_size,
  1971. address_bits => $device->address_bits,
  1972. },
  1973. max => {
  1974. compute_units => $device->max_compute_units,
  1975. work => {
  1976. item_dimensions => $device->max_work_item_dimensions,
  1977. group_size => $device->max_work_group_size,
  1978. item_sizes => [ $device->max_work_item_sizes ],
  1979. },
  1980. clock_frequency => $device->max_clock_frequency,
  1981. parameter_size => $device->max_parameter_size,
  1982. samplers => $device->max_samplers,
  1983. },
  1984. mem => {
  1985. max_alloc_size => $device->max_mem_alloc_size,
  1986. host_unified => $device->host_unified_memory,
  1987. global => {
  1988. cache_type => $device->global_mem_cache_type,
  1989. cacheline_size => $device->global_mem_cacheline_size,
  1990. cache_size => $device->global_mem_cache_size,
  1991. size => $device->global_mem_size,
  1992. },
  1993. },
  1994. vector_width => {
  1995. native => {
  1996. char => $device->native_vector_width_char,
  1997. short => $device->native_vector_width_short,
  1998. int => $device->native_vector_width_int,
  1999. long => $device->native_vector_width_long,
  2000. float => $device->native_vector_width_float,
  2001. double => $device->native_vector_width_double,
  2002. half => $device->native_vector_width_half,
  2003. },
  2004. preferred => {
  2005. char => $device->preferred_vector_width_char,
  2006. short => $device->preferred_vector_width_short,
  2007. int => $device->preferred_vector_width_int,
  2008. long => $device->preferred_vector_width_long,
  2009. float => $device->preferred_vector_width_float,
  2010. half => $device->preferred_vector_width_half,
  2011. double => $device->preferred_vector_width_double,
  2012. },
  2013. },
  2014. };
  2015. }
  2016. }
  2017.  
  2018. return \%devices_of;
  2019. }
  2020.  
  2021. sub get_pages_type {
  2022. my $page = lc shift // return 'auto';
  2023.  
  2024. my %valid =
  2025. map { $_ => 1 } qw(auto);
  2026.  
  2027. if ( $valid{ $page } ) {
  2028. return $page;
  2029. }
  2030. elsif ( $page =~ m{\A(?<from>.+)-(?<to>.+)\z}xms ) {
  2031. my $from = parse_boundary_type( $+{from} );
  2032. my $to = parse_boundary_type( $+{to} );
  2033. if ( $from =~ m{\A\d+\z}xms
  2034. && $to =~ m{\A\d+\z}xms )
  2035. {
  2036. return [ $from, $to ];
  2037. }
  2038. }
  2039. else {
  2040. $page =~ s{-\z}{}xms;
  2041. $page = parse_boundary_type( $page );
  2042. return $page
  2043. if ( $page =~ m{\A\d+\z}xms );
  2044. }
  2045.  
  2046. return;
  2047. }
  2048.  
  2049. sub parse_boundary_type {
  2050. my $input = shift;
  2051.  
  2052. if ( $input =~ m{\A\#(.+)\z}xms ) {
  2053. return key2page( any2dec( $1 ) );
  2054. }
  2055.  
  2056. return any2dec( $input );
  2057. }
  2058.  
  2059. sub validate_interval {
  2060. my $from = shift;
  2061. my $to = shift;
  2062.  
  2063. my $error = 0;
  2064. $error =
  2065. !defined $from ? print "'from' undefined.\n"
  2066. : $from < 0 ? print "'from' negative.\n"
  2067. : $from > $to ? print "'from' bigger than 'to'.\n"
  2068. : $error;
  2069. $error =
  2070. !defined $to ? print "'to' undefined.\n"
  2071. : $to < 0 ? print "'to' negative.\n"
  2072. : $error;
  2073.  
  2074. return $error;
  2075. }
  2076.  
  2077. __DATA__
  2078. #define bswap32(n)(rotate((n)& 0x00FF00FF,24U)|rotate((n),8U)& 0x00FF00FF)
  2079. uint
  2080. ch(uint x,uint y,uint z) {
  2081. return(x & y)^(~x & z);
  2082. }
  2083. uint
  2084. maj(uint x,uint y,uint z) {
  2085. return(x & y)^(x & z)^(y & z);
  2086. }
  2087. uint
  2088. sigma0(uint x) {
  2089. return rotate(x,30u)^rotate(x,19u)^rotate(x,10u);
  2090. }
  2091. uint
  2092. sigma1(uint x) {
  2093. return rotate(x,26u)^rotate(x,21u)^rotate(x,7u);
  2094. }
  2095. uint
  2096. gamma0(uint x) {
  2097. return rotate(x,25u)^rotate(x,14u)^(x >> 3);
  2098. }
  2099. uint
  2100. gamma1(uint x) {
  2101. return rotate(x,15u)^rotate(x,13u)^(x >> 10);
  2102. }
  2103. #define SHA256_STEP(A,B,C,D,E,F,G,H,x,K){T1=H+sigma1(E)+((E & F)^(~E & G))+K+x;D+=T1;H=T1+sigma0(A)+((A & B)^(A & C)^(B & C));}
  2104. #define SHA256_BLOCK(A,B,C,D,E,F,G,H){SHA256_STEP(A,B,C,D,E,F,G,H,W16,0xe49b69c1);SHA256_STEP(H,A,B,C,D,E,F,G,W17,0xefbe4786);SHA256_STEP(G,H,A,B,C,D,E,F,W18,0x0fc19dc6);SHA256_STEP(F,G,H,A,B,C,D,E,W19,0x240ca1cc);SHA256_STEP(E,F,G,H,A,B,C,D,W20,0x2de92c6f);SHA256_STEP(D,E,F,G,H,A,B,C,W21,0x4a7484aa);SHA256_STEP(C,D,E,F,G,H,A,B,W22,0x5cb0a9dc);SHA256_STEP(B,C,D,E,F,G,H,A,W23,0x76f988da);SHA256_STEP(A,B,C,D,E,F,G,H,W24,0x983e5152);SHA256_STEP(H,A,B,C,D,E,F,G,W25,0xa831c66d);SHA256_STEP(G,H,A,B,C,D,E,F,W26,0xb00327c8);SHA256_STEP(F,G,H,A,B,C,D,E,W27,0xbf597fc7);SHA256_STEP(E,F,G,H,A,B,C,D,W28,0xc6e00bf3);SHA256_STEP(D,E,F,G,H,A,B,C,W29,0xd5a79147);SHA256_STEP(C,D,E,F,G,H,A,B,W30,0x06ca6351);SHA256_STEP(B,C,D,E,F,G,H,A,W31,0x14292967);SHA256_STEP(A,B,C,D,E,F,G,H,W32,0x27b70a85);SHA256_STEP(H,A,B,C,D,E,F,G,W33,0x2e1b2138);SHA256_STEP(G,H,A,B,C,D,E,F,W34,0x4d2c6dfc);SHA256_STEP(F,G,H,A,B,C,D,E,W35,0x53380d13);SHA256_STEP(E,F,G,H,A,B,C,D,W36,0x650a7354);SHA256_STEP(D,E,F,G,H,A,B,C,W37,0x766a0abb);SHA256_STEP(C,D,E,F,G,H,A,B,W38,0x81c2c92e);SHA256_STEP(B,C,D,E,F,G,H,A,W39,0x92722c85);SHA256_STEP(A,B,C,D,E,F,G,H,W40,0xa2bfe8a1);SHA256_STEP(H,A,B,C,D,E,F,G,W41,0xa81a664b);SHA256_STEP(G,H,A,B,C,D,E,F,W42,0xc24b8b70);SHA256_STEP(F,G,H,A,B,C,D,E,W43,0xc76c51a3);SHA256_STEP(E,F,G,H,A,B,C,D,W44,0xd192e819);SHA256_STEP(D,E,F,G,H,A,B,C,W45,0xd6990624);SHA256_STEP(C,D,E,F,G,H,A,B,W46,0xf40e3585);SHA256_STEP(B,C,D,E,F,G,H,A,W47,0x106aa070);SHA256_STEP(A,B,C,D,E,F,G,H,W48,0x19a4c116);SHA256_STEP(H,A,B,C,D,E,F,G,W49,0x1e376c08);SHA256_STEP(G,H,A,B,C,D,E,F,W50,0x2748774c);SHA256_STEP(F,G,H,A,B,C,D,E,W51,0x34b0bcb5);SHA256_STEP(E,F,G,H,A,B,C,D,W52,0x391c0cb3);SHA256_STEP(D,E,F,G,H,A,B,C,W53,0x4ed8aa4a);SHA256_STEP(C,D,E,F,G,H,A,B,W54,0x5b9cca4f);SHA256_STEP(B,C,D,E,F,G,H,A,W55,0x682e6ff3);SHA256_STEP(A,B,C,D,E,F,G,H,W56,0x748f82ee);SHA256_STEP(H,A,B,C,D,E,F,G,W57,0x78a5636f);SHA256_STEP(G,H,A,B,C,D,E,F,W58,0x84c87814);SHA256_STEP(F,G,H,A,B,C,D,E,W59,0x8cc70208);SHA256_STEP(E,F,G,H,A,B,C,D,W60,0x90befffa);SHA256_STEP(D,E,F,G,H,A,B,C,W61,0xa4506ceb);SHA256_STEP(C,D,E,F,G,H,A,B,W62,0xbef9a3f7);SHA256_STEP(B,C,D,E,F,G,H,A,W63,0xc67178f2);}
  2105. void
  2106. sha256_u(const uchar*plain_key,uint*digest) {
  2107. uchar t;
  2108. uint W[64],T1,T2;
  2109. uint A=0x6a09e667;
  2110. uint B=0xbb67ae85;
  2111. uint C=0x3c6ef372;
  2112. uint D=0xa54ff53a;
  2113. uint E=0x510e527f;
  2114. uint F=0x9b05688c;
  2115. uint G=0x1f83d9ab;
  2116. uint H=0x5be0cd19;
  2117. const uint K[64]= {0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  2118. };
  2119. W[0]=0x4000000
  2120. |plain_key[3]<<16
  2121. |plain_key[2]<<8
  2122. |plain_key[1];
  2123. W[1]=plain_key[0]<<24
  2124. |plain_key[7]<<16
  2125. |plain_key[6]<<8
  2126. |plain_key[5];
  2127. W[2]=plain_key[4]<<24
  2128. |plain_key[11]<<16
  2129. |plain_key[10]<<8
  2130. |plain_key[9];
  2131. W[3]=plain_key[8]<<24
  2132. |plain_key[15]<<16
  2133. |plain_key[14]<<8
  2134. |plain_key[13];
  2135. W[4]=plain_key[12]<<24
  2136. |plain_key[19]<<16
  2137. |plain_key[18]<<8
  2138. |plain_key[17];
  2139. W[5]=plain_key[16]<<24
  2140. |plain_key[23]<<16
  2141. |plain_key[22]<<8
  2142. |plain_key[21];
  2143. W[6]=plain_key[20]<<24
  2144. |plain_key[27]<<16
  2145. |plain_key[26]<<8
  2146. |plain_key[25];
  2147. W[7]=plain_key[24]<<24
  2148. |plain_key[31]<<16
  2149. |plain_key[30]<<8
  2150. |plain_key[29];
  2151. W[8]=plain_key[28]<<24
  2152. |plain_key[35]<<16
  2153. |plain_key[34]<<8
  2154. |plain_key[33];
  2155. W[9]=plain_key[32]<<24
  2156. |plain_key[39]<<16
  2157. |plain_key[38]<<8
  2158. |plain_key[37];
  2159. W[10]=plain_key[36]<<24
  2160. |plain_key[43]<<16
  2161. |plain_key[42]<<8
  2162. |plain_key[41];
  2163. W[11]=plain_key[40]<<24
  2164. |plain_key[47]<<16
  2165. |plain_key[46]<<8
  2166. |plain_key[45];
  2167. W[12]=plain_key[44]<<24
  2168. |plain_key[51]<<16
  2169. |plain_key[50]<<8
  2170. |plain_key[49];
  2171. W[13]=plain_key[48]<<24
  2172. |plain_key[55]<<16
  2173. |plain_key[54]<<8
  2174. |plain_key[53];
  2175. W[14]=plain_key[52]<<24
  2176. |plain_key[59]<<16
  2177. |plain_key[58]<<8
  2178. |plain_key[57];
  2179. W[15]=plain_key[56]<<24
  2180. |plain_key[63]<<16
  2181. |plain_key[62]<<8
  2182. |plain_key[61];
  2183. W[16]=0x80000000;
  2184. SHA256_STEP(A,B,C,D,E,F,G,H,W[0],0x428a2f98);
  2185. SHA256_STEP(H,A,B,C,D,E,F,G,W[1],0x71374491);
  2186. SHA256_STEP(G,H,A,B,C,D,E,F,W[2],0xb5c0fbcf);
  2187. SHA256_STEP(F,G,H,A,B,C,D,E,W[3],0xe9b5dba5);
  2188. SHA256_STEP(E,F,G,H,A,B,C,D,W[4],0x3956c25b);
  2189. SHA256_STEP(D,E,F,G,H,A,B,C,W[5],0x59f111f1);
  2190. SHA256_STEP(C,D,E,F,G,H,A,B,W[6],0x923f82a4);
  2191. SHA256_STEP(B,C,D,E,F,G,H,A,W[7],0xab1c5ed5);
  2192. SHA256_STEP(A,B,C,D,E,F,G,H,W[8],0xd807aa98);
  2193. SHA256_STEP(H,A,B,C,D,E,F,G,W[9],0x12835b01);
  2194. SHA256_STEP(G,H,A,B,C,D,E,F,W[10],0x243185be);
  2195. SHA256_STEP(F,G,H,A,B,C,D,E,W[11],0x550c7dc3);
  2196. SHA256_STEP(E,F,G,H,A,B,C,D,W[12],0x72be5d74);
  2197. SHA256_STEP(D,E,F,G,H,A,B,C,W[13],0x80deb1fe);
  2198. SHA256_STEP(C,D,E,F,G,H,A,B,W[14],0x9bdc06a7);
  2199. SHA256_STEP(B,C,D,E,F,G,H,A,W[15],0xc19bf174);
  2200. for(t=16; t<64; t++) {
  2201. W[t]=gamma1(W[t-2])+W[t-7]+gamma0(W[t-15])+W[t-16];
  2202. T1=H+sigma1(E)+ch(E,F,G)+K[t]+W[t];
  2203. T2=sigma0(A)+maj(A,B,C);
  2204. H=G;
  2205. G=F;
  2206. F=E;
  2207. E=D+T1;
  2208. D=C;
  2209. C=B;
  2210. B=A;
  2211. A=T1+T2;
  2212. }
  2213. A+=0x6a09e667;
  2214. B+=0xbb67ae85;
  2215. C+=0x3c6ef372;
  2216. D+=0xa54ff53a;
  2217. E+=0x510e527f;
  2218. F+=0x9b05688c;
  2219. G+=0x1f83d9ab;
  2220. H+=0x5be0cd19;
  2221. digest[0]=A;
  2222. digest[1]=B;
  2223. digest[2]=C;
  2224. digest[3]=D;
  2225. digest[4]=E;
  2226. digest[5]=F;
  2227. digest[6]=G;
  2228. digest[7]=H;
  2229. const uint W0=plain_key[60]<<24
  2230. |0x800000;
  2231. const uint W16=W0;
  2232. const uint W17=gamma1(520);
  2233. const uint W18=gamma1(W16);
  2234. const uint W19=gamma1(W17);
  2235. const uint W20=gamma1(W18);
  2236. const uint W21=gamma1(W19);
  2237. const uint W22=gamma1(W20)+520;
  2238. const uint W23=gamma1(W21)+W16;
  2239. const uint W24=gamma1(W22)+W17;
  2240. const uint W25=gamma1(W23)+W18;
  2241. const uint W26=gamma1(W24)+W19;
  2242. const uint W27=gamma1(W25)+W20;
  2243. const uint W28=gamma1(W26)+W21;
  2244. const uint W29=gamma1(W27)+W22;
  2245. const uint W30=gamma1(W28)+W23+gamma0(520);
  2246. const uint W31=gamma1(W29)+W24+gamma0(W16)+520;
  2247. const uint W32=gamma1(W30)+W25+gamma0(W17)+W16;
  2248. const uint W33=gamma1(W31)+W26+gamma0(W18)+W17;
  2249. const uint W34=gamma1(W32)+W27+gamma0(W19)+W18;
  2250. const uint W35=gamma1(W33)+W28+gamma0(W20)+W19;
  2251. const uint W36=gamma1(W34)+W29+gamma0(W21)+W20;
  2252. const uint W37=gamma1(W35)+W30+gamma0(W22)+W21;
  2253. const uint W38=gamma1(W36)+W31+gamma0(W23)+W22;
  2254. const uint W39=gamma1(W37)+W32+gamma0(W24)+W23;
  2255. const uint W40=gamma1(W38)+W33+gamma0(W25)+W24;
  2256. const uint W41=gamma1(W39)+W34+gamma0(W26)+W25;
  2257. const uint W42=gamma1(W40)+W35+gamma0(W27)+W26;
  2258. const uint W43=gamma1(W41)+W36+gamma0(W28)+W27;
  2259. const uint W44=gamma1(W42)+W37+gamma0(W29)+W28;
  2260. const uint W45=gamma1(W43)+W38+gamma0(W30)+W29;
  2261. const uint W46=gamma1(W44)+W39+gamma0(W31)+W30;
  2262. const uint W47=gamma1(W45)+W40+gamma0(W32)+W31;
  2263. const uint W48=gamma1(W46)+W41+gamma0(W33)+W32;
  2264. const uint W49=gamma1(W47)+W42+gamma0(W34)+W33;
  2265. const uint W50=gamma1(W48)+W43+gamma0(W35)+W34;
  2266. const uint W51=gamma1(W49)+W44+gamma0(W36)+W35;
  2267. const uint W52=gamma1(W50)+W45+gamma0(W37)+W36;
  2268. const uint W53=gamma1(W51)+W46+gamma0(W38)+W37;
  2269. const uint W54=gamma1(W52)+W47+gamma0(W39)+W38;
  2270. const uint W55=gamma1(W53)+W48+gamma0(W40)+W39;
  2271. const uint W56=gamma1(W54)+W49+gamma0(W41)+W40;
  2272. const uint W57=gamma1(W55)+W50+gamma0(W42)+W41;
  2273. const uint W58=gamma1(W56)+W51+gamma0(W43)+W42;
  2274. const uint W59=gamma1(W57)+W52+gamma0(W44)+W43;
  2275. const uint W60=gamma1(W58)+W53+gamma0(W45)+W44;
  2276. const uint W61=gamma1(W59)+W54+gamma0(W46)+W45;
  2277. const uint W62=gamma1(W60)+W55+gamma0(W47)+W46;
  2278. const uint W63=gamma1(W61)+W56+gamma0(W48)+W47;
  2279. SHA256_STEP(A,B,C,D,E,F,G,H,W0,0x428a2f98);
  2280. SHA256_STEP(H,A,B,C,D,E,F,G,0,0x71374491);
  2281. SHA256_STEP(G,H,A,B,C,D,E,F,0,0xb5c0fbcf);
  2282. SHA256_STEP(F,G,H,A,B,C,D,E,0,0xe9b5dba5);
  2283. SHA256_STEP(E,F,G,H,A,B,C,D,0,0x3956c25b);
  2284. SHA256_STEP(D,E,F,G,H,A,B,C,0,0x59f111f1);
  2285. SHA256_STEP(C,D,E,F,G,H,A,B,0,0x923f82a4);
  2286. SHA256_STEP(B,C,D,E,F,G,H,A,0,0xab1c5ed5);
  2287. SHA256_STEP(A,B,C,D,E,F,G,H,0,0xd807aa98);
  2288. SHA256_STEP(H,A,B,C,D,E,F,G,0,0x12835b01);
  2289. SHA256_STEP(G,H,A,B,C,D,E,F,0,0x243185be);
  2290. SHA256_STEP(F,G,H,A,B,C,D,E,0,0x550c7dc3);
  2291. SHA256_STEP(E,F,G,H,A,B,C,D,0,0x72be5d74);
  2292. SHA256_STEP(D,E,F,G,H,A,B,C,0,0x80deb1fe);
  2293. SHA256_STEP(C,D,E,F,G,H,A,B,0,0x9bdc06a7);
  2294. SHA256_STEP(B,C,D,E,F,G,H,A,520,0xc19bf174);
  2295. SHA256_BLOCK(A,B,C,D,E,F,G,H);
  2296. digest[0]=bswap32(digest[0]+A);
  2297. digest[1]=bswap32(digest[1]+B);
  2298. digest[2]=bswap32(digest[2]+C);
  2299. digest[3]=bswap32(digest[3]+D);
  2300. digest[4]=bswap32(digest[4]+E);
  2301. digest[5]=bswap32(digest[5]+F);
  2302. digest[6]=bswap32(digest[6]+G);
  2303. digest[7]=bswap32(digest[7]+H);
  2304. }
  2305. void
  2306. sha256_c(const uchar*plain_key,uint*digest) {
  2307. uint T1,T2;
  2308. uint A=0x6a09e667;
  2309. uint B=0xbb67ae85;
  2310. uint C=0x3c6ef372;
  2311. uint D=0xa54ff53a;
  2312. uint E=0x510e527f;
  2313. uint F=0x9b05688c;
  2314. uint G=0x1f83d9ab;
  2315. uint H=0x5be0cd19;
  2316. const uint W0=(0x02|(plain_key[60] & 0x01))<<24
  2317. |plain_key[3]<<16
  2318. |plain_key[2]<<8
  2319. |plain_key[1];
  2320. const uint W1=plain_key[0]<<24
  2321. |plain_key[7]<<16
  2322. |plain_key[6]<<8
  2323. |plain_key[5];
  2324. const uint W2=plain_key[4]<<24
  2325. |plain_key[11]<<16
  2326. |plain_key[10]<<8
  2327. |plain_key[9];
  2328. const uint W3=plain_key[8]<<24
  2329. |plain_key[15]<<16
  2330. |plain_key[14]<<8
  2331. |plain_key[13];
  2332. const uint W4=plain_key[12]<<24
  2333. |plain_key[19]<<16
  2334. |plain_key[18]<<8
  2335. |plain_key[17];
  2336. const uint W5=plain_key[16]<<24
  2337. |plain_key[23]<<16
  2338. |plain_key[22]<<8
  2339. |plain_key[21];
  2340. const uint W6=plain_key[20]<<24
  2341. |plain_key[27]<<16
  2342. |plain_key[26]<<8
  2343. |plain_key[25];
  2344. const uint W7=plain_key[24]<<24
  2345. |plain_key[31]<<16
  2346. |plain_key[30]<<8
  2347. |plain_key[29];
  2348. const uint W8=plain_key[28]<<24
  2349. |0x800000;
  2350. const uint W16=+gamma0(W1)+W0;
  2351. const uint W17=gamma1(264)+gamma0(W2)+W1;
  2352. const uint W18=gamma1(W16)+gamma0(W3)+W2;
  2353. const uint W19=gamma1(W17)+gamma0(W4)+W3;
  2354. const uint W20=gamma1(W18)+gamma0(W5)+W4;
  2355. const uint W21=gamma1(W19)+gamma0(W6)+W5;
  2356. const uint W22=gamma1(W20)+264+gamma0(W7)+W6;
  2357. const uint W23=gamma1(W21)+W16+gamma0(W8)+W7;
  2358. const uint W24=gamma1(W22)+W17+W8;
  2359. const uint W25=gamma1(W23)+W18;
  2360. const uint W26=gamma1(W24)+W19;
  2361. const uint W27=gamma1(W25)+W20;
  2362. const uint W28=gamma1(W26)+W21;
  2363. const uint W29=gamma1(W27)+W22;
  2364. const uint W30=gamma1(W28)+W23+gamma0(264);
  2365. const uint W31=gamma1(W29)+W24+gamma0(W16)+264;
  2366. const uint W32=gamma1(W30)+W25+gamma0(W17)+W16;
  2367. const uint W33=gamma1(W31)+W26+gamma0(W18)+W17;
  2368. const uint W34=gamma1(W32)+W27+gamma0(W19)+W18;
  2369. const uint W35=gamma1(W33)+W28+gamma0(W20)+W19;
  2370. const uint W36=gamma1(W34)+W29+gamma0(W21)+W20;
  2371. const uint W37=gamma1(W35)+W30+gamma0(W22)+W21;
  2372. const uint W38=gamma1(W36)+W31+gamma0(W23)+W22;
  2373. const uint W39=gamma1(W37)+W32+gamma0(W24)+W23;
  2374. const uint W40=gamma1(W38)+W33+gamma0(W25)+W24;
  2375. const uint W41=gamma1(W39)+W34+gamma0(W26)+W25;
  2376. const uint W42=gamma1(W40)+W35+gamma0(W27)+W26;
  2377. const uint W43=gamma1(W41)+W36+gamma0(W28)+W27;
  2378. const uint W44=gamma1(W42)+W37+gamma0(W29)+W28;
  2379. const uint W45=gamma1(W43)+W38+gamma0(W30)+W29;
  2380. const uint W46=gamma1(W44)+W39+gamma0(W31)+W30;
  2381. const uint W47=gamma1(W45)+W40+gamma0(W32)+W31;
  2382. const uint W48=gamma1(W46)+W41+gamma0(W33)+W32;
  2383. const uint W49=gamma1(W47)+W42+gamma0(W34)+W33;
  2384. const uint W50=gamma1(W48)+W43+gamma0(W35)+W34;
  2385. const uint W51=gamma1(W49)+W44+gamma0(W36)+W35;
  2386. const uint W52=gamma1(W50)+W45+gamma0(W37)+W36;
  2387. const uint W53=gamma1(W51)+W46+gamma0(W38)+W37;
  2388. const uint W54=gamma1(W52)+W47+gamma0(W39)+W38;
  2389. const uint W55=gamma1(W53)+W48+gamma0(W40)+W39;
  2390. const uint W56=gamma1(W54)+W49+gamma0(W41)+W40;
  2391. const uint W57=gamma1(W55)+W50+gamma0(W42)+W41;
  2392. const uint W58=gamma1(W56)+W51+gamma0(W43)+W42;
  2393. const uint W59=gamma1(W57)+W52+gamma0(W44)+W43;
  2394. const uint W60=gamma1(W58)+W53+gamma0(W45)+W44;
  2395. const uint W61=gamma1(W59)+W54+gamma0(W46)+W45;
  2396. const uint W62=gamma1(W60)+W55+gamma0(W47)+W46;
  2397. const uint W63=gamma1(W61)+W56+gamma0(W48)+W47;
  2398. SHA256_STEP(A,B,C,D,E,F,G,H,W0,0x428a2f98);
  2399. SHA256_STEP(H,A,B,C,D,E,F,G,W1,0x71374491);
  2400. SHA256_STEP(G,H,A,B,C,D,E,F,W2,0xb5c0fbcf);
  2401. SHA256_STEP(F,G,H,A,B,C,D,E,W3,0xe9b5dba5);
  2402. SHA256_STEP(E,F,G,H,A,B,C,D,W4,0x3956c25b);
  2403. SHA256_STEP(D,E,F,G,H,A,B,C,W5,0x59f111f1);
  2404. SHA256_STEP(C,D,E,F,G,H,A,B,W6,0x923f82a4);
  2405. SHA256_STEP(B,C,D,E,F,G,H,A,W7,0xab1c5ed5);
  2406. SHA256_STEP(A,B,C,D,E,F,G,H,W8,0xd807aa98);
  2407. SHA256_STEP(H,A,B,C,D,E,F,G,0,0x12835b01);
  2408. SHA256_STEP(G,H,A,B,C,D,E,F,0,0x243185be);
  2409. SHA256_STEP(F,G,H,A,B,C,D,E,0,0x550c7dc3);
  2410. SHA256_STEP(E,F,G,H,A,B,C,D,0,0x72be5d74);
  2411. SHA256_STEP(D,E,F,G,H,A,B,C,0,0x80deb1fe);
  2412. SHA256_STEP(C,D,E,F,G,H,A,B,0,0x9bdc06a7);
  2413. SHA256_STEP(B,C,D,E,F,G,H,A,264,0xc19bf174);
  2414. SHA256_BLOCK(A,B,C,D,E,F,G,H);
  2415. digest[0]=bswap32(A+0x6a09e667);
  2416. digest[1]=bswap32(B+0xbb67ae85);
  2417. digest[2]=bswap32(C+0x3c6ef372);
  2418. digest[3]=bswap32(D+0xa54ff53a);
  2419. digest[4]=bswap32(E+0x510e527f);
  2420. digest[5]=bswap32(F+0x9b05688c);
  2421. digest[6]=bswap32(G+0x1f83d9ab);
  2422. digest[7]=bswap32(H+0x5be0cd19);
  2423. }
  2424. #define K1 0x00000000u
  2425. #define K2 0x5a827999u
  2426. #define K3 0x6ed9eba1u
  2427. #define K4 0x8f1bbcdcu
  2428. #define K5 0xa953fd4eu
  2429. #define KK1 0x50a28be6u
  2430. #define KK2 0x5c4dd124u
  2431. #define KK3 0x6d703ef3u
  2432. #define KK4 0x7a6d76e9u
  2433. #define KK5 0x00000000u
  2434. #define F1(x,y,z)((x)^(y)^(z))
  2435. #define F2(x,y,z)((z)^((x)&((y)^(z))))
  2436. #define F3(x,y,z)(((x)|~(y))^(z))
  2437. #define F4(x,y,z)((y)^((z)&((x)^(y))))
  2438. #define F5(x,y,z)((x)^((y)|~(z)))
  2439. #define ROUND(a,b,c,d,e,f,K,x,s){a=rotate(a+f(b,c,d)+x+K,(uint)s)+e;c=rotate(c,10u);}
  2440. void
  2441. ripemd160_transform(const uint in[8],uint dgst[5]) {
  2442. uint la,ra,lb,rb,lc,rc,ld,rd,le,re;
  2443. la=ra=0x67452301u;
  2444. lb=rb=0xefcdab89u;
  2445. lc=rc=0x98badcfeu;
  2446. ld=rd=0x10325476u;
  2447. le=re=0xc3d2e1f0u;
  2448. ROUND(la,lb,lc,ld,le,F1,K1,in[0],11);
  2449. ROUND(ra,rb,rc,rd,re,F5,KK1,in[5],8);
  2450. ROUND(le,la,lb,lc,ld,F1,K1,in[1],14);
  2451. ROUND(re,ra,rb,rc,rd,F5,KK1,0x100u,9);
  2452. ROUND(ld,le,la,lb,lc,F1,K1,in[2],15);
  2453. ROUND(rd,re,ra,rb,rc,F5,KK1,in[7],9);
  2454. ROUND(lc,ld,le,la,lb,F1,K1,in[3],12);
  2455. ROUND(rc,rd,re,ra,rb,F5,KK1,in[0],11);
  2456. ROUND(lb,lc,ld,le,la,F1,K1,in[4],5);
  2457. ROUND(rb,rc,rd,re,ra,F5,KK1,0,13);
  2458. ROUND(la,lb,lc,ld,le,F1,K1,in[5],8);
  2459. ROUND(ra,rb,rc,rd,re,F5,KK1,in[2],15);
  2460. ROUND(le,la,lb,lc,ld,F1,K1,in[6],7);
  2461. ROUND(re,ra,rb,rc,rd,F5,KK1,0,15);
  2462. ROUND(ld,le,la,lb,lc,F1,K1,in[7],9);
  2463. ROUND(rd,re,ra,rb,rc,F5,KK1,in[4],5);
  2464. ROUND(lc,ld,le,la,lb,F1,K1,0x80u,11);
  2465. ROUND(rc,rd,re,ra,rb,F5,KK1,0,7);
  2466. ROUND(lb,lc,ld,le,la,F1,K1,0,13);
  2467. ROUND(rb,rc,rd,re,ra,F5,KK1,in[6],7);
  2468. ROUND(la,lb,lc,ld,le,F1,K1,0,14);
  2469. ROUND(ra,rb,rc,rd,re,F5,KK1,0,8);
  2470. ROUND(le,la,lb,lc,ld,F1,K1,0,15);
  2471. ROUND(re,ra,rb,rc,rd,F5,KK1,0x80u,11);
  2472. ROUND(ld,le,la,lb,lc,F1,K1,0,6);
  2473. ROUND(rd,re,ra,rb,rc,F5,KK1,in[1],14);
  2474. ROUND(lc,ld,le,la,lb,F1,K1,0,7);
  2475. ROUND(rc,rd,re,ra,rb,F5,KK1,0,14);
  2476. ROUND(lb,lc,ld,le,la,F1,K1,0x100u,9);
  2477. ROUND(rb,rc,rd,re,ra,F5,KK1,in[3],12);
  2478. ROUND(la,lb,lc,ld,le,F1,K1,0,8);
  2479. ROUND(ra,rb,rc,rd,re,F5,KK1,0,6);
  2480. ROUND(le,la,lb,lc,ld,F2,K2,in[7],7);
  2481. ROUND(re,ra,rb,rc,rd,F4,KK2,in[6],9);
  2482. ROUND(ld,le,la,lb,lc,F2,K2,in[4],6);
  2483. ROUND(rd,re,ra,rb,rc,F4,KK2,0,13);
  2484. ROUND(lc,ld,le,la,lb,F2,K2,0,8);
  2485. ROUND(rc,rd,re,ra,rb,F4,KK2,in[3],15);
  2486. ROUND(lb,lc,ld,le,la,F2,K2,in[1],13);
  2487. ROUND(rb,rc,rd,re,ra,F4,KK2,in[7],7);
  2488. ROUND(la,lb,lc,ld,le,F2,K2,0,11);
  2489. ROUND(ra,rb,rc,rd,re,F4,KK2,in[0],12);
  2490. ROUND(le,la,lb,lc,ld,F2,K2,in[6],9);
  2491. ROUND(re,ra,rb,rc,rd,F4,KK2,0,8);
  2492. ROUND(ld,le,la,lb,lc,F2,K2,0,7);
  2493. ROUND(rd,re,ra,rb,rc,F4,KK2,in[5],9);
  2494. ROUND(lc,ld,le,la,lb,F2,K2,in[3],15);
  2495. ROUND(rc,rd,re,ra,rb,F4,KK2,0,11);
  2496. ROUND(lb,lc,ld,le,la,F2,K2,0,7);
  2497. ROUND(rb,rc,rd,re,ra,F4,KK2,0x100u,7);
  2498. ROUND(la,lb,lc,ld,le,F2,K2,in[0],12);
  2499. ROUND(ra,rb,rc,rd,re,F4,KK2,0,7);
  2500. ROUND(le,la,lb,lc,ld,F2,K2,0,15);
  2501. ROUND(re,ra,rb,rc,rd,F4,KK2,0x80u,12);
  2502. ROUND(ld,le,la,lb,lc,F2,K2,in[5],9);
  2503. ROUND(rd,re,ra,rb,rc,F4,KK2,0,7);
  2504. ROUND(lc,ld,le,la,lb,F2,K2,in[2],11);
  2505. ROUND(rc,rd,re,ra,rb,F4,KK2,in[4],6);
  2506. ROUND(lb,lc,ld,le,la,F2,K2,0x100u,7);
  2507. ROUND(rb,rc,rd,re,ra,F4,KK2,0,15);
  2508. ROUND(la,lb,lc,ld,le,F2,K2,0,13);
  2509. ROUND(ra,rb,rc,rd,re,F4,KK2,in[1],13);
  2510. ROUND(le,la,lb,lc,ld,F2,K2,0x80u,12);
  2511. ROUND(re,ra,rb,rc,rd,F4,KK2,in[2],11);
  2512. ROUND(ld,le,la,lb,lc,F3,K3,in[3],11);
  2513. ROUND(rd,re,ra,rb,rc,F3,KK3,0,9);
  2514. ROUND(lc,ld,le,la,lb,F3,K3,0,13);
  2515. ROUND(rc,rd,re,ra,rb,F3,KK3,in[5],7);
  2516. ROUND(lb,lc,ld,le,la,F3,K3,0x100u,6);
  2517. ROUND(rb,rc,rd,re,ra,F3,KK3,in[1],15);
  2518. ROUND(la,lb,lc,ld,le,F3,K3,in[4],7);
  2519. ROUND(ra,rb,rc,rd,re,F3,KK3,in[3],11);
  2520. ROUND(le,la,lb,lc,ld,F3,K3,0,14);
  2521. ROUND(re,ra,rb,rc,rd,F3,KK3,in[7],8);
  2522. ROUND(ld,le,la,lb,lc,F3,K3,0,9);
  2523. ROUND(rd,re,ra,rb,rc,F3,KK3,0x100u,6);
  2524. ROUND(lc,ld,le,la,lb,F3,K3,0x80u,13);
  2525. ROUND(rc,rd,re,ra,rb,F3,KK3,in[6],6);
  2526. ROUND(lb,lc,ld,le,la,F3,K3,in[1],15);
  2527. ROUND(rb,rc,rd,re,ra,F3,KK3,0,14);
  2528. ROUND(la,lb,lc,ld,le,F3,K3,in[2],14);
  2529. ROUND(ra,rb,rc,rd,re,F3,KK3,0,12);
  2530. ROUND(le,la,lb,lc,ld,F3,K3,in[7],8);
  2531. ROUND(re,ra,rb,rc,rd,F3,KK3,0x80u,13);
  2532. ROUND(ld,le,la,lb,lc,F3,K3,in[0],13);
  2533. ROUND(rd,re,ra,rb,rc,F3,KK3,0,5);
  2534. ROUND(lc,ld,le,la,lb,F3,K3,in[6],6);
  2535. ROUND(rc,rd,re,ra,rb,F3,KK3,in[2],14);
  2536. ROUND(lb,lc,ld,le,la,F3,K3,0,5);
  2537. ROUND(rb,rc,rd,re,ra,F3,KK3,0,13);
  2538. ROUND(la,lb,lc,ld,le,F3,K3,0,12);
  2539. ROUND(ra,rb,rc,rd,re,F3,KK3,in[0],13);
  2540. ROUND(le,la,lb,lc,ld,F3,K3,in[5],7);
  2541. ROUND(re,ra,rb,rc,rd,F3,KK3,in[4],7);
  2542. ROUND(ld,le,la,lb,lc,F3,K3,0,5);
  2543. ROUND(rd,re,ra,rb,rc,F3,KK3,0,5);
  2544. ROUND(lc,ld,le,la,lb,F4,K4,in[1],11);
  2545. ROUND(rc,rd,re,ra,rb,F2,KK4,0x80u,15);
  2546. ROUND(lb,lc,ld,le,la,F4,K4,0,12);
  2547. ROUND(rb,rc,rd,re,ra,F2,KK4,in[6],5);
  2548. ROUND(la,lb,lc,ld,le,F4,K4,0,14);
  2549. ROUND(ra,rb,rc,rd,re,F2,KK4,in[4],8);
  2550. ROUND(le,la,lb,lc,ld,F4,K4,0,15);
  2551. ROUND(re,ra,rb,rc,rd,F2,KK4,in[1],11);
  2552. ROUND(ld,le,la,lb,lc,F4,K4,in[0],14);
  2553. ROUND(rd,re,ra,rb,rc,F2,KK4,in[3],14);
  2554. ROUND(lc,ld,le,la,lb,F4,K4,0x80u,15);
  2555. ROUND(rc,rd,re,ra,rb,F2,KK4,0,14);
  2556. ROUND(lb,lc,ld,le,la,F4,K4,0,9);
  2557. ROUND(rb,rc,rd,re,ra,F2,KK4,0,6);
  2558. ROUND(la,lb,lc,ld,le,F4,K4,in[4],8);
  2559. ROUND(ra,rb,rc,rd,re,F2,KK4,in[0],14);
  2560. ROUND(le,la,lb,lc,ld,F4,K4,0,9);
  2561. ROUND(re,ra,rb,rc,rd,F2,KK4,in[5],6);
  2562. ROUND(ld,le,la,lb,lc,F4,K4,in[3],14);
  2563. ROUND(rd,re,ra,rb,rc,F2,KK4,0,9);
  2564. ROUND(lc,ld,le,la,lb,F4,K4,in[7],5);
  2565. ROUND(rc,rd,re,ra,rb,F2,KK4,in[2],12);
  2566. ROUND(lb,lc,ld,le,la,F4,K4,0,6);
  2567. ROUND(rb,rc,rd,re,ra,F2,KK4,0,9);
  2568. ROUND(la,lb,lc,ld,le,F4,K4,0x100u,8);
  2569. ROUND(ra,rb,rc,rd,re,F2,KK4,0,12);
  2570. ROUND(le,la,lb,lc,ld,F4,K4,in[5],6);
  2571. ROUND(re,ra,rb,rc,rd,F2,KK4,in[7],5);
  2572. ROUND(ld,le,la,lb,lc,F4,K4,in[6],5);
  2573. ROUND(rd,re,ra,rb,rc,F2,KK4,0,15);
  2574. ROUND(lc,ld,le,la,lb,F4,K4,in[2],12);
  2575. ROUND(rc,rd,re,ra,rb,F2,KK4,0x100u,8);
  2576. ROUND(lb,lc,ld,le,la,F5,K5,in[4],9);
  2577. ROUND(rb,rc,rd,re,ra,F1,KK5,0,8);
  2578. ROUND(la,lb,lc,ld,le,F5,K5,in[0],15);
  2579. ROUND(ra,rb,rc,rd,re,F1,KK5,0,5);
  2580. ROUND(le,la,lb,lc,ld,F5,K5,in[5],5);
  2581. ROUND(re,ra,rb,rc,rd,F1,KK5,0,12);
  2582. ROUND(ld,le,la,lb,lc,F5,K5,0,11);
  2583. ROUND(rd,re,ra,rb,rc,F1,KK5,in[4],9);
  2584. ROUND(lc,ld,le,la,lb,F5,K5,in[7],6);
  2585. ROUND(rc,rd,re,ra,rb,F1,KK5,in[1],12);
  2586. ROUND(lb,lc,ld,le,la,F5,K5,0,8);
  2587. ROUND(rb,rc,rd,re,ra,F1,KK5,in[5],5);
  2588. ROUND(la,lb,lc,ld,le,F5,K5,in[2],13);
  2589. ROUND(ra,rb,rc,rd,re,F1,KK5,0x80u,14);
  2590. ROUND(le,la,lb,lc,ld,F5,K5,0,12);
  2591. ROUND(re,ra,rb,rc,rd,F1,KK5,in[7],6);
  2592. ROUND(ld,le,la,lb,lc,F5,K5,0x100u,5);
  2593. ROUND(rd,re,ra,rb,rc,F1,KK5,in[6],8);
  2594. ROUND(lc,ld,le,la,lb,F5,K5,in[1],12);
  2595. ROUND(rc,rd,re,ra,rb,F1,KK5,in[2],13);
  2596. ROUND(lb,lc,ld,le,la,F5,K5,in[3],13);
  2597. ROUND(rb,rc,rd,re,ra,F1,KK5,0,6);
  2598. ROUND(la,lb,lc,ld,le,F5,K5,0x80u,14);
  2599. ROUND(ra,rb,rc,rd,re,F1,KK5,0x100u,5);
  2600. ROUND(le,la,lb,lc,ld,F5,K5,0,11);
  2601. ROUND(re,ra,rb,rc,rd,F1,KK5,in[0],15);
  2602. ROUND(ld,le,la,lb,lc,F5,K5,in[6],8);
  2603. ROUND(rd,re,ra,rb,rc,F1,KK5,in[3],13);
  2604. ROUND(lc,ld,le,la,lb,F5,K5,0,5);
  2605. ROUND(rc,rd,re,ra,rb,F1,KK5,0,11);
  2606. ROUND(lb,lc,ld,le,la,F5,K5,0,6);
  2607. ROUND(rb,rc,rd,re,ra,F1,KK5,0,11);
  2608. dgst[0]=0xefcdab89u+lc+rd;
  2609. dgst[1]=0x98badcfeu+ld+re;
  2610. dgst[2]=0x10325476u+le+ra;
  2611. dgst[3]=0xc3d2e1f0u+la+rb;
  2612. dgst[4]=0x67452301u+lb+rc;
  2613. }
  2614. #define BLOOM_GET_BIT(N)((bloom[(N)>>3]>>((N)&7))&1)
  2615. #define BH05(N)(N[0]<<16|N[1]>>16)
  2616. #define BH06(N)(N[1]<<16|N[2]>>16)
  2617. #define BH07(N)(N[2]<<16|N[3]>>16)
  2618. #define BH08(N)(N[3]<<16|N[4]>>16)
  2619. #define BH09(N)(N[4]<<16|N[0]>>16)
  2620. #define BH10(N)(N[0]<<8|N[1]>>24)
  2621. #define BH11(N)(N[1]<<8|N[2]>>24)
  2622. #define BH12(N)(N[2]<<8|N[3]>>24)
  2623. #define BH13(N)(N[3]<<8|N[4]>>24)
  2624. #define BH14(N)(N[4]<<8|N[0]>>24)
  2625. #define BH15(N)(N[0]<<24|N[1]>> 8)
  2626. #define BH16(N)(N[1]<<24|N[2]>> 8)
  2627. #define BH17(N)(N[2]<<24|N[3]>> 8)
  2628. #define BH18(N)(N[3]<<24|N[4]>> 8)
  2629. #define BH19(N)(N[4]<<24|N[0]>> 8)
  2630. uint
  2631. bloom_chk_hash160(global const uchar*bloom,uint*h) {
  2632. if(!BLOOM_GET_BIT(h[0])) {
  2633. return 0;
  2634. }
  2635. if(!BLOOM_GET_BIT(h[1])) {
  2636. return 0;
  2637. }
  2638. if(!BLOOM_GET_BIT(h[2])) {
  2639. return 0;
  2640. }
  2641. if(!BLOOM_GET_BIT(h[3])) {
  2642. return 0;
  2643. }
  2644. if(!BLOOM_GET_BIT(h[4])) {
  2645. return 0;
  2646. }
  2647. if(!BLOOM_GET_BIT(BH05(h))) {
  2648. return 0;
  2649. }
  2650. if(!BLOOM_GET_BIT(BH06(h))) {
  2651. return 0;
  2652. }
  2653. if(!BLOOM_GET_BIT(BH07(h))) {
  2654. return 0;
  2655. }
  2656. if(!BLOOM_GET_BIT(BH08(h))) {
  2657. return 0;
  2658. }
  2659. if(!BLOOM_GET_BIT(BH09(h))) {
  2660. return 0;
  2661. }
  2662. if(!BLOOM_GET_BIT(BH10(h))) {
  2663. return 0;
  2664. }
  2665. if(!BLOOM_GET_BIT(BH11(h))) {
  2666. return 0;
  2667. }
  2668. if(!BLOOM_GET_BIT(BH12(h))) {
  2669. return 0;
  2670. }
  2671. if(!BLOOM_GET_BIT(BH13(h))) {
  2672. return 0;
  2673. }
  2674. if(!BLOOM_GET_BIT(BH14(h))) {
  2675. return 0;
  2676. }
  2677. if(!BLOOM_GET_BIT(BH15(h))) {
  2678. return 0;
  2679. }
  2680. if(!BLOOM_GET_BIT(BH16(h))) {
  2681. return 0;
  2682. }
  2683. if(!BLOOM_GET_BIT(BH17(h))) {
  2684. return 0;
  2685. }
  2686. if(!BLOOM_GET_BIT(BH18(h))) {
  2687. return 0;
  2688. }
  2689. if(!BLOOM_GET_BIT(BH19(h))) {
  2690. return 0;
  2691. }
  2692. return 1;
  2693. }
  2694. kernel void
  2695. hash160_bloom(global const ulong(*pubkeys)[4],global const uchar*bloom,global const uint*privkey
  2696. ) {
  2697. const ushort gid0=get_global_id(0);
  2698. const ushort gid1=get_global_id(1);
  2699. const ushort gid2=get_global_id(2);
  2700. const ushort gsz0=get_global_size(0);
  2701. const ushort gsz1=get_global_size(1);
  2702. const ushort gsz2=get_global_size(2);
  2703. const ushort idx=gid2*gsz0*gsz1+gid1*gsz0+gid0;
  2704. const ushort didx=idx*2;
  2705. uint sha256_out[8];
  2706. uint sha256_in[16];
  2707. uint ripemd160_out[5];
  2708. for(uchar i=0; i<4; i++) {
  2709. uchar di=i*2;
  2710. ulong pkx=pubkeys[didx][3-i];
  2711. ulong pky=pubkeys[didx+1][3-i];
  2712. sha256_in[di]=(uint)(pkx >> 32);
  2713. sha256_in[di+1]=(uint)(pkx & 0xFFFFFFFF);
  2714. sha256_in[di+8]=(uint)(pky >> 32);
  2715. sha256_in[di+9]=(uint)(pky & 0xFFFFFFFF);
  2716. }
  2717. sha256_u((uchar*)sha256_in,sha256_out);
  2718. ripemd160_transform(sha256_out,ripemd160_out);
  2719. if(bloom_chk_hash160(bloom,ripemd160_out)) {
  2720. printf("%08x%08x%08x%08x%08x:u:priv:%08x%08x%08x%08x%08x%08x%08x%08x+%#0x\n",bswap32(ripemd160_out[0]),bswap32(ripemd160_out[1]),bswap32(ripemd160_out[2]),bswap32(ripemd160_out[3]),bswap32(ripemd160_out[4]),bswap32(privkey[0]),bswap32(privkey[1]),bswap32(privkey[2]),bswap32(privkey[3]),bswap32(privkey[4]),bswap32(privkey[5]),bswap32(privkey[6]),bswap32(privkey[7]),idx);
  2721. }
  2722. sha256_c((uchar*)sha256_in,sha256_out);
  2723. ripemd160_transform(sha256_out,ripemd160_out);
  2724. if(bloom_chk_hash160(bloom,ripemd160_out)) {
  2725. printf("%08x%08x%08x%08x%08x:c:priv:%08x%08x%08x%08x%08x%08x%08x%08x+%#0x\n",bswap32(ripemd160_out[0]),bswap32(ripemd160_out[1]),bswap32(ripemd160_out[2]),bswap32(ripemd160_out[3]),bswap32(ripemd160_out[4]),bswap32(privkey[0]),bswap32(privkey[1]),bswap32(privkey[2]),bswap32(privkey[3]),bswap32(privkey[4]),bswap32(privkey[5]),bswap32(privkey[6]),bswap32(privkey[7]),idx);
  2726. }
  2727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement