-
Notifications
You must be signed in to change notification settings - Fork 0
/
mg.h
67 lines (59 loc) · 1.33 KB
/
mg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef _MG_H
#define _MG_H
#include <gmp.h>
#include <stdbool.h>
/**
* @brief Structure holding key information for the Montgomery form
*
*/
typedef struct __mg_t
{
bool init;
mpz_t ctx;
mpz_t r;
mpz_t r_mask;
mpz_t r_sq;
mpz_t n;
mpz_t n_inv;
} mg_t;
/**
* @brief Initializes the mg_t structure
*
* @param mg struct mg_t to be initialized
* @param n modulus
* @return 0 on success, other on error.
*/
int mg_init(mg_t *mg, mpz_t n);
/**
* @brief Initializes the mg_t structure with a specific value for r
*
* @param mg struct mg_t to be initialized
* @param r power of two bigger than n
* @param n modulus
* @return 0 on success, other on error.
*/
int mg_init_r(mg_t *mg, mpz_t r, mpz_t n);
void print_mg_struct(const mg_t *mg);
int mg_release(mg_t *mg);
/**
* @brief Performs x * r^-1 (mod n) without using divisions.
*
* @param mg
* @param x
*/
void mg_redc(mg_t *mg, mpz_t x);
/**
* @brief Converts x in montgomery form
*
* @param mg mg_t struct associated with the transformation
* @param x OUT -- x gets converted into montgomery form
*/
void mg_i2mg(mg_t *mg, mpz_t x);
/**
* @brief Converts x out of montgomery form
*
* @param mg mg_t struct associated with the transformation
* @param x OUT -- x gets converted out of montgomery form
*/
void mg_mg2i(mg_t *mg, mpz_t x);
#endif