BN_MOD_MUL_MONTGOMERY(3)
BN_MOD_MUL_MONTGOMERY(3) β Montgomery multiplication π β¨
okay so bestie, this is the Library Functions Manual page for BN_MOD_MUL_MONTGOMERY(3), and it's literally all about Montgomery multiplication. Buckle up, it's giving math but make it cute π
NAME π
BN_MONT_CTX_newBN_MONT_CTX_freeBN_MONT_CTX_setBN_MONT_CTX_set_lockedBN_MONT_CTX_copyBN_mod_mul_montgomeryBN_from_montgomeryBN_to_montgomery
β Montgomery multiplication π
SYNOPSIS π¦
#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. iconic behavior, honestly π
BN_MONT_CTX_new()β allocates and initializes aBN_MONT_CTXstructure. fresh context, no cap β¨BN_MONT_CTX_set()β sets up themontstructure from the modulusmby precomputing its inverse and a valueR. precompute era π§BN_MONT_CTX_set_locked()β a wrapper aroundBN_MONT_CTX_new()andBN_MONT_CTX_set()that is useful if more than one thread intends to use the sameBN_MONT_CTXand none of these threads is exclusively responsible for creating and initializing the context. It first acquires the specified lock usingCRYPTO_lock(3). If*pmontalready differs fromNULL, no action occurs. Otherwise, a newBN_MONT_CTXis allocated withBN_MONT_CTX_new(), set up withBN_MONT_CTX_set(), and a pointer to it is stored in*pmont. Finally, the lock is released. lock girlie behavior πBN_MONT_CTX_copy()β copies theBN_MONT_CTXfromtoto. yes, that's exactly what it says, bestie π«ΆBN_MONT_CTX_free()β frees the components of theBN_MONT_CTX, and, if it was created byBN_MONT_CTX_new(), also the structure itself. Ifmontis aNULLpointer, no action occurs. clean up, clean up π§ΉBN_mod_mul_montgomery()β computes
Mont(a, b) := a * b * R^-1
and places the result in r. math slay π₯
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. important lil rule, don't skip it π
For all functions, ctx is a previously allocated BN_CTX used for temporary variables. temp storage bestie π
β οΈ Warning: The inputs must be reduced modulo m, otherwise the result will be outside the expected range. no cap, do the reduction first π
RETURN VALUES π
BN_MONT_CTX_new()returns the newly allocatedBN_MONT_CTXorNULLon error.BN_MONT_CTX_set_locked()returns a pointer to the existing or newly created context orNULLon error.- For the other functions,
1is returned for success or0on error. The error codes can be obtained byERR_get_error(3).
SEE ALSO π
HISTORY π
BN_MONT_CTX_new(),BN_MONT_CTX_free(),BN_MONT_CTX_set(),BN_mod_mul_montgomery(),BN_from_montgomery(), andBN_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.
That's the whole BN_MOD_MUL_MONTGOMERY(3) tea β β source/version: Debian; date: March 9, 2025. mwah πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: libressl-devel-doc 4.1.0-bp160.1.1 Β· OpenSSL
- Copyright (c) 2021 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2000 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms