---
name: "libmp"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/libmp"
---

# libmp(3)

traditional BSD multiple precision integer arithmetic library

## NAME

**libmp** – traditional BSD multiple precision integer arithmetic library

## SYNOPSIS

**#include \<mp.h\>**

Function prototypes are given in the main body of the text.

Applications using this interface must be linked with **-l***mp* (this library) and **-l***crypto* ([crypto(3)](/crypto/3.md)).

## DESCRIPTION

This interface is obsolete in favor of the [crypto(3)](/crypto/3.md) *BIGNUM* library.

**libmp** is the traditional BSD multiple precision integer arithmetic library. It has a number of problems, and is unsuitable for use in any programs where reliability is a concern. It is provided here for compatibility only.

These routines perform arithmetic on integers of arbitrary precision stored using the defined type *MINT*. Pointers to *MINT* are initialized using **mp\_itom**() or **mp\_xtom**(), and must be recycled with **mp\_mfree**() when they are no longer needed. Routines which store a result in one of their arguments expect that the latter has also been initialized prior to being passed to it. The following routines are defined and implemented:

*MINT \** *Ns* **mp\_itom**(*short n*);

*MINT \** *Ns* **mp\_xtom**(*const char \*s*);

*char \** *Ns* **mp\_mtox**(*const MINT \*mp*);

*void* **mp\_mfree**(*MINT \*mp*);

**mp\_itom**() returns an *MINT* with the value of *n*. **mp\_xtom**() returns an *MINT* with the value of *s*, which is treated to be in hexadecimal. The return values from **mp\_itom**() and **mp\_xtom**() must be released with **mp\_mfree**() when they are no longer needed. **mp\_mtox**() returns a null-terminated hexadecimal string having the value of *mp*; its return value must be released with **free**() ([free(3)](/free/3.md)) when it is no longer needed.

*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*.

*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.

*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 *emp*th power and reduced modulo *mmp*; the result is stored in *rmp*. **mp\_pow**() computes the result of *bmp* raised to the *e*th power and stores the result in *rmp*.

*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).

*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*.

*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*.

*void* **mp\_move**(*const MINT \*smp*, *MINT \*tmp*);

**mp\_move**() copies the value of *smp* to *tmp* (both values must be initialized).

*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)](/crypto/3.md) *BIGNUM* library.

## DIAGNOSTICS

Running out of memory or illegal operations result in error messages on standard error and a call to [abort(3)](/abort/3.md).

## SEE ALSO

[abort(3)](/abort/3.md), bn(3), [crypto(3)](/crypto/3.md), [free(3)](/free/3.md), [malloc(3)](/malloc/3.md), [math(3)](/math/3.md)

## 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

Errors are reported via output to standard error and abnormal program termination instead of via return values. The application cannot control this behavior.

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.

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.
