views:

83

answers:

1

Hi,

I'm trying to implement the Solovoy-Strassen primality test for arbritrary large integers. I will also be writing a bignum (cannot use 3rd party implementation as this is an academic project). I have decided on the following structure for the bignum:

struct {
  uint64_t *tab;
  int size; // number of limbs
  int sign;
}

I will be using base-32 for my digits (hence uint64_t, for partial products, at least I assume they will be partial products). This decision was based on a previous question asked.

I'm at a standstill. I cannot conceive how one can take a string represented as an arbitrary size decimal and convert it into the bignum structure above.

Could someone please enlighten me. Even a smaller example would be nice, such as converting maybe an arbitrary string into octal digits which would be stored in a uint16_t array.

Thanks.

+3  A: 

You need to do the arithmetic, calling your routines. For example, if the string is "2013" (representing 2013 in decimal), do: a=0; a=10*a+2; a=10*a+0; a=10*a+1; a=10*a+3.

lhf
One can probably take 9 digits at a time, use the system-provided `toInt()` conversion, and then convert that into to `BigInt` before multiplying - should add some efficiency.
Hamish Grubijan
By last 9 digits, you mean the number of decimal digits that can be stored in a single digit of the base. That seems reasonable. But there will be a digit that will be in the intersection of two consecutive digits. I just don't know how to handle that case (yet).
nn
OpenSSL bn does that. See BN_dec2bn for instance in http://www.opensource.apple.com/source/OpenSSL098/OpenSSL098-27/src/crypto/bn/bn_print.c
lhf
@nn - you need not worry about intersection of consecutive digits. doing 9 digits at a time is not really different from doing 1 digit at a time - just a tad faster. The example 2013 is not that great because it does not force you to think that a is a `BigInt` and not an `int'. So, Step 1) input = '12345123456789123456789', BigInt result = new BigInt(0), BigInt multiplier = BigInt(1) 2) input = '12345123456789', result += multiplier * int('123456789'), multiplier *= 1000000000 3) input = '12345', result += multiplier * int('123456789'), multiplier *= 1000000000, 4) //finally computes the res.
Hamish Grubijan