You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

  1. ---
  2. layout: simple
  3. ---
  4. # Proof of Work
  5. We use a [hashcash][1] type of proof of work (see also [Adam Back][ab] annonce)
  6. where the difficulities is set to <span id=dif><i>:dif</i></span>.
  7. hash algo: <input name="algo" value="SHA256" size="7">
  8. <br>data: <input name="data" value="some data" size=24>
  9. <!-- perl -Ilib -Mmisc -e 'print $misc::inonce'
  10. 5: 12131054599640000000
  11. 6: 12131054600130000000
  12. 8: 12131054604670000000
  13. 9: 12131054702320000000
  14. -->
  15. <br>nonce: <input name="nonce" value="12131054702320000000" size=20>
  16. <br>difficulty: <input name="dif" value="7" size="2">
  17. <br>hashcash: <span name="hashcash"><i>?</i></span>
  18. <br>proof: <span id="pow"><i>:pow</i></span>
  19. <button>compute</button>
  20. ---
  21. ## note:
  22. - perl hashcash : [/lib/Chain.pm#hashcash](/lib/Chain.pm#hashcash)
  23. - original hashcash's [gi(s)t]: &lt;[gist:8748be59087e244f916618ac2d66ae3b][hologit]> (SHA1 based)
  24. ### code
  25. ```perl
  26. # -----------------------------------------------------
  27. sub hashcash { # ex: hashcash('SHA256',$data,$nonce,7);
  28. my $alg = shift;
  29. my $difficulty = pop; # difficulty < 8
  30. my $nonce = pop; # 64 bits of nonce
  31. my $n = $nonce;
  32. printf "hashcash.nonce: %s\n",$nonce;
  33. printf "hashcash.difficulty: %s\n",$difficulty;
  34. #printf "data: %s\n",join'',@_;
  35. my $l = 0;
  36. my $match = '8'.substr('0'x$difficulty,0,$l); # set the MSB to 1 to avoid padding problems
  37. my $iv;
  38. use Crypt::Digest qw();
  39. my $msg = Crypt::Digest->new($alg) or die $!;
  40. $msg->add(join'',@_);
  41. #printf "iv: %s\n",$msg->hexdigest();
  42. my $h16;
  43. my $pn = pack'Q',$nonce; # Quad
  44. while (1) { # length($pn) < $difficulty) {
  45. $iv = $msg->clone;
  46. $iv->add($pn);
  47. $h16 = $iv->hexdigest();
  48. if (substr($h16,0,$l+1) eq $match) {
  49. my $elapse = time - $^T + 1;
  50. if ($0 =~ m/\.t$/) { $elapse = 3; }
  51. my $rate = ($n - $nonce) / $elapse / 1000;
  52. #printf "%d: %s %s %s %.0fkH/s %.1fmin\n",$l, $n,unpack('H*',$pn),$h16,$rate,$elapse/60;
  53. # count any zeros : ($h16 =~ m/0/g) !
  54. my $zc = ($h16 =~ /80*/) ? length($&) : 0;
  55. #print "zc: $zc\n";
  56. $l = $zc;
  57. last if $zc >= $difficulty;
  58. $match = '8'.substr('0'x$l,0,$l);
  59. #} else {
  60. # printf "%d: %s %s %s %s\r",$l, $n,unpack('H*',$pn),$h16,$match;
  61. }
  62. $pn = pack('Q',$n++);
  63. }
  64. return pack('H*',$h16),$n-1;
  65. }
  66. # -----------------------------------------------------
  67. ```
  68. [1]: http://www.hashcash.org/papers/hashcash.pdf
  69. [ab]: http://www.hashcash.org/papers/announce.txt
  70. [2]: https://www.cs.jhu.edu/~rdas/finalreport.pdf
  71. [3]: https://www.arijuels.com/wp-content/uploads/2013/09/JB99.pdf
  72. [4]: http://pubs.sciepub.com/jcsa/5/2/2/index.html
  73. [4d]: http://www.sciepub.com/portal/downloads?doi=10.12691/jcsa-5-2-2&filename=jcsa-5-2-2.pdf
  74. [hologit]: https://hologit-ml.ipns.cf-ipfs.com/cbuquo/hashcash.git
  75. [gist]: git@gist.github.com:8748be59087e244f916618ac2d66ae3b.git
  76. <script src="pow.js"></script>