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.

field.h 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef _SECP256K1_FIELD_
  2. #define _SECP256K1_FIELD_
  3. /** Field element module.
  4. *
  5. * Field elements can be represented in several ways, but code accessing
  6. * it (and implementations) need to take certain properaties into account:
  7. * - Each field element can be normalized or not.
  8. * - Each field element has a magnitude, which represents how far away
  9. * its representation is away from normalization. Normalized elements
  10. * always have a magnitude of 1, but a magnitude of 1 doesn't imply
  11. * normality.
  12. */
  13. #if defined(USE_FIELD_GMP)
  14. #include "field_gmp.h"
  15. #elif defined(USE_FIELD_10X26)
  16. #include "field_10x26.h"
  17. #elif defined(USE_FIELD_5X52)
  18. #include "field_5x52.h"
  19. #else
  20. #error "Please select field implementation"
  21. #endif
  22. typedef struct {
  23. secp256k1_num_t p;
  24. } secp256k1_fe_consts_t;
  25. static const secp256k1_fe_consts_t *secp256k1_fe_consts = NULL;
  26. /** Initialize field element precomputation data. */
  27. void static secp256k1_fe_start(void);
  28. /** Unload field element precomputation data. */
  29. void static secp256k1_fe_stop(void);
  30. /** Normalize a field element. */
  31. void static secp256k1_fe_normalize(secp256k1_fe_t *r);
  32. /** Set a field element equal to a small integer. Resulting field element is normalized. */
  33. void static secp256k1_fe_set_int(secp256k1_fe_t *r, int a);
  34. /** Verify whether a field element is zero. Requires the input to be normalized. */
  35. int static secp256k1_fe_is_zero(const secp256k1_fe_t *a);
  36. /** Check the "oddness" of a field element. Requires the input to be normalized. */
  37. int static secp256k1_fe_is_odd(const secp256k1_fe_t *a);
  38. /** Compare two field elements. Requires both inputs to be normalized */
  39. int static secp256k1_fe_equal(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
  40. /** Set a field element equal to 32-byte big endian value. Resulting field element is normalized. */
  41. void static secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a);
  42. /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
  43. void static secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a);
  44. /** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input
  45. * as an argument. The magnitude of the output is one higher. */
  46. void static secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m);
  47. /** Multiplies the passed field element with a small integer constant. Multiplies the magnitude by that
  48. * small integer. */
  49. void static secp256k1_fe_mul_int(secp256k1_fe_t *r, int a);
  50. /** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */
  51. void static secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  52. /** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.
  53. * The output magnitude is 1 (but not guaranteed to be normalized). */
  54. void static secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t *b);
  55. /** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.
  56. * The output magnitude is 1 (but not guaranteed to be normalized). */
  57. void static secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  58. /** Sets a field element to be the (modular) square root of another. Requires the inputs' magnitude to
  59. * be at most 8. The output magnitude is 1 (but not guaranteed to be normalized). */
  60. void static secp256k1_fe_sqrt(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  61. /** Sets a field element to be the (modular) inverse of another. Requires the input's magnitude to be
  62. * at most 8. The output magnitude is 1 (but not guaranteed to be normalized). */
  63. void static secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  64. /** Potentially faster version of secp256k1_fe_inv, without constant-time guarantee. */
  65. void static secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  66. /** Convert a field element to a hexadecimal string. */
  67. void static secp256k1_fe_get_hex(char *r, int *rlen, const secp256k1_fe_t *a);
  68. /** Convert a hexadecimal string to a field element. */
  69. void static secp256k1_fe_set_hex(secp256k1_fe_t *r, const char *a, int alen);
  70. #endif