1

Help! I need to implement a C program (using only string, stdlib, and stdio libraries) which use modular exponentiation of really big numbers, some of them is 260 digits. I'm thinking of using a linked list but I cannot find a good reference on how to implement it.I need this because I need to use RSA to encrypt and decrypt a message.

Also, I have the exact same problem in getting the GCD of two very large numbers. Is there any way I can do this?

Xael Yvette
  • 77
  • 1
  • 9
  • I forgot to mention that the numbers I am going to do the modular is already stored in individual digits in a linked list – Xael Yvette Sep 24 '16 at 23:01
  • You'll need a `BigInteger` implementation in C. If you are limited to those libraries then this is going to be a lot of work. Is this homework? Are you sure you can't implement it with smaller numbers? – Luke Joshua Park Sep 24 '16 at 23:30
  • Yes it is. We are expected to deal with numbers greater than the limit of integers. @LukePark – Xael Yvette Sep 25 '16 at 00:27
  • Well good luck with that. You'll have to write your own BigInteger implementation. Have fun. – Luke Joshua Park Sep 25 '16 at 00:30

1 Answers1

2

How to store large numbers? You can use this type of storing data witch one helps you to use a small amount of memory and for operations will be much faster than anything else because you can make them directley on bits and you ca use the special formulas : For add Irecomand you to check the overflow ;

multiply (x=x*y):

aux=x;x=0;
while(y)
{ 
  if(last_bit(y)==1)
     x=x+aux;
  shift_left(aux);
  shift_right(y);
}

function modular_pow(base, exponent, modulus)
{
if modulus = 1 then return 0
Assert :: (modulus - 1) * (modulus - 1) does not overflow base
result = 1
base = base mod modulus
while exponent > 0
    if (last_bit(exponent)==1):
       result = (result * base) mod modulus
    exponent = exponent >> 1
    base = (base * base) mod modulus
return result
}
Community
  • 1
  • 1
Popovici Sebi
  • 258
  • 3
  • 13