modexp_utils.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef _MONTGOMERY_UTILS_H
  2. #define _MONTGOMERY_UTILS_H
  3. #include "common.h"
  4. void expand_seed(uint64_t seed_in, void* seed_out, size_t out_len);
  5. struct BitWindow_LR {
  6. /** Size of a window, in bits **/
  7. unsigned window_size;
  8. /** Total number of windows covering the exponent **/
  9. unsigned nr_windows;
  10. /** Number of bits we miss for the next digit **/
  11. unsigned tg;
  12. /** Number of rightmost bits that have not been used yet **/
  13. unsigned available;
  14. /** Index to the byte in the big-endian exponent currently scanned **/
  15. unsigned scan_exp;
  16. /** Exponent where we extract digits from **/
  17. const uint8_t *exp;
  18. };
  19. struct BitWindow_RL {
  20. unsigned window_size;
  21. unsigned nr_windows;
  22. unsigned bytes_left;
  23. unsigned bits_left;
  24. const uint8_t *cursor;
  25. };
  26. /**
  27. * Initialize the data structure we can use to read groups of bits (windows)
  28. * from a big endian number.
  29. */
  30. struct BitWindow_LR init_bit_window_lr(unsigned window_size, const uint8_t *exp, size_t exp_len);
  31. struct BitWindow_RL init_bit_window_rl(unsigned window_size, const uint8_t *exp, size_t exp_len);
  32. /**
  33. * Return the next window.
  34. */
  35. unsigned get_next_digit_lr(struct BitWindow_LR *bw);
  36. unsigned get_next_digit_rl(struct BitWindow_RL *bw);
  37. typedef struct _ProtMemory {
  38. void *scattered;
  39. uint16_t *scramble;
  40. unsigned nr_arrays;
  41. unsigned array_len;
  42. } ProtMemory;
  43. int scatter(ProtMemory** pprot, const void *arrays[], uint8_t nr_arrays, size_t array_len, uint64_t seed);
  44. void gather(void *out, const ProtMemory *prot, unsigned index);
  45. void free_scattered(ProtMemory *prot);
  46. #endif