Skip to content

Commit

Permalink
Ratchet down some types where we currently can
Browse files Browse the repository at this point in the history
Use sequence dwimmery
A few cosmetic changes
  • Loading branch information
skids committed Nov 21, 2012
1 parent 7d1748c commit 02af8a5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 72 deletions.
6 changes: 3 additions & 3 deletions lib/Sum/CRC.pm6
Expand Up @@ -111,7 +111,7 @@ role Sum::CRC [ :@header?, :@footer?, :$residual = 0,
# should only be rw from inside the class. # should only be rw from inside the class.
has $.rem is rw = ( ($iniv.WHAT === Bool) ?? (-$iniv +& ((1 +< $columns) - 1)) !! $iniv ); has $.rem is rw = ( ($iniv.WHAT === Bool) ?? (-$iniv +& ((1 +< $columns) - 1)) !! $iniv );


method size () { $columns } method size ( --> int) { +$columns }


method add (*@addends) { method add (*@addends) {
for (@addends) -> $a { for (@addends) -> $a {
Expand Down Expand Up @@ -150,11 +150,11 @@ role Sum::CRC [ :@header?, :@footer?, :$residual = 0,
method buf8 () { method buf8 () {
my $f = self.finalize; my $f = self.finalize;
my $bytes = ($columns + 7) div 8; my $bytes = ($columns + 7) div 8;
Buf.new( 255 X+& ($f X+> (8 X* reverse(^$bytes))) ); Buf.new( 255 X+& ($f X+> ($bytes*8-8,{$_-8}...0)));
} }
method buf1 () { method buf1 () {
my $f = self.finalize; my $f = self.finalize;
Buf.new( 1 X+& ($f X+> reverse(^$columns)) ); Buf.new( 1 X+& ($f X+> ($columns-1...0)) );
} }
method Buf () { self.buf1; } method Buf () { self.buf1; }


Expand Down
64 changes: 31 additions & 33 deletions lib/Sum/MD.pm6
Expand Up @@ -78,17 +78,17 @@ $Sum::MD::Doc::synopsis = $=pod[0].content[3..4]>>.content.Str;


use Sum; use Sum;


role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5", role Sum::MD4_5 [ :$alg where { $_ eqv [|] <MD5 MD4 MD4ext RIPEMD-128 RIPEMD-160 RIPEMD-256 RIPEMD-320 > } = "MD5",
:$mod8 = False ] does Sum { :$mod8 = False ] does Sum {
has $!o is rw = 0; has $!o is rw = 0;
has $!final is rw; has Bool $!final is rw;
has @!w is rw; # "Parsed" message gets bound here. has @!w is rw; # "Parsed" message gets bound here.
has @!s is rw; # Current hash state. H in specification. has @!s is rw; # Current hash state. H in specification.


# MD5 table of constants (a.k.a. T[1..64] in RFC1321) # MD5 table of constants (a.k.a. T[1..64] in RFC1321)
my @t = (Int(4294967296 * .sin.abs) for 1..64); my @t = (Int(4294967296 * .sin.abs) for 1..64);


method size () { method size ( --> int) {
given $alg { given $alg {
when "MD4"| when "MD4"|
"MD5"| "MD5"|
Expand All @@ -97,7 +97,6 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
when "RIPEMD-256" { 256 } when "RIPEMD-256" { 256 }
when "RIPEMD-160" { 160 } when "RIPEMD-160" { 160 }
when "RIPEMD-320" { 320 } when "RIPEMD-320" { 320 }
default { Inf }
} }
} }


Expand All @@ -119,13 +118,13 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",


# A moment of silence for the pixies that die every time something # A moment of silence for the pixies that die every time something
# like this gets written in an HLL. # like this gets written in an HLL.
my sub rol ($v, Int $count where { -1 < * < 32 }) { my sub rol ($v, int $count where { -1 < * < 32 }) {
my $tmp = ($v +< $count) +& 0xffffffff; my $tmp = ($v +< $count) +& 0xffffffff;
$tmp +|= (($v +& 0xffffffff) +> (32 - $count)); $tmp +|= (($v +& 0xffffffff) +> (32 - $count));
$tmp; $tmp;
} }


method md4_round1_step ($data, $shift) { method md4_round1_step ($data, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -134,7 +133,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol(($a + $data + (($b +& $c) +| ((+^$b) +& $d))), $shift)); rol(($a + $data + (($b +& $c) +| ((+^$b) +& $d))), $shift));
} }


method md4_ext_round1_step ($data, $shift) { method md4_ext_round1_step ($data, int $shift) {
my $a := @!s[4]; my $a := @!s[4];
my $b := @!s[5]; my $b := @!s[5];
my $c := @!s[6]; my $c := @!s[6];
Expand All @@ -143,7 +142,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol(($a + $data + (($b +& $c) +| ((+^$b) +& $d))), $shift)); rol(($a + $data + (($b +& $c) +| ((+^$b) +& $d))), $shift));
} }


method md4_round2_step ($data, $shift) { method md4_round2_step ($data, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -153,7 +152,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
([+|] (($b,$b,$c) Z+& ($c,$d,$d)))), $shift)); ([+|] (($b,$b,$c) Z+& ($c,$d,$d)))), $shift));
} }


