
#use wml::openssl-macros area=docs page=BN_add

<title>Documents, BN_add(3)</title>

<h1>BN_add(3)</h1>

#use wml::imp::generic

{:
## What's this? [[s|(<STRONG>[^<].+?)</A>(</STRONG><DD>)|$1$2|sg]]
[[s|<P>\s+<P>|<P>|sg]]
[[s|<P>\s+</|</|sg]]
[[s|<DD>\s*<DT>|<DD>&nbsp;<DT>|sg]]
[[s|<DD>\s*</DL>|<DD>&nbsp;</DL>|sg]]
[[s|\[|&#91;|sg]]
[[s|\]|&#93;|sg]]

<!-- INDEX BEGIN -->

<UL>

	<LI><A HREF="#NAME">NAME</A>
	<LI><A HREF="#SYNOPSIS">SYNOPSIS</A>
	<LI><A HREF="#DESCRIPTION">DESCRIPTION</A>
	<LI><A HREF="#RETURN_VALUES">RETURN VALUES</A>
	<LI><A HREF="#SEE_ALSO">SEE ALSO</A>
	<LI><A HREF="#HISTORY">HISTORY</A>
</UL>
<!-- INDEX END -->

<HR>
<P>
<HR>
<H1><A NAME="NAME">NAME</A></H1>
<P>
BN_add, BN_sub, BN_mul, BN_sqr, BN_div, BN_mod, BN_nnmod, BN_mod_add,
BN_mod_sub, BN_mod_mul, BN_mod_sqr, BN_exp, BN_mod_exp, BN_gcd - arithmetic
operations on BIGNUMs

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bn.h&gt;
</PRE>
<PRE> int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
</PRE>
<PRE> int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
</PRE>
<PRE> int BN_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
</PRE>
<PRE> int BN_sqr(BIGNUM *r, BIGNUM *a, BN_CTX *ctx);
</PRE>
<PRE> int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *a, const BIGNUM *d,
         BN_CTX *ctx);
</PRE>
<PRE> int BN_mod(BIGNUM *rem, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
</PRE>
<PRE> int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_add(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
         BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_sub(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
         BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_mul(BIGNUM *r, BIGNUM *a, BIGNUM *b, const BIGNUM *m,
         BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_sqr(BIGNUM *r, BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
</PRE>
<PRE> int BN_exp(BIGNUM *r, BIGNUM *a, BIGNUM *p, BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_exp(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
         const BIGNUM *m, BN_CTX *ctx);
</PRE>
<PRE> int BN_gcd(BIGNUM *r, BIGNUM *a, BIGNUM *b, BN_CTX *ctx);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BN_add()</CODE> adds <EM>a</EM> and <EM>b</EM> and places the result in <EM>r</EM> (<CODE>r=a+b</CODE>).
<EM>r</EM> may be the same <STRONG>BIGNUM</STRONG> as <EM>a</EM> or <EM>b</EM>.

</P>
<P>
<CODE>BN_sub()</CODE> subtracts <EM>b</EM> from <EM>a</EM> and places the result in <EM>r</EM> (<CODE>r=a-b</CODE>).

</P>
<P>
<CODE>BN_mul()</CODE> multiplies <EM>a</EM> and <EM>b</EM> and places the result in <EM>r</EM> (<CODE>r=a*b</CODE>).
<EM>r</EM> may be the same <STRONG>BIGNUM</STRONG> as <EM>a</EM> or <EM>b</EM>. For multiplication by powers of 2, use <A HREF="../crypto/BN_set_bit.html#">BN_lshift(3)</A>.

</P>
<P>
<CODE>BN_sqr()</CODE> takes the square of <EM>a</EM> and places the result in <EM>r</EM>
(<CODE>r=a^2</CODE>). <EM>r</EM> and <EM>a</EM> may be the same <STRONG>BIGNUM</STRONG>. This function is faster than <CODE>BN_mul(r,a,a).</CODE>

</P>
<P>
<CODE>BN_div()</CODE> divides <EM>a</EM> by <EM>d</EM> and places the result in <EM>dv</EM> and the remainder in <EM>rem</EM> (<CODE>dv=a/d, rem=a%d</CODE>). Either of <EM>dv</EM> and <EM>rem</EM> may be <STRONG>NULL</STRONG>, in which case the respective value is not returned. The result is rounded
towards zero; thus if <EM>a</EM> is negative, the remainder will be zero or negative. For division by powers
of 2, use <CODE>BN_rshift(3).</CODE>

</P>
<P>
<CODE>BN_mod()</CODE> corresponds to <CODE>BN_div()</CODE> with <EM>dv</EM> set to <STRONG>NULL</STRONG>.

</P>
<P>
<CODE>BN_nnmod()</CODE> reduces <EM>a</EM> modulo <EM>m</EM> and places the non-negative remainder in <EM>r</EM>.

</P>
<P>
<CODE>BN_mod_add()</CODE> adds <EM>a</EM> to <EM>b</EM> modulo <EM>m</EM> and places the non-negative result in <EM>r</EM>.

</P>
<P>
<CODE>BN_mod_sub()</CODE> subtracts <EM>b</EM> from <EM>a</EM> modulo <EM>m</EM> and places the non-negative result in <EM>r</EM>.

</P>
<P>
<CODE>BN_mod_mul()</CODE> multiplies <EM>a</EM> by <EM>b</EM> and finds the non-negative remainder respective to modulus <EM>m</EM> (<CODE>r=(a*b) mod m</CODE>). <EM>r</EM> may be the same <STRONG>BIGNUM</STRONG> as <EM>a</EM> or <EM>b</EM>. For more efficient algorithms for repeated computations using the same
modulus, see
<A HREF="../crypto/BN_mod_mul_montgomery.html#">BN_mod_mul_montgomery(3)</A> and
<A HREF="../crypto/BN_mod_mul_reciprocal.html#">BN_mod_mul_reciprocal(3)</A>.

</P>
<P>
<CODE>BN_mod_sqr()</CODE> takes the square of <EM>a</EM> modulo <STRONG>m</STRONG> and places the result in <EM>r</EM>.

</P>
<P>
<CODE>BN_exp()</CODE> raises <EM>a</EM> to the <EM>p</EM>-th power and places the result in <EM>r</EM>
(<CODE>r=a^p</CODE>). This function is faster than repeated applications of
<CODE>BN_mul().</CODE>

</P>
<P>
<CODE>BN_mod_exp()</CODE> computes <EM>a</EM> to the <EM>p</EM>-th power modulo <EM>m</EM> (<CODE>r=a^p %
m</CODE>). This function uses less time and space than <CODE>BN_exp().</CODE>

</P>
<P>
<CODE>BN_gcd()</CODE> computes the greatest common divisor of <EM>a</EM> and <EM>b</EM> and places the result in <EM>r</EM>. <EM>r</EM> may be the same <STRONG>BIGNUM</STRONG> as <EM>a</EM> or
<EM>b</EM>.

</P>
<P>
For all functions, <EM>ctx</EM> is a previously allocated <STRONG>BN_CTX</STRONG> used for temporary variables; see <A HREF="../crypto/BN_CTX_new.html#">BN_CTX_new(3)</A>.

</P>
<P>
Unless noted otherwise, the result <STRONG>BIGNUM</STRONG> must be different from the arguments.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
For all functions, 1 is returned for success, 0 on error. The return value
should always be checked (e.g., <CODE>if (!BN_add(r,a,b)) goto err;</CODE>). The error codes can be obtained by <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>.

</P>
<P>
<HR>
<H1><A NAME="SEE_ALSO">SEE ALSO</A></H1>
<P>
<A HREF="../crypto/bn.html#">bn(3)</A>, <A HREF="../crypto/ERR_get_error.html#">ERR_get_error(3)</A>, <A HREF="../crypto/BN_CTX_new.html#">BN_CTX_new(3)</A>,
<A HREF="../crypto/BN_add_word.html#">BN_add_word(3)</A>, <A HREF="../crypto/BN_set_bit.html#">BN_set_bit(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<CODE>BN_add(),</CODE> <CODE>BN_sub(),</CODE> <CODE>BN_sqr(),</CODE>
<CODE>BN_div(),</CODE> <CODE>BN_mod(),</CODE> <CODE>BN_mod_mul(),</CODE>
<CODE>BN_mod_exp()</CODE> and <CODE>BN_gcd()</CODE> are available in all
versions of SSLeay and OpenSSL. The <EM>ctx</EM> argument to <CODE>BN_mul()</CODE> was added in SSLeay 0.9.1b.
<CODE>BN_exp()</CODE> appeared in SSLeay 0.9.0. <CODE>BN_nnmod(),</CODE>
<CODE>BN_mod_add(),</CODE> <CODE>BN_mod_sub(),</CODE> and
<CODE>BN_mod_sqr()</CODE> were added in OpenSSL 0.9.7.

</P>
:}


