3 mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpmodadd, mpmodsub, mpmodmul, mpdiv, mpcmp, mpsel, 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)
25 mpint* mpnorm(mpint *b)
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* mpnrand(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 void mptober(mpint *b, uchar *buf, int blen)
58 mpint* letomp(uchar *buf, uint blen, mpint *b)
61 int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp)
64 void mptolel(mpint *b, uchar *buf, int blen)
70 mpint* uitomp(uint, mpint*)
76 mpint* itomp(int, mpint*)
79 mpint* vtomp(vlong, mpint*)
85 mpint* uvtomp(uvlong, mpint*)
91 void mpadd(mpint *b1, mpint *b2, mpint *sum)
94 void mpmagadd(mpint *b1, mpint *b2, mpint *sum)
97 void mpsub(mpint *b1, mpint *b2, mpint *diff)
100 void mpmagsub(mpint *b1, mpint *b2, mpint *diff)
103 void mpleft(mpint *b, int shift, mpint *res)
106 void mpright(mpint *b, int shift, mpint *res)
109 void mpmul(mpint *b1, mpint *b2, mpint *prod)
112 void mpexp(mpint *b, mpint *e, mpint *m, mpint *res)
115 void mpmod(mpint *b, mpint *m, mpint *remainder)
118 void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient,
124 void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum)
127 void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff)
130 void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod)
133 int mpcmp(mpint *b1, mpint *b2)
136 int mpmagcmp(mpint *b1, mpint *b2)
139 void mpsel(int s, mpint *b1, mpint *b2, mpint *res)
142 void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x,
148 void mpinvert(mpint *b, mpint *m, mpint *res)
151 int mpsignif(mpint *b)
154 int mplowbits0(mpint *b)
157 void mpdigdiv(mpdigit *dividend, mpdigit divisor,
163 void mpvecadd(mpdigit *a, int alen, mpdigit *b, int blen,
169 void mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen,
175 void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p)
178 int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p)
181 void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen,
187 int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen)
190 CRTpre* crtpre(int nfactors, mpint **factors)
193 CRTres* crtin(CRTpre *crt, mpint *x)
196 void crtout(CRTpre *crt, CRTres *r, mpint *x)
199 void crtprefree(CRTpre *cre)
202 void crtresfree(CRTres *res)
205 mpint *mpzero, *mpone, *mptwo
208 These routines perform extended precision integer arithmetic.
211 which points to an array of
213 stored in little-endian order:
216 typedef struct mpint mpint;
219 int sign; /* +1 or -1 */
220 int size; /* allocated digits */
221 int top; /* significant digits */
231 is architecture-dependent and defined in
232 .BR /$cputype/include/u.h .
234 are dynamically allocated and must be explicitly freed. Operations
235 grow the array of digits as needed.
237 In general, the result parameters are last in the
240 Routines that return an
244 if the result parameter is
252 These functions, in addition to
258 if the allocation fails.
260 Input and result parameters may point to the same
262 The routines check and copy where necessary.
267 with an initial allocation of
272 is zero, the allocation will be whatever was specified in the
275 or to the initial value, 1056.
280 grows the allocation of
290 increases it to do so.
291 Unless you are writing new basic operations, you
292 can restrict yourself to
298 normalizes the representation by trimming any high order zero
299 digits. All routines except
301 return normalized results.
306 with the same value as
318 bit random number using the generator
321 takes a pointer to a string of uchar's and the number
327 to generate a uniform random number
329 .if t 0 ≤ \fIx\fR < \fIn\fR.
339 representations using the base indicated.
340 Only the bases 2, 4, 8, 10, 16, 32, and 64 are
341 supported. Base 0 defaults to 16.
343 skips any leading spaces or tabs.
345 scan stops when encountering a digit not valid in the
348 is zero then C-style prefixes are interpreted to
355 for octal. Otherwise decimal is assumed.
359 is set to point to the character immediately after the
361 If the parse terminates before any digits are found,
366 returns a pointer to the filled buffer.
371 the buffer is allocated.
377 to print hexadecimal representations of
379 The conventional verb is
391 to a byte array. The former creates a big endian representation,
392 the latter a little endian one.
397 it specifies the buffer of length
399 for the result. If the representation
402 bytes, the rest of the buffer is zero filled.
407 then a buffer is allocated and a pointer to it is
408 deposited in the location pointed to by
410 Sign is ignored in these conversions, i.e., the byte
411 array version is always positive.
420 into a fixed length byte array.
422 fills the bytes right adjusted in big endian order so that the least
423 significant byte is at
427 fills in little endian order; left adjusted; so that the least
428 significat byte is filled into
434 convert from a big or little endian byte array at
444 it refers to a preallocated
451 a new integer is allocated and returned as the result.
453 The integer conversions are:
457 .BR mpint -> "unsigned int"
460 .BR "unsigned int" -> mpint
469 .BR mpint -> "unsigned vlong"
472 .BR "unsigned vlong" -> mpint
481 When converting to the base integer types, if the integer is too large,
482 the largest integer of the appropriate sign
483 and size is returned.
485 The mathematical functions are:
489 .BR "sum = b1 + b2" .
492 .BR "sum = abs(b1) + abs(b2)" .
495 .BR "diff = b1 - b2" .
498 .BR "diff = abs(b1) - abs(b2)" .
501 .BR "res = b<<shift" .
504 .BR "res = b>>shift" .
515 .BR "res = b**e mod m" .
518 .BR "remainder = b % m" .
521 .BR "quotient = dividend/divisor" .
522 .BR "remainder = dividend % divisor" .
525 returns -1, 0, or +1 as
527 is less than, equal to, or greater than
533 but ignores the sign and just compares magnitudes.
542 is not zero, otherwise
552 .BR "sum = b1+b2 mod m" .
555 .BR "diff = b1-b2 mod m" .
558 .BR "prod = b1*b2 mod m" .
562 computes the greatest common denominator,
573 .BR "a*x + b*y = d" .
578 are required to be positive.
579 If called with negative arguments, it will
583 computes the multiplicative inverse of
589 returns the number of significant bits in
592 returns the number of consecutive zero bits
593 at the low end of the significant bits.
594 For example, for 0x14,
605 The remaining routines all work on arrays of
609 They are the basis of all the other routines. They are separated out
610 to allow them to be rewritten in assembler for each architecture. There
611 is also a portable C version for each one.
615 .BR "quotient = dividend[0:1] / divisor" .
618 .BR "sum[0:alen] = a[0:alen-1] + b[0:blen-1]" .
619 We assume alen >= blen and that sum has room for alen+1 digits.
622 .BR "diff[0:alen-1] = a[0:alen-1] - b[0:blen-1]" .
623 We assume that alen >= blen and that diff has room for alen digits.
626 .BR "p[0:n] += m * b[0:n-1]" .
627 This multiplies a an array of digits times a scalar and adds it to another array.
628 We assume p has room for n+1 digits.
631 .BR "p[0:n] -= m * b[0:n-1]" .
632 This multiplies a an array of digits times a scalar and subtracts it from another array.
633 We assume p has room for n+1 digits. It returns +1 is the result is positive and
637 .BR "p[0:alen+blen] = a[0:alen-1] * b[0:blen-1]" .
638 We assume that p has room for alen+blen+1 digits.
641 This returns -1, 0, or +1 as a - b is negative, 0, or positive.
648 are the constants 2, 1 and 0. These cannot be freed.
649 .SS "Time invariant computation"
651 In the field of cryptography, it is sometimes neccesary to implement
652 algorithms such that the runtime of the algorithm is not depdenent on
653 the input data. This library provides partial support for time
654 invariant computation with the
656 flag that can be set on input or destination operands to request timing
657 safe operation. The result of a timing safe operation will also have the
659 flag set and is not normalized.
660 .SS "Chinese remainder theorem
662 When computing in a non-prime modulus,
664 it is possible to perform the computations on the residues modulo the prime
667 instead. Since these numbers are smaller, multiplication and exponentiation
671 computes the residues of
673 and returns them in a newly allocated structure:
676 typedef struct CRTres CRTres;
678 int n; /* number of residues */
679 mpint *r[n]; /* residues */
684 takes a residue representation of a number and converts it back into
685 the number. It also frees the residue structure.
688 saves a copy of the factors and precomputes the constants necessary
689 for converting the residue form back into a number modulo
690 the product of the factors. It returns a newly allocated structure
700 structures respectively.