2
0

bn.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.c - the high-level bignum interface
  6. */
  7. #include "bn.h"
  8. /* Functions */
  9. void
  10. bnBegin(struct BigNum *bn)
  11. {
  12. static int bninit = 0;
  13. if (!bninit) {
  14. bnInit();
  15. bninit = 1;
  16. }
  17. bn->ptr = 0;
  18. bn->size = 0;
  19. bn->allocated = 0;
  20. }
  21. void
  22. bnSwap(struct BigNum *a, struct BigNum *b)
  23. {
  24. void *p;
  25. unsigned t;
  26. p = a->ptr;
  27. a->ptr = b->ptr;
  28. b->ptr = p;
  29. t = a->size;
  30. a->size = b->size;
  31. b->size = t;
  32. t = a->allocated;
  33. a->allocated = b->allocated;
  34. b->allocated = t;
  35. }
  36. int (*bnYield)(void);
  37. void (*bnEnd)(struct BigNum *bn);
  38. int (*bnPrealloc)(struct BigNum *bn, unsigned bits);
  39. int (*bnCopy)(struct BigNum *dest, struct BigNum const *src);
  40. void (*bnNorm)(struct BigNum *bn);
  41. void (*bnExtractBigBytes)(struct BigNum const *bn, unsigned char *dest,
  42. unsigned lsbyte, unsigned len);
  43. int (*bnInsertBigBytes)(struct BigNum *bn, unsigned char const *src,
  44. unsigned lsbyte, unsigned len);
  45. void (*bnExtractLittleBytes)(struct BigNum const *bn, unsigned char *dest,
  46. unsigned lsbyte, unsigned len);
  47. int (*bnInsertLittleBytes)(struct BigNum *bn, unsigned char const *src,
  48. unsigned lsbyte, unsigned len);
  49. unsigned (*bnLSWord)(struct BigNum const *src);
  50. int (*bnReadBit)(struct BigNum const *bn, unsigned bit);
  51. unsigned (*bnBits)(struct BigNum const *src);
  52. int (*bnAdd)(struct BigNum *dest, struct BigNum const *src);
  53. int (*bnSub)(struct BigNum *dest, struct BigNum const *src);
  54. int (*bnCmpQ)(struct BigNum const *a, unsigned b);
  55. int (*bnSetQ)(struct BigNum *dest, unsigned src);
  56. int (*bnAddQ)(struct BigNum *dest, unsigned src);
  57. int (*bnSubQ)(struct BigNum *dest, unsigned src);
  58. int (*bnCmp)(struct BigNum const *a, struct BigNum const *b);
  59. int (*bnSquare)(struct BigNum *dest, struct BigNum const *src);
  60. int (*bnMul)(struct BigNum *dest, struct BigNum const *a,
  61. struct BigNum const *b);
  62. int (*bnMulQ)(struct BigNum *dest, struct BigNum const *a, unsigned b);
  63. int (*bnDivMod)(struct BigNum *q, struct BigNum *r, struct BigNum const *n,
  64. struct BigNum const *d);
  65. int (*bnMod)(struct BigNum *dest, struct BigNum const *src,
  66. struct BigNum const *d);
  67. unsigned (*bnModQ)(struct BigNum const *src, unsigned d);
  68. int (*bnExpMod)(struct BigNum *result, struct BigNum const *n,
  69. struct BigNum const *exp, struct BigNum const *mod);
  70. int (*bnDoubleExpMod)(struct BigNum *dest,
  71. struct BigNum const *n1, struct BigNum const *e1,
  72. struct BigNum const *n2, struct BigNum const *e2,
  73. struct BigNum const *mod);
  74. int (*bnTwoExpMod)(struct BigNum *n, struct BigNum const *exp,
  75. struct BigNum const *mod);
  76. int (*bnGcd)(struct BigNum *dest, struct BigNum const *a,
  77. struct BigNum const *b);
  78. int (*bnInv)(struct BigNum *dest, struct BigNum const *src,
  79. struct BigNum const *mod);
  80. int (*bnLShift)(struct BigNum *dest, unsigned amt);
  81. void (*bnRShift)(struct BigNum *dest, unsigned amt);
  82. unsigned (*bnMakeOdd)(struct BigNum *n);
  83. int (*bnBasePrecompBegin)(struct BnBasePrecomp *pre, struct BigNum const *base,
  84. struct BigNum const *mod, unsigned maxebits);
  85. int (*bnBasePrecompCopy)(struct BnBasePrecomp *dst,
  86. struct BnBasePrecomp const *src);
  87. void (*bnBasePrecompEnd)(struct BnBasePrecomp *pre);
  88. int (*bnBasePrecompExpMod)(struct BigNum *dest,
  89. struct BnBasePrecomp const *pre, struct BigNum const *exp,
  90. struct BigNum const *mod);
  91. int (*bnDoubleBasePrecompExpMod)(struct BigNum *dest,
  92. struct BnBasePrecomp const *pre1, struct BigNum const *exp1,
  93. struct BnBasePrecomp const *pre2, struct BigNum const *exp2,
  94. struct BigNum const *mod);