\begin{quote}
Big\+Num in pure javascript \end{quote}
\href{http://travis-ci.org/indutny/bn.js}{\texttt{ }}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4001}{}\doxysection{Install}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4001}
{\ttfamily npm install -\/-\/save bn.\+js}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4002}{}\doxysection{Usage}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4002}

\begin{DoxyCode}{0}
\DoxyCodeLine{const BN = require('bn.js');}
\DoxyCodeLine{}
\DoxyCodeLine{var a = new BN('dead', 16);}
\DoxyCodeLine{var b = new BN('101010', 2);}
\DoxyCodeLine{}
\DoxyCodeLine{var res = a.add(b);}
\DoxyCodeLine{console.log(res.toString(10));  // 57047 }

\end{DoxyCode}


{\bfseries{Note}}\+: decimals are not supported in this library.\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4003}{}\doxysection{Notation}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4003}
\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4004}{}\doxysubsection{Prefixes}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4004}
There are several prefixes to instructions that affect the way the work. Here is the list of them in the order of appearance in the function name\+:


\begin{DoxyItemize}
\item {\ttfamily i} -\/ perform operation in-\/place, storing the result in the host object (on which the method was invoked). Might be used to avoid number allocation costs
\item {\ttfamily u} -\/ unsigned, ignore the sign of operands when performing operation, or always return positive value. Second case applies to reduction operations like {\ttfamily mod()}. In such cases if the result will be negative -\/ modulo will be added to the result to make it positive
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4005}{}\doxysubsection{Postfixes}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4005}

\begin{DoxyItemize}
\item {\ttfamily n} -\/ the argument of the function must be a plain Java\+Script Number. Decimals are not supported.
\item {\ttfamily rn} -\/ both argument and return value of the function are plain Java\+Script Numbers. Decimals are not supported.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4006}{}\doxysubsection{Examples}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4006}

\begin{DoxyItemize}
\item {\ttfamily a.\+iadd(b)} -\/ perform addition on {\ttfamily a} and {\ttfamily b}, storing the result in {\ttfamily a}
\item {\ttfamily a.\+umod(b)} -\/ reduce {\ttfamily a} modulo {\ttfamily b}, returning positive value
\item {\ttfamily a.\+iushln(13)} -\/ shift bits of {\ttfamily a} left by 13
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4007}{}\doxysection{Instructions}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4007}
Prefixes/postfixes are put in parens at the of the line. {\ttfamily endian} -\/ could be either {\ttfamily le} (little-\/endian) or {\ttfamily be} (big-\/endian).\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4008}{}\doxysubsection{Utilities}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4008}

