bignum.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Copyright (c) 2013-2014 Tomas Dzetkulic
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. * Copyright (c) 2016 Alex Beregszaszi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  20. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef __BIGNUM_H__
  25. #define __BIGNUM_H__
  26. #include <stdbool.h>
  27. #include <stddef.h>
  28. #include <stdint.h>
  29. #include "options.h"
  30. #define BN_LIMBS 9
  31. #define BN_BITS_PER_LIMB 29
  32. #define BN_BASE (1u << BN_BITS_PER_LIMB)
  33. #define BN_LIMB_MASK ((1u << BN_BITS_PER_LIMB) - 1)
  34. #define BN_EXTRA_BITS (32 - BN_BITS_PER_LIMB)
  35. #define BN_BITS_LAST_LIMB (256 - (BN_LIMBS - 1) * BN_BITS_PER_LIMB)
  36. // Represents the number sum([val[i] * 2**(29*i) for i in range(9))
  37. typedef struct {
  38. uint32_t val[BN_LIMBS];
  39. } bignum256;
  40. static inline uint32_t read_be(const uint8_t* data) {
  41. return (((uint32_t)data[0]) << 24) | (((uint32_t)data[1]) << 16) | (((uint32_t)data[2]) << 8) |
  42. (((uint32_t)data[3]));
  43. }
  44. static inline void write_be(uint8_t* data, uint32_t x) {
  45. data[0] = x >> 24;
  46. data[1] = x >> 16;
  47. data[2] = x >> 8;
  48. data[3] = x;
  49. }
  50. static inline uint32_t read_le(const uint8_t* data) {
  51. return (((uint32_t)data[3]) << 24) | (((uint32_t)data[2]) << 16) | (((uint32_t)data[1]) << 8) |
  52. (((uint32_t)data[0]));
  53. }
  54. static inline void write_le(uint8_t* data, uint32_t x) {
  55. data[3] = x >> 24;
  56. data[2] = x >> 16;
  57. data[1] = x >> 8;
  58. data[0] = x;
  59. }
  60. void bn_read_be(const uint8_t* in_number, bignum256* out_number);
  61. void bn_write_be(const bignum256* in_number, uint8_t* out_number);
  62. void bn_read_le(const uint8_t* in_number, bignum256* out_number);
  63. void bn_write_le(const bignum256* in_number, uint8_t* out_number);
  64. void bn_read_uint32(uint32_t in_number, bignum256* out_number);
  65. void bn_read_uint64(uint64_t in_number, bignum256* out_number);
  66. int bn_bitcount(const bignum256* x);
  67. unsigned int bn_digitcount(const bignum256* x);
  68. void bn_zero(bignum256* x);
  69. void bn_one(bignum256* x);
  70. int bn_is_zero(const bignum256* x);
  71. int bn_is_one(const bignum256* x);
  72. int bn_is_less(const bignum256* x, const bignum256* y);
  73. int bn_is_equal(const bignum256* x, const bignum256* y);
  74. void bn_cmov(
  75. bignum256* res,
  76. volatile uint32_t cond,
  77. const bignum256* truecase,
  78. const bignum256* falsecase);
  79. void bn_cnegate(volatile uint32_t cond, bignum256* x, const bignum256* prime);
  80. void bn_lshift(bignum256* x);
  81. void bn_rshift(bignum256* x);
  82. void bn_setbit(bignum256* x, uint16_t i);
  83. void bn_clearbit(bignum256* x, uint16_t i);
  84. uint32_t bn_testbit(const bignum256* x, uint16_t i);
  85. void bn_xor(bignum256* res, const bignum256* x, const bignum256* y);
  86. void bn_mult_half(bignum256* x, const bignum256* prime);
  87. void bn_mult_k(bignum256* x, uint8_t k, const bignum256* prime);
  88. void bn_mod(bignum256* x, const bignum256* prime);
  89. void bn_multiply(const bignum256* k, bignum256* x, const bignum256* prime);
  90. void bn_fast_mod(bignum256* x, const bignum256* prime);
  91. void bn_power_mod(const bignum256* x, const bignum256* e, const bignum256* prime, bignum256* res);
  92. void bn_sqrt(bignum256* x, const bignum256* prime);
  93. uint32_t inverse_mod_power_two(uint32_t a, uint32_t n);
  94. void bn_divide_base(bignum256* x, const bignum256* prime);
  95. void bn_normalize(bignum256* x);
  96. void bn_add(bignum256* x, const bignum256* y);
  97. void bn_addmod(bignum256* x, const bignum256* y, const bignum256* prime);
  98. void bn_addi(bignum256* x, uint32_t y);
  99. void bn_subi(bignum256* x, uint32_t y, const bignum256* prime);
  100. void bn_subtractmod(const bignum256* x, const bignum256* y, bignum256* res, const bignum256* prime);
  101. void bn_subtract(const bignum256* x, const bignum256* y, bignum256* res);
  102. void bn_long_division(bignum256* x, uint32_t d, bignum256* q, uint32_t* r);
  103. void bn_divmod58(bignum256* x, uint32_t* r);
  104. void bn_divmod1000(bignum256* x, uint32_t* r);
  105. void bn_inverse(bignum256* x, const bignum256* prime);
  106. size_t bn_format(
  107. const bignum256* amount,
  108. const char* prefix,
  109. const char* suffix,
  110. unsigned int decimals,
  111. int exponent,
  112. bool trailing,
  113. char thousands,
  114. char* output,
  115. size_t output_length);
  116. // Returns (uint32_t) in_number
  117. // Assumes in_number < 2**32
  118. // Assumes in_number is normalized
  119. static inline uint32_t bn_write_uint32(const bignum256* in_number) {
  120. return in_number->val[0] | (in_number->val[1] << BN_BITS_PER_LIMB);
  121. }
  122. // Returns (uint64_t) in_number
  123. // Assumes in_number < 2**64
  124. // Assumes in_number is normalized
  125. static inline uint64_t bn_write_uint64(const bignum256* in_number) {
  126. uint64_t acc;
  127. acc = in_number->val[2];
  128. acc <<= BN_BITS_PER_LIMB;
  129. acc |= in_number->val[1];
  130. acc <<= BN_BITS_PER_LIMB;
  131. acc |= in_number->val[0];
  132. return acc;
  133. }
  134. // y = x
  135. static inline void bn_copy(const bignum256* x, bignum256* y) {
  136. *y = *x;
  137. }
  138. // Returns x % 2 == 0
  139. static inline int bn_is_even(const bignum256* x) {
  140. return (x->val[0] & 1) == 0;
  141. }
  142. // Returns x % 2 == 0
  143. static inline int bn_is_odd(const bignum256* x) {
  144. return (x->val[0] & 1) == 1;
  145. }
  146. static inline size_t bn_format_uint64(
  147. uint64_t amount,
  148. const char* prefix,
  149. const char* suffix,
  150. unsigned int decimals,
  151. int exponent,
  152. bool trailing,
  153. char thousands,
  154. char* output,
  155. size_t output_length) {
  156. bignum256 bn_amount;
  157. bn_read_uint64(amount, &bn_amount);
  158. return bn_format(
  159. &bn_amount, prefix, suffix, decimals, exponent, trailing, thousands, output, output_length);
  160. }
  161. static inline size_t bn_format_amount(
  162. uint64_t amount,
  163. const char* prefix,
  164. const char* suffix,
  165. unsigned int decimals,
  166. char* output,
  167. size_t output_length) {
  168. return bn_format_uint64(
  169. amount, prefix, suffix, decimals, 0, false, ',', output, output_length);
  170. }
  171. #if USE_BN_PRINT
  172. void bn_print(const bignum256* x);
  173. void bn_print_raw(const bignum256* x);
  174. #endif
  175. #endif