method md4_ext_round2_step ($data, $shift) { method md4_ext_round2_step ($data, int $shift) {
my $a := @!s[4]; my $a := @!s[4];
my $b := @!s[5]; my $b := @!s[5];
my $c := @!s[6]; my $c := @!s[6];
Expand All @@ -163,7 +162,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
([+|] (($b,$b,$c) Z+& ($c,$d,$d)))), $shift)); ([+|] (($b,$b,$c) Z+& ($c,$d,$d)))), $shift));
} }


method md4_round3_step ($data, $shift) { method md4_round3_step ($data, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -172,7 +171,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol(($a + $data + 0x6ed9eba1 + ([+^] $b, $c, $d)), $shift)); rol(($a + $data + 0x6ed9eba1 + ([+^] $b, $c, $d)), $shift));
} }


method md4_ext_round3_step ($data, $shift) { method md4_ext_round3_step ($data, int $shift) {
my $a := @!s[4]; my $a := @!s[4];
my $b := @!s[5]; my $b := @!s[5];
my $c := @!s[6]; my $c := @!s[6];
Expand All @@ -191,7 +190,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
(($b +& $c) +| (+^$b +& $d))), $shift))); (($b +& $c) +| (+^$b +& $d))), $shift)));
} }


method md5_round2_step ($data, $idx, $shift) { method md5_round2_step ($data, int $idx, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -201,7 +200,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
(($b +& $d) +| (+^$d +& $c))), $shift))); (($b +& $d) +| (+^$d +& $c))), $shift)));
} }


method md5_round3_step ($data, $idx, $shift) { method md5_round3_step ($data, int $idx, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -210,7 +209,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
$b + rol(($a + $data + @t[$idx] + ([+^] $b, $c, $d)), $shift))); $b + rol(($a + $data + @t[$idx] + ([+^] $b, $c, $d)), $shift)));
} }


method md5_round4_step ($data, $idx, $shift) { method md5_round4_step ($data, int $idx, int $shift) {
my $a := @!s[0]; my $a := @!s[0];
my $b := @!s[1]; my $b := @!s[1];
my $c := @!s[2]; my $c := @!s[2];
Expand All @@ -219,7 +218,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
$b + rol(($a + $data + @t[$idx] + ($c +^ (+^$d +| $b))), $shift))); $b + rol(($a + $data + @t[$idx] + ($c +^ (+^$d +| $b))), $shift)));
} }


method ripe_f1_5 ($lr, $data, $k, $shift) { method ripe_f1_5 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -229,7 +228,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
($e + rol($a + $k + $data + ([+^] $b, $c, $d), $shift))); ($e + rol($a + $k + $data + ([+^] $b, $c, $d), $shift)));
} }


method ripe_f1_4 ($lr, $data, $k, $shift) { method ripe_f1_4 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -238,7 +237,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol($a + $k + $data + ([+^] $b, $c, $d), $shift)); rol($a + $k + $data + ([+^] $b, $c, $d), $shift));
} }


method ripe_f2_5 ($lr, $data, $k, $shift) { method ripe_f2_5 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -249,7 +248,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
$shift))); $shift)));
} }


method ripe_f2_4 ($lr, $data, $k, $shift) { method ripe_f2_4 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -258,7 +257,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol($a + $k + $data + (($b +& $c) +| (+^$b +& $d)), $shift)); rol($a + $k + $data + (($b +& $c) +| (+^$b +& $d)), $shift));
} }


method ripe_f3_5 ($lr, $data, $k, $shift) { method ripe_f3_5 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -268,7 +267,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
($e + rol($a + $k + $data + ((+^$c +| $b) +^ $d), $shift))); ($e + rol($a + $k + $data + ((+^$c +| $b) +^ $d), $shift)));
} }


method ripe_f3_4 ($lr, $data, $k, $shift) { method ripe_f3_4 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -277,7 +276,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol($a + $k + $data + ((+^$c +| $b) +^ $d), $shift)); rol($a + $k + $data + ((+^$c +| $b) +^ $d), $shift));
} }


method ripe_f4_5 ($lr, $data, $k, $shift) { method ripe_f4_5 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -288,7 +287,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
$shift))); $shift)));
} }


method ripe_f4_4 ($lr, $data, $k, $shift) { method ripe_f4_4 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand All @@ -297,7 +296,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
rol($a + $k + $data + (($b +& $d) +| (+^$d +& $c)), $shift)); rol($a + $k + $data + (($b +& $d) +| (+^$d +& $c)), $shift));
} }


