LIBMP(3)
π LIBMP(3) β the libmp girlies' guide β¨
okay so bestie, you've stumbled onto the libmp page from the Library Functions Manual and likeβ¦ buckle up, because this is the traditional BSD multiple precision integer arithmetic library and she is lowkey a mess. full details below, no gatekeeping π«Ά
β¨ NAME β¨
libmp β the traditional BSD multiple precision integer arithmetic library π
πΈ SYNOPSIS πΈ
#include <mp.h>
the function prototypes are all hanging out in the main body of the text, iykyk β go read the DESCRIPTION, they're all there π
β οΈ bestie listen: apps using this interface must be linked with-lmp(that's this library) and-lcrypto(crypto(3)). both. don't skip one, that's not the vibe π
π DESCRIPTION π
okay first thing first, the elephant in the room: this interface is obsolete in favor of the crypto(3) BIGNUM library. she's giving legacy, not main character energy π
libmp is the traditional BSD multiple precision integer arithmetic library. she has a number of problems, and is unsuitable for use in any programs where reliability is a concern. she's provided here for compatibility only. periodt. π
these routines perform arithmetic on integers of arbitrary precision, stored using the defined type MINT. pointers to MINT get initialized using mp_itom() or mp_xtom(), and must be recycled with mp_mfree() once you're done with them β literally don't forget, bestie π§
routines that store a result in one of their arguments expect that argument to have also been initialized before you pass it in. so like, init everything first, okay? π
the following routines are defined and implemented:
π¦ the constructor / destructor girlies
MINT *mp_itom(short n);
MINT *mp_xtom(const char *s);
char *mp_mtox(const MINT *mp);
void mp_mfree(MINT *mp);
mp_itom()returns anMINTwith the value ofnβ¨mp_xtom()returns anMINTwith the value ofs, which is treated to be in hexadecimal π’- the return values from
mp_itom()andmp_xtom()must be released withmp_mfree()when they're no longer needed β like, actually release them π«Ά mp_mtox()returns a null-terminated hexadecimal string having the value ofmp; its return value must be released withfree()(free(3)) when it's no longer needed π
π the math girlies (basic ops)
void mp_madd(const MINT *mp1, const MINT *mp2, MINT *rmp);
void mp_msub(const MINT *mp1, const MINT *mp2, MINT *rmp);
void mp_mult(const MINT *mp1, const MINT *mp2, MINT *rmp);
mp_madd(), mp_msub(), and mp_mult() store the sum, difference, or product, respectively, of mp1 and mp2 in rmp. ate. π₯
π the division girlies
void mp_mdiv(const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp);
void mp_sdiv(const MINT *nmp, short d, MINT *qmp, short *ro);
mp_mdiv() computes the quotient and remainder of nmp and dmp and stores the result in qmp and rmp, respectively. mp_sdiv() is similar to mp_mdiv() except the divisor (dmp or d) and remainder (rmp or ro) are ordinary integers π
π the exponentiation girlies
void mp_pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp);
void mp_rpow(const MINT *bmp, short e, MINT *rmp);
mp_rpow() computes the result of bmp raised to the empth power and reduced modulo mmp; the result is stored in rmp. mp_pow() computes the result of bmp raised to the eth power and stores the result in rmp β
(yes bestie, that's exactly what the page says β copy-pasted energy from 1989, we don't editorialize π)
πΈ the I/O girlies
void mp_min(MINT *mp);
void mp_mout(const MINT *mp);
mp_min() reads a line from standard input, tries to interpret it as a decimal number, and if successful, stores the result in mp. mp_mout() prints the value, in decimal, of mp to standard output (without a trailing newline) π
π§ the gcd girlie
void mp_gcd(const MINT *mp1, const MINT *mp2, MINT *rmp);
mp_gcd() computes the greatest common divisor of mp1 and mp2 and stores the result in rmp π«Ά
π¦ the comparison girlie
int mp_mcmp(const MINT *mp1, const MINT *mp2);
mcmp compares the values of mp1 and mp2 and returns 0 if the two values are equal, a value greater than 0 if mp1 is greater than mp2, and a value less than 0 if mp2 is greater than mp1. vibe check complete β
π the copy girlie
void mp_move(const MINT *smp, MINT *tmp);
mp_move() copies the value of smp to tmp (both values must be initialized β no cap, both of them) π
π₯ the sqrt girlie
void mp_msqrt(const MINT *nmp, MINT *xmp, MINT *rmp);
mp_msqrt() computes the square root and remainder of nmp and stores them in xmp and rmp, respectively π
π IMPLEMENTATION NOTES π
this version of libmp is implemented in terms of the crypto(3) BIGNUM library. so like, she's a wrapper, she's wearing a costume, that's the whole thing π
π DIAGNOSTICS π
running out of memory or illegal operations result in error messages on standard error and a call to abort(3). likeβ¦ she just aborts. no warning text, no return code, she's out πββοΈπ¨
π SEE ALSO π
abort(3), bn(3), crypto(3), free(3), malloc(3), math(3)
go read those, bestie, they're literally the homework πβ¨
π¦ HISTORY π¦
a libmp library appeared in 4.3BSD. FreeBSD 2.2 shipped with a libmp implemented in terms of libgmp. this implementation appeared in FreeBSD 5.0 π
π BUGS π
okay the bug section is where it gets real, buckle up:
- errors are reported via output to standard error and abnormal program termination instead of via return values. the application cannot control this behavior. so like, you get an error and then you're gone, and there's nothing your code can do about it π
- it is not clear whether the string returned by
mp_mtox()may be written to by the caller. this implementation allows it, but others may not. ideally,mp_mtox()would take a pointer to a buffer to fill in. soβ¦ undefined-ish, handle with care π« - it is not clear whether using the same variable as both source and destination in a single invocation is permitted. some of the calls in this implementation allow this, while others do not. so like, please don't alias your args unless you enjoy chaos π
β οΈ tl;dr β she's iconic but she's not safe, she's not obsolete-proof, and she aborts on you. compatibility only, bestie. use crypto(3) for new stuff π
β¨ that's the page! β¨ this one's from Debian, dated September 7, 1989 β yes, the 80s, she's vintage, she's a relic, she's thriving in a retro way ππΈπ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2001 Dima Dorfman
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms