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

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

<h1>BN_mod_mul_montgomery(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="#WARNING">WARNING</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_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_init, BN_MONT_CTX_free,
BN_MONT_CTX_set, BN_MONT_CTX_copy, BN_from_montgomery, BN_to_montgomery -
Montgomery multiplication

</P>
<P>
<HR>
<H1><A NAME="SYNOPSIS">SYNOPSIS</A></H1>
<PRE> #include &lt;openssl/bn.h&gt;
</PRE>
<PRE> BN_MONT_CTX *BN_MONT_CTX_new(void);
 void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
 void BN_MONT_CTX_free(BN_MONT_CTX *mont);
</PRE>
<PRE> int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
 BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
</PRE>
<PRE> int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
         BN_MONT_CTX *mont, BN_CTX *ctx);
</PRE>
<PRE> int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
         BN_CTX *ctx);
</PRE>
<PRE> int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
         BN_CTX *ctx);
</PRE>
<P>
<HR>
<H1><A NAME="DESCRIPTION">DESCRIPTION</A></H1>
<P>
These functions implement Montgomery multiplication. They are used
automatically when <A HREF="../crypto/BN_add.html#">BN_mod_exp(3)</A> is called with suitable input, but they may be useful when several
operations are to be performed using the same modulus.

</P>
<P>
<CODE>BN_MONT_CTX_new()</CODE> allocates and initializes a <STRONG>BN_MONT_CTX</STRONG> structure. <CODE>BN_MONT_CTX_init()</CODE> initializes an existing
uninitialized <STRONG>BN_MONT_CTX</STRONG>.

</P>
<P>
<CODE>BN_MONT_CTX_set()</CODE> sets up the <EM>mont</EM> structure from the modulus <EM>m</EM>
by precomputing its inverse and a value R.

</P>
<P>
<CODE>BN_MONT_CTX_copy()</CODE> copies the <STRONG>BN_MONT_CTX</STRONG>  <EM>from</EM> to <EM>to</EM>.

</P>
<P>
<CODE>BN_MONT_CTX_free()</CODE> frees the components of the <STRONG>BN_MONT_CTX</STRONG>, and, if it was created by <CODE>BN_MONT_CTX_new(),</CODE> also the
structure itself.

</P>
<P>
<CODE>BN_mod_mul_montgomery()</CODE> computes <CODE>Mont(</CODE><EM>a</EM>,<EM>b</EM>):=<EM>a</EM>*<EM>b</EM>*R^-1 and places the result in <EM>r</EM>.

</P>
<P>
<CODE>BN_from_montgomery()</CODE> performs the Montgomery reduction <EM>r</EM> = <EM>a</EM>*R^-1.

</P>
<P>
<CODE>BN_to_montgomery()</CODE> computes <CODE>Mont(</CODE><EM>a</EM>,R^2), i.e. <EM>a</EM>*R. Note that <EM>a</EM> must be non-negative and smaller than the modulus.

</P>
<P>
For all functions, <EM>ctx</EM> is a previously allocated <STRONG>BN_CTX</STRONG> used for temporary variables.

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

</P>
<PRE> typedef struct bn_mont_ctx_st
        {
        int ri;         /* number of bits in R */
        BIGNUM RR;      /* R^2 (used to convert to Montgomery form) */
        BIGNUM N;       /* The modulus */
        BIGNUM Ni;      /* R*(1/R mod N) - N*Ni = 1
                         * (Ni is only stored for bignum algorithm) */
        BN_ULONG n0;    /* least significant word of Ni */
        int flags;
        } BN_MONT_CTX;
</PRE>
<P>
<CODE>BN_to_montgomery()</CODE> is a macro.

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

</P>
<P>
<CODE>BN_MONT_CTX_init()</CODE> and <CODE>BN_MONT_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="WARNING">WARNING</A></H1>
<P>
The inputs must be reduced modulo <STRONG>m</STRONG>, otherwise the result will be outside the expected range.

</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>
<CODE>BN_MONT_CTX_new(),</CODE> <CODE>BN_MONT_CTX_free(),</CODE>
<CODE>BN_MONT_CTX_set(),</CODE> <CODE>BN_mod_mul_montgomery(),</CODE>
<CODE>BN_from_montgomery()</CODE> and <CODE>BN_to_montgomery()</CODE> are
available in all versions of SSLeay and OpenSSL.

</P>
<P>
<CODE>BN_MONT_CTX_init()</CODE> and <CODE>BN_MONT_CTX_copy()</CODE> were
added in SSLeay 0.9.1b.

</P>
:}


