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

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

<h1>BN_mod_mul_reciprocal(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_mod_mul_reciprocal, BN_div_recp, BN_RECP_CTX_new, BN_RECP_CTX_init,
BN_RECP_CTX_free, BN_RECP_CTX_set - modular multiplication using reciprocal

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bn.h&gt;
</PRE>
<PRE> BN_RECP_CTX *BN_RECP_CTX_new(void);
 void BN_RECP_CTX_init(BN_RECP_CTX *recp);
 void BN_RECP_CTX_free(BN_RECP_CTX *recp);
</PRE>
<PRE> int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *m, BN_CTX *ctx);
</PRE>
<PRE> int BN_div_recp(BIGNUM *dv, BIGNUM *rem, BIGNUM *a, BN_RECP_CTX *recp,
        BN_CTX *ctx);
</PRE>
<PRE> int BN_mod_mul_reciprocal(BIGNUM *r, BIGNUM *a, BIGNUM *b,
        BN_RECP_CTX *recp, BN_CTX *ctx);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
<CODE>BN_mod_mul_reciprocal()</CODE> can be used to perform an efficient
<A HREF="../crypto/BN_add.html#">BN_mod_mul(3)</A> operation when the operation will be performed repeatedly with the same
modulus. It computes <STRONG>r</STRONG>=(<STRONG>a</STRONG>*<STRONG>b</STRONG>)%<STRONG>m</STRONG>
using <STRONG>recp</STRONG>=1/<STRONG>m</STRONG>, which is set as described below.  <STRONG>ctx</STRONG> is a previously allocated <STRONG>BN_CTX</STRONG> used for temporary variables.

</P>
<P>
<CODE>BN_RECP_CTX_new()</CODE> allocates and initializes a <STRONG>BN_RECP</STRONG> structure. <CODE>BN_RECP_CTX_init()</CODE> initializes an existing
uninitialized <STRONG>BN_RECP</STRONG>.

</P>
<P>
<CODE>BN_RECP_CTX_free()</CODE> frees the components of the <STRONG>BN_RECP</STRONG>, and, if it was created by <CODE>BN_RECP_CTX_new(),</CODE> also the
structure itself.

</P>
<P>
<CODE>BN_RECP_CTX_set()</CODE> stores <STRONG>m</STRONG> in <STRONG>recp</STRONG> and sets it up for computing 1/<STRONG>m</STRONG> and shifting it left by <CODE>BN_num_bits(</CODE><STRONG>m</STRONG>)+1 to make it an integer. The result and the number of bits it was shifted
left will later be stored in <STRONG>recp</STRONG>.

</P>
<P>
<CODE>BN_div_recp()</CODE> divides <STRONG>a</STRONG> by <STRONG>m</STRONG> using <STRONG>recp</STRONG>. It places the quotient in <STRONG>dv</STRONG> and the remainder in <STRONG>rem</STRONG>.

</P>
<P>
The <STRONG>BN_RECP_CTX</STRONG> structure is defined as follows:

</P>
<PRE> typedef struct bn_recp_ctx_st
        {
        BIGNUM N;       /* the divisor */
        BIGNUM Nr;      /* the reciprocal */
        int num_bits;
        int shift;
        int flags;
        } BN_RECP_CTX;
</PRE>
<P>
It cannot be shared between threads.

</P>
<P>
<HR>
<H1><A NAME="RETURN_VALUES">RETURN VALUES</A></H1>
<P>
<CODE>BN_RECP_CTX_new()</CODE> returns the newly allocated <STRONG>BN_RECP_CTX</STRONG>, and NULL on error.

</P>
<P>
<CODE>BN_RECP_CTX_init()</CODE> and <CODE>BN_RECP_CTX_free()</CODE> have no
return values.

</P>
<P>
For the other functions, 1 is returned for success, 0 on error. 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_add.html#">BN_add(3)</A>,
<A HREF="../crypto/BN_CTX_new.html#">BN_CTX_new(3)</A>



</P>
<P>
<HR>
<H1><A NAME="HISTORY">HISTORY</A></H1>
<P>
<STRONG>BN_RECP_CTX</STRONG> was added in SSLeay 0.9.0. Before that, the function
<CODE>BN_reciprocal()</CODE> was used instead, and the
<CODE>BN_mod_mul_reciprocal()</CODE> arguments were different.

</P>
:}


