3 mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, 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
11 .ta +\w'\fLCRTpre* \fP'u
19 void mpsetminbits(int n)
22 void mpbits(mpint *b, int n)
28 mpint* mpcopy(mpint *b)
31 void mpassign(mpint *old, mpint *new)
34 mpint* mprand(int bits, void (*gen)(uchar*, int), mpint *b)
37 mpint* mnprand(mpint *n, void (*gen)(uchar*, int), mpint *b)
40 mpint* strtomp(char *buf, char **rptr, int base, mpint *b)
43 char* mptoa(mpint *b, int base, char *buf, int blen)
49 mpint* betomp(uchar *buf, uint blen, mpint *b)
52 int mptobe(mpint *b, uchar *buf, uint blen, uchar **bufp)
55 mpint* letomp(uchar *buf, uint blen, mpint *b)
58 int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp)
64 mpint* uitomp(uint, mpint*)
70 mpint* itomp(int, mpint*)
73 mpint* vtomp(vlong, mpint*)
79 mpint* uvtomp(uvlong, mpint*)
85 void mpadd(mpint *b1, mpint *b2, mpint *sum)
88 void mpmagadd(mpint *b1, mpint *b2, mpint *sum)
91 void mpsub(mpint *b1, mpint *b2, mpint *diff)
94 void mpmagsub(mpint *b1, mpint *b2, mpint *diff)
97 void mpleft(mpint *b, int shift, mpint *res)
100 void mpright(mpint *b, int shift, mpint *res)
103 void mpmul(mpint *b1, mpint *b2, mpint *prod)
106 void mpexp(mpint *b, mpint *e, mpint *m, mpint *res)
109 void mpmod(mpint *b, mpint *m, mpint *remainder)
112 void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient,
118 int mpcmp(mpint *b1, mpint *b2)
121 int mpmagcmp(mpint *b1, mpint *b2)
124 void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x,
130 void mpinvert(mpint *b, mpint *m, mpint *res)
133 int mpsignif(mpint *b)
136 int mplowbits0(mpint *b)
139 void mpdigdiv(mpdigit *dividend, mpdigit divisor,
145 void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen,
151 void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen,
157 void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p)
160 int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p)
163 void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen,
169 int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen)
172 CRTpre* crtpre(int nfactors, mpint **factors)
175 CRTres* crtin(CRTpre *crt, mpint *x)
178 void crtout(CRTpre *crt, CRTres *r, mpint *x)
181 void crtprefree(CRTpre *cre)
184 void crtresfree(CRTres *res)
187 mpint *mpzero, *mpone, *mptwo
190 These routines perform extended precision integer arithmetic.
193 which points to an array of
195 stored in little-endian order:
198 typedef struct mpint mpint;
201 int sign; /* +1 or -1 */
202 int size; /* allocated digits */
203 int top; /* significant digits */
213 is architecture-dependent and defined in
214 .BR /$cputype/include/u.h .
216 are dynamically allocated and must be explicitly freed. Operations
217 grow the array of digits as needed.
219 In general, the result parameters are last in the
222 Routines that return an
226 if the result parameter is
234 These functions, in addition to
240 if the allocation fails.
242 Input and result parameters may point to the same
244 The routines check and copy where necessary.
249 with an initial allocation of
254 is zero, the allocation will be whatever was specified in the
257 or to the initial value, 1056.
262 grows the allocation of
272 increases it to do so.
273 Unless you are writing new basic operations, you
274 can restrict yourself to
280 normalizes the representation by trimming any high order zero
281 digits. All routines except
283 return normalized results.
288 with the same value as
300 bit random number using the generator
303 takes a pointer to a string of uchar's and the number
309 to generate a uniform random number
311 .if t 0 ≤ \fIx\fR < \fIn\fR.
321 representations using the base indicated.
322 Only the bases 10, 16, 32, and 64 are
323 supported. Anything else defaults to 16.
325 skips any leading spaces or tabs.
327 scan stops when encountering a digit not valid in the
332 is set to point to the character immediately after the
334 If the parse terminates before any digits are found,
339 returns a pointer to the filled buffer.
344 the buffer is allocated.
350 to print hexadecimal representations of
352 The conventional verb is
364 to a byte array. The former creates a big endian representation,
365 the latter a little endian one.
370 it specifies the buffer of length
372 for the result. If the representation
375 bytes, the rest of the buffer is zero filled.
380 then a buffer is allocated and a pointer to it is
381 deposited in the location pointed to by
383 Sign is ignored in these conversions, i.e., the byte
384 array version is always positive.
389 convert from a big or little endian byte array at
399 it refers to a preallocated
406 a new integer is allocated and returned as the result.
408 The integer conversions are:
412 .BR mpint -> "unsigned int"
415 .BR "unsigned int" -> mpint
424 .BR mpint -> "unsigned vlong"
427 .BR "unsigned vlong" -> mpint
436 When converting to the base integer types, if the integer is too large,
437 the largest integer of the appropriate sign
438 and size is returned.
440 The mathematical functions are:
444 .BR "sum = b1 + b2" .
447 .BR "sum = abs(b1) + abs(b2)" .
450 .BR "diff = b1 - b2" .
453 .BR "diff = abs(b1) - abs(b2)" .
456 .BR "res = b<<shift" .
459 .BR "res = b>>shift" .
470 .BR "res = b**e mod m" .
473 .BR "remainder = b % m" .
476 .BR "quotient = dividend/divisor" .
477 .BR "remainder = dividend % divisor" .
480 returns -1, 0, or +1 as
482 is less than, equal to, or greater than
488 but ignores the sign and just compares magnitudes.
492 computes the greatest common denominator,
503 .BR "a*x + b*y = d" .
508 are required to be positive.
509 If called with negative arguments, it will
513 computes the multiplicative inverse of
519 returns the number of significant bits in
522 returns the number of consecutive zero bits
523 at the low end of the significant bits.
524 For example, for 0x14,
535 The remaining routines all work on arrays of
539 They are the basis of all the other routines. They are separated out
540 to allow them to be rewritten in assembler for each architecture. There
541 is also a portable C version for each one.
545 .BR "quotient = dividend[0:1] / divisor" .
548 .BR "sum[0:alen] = a[0:alen-1] + b[0:blen-1]" .
549 We assume alen >= blen and that sum has room for alen+1 digits.
552 .BR "diff[0:alen-1] = a[0:alen-1] - b[0:blen-1]" .
553 We assume that alen >= blen and that diff has room for alen digits.
556 .BR "p[0:n] += m * b[0:n-1]" .
557 This multiplies a an array of digits times a scalar and adds it to another array.
558 We assume p has room for n+1 digits.
561 .BR "p[0:n] -= m * b[0:n-1]" .
562 This multiplies a an array of digits times a scalar and subtracts it from another array.
563 We assume p has room for n+1 digits. It returns +1 is the result is positive and
567 .BR "p[0:alen*blen] = a[0:alen-1] * b[0:blen-1]" .
568 We assume that p has room for alen*blen+1 digits.
571 This returns -1, 0, or +1 as a - b is negative, 0, or positive.
578 are the constants 2, 1 and 0. These cannot be freed.
579 .SS "Chinese remainder theorem
581 When computing in a non-prime modulus,
583 it is possible to perform the computations on the residues modulo the prime
586 instead. Since these numbers are smaller, multiplication and exponentiation
590 computes the residues of
592 and returns them in a newly allocated structure:
595 typedef struct CRTres CRTres;
597 int n; /* number of residues */
598 mpint *r[n]; /* residues */
603 takes a residue representation of a number and converts it back into
604 the number. It also frees the residue structure.
607 saves a copy of the factors and precomputes the constants necessary
608 for converting the residue form back into a number modulo
609 the product of the factors. It returns a newly allocated structure
619 structures respectively.