annotate bn.tex @ 145:a96ff234ff19 libtommath

propagate of fc94c38452d9fd684a8e1eb9e3a73120aac0d38f and 3e4de4cbef3d9035a7b2f0c25e9f86e297f9f6d1 from branch 'au.asn.ucc.matt.ltm-orig' to 'au.asn.ucc.matt.ltm-db'
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 15:57:19 +0000
parents d29b64170cf0
children d8254fc979e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 \documentclass[b5paper]{book}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 \usepackage{hyperref}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 \usepackage{makeidx}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 \usepackage{amssymb}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 \usepackage{color}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 \usepackage{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 \usepackage{graphicx}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 \usepackage{layout}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 \def\union{\cup}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 \def\intersect{\cap}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 \def\getsrandom{\stackrel{\rm R}{\gets}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 \def\cross{\times}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 \def\cat{\hspace{0.5em} \| \hspace{0.5em}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 \def\catn{$\|$}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 \def\divides{\hspace{0.3em} | \hspace{0.3em}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 \def\nequiv{\not\equiv}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 \def\approx{\raisebox{0.2ex}{\mbox{\small $\sim$}}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 \def\lcm{{\rm lcm}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 \def\gcd{{\rm gcd}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 \def\log{{\rm log}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 \def\ord{{\rm ord}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 \def\abs{{\mathit abs}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 \def\rep{{\mathit rep}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 \def\mod{{\mathit\ mod\ }}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 \renewcommand{\pmod}[1]{\ ({\rm mod\ }{#1})}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 \newcommand{\floor}[1]{\left\lfloor{#1}\right\rfloor}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 \newcommand{\ceil}[1]{\left\lceil{#1}\right\rceil}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 \def\Or{{\rm\ or\ }}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 \def\And{{\rm\ and\ }}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 \def\iff{\hspace{1em}\Longleftrightarrow\hspace{1em}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 \def\implies{\Rightarrow}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 \def\undefined{{\rm ``undefined"}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 \def\Proof{\vspace{1ex}\noindent {\bf Proof:}\hspace{1em}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 \let\oldphi\phi
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 \def\phi{\varphi}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 \def\Pr{{\rm Pr}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 \newcommand{\str}[1]{{\mathbf{#1}}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 \def\F{{\mathbb F}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 \def\N{{\mathbb N}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 \def\Z{{\mathbb Z}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 \def\R{{\mathbb R}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 \def\C{{\mathbb C}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 \def\Q{{\mathbb Q}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 \definecolor{DGray}{gray}{0.5}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 \newcommand{\emailaddr}[1]{\mbox{$<${#1}$>$}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 \def\twiddle{\raisebox{0.3ex}{\mbox{\tiny $\sim$}}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 \def\gap{\vspace{0.5ex}}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 \makeindex
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 \begin{document}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 \frontmatter
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 \pagestyle{empty}
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
52 \title{LibTomMath User Manual \\ v0.32}
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 \author{Tom St Denis \\ [email protected]}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 \maketitle
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 formatted for B5 [176x250] paper using the \LaTeX{} {\em book} macro package.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 \vspace{10cm}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 \begin{flushright}Open Source. Open Academia. Open Minds.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 \mbox{ }
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 Tom St Denis,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 Ontario, Canada
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 \end{flushright}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 \tableofcontents
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 \listoffigures
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 \mainmatter
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 \pagestyle{headings}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 \chapter{Introduction}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 \section{What is LibTomMath?}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 LibTomMath is a library of source code which provides a series of efficient and carefully written functions for manipulating
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 large integer numbers. It was written in portable ISO C source code so that it will build on any platform with a conforming
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 C compiler.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 In a nutshell the library was written from scratch with verbose comments to help instruct computer science students how
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 to implement ``bignum'' math. However, the resulting code has proven to be very useful. It has been used by numerous
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 universities, commercial and open source software developers. It has been used on a variety of platforms ranging from
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 Linux and Windows based x86 to ARM based Gameboys and PPC based MacOS machines.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 \section{License}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 As of the v0.25 the library source code has been placed in the public domain with every new release. As of the v0.28
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 release the textbook ``Implementing Multiple Precision Arithmetic'' has been placed in the public domain with every new
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 release as well. This textbook is meant to compliment the project by providing a more solid walkthrough of the development
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 algorithms used in the library.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 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
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 public domain everyone is entitled to do with them as they see fit.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 \section{Building LibTomMath}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 LibTomMath is meant to be very ``GCC friendly'' as it comes with a makefile well suited for GCC. However, the library will
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 also build in MSVC, Borland C out of the box. For any other ISO C compiler a makefile will have to be made by the end
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 developer.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
99 \subsection{Static Libraries}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
100 To build as a static library for GCC issue the following
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 make
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
105 command. This will build the library and archive the object files in ``libtommath.a''. Now you link against
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
106 that and include ``tommath.h'' within your programs. Alternatively to build with MSVC issue the following
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 nmake -f makefile.msvc
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
111 This will build the library and archive the object files in ``tommath.lib''. This has been tested with MSVC
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
112 version 6.00 with service pack 5.
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
114 \subsection{Shared Libraries}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
115 To build as a shared library for GCC issue the following
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
116 \begin{alltt}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
117 make -f makefile.shared
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
118 \end{alltt}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
119 This requires the ``libtool'' package (common on most Linux/BSD systems). It will build LibTomMath as both shared
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
120 and static then install (by default) into /usr/lib as well as install the header files in /usr/include. The shared
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
121 library (resource) will be called ``libtommath.la'' while the static library called ``libtommath.a''. Generally
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
122 you use libtool to link your application against the shared object.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
123
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
124 There is limited support for making a ``DLL'' in windows via the ``makefile.cygwin\_dll'' makefile. It requires
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
125 Cygwin to work with since it requires the auto-export/import functionality. The resulting DLL and import library
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
126 ``libtommath.dll.a'' can be used to link LibTomMath dynamically to any Windows program using Cygwin.
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 \subsection{Testing}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 To build the library and the test harness type
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 make test
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 This will build the library, ``test'' and ``mtest/mtest''. The ``test'' program will accept test vectors and verify the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 results. ``mtest/mtest'' will generate test vectors using the MPI library by Michael Fromberger\footnote{A copy of MPI
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 is included in the package}. Simply pipe mtest into test using
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 mtest/mtest | test
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 If you do not have a ``/dev/urandom'' style RNG source you will have to write your own PRNG and simply pipe that into
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 mtest. For example, if your PRNG program is called ``myprng'' simply invoke
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 myprng | mtest/mtest | test
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 This will output a row of numbers that are increasing. Each column is a different test (such as addition, multiplication, etc)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 that is being performed. The numbers represent how many times the test was invoked. If an error is detected the program
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 will exit with a dump of the relevent numbers it was working with.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153
142
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
154 \section{Build Configuration}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
155 LibTomMath can configured at build time in three phases we shall call ``depends'', ``tweaks'' and ``trims''.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
156 Each phase changes how the library is built and they are applied one after another respectively.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
157
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
158 To make the system more powerful you can tweak the build process. Classes are defined in the file
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
159 ``tommath\_superclass.h''. By default, the symbol ``LTM\_ALL'' shall be defined which simply
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
160 instructs the system to build all of the functions. This is how LibTomMath used to be packaged. This will give you
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
161 access to every function LibTomMath offers.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
162
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
163 However, there are cases where such a build is not optional. For instance, you want to perform RSA operations. You
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
164 don't need the vast majority of the library to perform these operations. Aside from LTM\_ALL there is
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
165 another pre--defined class ``SC\_RSA\_1'' which works in conjunction with the RSA from LibTomCrypt. Additional
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
166 classes can be defined base on the need of the user.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
167
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
168 \subsection{Build Depends}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
169 In the file tommath\_class.h you will see a large list of C ``defines'' followed by a series of ``ifdefs''
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
170 which further define symbols. All of the symbols (technically they're macros $\ldots$) represent a given C source
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
171 file. For instance, BN\_MP\_ADD\_C represents the file ``bn\_mp\_add.c''. When a define has been enabled the
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
172 function in the respective file will be compiled and linked into the library. Accordingly when the define
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
173 is absent the file will not be compiled and not contribute any size to the library.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
174
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
175 You will also note that the header tommath\_class.h is actually recursively included (it includes itself twice).
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
176 This is to help resolve as many dependencies as possible. In the last pass the symbol LTM\_LAST will be defined.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
177 This is useful for ``trims''.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
178
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
179 \subsection{Build Tweaks}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
180 A tweak is an algorithm ``alternative''. For example, to provide tradeoffs (usually between size and space).
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
181 They can be enabled at any pass of the configuration phase.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
182
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
183 \begin{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
184 \begin{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
185 \begin{tabular}{|l|l|}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
186 \hline \textbf{Define} & \textbf{Purpose} \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
187 \hline BN\_MP\_DIV\_SMALL & Enables a slower, smaller and equally \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
188 & functional mp\_div() function \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
189 \hline
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
190 \end{tabular}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
191 \end{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
192 \end{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
193
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
194 \subsection{Build Trims}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
195 A trim is a manner of removing functionality from a function that is not required. For instance, to perform
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
196 RSA cryptography you only require exponentiation with odd moduli so even moduli support can be safely removed.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
197 Build trims are meant to be defined on the last pass of the configuration which means they are to be defined
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
198 only if LTM\_LAST has been defined.
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
199
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
200 \subsubsection{Moduli Related}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
201 \begin{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
202 \begin{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
203 \begin{tabular}{|l|l|}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
204 \hline \textbf{Restriction} & \textbf{Undefine} \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
205 \hline Exponentiation with odd moduli only & BN\_S\_MP\_EXPTMOD\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
206 & BN\_MP\_REDUCE\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
207 & BN\_MP\_REDUCE\_SETUP\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
208 & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
209 & BN\_FAST\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
210 \hline Exponentiation with random odd moduli & (The above plus the following) \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
211 & BN\_MP\_REDUCE\_2K\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
212 & BN\_MP\_REDUCE\_2K\_SETUP\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
213 & BN\_MP\_REDUCE\_IS\_2K\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
214 & BN\_MP\_DR\_IS\_MODULUS\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
215 & BN\_MP\_DR\_REDUCE\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
216 & BN\_MP\_DR\_SETUP\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
217 \hline Modular inverse odd moduli only & BN\_MP\_INVMOD\_SLOW\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
218 \hline Modular inverse (both, smaller/slower) & BN\_FAST\_MP\_INVMOD\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
219 \hline
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
220 \end{tabular}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
221 \end{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
222 \end{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
223
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
224 \subsubsection{Operand Size Related}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
225 \begin{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
226 \begin{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
227 \begin{tabular}{|l|l|}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
228 \hline \textbf{Restriction} & \textbf{Undefine} \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
229 \hline Moduli $\le 2560$ bits & BN\_MP\_MONTGOMERY\_REDUCE\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
230 & BN\_S\_MP\_MUL\_DIGS\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
231 & BN\_S\_MP\_MUL\_HIGH\_DIGS\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
232 & BN\_S\_MP\_SQR\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
233 \hline Polynomial Schmolynomial & BN\_MP\_KARATSUBA\_MUL\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
234 & BN\_MP\_KARATSUBA\_SQR\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
235 & BN\_MP\_TOOM\_MUL\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
236 & BN\_MP\_TOOM\_SQR\_C \\
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
237
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
238 \hline
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
239 \end{tabular}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
240 \end{center}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
241 \end{small}
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
242
d29b64170cf0 import of libtommath 0.32
Matt Johnston <matt@ucc.asn.au>
parents: 19
diff changeset
243
19
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 \section{Purpose of LibTomMath}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245 Unlike GNU MP (GMP) Library, LIP, OpenSSL or various other commercial kits (Miracl), LibTomMath was not written with
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
246 bleeding edge performance in mind. First and foremost LibTomMath was written to be entirely open. Not only is the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
247 source code public domain (unlike various other GPL/etc licensed code), not only is the code freely downloadable but the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
248 source code is also accessible for computer science students attempting to learn ``BigNum'' or multiple precision
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
249 arithmetic techniques.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
250
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 LibTomMath was written to be an instructive collection of source code. This is why there are many comments, only one
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 function per source file and often I use a ``middle-road'' approach where I don't cut corners for an extra 2\% speed
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253 increase.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 Source code alone cannot really teach how the algorithms work which is why I also wrote a textbook that accompanies
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 the library (beat that!).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
257
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
258 So you may be thinking ``should I use LibTomMath?'' and the answer is a definite maybe. Let me tabulate what I think
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 are the pros and cons of LibTomMath by comparing it to the math routines from GnuPG\footnote{GnuPG v1.2.3 versus LibTomMath v0.28}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261 \newpage\begin{figure}[here]
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 \begin{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 \begin{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 \begin{tabular}{|l|c|c|l|}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 \hline \textbf{Criteria} & \textbf{Pro} & \textbf{Con} & \textbf{Notes} \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 \hline Few lines of code per file & X & & GnuPG $ = 300.9$, LibTomMath $ = 76.04$ \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267 \hline Commented function prototypes & X && GnuPG function names are cryptic. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 \hline Speed && X & LibTomMath is slower. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 \hline Totally free & X & & GPL has unfavourable restrictions.\\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 \hline Large function base & X & & GnuPG is barebones. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 \hline Four modular reduction algorithms & X & & Faster modular exponentiation. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272 \hline Portable & X & & GnuPG requires configuration to build. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 \hline
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 \end{tabular}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 \end{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 \caption{LibTomMath Valuation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 \end{figure}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
280 It may seem odd to compare LibTomMath to GnuPG since the math in GnuPG is only a small portion of the entire application.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 However, LibTomMath was written with cryptography in mind. It provides essentially all of the functions a cryptosystem
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 would require when working with large integers.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 So it may feel tempting to just rip the math code out of GnuPG (or GnuMP where it was taken from originally) in your
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 own application but I think there are reasons not to. While LibTomMath is slower than libraries such as GnuMP it is
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 not normally significantly slower. On x86 machines the difference is normally a factor of two when performing modular
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287 exponentiations.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 Essentially the only time you wouldn't use LibTomMath is when blazing speed is the primary concern.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291 \chapter{Getting Started with LibTomMath}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 \section{Building Programs}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 In order to use LibTomMath you must include ``tommath.h'' and link against the appropriate library file (typically
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 libtommath.a). There is no library initialization required and the entire library is thread safe.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 \section{Return Codes}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297 There are three possible return codes a function may return.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 \index{MP\_OKAY}\index{MP\_YES}\index{MP\_NO}\index{MP\_VAL}\index{MP\_MEM}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 \begin{figure}[here!]
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 \begin{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302 \begin{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 \begin{tabular}{|l|l|}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 \hline \textbf{Code} & \textbf{Meaning} \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 \hline MP\_OKAY & The function succeeded. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 \hline MP\_VAL & The function input was invalid. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 \hline MP\_MEM & Heap memory exhausted. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 \hline &\\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 \hline MP\_YES & Response is yes. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
310 \hline MP\_NO & Response is no. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
311 \hline
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
312 \end{tabular}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
313 \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
314 \end{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
315 \caption{Return Codes}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
316 \end{figure}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
317
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
318 The last two codes listed are not actually ``return'ed'' by a function. They are placed in an integer (the caller must
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
319 provide the address of an integer it can store to) which the caller can access. To convert one of the three return codes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
320 to a string use the following function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
321
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
322 \index{mp\_error\_to\_string}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
323 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
324 char *mp_error_to_string(int code);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
325 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
326
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
327 This will return a pointer to a string which describes the given error code. It will not work for the return codes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
328 MP\_YES and MP\_NO.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
329
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
330 \section{Data Types}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
331 The basic ``multiple precision integer'' type is known as the ``mp\_int'' within LibTomMath. This data type is used to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
332 organize all of the data required to manipulate the integer it represents. Within LibTomMath it has been prototyped
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
333 as the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
334
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
335 \index{mp\_int}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
336 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
337 typedef struct \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
338 int used, alloc, sign;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
339 mp_digit *dp;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
340 \} mp_int;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
341 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
342
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
343 Where ``mp\_digit'' is a data type that represents individual digits of the integer. By default, an mp\_digit is the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
344 ISO C ``unsigned long'' data type and each digit is $28-$bits long. The mp\_digit type can be configured to suit other
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
345 platforms by defining the appropriate macros.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
346
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
347 All LTM functions that use the mp\_int type will expect a pointer to mp\_int structure. You must allocate memory to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
348 hold the structure itself by yourself (whether off stack or heap it doesn't matter). The very first thing that must be
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
349 done to use an mp\_int is that it must be initialized.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
350
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
351 \section{Function Organization}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
352
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
353 The arithmetic functions of the library are all organized to have the same style prototype. That is source operands
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
354 are passed on the left and the destination is on the right. For instance,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
355
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
356 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
357 mp_add(&a, &b, &c); /* c = a + b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
358 mp_mul(&a, &a, &c); /* c = a * a */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
359 mp_div(&a, &b, &c, &d); /* c = [a/b], d = a mod b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
360 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
361
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
362 Another feature of the way the functions have been implemented is that source operands can be destination operands as well.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
363 For instance,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
364
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
365 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
366 mp_add(&a, &b, &b); /* b = a + b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
367 mp_div(&a, &b, &a, &c); /* a = [a/b], c = a mod b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
368 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
369
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
370 This allows operands to be re-used which can make programming simpler.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
371
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
372 \section{Initialization}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
373 \subsection{Single Initialization}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
374 A single mp\_int can be initialized with the ``mp\_init'' function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
375
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
376 \index{mp\_init}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
377 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
378 int mp_init (mp_int * a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
379 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
380
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
381 This function expects a pointer to an mp\_int structure and will initialize the members of the structure so the mp\_int
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
382 represents the default integer which is zero. If the functions returns MP\_OKAY then the mp\_int is ready to be used
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
383 by the other LibTomMath functions.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
384
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
385 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
386 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
387 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
388 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
389 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
390
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
391 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
392 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
393 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
394 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
395 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
396
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
397 /* use the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
398
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
399 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
400 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
401 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
402
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
403 \subsection{Single Free}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
404 When you are finished with an mp\_int it is ideal to return the heap it used back to the system. The following function
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
405 provides this functionality.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
406
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
407 \index{mp\_clear}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
408 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
409 void mp_clear (mp_int * a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
410 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
411
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
412 The function expects a pointer to a previously initialized mp\_int structure and frees the heap it uses. It sets the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
413 pointer\footnote{The ``dp'' member.} within the mp\_int to \textbf{NULL} which is used to prevent double free situations.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
414 Is is legal to call mp\_clear() twice on the same mp\_int in a row.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
415
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
416 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
417 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
418 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
419 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
420 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
421
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
422 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
423 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
424 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
425 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
426 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
427
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
428 /* use the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
429
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
430 /* We're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
431 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
432
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
433 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
434 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
435 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
436
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
437 \subsection{Multiple Initializations}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
438 Certain algorithms require more than one large integer. In these instances it is ideal to initialize all of the mp\_int
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
439 variables in an ``all or nothing'' fashion. That is, they are either all initialized successfully or they are all
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
440 not initialized.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
441
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
442 The mp\_init\_multi() function provides this functionality.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
443
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
444 \index{mp\_init\_multi} \index{mp\_clear\_multi}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
445 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
446 int mp_init_multi(mp_int *mp, ...);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
447 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
448
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
449 It accepts a \textbf{NULL} terminated list of pointers to mp\_int structures. It will attempt to initialize them all
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
450 at once. If the function returns MP\_OKAY then all of the mp\_int variables are ready to use, otherwise none of them
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
451 are available for use. A complementary mp\_clear\_multi() function allows multiple mp\_int variables to be free'd
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
452 from the heap at the same time.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
453
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
454 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
455 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
456 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
457 mp_int num1, num2, num3;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
458 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
459
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
460 if ((result = mp_init_multi(&num1,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
461 &num2,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
462 &num3, NULL)) != MP\_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
463 printf("Error initializing the numbers. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
464 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
465 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
466 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
467
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
468 /* use the numbers */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
469
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
470 /* We're done with them. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
471 mp_clear_multi(&num1, &num2, &num3, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
472
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
473 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
474 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
475 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
476
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
477 \subsection{Other Initializers}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
478 To initialized and make a copy of an mp\_int the mp\_init\_copy() function has been provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
479
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
480 \index{mp\_init\_copy}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
481 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
482 int mp_init_copy (mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
483 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
484
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
485 This function will initialize $a$ and make it a copy of $b$ if all goes well.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
486
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
487 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
488 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
489 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
490 mp_int num1, num2;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
491 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
492
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
493 /* initialize and do work on num1 ... */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
494
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
495 /* We want a copy of num1 in num2 now */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
496 if ((result = mp_init_copy(&num2, &num1)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
497 printf("Error initializing the copy. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
498 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
499 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
500 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
501
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
502 /* now num2 is ready and contains a copy of num1 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
503
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
504 /* We're done with them. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
505 mp_clear_multi(&num1, &num2, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
506
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
507 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
508 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
509 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
510
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
511 Another less common initializer is mp\_init\_size() which allows the user to initialize an mp\_int with a given
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
512 default number of digits. By default, all initializers allocate \textbf{MP\_PREC} digits. This function lets
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
513 you override this behaviour.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
514
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
515 \index{mp\_init\_size}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
516 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
517 int mp_init_size (mp_int * a, int size);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
518 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
519
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
520 The $size$ parameter must be greater than zero. If the function succeeds the mp\_int $a$ will be initialized
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
521 to have $size$ digits (which are all initially zero).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
522
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
523 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
524 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
525 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
526 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
527 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
528
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
529 /* we need a 60-digit number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
530 if ((result = mp_init_size(&number, 60)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
531 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
532 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
533 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
534 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
535
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
536 /* use the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
537
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
538 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
539 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
540 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
541
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
542 \section{Maintenance Functions}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
543
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
544 \subsection{Reducing Memory Usage}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
545 When an mp\_int is in a state where it won't be changed again\footnote{A Diffie-Hellman modulus for instance.} excess
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
546 digits can be removed to return memory to the heap with the mp\_shrink() function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
547
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
548 \index{mp\_shrink}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
549 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
550 int mp_shrink (mp_int * a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
551 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
552
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
553 This will remove excess digits of the mp\_int $a$. If the operation fails the mp\_int should be intact without the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
554 excess digits being removed. Note that you can use a shrunk mp\_int in further computations, however, such operations
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
555 will require heap operations which can be slow. It is not ideal to shrink mp\_int variables that you will further
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
556 modify in the system (unless you are seriously low on memory).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
557
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
558 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
559 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
560 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
561 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
562 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
563
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
564 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
565 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
566 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
567 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
568 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
569
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
570 /* use the number [e.g. pre-computation] */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
571
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
572 /* We're done with it for now. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
573 if ((result = mp_shrink(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
574 printf("Error shrinking the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
575 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
576 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
577 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
578
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
579 /* use it .... */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
580
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
581
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
582 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
583 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
584
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
585 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
586 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
587 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
588
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
589 \subsection{Adding additional digits}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
590
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
591 Within the mp\_int structure are two parameters which control the limitations of the array of digits that represent
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
592 the integer the mp\_int is meant to equal. The \textit{used} parameter dictates how many digits are significant, that is,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
593 contribute to the value of the mp\_int. The \textit{alloc} parameter dictates how many digits are currently available in
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
594 the array. If you need to perform an operation that requires more digits you will have to mp\_grow() the mp\_int to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
595 your desired size.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
596
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
597 \index{mp\_grow}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
598 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
599 int mp_grow (mp_int * a, int size);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
600 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
601
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
602 This will grow the array of digits of $a$ to $size$. If the \textit{alloc} parameter is already bigger than
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
603 $size$ the function will not do anything.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
604
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
605 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
606 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
607 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
608 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
609 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
610
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
611 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
612 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
613 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
614 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
615 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
616
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
617 /* use the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
618
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
619 /* We need to add 20 digits to the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
620 if ((result = mp_grow(&number, number.alloc + 20)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
621 printf("Error growing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
622 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
623 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
624 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
625
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
626
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
627 /* use the number */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
628
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
629 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
630 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
631
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
632 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
633 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
634 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
635
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
636 \chapter{Basic Operations}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
637 \section{Small Constants}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
638 Setting mp\_ints to small constants is a relatively common operation. To accomodate these instances there are two
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
639 small constant assignment functions. The first function is used to set a single digit constant while the second sets
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
640 an ISO C style ``unsigned long'' constant. The reason for both functions is efficiency. Setting a single digit is quick but the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
641 domain of a digit can change (it's always at least $0 \ldots 127$).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
642
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
643 \subsection{Single Digit}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
644
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
645 Setting a single digit can be accomplished with the following function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
646
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
647 \index{mp\_set}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
648 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
649 void mp_set (mp_int * a, mp_digit b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
650 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
651
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
652 This will zero the contents of $a$ and make it represent an integer equal to the value of $b$. Note that this
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
653 function has a return type of \textbf{void}. It cannot cause an error so it is safe to assume the function
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
654 succeeded.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
655
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
656 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
657 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
658 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
659 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
660 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
661
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
662 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
663 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
664 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
665 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
666 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
667
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
668 /* set the number to 5 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
669 mp_set(&number, 5);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
670
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
671 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
672 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
673
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
674 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
675 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
676 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
677
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
678 \subsection{Long Constants}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
679
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
680 To set a constant that is the size of an ISO C ``unsigned long'' and larger than a single digit the following function
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
681 can be used.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
682
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
683 \index{mp\_set\_int}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
684 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
685 int mp_set_int (mp_int * a, unsigned long b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
686 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
687
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
688 This will assign the value of the 32-bit variable $b$ to the mp\_int $a$. Unlike mp\_set() this function will always
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
689 accept a 32-bit input regardless of the size of a single digit. However, since the value may span several digits
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
690 this function can fail if it runs out of heap memory.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
691
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
692 To get the ``unsigned long'' copy of an mp\_int the following function can be used.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
693
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
694 \index{mp\_get\_int}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
695 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
696 unsigned long mp_get_int (mp_int * a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
697 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
698
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
699 This will return the 32 least significant bits of the mp\_int $a$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
700
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
701 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
702 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
703 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
704 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
705 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
706
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
707 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
708 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
709 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
710 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
711 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
712
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
713 /* set the number to 654321 (note this is bigger than 127) */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
714 if ((result = mp_set_int(&number, 654321)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
715 printf("Error setting the value of the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
716 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
717 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
718 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
719
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
720 printf("number == \%lu", mp_get_int(&number));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
721
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
722 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
723 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
724
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
725 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
726 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
727 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
728
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
729 This should output the following if the program succeeds.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
730
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
731 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
732 number == 654321
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
733 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
734
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
735 \subsection{Initialize and Setting Constants}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
736 To both initialize and set small constants the following two functions are available.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
737 \index{mp\_init\_set} \index{mp\_init\_set\_int}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
738 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
739 int mp_init_set (mp_int * a, mp_digit b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
740 int mp_init_set_int (mp_int * a, unsigned long b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
741 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
742
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
743 Both functions work like the previous counterparts except they first mp\_init $a$ before setting the values.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
744
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
745 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
746 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
747 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
748 mp_int number1, number2;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
749 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
750
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
751 /* initialize and set a single digit */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
752 if ((result = mp_init_set(&number1, 100)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
753 printf("Error setting number1: \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
754 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
755 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
756 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
757
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
758 /* initialize and set a long */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
759 if ((result = mp_init_set_int(&number2, 1023)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
760 printf("Error setting number2: \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
761 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
762 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
763 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
764
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
765 /* display */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
766 printf("Number1, Number2 == \%lu, \%lu",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
767 mp_get_int(&number1), mp_get_int(&number2));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
768
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
769 /* clear */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
770 mp_clear_multi(&number1, &number2, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
771
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
772 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
773 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
774 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
775
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
776 If this program succeeds it shall output.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
777 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
778 Number1, Number2 == 100, 1023
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
779 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
780
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
781 \section{Comparisons}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
782
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
783 Comparisons in LibTomMath are always performed in a ``left to right'' fashion. There are three possible return codes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
784 for any comparison.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
785
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
786 \index{MP\_GT} \index{MP\_EQ} \index{MP\_LT}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
787 \begin{figure}[here]
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
788 \begin{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
789 \begin{tabular}{|c|c|}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
790 \hline \textbf{Result Code} & \textbf{Meaning} \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
791 \hline MP\_GT & $a > b$ \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
792 \hline MP\_EQ & $a = b$ \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
793 \hline MP\_LT & $a < b$ \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
794 \hline
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
795 \end{tabular}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
796 \end{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
797 \caption{Comparison Codes for $a, b$}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
798 \label{fig:CMP}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
799 \end{figure}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
800
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
801 In figure \ref{fig:CMP} two integers $a$ and $b$ are being compared. In this case $a$ is said to be ``to the left'' of
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
802 $b$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
803
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
804 \subsection{Unsigned comparison}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
805
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
806 An unsigned comparison considers only the digits themselves and not the associated \textit{sign} flag of the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
807 mp\_int structures. This is analogous to an absolute comparison. The function mp\_cmp\_mag() will compare two
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
808 mp\_int variables based on their digits only.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
809
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
810 \index{mp\_cmp\_mag}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
811 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
812 int mp_cmp(mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
813 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
814 This will compare $a$ to $b$ placing $a$ to the left of $b$. This function cannot fail and will return one of the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
815 three compare codes listed in figure \ref{fig:CMP}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
816
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
817 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
818 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
819 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
820 mp_int number1, number2;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
821 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
822
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
823 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
824 printf("Error initializing the numbers. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
825 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
826 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
827 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
828
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
829 /* set the number1 to 5 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
830 mp_set(&number1, 5);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
831
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
832 /* set the number2 to -6 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
833 mp_set(&number2, 6);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
834 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
835 printf("Error negating number2. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
836 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
837 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
838 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
839
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
840 switch(mp_cmp_mag(&number1, &number2)) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
841 case MP_GT: printf("|number1| > |number2|"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
842 case MP_EQ: printf("|number1| = |number2|"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
843 case MP_LT: printf("|number1| < |number2|"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
844 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
845
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
846 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
847 mp_clear_multi(&number1, &number2, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
848
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
849 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
850 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
851 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
852
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
853 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
854 successfully it should print the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
855
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
856 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
857 |number1| < |number2|
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
858 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
859
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
860 This is because $\vert -6 \vert = 6$ and obviously $5 < 6$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
861
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
862 \subsection{Signed comparison}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
863
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
864 To compare two mp\_int variables based on their signed value the mp\_cmp() function is provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
865
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
866 \index{mp\_cmp}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
867 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
868 int mp_cmp(mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
869 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
870
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
871 This will compare $a$ to the left of $b$. It will first compare the signs of the two mp\_int variables. If they
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
872 differ it will return immediately based on their signs. If the signs are equal then it will compare the digits
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
873 individually. This function will return one of the compare conditions codes listed in figure \ref{fig:CMP}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
874
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
875 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
876 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
877 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
878 mp_int number1, number2;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
879 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
880
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
881 if ((result = mp_init_multi(&number1, &number2, NULL)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
882 printf("Error initializing the numbers. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
883 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
884 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
885 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
886
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
887 /* set the number1 to 5 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
888 mp_set(&number1, 5);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
889
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
890 /* set the number2 to -6 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
891 mp_set(&number2, 6);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
892 if ((result = mp_neg(&number2, &number2)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
893 printf("Error negating number2. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
894 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
895 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
896 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
897
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
898 switch(mp_cmp(&number1, &number2)) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
899 case MP_GT: printf("number1 > number2"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
900 case MP_EQ: printf("number1 = number2"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
901 case MP_LT: printf("number1 < number2"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
902 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
903
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
904 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
905 mp_clear_multi(&number1, &number2, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
906
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
907 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
908 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
909 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
910
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
911 If this program\footnote{This function uses the mp\_neg() function which is discussed in section \ref{sec:NEG}.} completes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
912 successfully it should print the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
913
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
914 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
915 number1 > number2
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
916 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
917
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
918 \subsection{Single Digit}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
919
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
920 To compare a single digit against an mp\_int the following function has been provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
921
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
922 \index{mp\_cmp\_d}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
923 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
924 int mp_cmp_d(mp_int * a, mp_digit b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
925 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
926
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
927 This will compare $a$ to the left of $b$ using a signed comparison. Note that it will always treat $b$ as
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
928 positive. This function is rather handy when you have to compare against small values such as $1$ (which often
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
929 comes up in cryptography). The function cannot fail and will return one of the tree compare condition codes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
930 listed in figure \ref{fig:CMP}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
931
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
932
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
933 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
934 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
935 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
936 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
937 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
938
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
939 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
940 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
941 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
942 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
943 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
944
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
945 /* set the number to 5 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
946 mp_set(&number, 5);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
947
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
948 switch(mp_cmp_d(&number, 7)) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
949 case MP_GT: printf("number > 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
950 case MP_EQ: printf("number = 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
951 case MP_LT: printf("number < 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
952 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
953
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
954 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
955 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
956
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
957 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
958 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
959 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
960
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
961 If this program functions properly it will print out the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
962
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
963 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
964 number < 7
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
965 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
966
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
967 \section{Logical Operations}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
968
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
969 Logical operations are operations that can be performed either with simple shifts or boolean operators such as
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
970 AND, XOR and OR directly. These operations are very quick.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
971
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
972 \subsection{Multiplication by two}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
973
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
974 Multiplications and divisions by any power of two can be performed with quick logical shifts either left or
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
975 right depending on the operation.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
976
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
977 When multiplying or dividing by two a special case routine can be used which are as follows.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
978 \index{mp\_mul\_2} \index{mp\_div\_2}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
979 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
980 int mp_mul_2(mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
981 int mp_div_2(mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
982 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
983
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
984 The former will assign twice $a$ to $b$ while the latter will assign half $a$ to $b$. These functions are fast
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
985 since the shift counts and maskes are hardcoded into the routines.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
986
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
987 \begin{small} \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
988 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
989 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
990 mp_int number;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
991 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
992
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
993 if ((result = mp_init(&number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
994 printf("Error initializing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
995 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
996 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
997 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
998
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
999 /* set the number to 5 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1000 mp_set(&number, 5);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1001
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1002 /* multiply by two */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1003 if ((result = mp\_mul\_2(&number, &number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1004 printf("Error multiplying the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1005 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1006 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1007 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1008 switch(mp_cmp_d(&number, 7)) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1009 case MP_GT: printf("2*number > 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1010 case MP_EQ: printf("2*number = 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1011 case MP_LT: printf("2*number < 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1012 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1013
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1014 /* now divide by two */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1015 if ((result = mp\_div\_2(&number, &number)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1016 printf("Error dividing the number. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1017 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1018 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1019 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1020 switch(mp_cmp_d(&number, 7)) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1021 case MP_GT: printf("2*number/2 > 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1022 case MP_EQ: printf("2*number/2 = 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1023 case MP_LT: printf("2*number/2 < 7"); break;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1024 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1025
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1026 /* we're done with it. */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1027 mp_clear(&number);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1028
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1029 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1030 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1031 \end{alltt} \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1032
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1033 If this program is successful it will print out the following text.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1034
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1035 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1036 2*number > 7
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1037 2*number/2 < 7
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1038 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1039
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1040 Since $10 > 7$ and $5 < 7$. To multiply by a power of two the following function can be used.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1041
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1042 \index{mp\_mul\_2d}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1043 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1044 int mp_mul_2d(mp_int * a, int b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1045 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1046
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1047 This will multiply $a$ by $2^b$ and store the result in ``c''. If the value of $b$ is less than or equal to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1048 zero the function will copy $a$ to ``c'' without performing any further actions.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1049
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1050 To divide by a power of two use the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1051
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1052 \index{mp\_div\_2d}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1053 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1054 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1055 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1056 Which will divide $a$ by $2^b$, store the quotient in ``c'' and the remainder in ``d'. If $b \le 0$ then the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1057 function simply copies $a$ over to ``c'' and zeroes $d$. The variable $d$ may be passed as a \textbf{NULL}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1058 value to signal that the remainder is not desired.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1059
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1060 \subsection{Polynomial Basis Operations}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1061
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1062 Strictly speaking the organization of the integers within the mp\_int structures is what is known as a
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1063 ``polynomial basis''. This simply means a field element is stored by divisions of a radix. For example, if
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1064 $f(x) = \sum_{i=0}^{k} y_ix^k$ for any vector $\vec y$ then the array of digits in $\vec y$ are said to be
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1065 the polynomial basis representation of $z$ if $f(\beta) = z$ for a given radix $\beta$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1066
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1067 To multiply by the polynomial $g(x) = x$ all you have todo is shift the digits of the basis left one place. The
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1068 following function provides this operation.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1069
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1070 \index{mp\_lshd}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1071 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1072 int mp_lshd (mp_int * a, int b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1073 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1074
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1075 This will multiply $a$ in place by $x^b$ which is equivalent to shifting the digits left $b$ places and inserting zeroes
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1076 in the least significant digits. Similarly to divide by a power of $x$ the following function is provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1077
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1078 \index{mp\_rshd}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1079 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1080 void mp_rshd (mp_int * a, int b)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1081 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1082 This will divide $a$ in place by $x^b$ and discard the remainder. This function cannot fail as it performs the operations
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1083 in place and no new digits are required to complete it.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1084
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1085 \subsection{AND, OR and XOR Operations}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1086
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1087 While AND, OR and XOR operations are not typical ``bignum functions'' they can be useful in several instances. The
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1088 three functions are prototyped as follows.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1089
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1090 \index{mp\_or} \index{mp\_and} \index{mp\_xor}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1091 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1092 int mp_or (mp_int * a, mp_int * b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1093 int mp_and (mp_int * a, mp_int * b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1094 int mp_xor (mp_int * a, mp_int * b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1095 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1096
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1097 Which compute $c = a \odot b$ where $\odot$ is one of OR, AND or XOR.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1098
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1099 \section{Addition and Subtraction}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1100
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1101 To compute an addition or subtraction the following two functions can be used.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1102
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1103 \index{mp\_add} \index{mp\_sub}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1104 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1105 int mp_add (mp_int * a, mp_int * b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1106 int mp_sub (mp_int * a, mp_int * b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1107 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1108
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1109 Which perform $c = a \odot b$ where $\odot$ is one of signed addition or subtraction. The operations are fully sign
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1110 aware.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1111
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1112 \section{Sign Manipulation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1113 \subsection{Negation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1114 \label{sec:NEG}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1115 Simple integer negation can be performed with the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1116
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1117 \index{mp\_neg}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1118 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1119 int mp_neg (mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1120 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1121
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1122 Which assigns $-a$ to $b$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1123
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1124 \subsection{Absolute}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1125 Simple integer absolutes can be performed with the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1126
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1127 \index{mp\_neg}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1128 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1129 int mp_abs (mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1130 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1131
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1132 Which assigns $\vert a \vert$ to $b$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1133
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1134 \section{Integer Division and Remainder}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1135 To perform a complete and general integer division with remainder use the following function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1136
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1137 \index{mp\_div}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1138 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1139 int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1140 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1141
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1142 This divides $a$ by $b$ and stores the quotient in $c$ and $d$. The signed quotient is computed such that
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1143 $bc + d = a$. Note that either of $c$ or $d$ can be set to \textbf{NULL} if their value is not required. If
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1144 $b$ is zero the function returns \textbf{MP\_VAL}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1145
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1146
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1147 \chapter{Multiplication and Squaring}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1148 \section{Multiplication}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1149 A full signed integer multiplication can be performed with the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1150 \index{mp\_mul}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1151 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1152 int mp_mul (mp_int * a, mp_int * b, mp_int * c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1153 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1154 Which assigns the full signed product $ab$ to $c$. This function actually breaks into one of four cases which are
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1155 specific multiplication routines optimized for given parameters. First there are the Toom-Cook multiplications which
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1156 should only be used with very large inputs. This is followed by the Karatsuba multiplications which are for moderate
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1157 sized inputs. Then followed by the Comba and baseline multipliers.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1158
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1159 Fortunately for the developer you don't really need to know this unless you really want to fine tune the system. mp\_mul()
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1160 will determine on its own\footnote{Some tweaking may be required.} what routine to use automatically when it is called.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1161
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1162 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1163 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1164 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1165 mp_int number1, number2;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1166 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1167
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1168 /* Initialize the numbers */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1169 if ((result = mp_init_multi(&number1,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1170 &number2, NULL)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1171 printf("Error initializing the numbers. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1172 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1173 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1174 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1175
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1176 /* set the terms */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1177 if ((result = mp_set_int(&number, 257)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1178 printf("Error setting number1. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1179 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1180 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1181 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1182
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1183 if ((result = mp_set_int(&number2, 1023)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1184 printf("Error setting number2. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1185 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1186 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1187 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1188
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1189 /* multiply them */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1190 if ((result = mp_mul(&number1, &number2,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1191 &number1)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1192 printf("Error multiplying terms. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1193 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1194 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1195 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1196
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1197 /* display */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1198 printf("number1 * number2 == \%lu", mp_get_int(&number1));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1199
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1200 /* free terms and return */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1201 mp_clear_multi(&number1, &number2, NULL);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1202
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1203 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1204 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1205 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1206
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1207 If this program succeeds it shall output the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1208
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1209 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1210 number1 * number2 == 262911
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1211 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1212
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1213 \section{Squaring}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1214 Since squaring can be performed faster than multiplication it is performed it's own function instead of just using
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1215 mp\_mul().
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1216
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1217 \index{mp\_sqr}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1218 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1219 int mp_sqr (mp_int * a, mp_int * b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1220 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1221
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1222 Will square $a$ and store it in $b$. Like the case of multiplication there are four different squaring
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1223 algorithms all which can be called from mp\_sqr(). It is ideal to use mp\_sqr over mp\_mul when squaring terms.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1224
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1225 \section{Tuning Polynomial Basis Routines}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1226
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1227 Both of the Toom-Cook and Karatsuba multiplication algorithms are faster than the traditional $O(n^2)$ approach that
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1228 the Comba and baseline algorithms use. At $O(n^{1.464973})$ and $O(n^{1.584962})$ running times respectfully they require
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1229 considerably less work. For example, a 10000-digit multiplication would take roughly 724,000 single precision
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1230 multiplications with Toom-Cook or 100,000,000 single precision multiplications with the standard Comba (a factor
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1231 of 138).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1232
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1233 So why not always use Karatsuba or Toom-Cook? The simple answer is that they have so much overhead that they're not
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1234 actually faster than Comba until you hit distinct ``cutoff'' points. For Karatsuba with the default configuration,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1235 GCC 3.3.1 and an Athlon XP processor the cutoff point is roughly 110 digits (about 70 for the Intel P4). That is, at
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1236 110 digits Karatsuba and Comba multiplications just about break even and for 110+ digits Karatsuba is faster.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1237
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1238 Toom-Cook has incredible overhead and is probably only useful for very large inputs. So far no known cutoff points
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1239 exist and for the most part I just set the cutoff points very high to make sure they're not called.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1240
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1241 A demo program in the ``etc/'' directory of the project called ``tune.c'' can be used to find the cutoff points. This
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1242 can be built with GCC as follows
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1243
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1244 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1245 make XXX
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1246 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1247 Where ``XXX'' is one of the following entries from the table \ref{fig:tuning}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1248
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1249 \begin{figure}[here]
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1250 \begin{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1251 \begin{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1252 \begin{tabular}{|l|l|}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1253 \hline \textbf{Value of XXX} & \textbf{Meaning} \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1254 \hline tune & Builds portable tuning application \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1255 \hline tune86 & Builds x86 (pentium and up) program for COFF \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1256 \hline tune86c & Builds x86 program for Cygwin \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1257 \hline tune86l & Builds x86 program for Linux (ELF format) \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1258 \hline
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1259 \end{tabular}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1260 \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1261 \end{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1262 \caption{Build Names for Tuning Programs}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1263 \label{fig:tuning}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1264 \end{figure}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1265
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1266 When the program is running it will output a series of measurements for different cutoff points. It will first find
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1267 good Karatsuba squaring and multiplication points. Then it proceeds to find Toom-Cook points. Note that the Toom-Cook
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1268 tuning takes a very long time as the cutoff points are likely to be very high.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1269
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1270 \chapter{Modular Reduction}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1271
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1272 Modular reduction is process of taking the remainder of one quantity divided by another. Expressed
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1273 as (\ref{eqn:mod}) the modular reduction is equivalent to the remainder of $b$ divided by $c$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1274
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1275 \begin{equation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1276 a \equiv b \mbox{ (mod }c\mbox{)}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1277 \label{eqn:mod}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1278 \end{equation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1279
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1280 Of particular interest to cryptography are reductions where $b$ is limited to the range $0 \le b < c^2$ since particularly
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1281 fast reduction algorithms can be written for the limited range.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1282
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1283 Note that one of the four optimized reduction algorithms are automatically chosen in the modular exponentiation
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1284 algorithm mp\_exptmod when an appropriate modulus is detected.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1285
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1286 \section{Straight Division}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1287 In order to effect an arbitrary modular reduction the following algorithm is provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1288
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1289 \index{mp\_mod}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1290 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1291 int mp_mod(mp_int *a, mp_int *b, mp_int *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1292 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1293
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1294 This reduces $a$ modulo $b$ and stores the result in $c$. The sign of $c$ shall agree with the sign
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1295 of $b$. This algorithm accepts an input $a$ of any range and is not limited by $0 \le a < b^2$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1296
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1297 \section{Barrett Reduction}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1298
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1299 Barrett reduction is a generic optimized reduction algorithm that requires pre--computation to achieve
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1300 a decent speedup over straight division. First a $mu$ value must be precomputed with the following function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1301
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1302 \index{mp\_reduce\_setup}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1303 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1304 int mp_reduce_setup(mp_int *a, mp_int *b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1305 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1306
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1307 Given a modulus in $b$ this produces the required $mu$ value in $a$. For any given modulus this only has to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1308 be computed once. Modular reduction can now be performed with the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1309
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1310 \index{mp\_reduce}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1311 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1312 int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1313 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1314
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1315 This will reduce $a$ in place modulo $b$ with the precomputed $mu$ value in $c$. $a$ must be in the range
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1316 $0 \le a < b^2$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1317
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1318 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1319 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1320 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1321 mp_int a, b, c, mu;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1322 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1323
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1324 /* initialize a,b to desired values, mp_init mu,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1325 * c and set c to 1...we want to compute a^3 mod b
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1326 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1327
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1328 /* get mu value */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1329 if ((result = mp_reduce_setup(&mu, b)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1330 printf("Error getting mu. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1331 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1332 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1333 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1334
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1335 /* square a to get c = a^2 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1336 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1337 printf("Error squaring. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1338 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1339 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1340 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1341
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1342 /* now reduce `c' modulo b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1343 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1344 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1345 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1346 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1347 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1348
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1349 /* multiply a to get c = a^3 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1350 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1351 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1352 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1353 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1354 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1355
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1356 /* now reduce `c' modulo b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1357 if ((result = mp_reduce(&c, &b, &mu)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1358 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1359 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1360 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1361 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1362
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1363 /* c now equals a^3 mod b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1364
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1365 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1366 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1367 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1368
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1369 This program will calculate $a^3 \mbox{ mod }b$ if all the functions succeed.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1370
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1371 \section{Montgomery Reduction}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1372
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1373 Montgomery is a specialized reduction algorithm for any odd moduli. Like Barrett reduction a pre--computation
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1374 step is required. This is accomplished with the following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1375
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1376 \index{mp\_montgomery\_setup}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1377 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1378 int mp_montgomery_setup(mp_int *a, mp_digit *mp);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1379 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1380
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1381 For the given odd moduli $a$ the precomputation value is placed in $mp$. The reduction is computed with the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1382 following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1383
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1384 \index{mp\_montgomery\_reduce}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1385 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1386 int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1387 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1388 This reduces $a$ in place modulo $m$ with the pre--computed value $mp$. $a$ must be in the range
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1389 $0 \le a < b^2$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1390
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1391 Montgomery reduction is faster than Barrett reduction for moduli smaller than the ``comba'' limit. With the default
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1392 setup for instance, the limit is $127$ digits ($3556$--bits). Note that this function is not limited to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1393 $127$ digits just that it falls back to a baseline algorithm after that point.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1394
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1395 An important observation is that this reduction does not return $a \mbox{ mod }m$ but $aR^{-1} \mbox{ mod }m$
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1396 where $R = \beta^n$, $n$ is the n number of digits in $m$ and $\beta$ is radix used (default is $2^{28}$).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1397
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1398 To quickly calculate $R$ the following function was provided.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1399
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1400 \index{mp\_montgomery\_calc\_normalization}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1401 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1402 int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1403 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1404 Which calculates $a = R$ for the odd moduli $b$ without using multiplication or division.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1405
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1406 The normal modus operandi for Montgomery reductions is to normalize the integers before entering the system. For
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1407 example, to calculate $a^3 \mbox { mod }b$ using Montgomery reduction the value of $a$ can be normalized by
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1408 multiplying it by $R$. Consider the following code snippet.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1409
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1410 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1411 int main(void)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1412 \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1413 mp_int a, b, c, R;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1414 mp_digit mp;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1415 int result;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1416
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1417 /* initialize a,b to desired values,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1418 * mp_init R, c and set c to 1....
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1419 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1420
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1421 /* get normalization */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1422 if ((result = mp_montgomery_calc_normalization(&R, b)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1423 printf("Error getting norm. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1424 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1425 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1426 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1427
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1428 /* get mp value */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1429 if ((result = mp_montgomery_setup(&c, &mp)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1430 printf("Error setting up montgomery. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1431 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1432 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1433 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1434
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1435 /* normalize `a' so now a is equal to aR */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1436 if ((result = mp_mulmod(&a, &R, &b, &a)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1437 printf("Error computing aR. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1438 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1439 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1440 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1441
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1442 /* square a to get c = a^2R^2 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1443 if ((result = mp_sqr(&a, &c)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1444 printf("Error squaring. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1445 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1446 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1447 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1448
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1449 /* now reduce `c' back down to c = a^2R^2 * R^-1 == a^2R */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1450 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1451 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1452 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1453 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1454 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1455
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1456 /* multiply a to get c = a^3R^2 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1457 if ((result = mp_mul(&a, &c, &c)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1458 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1459 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1460 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1461 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1462
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1463 /* now reduce `c' back down to c = a^3R^2 * R^-1 == a^3R */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1464 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1465 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1466 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1467 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1468 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1469
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1470 /* now reduce (again) `c' back down to c = a^3R * R^-1 == a^3 */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1471 if ((result = mp_montgomery_reduce(&c, &b, mp)) != MP_OKAY) \{
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1472 printf("Error reducing. \%s",
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1473 mp_error_to_string(result));
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1474 return EXIT_FAILURE;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1475 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1476
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1477 /* c now equals a^3 mod b */
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1478
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1479 return EXIT_SUCCESS;
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1480 \}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1481 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1482
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1483 This particular example does not look too efficient but it demonstrates the point of the algorithm. By
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1484 normalizing the inputs the reduced results are always of the form $aR$ for some variable $a$. This allows
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1485 a single final reduction to correct for the normalization and the fast reduction used within the algorithm.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1486
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1487 For more details consider examining the file \textit{bn\_mp\_exptmod\_fast.c}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1488
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1489 \section{Restricted Dimminished Radix}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1490
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1491 ``Dimminished Radix'' reduction refers to reduction with respect to moduli that are ameniable to simple
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1492 digit shifting and small multiplications. In this case the ``restricted'' variant refers to moduli of the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1493 form $\beta^k - p$ for some $k \ge 0$ and $0 < p < \beta$ where $\beta$ is the radix (default to $2^{28}$).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1494
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1495 As in the case of Montgomery reduction there is a pre--computation phase required for a given modulus.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1496
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1497 \index{mp\_dr\_setup}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1498 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1499 void mp_dr_setup(mp_int *a, mp_digit *d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1500 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1501
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1502 This computes the value required for the modulus $a$ and stores it in $d$. This function cannot fail
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1503 and does not return any error codes. After the pre--computation a reduction can be performed with the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1504 following.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1505
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1506 \index{mp\_dr\_reduce}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1507 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1508 int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1509 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1510
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1511 This reduces $a$ in place modulo $b$ with the pre--computed value $mp$. $b$ must be of a restricted
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1512 dimminished radix form and $a$ must be in the range $0 \le a < b^2$. Dimminished radix reductions are
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1513 much faster than both Barrett and Montgomery reductions as they have a much lower asymtotic running time.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1514
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1515 Since the moduli are restricted this algorithm is not particularly useful for something like Rabin, RSA or
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1516 BBS cryptographic purposes. This reduction algorithm is useful for Diffie-Hellman and ECC where fixed
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1517 primes are acceptable.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1518
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1519 Note that unlike Montgomery reduction there is no normalization process. The result of this function is
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1520 equal to the correct residue.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1521
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1522 \section{Unrestricted Dimminshed Radix}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1523
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1524 Unrestricted reductions work much like the restricted counterparts except in this case the moduli is of the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1525 form $2^k - p$ for $0 < p < \beta$. In this sense the unrestricted reductions are more flexible as they
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1526 can be applied to a wider range of numbers.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1527
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1528 \index{mp\_reduce\_2k\_setup}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1529 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1530 int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1531 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1532
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1533 This will compute the required $d$ value for the given moduli $a$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1534
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1535 \index{mp\_reduce\_2k}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1536 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1537 int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1538 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1539
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1540 This will reduce $a$ in place modulo $n$ with the pre--computed value $d$. From my experience this routine is
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1541 slower than mp\_dr\_reduce but faster for most moduli sizes than the Montgomery reduction.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1542
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1543 \chapter{Exponentiation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1544 \section{Single Digit Exponentiation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1545 \index{mp\_expt\_d}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1546 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1547 int mp_expt_d (mp_int * a, mp_digit b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1548 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1549 This computes $c = a^b$ using a simple binary left-to-right algorithm. It is faster than repeated multiplications by
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1550 $a$ for all values of $b$ greater than three.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1551
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1552 \section{Modular Exponentiation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1553 \index{mp\_exptmod}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1554 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1555 int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1556 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1557 This computes $Y \equiv G^X \mbox{ (mod }P\mbox{)}$ using a variable width sliding window algorithm. This function
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1558 will automatically detect the fastest modular reduction technique to use during the operation. For negative values of
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1559 $X$ the operation is performed as $Y \equiv (G^{-1} \mbox{ mod }P)^{\vert X \vert} \mbox{ (mod }P\mbox{)}$ provided that
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1560 $gcd(G, P) = 1$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1561
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1562 This function is actually a shell around the two internal exponentiation functions. This routine will automatically
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1563 detect when Barrett, Montgomery, Restricted and Unrestricted Dimminished Radix based exponentiation can be used. Generally
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1564 moduli of the a ``restricted dimminished radix'' form lead to the fastest modular exponentiations. Followed by Montgomery
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1565 and the other two algorithms.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1566
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1567 \section{Root Finding}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1568 \index{mp\_n\_root}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1569 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1570 int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1571 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1572 This computes $c = a^{1/b}$ such that $c^b \le a$ and $(c+1)^b > a$. The implementation of this function is not
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1573 ideal for values of $b$ greater than three. It will work but become very slow. So unless you are working with very small
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1574 numbers (less than 1000 bits) I'd avoid $b > 3$ situations. Will return a positive root only for even roots and return
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1575 a root with the sign of the input for odd roots. For example, performing $4^{1/2}$ will return $2$ whereas $(-8)^{1/3}$
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1576 will return $-2$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1577
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1578 This algorithm uses the ``Newton Approximation'' method and will converge on the correct root fairly quickly. Since
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1579 the algorithm requires raising $a$ to the power of $b$ it is not ideal to attempt to find roots for large
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1580 values of $b$. If particularly large roots are required then a factor method could be used instead. For example,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1581 $a^{1/16}$ is equivalent to $\left (a^{1/4} \right)^{1/4}$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1582
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1583 \chapter{Prime Numbers}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1584 \section{Trial Division}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1585 \index{mp\_prime\_is\_divisible}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1586 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1587 int mp_prime_is_divisible (mp_int * a, int *result)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1588 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1589 This will attempt to evenly divide $a$ by a list of primes\footnote{Default is the first 256 primes.} and store the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1590 outcome in ``result''. That is if $result = 0$ then $a$ is not divisible by the primes, otherwise it is. Note that
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1591 if the function does not return \textbf{MP\_OKAY} the value in ``result'' should be considered undefined\footnote{Currently
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1592 the default is to set it to zero first.}.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1593
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1594 \section{Fermat Test}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1595 \index{mp\_prime\_fermat}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1596 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1597 int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1598 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1599 Performs a Fermat primality test to the base $b$. That is it computes $b^a \mbox{ mod }a$ and tests whether the value is
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1600 equal to $b$ or not. If the values are equal then $a$ is probably prime and $result$ is set to one. Otherwise $result$
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1601 is set to zero.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1602
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1603 \section{Miller-Rabin Test}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1604 \index{mp\_prime\_miller\_rabin}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1605 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1606 int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1607 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1608 Performs a Miller-Rabin test to the base $b$ of $a$. This test is much stronger than the Fermat test and is very hard to
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1609 fool (besides with Carmichael numbers). If $a$ passes the test (therefore is probably prime) $result$ is set to one.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1610 Otherwise $result$ is set to zero.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1611
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1612 Note that is suggested that you use the Miller-Rabin test instead of the Fermat test since all of the failures of
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1613 Miller-Rabin are a subset of the failures of the Fermat test.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1614
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1615 \subsection{Required Number of Tests}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1616 Generally to ensure a number is very likely to be prime you have to perform the Miller-Rabin with at least a half-dozen
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1617 or so unique bases. However, it has been proven that the probability of failure goes down as the size of the input goes up.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1618 This is why a simple function has been provided to help out.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1619
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1620 \index{mp\_prime\_rabin\_miller\_trials}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1621 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1622 int mp_prime_rabin_miller_trials(int size)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1623 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1624 This returns the number of trials required for a $2^{-96}$ (or lower) probability of failure for a given ``size'' expressed
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1625 in bits. This comes in handy specially since larger numbers are slower to test. For example, a 512-bit number would
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1626 require ten tests whereas a 1024-bit number would only require four tests.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1627
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1628 You should always still perform a trial division before a Miller-Rabin test though.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1629
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1630 \section{Primality Testing}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1631 \index{mp\_prime\_is\_prime}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1632 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1633 int mp_prime_is_prime (mp_int * a, int t, int *result)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1634 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1635 This will perform a trial division followed by $t$ rounds of Miller-Rabin tests on $a$ and store the result in $result$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1636 If $a$ passes all of the tests $result$ is set to one, otherwise it is set to zero. Note that $t$ is bounded by
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1637 $1 \le t < PRIME\_SIZE$ where $PRIME\_SIZE$ is the number of primes in the prime number table (by default this is $256$).
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1638
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1639 \section{Next Prime}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1640 \index{mp\_prime\_next\_prime}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1641 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1642 int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1643 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1644 This finds the next prime after $a$ that passes mp\_prime\_is\_prime() with $t$ tests. Set $bbs\_style$ to one if you
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1645 want only the next prime congruent to $3 \mbox{ mod } 4$, otherwise set it to zero to find any next prime.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1646
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1647 \section{Random Primes}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1648 \index{mp\_prime\_random}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1649 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1650 int mp_prime_random(mp_int *a, int t, int size, int bbs,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1651 ltm_prime_callback cb, void *dat)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1652 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1653 This will find a prime greater than $256^{size}$ which can be ``bbs\_style'' or not depending on $bbs$ and must pass
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1654 $t$ rounds of tests. The ``ltm\_prime\_callback'' is a typedef for
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1655
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1656 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1657 typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1658 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1659
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1660 Which is a function that must read $len$ bytes (and return the amount stored) into $dst$. The $dat$ variable is simply
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1661 copied from the original input. It can be used to pass RNG context data to the callback. The function
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1662 mp\_prime\_random() is more suitable for generating primes which must be secret (as in the case of RSA) since there
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1663 is no skew on the least significant bits.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1664
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1665 \textit{Note:} As of v0.30 of the LibTomMath library this function has been deprecated. It is still available
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1666 but users are encouraged to use the new mp\_prime\_random\_ex() function instead.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1667
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1668 \subsection{Extended Generation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1669 \index{mp\_prime\_random\_ex}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1670 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1671 int mp_prime_random_ex(mp_int *a, int t,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1672 int size, int flags,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1673 ltm_prime_callback cb, void *dat);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1674 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1675 This will generate a prime in $a$ using $t$ tests of the primality testing algorithms. The variable $size$
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1676 specifies the bit length of the prime desired. The variable $flags$ specifies one of several options available
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1677 (see fig. \ref{fig:primeopts}) which can be OR'ed together. The callback parameters are used as in
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1678 mp\_prime\_random().
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1679
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1680 \begin{figure}[here]
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1681 \begin{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1682 \begin{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1683 \begin{tabular}{|r|l|}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1684 \hline \textbf{Flag} & \textbf{Meaning} \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1685 \hline LTM\_PRIME\_BBS & Make the prime congruent to $3$ modulo $4$ \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1686 \hline LTM\_PRIME\_SAFE & Make a prime $p$ such that $(p - 1)/2$ is also prime. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1687 & This option implies LTM\_PRIME\_BBS as well. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1688 \hline LTM\_PRIME\_2MSB\_OFF & Makes sure that the bit adjacent to the most significant bit \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1689 & Is forced to zero. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1690 \hline LTM\_PRIME\_2MSB\_ON & Makes sure that the bit adjacent to the most significant bit \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1691 & Is forced to one. \\
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1692 \hline
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1693 \end{tabular}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1694 \end{small}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1695 \end{center}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1696 \caption{Primality Generation Options}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1697 \label{fig:primeopts}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1698 \end{figure}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1699
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1700 \chapter{Input and Output}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1701 \section{ASCII Conversions}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1702 \subsection{To ASCII}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1703 \index{mp\_toradix}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1704 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1705 int mp_toradix (mp_int * a, char *str, int radix);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1706 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1707 This still store $a$ in ``str'' as a base-``radix'' string of ASCII chars. This function appends a NUL character
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1708 to terminate the string. Valid values of ``radix'' line in the range $[2, 64]$. To determine the size (exact) required
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1709 by the conversion before storing any data use the following function.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1710
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1711 \index{mp\_radix\_size}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1712 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1713 int mp_radix_size (mp_int * a, int radix, int *size)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1714 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1715 This stores in ``size'' the number of characters (including space for the NUL terminator) required. Upon error this
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1716 function returns an error code and ``size'' will be zero.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1717
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1718 \subsection{From ASCII}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1719 \index{mp\_read\_radix}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1720 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1721 int mp_read_radix (mp_int * a, char *str, int radix);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1722 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1723 This will read the base-``radix'' NUL terminated string from ``str'' into $a$. It will stop reading when it reads a
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1724 character it does not recognize (which happens to include th NUL char... imagine that...). A single leading $-$ sign
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1725 can be used to denote a negative number.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1726
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1727 \section{Binary Conversions}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1728
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1729 Converting an mp\_int to and from binary is another keen idea.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1730
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1731 \index{mp\_unsigned\_bin\_size}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1732 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1733 int mp_unsigned_bin_size(mp_int *a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1734 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1735
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1736 This will return the number of bytes (octets) required to store the unsigned copy of the integer $a$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1737
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1738 \index{mp\_to\_unsigned\_bin}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1739 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1740 int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1741 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1742 This will store $a$ into the buffer $b$ in big--endian format. Fortunately this is exactly what DER (or is it ASN?)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1743 requires. It does not store the sign of the integer.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1744
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1745 \index{mp\_read\_unsigned\_bin}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1746 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1747 int mp_read_unsigned_bin(mp_int *a, unsigned char *b, int c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1748 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1749 This will read in an unsigned big--endian array of bytes (octets) from $b$ of length $c$ into $a$. The resulting
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1750 integer $a$ will always be positive.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1751
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1752 For those who acknowledge the existence of negative numbers (heretic!) there are ``signed'' versions of the
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1753 previous functions.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1754
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1755 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1756 int mp_signed_bin_size(mp_int *a);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1757 int mp_read_signed_bin(mp_int *a, unsigned char *b, int c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1758 int mp_to_signed_bin(mp_int *a, unsigned char *b);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1759 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1760 They operate essentially the same as the unsigned copies except they prefix the data with zero or non--zero
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1761 byte depending on the sign. If the sign is zpos (e.g. not negative) the prefix is zero, otherwise the prefix
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1762 is non--zero.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1763
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1764 \chapter{Algebraic Functions}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1765 \section{Extended Euclidean Algorithm}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1766 \index{mp\_exteuclid}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1767 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1768 int mp_exteuclid(mp_int *a, mp_int *b,
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1769 mp_int *U1, mp_int *U2, mp_int *U3);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1770 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1771
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1772 This finds the triple U1/U2/U3 using the Extended Euclidean algorithm such that the following equation holds.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1773
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1774 \begin{equation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1775 a \cdot U1 + b \cdot U2 = U3
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1776 \end{equation}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1777
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1778 Any of the U1/U2/U3 paramters can be set to \textbf{NULL} if they are not desired.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1779
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1780 \section{Greatest Common Divisor}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1781 \index{mp\_gcd}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1782 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1783 int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1784 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1785 This will compute the greatest common divisor of $a$ and $b$ and store it in $c$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1786
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1787 \section{Least Common Multiple}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1788 \index{mp\_lcm}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1789 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1790 int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1791 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1792 This will compute the least common multiple of $a$ and $b$ and store it in $c$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1793
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1794 \section{Jacobi Symbol}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1795 \index{mp\_jacobi}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1796 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1797 int mp_jacobi (mp_int * a, mp_int * p, int *c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1798 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1799 This will compute the Jacobi symbol for $a$ with respect to $p$. If $p$ is prime this essentially computes the Legendre
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1800 symbol. The result is stored in $c$ and can take on one of three values $\lbrace -1, 0, 1 \rbrace$. If $p$ is prime
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1801 then the result will be $-1$ when $a$ is not a quadratic residue modulo $p$. The result will be $0$ if $a$ divides $p$
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1802 and the result will be $1$ if $a$ is a quadratic residue modulo $p$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1803
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1804 \section{Modular Inverse}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1805 \index{mp\_invmod}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1806 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1807 int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1808 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1809 Computes the multiplicative inverse of $a$ modulo $b$ and stores the result in $c$ such that $ac \equiv 1 \mbox{ (mod }b\mbox{)}$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1810
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1811 \section{Single Digit Functions}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1812
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1813 For those using small numbers (\textit{snicker snicker}) there are several ``helper'' functions
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1814
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1815 \index{mp\_add\_d} \index{mp\_sub\_d} \index{mp\_mul\_d} \index{mp\_div\_d} \index{mp\_mod\_d}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1816 \begin{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1817 int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1818 int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1819 int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1820 int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1821 int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1822 \end{alltt}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1823
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1824 These work like the full mp\_int capable variants except the second parameter $b$ is a mp\_digit. These
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1825 functions fairly handy if you have to work with relatively small numbers since you will not have to allocate
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1826 an entire mp\_int to store a number like $1$ or $2$.
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1827
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1828 \input{bn.ind}
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1829
e1037a1e12e7 0.30 release of LibTomMath
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1830 \end{document}