Mathematical Operations Arbitrary Precision Math

88 char BN_bn2hexconst BIGNUM num; The function BN_bn2dec converts a BIGNUM into a decimal representation stored in a C-style string. The C-style string is allocated dynamically using OPENSSL_malloc , which must then be freed by the caller using OPENSSL_free . char BN_bn2decconst BIGNUM num; The function BN_hex2bn converts a hexadecimal representation of a number stored in a C-style string to a BIGNUM . The resulting value is stored in the supplied BIGNUM , or a new BIGNUM is created with BN_new if the BIGNUM is supplied as NULL . int BN_hex2bnBIGNUM num, const char str; The function BN_dec2bn converts a decimal representation of a number stored in a C-style string to a BIGNUM . The resulting value is stored in the supplied BIGNUM , or a new BIGNUM is created with BN_new if the BIGNUM is supplied as NULL . int BN_dec2bnBIGNUM num, const char str;

4.5.2 Mathematical Operations

With few exceptions, the majority of the remainder of the functions that make up the BN package all perform mathematical operations such as addition and multiplication. Most of the functions require at least two BIGNUM operands and store their result in a third BIGNUM . It is often safe to use one of the operands to store the result, but it isnt always, so you should exercise care in doing so. Consult Table 4-1 , which lists the most common arithmetic functions, if youre not sure. Unless otherwise noted in the table, the BIGNUM that will receive the result of the operation may not be the same as any of the operands. Each of the functions returns nonzero or zero, indicating success or failure, respectively. Many of the functions in Table 4-1 are shown as accepting an argument labeled ctx. This argument is a pointer to a BN_CTX structure. This argument may not be specified as NULL , but instead should be a context structure returned by BN_CTX_new . The purpose of the context structure is to store temporary values used by many of the arithmetic operations. Storing the temporary values in a context increases the performance of the various functions. When a context structure is no longer needed, it should be destroyed using BN_CTX_free . Table 4-1. Arithmetic functions for BIGNUMs Function Comments BN_addr, a, b r = a + b r may be the same as a or b. BN_subr, a, b r = a - b BN_mulr, a, b, ctx r = a x b r may be the same as a or b. BN_sqrr, a, ctx r = powa, 2 r may be the same as a. This function is faster than BN_mulr, a, a. BN_divd, r, a, b, ctx d = a b, r = a b Neither d nor r may be the same as either a or b. Either d or r may be NULL. BN_modr, a, b, ctx r = a b BN_nnmodr, a, b, ctx r = absa b BN_mod_addr, a, b, m, ctx r = absa + b m BN mod subr, a, b, m, r = absa - b m 89 ctx BN_mod_mulr, a, b, m, ctx r = absa x b m r may be the same as a or b. BN_mod_sqrr, a, m, ctx r = abspowa, 2 m BN_expr, a, p, ctx r = powa, p BN_mod_expr, a, p, m, ctx r = powa, 2 m BN_gcdr, a, b, ctx Finds the greatest common divisor of a and b. r may be the same as a or b.

4.5.3 Generating Prime Numbers