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.h 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 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_HEADER
  19. #define BIGINT_HEADER
  20. /* enable features based on a 'super-set' capbaility. */
  21. #if defined(CONFIG_SSL_FULL_MODE)
  22. #define CONFIG_SSL_ENABLE_CLIENT
  23. #define CONFIG_SSL_CERT_VERIFICATION
  24. #elif defined(CONFIG_SSL_ENABLE_CLIENT)
  25. #define CONFIG_SSL_CERT_VERIFICATION
  26. #endif
  27. #include "os_port.h"
  28. #include "bigint_impl.h"
  29. #ifndef CONFIG_BIGINT_CHECK_ON
  30. #define check(A) /**< disappears in normal production mode */
  31. #endif
  32. BI_CTX *bi_initialize(void);
  33. void bi_terminate(BI_CTX *ctx);
  34. void bi_permanent(bigint *bi);
  35. void bi_depermanent(bigint *bi);
  36. void bi_free(BI_CTX *ctx, bigint *bi);
  37. bigint *bi_copy(bigint *bi);
  38. bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
  39. void bi_export(BI_CTX *ctx, bigint *bi, uint8_t *data, int size);
  40. bigint *bi_import(BI_CTX *ctx, const uint8_t *data, int len);
  41. bigint *int_to_bi(BI_CTX *ctx, comp i);
  42. /* the functions that actually do something interesting */
  43. bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
  44. bigint *bi_subtract(BI_CTX *ctx, bigint *bia,
  45. bigint *bib, int *is_negative);
  46. bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod);
  47. bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
  48. bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
  49. bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp);
  50. int bi_compare(bigint *bia, bigint *bib);
  51. void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
  52. void bi_free_mod(BI_CTX *ctx, int mod_offset);
  53. #ifdef CONFIG_SSL_FULL_MODE
  54. void bi_print(const char *label, bigint *bi);
  55. bigint *bi_str_import(BI_CTX *ctx, const char *data);
  56. #endif
  57. /**
  58. * @def bi_mod
  59. * Find the residue of B. bi_set_mod() must be called before hand.
  60. */
  61. #define bi_mod(A, B) bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)
  62. /**
  63. * bi_residue() is technically the same as bi_mod(), but it uses the
  64. * appropriate reduction technique (which is bi_mod() when doing classical
  65. * reduction).
  66. */
  67. #if defined(CONFIG_BIGINT_MONTGOMERY)
  68. #define bi_residue(A, B) bi_mont(A, B)
  69. bigint *bi_mont(BI_CTX *ctx, bigint *bixy);
  70. #elif defined(CONFIG_BIGINT_BARRETT)
  71. #define bi_residue(A, B) bi_barrett(A, B)
  72. bigint *bi_barrett(BI_CTX *ctx, bigint *bi);
  73. #else /* if defined(CONFIG_BIGINT_CLASSICAL) */
  74. #define bi_residue(A, B) bi_mod(A, B)
  75. #endif
  76. #ifdef CONFIG_BIGINT_SQUARE
  77. bigint *bi_square(BI_CTX *ctx, bigint *bi);
  78. #else
  79. #define bi_square(A, B) bi_multiply(A, bi_copy(B), B)
  80. #endif
  81. #endif