-
Notifications
You must be signed in to change notification settings - Fork 3
/
ec_gmp_p_mul.c
254 lines (218 loc) · 5.98 KB
/
ec_gmp_p_mul.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
ec_gmp_p_mul.c
2014 masterzorag@gmail.com
entirely based on the gmplib implementation of elliptic curve scalar point
available at http://researchtrend.net/ijet32/6%20KULDEEP%20BHARDWAJ.pdf
program sets curve domain parameters, an integer and a point to perform point
smultiplication by scalar, computing the derived point
cryptographically speaking, it verifies private/public math correlation
Pass a first arg to verify known R point for first curve
41da1a8f74ff8d3f1ce20ef3e9d8865c96014fe3
73ca143c9badedf2d9d3c7573307115ccfe04f13
*/
// Point at Infinity is Denoted by (0,0)
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
struct Elliptic_Curve {
mpz_t a;
mpz_t b;
mpz_t p;
};
struct Point {
mpz_t x;
mpz_t y;
};
struct Elliptic_Curve EC;
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_add(slope, slope, EC.a);
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_mod(P.x, P.x, EC.p);
mpz_mod(P.y, P.y, EC.p);
mpz_mod(Q.x, Q.x, EC.p);
mpz_mod(Q.y, Q.y, EC.p);
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);
return;
}
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);
return;
}
mpz_t temp;
mpz_init(temp);
if(mpz_cmp_ui(Q.y, 0) != 0) {
mpz_sub(temp, EC.p, Q.y);
mpz_mod(temp, temp, EC.p);
} else
mpz_set_ui(temp, 0);
//gmp_printf("\n temp=%Zd\n", temp);
if(mpz_cmp(P.y, temp) == 0 && mpz_cmp(P.x, Q.x) == 0) {
mpz_set_ui(R->x, 0);
mpz_set_ui(R->y, 0);
mpz_clear(temp);
return;
}
if(mpz_cmp(P.x, Q.x) == 0 && mpz_cmp(P.y, Q.y) == 0) {
Point_Doubling(P, R);
mpz_clear(temp);
return;
} else {
mpz_t slope;
mpz_init_set_ui(slope, 0);
mpz_sub(temp, P.x, Q.x);
mpz_mod(temp, temp, EC.p);
mpz_invert(temp, temp, EC.p);
mpz_sub(slope, P.y, Q.y);
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, Q.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);
mpz_clear(temp);
mpz_clear(slope);
return;
}
}
void Scalar_Multiplication(struct Point P, struct Point *R, mpz_t m)
{
struct Point Q, T;
mpz_init(Q.x); mpz_init(Q.y);
mpz_init(T.x); mpz_init(T.y);
long no_of_bits, loop;
no_of_bits = mpz_sizeinbase(m, 2);
mpz_set_ui(R->x, 0);
mpz_set_ui(R->y, 0);
if(mpz_cmp_ui(m, 0) == 0)
return;
mpz_set(Q.x, P.x);
mpz_set(Q.y, P.y);
if(mpz_tstbit(m, 0) == 1){
mpz_set(R->x, P.x);
mpz_set(R->y, P.y);
}
for(loop = 1; loop < no_of_bits; loop++) {
mpz_set_ui(T.x, 0);
mpz_set_ui(T.y, 0);
Point_Doubling(Q, &T);
//gmp_printf("\n %Zd %Zd %Zd %Zd ", Q.x, Q.y, T.x, T.y);
mpz_set(Q.x, T.x);
mpz_set(Q.y, T.y);
mpz_set(T.x, R->x);
mpz_set(T.y, R->y);
if(mpz_tstbit(m, loop))
Point_Addition(T, Q, R);
}
mpz_clear(Q.x); mpz_clear(Q.y);
mpz_clear(T.x); mpz_clear(T.y);
}
int main(int argc, char *argv[])
{
mpz_init(EC.a);
mpz_init(EC.b);
mpz_init(EC.p);
struct Point P, R;
mpz_init_set_ui(R.x, 0);
mpz_init_set_ui(R.y, 0);
mpz_init(P.x);
mpz_init(P.y);
mpz_t m;
mpz_init(m);
if(argv[1]){
/*
Valid test case
----------------
Curve domain parameters:
p: c1c627e1638fdc8e24299bb041e4e23af4bb5427
a: c1c627e1638fdc8e24299bb041e4e23af4bb5424
b: 877a6d84155a1de374b72d9f9d93b36bb563b2ab
*/
mpz_set_str(EC.p, "0xc1c627e1638fdc8e24299bb041e4e23af4bb5427", 0);
mpz_set_str(EC.a, "0xc1c627e1638fdc8e24299bb041e4e23af4bb5424", 0);
mpz_set_str(EC.b, "0x877a6d84155a1de374b72d9f9d93b36bb563b2ab", 0);
/*
Base point:
Gx: 010aff82b3ac72569ae645af3b527be133442131
Gy: 46b8ec1e6d71e5ecb549614887d57a287df573cc
*/
mpz_set_str(P.x, "0x010aff82b3ac72569ae645af3b527be133442131", 0);
mpz_set_str(P.y, "0x46b8ec1e6d71e5ecb549614887d57a287df573cc", 0);
/*
known verified R point for first curve
R.x 41da1a8f74ff8d3f1ce20ef3e9d8865c96014fe3
R.y 73ca143c9badedf2d9d3c7573307115ccfe04f13
using this as
k: 00542d46e7b3daac8aeb81e533873aabd6d74bb710
*/
mpz_set_str(m, "0x00542d46e7b3daac8aeb81e533873aabd6d74bb710", 0);
} else {
/*
Curve domain parameters:
p: dfd7e09d5092e7a5d24fd2fec423f7012430ae9d
a: dfd7e09d5092e7a5d24fd2fec423f7012430ae9a
b: 01914dc5f39d6da3b1fa841fdc891674fa439bd4
N: 00dfd7e09d5092e7a5d25167ecfcfde992ebf8ecad
*/
mpz_set_str(EC.p, "0xdfd7e09d5092e7a5d24fd2fec423f7012430ae9d", 0);
mpz_set_str(EC.a, "0xdfd7e09d5092e7a5d24fd2fec423f7012430ae9a", 0);
mpz_set_str(EC.b, "0x01914dc5f39d6da3b1fa841fdc891674fa439bd4", 0);
/*
Base point:
Gx: 70ee7b94f7d52ed6b1a1d3201e2d85d3b82a9810
Gy: 0b23823cd6dc3df20979373e5662f7083f6aa56f
*/
mpz_set_str(P.x, "0x70ee7b94f7d52ed6b1a1d3201e2d85d3b82a9810", 0);
mpz_set_str(P.y, "0x0b23823cd6dc3df20979373e5662f7083f6aa56f", 0);
/*
known verified R point for second curve
R.x 5432bddd1f97418147aff016eaa6100834f2caa8
R.y c498b88965689ee44df349b066cd43cbf4f2c5d0
problem is found discrete logaritm, unknown k
so just set the same...
*/
mpz_set_str(m, "0x00542d46e7b3daac8aeb81e533873aabd6d74bb710", 0);
}
/* p = k x G == R = m x P */
Scalar_Multiplication(P, &R, m);
mpz_out_str(stdout, 16, R.x); puts("");
mpz_out_str(stdout, 16, R.y); puts("");
// Free variables
mpz_clear(EC.a); mpz_clear(EC.b); mpz_clear(EC.p);
mpz_clear(R.x); mpz_clear(R.y);
mpz_clear(P.x); mpz_clear(P.y);
mpz_clear(m);
}