bn.h
上传用户:zbbssh
上传日期:2007-01-08
资源大小:196k
文件大小:6k
源码类别:

CA认证

开发平台:

C/C++

  1. /*
  2.  * bn.h - the interface to the bignum routines.
  3.  * All functions which return ints can potentially allocate memory
  4.  * and return -1 if they are unable to. All "const" arguments
  5.  * are unmodified.
  6.  *
  7.  * This is not particularly asymmetric, as some operations are of the
  8.  * form a = b @ c, while others do a @= b.  In general, outputs may not
  9.  * point to the same struct BigNums as inputs, except as specified
  10.  * below.  This relationship is referred to as "being the same as".
  11.  * This is not numerical equivalence.
  12.  *
  13.  * The "Q" operations take "unsigned" inputs.  Higher values of the
  14.  * extra input may work on some implementations, but 65535 is the
  15.  * highest portable value.  Just because UNSIGNED_MAX is larger than
  16.  * that, or you know that the word size of the library is larger than that,
  17.  * that, does *not* mean it's allowed.
  18.  */
  19. #ifndef BN_H
  20. #define BN_H
  21. struct BigNum {
  22. void *ptr;
  23. unsigned size; /* Note: in (variable-sized) words */
  24. unsigned allocated;
  25. };
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* Functions */
  30. /* bnInit *must* be called before any other bignum library function */
  31. void bnInit(void);
  32. /*
  33.  * This initializes an empty struct BigNum to a zero value.
  34.  * Do not use this on a BigNum which has had a value stored in it!
  35.  */
  36. void bnBegin(struct BigNum *bn);
  37. /* Swap two BigNums.  Cheap. */
  38. void bnSwap(struct BigNum *a, struct BigNum *b);
  39. /* Reset an initialized bigNum to empty, pending deallocation. */
  40. extern void (*bnEnd)(struct BigNum *bn);
  41. /*
  42.  * If you know you'll need space in the number soon, you can use this function
  43.  * to ensure that there is room for at least "bits" bits.  Optional.
  44.  * Returns <0 on out of memory, but the value is unaffected.
  45.  */
  46. extern int (*bnPrealloc)(struct BigNum *bn, unsigned bits);
  47. /* Hopefully obvious.  dest = src.   dest may be the same as src. */
  48. extern int (*bnCopy)(struct BigNum *dest, struct BigNum const *src);
  49. /*
  50.  * Mostly done automatically, but this removes leading zero words from
  51.  * the internal representation of the BigNum.  Use is unclear.
  52.  */
  53. extern void (*bnNorm)(struct BigNum *bn);
  54. /*
  55.  * Move bytes between the given buffer and the given BigNum encoded in
  56.  * base 256.  I.e. after either of these, the buffer will be equal to
  57.  * (bn / 256^lsbyte) % 256^len.  The difference is which is altered to
  58.  * match the other!
  59.  */
  60. extern void (*bnExtractBigBytes)(struct BigNum const *bn,
  61. unsigned char *dest, unsigned lsbyte, unsigned len);
  62. extern int (*bnInsertBigBytes)(struct BigNum *bn, unsigned char const *src,
  63. unsigned lsbyte, unsigned len);
  64. /* The same, but the buffer is little-endian. */
  65. extern void (*bnExtractLittleBytes)(struct BigNum const *bn,
  66. unsigned char *dest, unsigned lsbyte, unsigned len);
  67. extern int (*bnInsertLittleBytes)(struct BigNum *bn, unsigned char const *src,
  68. unsigned lsbyte, unsigned len);
  69. /* Return the least-significant bits (at least 16) of the BigNum */
  70. extern unsigned (*bnLSWord)(struct BigNum const *src);
  71. /*
  72.  * Return the number of significant bits in the BigNum.
  73.  * 0 or 1+floor(log2(src))
  74.  */
  75. extern unsigned (*bnBits)(struct BigNum const *src);
  76. /*
  77.  * dest += src.  dest and src may be the same.  Guaranteed not to
  78.  * allocate memory unnecessarily, so if you're sure bnBits(dest)
  79.  * won't change, you don't need to check the return value.
  80.  */
  81. extern int (*bnAdd)(struct BigNum *dest, struct BigNum const *src);
  82. /*
  83.  * dest -= src.  dest and src may be the same, but bnSetQ(dest, 0) is faster.
  84.  * if dest < src, returns +1 and sets dest = src-dest.
  85.  */
  86. extern int (*bnSub)(struct BigNum *dest, struct BigNum const *src);
  87. /* dest = src, where 0 <= src < 2^16. */
  88. extern int (*bnSetQ)(struct BigNum *dest, unsigned src);
  89. /* dest += src, where 0 <= src < 2^16 */
  90. extern int (*bnAddQ)(struct BigNum *dest, unsigned src);
  91. /* dest -= src, where 0 <= src < 2^16 */
  92. extern int (*bnSubQ)(struct BigNum *dest, unsigned src);
  93. /* Return sign (-1, 0, +1) of a-b.  a <=> b --> bnCmp(a, b) <=> 0 */
  94. extern int (*bnCmp)(struct BigNum const *a, struct BigNum const *b);
  95. /* dest = src^2.  dest may be the same as src, but it costs time. */
  96. extern int (*bnSquare)(struct BigNum *dest, struct BigNum const *src);
  97. /* dest = a * b.  dest may be the same as a or b, but it costs time. */
  98. extern int (*bnMul)(struct BigNum *dest, struct BigNum const *a,
  99. struct BigNum const *b);
  100. /* dest = a * b, where 0 <= b < 2^16.  dest and a may be the same. */
  101. extern int (*bnMulQ)(struct BigNum *dest, struct BigNum const *a, unsigned b);
  102. /*
  103.  * q = n/d, r = n%d.  r may be the same as n, but not d,
  104.  * and q may not be the same as n or d.
  105.  * re-entrancy issue: this temporarily modifies d, but restores
  106.  * it for return.
  107.  */
  108. extern int (*bnDivMod)(struct BigNum *q, struct BigNum *r,
  109. struct BigNum const *n, struct BigNum const *d);
  110. /*
  111.  * dest = src % d.  dest and src may be the same, but not dest and d.
  112.  * re-entrancy issue: this temporarily modifies d, but restores
  113.  * it for return.
  114.  */
  115. extern int (*bnMod)(struct BigNum *dest, struct BigNum const *src,
  116. struct BigNum const *d);
  117. /* return src % d, where 0 <= d < 2^16.  */
  118. extern unsigned int (*bnModQ)(struct BigNum const *src, unsigned d);
  119. /* n = n^exp, modulo "mod"   "mod" *must* be odd */
  120. extern int (*bnExpMod)(struct BigNum *result, struct BigNum const *n,
  121. struct BigNum const *exp, struct BigNum const *mod);
  122. /*
  123.  * dest = n1^e1 * n2^e2, modulo "mod".  "mod" *must* be odd.
  124.  * dest may be the same as n1 or n2.
  125.  */
  126. extern int (*bnDoubleExpMod)(struct BigNum *dest,
  127. struct BigNum const *n1, struct BigNum const *e1,
  128. struct BigNum const *n2, struct BigNum const *e2,
  129. struct BigNum const *mod);
  130. /* n = 2^exp, modulo "mod"   "mod" *must* be odd */
  131. extern int (*bnTwoExpMod)(struct BigNum *n, struct BigNum const *exp,
  132. struct BigNum const *mod);
  133. /* dest = gcd(a, b).  The inputs may overlap arbitrarily. */
  134. extern int (*bnGcd)(struct BigNum *dest, struct BigNum const *a,
  135. struct BigNum const *b);
  136. /* dest = src^-1, modulo "mod".  dest may be the same as src. */
  137. extern int (*bnInv)(struct BigNum *dest, struct BigNum const *src,
  138. struct BigNum const *mod);
  139. /* Shift dest left "amt" places */
  140. extern int (*bnLShift)(struct BigNum *dest, unsigned amt);
  141. /* Shift dest right "amt" places, discarding low-order bits */
  142. extern void (*bnRShift)(struct BigNum *dest, unsigned amt);
  143. /* For the largest 2^k that divides n, divide n by it and return k. */
  144. extern unsigned (*bnMakeOdd)(struct BigNum *n);
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif/* !BN_H */