---
name: "libressl-BN_mod_mul_montgomery"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-BN_mod_mul_montgomery"
---

# libressl-BN\_mod\_mul\_montgomery(3)

Montgomery multiplication

## NAME

**BN\_MONT\_CTX\_new**, **BN\_MONT\_CTX\_free**, **BN\_MONT\_CTX\_set**, **BN\_MONT\_CTX\_set\_locked**, **BN\_MONT\_CTX\_copy**, **BN\_mod\_mul\_montgomery**, **BN\_from\_montgomery**, **BN\_to\_montgomery** – Montgomery multiplication

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/bn.h\>**

*BN\_MONT\_CTX* *\**\

**BN\_MONT\_CTX\_new**(*void*);

*void*\

**BN\_MONT\_CTX\_free**(*BN\_MONT\_CTX \*mont*);

*int*\

**BN\_MONT\_CTX\_set**(*BN\_MONT\_CTX \*mont*, *const BIGNUM \*m*, *BN\_CTX \*ctx*);

*BN\_MONT\_CTX* *\**\

**BN\_MONT\_CTX\_set\_locked**(*BN\_MONT\_CTX \*\*pmont*, *int lock*, *const BIGNUM \*m*, *BN\_CTX \*ctx*);

*BN\_MONT\_CTX* *\**\

**BN\_MONT\_CTX\_copy**(*BN\_MONT\_CTX \*to*, *const BN\_MONT\_CTX \*from*);

*int*\

**BN\_mod\_mul\_montgomery**(*BIGNUM \*r*, *const BIGNUM \*a*, *const BIGNUM \*b*, *BN\_MONT\_CTX \*mont*, *BN\_CTX \*ctx*);

*int*\

**BN\_from\_montgomery**(*BIGNUM \*r*, *const BIGNUM \*a*, *BN\_MONT\_CTX \*mont*, *BN\_CTX \*ctx*);

*int*\

**BN\_to\_montgomery**(*BIGNUM \*r*, *const BIGNUM \*a*, *BN\_MONT\_CTX \*mont*, *BN\_CTX \*ctx*);

## DESCRIPTION

These functions implement Montgomery multiplication. They are used automatically when BN\_mod\_exp(3) is called with suitable input, but they may be useful when several operations are to be performed using the same modulus.

**BN\_MONT\_CTX\_new**() allocates and initializes a *BN\_MONT\_CTX* structure.

**BN\_MONT\_CTX\_set**() sets up the *mont* structure from the modulus *m* by precomputing its inverse and a value R.

**BN\_MONT\_CTX\_set\_locked**() is a wrapper around **BN\_MONT\_CTX\_new**() and **BN\_MONT\_CTX\_set**() that is useful if more than one thread intends to use the same *BN\_MONT\_CTX* and none of these threads is exclusively responsible for creating and initializing the context. **BN\_MONT\_CTX\_set\_locked**() first acquires the specified *lock* using CRYPTO\_lock(3). If \**pmont* already differs from NULL, no action occurs. Otherwise, a new *BN\_MONT\_CTX* is allocated with **BN\_MONT\_CTX\_new**(), set up with **BN\_MONT\_CTX\_set**(), and a pointer to it is stored in \**pmont*. Finally, the *lock* is released.

**BN\_MONT\_CTX\_copy**() copies the *BN\_MONT\_CTX* *from* to *to*.

**BN\_MONT\_CTX\_free**() frees the components of the *BN\_MONT\_CTX*, and, if it was created by **BN\_MONT\_CTX\_new**(), also the structure itself. If *mont* is a NULL pointer, no action occurs.

**BN\_mod\_mul\_montgomery**() computes

Mont(*a*, *b*) := *a* \* *b* \* R^-1

and places the result in *r*.

**BN\_from\_montgomery**() performs the Montgomery reduction

*r* = *a* \* R^-1

**BN\_to\_montgomery**() computes

Mont(*a*, R^2) = *a* \* R

Note that *a* must be non-negative and smaller than the modulus.

For all functions, *ctx* is a previously allocated *BN\_CTX* used for temporary variables.

**Warning**: The inputs must be reduced modulo *m*, otherwise the result will be outside the expected range.

## RETURN VALUES

**BN\_MONT\_CTX\_new**() returns the newly allocated *BN\_MONT\_CTX* or NULL on error.

**BN\_MONT\_CTX\_set\_locked**() returns a pointer to the existing or newly created context or NULL on error.

For the other functions, 1 is returned for success or 0 on error. The error codes can be obtained by ERR\_get\_error(3).

## SEE ALSO

BN\_add(3), BN\_CTX\_new(3), BN\_new(3), CRYPTO\_lock(3)

## HISTORY

**BN\_MONT\_CTX\_new**(), **BN\_MONT\_CTX\_free**(), **BN\_MONT\_CTX\_set**(), **BN\_mod\_mul\_montgomery**(), **BN\_from\_montgomery**(), and **BN\_to\_montgomery**() first appeared in SSLeay 0.6.1 and have been available since OpenBSD 2.4.

**BN\_MONT\_CTX\_copy**() first appeared in SSLeay 0.9.1 and has been available since OpenBSD 2.6.

**BN\_MONT\_CTX\_set\_locked**() first appeared in OpenSSL 0.9.8 and has been available since OpenBSD 4.0.
