\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#237: Arbitrary-Precision Integers for Tcl}
\date{January 14, 2005}
\author{Kevin B. Kenny, Don Porter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#237}&\textbf{Arbitrary-Precision Integers for Tcl}\\\hline
Author&

Kevin B. Kenny \tipmail{kennykb}{acm.org}
\par
Don Porter \tipmail{dgp}{users.sf.net}
\\
Created&Friday, \(\text{14}^{\text{th}}\) January 2005\\
Type&Project\\
Tcl Version&\(8.5\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.19 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP adds the capability to perform computations on integer values of arbitrary precision.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

There have been a number of issues in Tcl 8.4 dealing with the limited range of native integers and wide values. The original ideas of \cite{tip72}, while they have given at least the basics of 64-bit integer support, have also introduced some subtle violations of the doctrine that ``everything is a string.'' Some of these have been insidious bugs --- [\url{http://sf.net/tracker/?func=detail&aid=868489&group_id=10894&atid=110894}] and [\url{http://sf.net/tracker/?func=detail&aid=1006626&group_id=10894&atid=110894}] are illustrative --- while others are perhaps not ``bugs'' in the strictest sense, but are still surprising behaviour.

For instance, it is possible for a script to tell integers from wide integers even when their string representations are equal, by performing any arithmetic operation that overflows:
\begingroup\small\begin{verbatim}
    % set x 2147483647    % set x [expr { wide(2146483647) }]
    2147483647            2147483647
    % incr x              % incr x
    -2147483648           2147483648
\end{verbatim}
\endgroup

With things as they stand, [\url{http://sf.net/tracker/?func=detail&aid=1006626&group_id=10894&atid=110894}] is nearly unfixable. It causes misconversion of large floating point numbers that look like integers:
\begingroup\small\begin{verbatim}
    % set x -9223372036854775809
    -9223372036854775809
    % expr {double($x)}
    9.22337203685e+018
    % scan $x %g
    -9.22337203685e+018
\end{verbatim}
\endgroup

The reason here is that the string of digits is first converted to a 64-bit unsigned integer (\textbf{Tcl\_WideUInt}). The '-' sign causes the unsigned integer to be interpreted as a signed integer, and the sign reversed. Since interpreting the unsigned integer as signed yields an overflow, the result is positive rather than negative and gives the odd behaviour shown above.

Of course, even if the implementation of 64-bit arithmetic were bug free, there would be uses for arithmetic of higher precision. One example is that several Tcl users have attempted pure-Tcl implementations of RSA and Diffie-Hellman cryptographic algorithms. These algorithms depend on arithmetic of high precision; implementing them efficiently in today's Tcl requires a C extension because the high-precision algorithms implemented in Tcl are simply too slow to be acceptable.

Finally, studying the references for accurate conversion of floating point numbers \cite{tip132} reveals that input and output conversions both require arbitrary-precision arithmetic in their implementation. The reference implementation supplied with that TIP, alas, is code that is poorly commented and difficult to integrate into Tcl's build system. Reimplementing it according to Tcl's engineering practices means implementing a large part of an arbitrary-precision arithmetic library.

\section{Proposal}

This TIP proposes augmenting Tcl with code for processing integers of arbitrary precision. Specifically:
\begin{enumerate}
\item{}
The \textit{libtommath} library [\url{http://math.libtomcrypt.com/}] shall be added in a subdirectory of the Tcl source tree, parallel to \textit{generic/}, \textit{compat/}, etc. This library implements arithmetic on integers of arbitrary precision. For the rationale behind this library, and some of the precise integration details, see ``Choice of Libary'' below.
\setcounter{enumi}{1}
\item{}
New functions, \textit{Tcl\_NewBignumObj}, \textit{Tcl\_SetBignumObj}, \textit{Tcl\_GetBignumFromObj} and \textit{Tcl\_GetBignumAndClearObj} shall be added to the Tcl library. They shall be specified as follows:
\begin{description}
\item[Tcl\_NewBignumObj]
Creates an object containing an integer of arbitrary precision. The \textit{value} argument gives the integer in the format native to \textit{libtommath}. \textit{Upon return, the value argument is cleared, because its digit array has had its ownership transferred to the Tcl library.}

Tcl\_Obj *\textbf{Tcl\_NewBignumObj}(mp\_int *\textit{value})
\item[Tcl\_SetBignumObj]
Changes the value of an object to an integer of arbitrary precision. The \textit{value} argument gives the integer in the format native to \textit{libtommath}. As with other \textbf{Tcl\_SetFooObj} routines, the \textbf{Tcl\_Obj} having its value set must be unshared (copy on write). As with \textbf{Tcl\_NewBignumObj}, the \textit{value} argument is cleared on return and the digit list is owned by Tcl thereafter.

void \textbf{Tcl\_SetBignumObj}(Tcl\_Obj *\textit{objPtr}, mp\_int *\textit{value})
\item[Tcl\_GetBignumFromObj]
Interprets an object as a large integer, and constructs a copy of that large integer in the \textit{value} argument. Returns \textit{TCL\_OK} if successful. On failure, stores an error message in the result of \textit{interp} and returns \textit{TCL\_ERROR}.

int \textbf{Tcl\_GetBignumFromObj}(Tcl\_Interp *\textit{interp}, Tcl\_Obj*\textit{objPtr}, mp\_int *\textit{value})
\item[Tcl\_GetBignumAndClearObj]
Interprets an object as a large integer, and stores the large integer in the \textit{value} argument. Returns \textit{TCL\_OK} if successful. On failure, stores an error message in the result of \textit{interp} and returns \textit{TCL\_ERROR}. Calls \textbf{Tcl\_Panic} if the object is shared. The object is reset to the empty state prior to return from this call.

int \textbf{Tcl\_GetBignumAndClearObj}(Tcl\_Interp *\textit{interp}, Tcl\_Obj*\textit{objPtr}, mp\_int *\textit{value})
\end{description}

The memory management of these routines deserves some explanation. For performance, it is desirable that copying bignums be avoided as far as possible. For this reason, the digit array stored in the \textit{mp\_int} object will be stored by pointer in the Tcl internal representation. This will result in memory problems unless the \textit{mp\_int} appears to be destroyed after it has been placed in a Tcl object, since further calls to the \textit{libtommath} functions may reallocate the array.

Similarly, when retrieving a large integer from an object, if the object retains an internal representation with the \textit{mp\_int}, the \textit{mp\_int} must be copied. Code that intends to overwrite or destroy an unshared object immediately can avoid the copy by using the call that clears the object; this call returns the original \textit{mp\_int} without needing to do any memory management.
\setcounter{enumi}{2}
\item{}
The \textit{internalRep} union in the \textit{Tcl\_Obj} structure shall be augmented with a \textit{ptrAndLongRep} field. This field shall be a structure comprising a pointer and a long integer. The pointer will designate an array of digits (the \textit{dp} member of an \textit{mp\_int} structure in \textit{libtommath}). The long integer will be an encoding comprising:
\begin{itemize}
\item{}
one bit representing the sign of the number
\item{}
fifteen bits giving the size of allocated memory in the digit array.
\item{}
fifteen bits giving the size of used memory in the digit array.
\end{itemize}

The reason for this tight encoding is that the size of a \textit{Tcl\_Obj} shall not change, and yet an \textit{mp\_int} structure can be stored in the internal representation. This encoding will allow packing and unpacking the object with a few Boolean operations and shifts rather than needing to use a pointer internal representation and dynamically allocated memory to store the \textit{mp\_int}. This packed representation is adequate to represent integers of (2**15 --- 1) \textit{mp\_digit}s (or 917,476 bits, since libtommath does its arithmetic in base 2**28). Since cryptographic algorithms, floating point conversions, and most number theoretic work will not exceed this length, the packed representation should cover nearly all values used in practice.

When an \textit{mp\_int} value too large for that tight packing is to be stored as the internal rep of a \textit{Tcl\_Obj}, a copy of the \textit{mp\_int} value will be allocated from the heap, and a pointer to the copy stored in the pointer field. A value of \textbf{-1} in the long integer field will indicate this unpacked storage option.
\setcounter{enumi}{3}
\item{}
A new \textit{tclBignumType} (with type name \textbf{bignum}) shall be added to the internal set of object types; it will have the \textit{ptrAndLongRep} internal representation, and the usual four conversion routines.
\setcounter{enumi}{4}
\item{}
The \textit{wideIntRep} field in the \textit{internalRep} union shall remain (lest there be extensions that use it), but all code in the Tcl library that deals with it shall be removed. The routines, \textit{Tcl\_GetWideIntFromObj}, \textit{Tcl\_SetWideIntObj}, and \textit{Tcl\_NewWideIntObj} shall remain as part of the API, but will create objects with either \textit{int} or \textit{bignum} internal representations.
\setcounter{enumi}{5}
\item{}
The \textbf{expr} command shall be reworked so that all integer arithmetic is performed \textit{as if} all integers are of arbitrary precision. In practice, numbers between LONG\_MIN and LONG\_MAX shall be stored in native long integers. The operators shall perform type promotions as needed. The command shall avoid (as far as practicable) performing arbitrary-precision arithmetic when native long ints are presented. Specifically:
\begin{itemize}
\item{}
Mixed mode arithmetic with floating point numbers shall work as it does today; the argument that is not a floating point number shall be converted. Note that it will be possible for conversion to overflow the floating-point arithmetic range; in this case, the value shall be replaced with \textit{Inf}.
\end{itemize}

For arithmetic involving only integers:
\begin{itemize}
\item{}
The unary '-' operator shall promote LONG\_MIN to a mp\_int; this is the only input that can cause it to overflow.
\item{}
The unary '+' and '!' operators require no promotion; '+' does nothing but verify that its argument is numeric, and '!' simply tests whether its argument is zero.
\item{}
The binary '**' operator (and the \textit{pow} function) shall promote to an arbitrary precision integer conservatively: when computing 'a**b', it will precompute 'ceil(log2(a)*b)', and promote if this logarithm exceeds LONG\_BITS-1. The result shall be demoted to an integer if it fits.
\item{}
The binary '*' operator, if either argument is an arbitary-precision integer, shall promote the other to an arbitrary-precision integer. If both are native integers, and \textit{Tcl\_WideInt} is at least twice the width of a native \textit{long}, then the product will be computed in a \textit{Tcl\_WideInt}, and the result will be either demoted to a \textit{long} (if it fits) or promoted to an \textit{mp\_int}.

In the case where \textit{Tcl\_WideInt} is not at least twice the width of \textit{long}, the product will be computed to arbitrary precision and then demoted if it fits in a \textit{long}. \textit{This case is the only identified place where arbitrary-precision arithmetic will be used on native integers.}
\item{}
The binary '/' operator, if either argument is an arbitrary-precision integer, shall promote the other. If the quotient fits in a \textit{long}, it shall be demoted.
\item{}
The binary '\%' operator, in computing \textit{a\%b}, shall do the following:
\begin{itemize}
\item{}
If \textit{a} and \textit{b} are both native long integers, the result is also a native long integer.
\item{}
If \textit{a} is a native long integer but \textit{b} is an arbitrary-precision integer, then \textit{a\tiplangle{}b}, and \textit{a\%b} can be computed without division.
\item{}
If \textit{b} is a native \textit{long}, the division will be carried out using the arbitrary precision library, but the result will always be a native \textit{long.}
\item{}
If \textit{a} and \textit{b} are both arbitrary-precision integers, the result will be computed to arbitrary precision, but demoted if it fits in a \textit{long}.
\end{itemize}
\item{}
The binary \textit{+} and \textit{-} operators, if either operand is an arbitrary-precision integer, shall promote the other operand to an arbitrary-precision integer, compute the result to arbitrary precision, and demote the result to \textit{long} if it fits. If both operands are native \textit{long}, and \textit{Tcl\_WideInt} is larger than a native \textit{long}, then the result will be computed to \textit{Tcl\_WideInt} precision, and demoted to \textit{long} if it fits.

In the case where \textit{Tcl\_WideInt} is only as wide as \textit{long}, the operators shall test for overflow when adding numbers of like sign or subtracting numbers of opposite sign. If the sign of the result of one of these operations differs from the sign of the first operand, overflow has occurred; the result is promoted to an arbitrary precision integer and the sign is restored.
\item{}
The \textit{\tiplangle{}\tiplangle{}} operator shall fail if its second operand is an arbitrary-precision integer and the first is nonzero (because this case must exceed the allowable number of digits). It returns an arbitrary-precision integer if its first argument is an arbitrary-precision integer, or if the shift will overflow. The overflow check for \textit{long} values (\textit{a\tiplangle{}\tiplangle{}b}) is
\begin{itemize}
\item{}
if \textit{b}\tiprangle{}LONG\_BITS-1, overflow.
\item{}
if \textit{a}\tiprangle{}0, and (a \& -(1\tiplangle{}\tiplangle{}(LONG\_BITS-1-b))), overflow.
\item{}
if \textit{a}\tiplangle{}0, and (\~{}a \& -(1\tiplangle{}\tiplangle{}LONG\_BITS-1-b))), overflow.
\end{itemize}
\item{}
The '\tiprangle{}\tiprangle{}' operator \textit{a\tiprangle{}\tiprangle{}b}, shall return 0 (\textit{a\tiprangle{}=0}) or -1 (\textit{a\tiplangle{}0}) if \textit{b} is an arbitrary-precision integer (it would have shifted away all significant bits). Otherwise, the shift shall be performed to the precision of \textit{a}, and if \textit{a} was an arbitrary-precision integer, the result shall be demoted to a \textit{long} if it fits.
\item{}
The six comparison operators \tiplangle{}, \tiplangle{}=, ==, !=, \tiprangle{}=, and \tiprangle{}, can work knowing only the signs of the operands wben native \textit{long} values are compared with arbitrary-precision integers. Arbitrary-precision comparison is needed only when comparing arbitrary-precision integers of like sign. In any case, the result is a native \textit{long}.
\item{}
The \textit{eq}, \textit{ne}, \textit{in}, and \textit{ni} operators work only on string representations and will not change.
\item{}
The \textit{\&\&} and \textit{\tipbar{}\tipbar{}} and \textit{?:} operators only test their operands for zero and will not change.
\item{}
The \~{} operator shall follow the algebraic identity:

\~{}a == -a --- 1

This identity holds if \textit{a} is represented in any word size large enough to hold it without overflowing. It therefore generalizes to integers of arbitary precision; essentially, negative numbers are thought of as ``twos-complement numbers with an infinite number of 1 bits at the most significant end.''
\item{}
The base case of the \textit{\&} (\textit{a\&b}) operator shall be defined in the obvious way if \textit{a} and \textit{b} are both positive; corresponding bits of their binary representation will be ANDed together. For negative operands, the algebraic identity above, together with De Morgan's laws, can reduce the operation to the base case:
\begin{itemize}
\item{}
if a\tiprangle{}=0, b\tiplangle{}0, a\&b == a \& \~{}( \~{}b ) == a \& ( --- b --- 1 )
\item{}
if a\tiplangle{}0, b\tiprangle{}=0, symmetric with the above.
\item{}
if a\tiplangle{}0, b\tiplangle{}0, a \& b = \~{}( \~{}a \tipbar{} \~{}b ) = -( ( -a --- 1 ) \tipbar{} ( -b --- 1 ) ) --- 1
\end{itemize}
\item{}
The base case of the \textit{\tipbar{}} (\textit{a\tipbar{}b}) operator shall be defined in the obvious way if \textit{a} and \textit{b} are both positive: corresponding bits of their binary representation will be ORed together. For negative operands, the algebraic identity above, together with De Morgan's laws, can reduce the operation to the base case:
\begin{itemize}
\item{}
if a\tiprangle{}=0, b\tiplangle{}0, a\tipbar{}b == \~{}( \~{}a \& \~{}b ) == -( \~{}a \& ( -b --- 1 )) --- 1
\item{}
if a\tiplangle{}0, b\tiprangle{}=0, symmetric with the above.
\item{}
if a\tiplangle{}0, b\tiplangle{}0, a\tipbar{}b == \~{}( \~{}a \& \~{}b ) == -( ( -a --- 1 ) \& ( -b --- 1 ) ) --- 1
\end{itemize}
\item{}
The base case of the \textit{\textasciicircum{}} (\textit{a\textasciicircum{}b}) operator shall be defined in the obvious way if \textit{a} and \textit{b} are both positive: corresponding bits of their binary representation will be EXCLUSIVE ORed together. For negative operands, the algebraic identity above, together with the contrapositive law, can reduce the operation to the base case:
\begin{itemize}
\item{}
if a\tiprangle{}=0, b\tiplangle{}0, a\textasciicircum{}b == \~{}( a \textasciicircum{} \~{}b ) == -( a \textasciicircum{} ( -b --- 1 ) ) --- 1
\item{}
if a\tiplangle{}0, b\tiprangle{}=0, symmetric with the above.
\item{}
if a\tiplangle{}0, b\tiplangle{}0, a\textasciicircum{}b == \~{}a \textasciicircum{} \~{}b == ( -a --- 1 ) \textasciicircum{} ( -b --- 1 )
\end{itemize}
\item{}
The abs(), ceil(), double(), floor(), int(), round(), sqrt(), and wide() math functions shall all be modified to accept arbitrary-precision integers as parameters. All these functions will continue to return the same ``type'' as they do now (integer vs. floating point), but the domain and/or range will be extended to permit arbitrarily large integers as appropriate.
\item{}
A new function, \textit{entier(\$x)} will be introduced; the function coerces \textit{\$x} to an integer of appropriate size. \textit{entier()} is distinguished from \textit{int()} in that \textit{int()} results in an integer limited to the size of the native long integer always, while \textit{entier()} results in whatever size of integer is needed to hold the full value.
\end{itemize}
\setcounter{enumi}{6}
\item{}
The \textbf{incr} and \textbf{dict incr} commands shall work on arbitary-precision values. Specifically, [incr a \$n] will behave like [set a [expr \{ \$a + \$n \}]] with the constraint that \textit{\$a} and \textit{\$b} must both be integers.
\setcounter{enumi}{7}
\item{}
The \textbf{format} and \textbf{scan} commands will acquire \textit{\%lld}, \textit{\%lli}, \textit{\%llo}, \textit{\%llx} and \textit{\%llX} specifiers that format their arguments as arbitrary-precision decimal, (decimal/any format), octal, and hexadecimal integers, respectively. The format specifier \textit{\%llu} is invalid and will cause an error. The \textit{\%llo} and \textit{\%llx} specifiers, unlike their native-integer counterparts, will format \textit{signed} numbers; the result of [format \%\#llx -12345] will not be 0xffffcfc7, but rather -0x3039. (If an application requires hexadecimal numbers in two's complement notation, it can get them by forcing a number to be positive:
\begingroup\small\begin{verbatim}
     set x -12345
     set x64bit [expr { $x & ((1<<64) - 1) }]
     format %#llx $x64bit
\end{verbatim}
\endgroup

will yield '0xffffffffffffcfc7'.
\setcounter{enumi}{8}
\item{}
User defined math functions will be able to gain access to a \textit{bignumValue} only if they are created using the techniques described in \cite{tip232}. The Tcl command that implements the user defined math function will be able to receive a \textbf{bignum} Tcl\_Obj value just as it can receive any other Tcl\_ObjType. The legacy \textit{Tcl\_Value} structure, will \textbf{not} be updated to add a bignum-valued field.
\setcounter{enumi}{9}
\item{}
The number parser detailed in \cite{tip249} will be adopted into the Tcl internals. See \cite{tip249} for details on the implications.
\end{enumerate}

\section{Integration Details}

The \textit{libtommath} source code shall be extracted from the distributions available at [\url{http://math.libtomcrypt.com/}] and brought into the CVS repository as a \textit{vendor branch} (see [\url{https://www.cvshome.org/docs/manual/cvs-1.11.18/cvs_13.html\#SEC103}] for a discussion of managing vendor branches. It appears that all the necessary modifications to integrate this source code with Tcl can be made in the single file, \textit{tommath.h}; it is likely that the \textit{tools/} directory in the Tcl source tree will contain a Tcl script to make the modifications automatically when importing a new version. CVS can maintain local modifications effectively, should we find it necessary to patch the other sources.

The chief modification is that all the external symbols in the library will have TclBN prepended to their names, so that they will not give linking conflicts if an extension uses a 'tommath' library not supplied with Tcl --- or uses any other library compatible with the Berkeley \textit{mp} API.

\section{Choice of Library}

The \textit{libtommath} code is chosen from among a fairly large set of possible third-party bignum codes. Among the ones considered were:
\begin{description}
\item[GMP]
The Gnu GMP library is probably the fastest and most complete of the codes available. Alas, it is licensed under LGPL, which would require all distributors who prepare binary Tcl distributions to include the GMP source code. I chose to avoid any legal entanglements, and avoided GMP for this reason.
\item[The original Berkeley mp library]
This library is atrociously slow, and was avoided for that reason.
\item[Gnu Calc]
GPL licensed. A non-starter for that reason.
\item[mpexpr]
This Tcl extension has been available for many years and is released under the BSD license. (It was the basis for Gnu Calc but predates the fork, and hence is not GPL-contaminated.) It would certainly be a possibility, but the code is not terribly well documented, is slow by today's standards, and still uses string-based Tcl API's. It would be a considerable amount of work to bring it into conformance with today's Tcl core engineering practices.
\item[OpenSSL]
The OpenSSL library includes a fast and well-documented bignum library (developed so that OpenSSL can do RSA cryptography). Alas, the code is licensed under the original BSD license agreement, and has several parties added to the dreaded Advertising Clause. The Advertising Clause would present serious difficulties for our distributors, and so OpenSSL is not suitable. (The OpenSSL developers are not amenable to removing the Advertising Clause from the license.)
\end{description}

Several libraries implemented in C++ were dismissed out of hand, because of the deployment issues associated with C++ runtime libraries and static constructors.

The \textit{libtommath} code is released to the Public Domain. Its author, Tom St. Denis, explicitly and irrevocably authorizes its use for any purpose, without fee, and without attribution. So license incompatibilities aren't going to be an issue. The documentation is wonderful (Tom has written a book of several hundred pages [\url{http://book.libtomcrypt.com/draft/tommath.pdf}] describing the algorithms used), and the code is lucid.

The chief disadvantage to \textit{libtommath} is that it is a one-person effort, and hence has the risk of becoming orphaned. I consider this risk to be of acceptable severity, because of the quality of the code. The Tcl maintainers could take it on quite readily.

\section{Additional Possibilities}

The Tcl library will actually use only about one-third of \textit{libtommath} to implement the \textit{expr} command. In particular, the library functions for squaring, fast modular reduction, modular exponentiation, greatest common divisor, least common multiple, Jacobi symbol, modular inverse, primality testing and the solution of linear Diophantine equations are not needed, and shall not be include in the Tcl library to save memory footprint.

Nevertheless, these operations would be extremely useful in an extension, so that programs that don't live with tight memory constraints can do things like fast Diffie-Hellman or RSA cryptography. We should probably consider a 'bignum' package to be bundled with the core distribution that would add appropriate math functions wrapping these operations.

\section{Reference Implementation}

A feature complete implementation is present on the CVS branch called 'kennykb-numerics-branch'.

\section{Copyright}

Copyright (c) 2005 by Kevin B. Kenny. All rights reserved.

This document may be distributed subject to the terms and conditions set forth in the Open Publication License, version 1.0 [\url{http://www.opencontent.org/openpub/}].

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#4}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#72]{tip72}
Donal K. Fellows,
\emph{64-Bit Value Support for Tcl on 32-Bit Platforms},
on-line at \url{http://purl.org/tcl/tip/72.html}
\bibitem[TIP \#132]{tip132}
Kevin Kenny,
Donal K. Fellows,
\emph{Revised Floating-Point Conversions in Tcl},
on-line at \url{http://purl.org/tcl/tip/132.html}
\bibitem[TIP \#232]{tip232}
Arjen Markus,
Kevin Kenny,
\emph{Creating New Math Functions for the 'expr' Command},
on-line at \url{http://purl.org/tcl/tip/232.html}
\bibitem[TIP \#249]{tip249}
Kevin B. Kenny,
David S. Cargo,
Don Porter,
\emph{Unification of Tcl's Parsing of Numbers},
on-line at \url{http://purl.org/tcl/tip/249.html}
\end{thebibliography}
\end{document}
