⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bn.tex

📁 tommath库
💻 TEX
📖 第 1 页 / 共 5 页
字号:
\documentclass[b5paper]{book}\usepackage{hyperref}\usepackage{makeidx}\usepackage{amssymb}\usepackage{color}\usepackage{alltt}\usepackage{graphicx}\usepackage{layout}\def\union{\cup}\def\intersect{\cap}\def\getsrandom{\stackrel{\rm R}{\gets}}\def\cross{\times}\def\cat{\hspace{0.5em} \| \hspace{0.5em}}\def\catn{$\|$}\def\divides{\hspace{0.3em} | \hspace{0.3em}}\def\nequiv{\not\equiv}\def\approx{\raisebox{0.2ex}{\mbox{\small $\sim$}}}\def\lcm{{\rm lcm}}\def\gcd{{\rm gcd}}\def\log{{\rm log}}\def\ord{{\rm ord}}\def\abs{{\mathit abs}}\def\rep{{\mathit rep}}\def\mod{{\mathit\ mod\ }}\renewcommand{\pmod}[1]{\ ({\rm mod\ }{#1})}\newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor}\newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil}\def\Or{{\rm\ or\ }}\def\And{{\rm\ and\ }}\def\iff{\hspace{1em}\Longleftrightarrow\hspace{1em}}\def\implies{\Rightarrow}\def\undefined{{\rm ``undefined"}}\def\Proof{\vspace{1ex}\noindent {\bf Proof:}\hspace{1em}}\let\oldphi\phi\def\phi{\varphi}\def\Pr{{\rm Pr}}\newcommand{\str}[1]{{\mathbf{#1}}}\def\F{{\mathbb F}}\def\N{{\mathbb N}}\def\Z{{\mathbb Z}}\def\R{{\mathbb R}}\def\C{{\mathbb C}}\def\Q{{\mathbb Q}}\definecolor{DGray}{gray}{0.5}\newcommand{\emailaddr}[1]{\mbox{$<${#1}$>$}}\def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}}\def\gap{\vspace{0.5ex}}\makeindex\begin{document}\frontmatter\pagestyle{empty}\title{LibTomMath User Manual \\ v0.33}\author{Tom St Denis \\ tomstdenis@iahu.ca}\maketitleThis text, the library and the accompanying textbook are all hereby placed in the public domain.  This book has been formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.\vspace{10cm}\begin{flushright}Open Source.  Open Academia.  Open Minds.\mbox{ }Tom St Denis,Ontario, Canada\end{flushright}\tableofcontents\listoffigures\mainmatter\pagestyle{headings}\chapter{Introduction}\section{What is LibTomMath?}LibTomMath is a library of source code which provides a series of efficient and carefully written functions for manipulatinglarge integer numbers.  It was written in portable ISO C source code so that it will build on any platform with a conformingC compiler.  In a nutshell the library was written from scratch with verbose comments to help instruct computer science students howto implement ``bignum'' math.  However, the resulting code has proven to be very useful.  It has been used by numerous universities, commercial and open source software developers.  It has been used on a variety of platforms ranging fromLinux and Windows based x86 to ARM based Gameboys and PPC based MacOS machines.  \section{License}As of the v0.25 the library source code has been placed in the public domain with every new release.  As of the v0.28release the textbook ``Implementing Multiple Precision Arithmetic'' has been placed in the public domain with every newrelease as well.  This textbook is meant to compliment the project by providing a more solid walkthrough of the developmentalgorithms used in the library.Since both\footnote{Note that the MPI files under mtest/ are copyrighted by Michael Fromberger.  They are not required to use LibTomMath.} are in the public domain everyone is entitled to do with them as they see fit.\section{Building LibTomMath}LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well suited for GCC.  However, the library willalso build in MSVC, Borland C out of the box.  For any other ISO C compiler a makefile will have to be made by the enddeveloper.  \subsection{Static Libraries}To build as a static library for GCC issue the following\begin{alltt}make\end{alltt}command.  This will build the library and archive the object files in ``libtommath.a''.  Now you link against that and include ``tommath.h'' within your programs.  Alternatively to build with MSVC issue the following\begin{alltt}nmake -f makefile.msvc\end{alltt}This will build the library and archive the object files in ``tommath.lib''.  This has been tested with MSVC version 6.00 with service pack 5.  \subsection{Shared Libraries}To build as a shared library for GCC issue the following\begin{alltt}make -f makefile.shared\end{alltt}This requires the ``libtool'' package (common on most Linux/BSD systems).  It will build LibTomMath as both sharedand static then install (by default) into /usr/lib as well as install the header files in /usr/include.  The shared library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''.  Generally you use libtool to link your application against the shared object.  There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile.  It requires Cygwin to work with since it requires the auto-export/import functionality.  The resulting DLL and import library ``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin.\subsection{Testing}To build the library and the test harness type\begin{alltt}make test\end{alltt}This will build the library, ``test'' and ``mtest/mtest''.  The ``test'' program will accept test vectors and verify theresults.  ``mtest/mtest'' will generate test vectors using the MPI library by Michael Fromberger\footnote{A copy of MPIis included in the package}.  Simply pipe mtest into test using\begin{alltt}mtest/mtest | test\end{alltt}If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into mtest.  For example, if your PRNG program is called ``myprng'' simply invoke\begin{alltt}myprng | mtest/mtest | test\end{alltt}This will output a row of numbers that are increasing.  Each column is a different test (such as addition, multiplication, etc)that is being performed.  The numbers represent how many times the test was invoked.  If an error is detected the programwill exit with a dump of the relevent numbers it was working with.\section{Build Configuration}LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''.  Each phase changes how the library is built and they are applied one after another respectively.  To make the system more powerful you can tweak the build process.  Classes are defined in the file``tommath\_superclass.h''.  By default, the symbol ``LTM\_ALL'' shall be defined which simply instructs the system to build all of the functions.  This is how LibTomMath used to be packaged.  This will give you access to every function LibTomMath offers.However, there are cases where such a build is not optional.  For instance, you want to perform RSA operations.  You don't need the vast majority of the library to perform these operations.  Aside from LTM\_ALL there is another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt.  Additional classes can be defined base on the need of the user.\subsection{Build Depends}In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs''which further define symbols.  All of the symbols (technically they're macros $\ldots$) represent a given C sourcefile.  For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''.  When a define has been enabled thefunction in the respective file will be compiled and linked into the library.  Accordingly when the defineis absent the file will not be compiled and not contribute any size to the library.You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice).  This is to help resolve as many dependencies as possible.  In the last pass the symbol LTM\_LAST will be defined.  This is useful for ``trims''.\subsection{Build Tweaks}A tweak is an algorithm ``alternative''.  For example, to provide tradeoffs (usually between size and space).They can be enabled at any pass of the configuration phase.\begin{small}\begin{center}\begin{tabular}{|l|l|}\hline \textbf{Define} & \textbf{Purpose} \\\hline BN\_MP\_DIV\_SMALL & Enables a slower, smaller and equally \\                          & functional mp\_div() function \\\hline\end{tabular}\end{center}\end{small}\subsection{Build Trims}A trim is a manner of removing functionality from a function that is not required.  For instance, to performRSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed.  Build trims are meant to be defined on the last pass of the configuration which means they are to be definedonly if LTM\_LAST has been defined.\subsubsection{Moduli Related}\begin{small}\begin{center}\begin{tabular}{|l|l|}\hline \textbf{Restriction} & \textbf{Undefine} \\\hline Exponentiation with odd moduli only & BN\_S\_MP\_EXPTMOD\_C \\                                           & BN\_MP\_REDUCE\_C \\                                           & BN\_MP\_REDUCE\_SETUP\_C \\                                           & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\                                           & BN\_FAST\_S\_MP\_MUL\_HIGH\_DIGS\_C \\\hline Exponentiation with random odd moduli & (The above plus the following) \\                                           & BN\_MP\_REDUCE\_2K\_C \\                                           & BN\_MP\_REDUCE\_2K\_SETUP\_C \\                                           & BN\_MP\_REDUCE\_IS\_2K\_C \\                                           & BN\_MP\_DR\_IS\_MODULUS\_C \\                                           & BN\_MP\_DR\_REDUCE\_C \\                                           & BN\_MP\_DR\_SETUP\_C \\\hline Modular inverse odd moduli only     & BN\_MP\_INVMOD\_SLOW\_C \\\hline Modular inverse (both, smaller/slower) & BN\_FAST\_MP\_INVMOD\_C \\\hline\end{tabular}\end{center}\end{small}\subsubsection{Operand Size Related}\begin{small}\begin{center}\begin{tabular}{|l|l|}\hline \textbf{Restriction} & \textbf{Undefine} \\\hline Moduli $\le 2560$ bits              & BN\_MP\_MONTGOMERY\_REDUCE\_C \\                                           & BN\_S\_MP\_MUL\_DIGS\_C \\                                           & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\                                           & BN\_S\_MP\_SQR\_C \\\hline Polynomial Schmolynomial            & BN\_MP\_KARATSUBA\_MUL\_C \\                                           & BN\_MP\_KARATSUBA\_SQR\_C \\                                           & BN\_MP\_TOOM\_MUL\_C \\                                            & BN\_MP\_TOOM\_SQR\_C \\\hline\end{tabular}\end{center}\end{small}\section{Purpose of LibTomMath}Unlike  GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with bleeding edge performance in mind.  First and foremost LibTomMath was written to be entirely open.  Not only is the source code public domain (unlike various other GPL/etc licensed code), not only is the code freely downloadable but thesource code is also accessible for computer science students attempting to learn ``BigNum'' or multiple precisionarithmetic techniques. LibTomMath was written to be an instructive collection of source code.  This is why there are many comments, only onefunction per source file and often I use a ``middle-road'' approach where I don't cut corners for an extra 2\% speedincrease.Source code alone cannot really teach how the algorithms work which is why I also wrote a textbook that accompaniesthe library (beat that!).So you may be thinking ``should I use LibTomMath?'' and the answer is a definite maybe.  Let me tabulate what I thinkare the pros and cons of LibTomMath by comparing it to the math routines from GnuPG\footnote{GnuPG v1.2.3 versus LibTomMath v0.28}.\newpage\begin{figure}[here]\begin{small}\begin{center}\begin{tabular}{|l|c|c|l|}\hline \textbf{Criteria} & \textbf{Pro} & \textbf{Con} & \textbf{Notes} \\\hline Few lines of code per file & X & & GnuPG $ = 300.9$, LibTomMath  $ = 76.04$ \\\hline Commented function prototypes & X && GnuPG function names are cryptic. \\\hline Speed && X & LibTomMath is slower.  \\\hline Totally free & X & & GPL has unfavourable restrictions.\\\hline Large function base & X & & GnuPG is barebones. \\\hline Four modular reduction algorithms & X & & Faster modular exponentiation. \\\hline Portable & X & & GnuPG requires configuration to build. \\\hline\end{tabular}\end{center}\end{small}\caption{LibTomMath Valuation}\end{figure}It may seem odd to compare LibTomMath to GnuPG since the math in GnuPG is only a small portion of the entire application. However, LibTomMath was written with cryptography in mind.  It provides essentially all of the functions a cryptosystemwould require when working with large integers.  So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in yourown application but I think there are reasons not to.  While LibTomMath is slower than libraries such as GnuMP it isnot normally significantly slower.  On x86 machines the difference is normally a factor of two when performing modularexponentiations.Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern.\chapter{Getting Started with LibTomMath}\section{Building Programs}In order to use LibTomMath you must include ``tommath.h'' and link against the appropriate library file (typically libtommath.a).  There is no library initialization required and the entire library is thread safe.\section{Return Codes}There are three possible return codes a function may return.\index{MP\_OKAY}\index{MP\_YES}\index{MP\_NO}\index{MP\_VAL}\index{MP\_MEM}\begin{figure}[here!]\begin{center}\begin{small}\begin{tabular}{|l|l|}\hline \textbf{Code} & \textbf{Meaning} \\\hline MP\_OKAY & The function succeeded. \\\hline MP\_VAL  & The function input was invalid. \\\hline MP\_MEM  & Heap memory exhausted. \\\hline &\\\hline MP\_YES  & Response is yes. \\\hline MP\_NO   & Response is no. \\\hline\end{tabular}\end{small}\end{center}\caption{Return Codes}\end{figure}The last two codes listed are not actually ``return'ed'' by a function.  They are placed in an integer (the caller mustprovide the address of an integer it can store to) which the caller can access.  To convert one of the three return codesto a string use the following function.\index{mp\_error\_to\_string}\begin{alltt}char *mp_error_to_string(int code);\end{alltt}This will return a pointer to a string which describes the given error code.  It will not work for the return codes MP\_YES and MP\_NO.  \section{Data Types}The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath.  This data type is used toorganize all of the data required to manipulate the integer it represents.  Within LibTomMath it has been prototypedas the following.\index{mp\_int}\begin{alltt}typedef struct  \{    int used, alloc, sign;    mp_digit *dp;\} mp_int;\end{alltt}Where ``mp\_digit'' is a data type that represents individual digits of the integer.  By default, an mp\_digit is theISO C ``unsigned long'' data type and each digit is $28-$bits long.  The mp\_digit type can be configured to suit otherplatforms by defining the appropriate macros.  All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure.  You must allocate memory tohold the structure itself by yourself (whether off stack or heap it doesn't matter).  The very first thing that must bedone to use an mp\_int is that it must be initialized.\section{Function Organization}The arithmetic functions of the library are all organized to have the same style prototype.  That is source operandsare passed on the left and the destination is on the right.  For instance,\begin{alltt}mp_add(&a, &b, &c);       /* c = a + b */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -