|
|
--- layout: simple --- # Proof of Work
We use a [hashcash][1] type of proof of work (see also [Adam Back][ab] annonce) where the difficulities is set to <span id=dif><i>:dif</i></span>.
hash algo: <input name="algo" value="SHA256" size="7"> <br>data: <input name="data" value="some data" size=24> <!-- perl -Ilib -Mmisc -e 'print $misc::inonce'
5: 12131054599640746025 6: 12131054600136000000 8: 12131054604672000000 --> <br>nonce: <input name="nonce" value="12131054604672000000" size=20> <br>difficulty: <input name="dif" value="7" size="2"> <br>hashcash: <span name="hashcash"><i>?</i></span> <br>proof: <span id="pow"><i>:pow</i></span> <button>compute</button>
---
## note:
- perl hashcash : [/lib/Chain.pm#hashcash](/lib/Chain.pm#hashcash) - original hashcash's [gi(s)t]: <[gist:8748be59087e244f916618ac2d66ae3b][hologit]> (SHA1 based)
### code
```perl # -----------------------------------------------------
sub hashcash { # ex: hashcash('SHA256',$data,$nonce,7); my $alg = shift; my $difficulty = pop; # difficulty < 8 my $nonce = pop; # 64 bits of nonce my $n = $nonce; printf "hashcash.nonce: %s\n",$nonce; printf "hashcash.difficulty: %s\n",$difficulty; #printf "data: %s\n",join'',@_; my $l = 0; my $match = '8'.substr('0'x$difficulty,0,$l); # set the MSB to 1 to avoid padding problems my $iv; use Crypt::Digest qw(); my $msg = Crypt::Digest->new($alg) or die $!; $msg->add(join'',@_); #printf "iv: %s\n",$msg->hexdigest(); my $h16; my $pn = pack'Q',$nonce; # Quad while (1) { # length($pn) < $difficulty) { $iv = $msg->clone; $iv->add($pn); $h16 = $iv->hexdigest(); if (substr($h16,0,$l+1) eq $match) { my $elapse = time - $^T + 1; if ($0 =~ m/\.t$/) { $elapse = 3; } my $rate = ($n - $nonce) / $elapse / 1000; #printf "%d: %s %s %s %.0fkH/s %.1fmin\n",$l, $n,unpack('H*',$pn),$h16,$rate,$elapse/60; # count any zeros : ($h16 =~ m/0/g) ! my $zc = ($h16 =~ /80+/) ? length($&) : 0; #print "zc: $zc\n"; $l = $zc; last if $zc >= $difficulty; $match = '8'.substr('0'x$l,0,$l); #} else { # printf "%d: %s %s %s %s\r",$l, $n,unpack('H*',$pn),$h16,$match; } $pn = pack('Q',$n++); } return pack('H*',$h16),$n-1; } # -----------------------------------------------------
```
[1]: http://www.hashcash.org/papers/hashcash.pdf [ab]: http://www.hashcash.org/papers/announce.txt [2]: https://www.cs.jhu.edu/~rdas/finalreport.pdf [3]: https://www.arijuels.com/wp-content/uploads/2013/09/JB99.pdf [4]: http://pubs.sciepub.com/jcsa/5/2/2/index.html [4d]: http://www.sciepub.com/portal/downloads?doi=10.12691/jcsa-5-2-2&filename=jcsa-5-2-2.pdf [hologit]: https://hologit-ml.ipns.cf-ipfs.com/cbuquo/hashcash.git [gist]: git@gist.github.com:8748be59087e244f916618ac2d66ae3b.git
<script src="pow.js"></script>
|