Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use bigint and float perl

Tags:

perl

I am extremely new to Perl, so I hope you will excuse my inexperience.

I have the following lines of code:

use warnings;
use strict;
use POSIX 'ceil';
use bigint;


my($g, $y, $n) = ($ARGV[0], $ARGV[1], $ARGV[2]);

my $z = ceil(sqrt($n-1));

my $entry;

print "list1: \n";
for my $v (0 .. $z) {
    $entry = ($g ** $v) % $n;
    $entry = ($entry ** ($n - 2)) % $n;
    $entry = ($entry * $y) % $n;

    print "$entry : $v\n";
}

print "list2: \n";
for my $u (0 .. $z) {
    $entry = ($g ** ($u * $z)) % $n;
    print "$entry: $u\n";
}

I need to use the bigint environment because of some following statements. Whenever I look into $z it evaluates to 6 instead of 7, when i call my program with $n = 41. It looks as if the bigint environment rounds the value of the sqrt method. I also tried to use BigFloat instead of bigint, but then the result of $entry = ($g ** ($u * $z)) % $n; gets calculated wrong (with ($g, $y, $n) = (15, 38, 41) the result is 3, when $u reached 3 in the for loop, but should be 26 instead).

Is there any option to avoid this rounding, so I can use float while calculating the square root and bigint in all following statements, so the pow operation works properly?

My call is perl program.pl 15 38 41. I try to implement the baby-step-giant-step algorithm.

like image 432
Heinz Bauer Avatar asked Oct 23 '25 16:10

Heinz Bauer


1 Answers

you need to use bignum, not bigint:

$ cat bauer.pl 
#!/usr/bin/perl
use warnings;
use strict;
use POSIX;
use bigint;


my($g, $y, $n) = ($ARGV[0], $ARGV[1], $ARGV[2]);

my $z = ceil(sqrt($n-1));

$ perl r.pl 
1.41421356237309504880168872420969807857

With your program signature:

$ cat bauer.pl 
#!/usr/bin/perl

use warnings;
use strict;
use POSIX;
use bignum;


my($g, $y, $n) = ($ARGV[0], $ARGV[1], $ARGV[2]);

my $z = ceil(sqrt($n-1));
print STDOUT "$z\n";

$ perl bauer.pl 1 2 48
7
like image 59
richyen Avatar answered Oct 26 '25 09:10

richyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!