BN_MOD_MUL_RECIPROCAL(3SSL)

✨ BN_MOD_MUL_RECIPROCAL ✨

okay bestie, this is a page from the OpenSSL manual, section 3SSL, and it's all about doing modular multiplication the fast way when you're stuck in a loop. πŸ’…


🌸 NAME 🌸

BN_mod_mul_reciprocal, BN_div_recp, BN_RECP_CTX_new, BN_RECP_CTX_free, BN_RECP_CTX_set β€” modular multiplication using reciprocal


πŸŽ€ SYNOPSIS πŸŽ€

#include <openssl/bn.h>

BN_RECP_CTX *BN_RECP_CTX_new(void);
void BN_RECP_CTX_free(BN_RECP_CTX *recp);

int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);

int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, BN_RECP_CTX *recp,
                BN_CTX *ctx);

int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
                          BN_RECP_CTX *recp, BN_CTX *ctx);

πŸ’– DESCRIPTION πŸ’–

so here's the tea β˜• β€” BN_mod_mul_reciprocal() is your girl when you need an efficient BN_mod_mul(3) operation and you're gonna do it over and over and over again with the same modulus. like, why redo the work every single time? it computes r=(a*b)%m using recp=1/m, and that recp gets set up the way we describe down below. the ctx is a previously allocated BN_CTX that you use for temporary variables. 🫢

BN_RECP_CTX_new() allocates and initializes a BN_RECP structure. brand new, fresh out the box. ✨

BN_RECP_CTX_free() frees the components of the BN_RECP, and β€” if it was created by BN_RECP_CTX_new() β€” it also frees the structure itself. and if recp is NULL? nothing happens. literally nothing. it just looks at it and moves on. πŸ’€

BN_RECP_CTX_set() stores m in recp and sets it up for computing 1/m, then shifts it left by BN_num_bits(m)+1 to make it an integer. the result and the number of bits it was shifted left get stored in recp later. the way it's keeping receipts on both of those... πŸ“

BN_div_recp() divides a by m using recp. it places the quotient in dv and the remainder in rem. two outputs, one call, we love an efficient queen. πŸ‘‘

⚠️ important bestie: the BN_RECP_CTX structure cannot be shared between threads. no passing it around the group chat, no shared custody. one thread, one context. periodt.

🌈 RETURN VALUES 🌈

  • BN_RECP_CTX_new() returns the newly allocated BN_RECP_CTX, and NULL on error. 😭
  • BN_RECP_CTX_free() has no return value. it gives nothing back and that's fine, that's its whole vibe.
  • for the other functions, 1 is returned for success and 0 on error. if you need the error codes, go get them with ERR_get_error(3). πŸ”

πŸ¦‹ SEE ALSO πŸ¦‹

ERR_get_error(3), BN_add(3), BN_CTX_new(3)


πŸ’œ HISTORY πŸ’œ

BN_RECP_CTX_init() was removed in OpenSSL 1.1.0. she's gone, she's not coming back, we don't talk about her anymore. πŸ™ˆ


Copyright 2000-2017 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>.


that's the whole page, bestie β€” no notes, ate it up, left zero crumbs. πŸ“ this was OpenSSL 4.0.3, dated 2026-09-29, BN_MOD_MUL_RECIPROCAL(3SSL), and yes the version number is giving ✨ so put some respect on it. mwah πŸ’‹πŸ§š

πŸ“¦ where this page comes from