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.
 
 
 
 
 
 

2.8 KiB

layout
simple

Proof of Work

We use a hashcash type of proof of work (see also Adam Back annonce) where the difficulities is set to :dif.

hash algo:
data:


nonce:
difficulty:
hashcash: ?
proof: :pow compute


note:

code

# -----------------------------------------------------
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;
}
# -----------------------------------------------------