2

I was just wondering if anyone can give me a hand with the code for a simple program. I've written the program but i seem to be strugling trying to calculate values with large values of e and n.

But now when i try to calculate the following fea(2, 968365546456, 132132156132132) it comes up with an error saying:

Error, (in fea) numeric exception: overflow

Can anyone help me with the code so I can fix the error? I'm assuming it will need an then if statement?

My code so far is:

fea := proc (x, e, n)
    (x^e) mod n;  
end proc;

The procedure

help-info.de
  • 6,695
  • 16
  • 39
  • 41
Gibberish
  • 147
  • 9
  • 1
    Welcome to Stack Overflow! Please go through the [tour](http://stackoverflow.com/tour), the [help center](http://stackoverflow.com/help) and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to help you improve your current and future questions, which can help you get better answers. – help-info.de Dec 09 '16 at 15:15

2 Answers2

2

The help page for topic mod states this:

"To compute `mod`(i^n,m) where i is an integer, it is undesirable
 to use this "obvious" syntax because the powering will be
 performed first over the integers (possibly resulting in a very
 large integer) before reduction modulo m. Rather, the inert
 operator &^ should be used: i &^ n mod m.  In the latter form,
 the powering will be performed intelligently by the mod operation."

So let's see:

restart;

12367^ 13 mod 87; # for basic test

                          67

fea := proc (x, e, n)
   (x &^ e) mod n;  
end proc:

fea(12367, 13, 87);

                           67

# The following returns very quickly.

fea(2, 968365546456, 132132156132132);

               131464616935876

Your original was attempting to compute the intermediate result:

restart;

2^968365546456;
Error, numeric exception: overflow
acer
  • 6,671
  • 15
  • 15
1

One of the best method for calculating power is Square and Multiply Algorithm for Modular Exponentiation.

This algorithm is in the following form

Square and Multiply Algorithm for Modular Exponentiation

For example Example

the maple proc of this algorithm is as follows

Maple code

suppose that we want to compute a^b mod n. This line

for i from 0 to b while 2^i <= b do c := c+1 end do

compute the binary length of number b. This line

for i from 0 to c-1 do if irem(m, 2, 'q') = 1 then B[1, c-i] := 1 end if; m := q end do

obtain the binary form of number b and place in a matrix. This line

for i to c do m := irem(m^2, n); if B[1, i] = 1 then m := irem(m*a, n) end if end do

do square and multiply algorithm. Please do one example to learn it.

If you want to obtain (a^b mod n) you should first run proce code and after that write Pow(a,b,n) and do enter key. For example for your numbers, after run the proce you should write

Pow(2, 968365546456, 132132156132132)

and after enter key you see the following message

2^(968365546456) mod (132132156132132) = (131464616935876)

Please see the source code. The best book in this field is Introduction to Cryptography with Maple.

Amin235
  • 111
  • 4
  • It was good to mention why did you vote to this answer negative. I answered to this question with the example and maple code and after that you voted negative. Please vote in Fair way. Thanks – Amin235 Dec 12 '16 at 05:59
  • i didnt downvote it , i just voted now, thank you for your script it is very useful to look at it in a different way, how would you evaluate the following with this script fea(2, 968365546456, 132132156132132) – Gibberish Dec 12 '16 at 12:55
  • @mathdeds I edited my post to clarify how to work with this maple code. Thanks for your vote. – Amin235 Dec 12 '16 at 17:19
  • Thank you i have now been able to do this, is it possible you could add comments to the script as i want to understand what every part of the script is doing i understand setting up the local variables but after there i am lost. – Gibberish Dec 12 '16 at 17:24
  • @mathdeds I edited again. If you do one example manually you find this powerful algorithm. another algorithm for this question is **naf algorithm**. – Amin235 Dec 12 '16 at 17:42