X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-perl/blobdiff_plain/ef3af890207c78e8352e850ed9fd510c10f7db56..f9952aec1cf6c64a5681308eea817b6113a37433:/Catacomb.pm diff --git a/Catacomb.pm b/Catacomb.pm index 35812e8..fd0e455 100644 --- a/Catacomb.pm +++ b/Catacomb.pm @@ -1,6 +1,6 @@ # -*-perl-*- # -# $Id: Catacomb.pm,v 1.3 2004/04/18 15:05:08 mdw Exp $ +# $Id$ # # Perl interface to Catacomb crypto library # @@ -58,13 +58,13 @@ use overload '*' => sub { _binop(\&mul, @_); }, '/' => sub { _binop(\&div, @_); }, '%' => sub { _binop(\&mod, @_); }, - '&' => sub { _binop(\&and, @_); }, - '|' => sub { _binop(\&or, @_); }, - '^' => sub { _binop(\&xor, @_); }, + '&' => sub { _binop(\&and2c, @_); }, + '|' => sub { _binop(\&or2c, @_); }, + '^' => sub { _binop(\&xor2c, @_); }, '**' => sub { _binop(\&pow, @_); }, - '>>' => sub { &lsr(@_[0, 1]); }, - '<<' => sub { &lsl(@_[0, 1]); }, - '~' => sub { ¬($_[0]) }, + '>>' => sub { &lsr2c(@_[0, 1]); }, + '<<' => sub { &lsl2c(@_[0, 1]); }, + '~' => sub { ¬2c($_[0]) }, '==' => sub { _binop(\&eq, @_); }, '<=>' => sub { _binop(\&cmp, @_); }, '""' => sub { &tostring($_[0]); }, @@ -112,6 +112,38 @@ sub modinv { return $i; } +#----- Binary polynomials --------------------------------------------------- + +package Catacomb::GF; +use Carp; + +@ISA = qw(Catacomb::MP); + +use overload + '+' => sub { _binop(\&add, @_); }, + '-' => sub { _binop(\&add, @_); }, + '*' => sub { _binop(\&mul, @_); }, + '/' => sub { _binop(\&div, @_); }, + '%' => sub { _binop(\&mod, @_); }, + '&' => sub { _binop(\&Catacomb::MP::and, @_); }, + '|' => sub { _binop(\&Catacomb::MP::or, @_); }, + '^' => sub { _binop(\&Catacomb::MP::xor, @_); }, + '>>' => sub { &Catacomb::MP::lsr(@_[0, 1]); }, + '<<' => sub { &Catacomb::MP::lsl(@_[0, 1]); }, + '~' => sub { &Catacomb::MP::not($_[0]) }, + '==' => sub { _binop(\&Catacomb::MP::eq, @_); }, + '<=>' => sub { _binop(\&Catacomb::MP::cmp, @_); }, + '""' => sub { "0x" . &Catacomb::MP::tostring($_[0], 16); }, + 'neg' => sub { $_[0]; }, + '0+' => sub { &Catacomb::MP::toint($_[0]); }; + +sub mod { (&div($_[0], $_[1]))[1]; } + +sub _binop { + my ($func, $a, $b, $flag) = @_; + return $flag ? &$func($b, $a) : &$func($a, $b); +} + #----- Prime testing -------------------------------------------------------- {