2
0

bn.doc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. * The BigNum multi-precision integer math library
  2. This is a multi-precision math library designed to be very portable,
  3. reasonably clean and easy to use, have very liberal bounds on the sizes
  4. of numbers that can be represented, but above all to perform extremely
  5. fast modular exponentiation. It has some limitations, such as
  6. representing positive numbers only, and supporting only odd moduli,
  7. which simplify it without impairing this ability.
  8. A second speed goal which has had considerable effort applied to it is
  9. prime number generation.
  10. Finally, while there is probably a long way to go in this direction,
  11. some effort has gone into commenting the code a lot more than seems to
  12. be fashionable among mathematicians.
  13. It is written in C, and should compile on any platform with an ANSI C
  14. compiler and 16 and 32-bit unsigned data types, but various primitives
  15. can be replaced with assembly versions in a great variety of ways for
  16. greater speedup. See "bnintern.doc" for a description.
  17. In case you're wondering, yes C++ would produce a much nicer syntax
  18. for working with these numbers, but there are a lot of compilers out
  19. there that actually implement ANSI C, and get it almost right. I have
  20. a few kludges to deal with some that get little things wrong, but
  21. overall it's not too difficult to write code that I can be sure
  22. will work on lots of machines. And porting it to a K&R C compiler,
  23. if it ever becomes necessary, shouldn't be all *that* difficult.
  24. The C++ compiler world is a less friendly place. First of all, C++
  25. compilers are still not as common as C compilers, so that hurts
  26. portability right there, and I don't need the extra power to write my
  27. code. C++ compilers all seem to have important bugs, and different
  28. bugs for each compiler. First I have to learn all the foibles of a
  29. whole lot of C++ compilers, and then I have write code that uses only
  30. the features that work in all of them. This is a language not a whole
  31. heck of a lot bigger than C.
  32. (The fact that it drives me *batty* the way that C++ drags *everything*
  33. into the same name space is also a contributing factor. I *like*
  34. writing "struct" (or "class") before structure names. I *like* putting
  35. "this->" in front of member references. It makes it clear to me, when
  36. reading a single line of code, roughly what is being affected by it and
  37. where I can find the relevant source code to find out more. I've seen
  38. people develop complicated naming conventions to make all this clear,
  39. but the conventions are still very much in flux.)
  40. Anyway...
  41. The main public interface is contained in the file bn.h. This is
  42. mostly a bunch of pointers to functions which start out uninitialized,
  43. but are set by bnInit() (which is called by bnBegin()).
  44. All of the public routines have names of the bnFunction variety.
  45. Some internal routines are lbnFunction, but you should never have to
  46. worry about those unless you're hacking with the code.
  47. The code uses the assert() macro a lot internally. If you do something
  48. you're not supposed to, you'll generally notice because an assert()
  49. will fail. The library does not have special error codes for division
  50. by zero or the like - it assert fails instead. Just don't do that.
  51. A BigNum is represented by a struct BigNum, which really doesn't
  52. need to be understood, but it often makes me feel better to understand
  53. what's going on, so here it is:
  54. #> struct BigNum {
  55. #> void *ptr;
  56. #> unsigned size; /* Note: in (variable-sized) words */
  57. #> unsigned allocated;
  58. #> };
  59. The pointer points to the least-significant end of an array of words which
  60. hold the number. The array contains "allocated" words, but only "size"
  61. of them are actually meaningful. The others may have any value.
  62. This is all of limited use because the size of a word is not specified.
  63. In fact, it can change at run time - if you run on an 8086 one day and an
  64. 80386 the next, you may find the word size different.
  65. * Initialization
  66. The user of the library is responsible for allocating and freeing each
  67. struct BigNum. Usually they're just local variables. All the library
  68. functions take pointers to them. The first thing you need to do is
  69. initialize all the fields to empty, a zero-valued BigNum. This is done
  70. with the function bnBegin:
  71. #> void bnBegin(struct BigNum *bn);
  72. When you're done with a BigNum, call bnEnd to deallocate the data storage
  73. in preparation for deallocating the structure:
  74. #> void bnEnd(struct BigNum *bn);
  75. This resets the number to the 0 state. You can actually start using the
  76. number right away again, or call bnEnd again, so if you're really
  77. memory-conscious you might want to use this to free a large
  78. number you're done with this way before going on to use the buffer
  79. for smaller things.
  80. A simple assignment can be done with bnCopy.
  81. #> int bnCopy(struct BigNum *dest, struct BigNum const *src);
  82. This sets dest = src, and returns an error code. Most functions in the
  83. library do this, and return 0 on success and -1 if they were unable to
  84. allocate needed memory. If you're lazy and sure you'll never run out
  85. of memory, you can avoid checking this, but it's better to be
  86. paranoid. If a function returns -1, the what has happened to the
  87. destination values is undefined. They're usually unmodified, and
  88. they're always still valid BigNum numbers, but their values might be
  89. strange.
  90. In general, anywhere that follows, unless otherwise documented, assume
  91. that an "int" return value is 0 for success or -1 for error.
  92. A trivial little function which is sometimes handy, and quite cheap to
  93. execute (it just swaps the pointers) is:
  94. #> void bnSwap(struct BigNum *a, struct BigNum *b);
  95. * Input and output
  96. For now, the library only works with numbers in binary form - there's
  97. no way to get decimal numbers into or out of it. But it's pretty
  98. flexible on how it does that.
  99. The first function just sets a BigNum to have a small value. There are
  100. several such "quick" forms which work with "small" second operads.
  101. "Small" is defined as less than 65536, the minimum 16-bit word size
  102. supported by the library. The limit applies even if unsigned is larger
  103. or the library is compiled for a larger word size.
  104. #> int bnSetQ(struct BigNum *dest, unsigned src);
  105. This returns the usual -1 error if it couldn't allocate memory.
  106. There's also a function to determine the size of a BigNum, in bits.
  107. The size is the number of bits required to represent the number,
  108. 0 if the number is 0, and floor(log2(src)) + 1 otherwise. E.g. 1 is
  109. the only 1-bit number, 2 and 3 are 2-bit numbers, etc.
  110. #> unsigned bnBits(struct BigNum const *src);
  111. If bnBits(src) <= 16, you can get the whole number with this function.
  112. If it's larger, you get the low k bits, where k is at least 16.
  113. (This doesn't bother masking if it's easy to return more, but you
  114. shouldn't rely on it.) Even that is useful for many things, like
  115. deciding if a number is even or odd.
  116. #> unsigned bnLSWord(struct BigNum const *src);
  117. For larger numbers, the format used by the library is an array of
  118. unsigned 8-bit bytes. These bytes may be in big-endian or little-endian
  119. order, and it's possible to examine or change just part of a number.
  120. The functions are:
  121. #> void bnExtractBigBytes(struct BigNum const *bn, unsigned char *dest,
  122. #> unsigned lsbyte, unsigned len);
  123. #> int bnInsertBigBytes(struct BigNum *bn, unsigned char const *src,
  124. #> unsigned lsbyte, unsigned len);
  125. #> void bnExtractLittleBytes(struct BigNum const *bn, unsigned char *dest,
  126. #> unsigned lsbyte, unsigned len);
  127. #> int bnInsertLittleBytes(struct BigNum *bn, unsigned char const *src,
  128. #> unsigned lsbyte, unsigned len);
  129. These move bytes between the BigNum and the buffer of 8-bit bytes. The
  130. Insert functions can allocate memory, so return an error code. The
  131. Extract functions always succeed.
  132. The buffer is encoded in base 256, with either the most significant
  133. byte (the Big functions) or the least significant byte (the Little
  134. functions) coming first. "len" is the length of the buffer, so the
  135. buffer always encodes a value between 0 and 256^len. (That's
  136. "to the power of", not "xor".)
  137. "lsbyte" gives the offset into the BigNum which is being worked with.
  138. This is usually zero, but you can, for example, read out a large
  139. BigNum in 32-byte chunks, using a len of 32 and an lsbyte of 0, 32,
  140. 64, 96, etc.
  141. After these complete, the number encoded in the buffer will be
  142. equal to (bn / 256^lsbyte) % 256^len. The only difference between
  143. Insert and Extract is which is changed to match the other.
  144. * Simple math
  145. #> int bnAdd(struct BigNum *dest, struct BigNum const *src);
  146. #> int bnAddQ(struct BigNum *dest, unsigned src);
  147. These add dest += src. In the Q form, as mentioned above with bnSetQ,
  148. src must be < 65536. In either case, the functions can fail and return
  149. -1, as usual.
  150. #> int bnSub(struct BigNum *dest, struct BigNum const *src);
  151. #> int bnSubQ(struct BigNum *dest, unsigned src);
  152. These subtract dest -= src. If this would make the result negative,
  153. dest is set to (src-dest) and a value of 1 is returned, so you can
  154. keep track of a separate sign if you need to. Otherwise, they return
  155. 0 on success and -1 if they were unable to allocate needed memory.
  156. To make your life simpler if you are error checking, these four functions
  157. are guaranteed not to allocate memory unnecessarily. So if you know
  158. that the addition or subtraction you're doing won't produce a result
  159. larger than the input, and won't underflow either (like subtracting 1
  160. from an odd number or adding 1 to an even number), you can skip checking
  161. the error code.
  162. #> extern int (*bnCmp)(struct BigNum const *a, struct BigNum const *b);
  163. #> extern int (*bnCmpQ)(struct BigNum const *a, unsigned b);
  164. This returns the sign (-1, 0 or +1) of a-b. Another way of saying
  165. this is that a <=> b is the same as bnCmp(a, b) <=> 0, where "<=>"
  166. stands for one of <, <=, =, !=, >= or >. The bnCmpQ form is the same,
  167. but (as in all the Q functions) the second argument is a number < 65536.
  168. #> int bnSquare(struct BigNum *dest, struct BigNum const *src);
  169. This computes dest = src^2, returning an error if it ran out of memory.
  170. If you care about performance tuning, this slows down when dest and
  171. src are the same BigNum, since it needs to allocate a temporary buffer
  172. to do the work in. It does work, however.
  173. #> int bnMul(struct BigNum *dest, struct BigNum const *a,
  174. #> struct BigNum const *b);
  175. #> int bnMulQ(struct BigNum *dest, struct BigNum const *a, unsigned b);
  176. These compute dest = a * b, and work in the same way as bnSquare.
  177. (Including the fact that it's faster if dest is not the same as any of
  178. the inputs.) bnSquare is faster if a and b are the same. The second
  179. input operand to bnMulQ must be < 65536, like all the "Q" functions.
  180. #> int bnDivMod(struct BigNum *q, struct BigNum *r,
  181. #> struct BigNum const *n, struct BigNum const *d);
  182. This computes division with remainder, q = n/d and r = n%d. Don't
  183. pass in a zero d; it will blow up. In general, all of the values
  184. must be different (it will blow up if you try), but r and n may be the
  185. same.
  186. RE-ENTRANCY NOTE: This temporarily modifies the BigNum "d" internally,
  187. although it restores it before returning. If you're doing something
  188. multi-threaded, you can't share the d value between threads, even though
  189. it says "const". That's a safe assumption elsewhere, but this is an
  190. exception.
  191. That note also means that it's not safe to let n be the same as d,
  192. although that's such a stupid way to set q to 1 and r to 0 that
  193. I don't think it's worth worrying about. (I hope you understand that
  194. this doesn't mean that n and d can't have the same numerical value,
  195. just that they can't both point to the same struct BigNum.)
  196. #> int bnMod(struct BigNum *dest, struct BigNum const *src,
  197. #> struct BigNum const *d);
  198. This works just the same as the above, but doesn't bother you with the
  199. quotient. (No, there's no function that doesn't bother you with the
  200. remainder.) Again, dest and src may be the same (it's actually
  201. more efficient if they are), but d may not be the same as either.
  202. #> unsigned int bnModQ(struct BigNum const *src, unsigned d);
  203. This also computes src % d, but does so for small (up to 65535,
  204. the usual limit on "Q" functions) values of d. It returns the
  205. remainder. (No error is possible.)
  206. * Advanced math
  207. #> int bnLShift(struct BigNum *dest, unsigned amt);
  208. #> void bnRShift(struct BigNum *dest, unsigned amt);
  209. These shift the given bignum left or right by "amt" bit positions.
  210. Left shifts multiply by 2^amt, and may have to allocate memory
  211. (and thus fail). Right shifts divide by 2^amt, throwing away the
  212. remainder, and can never fail.
  213. #> unsigned bnMakeOdd(struct BigNum *n);
  214. This right shifts the input number as many places as possible without
  215. throwing anything away, and returns the number of bits shifted.
  216. If you see "let n = s * 2^t, where s is odd" in an algorithm,
  217. this is the function to call. It modifies n in place to produce s
  218. and returns t.
  219. This returns 0 if you pass it 0.
  220. #> int bnExpMod(struct BigNum *result, struct BigNum const *n,
  221. #> struct BigNum const *exp, struct BigNum const *mod);
  222. Ah, now we get to the heart of the library - probably the most heavily
  223. optimized function in it. This computes result = n^exp, modulo "mod".
  224. result may be the same as n, but not the same as exp or mod. For large
  225. exponents and moduli, it can try to allocate quite a bit of working
  226. storage, although it will manage to finish its work (just slower)
  227. if some of those allocations fail. (Not all, though - the first few
  228. are essential.)
  229. "mod" must be odd. It will blow up if not. Also, n must be less than
  230. mod. If you're not sure if it is, use bnMod first. The return value
  231. is always between 0 and mod-1.
  232. #> int bnTwoExpMod(struct BigNum *result, struct BigNum const *exp,
  233. #> struct BigNum const *mod);
  234. This computes result = 2^exp, modulo "mod". It's faster than the general
  235. bnExpMod function, although that function checks to see if n = 2 and calls
  236. this one internally, so you don't need to check yourself if you're not
  237. sure. The main reason to mention this is that if you're doing something
  238. like a pseudoprimality test, using a base of 2 first can save some time.
  239. #> int bnDoubleExpMod(struct BigNum *result,
  240. #> struct BigNum const *n1, struct BigNum const *e1,
  241. #> struct BigNum const *n2, struct BigNum const *e2,
  242. #> struct BigNum const *mod);
  243. This computes dest = n1^e1 * n2^e2, modulo "mod". It does it quite
  244. a bit faster than doing two separate bnExpMod operations; in fact,
  245. it's not that much more expensive than one. "result" may be the
  246. same BigNum as n1 or n2, but it may not be the same as the exponents
  247. or the modulus. All of the other caveats about bnExpMod apply.
  248. #> int bnGcd(struct BigNum *dest, struct BigNum const *a,
  249. #> struct BigNum const *b);
  250. This returns dest = gcd(a,b). dest may be the same as either input.
  251. /* dest = src^-1, modulo "mod". dest may be the same as src. */
  252. #> int bnInv(struct BigNum *dest, struct BigNum const *src,
  253. #> struct BigNum const *mod);
  254. This requires that gcd(src, mod) = 1, and returns dest = src^-1, modulo
  255. "mod". That is, 0 < dest < mod and dest*src = 1, modulo "mod".
  256. dest and src may be the same, but mod must be different.
  257. This will probably get extended at some point to find dest such that
  258. dest * src = gcd(src, mod), modulo "mod", but that isn't implemented
  259. yet.
  260. * Auxiliary functions
  261. These mostly-internal functions aren't very useful to call directly,
  262. and might even get removed, but for now they're there in the unusual
  263. case where you might want them.
  264. #> void bnInit(void);
  265. This does global library initialization. It is called by the first
  266. call to bnBegin(), so you shouldn't need to call it explicitly. It is
  267. idempotent, so you can call it multiple times if you like. The only
  268. thing it does right now is set up the function pointers to the rest of
  269. the library. If a program crashes and the debugger tells you that
  270. it's trying to execute at address 0, bnInit never got called.
  271. #> int bnPrealloc(struct BigNum *bn, unsigned bits);
  272. This preallocates space in bn to make sure that it can hold "bits" bits.
  273. If the overflow characteristics of various algorithms get documented
  274. better, this might allow even more error-checking to be avoided, but
  275. for now it's only to reduce memory fragmentation.
  276. #> void bnNorm(struct BigNum *bn);
  277. This decreases the "size" field of the given bignum until it has no leading
  278. zero words in its internal representation. Given that almost everything
  279. in the library does the equivalent of this on input and output, the utility
  280. of this function is a bit dubious. It's kind of a legacy.
  281. * Extra libraries
  282. There are a number of utilities built on top of the basic library.
  283. They are built on top of the interfaces just described, and can be used
  284. if you like.
  285. * jacobi.h
  286. #> int bnJacobiQ(unsigned p, struct BigNum const *bn);
  287. This returns the Jacobi symbol J(p,bn), where p is a small number.
  288. The Jacobi symbol is always -1, 0, or +1. You'll note that p may
  289. only be positive, even though the Jacobi symbol is defined for
  290. negative p. If you want to worry about negative p, do it yourself.
  291. J(-p,bn) = (bnLSWord(bn) & 2 ? -1 : +1) * bnJacobiQ(p, bn).
  292. A function to compute the Jacobi symbol for large p would be nice.
  293. * prime.h
  294. #> int primeGen(struct BigNum *bn, unsigned (*rand)(unsigned),
  295. #> int (*f)(void *arg, int c), void *arg, unsigned exponent, ...);
  296. This finds the next prime p >= bn, and sets bn to equal it.
  297. Well, sort of.
  298. It always leaves bn at least as large as when it started (unless it
  299. runs out of memory and returns -1), and if you pass a 0 for the rand
  300. function, it will be the next prime >= bn.
  301. Except:
  302. - It doesn't bother coping with small primes. If it's divisible by any
  303. prime up to 65521, it's considered non-prime. Even if the quotient is 0.
  304. If you pass in "1", expecting to get "2" back, you'll get 65537. Maybe
  305. it would be nice to fix that.
  306. - It actually only does a few strong pseudoprimality tests to fixed
  307. bases to determine if the candidate number is prime. For random input,
  308. this is fine; the chance of error is so infinitesimal that it is
  309. absolutely not worth worrying about. But if you give it numbers carefully
  310. chosen to be strong pseudoprimes, it will think they're primes and not
  311. complain. For example, 341550071728321 = 10670053 * 32010157 will
  312. pass the primality test quite handily. So will
  313. 68528663395046912244223605902738356719751082784386681071.
  314. - If you supply a rand() function, which returns 0 <= rand(n) < n
  315. (n never gets very large - currently, at most 256), this shuffles the
  316. candidates before testing and accepting one. If you want a "random"
  317. prime, this produces a more uniformly distributed prime, while
  318. retaining all of the speed advantages of a sequential search from a
  319. random starting point, which would otherwise produce a bias towards
  320. primes which were not closely preceded by other primes. So, for
  321. example, the second of a pair of twin primes would be very unlikely to
  322. be chosen. rand() doesn't totally flatten the distribution, but it
  323. comes very close.
  324. The "f" function is called periodically during the progress of the
  325. search (which can take a while) with the supplied argument (for private
  326. context) and a character c, which sort of tells you what it's doing.
  327. c is either '.' or '*' (if it's found something and is confirming that
  328. it's really prime) or '/' (if it's having a really hard time finding
  329. something). Also, if f returns < 0, primeGen immediately returns that
  330. value. This can form the basis for a user interface which can show some
  331. life occasionally and abort the computation if desired.
  332. If you just print these characters to the screen, don't forget to
  333. fflush() after printing them.
  334. Finally, "exponent, ..." is a zero-terminated list of small numbers
  335. which must not divide p-1 when the function returns. If the numbers
  336. are chosen to be the prime factors of n, then gcd(n, p-1) will be
  337. 1, so the map f(x) -> x^n is invertible modulo p.
  338. #> int primeGenStrong(struct BigNum *bn, struct BigNum const *step,
  339. #> int (*f)(void *arg, int c), void *arg);
  340. This is similar, but searches in steps of "step", rather than 1, from the
  341. given starting value. The starting value must be odd and the step
  342. size must be even! If you start with bn == 1 (mod step), and step
  343. is 2*q, where q is a large prime, then this generates "strong" primes,
  344. p-1 having a large prime factor q. There are other uses, too.
  345. #ifdef __cplusplus
  346. }
  347. #endif
  348. * germain.h
  349. #> int germainPrimeGen(struct BigNum *bn, int (*f)(void *arg, int c),
  350. #> void *arg);
  351. This increases bn until it is a Sophie Germain prime, that is, a number p
  352. such that p and (p-1)/2 are both prime. These numbers are rarer than
  353. ordinary primes and the search takes correspondingly longer.
  354. It omits the randomization portion of primeGen, and the exponent list,
  355. since the factors of bn-1 are known already. The f function for
  356. progress is the same, but it is also sometimes passed a '+' or '-'
  357. character when it's found a (p-1)/2 that's prime. This is just to lend
  358. some interest to an otherwise very boring row of dots. Finding large
  359. primes with this function, even though it's pretty optimized, takes a
  360. *while*, and otherwise once the screen filled with dots (one every few
  361. seconds) it would be hard to keep track of the scroll.
  362. It varies a lot, depending on luck of the starting value and the speed
  363. of your machine, but if your starting number is over 1024 bits, plan on
  364. over an hour of run time, and if it's over 2048 bits, plan on a day.
  365. At 4096 bits, start thinking about a week.
  366. Past that, supporting checkpoint/restart is a good idea. Every time
  367. the progress function gets a '/' is probably a good interval, and when
  368. it happens have f return a distinct error value like -2. When
  369. germainPrimeGen returns with that value, save the value in bn to a file
  370. somewhere and call it again with the same bn to continue searching.
  371. * sieve.h
  372. This is the sieving code that the other prime-finding functions call
  373. to do trial division. You might use it if you are doing some magic
  374. prime-finding of your own. A sieve is an array of bits, stored
  375. little-endian in an array of bytes (i.e. the lsb of byte 0 is bit 0).
  376. Sieves are indexed with the "unsigned" data type, so should not, for
  377. portability, be larger than 65536/8 = 8192 bytes long.
  378. A 1 bit is considered "in" the sieve, it has passed all the sieving.
  379. A 0 bit has been removed by some step.
  380. The functions are:
  381. #> void sieveSingle(unsigned char *array, unsigned size, unsigned start,
  382. #> unsigned step);
  383. This (efficiently) clears the bits at positions start, start+step,
  384. start+2*step, etc. in the sieve given by array and size. This is the
  385. elementary sieve-building step. Start with a sieve of all 1s, and
  386. apply this as required.
  387. #> unsigned sieveSearch(unsigned char const *array, unsigned size,
  388. #> unsigned start);
  389. This returns the next bit position *greater than* start which is set
  390. in the indicated sieve, or 0 on failure. NOTE that this means that
  391. you have to look at the bit at position 0 (array[0] & 1) by yourself
  392. if you want to pay attention to it, because there's no way to tell
  393. sieveSearch to start searching at 0 - it starts at start+1.
  394. #> int sieveBuild(unsigned char *array, unsigned size, struct BigNum const *bn,
  395. #> unsigned step, unsigned dbl);
  396. This initializes a sieve where, if bit i is set, then bn+step*i is not
  397. divisible by any small primes. (Small is from 2 through 65521, the
  398. largest prime less that 65536.) If "dbl" is > 0, then bits are also
  399. cleared if 2*(bn+step*i)+1 is divisible. If dbl > 1, then
  400. 4*(bn+step*i)+3 is also checked, and so on. This feature is used when
  401. generating Sohpie Germain primes.
  402. Usually, you use a step of 2.
  403. #> int sieveBuildBig(unsigned char *array, unsigned size,
  404. #> struct BigNum const *bn, struct BigNum const *step, unsigned dbl);
  405. This is just the same, but accepts a BigNum step size, and is correspondingly
  406. slower.
  407. * bnprint.h
  408. #> int bnPrint(FILE *f, char const *prefix, struct BigNum const *bn,
  409. #> char const *suffix);
  410. This prints a nicely-formatted BigNum in hexadecimal form to the given
  411. FILE *. The "prefix" is printed before it, as a prompt, and the
  412. "suffix" is printed afterwards. The BigNum itself is printed in
  413. 64-character lines, broken with a trailing backslash if necessary.
  414. Continuation lines are indented by the length of the prefix.
  415. E.g. a 2^512-1, printed with the call bnPrint(stdout, "a = (", bn, ")\n")
  416. would result in:
  417. a = (FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
  418. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
  419. Hex digits are printed in upper case to facilitate cutting and pasting into
  420. the Unix "dc" utility.