Browse Source

library update (c73ee3d) on Sun Jun 20 05:58:43 PM CEST 2021

master
Vickie D. Weigleb 4 years ago
committed by Brooke L. Rediker
parent
commit
3000a6a44a
  1. 3
      .gitmodules
  2. 6
      _data/timelog.yml
  3. 86
      encode.pm
  4. 1
      lib
  5. 133
      misc.pm
  6. 2
      notes.md
  7. 50
      seed.pm
  8. 12
      signatures.pm
  9. 12
      t/blockaddr.t
  10. 2
      t/hashcash.t
  11. 40
      t/run.sh

3
.gitmodules

@ -1,3 +1,6 @@
[submodule "websocketd"]
path = websocketd
url = http://holoGit.ml.ipns.localhost:8080/wpreisser/websocketd.git
[submodule "lib"]
path = lib
url = https://hologit-ml.ipns.dweb.link/michelc/toychain/blockchain-lib.git

6
_data/timelog.yml

@ -4,3 +4,9 @@
1623820719262653597: start toptal -- /drg/toychain
1623840120298864700: stop toptal -- /drg/toychain
1623918295152043953: start toptal keygeneration -- /drg/toychain
1623922600629316388: stop toptal -- /drg/toychain
1623940958318899854: start toptal protocol -- /drg/toychain
1623952980497825186: stop toptal protocol -- /drg/toychain
1624075325993120885: start toptal chaining -- /drg/toychain
1624117500619195903: stop toptal -- /drg/toychain
1624171149798092491: start toptal -- /drg/toychain

86
encode.pm

@ -1,86 +0,0 @@
#
# Intent:
# provide encoding routines
#
# Note:
# This work has been done during my time at Doctor I·T
#
# -- Copyright drit, 2021 --
#
BEGIN { if (-e $ENV{SITE}.'/lib') { use lib $ENV{SITE}.'/lib'; } }
#
package encode;
require Exporter;
@ISA = qw(Exporter);
# Subs we export by default.
@EXPORT = qw();
# Subs we will export if asked.
#@EXPORT_OK = qw(nickname);
@EXPORT_OK = grep { $_ !~ m/^_/ && defined &$_; } keys %{__PACKAGE__ . '::'};
use strict;
use misc qw(version);
# The "use vars" and "$VERSION" statements seem to be required.
use vars qw/$dbug $VERSION/;
# ----------------------------------------------------
our $VERSION = sprintf "%d.%02d", q$Revision: 0.0 $ =~ /: (\d+)\.(\d+)/;
my ($State) = q$State: Exp $ =~ /: (\w+)/; our $dbug = ($State eq 'dbug')?1:0;
# ----------------------------------------------------
$VERSION = &version(__FILE__) unless ($VERSION ne '0.00');
sub mbase58 {
my $mh = sprintf'Z%s',&encode_base58f(@_);
return $mh;
}
sub mbase36 {
my $mh = sprintf'K%s',uc&encode_base36(@_);
return $mh;
}
sub mbase16 {
my $mh = sprintf'f%s',unpack'H*',@_;
return $mh;
}
sub encode_base36 {
use Math::BigInt;
use Math::Base36 qw();
my $n = Math::BigInt->from_bytes(shift);
my $k36 = Math::Base36::encode_base36($n,@_);
#$k36 =~ y,0-9A-Z,A-Z0-9,;
return $k36;
}
sub decode_base36 {
use Math::BigInt;
use Math::Base36 qw();
#$k36 = uc($_[0])
#$k36 =~ y,A-Z0-9,0-9A-Z;
my $n = Math::Base36::decode_base36($_[0]);
my $bin = Math::BigInt->new($n)->as_bytes();
return $bin;
}
sub encode_base58f { # flickr
use Math::BigInt;
use Encode::Base58::BigInt qw();
my $bin = join'',@_;
my $bint = Math::BigInt->from_bytes($bin);
my $h58 = Encode::Base58::BigInt::encode_base58($bint);
# $h58 =~ tr/a-km-zA-HJ-NP-Z/A-HJ-NP-Za-km-z/; # btc
return $h58;
}
sub decode_base58f {
use Math::BigInt;
use Encode::Base58::BigInt qw();
my $s = $_[0];
#$s =~ tr/A-HJ-NP-Za-km-zIO0l/a-km-zA-HJ-NP-ZiooL/; # btc
$s =~ tr/IO0l/iooL/; # forbidden chars
my $bint = Encode::Base58::BigInt::decode_base58($s);
my $bin = Math::BigInt->new($bint)->as_bytes();
return $bin;
}
# -----------------------------------------------------------------------
1; # $Source: /my/perl/modules/encode.pm $

1
lib

@ -0,0 +1 @@
Subproject commit c73ee3d50c2842d0beb4d7e8ae20554da32660d4

133
misc.pm

