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