Greetings!

In my current project (reinventing the wheel: a request tracking system) I use Crypt::RSA to encrypt username and password strings when sent between client and server. To exchange the public key between two perl clients are a piece of cake but I need to be able to exchange the key with a Visual Basic.NET client aswell. To do this I convert the modulus (a really big integer) to an array of bytes and then to a comma separated string. This is fairly easy to import in .NET Framework cryptography API (it works, I've tested it).

Anyway, I use the module Math::BigInt to be able to handle really large numbers. I have two subroutines: bigint_to_bytearray($bigint) and bytearray_to_bigint(@bytearray). The first one (used when converting the modulus to an array of bytes) runs fast on my development box (a Pentium II 350MHz) but the second one (used when converting an array of bytes back to a bigint) takes 4-5 seconds.

I'm wondering if there's something I can do to speed this up, besides getting a better development box? :)

sub bigint_to_bytearray { my $bigint = shift; my @bytes; while(1) { my ($q,$r) = $bigint->brsft(8); push(@bytes,$r+0); last if $q == 0; $bigint = Math::BigInt->new($q); } return @bytes; }
## This is the one I would like to speed up ## The array looks something like (127,6,64,27,166,33 .... ) sub bytearray_to_bigint { my @array = @_; my $count = Math::BigInt->new('0'); my $result = Math::BigInt->new('0'); foreach my $a (@array) { $result += $a * (256**$count++); } return $result; }

-- 
dempa


In reply to Converting an array of bytes into a BigInt - speed considerations by dempa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.