\begin{DoxyItemize}
\item {\ttfamily a.\+clone()} -\/ clone number
\item {\ttfamily a.\+to\+String(base, length)} -\/ convert to base-\/string and pad with zeroes
\item {\ttfamily a.\+to\+Number()} -\/ convert to Javascript Number (limited to 53 bits)
\item {\ttfamily a.\+to\+JSON()} -\/ convert to JSON compatible hex string (alias of {\ttfamily to\+String(16)})
\item {\ttfamily a.\+to\+Array(endian, length)} -\/ convert to byte {\ttfamily Array}, and optionally zero pad to length, throwing if already exceeding
\item {\ttfamily a.\+to\+Array\+Like(type, endian, length)} -\/ convert to an instance of {\ttfamily type}, which must behave like an {\ttfamily Array}
\item {\ttfamily a.\+to\+Buffer(endian, length)} -\/ convert to Node.\+js Buffer (if available). For compatibility with browserify and similar tools, use this instead\+: {\ttfamily a.\+to\+Array\+Like(\+Buffer, endian, length)}
\item {\ttfamily a.\+bit\+Length()} -\/ get number of bits occupied
\item {\ttfamily a.\+zero\+Bits()} -\/ return number of less-\/significant consequent zero bits (example\+: {\ttfamily 1010000} has 4 zero bits)
\item {\ttfamily a.\+byte\+Length()} -\/ return number of bytes occupied
\item {\ttfamily a.\+is\+Neg()} -\/ true if the number is negative
\item {\ttfamily a.\+is\+Even()} -\/ no comments
\item {\ttfamily a.\+is\+Odd()} -\/ no comments
\item {\ttfamily a.\+is\+Zero()} -\/ no comments
\item {\ttfamily a.\+cmp(b)} -\/ compare numbers and return {\ttfamily -\/1} (a {\ttfamily $<$} b), {\ttfamily 0} (a {\ttfamily ==} b), or {\ttfamily 1} (a {\ttfamily $>$} b) depending on the comparison result ({\ttfamily ucmp}, {\ttfamily cmpn})
\item {\ttfamily a.\+lt(b)} -\/ {\ttfamily a} less than {\ttfamily b} ({\ttfamily n})
\item {\ttfamily a.\+lte(b)} -\/ {\ttfamily a} less than or equals {\ttfamily b} ({\ttfamily n})
\item {\ttfamily a.\+gt(b)} -\/ {\ttfamily a} greater than {\ttfamily b} ({\ttfamily n})
\item {\ttfamily a.\+gte(b)} -\/ {\ttfamily a} greater than or equals {\ttfamily b} ({\ttfamily n})
\item {\ttfamily a.\+eq(b)} -\/ {\ttfamily a} equals {\ttfamily b} ({\ttfamily n})
\item {\ttfamily a.\+to\+Twos(width)} -\/ convert to two\textquotesingle{}s complement representation, where {\ttfamily width} is bit width
\item {\ttfamily a.\+from\+Twos(width)} -\/ convert from two\textquotesingle{}s complement representation, where {\ttfamily width} is the bit width
\item {\ttfamily BN.\+is\+BN(object)} -\/ returns true if the supplied {\ttfamily object} is a BN.\+js instance
\item {\ttfamily BN.\+max(a, b)} -\/ return {\ttfamily a} if {\ttfamily a} bigger than {\ttfamily b}
\item {\ttfamily BN.\+min(a, b)} -\/ return {\ttfamily a} if {\ttfamily a} less than {\ttfamily b}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4009}{}\doxysubsection{Arithmetics}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4009}

\begin{DoxyItemize}
\item {\ttfamily a.\+neg()} -\/ negate sign ({\ttfamily i})
\item {\ttfamily a.\+abs()} -\/ absolute value ({\ttfamily i})
\item {\ttfamily a.\+add(b)} -\/ addition ({\ttfamily i}, {\ttfamily n}, {\ttfamily in})
\item {\ttfamily a.\+sub(b)} -\/ subtraction ({\ttfamily i}, {\ttfamily n}, {\ttfamily in})
\item {\ttfamily a.\+mul(b)} -\/ multiply ({\ttfamily i}, {\ttfamily n}, {\ttfamily in})
\item {\ttfamily a.\+sqr()} -\/ square ({\ttfamily i})
\item {\ttfamily a.\+pow(b)} -\/ raise {\ttfamily a} to the power of {\ttfamily b}
\item {\ttfamily a.\+div(b)} -\/ divide ({\ttfamily divn}, {\ttfamily idivn})
\item {\ttfamily a.\+mod(b)} -\/ reduct ({\ttfamily u}, {\ttfamily n}) (but no {\ttfamily umodn})
\item {\ttfamily a.\+div\+Round(b)} -\/ rounded division
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4010}{}\doxysubsection{Bit operations}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4010}

\begin{DoxyItemize}
\item {\ttfamily a.\+or(b)} -\/ or ({\ttfamily i}, {\ttfamily u}, {\ttfamily iu})
\item {\ttfamily a.\+and(b)} -\/ and ({\ttfamily i}, {\ttfamily u}, {\ttfamily iu}, {\ttfamily andln}) (NOTE\+: {\ttfamily andln} is going to be replaced with {\ttfamily andn} in future)
\item {\ttfamily a.\+xor(b)} -\/ xor ({\ttfamily i}, {\ttfamily u}, {\ttfamily iu})
\item {\ttfamily a.\+setn(b)} -\/ set specified bit to {\ttfamily 1}
\item {\ttfamily a.\+shln(b)} -\/ shift left ({\ttfamily i}, {\ttfamily u}, {\ttfamily iu})
\item {\ttfamily a.\+shrn(b)} -\/ shift right ({\ttfamily i}, {\ttfamily u}, {\ttfamily iu})
\item {\ttfamily a.\+testn(b)} -\/ test if specified bit is set
\item {\ttfamily a.\+maskn(b)} -\/ clear bits with indexes higher or equal to {\ttfamily b} ({\ttfamily i})
\item {\ttfamily a.\+bincn(b)} -\/ add {\ttfamily 1 $<$$<$ b} to the number
\item {\ttfamily a.\+notn(w)} -\/ not (for the width specified by {\ttfamily w}) ({\ttfamily i})
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4011}{}\doxysubsection{Reduction}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4011}

