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.

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