Bignums

Mikuro

Crotchety UI Nitpicker
I posted this yesterday, but I guess it got deleted with the DB errors macosx.com had. Anyway...

I need help using big (1024-bit) integers in Objective-C. ....lots and lots of help. * Really, I'm totally clueless here, and barely know where to start. I'm still not really "in my element" when it comes to C.

It looks like what I need is in vBigNum.h, under the Accelerate framework. But...I can't for the life of me figure out how to actually use it. Something like this...
Code:
#import <Accelerate/Accelerate.h>
.....
vU1024 booga;
booga = 100;
...clearly does not work, since vU1024 is not an int (not that I expected it to, but like I said, I'm clueless here).

How do I establish the value of a bigNum? And once established, how do I manipulate/print it? Back in REALbasic, I used the Extended Plugin. It seems like vBigNum isn't quite as flexible, but it also seems like it'll do what I need (I only need it to work up to about 10^175), if I could only figure out how to use it.

Despite my best efforts, I can't find any example code anywhere. Could someone with a little more experience please give me a push in the right direction?

Also, is vBigNum even where I ought to be looking? Speed is important here, so any advice in that regard would be greatly appreciated.
 

Viro

Registered
I posted this earlier but it got deleted due to DB errors too :).

This is where it looks like it'll get complex :).

BigNum(BN) works with structs and unions. Hence, you can't just assign to it with the usual = operator. Before you use your BN variable, you need to ensure that it is zero. In lieu of better documentation, I'm guessing that to perform an assignment, you need to clear out the old BN variable by setting it to zero, and then messing with the individual struct fields. here's an example of what I mean.

Code:
vU1024 booga;
memset(booga, 0, sizeof(booga));

booga.LSW = 10;
Because BN deals with numbers that are far larger than the natively supported data types, the only way to represent numbers that large (1024 bits) is to make it up of 32 * 32bit integers. Hence all the messing with struct fields. LSW represents the least significant word, i.e. the lowest 32 bits. d31 represents values >32bits and <= 64 bits. d30 represents values > 64 bits and <= 96 bits. The fields go right down to d2 and MSW.

With regards to performance, this library is probably the fastest you can find since it makes use of the Altivec engine and is tuned by Apple.
 
Top