0

I don't understand what this statement means by using in conjunction with repeated subroutine calls.

"Since dynamic memory allocation to create BIGNUMs is rather expensive when used in conjunction with repeated subroutine calls, the BN_CTX structure is used."

Source: https://www.openssl.org/docs/man1.0.2/crypto/BN_CTX_new.html

Does this mean that it's expensive to create BIGNUMs many times? I don't understand why that means you should use a structure

itsmarziparzi
  • 699
  • 7
  • 21
  • BN_CTX is used so that you don't re-calculate BIGNUM in every sub-call. You can (thus don't use up memory for BN_CTX), but it will be cpu extensive, because it calls malloc meny times, so it's overall cheaper to have BN_CTX then to have BIGNUM reculate on each sub-call entry. – KamilCuk Dec 06 '18 at 21:53
  • Wouldn't just doing one malloc, assigning the pointer to a variable, and using that variable be the same thing? – itsmarziparzi Dec 06 '18 at 21:58
  • ? That's what is happening. And doing one malloc in `BN_CTX_new()` only is better then doing the same malloc in all the sub-calls that use BN_CTX. So it's cheaper, if the programmer can re-use same memory in different sub-calls, since allocating that memory is expensive. Actually, BN_CTX is a complex structure, it's not a "simple one malloc", it has multiple objects in it, so re-using it in different sub-calls will save many memory calls. So if one function uses the stack from BN_CTX thus reserves memory, other function can re-use that same memory. – KamilCuk Dec 06 '18 at 22:01
  • Ok, I guess my confusion was that the allocation is complex. I thought it was a simple allocation so I was confused why it had to be wrapped in a struct – itsmarziparzi Dec 06 '18 at 22:25

0 Answers0