You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bigint_impl.h 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright(C) 2006 Cameron Rich
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2.1 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef BIGINT_IMPL_HEADER
  19. #define BIGINT_IMPL_HEADER
  20. /* Maintain a number of precomputed variables when doing reduction */
  21. #define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */
  22. #ifdef CONFIG_BIGINT_CRT
  23. #define BIGINT_P_OFFSET 1 /**< p modulo offset. */
  24. #define BIGINT_Q_OFFSET 2 /**< q module offset. */
  25. #define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */
  26. #else
  27. #define BIGINT_NUM_MODS 1
  28. #endif
  29. /* Architecture specific functions for big ints */
  30. #ifdef WIN32
  31. #define COMP_RADIX 4294967296i64
  32. #define COMP_BIG_MSB 0x8000000000000000i64
  33. #else
  34. #define COMP_RADIX 4294967296ULL /**< Max component + 1 */
  35. #define COMP_BIG_MSB 0x8000000000000000ULL /**< (Max dbl comp + 1)/ 2 */
  36. #endif
  37. #define COMP_BIT_SIZE 32 /**< Number of bits in a component. */
  38. #define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */
  39. #define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */
  40. typedef uint32_t comp; /**< A single precision component. */
  41. typedef uint64_t long_comp; /**< A double precision component. */
  42. typedef int64_t slong_comp; /**< A signed double precision component. */
  43. /**
  44. * @struct _bigint
  45. * @brief A big integer basic object
  46. */
  47. struct _bigint
  48. {
  49. struct _bigint* next; /**< The next bigint in the cache. */
  50. short size; /**< The number of components in this bigint. */
  51. short max_comps; /**< The heapsize allocated for this bigint */
  52. int refs; /**< An internal reference count. */
  53. comp* comps; /**< A ptr to the actual component data */
  54. };
  55. typedef struct _bigint bigint; /**< An alias for _bigint */
  56. /**
  57. * Maintains the state of the cache, and a number of variables used in
  58. * reduction.
  59. */
  60. typedef struct /**< A big integer "session" context. */
  61. {
  62. bigint *active_list; /**< Bigints currently used. */
  63. bigint *free_list; /**< Bigints not used. */
  64. bigint *bi_radix; /**< The radix used. */
  65. bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */
  66. #if defined(CONFIG_BIGINT_MONTGOMERY)
  67. bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */
  68. bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */
  69. comp N0_dash[BIGINT_NUM_MODS];
  70. #elif defined(CONFIG_BIGINT_BARRETT)
  71. bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */
  72. #endif
  73. bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */
  74. bigint **g; /**< Used by sliding-window. */
  75. int window; /**< The size of the sliding window */
  76. int active_count; /**< Number of active bigints. */
  77. int free_count; /**< Number of free bigints. */
  78. #ifdef CONFIG_BIGINT_MONTGOMERY
  79. uint8_t use_classical; /**< Use classical reduction. */
  80. #endif
  81. uint8_t mod_offset; /**< The mod offset we are using */
  82. } BI_CTX;
  83. #ifndef WIN32
  84. #define max(a,b) ((a)>(b)?(a):(b)) /**< Find the maximum of 2 numbers. */
  85. #define min(a,b) ((a)<(b)?(a):(b)) /**< Find the minimum of 2 numbers. */
  86. #endif
  87. #define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */
  88. #define V1 v->comps[v->size-1] /**< v1 for division */
  89. #define V2 v->comps[v->size-2] /**< v2 for division */
  90. #define U(j) tmp_u->comps[tmp_u->size-j-1] /**< uj for division */
  91. #define Q(j) quotient->comps[quotient->size-j-1] /**< qj for division */
  92. #endif