method ripe_f5_5 ($lr, $data, $k, $shift) { method ripe_f5_5 (int $lr, $data, $k, int $shift) {
my $a := @!s[$lr + 0]; my $a := @!s[$lr + 0];
my $b := @!s[$lr + 1]; my $b := @!s[$lr + 1];
my $c := @!s[$lr + 2]; my $c := @!s[$lr + 2];
Expand Down Expand Up @@ -486,13 +485,13 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
multi method do_add (Buf $block where { -1 < .elems < 64 }, multi method do_add (Buf $block where { -1 < .elems < 64 },
Bool $b7?, Bool $b6?, Bool $b5?, Bool $b4?, Bool $b7?, Bool $b6?, Bool $b5?, Bool $b4?,
Bool $b3?, Bool $b2?, Bool $b1?) { Bool $b3?, Bool $b2?, Bool $b1?) {
my $bits = 0; my int $bits = 0;
my $byte = 0; my int $byte = 0;


# $block.gist.say; # $block.gist.say;


# Count how many stray bits we have and build them into a byte # Count how many stray bits we have and build them into a byte
( $byte +|= +$_ +< (7 - $bits++) ) ( $byte = $byte +| (+$_ +< (7 - (($bits = $bits + 1)-1))) )
if .defined for ($b7,$b6,$b5,$b4,$b3,$b2,$b1); if .defined for ($b7,$b6,$b5,$b4,$b3,$b2,$b1);


# Update the count of the total number of bits sent. # Update the count of the total number of bits sent.
Expand All @@ -505,7 +504,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
# Note 1 +< (7 - $bits) just happily also DTRT when !$bits # Note 1 +< (7 - $bits) just happily also DTRT when !$bits
self.add(Buf.new($block[],$byte +| 1 +< (7 - $bits), self.add(Buf.new($block[],$byte +| 1 +< (7 - $bits),
0 xx (55 - $block.elems), 0 xx (55 - $block.elems),
(255 X+& ($!o X+> (0,8,16,24,32,40,48,56))))); (255 X+& ($!o X+> (0,8...56)))));
$!o -= 512; # undo what the other multimethod did. $!o -= 512; # undo what the other multimethod did.
} }
else { # No else { # No
Expand All @@ -515,7 +514,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
0 xx (63 - $block.elems))); 0 xx (63 - $block.elems)));
$!o -= 512; # undo what the other multimethod did. $!o -= 512; # undo what the other multimethod did.
self.add(Buf.new(0 xx 56, self.add(Buf.new(0 xx 56,
(255 X+& ($!o X+> (0,8,16,24,32,40,48,56))))); (255 X+& ($!o X+> (0,8...56)))));
$!o -= 512; # undo what the other multimethod did. $!o -= 512; # undo what the other multimethod did.
} }
$!final = True; $!final = True;
Expand All @@ -531,8 +530,7 @@ role Sum::MD4_5 [ :$alg where { * eqv [|] <MD5 MD4 MD4ext> } = "MD5",
# First 16 uint32's are a straight copy of the data. # First 16 uint32's are a straight copy of the data.
# When endianness matches and with native types, # When endianness matches and with native types,
# this would boil down to a simple memcpy. # this would boil down to a simple memcpy.
my @m = (:256[ $block[ reverse($_ ..^ $_+4) ] ] my @m = (:256[ $block[ $_+3 ... $_ ] ] for 0,4 ...^ 64);
for 0,{$^idx + 4} ...^ 64);


@!w := @m; @!w := @m;
self.md4_comp if $alg eqv ("MD4"|"MD4ext"); self.md4_comp if $alg eqv ("MD4"|"MD4ext");
Expand Down Expand Up @@ -618,7 +616,7 @@ role Sum::MD2 does Sum {


has @!C is rw = 0 xx 16; # The checksum, computed in parallel has @!C is rw = 0 xx 16; # The checksum, computed in parallel
has @!X is rw = 0 xx 48; # The digest state has @!X is rw = 0 xx 48; # The digest state
has $!final is rw = False; # whether pad/checksum is in state already has Bool $!final is rw = False; # whether pad/checksum is in state already


multi method do_add (*@addends) { multi method do_add (*@addends) {
sink for (@addends) { self.add($_) } sink for (@addends) { self.add($_) }
Expand All @@ -628,7 +626,7 @@ role Sum::MD2 does Sum {
die("Marshalling error. Addends must be Buf with 0..16 bytes."); die("Marshalling error. Addends must be Buf with 0..16 bytes.");
} }
multi method do_add (Buf $block where { -1 < .elems < 16 }) { multi method do_add (Buf $block where { -1 < .elems < 16 }) {
my Int $empty = 16 - $block.elems; my int $empty = 16 - $block.elems;
$!final = True; $!final = True;
self.do_add(Buf.new($block.values, $empty xx $empty)); self.do_add(Buf.new($block.values, $empty xx $empty));
self.do_add(Buf.new(@!C[])); self.do_add(Buf.new(@!C[]));
Expand All @@ -647,7 +645,7 @@ role Sum::MD2 does Sum {
} }
return; return;
} }
method size { 128 }; method size ( --> int) { 128 };
method add (*@addends) { self.do_add(|@addends) } method add (*@addends) { self.do_add(|@addends) }
method finalize(*@addends) { method finalize(*@addends) {
given self.push(@addends) { given self.push(@addends) {
Expand Down

0 comments on commit 02af8a5

Please sign in to comment.