\begin{DoxyItemize}
\item {\ttfamily a.\+gcd(b)} -\/ GCD
\item {\ttfamily a.\+egcd(b)} -\/ Extended GCD results ({\ttfamily \{ a\+: ..., b\+: ..., gcd\+: ... \}})
\item {\ttfamily a.\+invm(b)} -\/ inverse {\ttfamily a} modulo {\ttfamily b}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4012}{}\doxysection{Fast reduction}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4012}
When doing lots of reductions using the same modulo, it might be beneficial to use some tricks\+: like \href{https://en.wikipedia.org/wiki/Montgomery_modular_multiplication}{\texttt{ Montgomery multiplication}}, or using special algorithm for \href{https://en.wikipedia.org/wiki/Mersenne_prime}{\texttt{ Mersenne Prime}}.\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4013}{}\doxysubsection{Reduction context}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4013}
To enable this tricks one should create a reduction context\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var red = BN.red(num); }

\end{DoxyCode}
 where {\ttfamily num} is just a BN instance.

Or\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var red = BN.red(primeName); }

\end{DoxyCode}


Where {\ttfamily prime\+Name} is either of these \href{https://en.wikipedia.org/wiki/Mersenne_prime}{\texttt{ Mersenne Primes}}\+:


\begin{DoxyItemize}
\item `\textquotesingle{}k256'{\ttfamily  $\ast$}\textquotesingle{}p224\textquotesingle{}{\ttfamily  $\ast$}\textquotesingle{}p192\textquotesingle{}{\ttfamily  $\ast$}\textquotesingle{}p25519\textquotesingle{}\`{}
\end{DoxyItemize}

Or\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var red = BN.mont(num); }

\end{DoxyCode}


To reduce numbers with \href{https://en.wikipedia.org/wiki/Montgomery_modular_multiplication}{\texttt{ Montgomery trick}}. {\ttfamily .mont()} is generally faster than {\ttfamily .red(num)}, but slower than {\ttfamily BN.\+red(prime\+Name)}.\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4014}{}\doxysubsection{Converting numbers}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4014}
Before performing anything in reduction context -\/ numbers should be converted to it. Usually, this means that one should\+:


\begin{DoxyItemize}
\item Convert inputs to reducted ones
\item Operate on them in reduction context
\item Convert outputs back from the reduction context
\end{DoxyItemize}

Here is how one may convert numbers to {\ttfamily red}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var redA = a.toRed(red); }

\end{DoxyCode}
 Where {\ttfamily red} is a reduction context created using instructions above

Here is how to convert them back\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var a = redA.fromRed(); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4015}{}\doxysubsection{Red instructions}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4015}
Most of the instructions from the very start of this readme have their counterparts in red context\+:


\begin{DoxyItemize}
\item {\ttfamily a.\+red\+Add(b)}, {\ttfamily a.\+red\+IAdd(b)}
\item {\ttfamily a.\+red\+Sub(b)}, {\ttfamily a.\+red\+ISub(b)}
\item {\ttfamily a.\+red\+Shl(num)}
\item {\ttfamily a.\+red\+Mul(b)}, {\ttfamily a.\+red\+IMul(b)}
\item {\ttfamily a.\+red\+Sqr()}, {\ttfamily a.\+red\+ISqr()}
\item {\ttfamily a.\+red\+Sqrt()} -\/ square root modulo reduction context\textquotesingle{}s prime
\item {\ttfamily a.\+red\+Invm()} -\/ modular inverse of the number
\item {\ttfamily a.\+red\+Neg()}
\item {\ttfamily a.\+red\+Pow(b)} -\/ modular exponentiation
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4016}{}\doxysubsection{Number Size}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4016}
Optimized for elliptic curves that work with 256-\/bit numbers. There is no limitation on the size of the numbers.\hypertarget{md_sfcode_node_modules_bn_js_README_autotoc_md4017}{}\doxysection{LICENSE}\label{md_sfcode_node_modules_bn_js_README_autotoc_md4017}
This software is licensed under the MIT License.

Copyright Fedor Indutny, 2015.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \char`\"{}\+Software\char`\"{}), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions\+:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED \char`\"{}\+AS IS\char`\"{}, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 