BN_MOD_MUL_MONTGOMERY(3ossl)
π BN_MOD_MUL_MONTGOMERY β Montgomery multiplication, but make it make sense
okay so bestie, welcome to the 3ossl manual page for BN_MOD_MUL_MONTGOMERY. yes the name is a whole mouthful, no we are not changing it, it's iconic in its own way π
this is the OpenSSL squad's guide to Montgomery multiplication β the math trick that makes modular exponentiation go brrr. buckle up π¦
β¨ 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_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
BN_MONT_CTX *mont, BN_CTX *ctx);
int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);
int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
BN_CTX *ctx);
look at all those function signatures. so many. we love a full roster π
πΈ DESCRIPTION πΈ
so these functions implement Montgomery multiplication. the way that they're used automatically when BN_mod_exp(3) gets called with suitable input? lowkey magical. but here's the tea β they're also genuinely useful on their own when you've got several operations to do using the same modulus. reuse the setup, skip the repeat work, periodt.
BN_MONT_CTX_new()β allocates and initializes aBN_MONT_CTXstructure. brand new baby struct, freshly allocated, ready to live its main character era β¨BN_MONT_CTX_set()β sets up themontstructure from the modulusmby precomputing its inverse and a valueR. this is the prep work, the getting-ready-with-me moment πBN_MONT_CTX_copy()β copies theBN_MONT_CTXfromtoto... wait no, fromfromtoto. copy-paste but for structs π«ΆBN_MONT_CTX_free()β frees the components of theBN_MONT_CTX, and, if it was created byBN_MONT_CTX_new(), also the structure itself. and ifmontisNULL? nothing is done. no crash, no drama, it just vibes πBN_mod_mul_montgomery()β computesMont(a,b):=a*b*R^-1and places the result inr. that's the whole thing, that's the function πBN_from_montgomery()β performs the Montgomery reductionr = a*R^-1. bringing it back down to earth πBN_to_montgomery()β computesMont(a,R^2), i.e.a*R. β οΈ note thatamust be nonnegative and smaller than the modulus β this is not a suggestion bestie, it's a requirement π¨
and for all of these functions, ctx is a previously allocated BN_CTX used for temporary variables. don't forget to bring your ctx, it's the emotional support scratchpad π§
π₯ RETURN VALUES π₯
BN_MONT_CTX_new()returns the newly allocatedBN_MONT_CTX, andNULLon error. glowed up on success,NULLon the flop πBN_MONT_CTX_free()has no return value. it gives nothing back and that's fine, it's a giver not a taker π- for the other functions:
1is returned for success,0on error. and if you get a0? the error codes can be obtained byERR_get_error(3). go check, the receipts are there π
β οΈ WARNINGS β οΈ
The inputs must be reduced modulo m, otherwise the result will be outside the expected range. no cap. this is not a drill. reduce your inputs or perish (mathematically) π
π SEE ALSO π
ERR_get_error(3), BN_add(3), BN_CTX_new(3)
(the holy trinity of "go read these next", iykyk)
π HISTORY π
BN_MONT_CTX_init() was removed in OpenSSL 1.1.0. gone but not forgotten, rip to a real one π
π COPYRIGHT π
Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the whole page bestie!! this was OpenSSL 3.5.9, dated 2026-09-29, and you just read BN_MOD_MUL_MONTGOMERY(3ossl) π now go reduce your inputs modulo m before you embarrass us both. mwah πβ¨π¦
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms