mp 11 KB

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