@ -1,133 +0,0 @@
#!/usr/bin/perl
#
# Intent:
# re-usable routines ...
#
# Note:
# This work has been done during my time at Doctor I·T
#
# -- Copyright drit, 2021 --
#
package misc;
require Exporter;
@ISA = qw(Exporter);
# Subs we export by default.
@EXPORT = qw();
# Subs we will export if asked.
#@EXPORT_OK = qw(nickname);
@EXPORT_OK = grep { $_ !~ m/^_/ && defined &$_; } keys %{__PACKAGE__ . '::'};
use strict;
# The "use vars" and "$VERSION" statements seem to be required.
use vars qw/$dbug $VERSION/;
# ----------------------------------------------------
our $VERSION = sprintf "%d.%02d", q$Revision: 0.0 $ =~ /: (\d+)\.(\d+)/;
my ($State) = q$State: Exp $ =~ /: (\w+)/; our $dbug = ($State eq 'dbug')?1:0;
# ----------------------------------------------------
$VERSION = &version(__FILE__) unless ($VERSION ne '0.00');
# -----------------------------------------------------------------------
sub jsonify {
use JSON qw(encode_json);
my $obj = shift;
my $json = encode_json( $obj ); # /!\ keys are note sorted !
return $json;
}
sub yamlify {
use YAML::Syck qw(Dump);
my $obj = shift;
my $yml = Dump($obj);
return $yml;
}
# -----------------------------------------------------------------------
sub khash { # keyed hash
use Crypt::Digest qw();
my $alg = shift;
my $data = join'',@_;
my $msg = Crypt::Digest->new($alg) or die $!;
$msg->add($data);
my $hash = $msg->digest();
return $hash;
}
# -----------------------------------------------------------------------
sub version {
#y ($atime,$mtime,$ctime) = (lstat($_[0]))[8,9,10];
my @times = sort { $a <=> $b } (lstat($_[0]))[9,10]; # ctime,mtime
my $vtime = $times[-1]; # biggest time...
my $version = &rev($vtime);
if (wantarray) {
my $shk = &get_shake(160,$_[0]);
print "$_[0] : shk:$shk\n" if $dbug;
my $pn = unpack('n',substr($shk,-4)); # 16-bit
my $build = &word($pn);
return ($version, $build);
} else {
return sprintf '%g',$version;
}
}
# -----------------------------------------------------------------------
sub rev {
my ($sec,$min,$hour,$mday,$mon,$yy,$wday,$yday) = (localtime($_[0]))[0..7];
my $rweek=($yday+&fdow($_[0]))/7;
my $rev_id = int($rweek) * 4;
my $low_id = int(($wday+($hour/24)+$min/(24*60))*4/7);
my $revision = ($rev_id + $low_id) / 100;
return (wantarray) ? ($rev_id,$low_id) : $revision;
}
# -----------------------------------------------------------------------
sub fdow {
my $tic = shift;
use Time::Local qw(timelocal);
## 0 1 2 3 4 5 6 7
#y ($sec,$min,$hour,$day,$mon,$year,$wday,$yday)
my $year = (localtime($tic))[5]; my $yr4 = 1900 + $year ;
my $first = timelocal(0,0,0,1,0,$yr4);
our $fdow = (localtime($first))[6];
#printf "1st: %s -> fdow: %s\n",&hdate($first),$fdow;
return $fdow;
}
# -----------------------------------------------------------------------
sub get_shake { # use shake 256 because of ipfs' minimal length of 20Bytes
use Crypt::Digest::SHAKE;
my $len = shift;
local *F; open F,$_[0] or do { warn qq{"$_[0]": $!}; return undef };
#binmode F unless $_[0] =~ m/\.txt/;
my $msg = Crypt::Digest::SHAKE->new(256);
$msg->addfile(*F);
my $digest = $msg->done(($len+7)/8);
return $digest;
}
# -----------------------------------------------------------------------
sub word { # 20^4 * 6^3 words (25bit worth of data ...)
use integer;
my $n = $_[0];
my $vo = [qw ( a e i o u y )]; # 6
my $cs = [qw ( b c d f g h j k l m n p q r s t v w x z )]; # 20
my $str = '';
if (1 && $n < 26) {
$str = chr(ord('a') +$n%26);
} else {
$n -= 6;
while ($n >= 20) {
my $c = $n % 20;
$n /= 20;
$str .= $cs->[$c];
#print "cs: $n -> $c -> $str\n";
$c = $n % 6;
$n /= 6;
$str .= $vo->[$c];
#print "vo: $n -> $c -> $str\n";
}
if ($n > 0) {
$str .= $cs->[$n];
}
return $str;
}
}
# -----------------------------------------------------------------------
1; # $Source: /my/perl/modules/misc.pm $

2
notes.md

@ -15,4 +15,4 @@
minimally sized blocks :
- segreated signature, timestamp, audit etc.
- {seq number, hash, previous}
- block: {address, previous, pow}

50
seed.pm

