BN_ADD_WORD(3)

๐Ÿ“ฆ version 4.4.0, in alpine 3.24, arch rolling

๐Ÿ’… BN_ADD_WORD(3) โ€” bestie's guide to BIGNUM math with plain integers โœจ

okay so this is BN_ADD_WORD(3), from the Library Functions Manual โ€” it's giving "tiny manpage, huge flex" because these functions do arithmetic on BIGNUMs with regular unsigned ints and they're so much faster than the normal BIGNUM ops. like, unbelievably faster. you're welcome. ๐ŸŽ€


โœจ NAME โœจ

BN_add_word, BN_sub_word, BN_mul_word, BN_div_word, BN_mod_word โ€” arithmetic functions on BIGNUMs with integers ๐Ÿงฎ๐Ÿ’–


๐ŸŒธ SYNOPSIS ๐ŸŒธ

library โ€œlibcryptoโ€

#include <openssl/bn.h>
int
BN_add_word(BIGNUM *a, BN_ULONG w);
int
BN_sub_word(BIGNUM *a, BN_ULONG w);
int
BN_mul_word(BIGNUM *a, BN_ULONG w);
BN_ULONG
BN_div_word(BIGNUM *a, BN_ULONG w);
BN_ULONG
BN_mod_word(const BIGNUM *a, BN_ULONG w);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

These functions do arithmetic operations on BIGNUMs with unsigned integers, and bestie, they are much more efficient than the normal BIGNUM arithmetic operations. like not even close. it's giving "why would you ever do it the slow way" energy. ๐Ÿ”ฅ

BN_ULONG is a macro that expands to unsigned long (= uint64_t) on _LP64 platforms, and to unsigned int (= uint32_t) everywhere else. iykyk ๐ŸŒˆ

  • BN_add_word() โ€” adds w to a (a+=w). simple. iconic. โœจ
  • BN_sub_word() โ€” subtracts w from a (a-=w). bye bye w ๐Ÿ’…
  • BN_mul_word() โ€” multiplies a and w (a*=w). they're in their multiplication era ๐Ÿซถ
  • BN_div_word() โ€” divides a by w (a/=w) and returns the remainder. yes, it does both. main character behavior ๐Ÿ‘‘
  • BN_mod_word() โ€” returns the remainder of a divided by w (a%w). just the leftovers, no division happening to your a ๐Ÿ“
โš ๏ธ for BN_div_word() and BN_mod_word(), w must not be 0. literally do not. div by zero is not the vibe and it will not slay. ๐Ÿ’€

๐Ÿ’œ RETURN VALUES ๐Ÿ’œ

BN_add_word(), BN_sub_word(), and BN_mul_word() return 1 for success or 0 on error. the error codes can be obtained by ERR_get_error(3) โ€” go look them up if something's off, bestie, don't just sit there guessing ๐Ÿ˜ญ

BN_mod_word() and BN_div_word() return a%w on success, and (BN_ULONG)-1 if an error occurred. yup, that's the error sentinel. remember it. โœจ


๐ŸŒˆ SEE ALSO ๐ŸŒˆ


๐Ÿงš HISTORY ๐Ÿงš

BN_add_word(), BN_div_word(), and BN_mod_word() first appeared in SSLeay 0.5.1. BN_sub_word() and BN_mul_word() first appeared in SSLeay 0.9.0. all these functions have been available since OpenBSD 2.4. ancient. historical. still serving. โญ

๐Ÿ™ˆ lowkey important: before 0.9.8a, the return value for BN_div_word() and BN_mod_word() in case of an error was 0. so like, if you're reading really old code, that's a thing that existed. fr fr.

that's the whole page bestie, we ate and left no crumbs ๐Ÿ’… โ€” this one's from the Debian build, dated June 8, 2025, and it lives at BN_ADD_WORD(3) in the Library Functions Manual. mwah ๐Ÿซถ๐Ÿ’–

๐Ÿ“ฆ where this page comes from