forked from albertobsd/keysubtracter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmpecc.c
141 lines (134 loc) · 3.58 KB
/
gmpecc.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <gmp.h>
#include "gmpecc.h"
struct Elliptic_Curve EC;
struct Point G;
struct Point DoublingG[256];
void Point_Doubling(struct Point *P, struct Point *R) {
mpz_t slope, temp;
mpz_init(temp);
mpz_init(slope);
if(mpz_cmp_ui(P->y, 0) != 0) {
mpz_mul_ui(temp, P->y, 2);
mpz_invert(temp, temp, EC.p);
mpz_mul(slope, P->x, P->x);
mpz_mul_ui(slope, slope, 3);
mpz_mul(slope, slope, temp);
mpz_mod(slope, slope, EC.p);
mpz_mul(R->x, slope, slope);
mpz_sub(R->x, R->x, P->x);
mpz_sub(R->x, R->x, P->x);
mpz_mod(R->x, R->x, EC.p);
mpz_sub(temp, P->x, R->x);
mpz_mul(R->y, slope, temp);
mpz_sub(R->y, R->y, P->y);
mpz_mod(R->y, R->y, EC.p);
} else {
mpz_set_ui(R->x, 0);
mpz_set_ui(R->y, 0);
}
mpz_clear(temp);
mpz_clear(slope);
}
void Point_Addition(struct Point *P, struct Point *Q, struct Point *R) {
mpz_t PA_temp,PA_slope;
mpz_init(PA_temp);
mpz_init(PA_slope);
if(mpz_cmp_ui(P->x, 0) == 0 && mpz_cmp_ui(P->y, 0) == 0) {
mpz_set(R->x, Q->x);
mpz_set(R->y, Q->y);
}
else {
if(mpz_cmp_ui(Q->x, 0) == 0 && mpz_cmp_ui(Q->y, 0) == 0) {
mpz_set(R->x, P->x);
mpz_set(R->y, P->y);
}
else {
if(mpz_cmp_ui(Q->y, 0) != 0) {
mpz_sub(PA_temp, EC.p, Q->y);
mpz_mod(PA_temp, PA_temp, EC.p);
}
else {
mpz_set_ui(PA_temp, 0);
}
if(mpz_cmp(P->y, PA_temp) == 0 && mpz_cmp(P->x, Q->x) == 0) {
mpz_set_ui(R->x, 0);
mpz_set_ui(R->y, 0);
}
else {
if(mpz_cmp(P->x, Q->x) == 0 && mpz_cmp(P->y, Q->y) == 0) {
Point_Doubling(P, R);
}
else {
mpz_set_ui(PA_slope, 0);
mpz_sub(PA_temp, P->x, Q->x); //dx = B.x - A.x
mpz_mod(PA_temp, PA_temp, EC.p); ///dx = dx % p
mpz_invert(PA_temp, PA_temp, EC.p); //gmpy2.invert(dx, p) % p
mpz_sub(PA_slope, P->y, Q->y);
mpz_mul(PA_slope, PA_slope, PA_temp);
mpz_mod(PA_slope, PA_slope, EC.p);
mpz_mul(R->x, PA_slope, PA_slope); //c*c
mpz_sub(R->x, R->x, P->x); // c*c - A.x
mpz_sub(R->x, R->x, Q->x); //(c*c - A.x) - B.x
mpz_mod(R->x, R->x, EC.p); // Rx % p
mpz_sub(PA_temp, P->x, R->x);
mpz_mul(R->y, PA_slope, PA_temp);
mpz_sub(R->y, R->y, P->y);
mpz_mod(R->y, R->y, EC.p);
}
}
}
}
mpz_clear(PA_temp);
mpz_clear(PA_slope);
}
void Scalar_Multiplication(struct Point P, struct Point *R, mpz_t m) {
struct Point SM_T,SM_Q;
int no_of_bits, i;
no_of_bits = mpz_sizeinbase(m, 2);
mpz_init_set_ui(SM_Q.x,0);
mpz_init_set_ui(SM_Q.y,0);
mpz_init_set_ui(SM_T.x,0);
mpz_init_set_ui(SM_T.y,0);
mpz_set_ui(R->x, 0);
mpz_set_ui(R->y, 0);
if(mpz_cmp_ui(m, 0) != 0) {
mpz_set(SM_Q.x, P.x);
mpz_set(SM_Q.y, P.y);
for(i = 0; i < no_of_bits; i++) {
if(mpz_tstbit(m, i)) {
mpz_set(SM_T.x, R->x);
mpz_set(SM_T.y, R->y);
mpz_set(SM_Q.x,DoublingG[i].x);
mpz_set(SM_Q.y,DoublingG[i].y);
Point_Addition(&SM_T, &SM_Q, R);
}
}
}
mpz_clear(SM_T.x);
mpz_clear(SM_T.y);
mpz_clear(SM_Q.x);
mpz_clear(SM_Q.y);
}
void Point_Negation(struct Point *A, struct Point *S) {
mpz_sub(S->y, EC.p, A->y);
mpz_set(S->x, A->x);
}
/*
Precalculate G Doublings for Scalar_Multiplication
*/
void init_doublingG(struct Point *P) {
int i = 0;
mpz_init(DoublingG[i].x);
mpz_init(DoublingG[i].y);
mpz_set(DoublingG[i].x,P->x);
mpz_set(DoublingG[i].y,P->y);
i = 1;
while(i < 256){
mpz_init(DoublingG[i].x);
mpz_init(DoublingG[i].y);
Point_Doubling(&DoublingG[i-1] ,&DoublingG[i]);
mpz_mod(DoublingG[i].x, DoublingG[i].x, EC.p);
mpz_mod(DoublingG[i].y, DoublingG[i].y, EC.p);
i++;
}
}