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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef BIGINT_IMPL_HEADER
  31. #define BIGINT_IMPL_HEADER
  32. /* Maintain a number of precomputed variables when doing reduction */
  33. #define BIGINT_M_OFFSET 0 /**< Normal modulo offset. */
  34. #ifdef CONFIG_BIGINT_CRT
  35. #define BIGINT_P_OFFSET 1 /**< p modulo offset. */
  36. #define BIGINT_Q_OFFSET 2 /**< q module offset. */
  37. #define BIGINT_NUM_MODS 3 /**< The number of modulus constants used. */
  38. #else
  39. #define BIGINT_NUM_MODS 1
  40. #endif
  41. /* Architecture specific functions for big ints */
  42. #if defined(CONFIG_INTEGER_8BIT)
  43. #define COMP_RADIX 256U /**< Max component + 1 */
  44. #define COMP_MAX 0xFFFFU/**< (Max dbl comp -1) */
  45. #define COMP_BIT_SIZE 8 /**< Number of bits in a component. */
  46. #define COMP_BYTE_SIZE 1 /**< Number of bytes in a component. */
  47. #define COMP_NUM_NIBBLES 2 /**< Used For diagnostics only. */
  48. typedef uint8_t comp; /**< A single precision component. */
  49. typedef uint16_t long_comp; /**< A double precision component. */
  50. typedef int16_t slong_comp; /**< A signed double precision component. */
  51. #elif defined(CONFIG_INTEGER_16BIT)
  52. #define COMP_RADIX 65536U /**< Max component + 1 */
  53. #define COMP_MAX 0xFFFFFFFFU/**< (Max dbl comp -1) */
  54. #define COMP_BIT_SIZE 16 /**< Number of bits in a component. */
  55. #define COMP_BYTE_SIZE 2 /**< Number of bytes in a component. */
  56. #define COMP_NUM_NIBBLES 4 /**< Used For diagnostics only. */
  57. typedef uint16_t comp; /**< A single precision component. */
  58. typedef uint32_t long_comp; /**< A double precision component. */
  59. typedef int32_t slong_comp; /**< A signed double precision component. */
  60. #else /* regular 32 bit */
  61. #ifdef WIN32
  62. #define COMP_RADIX 4294967296i64
  63. #define COMP_MAX 0xFFFFFFFFFFFFFFFFui64
  64. #else
  65. #define COMP_RADIX 4294967296ULL /**< Max component + 1 */
  66. #define COMP_MAX 0xFFFFFFFFFFFFFFFFULL/**< (Max dbl comp -1) */
  67. #endif
  68. #define COMP_BIT_SIZE 32 /**< Number of bits in a component. */
  69. #define COMP_BYTE_SIZE 4 /**< Number of bytes in a component. */
  70. #define COMP_NUM_NIBBLES 8 /**< Used For diagnostics only. */
  71. typedef uint32_t comp; /**< A single precision component. */
  72. typedef uint64_t long_comp; /**< A double precision component. */
  73. typedef int64_t slong_comp; /**< A signed double precision component. */
  74. #endif
  75. /**
  76. * @struct _bigint
  77. * @brief A big integer basic object
  78. */
  79. struct _bigint
  80. {
  81. struct _bigint* next; /**< The next bigint in the cache. */
  82. short size; /**< The number of components in this bigint. */
  83. short max_comps; /**< The heapsize allocated for this bigint */
  84. int refs; /**< An internal reference count. */
  85. comp* comps; /**< A ptr to the actual component data */
  86. };
  87. typedef struct _bigint bigint; /**< An alias for _bigint */
  88. /**
  89. * Maintains the state of the cache, and a number of variables used in
  90. * reduction.
  91. */
  92. typedef struct /**< A big integer "session" context. */
  93. {
  94. bigint *active_list; /**< Bigints currently used. */
  95. bigint *free_list; /**< Bigints not used. */
  96. bigint *bi_radix; /**< The radix used. */
  97. bigint *bi_mod[BIGINT_NUM_MODS]; /**< modulus */
  98. #if defined(CONFIG_BIGINT_MONTGOMERY)
  99. bigint *bi_RR_mod_m[BIGINT_NUM_MODS]; /**< R^2 mod m */
  100. bigint *bi_R_mod_m[BIGINT_NUM_MODS]; /**< R mod m */
  101. comp N0_dash[BIGINT_NUM_MODS];
  102. #elif defined(CONFIG_BIGINT_BARRETT)
  103. bigint *bi_mu[BIGINT_NUM_MODS]; /**< Storage for mu */
  104. #endif
  105. bigint *bi_normalised_mod[BIGINT_NUM_MODS]; /**< Normalised mod storage. */
  106. bigint **g; /**< Used by sliding-window. */
  107. int window; /**< The size of the sliding window */
  108. int active_count; /**< Number of active bigints. */
  109. int free_count; /**< Number of free bigints. */
  110. #ifdef CONFIG_BIGINT_MONTGOMERY
  111. uint8_t use_classical; /**< Use classical reduction. */
  112. #endif
  113. uint8_t mod_offset; /**< The mod offset we are using */
  114. } BI_CTX;
  115. #ifndef WIN32
  116. #define max(a,b) ((a)>(b)?(a):(b)) /**< Find the maximum of 2 numbers. */
  117. #define min(a,b) ((a)<(b)?(a):(b)) /**< Find the minimum of 2 numbers. */
  118. #endif
  119. #define PERMANENT 0x7FFF55AA /**< A magic number for permanents. */
  120. #endif