bigint.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* bigint.h */
  2. /*
  3. This file is part of the ARM-Crypto-Lib.
  4. Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file bigint.h
  18. * \author Daniel Otte
  19. * \date 2010-02-22
  20. *
  21. * \license GPLv3 or later
  22. *
  23. */
  24. #ifndef BIGINT_H_
  25. #define BIGINT_H_
  26. #include <stdint.h>
  27. typedef uint32_t bigint_word_t;
  28. typedef uint64_t bigint_wordplus_t;
  29. typedef int64_t bigint_wordplus_signed_t;
  30. #define BIGINT_WORD_SIZE 32
  31. #define BIGINT_FBS_MASK (BIGINT_WORD_SIZE-1) /* the last five bits indicate which is the first bit set */
  32. #define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */
  33. typedef struct{
  34. uint16_t length_W;
  35. uint8_t info;
  36. bigint_word_t *wordv; /* word vector, pointing to the LSB */
  37. }bigint_t;
  38. /******************************************************************************/
  39. void bigint_adjust(bigint_t* a);
  40. uint32_t bigint_get_first_set_bit(const bigint_t* a);
  41. uint32_t bigint_get_last_set_bit(const bigint_t* a);
  42. uint16_t bigint_length_b(const bigint_t* a);
  43. uint16_t bigint_length_B(const bigint_t* a);
  44. void bigint_copy(bigint_t* dest, const bigint_t* src);
  45. void bigint_add_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  46. void bigint_add_scale_u(bigint_t* dest, const bigint_t* a, uint16_t scale);
  47. void bigint_sub_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  48. int8_t bigint_cmp_u(const bigint_t * a, const bigint_t * b);
  49. void bigint_add_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  50. void bigint_sub_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  51. int8_t bigint_cmp_s(const bigint_t* a, const bigint_t* b);
  52. void bigint_shiftleft(bigint_t* a, uint16_t shift);
  53. void bigint_shiftright(bigint_t* a, uint16_t shift);
  54. void bigint_xor(bigint_t* dest, const bigint_t* a);
  55. void bigint_set_zero(bigint_t* a);
  56. void bigint_mul_u(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  57. void bigint_mul_s(bigint_t* dest, const bigint_t* a, const bigint_t* b);
  58. void bigint_square(bigint_t* dest, const bigint_t* a);
  59. void bigint_sub_u_bitscale(bigint_t* a, const bigint_t* b, uint16_t bitscale);
  60. void bigint_reduce(bigint_t* a, const bigint_t* r);
  61. void bigint_expmod_u(bigint_t* dest, const bigint_t* a, const bigint_t* exp, const bigint_t* r);
  62. void bigint_gcdext(bigint_t* gcd, bigint_t* a, bigint_t* b, const bigint_t* x, const bigint_t* y);
  63. void bigint_inverse(bigint_t* dest, const bigint_t* a, const bigint_t* m);
  64. void bigint_changeendianess(bigint_t* a);
  65. /******************************************************************************/
  66. #endif /*BIGINT_H_*/