Blob


1 .TH MP 3
2 .SH NAME
4 mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, strtomp, mpfmt,mptoa, betomp, mptobe, letomp, mptole, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic
5 .SH SYNOPSIS
6 .B #include <u.h>
7 .br
8 .B #include <libc.h>
9 .br
10 .B #include <mp.h>
11 .PP
12 .B
13 mpint* mpnew(int n)
14 .PP
15 .B
16 void mpfree(mpint *b)
17 .PP
18 .B
19 void mpsetminbits(int n)
20 .PP
21 .B
22 void mpbits(mpint *b, int n)
23 .PP
24 .B
25 void mpnorm(mpint *b)
26 .PP
27 .B
28 mpint* mpcopy(mpint *b)
29 .PP
30 .B
31 void mpassign(mpint *old, mpint *new)
32 .PP
33 .B
34 mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b)
35 .PP
36 .B
37 mpint* strtomp(char *buf, char **rptr, int base, mpint *b)
38 .PP
39 .B
40 char* mptoa(mpint *b, int base, char *buf, int blen)
41 .PP
42 .B
43 int mpfmt(Fmt*)
44 .PP
45 .B
46 mpint* betomp(uchar *buf, uint blen, mpint *b)
47 .PP
48 .B
49 int mptobe(mpint *b, uchar *buf, uint blen, uchar **bufp)
50 .PP
51 .B
52 mpint* letomp(uchar *buf, uint blen, mpint *b)
53 .PP
54 .B
55 int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp)
56 .PP
57 .B
58 uint mptoui(mpint*)
59 .PP
60 .B
61 mpint* uitomp(uint, mpint*)
62 .PP
63 .B
64 int mptoi(mpint*)
65 .PP
66 .B
67 mpint* itomp(int, mpint*)
68 .PP
69 .B
70 mpint* vtomp(vlong, mpint*)
71 .PP
72 .B
73 vlong mptov(mpint*)
74 .PP
75 .B
76 mpint* uvtomp(uvlong, mpint*)
77 .PP
78 .B
79 uvlong mptouv(mpint*)
80 .PP
81 .B
82 void mpadd(mpint *b1, mpint *b2, mpint *sum)
83 .PP
84 .B
85 void mpmagadd(mpint *b1, mpint *b2, mpint *sum)
86 .PP
87 .B
88 void mpsub(mpint *b1, mpint *b2, mpint *diff)
89 .PP
90 .B
91 void mpmagsub(mpint *b1, mpint *b2, mpint *diff)
92 .PP
93 .B
94 void mpleft(mpint *b, int shift, mpint *res)
95 .PP
96 .B
97 void mpright(mpint *b, int shift, mpint *res)
98 .PP
99 .B
100 void mpmul(mpint *b1, mpint *b2, mpint *prod)
101 .PP
102 .B
103 void mpexp(mpint *b, mpint *e, mpint *m, mpint *res)
104 .PP
105 .B
106 void mpmod(mpint *b, mpint *m, mpint *remainder)
107 .PP
108 .B
109 void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder)
110 .PP
111 .B
112 int mpcmp(mpint *b1, mpint *b2)
113 .PP
114 .B
115 int mpmagcmp(mpint *b1, mpint *b2)
116 .PP
117 .B
118 void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y)
119 .PP
120 .B
121 void mpinvert(mpint *b, mpint *m, mpint *res)
122 .PP
123 .B
124 int mpsignif(mpint *b)
125 .PP
126 .B
127 int mplowbits0(mpint *b)
128 .PP
129 .B
130 void mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient)
131 .PP
132 .B
133 void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *sum)
134 .PP
135 .B
136 void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff)
137 .PP
138 .B
139 void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p)
140 .PP
141 .B
142 int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p)
143 .PP
144 .B
145 void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
146 .PP
147 .B
148 int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen)
149 .PP
150 .B
151 CRTpre* crtpre(int nfactors, mpint **factors)
152 .PP
153 .B
154 CRTres* crtin(CRTpre *crt, mpint *x)
155 .PP
156 .B
157 void crtout(CRTpre *crt, CRTres *r, mpint *x)
158 .PP
159 .B
160 void crtprefree(CRTpre *cre)
161 .PP
162 .B
163 void crtresfree(CRTres *res)
164 .PP
165 .B
166 mpint *mpzero, *mpone, *mptwo
167 .SH DESCRIPTION
168 .PP
169 These routines perform extended precision integer arithmetic.
170 The basic type is
171 .BR mpint ,
172 which points to an array of
173 .BR mpdigit s,
174 stored in little-endian order:
175 .sp
176 .EX
177 typedef struct mpint mpint;
178 struct mpint
180 int sign; /* +1 or -1 */
181 int size; /* allocated digits */
182 int top; /* significant digits */
183 mpdigit *p;
184 char flags;
185 };
186 .EE
187 .PP
188 The sign of 0 is +1.
189 .PP
190 The size of
191 .B mpdigit
192 is architecture-dependent and defined in
193 .BR /$cputype/include/u.h .
194 .BR Mpint s
195 are dynamically allocated and must be explicitly freed. Operations
196 grow the array of digits as needed.
197 .PP
198 In general, the result parameters are last in the
199 argument list.
200 .PP
201 Routines that return an
202 .B mpint
203 will allocate the
204 .B mpint
205 if the result parameter is
206 .BR nil .
207 This includes
208 .IR strtomp ,
209 .IR itomp ,
210 .IR uitomp ,
211 and
212 .IR btomp .
213 These functions, in addition to
214 .I mpnew
215 and
216 .IR mpcopy ,
217 will return
218 .B nil
219 if the allocation fails.
220 .PP
221 Input and result parameters may point to the same
222 .BR mpint .
223 The routines check and copy where necessary.
224 .PP
225 .I Mpnew
226 creates an
227 .B mpint
228 with an initial allocation of
229 .I n
230 bits.
231 If
232 .I n
233 is zero, the allocation will be whatever was specified in the
234 last call to
235 .I mpsetminbits
236 or to the initial value, 1056.
237 .I Mpfree
238 frees an
239 .BR mpint .
240 .I Mpbits
241 grows the allocation of
242 .I b
243 to fit at least
244 .I n
245 bits. If
246 .B b->top
247 doesn't cover
248 .I n
249 bits it increases it to do so.
250 Unless you are writing new basic operations, you
251 can restrict yourself to
252 .B mpnew(0)
253 and
254 .BR mpfree(b) .
255 .PP
256 .I Mpnorm
257 normalizes the representation by trimming any high order zero
258 digits. All routines except
259 .B mpbits
260 return normalized results.
261 .PP
262 .I Mpcopy
263 creates a new
264 .B mpint
265 with the same value as
266 .I b
267 while
268 .I mpassign
269 sets the value of
270 .I new
271 to be that of
272 .IR old .
273 .PP
274 .I Mprand
275 creates an
276 .I n
277 bit random number using the generator
278 .IR gen .
279 .I Gen
280 takes a pointer to a string of uchar's and the number
281 to fill in.
282 .PP
283 .I Strtomp
284 and
285 .I mptoa
286 convert between
287 .SM ASCII
288 and
289 .B mpint
290 representations using the base indicated.
291 Only the bases 10, 16, 32, and 64 are
292 supported. Anything else defaults to 16.
293 .IR Strtomp
294 skips any leading spaces or tabs.
295 .IR Strtomp 's
296 scan stops when encountering a digit not valid in the
297 base. If
298 .I rptr
299 is not zero,
300 .I *rptr
301 is set to point to the character immediately after the
302 string converted.
303 If the parse pterminates before any digits are found,
304 .I strtomp
305 return
306 .BR nil .
307 .I Mptoa
308 returns a pointer to the filled buffer.
309 If the parameter
310 .I buf
311 is
312 .BR nil ,
313 the buffer is allocated.
314 .I Mpfmt
315 can be used with
316 .IR fmtinstall (3)
317 and
318 .IR print (3)
319 to print hexadecimal representations of
320 .BR mpint s.
321 .PP
322 .I Mptobe
323 and
324 .I mptole
325 convert an
326 .I mpint
327 to a byte array. The former creates a big endian representation,
328 the latter a little endian one.
329 If the destination
330 .I buf
331 is not
332 .BR nil ,
333 it specifies the buffer of length
334 .I blen
335 for the result. If the representation
336 is less than
337 .I blen
338 bytes, the rest of the buffer is zero filled.
339 If
340 .I buf
341 is
342 .BR nil ,
343 then a buffer is allocated and a pointer to it is
344 deposited in the location pointed to by
345 .IR bufp .
346 Sign is ignored in these conversions, i.e., the byte
347 array version is always positive.
348 .PP
349 .IR Betomp ,
350 and
351 .I letomp
352 convert from a big or little endian byte array at
353 .I buf
354 of length
355 .I blen
356 to an
357 .IR mpint .
358 If
359 .I b
360 is not
361 .IR nil ,
362 it refers to a preallocated
363 .I mpint
364 for the result.
365 If
366 .I b
367 is
368 .BR nil ,
369 a new integer is allocated and returned as the result.
370 .PP
371 The integer conversions are:
372 .TF Mptouv
373 .TP
374 .I mptoui
375 .BR mpint -> "unsigned int"
376 .TP
377 .I uitomp
378 .BR "unsigned int" -> mpint
379 .TP
380 .I mptoi
381 .BR mpint -> "int"
382 .TP
383 .I itomp
384 .BR "int" -> mpint
385 .TP
386 .I mptouv
387 .BR mpint -> "unsigned vlong"
388 .TP
389 .I uvtomp
390 .BR "unsigned vlong" -> mpint
391 .TP
392 .I mptov
393 .BR mpint -> "vlong"
394 .TP
395 .I vtomp
396 .BR "vlong" -> mpint
397 .PD
398 .PP
399 When converting to the base integer types, if the integer is too large,
400 the largest integer of the appropriate sign
401 and size is returned.
402 .PP
403 The mathematical functions are:
404 .TF mpmagadd
405 .TP
406 .I mpadd
407 .BR "sum = b1 + b2" .
408 .TP
409 .I mpmagadd
410 .BR "sum = abs(b1) + abs(b2)" .
411 .TP
412 .I mpsub
413 .BR "diff = b1 - b2" .
414 .TP
415 .I mpmagsub
416 .BR "diff = abs(b1) - abs(b2)" .
417 .TP
418 .I mpleft
419 .BR "res = b<<shift" .
420 .TP
421 .I mpright
422 .BR "res = b>>shift" .
423 .TP
424 .I mpmul
425 .BR "prod = b1*b2" .
426 .TP
427 .I mpexp
428 if
429 .I m
430 is nil,
431 .BR "res = b**e" .
432 Otherwise,
433 .BR "res = b**e mod m" .
434 .TP
435 .I mpmod
436 .BR "remainder = b % m" .
437 .TP
438 .I mpdiv
439 .BR "quotient = dividend/divisor" .
440 .BR "remainder = dividend % divisor" .
441 .TP
442 .I mpcmp
443 returns -1, 0, or +1 as
444 .I b1
445 is less than, equal to, or greater than
446 .IR b2 .
447 .TP
448 .I mpmagcmp
449 the same as
450 .I mpcmp
451 but ignores the sign and just compares magnitudes.
452 .PD
453 .PP
454 .I Mpextendedgcd
455 computes the greatest common denominator,
456 .IR d ,
457 of
458 .I a
459 and
460 .IR b .
461 It also computes
462 .I x
463 and
464 .I y
465 such that
466 .BR "a*x + b*y = d" .
467 Both
468 .I a
469 and
470 .I b
471 are required to be positive.
472 If called with negative arguments, it will
473 return a gcd of 0.
474 .PP
475 .I Mpinverse
476 computes the multiplicative inverse of
477 .I b
478 .B mod
479 .IR m .
480 .PP
481 .I Mpsignif
482 returns the bit offset of the left most 1 bit in
483 .IR b .
484 .I Mplowbits0
485 returns the bit offset of the right most 1 bit.
486 For example, for 0x14,
487 .I mpsignif
488 would return 4 and
489 .I mplowbits0
490 would return 2.
491 .PP
492 The remaining routines all work on arrays of
493 .B mpdigit
494 rather than
495 .BR mpint 's.
496 They are the basis of all the other routines. They are separated out
497 to allow them to be rewritten in assembler for each architecture. There
498 is also a portable C version for each one.
499 .TF mpvecdigmuladd
500 .TP
501 .I mpdigdiv
502 .BR "quotient = dividend[0:1] / divisor" .
503 .TP
504 .I mpvecadd
505 .BR "sum[0:alen] = a[0:alen-1] + b[0:blen-1]" .
506 We assume alen >= blen and that sum has room for alen+1 digits.
507 .TP
508 .I mpvecsub
509 .BR "diff[0:alen-1] = a[0:alen-1] - b[0:blen-1]" .
510 We assume that alen >= blen and that diff has room for alen digits.
511 .TP
512 .I mpvecdigmuladd
513 .BR "p[0:n] += m * b[0:n-1]" .
514 This multiplies a an array of digits times a scalar and adds it to another array.
515 We assume p has room for n+1 digits.
516 .TP
517 .I mpvecdigmulsub
518 .BR "p[0:n] -= m * b[0:n-1]" .
519 This multiplies a an array of digits times a scalar and subtracts it fromo another array.
520 We assume p has room for n+1 digits. It returns +1 is the result is positive and
521 -1 if negative.
522 .TP
523 .I mpvecmul
524 .BR "p[0:alen*blen] = a[0:alen-1] * b[0:blen-1]" .
525 We assume that p has room for alen*blen+1 digits.
526 .TP
527 .I mpveccmp
528 This returns -1, 0, or +1 as a - b is negative, 0, or positive.
529 .PD
530 .PP
531 .IR mptwo ,
532 .I mpone
533 and
534 .I mpzero
535 are the constants 2, 1 and 0. These cannot be freed.
536 .SS "Chinese remainder theorem
537 .PP
538 When computing in a non-prime modulus,
539 .IR n,
540 it is possible to perform the computations on the residues modulo the prime
541 factors of
542 .I n
543 instead. Since these numbers are smaller, multiplication and exponentiation
544 can be much faster.
545 .PP
546 .I Crtin
547 computes the residues of
548 .I x
549 and returns them in a newly allocated structure:
550 .EX
551 typedef struct CRTres CRTres;
553 int n; // number of residues
554 mpint *r[n]; // residues
555 };
556 .EE
557 .PP
558 .I Crtout
559 takes a residue representation of a number and converts it back into
560 the number. It also frees the residue structure.
561 .PP
562 .I Crepre
563 saves a copy of the factors and precomputes the constants necessary
564 for converting the residue form back into a number modulo
565 the product of the factors. It returns a newly allocated structure
566 containing values.
567 .PP
568 .I Crtprefree
569 and
570 .I crtresfree
571 free
572 .I CRTpre
573 and
574 .I CRTres
575 structures respectively.
576 .SH SOURCE
577 .B /usr/local/plan9/src/libmp