Permalink
Browse files

Use some more recently-implemented features/remove workarounds

Fix .new(:key(0)) handling to keep promise of overridable class-default key.
No need for actual explicit constructor.  Just doc it like there is one.
Localize docs, and other doc massaging.
Clean some cruft e.g. unnecessary parens that do not aid understandability
  • Loading branch information...
skids committed Dec 9, 2012
1 parent 5fc4f6f commit 87b29bb2c23b7e00ce5f82ba8363475cf60969df
Showing with 76 additions and 85 deletions.
  1. +76 −85 lib/Sum/SipHash.pm6
View
@@ -25,11 +25,12 @@ $Sum::SipHash::Doc::synopsis = $=pod[0].content[4].content.Str;
=head1 DESCRIPTION
- Using C<Sum::SipHash> defines a parameterized role for generating
- types of C<Sum> that calculate variants of a hash code which was
- developed to be efficient enough for general use, while remaining
- resistant to denial-of-service attacks that rely on finding hash
- collisions. It is not intended for use in strong cryptography.
+ C<Sum::SipHash> defines a parameterized role for generating types
+ of C<Sum> that calculate variants of SipHash. SipHash is a hash
+ code which was developed to be efficient enough for general use,
+ including use in live data structures, while remaining resistant
+ to denial-of-service attacks that rely on finding hash collisions.
+ It is not intended for use in strong cryptography.
=head1 ROLES
@@ -39,59 +40,26 @@ $Sum::SipHash::Doc::synopsis = $=pod[0].content[4].content.Str;
that calculates a variant of SipHash. By default, it calculates
SipHash-2-4, which is the suggested variant for general use.
- The resulting C<Sum> expects values that will numerically coerce to
- single byte addends. A C<Sum::Marshal::*> role must be mixed into
- the class, and some such roles may also be used to properly process
- wider or narrower addends as appropriate to the application.
+ The C<:defkey> parameter provides an integer seed value that will be
+ applied to all instances which do not specify their own.
- The C<$c> parameter specifies the number of SipRounds performed
+ The C<:c> parameter specifies the number of SipRounds performed
during a "compression" (which happens about once per eight bytes of
- data) and the C<$d> parameter specifies the number of rounds used
+ data) and the C<:d> parameter specifies the number of rounds used
when the C<Sum> is C<.finalize>d. Together they determine the
strength of the hash: increasing either parameter yields more
resistance to collision analysis, but will increase the computational
cost.
+ The resulting C<Sum> expects values that will numerically coerce to
+ single byte addends. A C<Sum::Marshal::*> role must be mixed into
+ the class, and some such roles may also be used to properly process
+ wider or narrower addends as appropriate to the application.
+
The number of addends may be determined on the fly, and in this
implementation, finalization is performed without altering internal
state, so the C<Sum::Partial> role is available.
- The C<$defkey> parameter defines a seed value that will be applied
- to all instances which do not specify their own. There is an internal
- well-known seed built into the SipHash. Up to 128 bits of an integer
- value will be used to alter this seed if provided.
-
-=head2 METHODS
-
-=head3 method new(:$key?)
-
- The constructor allows an individual instance to use its own seed,
- as described above. The class may supply a default seed if the
- C<$key> argument is omitted from the constructor. The class-provided
- seed will not be used at all if the seed is provided through this
- parameter, and as such, two instances of classes with compatible
- parameters will always generate the same results if they provide the
- same seed, even if their parent classes specified different seeds.
-
-=head1 AUTHOR
-
- Written by Brian S. Julin
-
-=head1 COPYRIGHT
-
- Copyright (c) 2012 Brian S. Julin. All rights reserved. This program is
- free software; you can redistribute it and/or modify it under the terms
- of the Perl Artistic License 2.0.
-
-=head1 REFERENCES
-
- "SipHash: a fast short-input PRF" Aumasson/Bernstein NAGRA document
- ID b9a943a805fbfc6fde808af9fc0ecdfa
-
-=head1 SEE ALSO
-
- C<Sum::(pm3)>
-
=end pod
use Sum;
@@ -100,43 +68,46 @@ role SipHash [ :$c = 2, :$d = 4, Int :$defkey = 0 ] does Sum::Partial {
my Buf $keyfrob = "somepseudorandomlygeneratedbytes".encode("ascii");
- has $!k0 is rw;
- has $!k1 is rw;
- has $!v0 is rw;
- has $!v1 is rw;
- has $!v2 is rw;
- has $!v3 is rw;
- has $!b is rw = 0;
+ has $!k0 is rw = 0;
+ has $!k1 is rw = 0;
+ has $!v0 is rw = 0;
+ has $!v1 is rw = 0;
+ has $!v2 is rw = 0;
+ has $!v3 is rw = 0;
+ has $!b is rw = 0;
has $!left is rw = 0;
- method new (:$key is copy = 0) {
- $key ||= $defkey;
- my $res = self.bless(*,:$key);
- }
- submethod BUILD (:$key is copy) {
+=begin pod
+
+=head2 METHODS
+
+=head3 method new(:$key?)
- $!k0 = $!k1 = $!v0 = $!v1 = $!v2 = $!v3 = 0;
+ There is an internal well-known seed built into the SipHash
+ specification. The least significant 128 bits of a key may
+ be used to alter this seed if provided.
+ The constructor allows an individual instance to use its own seed
+ by providing a C<:key> argument. The class may supply a default
+ seed if the C<:key> argument is omitted from the constructor.
+ The class-provided seed will not be used at all if the seed is
+ provided through this parameter, and as such, two instances of
+ classes with compatible parameters will always generate the same
+ results if they provide the same seed, even if their parent
+ classes specified different seeds.
+
+=end pod
+
+ submethod BUILD (:$key is copy = $defkey) {
# The K constants must be a little-endian encoding of the key.
- for (0..7) {
- $!k1 +<= 8;
- $!k1 +|= $key +& 255;
- $key +>= 8;
- }
- for (0..7) {
- $!k0 +<= 8;
- $!k0 +|= $key +& 255;
- $key +>= 8;
- }
+ $!k1 = :256[ 255 X+& ($key X+> 0,8...^64) ];
+ $!k0 = :256[ 255 X+& ($key X+> 64,72...^128) ];
+
# The internal key also uses a little-endian representation.
- $!v0 = :256[$keyfrob[^8]];
- $!v1 = :256[$keyfrob[8..^16]];
- $!v2 = :256[$keyfrob[16..^24]];
- $!v3 = :256[$keyfrob[24..^32]];
- $!v0 +^= $!k0;
- $!v1 +^= $!k1;
- $!v2 +^= $!k0;
- $!v3 +^= $!k1;
+ $!v0 = $!k0 +^ :256[$keyfrob[^8]];
+ $!v1 = $!k1 +^ :256[$keyfrob[8..^16]];
+ $!v2 = $!k0 +^ :256[$keyfrob[16..^24]];
+ $!v3 = $!k1 +^ :256[$keyfrob[24..^32]];
}
method size ( --> int ) { 64 };
@@ -187,25 +158,45 @@ role SipHash [ :$c = 2, :$d = 4, Int :$defkey = 0 ] does Sum::Partial {
method finalize(*@addends) {
self.push(@addends);
- my $left = $!left;
- my $v0 = $!v0;
- my $v1 = $!v1;
- my $v2 = $!v2;
- my $v3 = $!v3;
+ my ($v0, $v1, $v2, $v3) = $!v0, $!v1, $!v2, $!v3;
- $left +|= (($!b +& 255) +< 56);
+ my $left = $!left;
+ $left +|= ($!b +& 255) +< 56;
compression($left,$v0,$v1,$v2,$v3);
$v2 +^= 0xff;
SipRound($left, $v0, $v1, $v2, $v3) for ^$d;
- [+^] ($v0, $v1, $v2, $v3);
+ [+^] $v0, $v1, $v2, $v3;
}
method Numeric () { self.finalize };
method buf8 () {
Buf.new(255 X+& (self.finalize X+> (56,48,40,32,24,16,8,0)));
}
method Buf () { self.buf8 }
}
+
+=begin pod
+
+=head1 AUTHOR
+
+ Written by Brian S. Julin
+
+=head1 COPYRIGHT
+
+ Copyright (c) 2012 Brian S. Julin. All rights reserved. This program is
+ free software; you can redistribute it and/or modify it under the terms
+ of the Perl Artistic License 2.0.
+
+=head1 REFERENCES
+
+ "SipHash: a fast short-input PRF" Aumasson/Bernstein NAGRA document
+ ID b9a943a805fbfc6fde808af9fc0ecdfa
+
+=head1 SEE ALSO
+
+ C<Sum::(pm3)>
+
+=end pod

0 comments on commit 87b29bb

Please sign in to comment.