Re: [dev] suckless bignum library

From: Louis Santillan <lpsantil_AT_gmail.com>
Date: Thu, 10 Dec 2015 10:17:04 -0800

On Thu, Dec 10, 2015 at 7:30 AM, FRIGN <dev_AT_frign.de> wrote:
>
> On Wed, 9 Dec 2015 23:44:11 -0800
> Louis Santillan <lpsantil_AT_gmail.com> wrote:
>
> > Are libtommath [0]/tomsfastmath [1] not suckless?
> >
> > [0] https://github.com/libtom/libtommath
> > [1] https://github.com/libtom/tomsfastmath
>
> too complex for my tastes, but don't get me wrong. I know that for
> numerical perfection, you have to dig out very complex methods.
> A good example here are ODE-solvers (Euler, Runge-Kutta,...).
> I think a suckless bignum library should be much more lightweight.
> In the end, we don't want to do simulations on big mainframes,
> but just a library with just enough functions to get around with
> to handle tasks like dc(1) and bc(1).


Well, buried in David Dunfield's Micro-C site and within his
mc323exa.zip [0][1][2] file under MISC/LNUM.C is a Tom St. Denis
ported version of libtommath to the 16-bit not quite ANSI Micro-C.
It's 502 sloc, and will compile with gcc 4.8.4 on Ubuntu 14.04.3 using
CFLAGS="-c -Wall -ansi -pedantic" after you massage it lightly (add
#include <string.h>) to the top. I haven't tested it so it may make
some bad assumptions about word size, endianess, etc. Like most of
Tom St. Denis' work, it appears to be not-copyrighted (or public
domain, "Tom St Denis (tom_AT_dasoft.org), Jan 2000. Distribute like
mad!!!"). See the accompanying MISC/LNUM.TXT [2]. A file list from
the header is copied below. And if you remember SOH-CAH-TOA, you have
some trig functions as well.

[0] http://www.classiccmp.org/dunfield/dos/index.htm
[1] http://www.classiccmp.org/dunfield/dos/sample.txt
[2] http://www.classiccmp.org/dunfield/dos/mc323exa.zip

Function list
--------------
void l_copy(word *a, word *b) [ Copy the bignum from a to b ]
void l_clear(word *a) [ Set a to zero ]
void l_set(word *a, word n) [ Set a to the digit n ]

int l_iszero(word *a) [ is a == 0 ]
int l_cmp(word *a, word *b) [ compare a and b ]
int l_cmp_d(word *a, word b) [ compare a and 'b' ]

int l_shr(word *a, word *b) [ b = a/2 (return carry) ]
int l_shr_s(word *a) [ a = a/2 (return carry) ]
int l_shl(word *a, word *b) [ b = 2a (return carry) ]
int l_shl_s(word *a) [ a = 2a (return carry) ]

void l_add(word *a, word *b, word *c) [ c = a + b ]
void l_add_s(word *a, word *b) [ a += b ]
void l_add_d(word *a, word b) [ a += 'b' ]
void l_sub(word *a, word *b, word *c) [ c = a - b ]
void l_sub_s(word *a, word *b) [ a -= b ]
void l_sub_d(word *a, word b) [ a -= 'b']
void l_mul(word *a, word *b, word *c) [ c = ab ]
void l_mul_s(word *a, word *b) [ a *= b ]
void l_mul_d(word *a, word b) [ a *= 'b']
void l_div(word *a, word *b, word *q, word *r) [ q = a/b, r = a%b ]
void l_div_s(word *a, word *b) [ a /= b ]
void l_div_d(word *a, word b) [ a /= 'b']
void l_mod(word *a, word *b, word *c) [ c = a%b ]
void l_mod_s(word *a, word *b) [ a %= b ]
word l_mod_d(word *a, word b) [ returns a mod b ]

void l_addmod(word *a, word *b, word *m, word *c) [ c = (a+b) mod m ]
void l_submod(word *a, word *b, word *m, word *c) [ c = (a-b) mod m ]
void l_mulmod(word *a, word *b, word *m, word *c) [ c = (ab) mod m ]

void l_sqr(word *a) [ c = a^2 ]
void l_sqrmod(word *a, word *m, word *c) [ c = a^2 mod m ]
void l_expt(word *a, word *b, word *c) [ c = a^b ]
void l_exptmod(word *a, word *b, word *m, word *c) [ c = a^b mod m ]

void l_gcd(word *a, word *b, word *c) [ c = gcd(a, b) ]
void l_lcm(word *a, word *b, word *c) [ c = lcm(a, b) ]
void l_invmod(word *a, word *n, word *b) [ b = a^-1 mod n ]
void l_sqrt(word *n, word *N) [ N = n^1/2 ]

void l_print(word *a, word radix, FILE *out) [ Output ]
void l_println(word *a, word radix, FILE *out) [ Output with new line ]

[ radixes from 2 to 64 ]
void l_toradix(word *a, word radix, char *out) [ put in buffer ]
void l_fromradix(char *a, word radix, word *b) [ read string ]

void l_readraw(word *a, unsigned char *in, int len) [ read binary ]
int l_toraw(word *a, unsigned char *out) [ write binary ]
Received on Thu Dec 10 2015 - 19:17:04 CET

This archive was generated by hypermail 2.3.0 : Thu Dec 10 2015 - 19:24:10 CET