mont.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef _MONT_H
  2. #define _MONT_H
  3. #include "common.h"
  4. /*
  5. * How many numbers in Montgomery form a temporary scratchpad should contain.
  6. */
  7. #define SCRATCHPAD_NR 7
  8. typedef enum _ModulusType { ModulusGeneric, ModulusP256, ModulusP384, ModulusP521 } ModulusType;
  9. typedef struct mont_context {
  10. ModulusType modulus_type;
  11. unsigned words; /* Number of words allocated to hold the number */
  12. unsigned bytes; /* Number of bytes allocated to hold the number */
  13. unsigned modulus_len; /* Max bytes taken by an affine coordinate */
  14. uint64_t *modulus;
  15. uint64_t *one;
  16. uint64_t *r2_mod_n; /* R^2 mod N */
  17. uint64_t m0;
  18. uint64_t *r_mod_n; /* R mod N */
  19. uint64_t *modulus_min_2;
  20. } MontContext;
  21. int mont_context_init(MontContext **out, const uint8_t *modulus, size_t mod_len);
  22. void mont_context_free(MontContext *ctx);
  23. size_t mont_bytes(const MontContext *ctx);
  24. int mont_number(uint64_t **out, unsigned count, const struct mont_context *ctx);
  25. int mont_random_number(uint64_t **out, unsigned count, uint64_t seed, const struct mont_context *ctx);
  26. int mont_from_bytes(uint64_t **out, const uint8_t *number, size_t len, const MontContext *ctx);
  27. int mont_to_bytes(uint8_t *number, size_t len, const uint64_t* mont_number, const MontContext *ctx);
  28. int mont_add(uint64_t* out, const uint64_t* a, const uint64_t* b, uint64_t *tmp, const MontContext *ctx);
  29. int mont_mult(uint64_t* out, const uint64_t* a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx);
  30. int mont_shift_left(uint64_t* out, const uint64_t* a, uint64_t k, const MontContext *ctx);
  31. int mont_sub(uint64_t *out, const uint64_t *a, const uint64_t *b, uint64_t *tmp, const MontContext *ctx);
  32. int mont_inv_prime(uint64_t *out, uint64_t *a, const MontContext *ctx);
  33. int mont_set(uint64_t *out, uint64_t x, const MontContext *ctx);
  34. int mont_is_zero(const uint64_t *a, const MontContext *ctx);
  35. int mont_is_one(const uint64_t *a, const MontContext *ctx);
  36. int mont_is_equal(const uint64_t *a, const uint64_t *b, const MontContext *ctx);
  37. int mont_copy(uint64_t *out, const uint64_t *a, const MontContext *ctx);
  38. #endif