BN_MOD_MUL_MONTGOMERY(3SSL)
π BN_MOD_MUL_MONTGOMERY π
okay bestie, pull up a chair, because we're about to talk about Montgomery multiplication β and yes it's giving math, but it's fast math, which is the best kind. This is the OpenSSL 3SSL manual page for BN_mod_mul_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_free, BN_MONT_CTX_set, BN_MONT_CTX_copy, BN_from_montgomery, and BN_to_montgomery. seven functions, one very slay little corner of libcrypto. β¨
β¨ NAME β¨
BN_mod_mul_montgomery,BN_MONT_CTX_new,BN_MONT_CTX_free,BN_MONT_CTX_set,BN_MONT_CTX_copy,BN_from_montgomery,BN_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_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);
πΈ DESCRIPTION πΈ
so these functions implement Montgomery multiplication, which is the whole entire vibe. they get used automatically when you call BN_mod_exp(3) with suitable input, no effort required from you π«Ά β but they're also here for you directly if you've got several operations you want to do against the same modulus. that's the moment they really eat. π₯
BN_MONT_CTX_new()β allocates and initializes aBN_MONT_CTXstructure. brand new, freshly built, main character energy. πBN_MONT_CTX_set()β sets up themontstructure from the modulusmby precomputing its inverse and a valueR. this is the getting-ready montage scene. β¨BN_MONT_CTX_copy()β copies theBN_MONT_CTXfromtoto. yep, that's the direction, don't @ me, I'm just reporting the facts. π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 listen: ifmontisNULL, nothing is done. no crash, no drama, she simply does not care. πBN_mod_mul_montgomery()β computesMont(a,b):=a*b*R^-1and places the result inr. that's the moment. π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. β οΈ heads up though:amust be nonnegative and smaller than the modulus. no exceptions, she's strict.
for all of these functions, ctx is a previously allocated BN_CTX used for temporary variables. don't forget her, she's doing unpaid labor. π
β RETURN VALUES β
BN_MONT_CTX_new()returns the newly allocatedBN_MONT_CTX, andNULLon error. πBN_MONT_CTX_free()has no return value. she's above returning things. π- for the other functions,
1is returned for success,0on error. the error codes can be obtained byERR_get_error(3).
π WARNINGS π
the inputs must be reduced modulo m, otherwise the result will be outside the expected range. no cap, this is not a drill, check your reduction before you come crying to me. π¨
π¦ SEE ALSO π¦
π HISTORY π
BN_MONT_CTX_init() was removed in OpenSSL 1.1.0. she's gone girl, we don't talk about her anymore. π
π 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, we ate and left no crumbs πβ¨ this was OpenSSL version 4.0.3, dated 2026-09-29 β catch you in the next manpage, mwah ππ«Ά
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms