Part of the functions from <openssl/bn.h> take BN_CTX *ctx as the last argument.
This is a structure that stores temporary BIGNUM variables, allowing you to avoid frequent memory allocation when creating variables with repeated subroutine calls.
I thought that using ctx is not necessary, because it can only help optimize performance. But this functionality may not be used. For example, I will call the division operation only once.
I also found that in the OpenSSL 1.1.1 version, BN_mod, BN_div and BN_mul worked, even if NULL was passed instead of a ctx pointer.
In version 3.2.0, this leads to a segmentation fault.
Please explain the logic of using BN_CTX.
UPD: Minimally reproducible example
#include <openssl/bn.h>
#include <stdio.h>
int main()
{
BIGNUM *a = BN_new();
BIGNUM *d = BN_new();
BIGNUM *dv = BN_new();
BIGNUM *rem = BN_new();
BN_CTX *ctx = BN_CTX_new();
if(!BN_one(a) || !BN_one(d)) goto err;
if(!BN_div(dv, rem, a, d, ctx)) goto err; // a / d = (dv, rem)
if(!BN_print_fp(stdout, dv)) goto err;
putc('\n', stdout);
return 1;
err:
return 0;
}
If NULL is put in place of ctx in BN_div, execution will be terminated when using the BN_div result (dv or rem) with the Segmentation fault (core dumped).