field.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**********************************************************************
  2. * Copyright (c) 2013, 2014 Pieter Wuille *
  3. * Distributed under the MIT software license, see the accompanying *
  4. * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
  5. **********************************************************************/
  6. #ifndef _SECP256K1_FIELD_
  7. #define _SECP256K1_FIELD_
  8. /** Field element module.
  9. *
  10. * Field elements can be represented in several ways, but code accessing
  11. * it (and implementations) need to take certain properaties into account:
  12. * - Each field element can be normalized or not.
  13. * - Each field element has a magnitude, which represents how far away
  14. * its representation is away from normalization. Normalized elements
  15. * always have a magnitude of 1, but a magnitude of 1 doesn't imply
  16. * normality.
  17. */
  18. #if defined HAVE_CONFIG_H
  19. #include "libsecp256k1-config.h"
  20. #endif
  21. #if defined(USE_FIELD_10X26)
  22. #include "field_10x26.h"
  23. #elif defined(USE_FIELD_5X52)
  24. #include "field_5x52.h"
  25. #else
  26. #error "Please select field implementation"
  27. #endif
  28. /** Normalize a field element. */
  29. static void secp256k1_fe_normalize(secp256k1_fe_t *r);
  30. /** Weakly normalize a field element: reduce it magnitude to 1, but don't fully normalize. */
  31. static void secp256k1_fe_normalize_weak(secp256k1_fe_t *r);
  32. /** Normalize a field element, without constant-time guarantee. */
  33. static void secp256k1_fe_normalize_var(secp256k1_fe_t *r);
  34. /** Verify whether a field element represents zero i.e. would normalize to a zero value. The field
  35. * implementation may optionally normalize the input, but this should not be relied upon. */
  36. static int secp256k1_fe_normalizes_to_zero(secp256k1_fe_t *r);
  37. /** Verify whether a field element represents zero i.e. would normalize to a zero value. The field
  38. * implementation may optionally normalize the input, but this should not be relied upon. */
  39. static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe_t *r);
  40. /** Set a field element equal to a small integer. Resulting field element is normalized. */
  41. static void secp256k1_fe_set_int(secp256k1_fe_t *r, int a);
  42. /** Verify whether a field element is zero. Requires the input to be normalized. */
  43. static int secp256k1_fe_is_zero(const secp256k1_fe_t *a);
  44. /** Check the "oddness" of a field element. Requires the input to be normalized. */
  45. static int secp256k1_fe_is_odd(const secp256k1_fe_t *a);
  46. /** Compare two field elements. Requires magnitude-1 inputs. */
  47. static int secp256k1_fe_equal_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
  48. /** Compare two field elements. Requires both inputs to be normalized */
  49. static int secp256k1_fe_cmp_var(const secp256k1_fe_t *a, const secp256k1_fe_t *b);
  50. /** Set a field element equal to 32-byte big endian value. If succesful, the resulting field element is normalized. */
  51. static int secp256k1_fe_set_b32(secp256k1_fe_t *r, const unsigned char *a);
  52. /** Convert a field element to a 32-byte big endian value. Requires the input to be normalized */
  53. static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe_t *a);
  54. /** Set a field element equal to the additive inverse of another. Takes a maximum magnitude of the input
  55. * as an argument. The magnitude of the output is one higher. */
  56. static void secp256k1_fe_negate(secp256k1_fe_t *r, const secp256k1_fe_t *a, int m);
  57. /** Multiplies the passed field element with a small integer constant. Multiplies the magnitude by that
  58. * small integer. */
  59. static void secp256k1_fe_mul_int(secp256k1_fe_t *r, int a);
  60. /** Adds a field element to another. The result has the sum of the inputs' magnitudes as magnitude. */
  61. static void secp256k1_fe_add(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  62. /** Sets a field element to be the product of two others. Requires the inputs' magnitudes to be at most 8.
  63. * The output magnitude is 1 (but not guaranteed to be normalized). */
  64. static void secp256k1_fe_mul(secp256k1_fe_t *r, const secp256k1_fe_t *a, const secp256k1_fe_t * SECP256K1_RESTRICT b);
  65. /** Sets a field element to be the square of another. Requires the input's magnitude to be at most 8.
  66. * The output magnitude is 1 (but not guaranteed to be normalized). */
  67. static void secp256k1_fe_sqr(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  68. /** Sets a field element to be the (modular) square root (if any exist) of another. Requires the
  69. * input's magnitude to be at most 8. The output magnitude is 1 (but not guaranteed to be
  70. * normalized). Return value indicates whether a square root was found. */
  71. static int secp256k1_fe_sqrt_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  72. /** Sets a field element to be the (modular) inverse of another. Requires the input's magnitude to be
  73. * at most 8. The output magnitude is 1 (but not guaranteed to be normalized). */
  74. static void secp256k1_fe_inv(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  75. /** Potentially faster version of secp256k1_fe_inv, without constant-time guarantee. */
  76. static void secp256k1_fe_inv_var(secp256k1_fe_t *r, const secp256k1_fe_t *a);
  77. /** Calculate the (modular) inverses of a batch of field elements. Requires the inputs' magnitudes to be
  78. * at most 8. The output magnitudes are 1 (but not guaranteed to be normalized). The inputs and
  79. * outputs must not overlap in memory. */
  80. static void secp256k1_fe_inv_all_var(size_t len, secp256k1_fe_t *r, const secp256k1_fe_t *a);
  81. /** Convert a field element to the storage type. */
  82. static void secp256k1_fe_to_storage(secp256k1_fe_storage_t *r, const secp256k1_fe_t*);
  83. /** Convert a field element back from the storage type. */
  84. static void secp256k1_fe_from_storage(secp256k1_fe_t *r, const secp256k1_fe_storage_t*);
  85. /** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
  86. static void secp256k1_fe_storage_cmov(secp256k1_fe_storage_t *r, const secp256k1_fe_storage_t *a, int flag);
  87. /** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. */
  88. static void secp256k1_fe_cmov(secp256k1_fe_t *r, const secp256k1_fe_t *a, int flag);
  89. #endif