Skip to content

Commit

Permalink
more FP style
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucien Grondin committed Dec 12, 2022
1 parent 11fdded commit e1c137e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion META6.json
Expand Up @@ -2,7 +2,7 @@
"perl" : "6.*",
"name" : "Digest",
"license" : "Artistic-2.0",
"version" : "0.24.0",
"version" : "0.25.0",
"description" : "Raku implementation of digest algorigthms.",
"author" : "grondilu",
"depends" : [ ],
Expand Down
27 changes: 15 additions & 12 deletions lib/Digest/RIPEMD.rakumod
Expand Up @@ -44,21 +44,24 @@ multi rmd160(Blob $data) {
{ $^x +^ ($^y +| +^$^z) }
;

my buf8 $b .= new: |@$data, 0x80;
my buf8 $b .= new: $data.list, 0x80;
$b.push: 0 until (8*@$b-448) %% 512;
$b.push: |(8 * $data).polymod: 256 xx 7;

my blob32 $word .= new: $b.rotor(4).map: { :256[@^x.reverse] };
my buf32 $h .= new: 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0;
loop (my $i = 0; $i < @$word; $i += 16) {
my buf32 ($X, $Y) = $h.clone xx 2;
for ^80 -> $j {
$X[] = [$X[4], rotl(($X[0] + @F[ $j div 16](|$X[1..3]) + ($word[$i+r1[$j]] // 0) + @K1[$j]) mod 2**32, s1[$j]) + $X[4], $X[1], rotl($X[2], 10), $X[3]];
$Y[] = [$Y[4], rotl(($Y[0] + @F[(79-$j) div 16](|$Y[1..3]) + ($word[$i+r2[$j]] // 0) + @K2[$j]) mod 2**32, s2[$j]) + $Y[4], $Y[1], rotl($Y[2], 10), $Y[3]];
}
$h[] = [$h[1,2,3,4,0] Z+ $X[2,3,4,0,1] Z+ $Y[3,4,0,1,2]];
}
return blob8.new: $h.map: |*.polymod: 256 xx 3;
blob8.new: (
reduce
-> blob32 $h, @words {
my buf32 ($X, $Y) = buf32.new($h).clone xx 2;
for ^80 -> $j {
$X[] = [$X[4], rotl(($X[0] + @F[ $j div 16](|$X[1..3]) + @words[r1[$j]] + @K1[$j]) mod 2**32, s1[$j]) + $X[4], $X[1], rotl($X[2], 10), $X[3]];
$Y[] = [$Y[4], rotl(($Y[0] + @F[(79-$j) div 16](|$Y[1..3]) + @words[r2[$j]] + @K2[$j]) mod 2**32, s2[$j]) + $Y[4], $Y[1], rotl($Y[2], 10), $Y[3]];
}
blob32.new: $h[1,2,3,4,0] Z+ $X[2,3,4,0,1] Z+ $Y[3,4,0,1,2];
},
(BEGIN blob32.new(0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0)),
|blob32.new($b.rotor(4).map: { :256[@^x.reverse] }).rotor(16);
).map: |*.polymod(256 xx 3);

}

# vim: ft=raku
9 changes: 5 additions & 4 deletions t/ripemd.t
Expand Up @@ -2,7 +2,6 @@
use Test;
use Digest::RIPEMD;

plan 8;
for (
'' => '9c1185a5c5e9fc54612808977ee8f548b2258d31',
'a' => '0bdc9d2d256b3ee9daae347be6f4dc835a467ffe',
Expand All @@ -11,10 +10,12 @@ for (
('a'..'z').join => 'f71c27109c692c1b56bbdceb5b9d2865b3708dbc',
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' => '12a053384a9c0c88e405a06c27dcf49ada62eb2b',
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' => 'b0e20b6e3116640286ed3a87a5713079b21f5189',
'1234567890' x 8 => '9b752e45573d4b39f4dbd3323cab82bf63326bfb'
# 'a' x 1_000_000 => '52783243c1697bdbe16d37f97f68f08325dc1528'
'1234567890' x 8 => '9b752e45573d4b39f4dbd3323cab82bf63326bfb',
#'a' x 1_000_000 => '52783243c1697bdbe16d37f97f68f08325dc1528'
) {
is rmd160(.key), Blob.new(parse-base(.value, 16).polymod(256 xx *).reverse), "RIPEMD('{.key}')";
is rmd160(.key), Blob.new(parse-base(.value, 16).polymod(256 xx *).reverse), "RIPEMD('{.key.chars > 50 ?? .key.substr(0, 50) ~ "..." !! .key}')";
}

done-testing;

# vim: ft=raku

0 comments on commit e1c137e

Please sign in to comment.