Fully Portable Big Number Library

Discussion in 'C Programming' started by Tomás Ó hÉilidhe, Oct 27, 2008.

  1. I need a Big Number library. I've been considering switching my
    project to C++ but at the moment I'm exploring the avenue of keeping
    it in C.

    What's the best Big Number library for C? I need to be able to
    represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.

    So I need to be able to do division and multiplication on a 512-Bit
    number. Has anyone here had good experiences with a particular Big
    Number library?
    Tomás Ó hÉilidhe, Oct 27, 2008
    #1
    1. Advertising

  2. Tomás Ó hÉilidhe

    Phil Carmody Guest

    Tomás Ó hÉilidhe <> writes:
    > I need a Big Number library. I've been considering switching my
    > project to C++ but at the moment I'm exploring the avenue of keeping
    > it in C.
    >
    > What's the best Big Number library for C? I need to be able to
    > represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    > which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
    >
    > So I need to be able to do division and multiplication on a 512-Bit
    > number. Has anyone here had good experiences with a particular Big
    > Number library?


    GMP is supported on a very large number of platforms, and is very
    efficient as it's had a lot of effort spent optimising it. It
    achieves its portability via the traditional mechanism of a
    fairly thorough platform-differentiating configuration stage,
    and plenty of use of the pre-processor. Only small amounts of
    the code actually being compiled are identical on all platforms.
    I use GMP for all my 'small' bignum work (up to a few thousand
    digits).

    For something simpler, and designed with the express intention
    of being a learning experience in how bignums can be implemented
    for both the author and user, there's also a library called
    'libtommath' which is I believe public domain. It might not work
    in "36-bit la-la land", but seems to be portable across modern
    32- and 64-bit architectures. (I think I've also run it on 16-bit
    architectures too.) That might be good enough for your needs.

    Google is your friend.

    Phil
    --
    Richard Heathfield - please do not waste your time mailing unsolicited
    Christian ramblings in response to my signatures.

    As with the Christian religion, the worst advertisement for Socialism is its
    adherents. -- Eric Arthur Blair (1903-1950), The Road to Wigan Pier (1937)
    Phil Carmody, Oct 27, 2008
    #2
    1. Advertising

  3. Tomás Ó hÉilidhe

    Guest

    On Oct 27, 1:41 pm, Tomás Ó hÉilidhe <> wrote:
    > I need a Big Number library. I've been considering switching my
    > project to C++ but at the moment I'm exploring the avenue of keeping
    > it in C.
    >
    > What's the best Big Number library for C? I need to be able to
    > represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    > which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
    >
    > So I need to be able to do division and multiplication on a 512-Bit
    > number. Has anyone here had good experiences with a particular Big
    > Number library?


    First of all, asking for advice on libraries here is off-topic.
    Your questions (and my answers) are:

    > What's the best Big Number library for C?


    Define best (your subject suggests 'fully portable' = 'best', however
    you don't mention it in the body of your article)

    > Has anyone here had good experiences with a particular Big
    > Number library?


    Yes
    , Oct 28, 2008
    #3
  4. Tomás Ó hÉilidhe

    Ian Collins Guest

    wrote:
    > On Oct 27, 1:41 pm, Tomás Ó hÉilidhe <> wrote:
    >> I need a Big Number library. I've been considering switching my
    >> project to C++ but at the moment I'm exploring the avenue of keeping
    >> it in C.
    >>
    >> What's the best Big Number library for C? I need to be able to
    >> represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    >> which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
    >>
    >> So I need to be able to do division and multiplication on a 512-Bit
    >> number. Has anyone here had good experiences with a particular Big
    >> Number library?

    >
    > First of all, asking for advice on libraries here is off-topic.
    > Your questions (and my answers) are:
    >
    >> What's the best Big Number library for C?

    >
    > Define best (your subject suggests 'fully portable' = 'best', however
    > you don't mention it in the body of your article)
    >
    >> Has anyone here had good experiences with a particular Big
    >> Number library?

    >
    > Yes


    Well that was about as helpful as a fart in a wetsuit, wasn't it?

    --
    Ian Collins
    Ian Collins, Oct 28, 2008
    #4
  5. Tomás Ó hÉilidhe

    Don Bruder Guest

    In article
    <>,
    Tomas O hEilidhe <> wrote:

    > I need a Big Number library. I've been considering switching my
    > project to C++ but at the moment I'm exploring the avenue of keeping
    > it in C.
    >
    > What's the best Big Number library for C? I need to be able to
    > represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    > which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
    >
    > So I need to be able to do division and multiplication on a 512-Bit
    > number. Has anyone here had good experiences with a particular Big
    > Number library?


    Have you looked into "MIRACL"? I've long since forgotten what the
    letters stand for, and have no idea if it's still available, but it was
    a C library I used a few years back when I was tinkering with some
    *REALLY* big numbers as part of a crypto project. According to memory,
    the only practical limitation on it was available RAM - In theory, you
    could use it for doing all the math primitives (as well as input and
    formatted output, ala printf(), plus several built-in crypto functions)
    with arbitrary-length numbers - 5 decimal digits? No problem. 50 of 'em?
    500? 5000? Still no problem, although if you expected to exceed a
    certain number of decimal digits (I forget the exact number now) you
    needed to twiddle a #define in one of the header files and recompile the
    library.

    Largest number I ever personally saw come out of it (as an intermediate
    result from a calculation in the crypto project) took a little less than
    8 full screens worth of decimal digits to output to the 80x24 display of
    the old Apple //e I was working on at the time. (Which should also tell
    you that it's portable, and not all that big... I compiled it for the
    enhanced //e's 65C02 processor under Manx Aztec C, and the program that
    linked it ran in the main 64K bank of RAM.)

    --
    Don Bruder - - If your "From:" address isn't on my whitelist,
    or the subject of the message doesn't contain the exact text "PopperAndShadow"
    somewhere, any message sent to this address will go in the garbage without my
    ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
    Don Bruder, Oct 28, 2008
    #5
  6. Tomás Ó hÉilidhe

    user923005 Guest

    On Oct 27, 4:41 am, Tomás Ó hÉilidhe <> wrote:
    > I need a Big Number library. I've been considering switching my
    > project to C++ but at the moment I'm exploring the avenue of keeping
    > it in C.
    >
    > What's the best Big Number library for C? I need to be able to
    > represent the number (95 ^ 63), which is approximately (2 ^ 6.6) ^ 63,
    > which is about 2 ^ 416, so we're talking about maybe a 512-Bit number.
    >
    > So I need to be able to do division and multiplication on a 512-Bit
    > number. Has anyone here had good experiences with a particular Big
    > Number library?


    Maybe this can help you:
    http://pari.math.u-bordeaux.fr/benchs/timings-mpfr.html

    I am fond of MPFR.
    user923005, Oct 28, 2008
    #6
  7. Tomás Ó hÉilidhe

    Lew Pitcher Guest

    On October 28, 2008 15:21, in comp.lang.c, Don Bruder ()
    wrote:

    > Re: Fully Portable Big Number Library
    > From:
    > Don Bruder <>  (Chaotic Creations Unlimited)
    > Date:
    > October 28, 2008 15:21:47
    > Groups:
    > comp.lang.c
    > In article
    > <>,
    > Tomas O hEilidhe <> wrote:
    >
    >> I need a Big Number library. I've been considering switching my
    >> project to C++ but at the moment I'm exploring the avenue of keeping
    >> it in C.

    [snip]
    > Have you looked into "MIRACL"? I've long since forgotten what the
    > letters stand for,


    MIRACL stands for "Multiprecision Integer and Rational Arithmetic C/C++
    Library".


    > and have no idea if it's still available,


    Apparently, it is. Take a look at http://www.shamus.ie/


    [snip]
    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------
    Lew Pitcher, Oct 28, 2008
    #7
  8. Tomás Ó hÉilidhe

    user923005 Guest

    On Oct 28, 2:01 pm, "Malcolm McLean" <> wrote:
    > "Tomás Ó hÉilidhe" <> wrote in message
    >
    > > What's the best Big Number library for C?

    >
    > A libraries become faster they become bigger and more difficult to set up..


    I generally find it is no more difficult than:
    ../configure
    make

    If you are using a PC, then the MINGW toolchain is an easy way to
    build GNU style projects.

    > If speed is your priority you could do worse than use a GNU job that uses
    > things like Fast Fourier transforms for multiplications.
    >
    > If you simply want a not grossly-inefficient library to drop into code, pull
    > over the Basic Algorithms bignum source file, from my website.
    user923005, Oct 28, 2008
    #8
  9. Tomás Ó hÉilidhe <> wrote:
    > I need a Big Number library. I've been considering
    > switching my project to C++ but at the moment I'm
    > exploring the avenue of keeping it in C.
    >
    > What's the best Big Number library for C? I need
    > to be able to represent the number (95 ^ 63), which
    > is approximately (2 ^ 6.6) ^ 63, which is about 2 ^
    > 416, so we're talking about maybe a 512-Bit number.


    <http://groups.google.com/group/comp.lang.c/msg/696064e8b22a223d>

    > So I need to be able to do division and multiplication
    > on a 512-Bit number.


    Ah well in that case, add another 10 minutes onto the 2
    spent implementing multiplication. :)

    > Has anyone here had good experiences with a particular
    > Big Number library?


    I've had good experiences with the simplest of primary
    school algorithms.[1] In recent times, I only resort to
    genuine bignum algorithms or libraries when the
    calculation looks like taking days, not minutes.

    Couple your code if you have to, but first determine
    that you actually have to.

    [1] Try calculating the 73 digit base 16 narcissistic
    numbers, with or without a bignumb library.

    --
    Peter
    Peter Nilsson, Oct 28, 2008
    #9
  10. Tomás Ó hÉilidhe

    CBFalconer Guest

    Ian Collins wrote:
    > wrote:
    >> Tomás Ó hÉilidhe <> wrote:
    >>

    .... snip ...
    >>
    >> First of all, asking for advice on libraries here is off-topic.
    >> Your questions (and my answers) are:
    >>
    >>> What's the best Big Number library for C?

    >>
    >> Define best (your subject suggests 'fully portable' = 'best',
    >> however you don't mention it in the body of your article)
    >>
    >>> Has anyone here had good experiences with a particular Big
    >>> Number library?

    >>
    >> Yes

    >
    > Well that was about as helpful as a fart in a wetsuit, wasn't it?


    Which may be a great relief to the farter. :) Define terms.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Oct 29, 2008
    #10
  11. Tomás Ó hÉilidhe

    CBFalconer Guest

    Richard Harter wrote:
    > Richard Heathfield <> wrote:
    >> said:
    >>

    .... snip ...
    >>
    >>> First of all, asking for advice on libraries here is off-topic.

    >>
    >> Why? It seems to me that the topic of this group is C programming,
    >> regardless of platform. Library usage is certainly a part of
    >> programming. Okay, I can understand - and agree - that detailed
    >> discussions on the usage of platform-specific libraries is off-
    >> topic here, but what he's asking for is a fully portable library.
    >>
    >> I know of one reason why people might demur from helping the OP,
    >> but topicality isn't it.

    >
    > Well said. In almost all other language groups, the focus is on
    > programming in that language. Naturally the details of the
    > language standard(s) (if any) are part of the discussion, but
    > they are not the entirety.


    Well, the topic is portable C programming, as defined by the
    standard. To me, this means libraries are topical, provided that
    the library source is available, and primarily in standard C.
    Queries about library availability seems quite topical too.

    Observation: Big Number libraries do not require non-standard C.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Oct 29, 2008
    #11
  12. Tomás Ó hÉilidhe

    user923005 Guest

    On Oct 28, 2:51 pm, Peter Nilsson <> wrote:
    > Tomás Ó hÉilidhe <> wrote:
    >
    > > I need a Big Number library. I've been considering
    > > switching my project to C++ but at the moment I'm
    > > exploring the avenue of keeping it in C.

    >
    > > What's the best Big Number library for C? I need
    > > to be able to represent the number (95 ^ 63), which
    > > is approximately (2 ^ 6.6) ^ 63, which is about 2 ^
    > > 416, so we're talking about maybe a 512-Bit number.

    >
    > <http://groups.google.com/group/comp.lang.c/msg/696064e8b22a223d>
    >
    > > So I need to be able to do division and multiplication
    > > on a 512-Bit number.

    >
    > Ah well in that case, add another 10 minutes onto the 2
    > spent implementing multiplication. :)
    >
    > > Has anyone here had good experiences with a particular
    > > Big Number library?

    >
    > I've had good experiences with the simplest of primary
    > school algorithms.[1] In recent times, I only resort to
    > genuine bignum algorithms or libraries when the
    > calculation looks like taking days, not minutes.
    >
    > Couple your code if you have to, but first determine
    > that you actually have to.
    >
    > [1] Try calculating the 73 digit base 16 narcissistic
    > numbers, with or without a bignumb library.


    http://homepages.cwi.nl/~dik/english/mathematics/armstrong.html
    user923005, Oct 29, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederick Gotham

    Fully-portable version of limits.h

    Frederick Gotham, Sep 18, 2006, in forum: C Programming
    Replies:
    4
    Views:
    295
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
    Sep 19, 2006
  2. Shaguf
    Replies:
    0
    Views:
    326
    Shaguf
    Dec 24, 2008
  3. Shaguf
    Replies:
    0
    Views:
    431
    Shaguf
    Dec 26, 2008
  4. Shaguf
    Replies:
    0
    Views:
    218
    Shaguf
    Dec 26, 2008
  5. Shaguf
    Replies:
    0
    Views:
    202
    Shaguf
    Dec 24, 2008
Loading...

Share This Page