@ -1,50 +0,0 @@
#
# Intent:
# entropy routines grasping source intention !
#
# Note:
# This work has been done during my time at Doctor I·T
#
# -- Copyright drit, 2021 --
BEGIN { if (-e $ENV{SITE}.'/lib') { use lib $ENV{SITE}.'/lib'; } }
#
package seed;
require Exporter;
@ISA = qw(Exporter);
# Subs we export by default.
@EXPORT = qw();
# Subs we will export if asked.
#@EXPORT_OK = qw(nickname);
@EXPORT_OK = grep { $_ !~ m/^_/ && defined &$_; } keys %{__PACKAGE__ . '::'};
use strict;
use misc qw(version);
# The "use vars" and "$VERSION" statements seem to be required.
use vars qw/$dbug $VERSION/;
# ----------------------------------------------------
our $VERSION = sprintf "%d.%02d", q$Revision: 0.0 $ =~ /: (\d+)\.(\d+)/;
my ($State) = q$State: Exp $ =~ /: (\w+)/; our $dbug = ($State eq 'dbug')?1:0;
# ----------------------------------------------------
$VERSION = &version(__FILE__) unless ($VERSION ne '0.00');
sub setseed {
our $seed = undef;
# ecf7fa3d : 15min
$seed = ($_[0]) ? srand($_[0]) : srand();
printf "seed: %08x\n",$seed;
return $seed;
}
# -----------------------------------------------------------------------
sub rand64 { # /!\ NOT Cryptographycally safe
my $i1 = int(rand(0xFFFF_FFFF));
my $i2 = int(rand(0xFFFF_FFFF));
my $q = $i1 <<32 | $i2;
printf "i1: %08x\n",$i1;
printf "i2: %08x\n",$i2;
printf "rnd64: %s\n",unpack'H*',pack'Q',$q;
return $q;
}
# -----------------------------------------------------------------------
1; # $Source: /my/perl/modules/pseudorandom.pm $

12
signatures.pm

@ -1,12 +0,0 @@
#!/usr/bin/perl
use Crypt::OpenSSL::ECDSA;
$sig = Crypt::OpenSSL::ECDSA::ECDSA_do_sign( $digest, $eckey );
$r = $sig->get_r;
$s = $sig->get_s;
$sig = Crypt::OpenSSL::ECDSA::ECDSA_SIG->new();
$sig->set_r($r);
$sig->set_s($s);
Crypt::OpenSSL::ECDSA::ECDSA_do_verify( $digest, $sig, $eckey );

12
t/blockaddr.t

@ -13,18 +13,24 @@ BEGIN { if (-e $ENV{SITE}.'/lib') { use lib $ENV{SITE}.'/lib'; } }
#use if -e $LIB, $LIB.'/seed';
use seed qw(rand64);
use toychain qw(blockAddr);
use toychain qw(blockHash displayBlock);
use YAML::Syck qw(Dump);
print "testing blockaddr\n";
print "// testing blockaddr\n";
printf "chain.addr: %s\n",$toychain::chain->{bkaddr};
# f96885aa : 21.4min
# 044ce1de : 20.5min
if (0) {
&seed::setseed(0x21683774);
my $n0 = rand64(); # n < 18446744073709551616
printf "n0:%s\n",$n0;
}
my $addr = &blockAddr($toychain::genesis);
displayBlock($toychain::genesis);
my $addr = &blockHash($toychain::genesis);
printf "genesis.addr: %s\n",$addr;
print ".\n";
exit $?;

2
t/hashcash.t

@ -28,6 +28,8 @@ printf "n0:%s\n",$n0;
my ($hc,$n) = &hashcash('SHA256','hello world',$n0,5);
printf "hc: %s %x %s\n",unpack('H*',$hc),$n,unpack'H*',pack('Q',$n);
#my ($hc,$n) = &hashcash('SHA256','hello world',$n0,'abcdef');
#printf "hc: %s %x %s\n",unpack('H*',$hc),$n,unpack'H*',pack('Q',$n);
printf "// done $0\n";

40
t/run.sh

@ -0,0 +1,40 @@
#
intent="testing all subroutines ..."
if echo -n "\e" | grep -q -e 'e'; then
e="-e" # bash needs a -e !
fi
# colors : [see also](https://en.wikipedia.org/wiki/ANSI_escape_code)
nc=$(echo -n $e "\e[0m")
export SITE='..';
cp -p test.log test.log.1
# savestdout and stderr
exec 3>&1 4>&2
exec > test.log
exec 2>&1
echo "--- ${0##*/}"
for tc in *.t; do
echo "tc: $tc"
perl $tc
echo '---'
done
echo '...'
# restore stdout and stderr
exec 1>&3 2>&4 3>&- 4>&-
ipfs add -n -q test.log
diff test.log test.log.1
if [ $? = 0 ]; then
green=$(echo -n $e "\e[1;32m")
echo "${green}PASS${nc}"
else
red=$(echo -n $e "\e[31m")
echo "${red}FAIL${nc}"
exit 252
fi
true;
Loading…
Cancel
Save