bn.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 1995 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * bn.h - the interface to the bignum routines.
  6. * All functions which return ints can potentially allocate memory
  7. * and return -1 if they are unable to. All "const" arguments
  8. * are unmodified.
  9. *
  10. * This is not particularly asymmetric, as some operations are of the
  11. * form a = b @ c, while others do a @= b. In general, outputs may not
  12. * point to the same struct BigNums as inputs, except as specified
  13. * below. This relationship is referred to as "being the same as".
  14. * This is not numerical equivalence.
  15. *
  16. * The "Q" operations take "unsigned" inputs. Higher values of the
  17. * extra input may work on some implementations, but 65535 is the
  18. * highest portable value. Just because UNSIGNED_MAX is larger than
  19. * that, or you know that the word size of the library is larger than that,
  20. * that, does *not* mean it's allowed.
  21. */
  22. #ifndef BN_H
  23. #define BN_H
  24. struct BigNum {
  25. void *ptr;
  26. unsigned size; /* Note: in (variable-sized) words */
  27. unsigned allocated;
  28. };
  29. /*
  30. * User-supplied function: if non-NULL, this is called during long-running
  31. * computations. You may put Yield() calls in here to give CPU time to
  32. * other processes. You may also force the computation to be aborted,
  33. * by returning a value < 0, which will be the return value of the
  34. * bnXXX call. (You probably want the value to be someting other than
  35. * -1, to distinguish it from a n out-of-memory error.)
  36. *
  37. * The functions that this is called from, and the intervals at which it
  38. * is called, are not well defined, just "reasonably often". (Currently,
  39. * once per exponent bit in nodular exponentiation, and once per two
  40. * divisions in GCD and inverse computation.)
  41. */
  42. extern int (*bnYield)(void);
  43. /* Functions */
  44. /*
  45. * You usually never have to call this function explicitly, as
  46. * bnBegin() takes care of it. If the program jumps to address 0,
  47. * this function has bot been called.
  48. */
  49. void bnInit(void);
  50. /*
  51. * This initializes an empty struct BigNum to a zero value.
  52. * Do not use this on a BigNum which has had a value stored in it!
  53. */
  54. void bnBegin(struct BigNum *bn);
  55. /* Swap two BigNums. Cheap. */
  56. void bnSwap(struct BigNum *a, struct BigNum *b);
  57. /* Reset an initialized bigNum to empty, pending deallocation. */
  58. extern void (*bnEnd)(struct BigNum *bn);
  59. /*
  60. * If you know you'll need space in the number soon, you can use this function
  61. * to ensure that there is room for at least "bits" bits. Optional.
  62. * Returns <0 on out of memory, but the value is unaffected.
  63. */
  64. extern int (*bnPrealloc)(struct BigNum *bn, unsigned bits);
  65. /* Hopefully obvious. dest = src. dest may be the same as src. */
  66. extern int (*bnCopy)(struct BigNum *dest, struct BigNum const *src);
  67. /*
  68. * Mostly done automatically, but this removes leading zero words from
  69. * the internal representation of the BigNum. Use is unclear.
  70. */
  71. extern void (*bnNorm)(struct BigNum *bn);
  72. /*
  73. * Move bytes between the given buffer and the given BigNum encoded in
  74. * base 256. I.e. after either of these, the buffer will be equal to
  75. * (bn / 256^lsbyte) % 256^len. The difference is which is altered to
  76. * match the other!
  77. */
  78. extern void (*bnExtractBigBytes)(struct BigNum const *bn,
  79. unsigned char *dest, unsigned lsbyte, unsigned len);
  80. extern int (*bnInsertBigBytes)(struct BigNum *bn, unsigned char const *src,
  81. unsigned lsbyte, unsigned len);
  82. /* The same, but the buffer is little-endian. */
  83. extern void (*bnExtractLittleBytes)(struct BigNum const *bn,
  84. unsigned char *dest, unsigned lsbyte, unsigned len);
  85. extern int (*bnInsertLittleBytes)(struct BigNum *bn, unsigned char const *src,
  86. unsigned lsbyte, unsigned len);
  87. /* Return the least-significant bits (at least 16) of the BigNum */
  88. extern unsigned (*bnLSWord)(struct BigNum const *src);
  89. /* Return the selected bit of the BigNum (bit 0 is bn mod 2) */
  90. extern int (*bnReadBit)(struct BigNum const *bn, unsigned bit);
  91. /*
  92. * Return the number of significant bits in the BigNum.
  93. * 0 or 1+floor(log2(src))
  94. */
  95. extern unsigned (*bnBits)(struct BigNum const *src);
  96. #define bnBytes(bn) ((bnBits(bn)+7)/8)
  97. /*
  98. * dest += src. dest and src may be the same. Guaranteed not to
  99. * allocate memory unnecessarily, so if you're sure bnBits(dest)
  100. * won't change, you don't need to check the return value.
  101. */
  102. extern int (*bnAdd)(struct BigNum *dest, struct BigNum const *src);
  103. /*
  104. * dest -= src. dest and src may be the same, but bnSetQ(dest, 0) is faster.
  105. * if dest < src, returns +1 and sets dest = src-dest.
  106. */
  107. extern int (*bnSub)(struct BigNum *dest, struct BigNum const *src);
  108. /* Return sign (-1, 0, +1) of a-b. a <=> b --> bnCmpQ(a, b) <=> 0 */
  109. extern int (*bnCmpQ)(struct BigNum const *a, unsigned b);
  110. /* dest = src, where 0 <= src < 2^16. */
  111. extern int (*bnSetQ)(struct BigNum *dest, unsigned src);
  112. /* dest += src, where 0 <= src < 2^16 */
  113. extern int (*bnAddQ)(struct BigNum *dest, unsigned src);
  114. /* dest -= src, where 0 <= src < 2^16 */
  115. extern int (*bnSubQ)(struct BigNum *dest, unsigned src);
  116. /* Return sign (-1, 0, +1) of a-b. a <=> b --> bnCmp(a, b) <=> 0 */
  117. extern int (*bnCmp)(struct BigNum const *a, struct BigNum const *b);
  118. /* dest = src^2. dest may be the same as src, but it costs time. */
  119. extern int (*bnSquare)(struct BigNum *dest, struct BigNum const *src);
  120. /* dest = a * b. dest may be the same as a or b, but it costs time. */
  121. extern int (*bnMul)(struct BigNum *dest, struct BigNum const *a,
  122. struct BigNum const *b);
  123. /* dest = a * b, where 0 <= b < 2^16. dest and a may be the same. */
  124. extern int (*bnMulQ)(struct BigNum *dest, struct BigNum const *a, unsigned b);
  125. /*
  126. * q = n/d, r = n%d. r may be the same as n, but not d,
  127. * and q may not be the same as n or d.
  128. * re-entrancy issue: this temporarily modifies d, but restores
  129. * it for return.
  130. */
  131. extern int (*bnDivMod)(struct BigNum *q, struct BigNum *r,
  132. struct BigNum const *n, struct BigNum const *d);
  133. /*
  134. * dest = src % d. dest and src may be the same, but not dest and d.
  135. * re-entrancy issue: this temporarily modifies d, but restores
  136. * it for return.
  137. */
  138. extern int (*bnMod)(struct BigNum *dest, struct BigNum const *src,
  139. struct BigNum const *d);
  140. /* return src % d, where 0 <= d < 2^16. */
  141. extern unsigned int (*bnModQ)(struct BigNum const *src, unsigned d);
  142. /* n = n^exp, modulo "mod" "mod" *must* be odd */
  143. extern int (*bnExpMod)(struct BigNum *result, struct BigNum const *n,
  144. struct BigNum const *exp, struct BigNum const *mod);
  145. /*
  146. * dest = n1^e1 * n2^e2, modulo "mod". "mod" *must* be odd.
  147. * dest may be the same as n1 or n2.
  148. */
  149. extern int (*bnDoubleExpMod)(struct BigNum *dest,
  150. struct BigNum const *n1, struct BigNum const *e1,
  151. struct BigNum const *n2, struct BigNum const *e2,
  152. struct BigNum const *mod);
  153. /* n = 2^exp, modulo "mod" "mod" *must* be odd */
  154. extern int (*bnTwoExpMod)(struct BigNum *n, struct BigNum const *exp,
  155. struct BigNum const *mod);
  156. /* dest = gcd(a, b). The inputs may overlap arbitrarily. */
  157. extern int (*bnGcd)(struct BigNum *dest, struct BigNum const *a,
  158. struct BigNum const *b);
  159. /* dest = src^-1, modulo "mod". dest may be the same as src. */
  160. extern int (*bnInv)(struct BigNum *dest, struct BigNum const *src,
  161. struct BigNum const *mod);
  162. /* Shift dest left "amt" places */
  163. extern int (*bnLShift)(struct BigNum *dest, unsigned amt);
  164. /* Shift dest right "amt" places, discarding low-order bits */
  165. extern void (*bnRShift)(struct BigNum *dest, unsigned amt);
  166. /* For the largest 2^k that divides n, divide n by it and return k. */
  167. extern unsigned (*bnMakeOdd)(struct BigNum *n);
  168. /*
  169. * Precomputed data for rapid base^exp (mod mod) computation with fixed
  170. * base and mod.
  171. */
  172. struct BnBasePrecomp {
  173. void *array; /* Ponter to array of pointers to words */
  174. unsigned msize; /* Words in modulis (normalized) */
  175. unsigned bits; /* Bits per array element */
  176. unsigned maxebits; /* Maximum exponent bits */
  177. unsigned entries; /* Number of entries */
  178. unsigned arraysize;
  179. };
  180. extern int (*bnBasePrecompBegin)(struct BnBasePrecomp *pre,
  181. struct BigNum const *base, struct BigNum const *mod,
  182. unsigned maxebits);
  183. extern void (*bnBasePrecompEnd)(struct BnBasePrecomp *pre);
  184. extern int (*bnBasePrecompExpMod)(struct BigNum *dest,
  185. struct BnBasePrecomp const *pre, struct BigNum const *exp,
  186. struct BigNum const *mod);
  187. extern int (*bnDoubleBasePrecompExpMod)(struct BigNum *dest,
  188. struct BnBasePrecomp const *pre1, struct BigNum const *exp1,
  189. struct BnBasePrecomp const *pre2, struct BigNum const *exp2,
  190. struct BigNum const *mod);
  191. #endif/* !BN_H */