/usr/share/doc/skalibs-doc/libbiguint.html is in skalibs-doc 0.47-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | <?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Language" content="en" />
<title>skalibs: the biguint library interface</title>
<meta name="Description" content="skalibs: the biguint library interface" />
<meta name="Keywords" content="skalibs biguint libbiguint library interface" />
<!-- <link rel="stylesheet" type="text/css" href="http://www.skarnet.org/default.css" /> -->
</head>
<body>
<p>
<a href="index.html">skalibs</a><br />
<a href="http://www.skarnet.org/software/">Software</a><br />
<a href="http://www.skarnet.org/">www.skarnet.org</a>
</p>
<h1> The <tt>biguint</tt> library interface </h1>
<p>
<tt>biguint</tt> is set of simple primitives performing arithmetical
operations on (unsigned) integers of arbitrary length. It is nowhere
near as powerful or efficient as specialized,
assembly language-optimized libraries such as
<a href="http://www.swox.com/gmp/">GMP</a>, but it has the advantages
of smallness and simplicity. <tt>biguint</tt> was developed for use in
<a href="http://www.skarnet.org/software/minssl/">minssl</a>; now that
it provides every function that <tt>minssl</tt> needs, no feature will
most probably be added.
</p>
<h2> Compiling </h2>
<ul>
<li> Add <tt>/package/prog/skalibs/include</tt> to your header directory list </li>
<li> Use <tt>#include "uint32.h"</tt> and <tt>#include "biguint.h"</tt></li>
</ul>
<h2> Linking </h2>
<ul>
<li> Define a global variable <tt>PROG</tt> of type <tt>char const *</tt>
that contains the name of your executable. </li>
<li> Link against <tt>/package/prog/skalibs/library/libbiguint.a</tt> and
<tt>/package/prog/skalibs/library/libstddjb.a</tt>. </li>
</ul>
<h2> Programming </h2>
<p>
You should refer to the <tt>biguint.h</tt> header for the exact function
prototypes.
</p>
<h3> <a name="defs" />
Definitions </h3>
<ul>
<li> A <em>biguint</em> <tt>x</tt> is a pointer to an array <tt>u</tt>
of uint32, together with an unsigned integer <tt>n</tt> called its <em>length</em>.
<br><tt>x = (2^32)^0 * u[0] + (2^32)^1 * u[1] + ... + (2^32)^(n-1) * u[n-1]</tt>.
<br> <tt>n</tt> must be lesser than BIGUINT_MAXLIMBS, which is currently 64. </li>
<li> Every <tt>u[i]</tt> is called a <em>limb</em>. </li>
<li> The greatest integer <tt>i</tt> lesser than <tt>n</tt> for which
<tt>u[i]</tt> is non-zero is called the <em>order</em> of <tt>x</tt>. The
order of zero is 0. </li>
</ul>
<h3> <a name="basic" />
Basic operations </h3>
<h4> Creating a biguint </h4>
<p>
Just declare <tt>uint32 x[BIGUINT_MAXLIMBS] ;</tt> . In the following,
we will refer to a biguint as a <tt>uint32 *</tt>; remember that it
must be pre-allocated.
</p>
<h4> Setting it to zero </h4>
<pre>
uint32 *x ;
unsigned int n ;
bu_zero(x, n) ;
</pre>
<p>
<tt>bu_zero()</tt> sets the first <tt>n</tt> limbs of <tt>x</tt> to zero.
</p>
<h4> Copying a biguint </h4>
<pre>
uint32 const *x ;
uint32 *y ;
unsigned int n ;
bu_copy(y, x, n) ;
</pre>
<p>
<tt>bu_copy()</tt> will copy the first <tt>n</tt> limbs from <tt>x</tt>
to <tt>y</tt>.
</p>
<h4> Calculating the order </h4>
<pre>
uint32 const *x ;
unsigned int n ;
unsigned int r ;
r = bu_len(x, n) ;
</pre>
<p>
<tt>bu_len()</tt> outputs the order of <tt>x</tt> of length <tt>n</tt>.
<tt>0 <= r <= n</tt>.
</p>
<h4> Comparing two biguints </h4>
<pre>
uint32 const *a ;
uint32 const *b ;
unsigned int n ;
int r ;
r = bu_cmp(a, b, n) ;
</pre>
<p>
<tt>bu_cmp()</tt> returns -1 if <tt>a < b</tt>, 1 if
<tt>a > b</tt>, and 0 if <tt>a = b</tt>.
<tt>a</tt> and <tt>b</tt> must have the same length <tt>n</tt>.
</p>
<h3> <a name="io" />
I/O operations </h3>
<h4> Writing a biguint as an array of bytes </h4>
<pre>
char *s ;
uint32 const *x ;
unsigned int n ;
bu_pack(s, x, n) ;
bu_pack_big(s, x, n) ;
</pre>
<p>
<tt>bu_pack()</tt> writes <tt>4*n</tt> bytes to <tt>s</tt>. The bytes
are a little-endian representation of <tt>x</tt>.<br />
<tt>bu_pack_big()</tt> is the same, with a big-endian representation.
</p>
<h4> Reading a biguint from an array of bytes </h4>
<pre>
char const *s ;
uint32 *x ;
unsigned int n ;
bu_unpack(s, x, n) ;
bu_unpack_big(s, x, n) ;
</pre>
<p>
<tt>bu_unpack()</tt> reads <tt>4*n</tt> little-endian bytes from <tt>s</tt>
and builds the corresponding biguint <tt>x</tt>. <br />
<tt>bu_unpack_big()</tt> is the same, but the bytes are interpreted as
big-endian.
</p>
<h4> Formatting a biguint for readable output </h4>
<pre>
char *s ;
uint32 const *x ;
unsigned int n ;
bu_fmt(s, x, n) ;
</pre>
<p>
<tt>bu_fmt()</tt> writes <tt>x</tt> in <tt>s</tt> as a standard big-endian
hexadecimal number. <tt>x</tt> is considered of length <tt>n</tt>, so
<tt>8*n</tt> bytes will be written to <tt>s</tt>, even if it <tt>x</tt>
starts with zeros.
</p>
<h4> Reading a biguint from readable format </h4>
<pre>
char const *s ;
uint32 *x ;
unsigned int n ;
unsigned int r ;
r = bu_scan(s, x, &n) ;
</pre>
<p>
<tt>bu_scan()</tt> is the inverse of <tt>bu_fmt()</tt>: some
bytes are read from <tt>s</tt>, and they build a biguint <tt>x</tt> of
computed length <tt>n</tt>. The reading stops at the first byte encountered
that is not in the 0-9, A-F or a-f range. <tt>bu_scan()</tt> returns the
number of bytes read.
</p>
<h3> <a name="arith" />
Arithmetic operations </h3>
<h4> Addition </h4>
<pre>
uint32 const *a ;
uint32 const *b ;
uint32 *c ;
unsigned int n ;
unsigned char carrybefore ;
unsigned char carryafter ;
carryafter = bu_addc(c, a, b, n, carrybefore) ;
carryafter = bu_subc(c, a, b, n, carrybefore) ;
</pre>
<p>
<tt>bu_addc()</tt> adds <tt>a</tt> and <tt>b</tt>, and puts the result
into <tt>c</tt>. <tt>a</tt> and <tt>b</tt> must have the same length,
<tt>n</tt>; after the addition, <tt>c</tt> has length <tt>n</tt>.
<tt>carrybefore</tt> must be 0 or 1; if it is 1, then <tt>b+1</tt> is
used instead of <tt>b</tt>. If <tt>c</tt> doesn't fit in <tt>n</tt>
limbs, then the <tt>n</tt> least significant limbs are kept, and
<tt>bu_addc()</tt> returns 1. Else it returns 0. <br />
<tt>bu_subc()</tt> is the same, with substraction. If <tt>c</tt>
should be negative, then <tt>c</tt> is really <tt>(2^32)^n - c</tt>
and <tt>bu_subc()</tt> returns 1.<br />
<tt>bu_add(c, a, b, n)</tt> is a macro for <tt>bu_addc(c, a, b, n, 0)</tt>.<br />
<tt>bu_sub(c, a, b, n)</tt> is a macro for <tt>bu_subc(c, a, b, n, 0)</tt>.<br />
</p>
<h4> Multiplication </h4>
<pre>
uint32 const *a ;
uint32 const *b ;
uint32 *c ;
unsigned int an, bn ;
bu_mul(c, a, an, b, bn) ;
</pre>
<p>
<tt>bu_mul()</tt> computes <tt>c=a*b</tt>. <tt>a</tt>'s length is <tt>an</tt>;
<tt>b</tt>'s length is <tt>bn</tt>; <tt>c</tt>'s length will be <tt>an+bn</tt>.
</p>
<h4> Division </h4>
<pre>
uint32 const *a ;
uint32 const *b ;
uint32 *q ;
uint32 *r ;
unsigned int n ;
bu_div(a, b, q, r, n) ;
bu_mod(a, b, n) ;
</pre>
<p>
<tt>bu_div()</tt> computes <tt>q</tt>, the quotient, and <tt>r</tt>, the
remainder, of <tt>a</tt> divided by <tt>b</tt>. If <tt>b</tt> is zero,
a SIGFPE is raised: this is intentional.<br />
<tt>bu_mod()</tt> computes only the remainder, and stores it into <tt>a</tt>.
</p>
<h4> Left-shifts and right-shifts </h4>
<pre>
uint32 *x ;
unsigned int n ;
unsigned char carryafter ;
unsigned char carrybefore ;
carryafter = bu_slbc(x, n, carrybefore) ;
carryafter = bu_srbc(x, n, carrybefore) ;
</pre>
<p>
<tt>bu_slbc()</tt> computes <tt>x <<= 1</tt>.
The least significant bit of <tt>x</tt> is then set to
<tt>carrybefore</tt>. <tt>bu_slbc()</tt> returns the
previous value of <tt>x</tt>'s most significant bit. <br />
<tt>bu_srbc()</tt> computes <tt>x >>= 1</tt>.
The most significant bit of <tt>x</tt> is then set to
<tt>carrybefore</tt>. <tt>bu_slbc()</tt> returns the
previous value of <tt>x</tt>'s least significant bit.<br />
<tt>bu_slb(x, n)</tt> and <tt>bu_srb(x, n)</tt> are macros for
respectively <tt>bu_slbc(x, n, 0)</tt> and <tt>bu_srbc(x, n, 0)</tt>.
</p>
<h4> Modular operations </h4>
<pre>
uint32 const *a ;
uint32 const *b ;
uint32 *c ;
uint32 const *m ;
unsigned int n ;
bu_addmod(c, a, b, m, n) ;
bu_submod(c, a, b, m, n) ;
bu_divmod(c, a, b, m, n) ;
bu_invmod(c, m, n) ;
</pre>
<p>
<tt>bu_addmod()</tt> computes <tt>c = (a+b) mod m</tt>.<br />
<tt>bu_submod()</tt> computes <tt>c = (a-b) mod m</tt>.<br />
<tt>a</tt>, <tt>b</tt> and <tt>m</tt> must have the same length <tt>n</tt>.
<tt>a</tt> and <tt>b</tt> must already be numbers modulo <tt>m</tt>.
</p>
<p>
<tt>bu_divmod()</tt> computes <tt>a</tt> divided by <tt>b</tt> modulo
<tt>m</tt> and stores it into <tt>c</tt>. <br />
<tt>bu_invmod()</tt> computes the inverse of <tt>c</tt> modulo <tt>m</tt>
and stores it into <tt>c</tt>. <br />
<strong>The divisor and <tt>m</tt> must be relatively prime</strong>, else
those functions loop forever. <br />
The algorithm for modular division and inversion is due to
<a href="http://research.sun.com/techrep/2001/abstract-95.html">Sheueling
Chang Shantz</a>.
</p>
</body>
</html>
|