forked from TGPPQ89/EllipticCurveCryptography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EllipticCurve.sol
420 lines (378 loc) · 12.8 KB
/
EllipticCurve.sol
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
pragma solidity ^0.4.16;
contract EllipticCurve{
uint256 constant n = 23;
uint256 constant a = 9;
uint256 constant b = 17;
function EllipticCurve(){
}
function _jAdd( uint256 x1, uint256 z1, uint256 x2, uint256 z2) constant returns (uint256 x3, uint256 z3){
(x3, z3) = (addmod( mulmod(z2, x1 , n) ,
mulmod(x2, z1 , n),
n),
mulmod(z1, z2 , n)
);
}
function _jSub( uint256 x1, uint256 z1, uint256 x2, uint256 z2) constant returns (uint256 x3, uint256 z3){
(x3, z3) = (addmod( mulmod(z2, x1, n),
mulmod(n - x2, z1, n),
n),
mulmod(z1, z2 , n)
);
}
function _jMul( uint256 x1, uint256 z1, uint256 x2, uint256 z2) constant returns (uint256 x3, uint256 z3){
(x3, z3) = (mulmod(x1, x2 , n), mulmod(z1, z2 , n));
}
function _jDiv( uint256 x1, uint256 z1, uint256 x2, uint256 z2) constant returns (uint256 x3, uint256 z3){
(x3, z3) = (mulmod(x1, z2 , n), mulmod(z1 , x2 , n));
}
/// Return x such that ax = 1 (mod n) so x = a**-1 (mod n)
/// Works perfectly
function modularInverse( uint256 c) constant returns (uint256 modularinverse){
uint256 t = 0;
uint256 newT = 1;
uint256 r = n;
uint256 newR = c;
uint256 q;
while (newR != 0){
q = r / newR;
(t, newT) = (newT, addmod(t, (n - mulmod(q, newT, n)), n));
(r, newR) = (newR, r - q * newR );
}
return t;
}
/// Adds P and Q in jacobian coordinates
/// Works perfectly
function ellipticCurveAdditionJacobian( uint256 x1, uint256 y1, uint256 z1, uint256 x2, uint256 y2, uint256 z2) constant returns (uint256 x3, uint256 y3, uint256 z3){
uint256 l;
uint256 lz;
uint256 da;
uint256 db;
if ((x1 == 0) && (y1 == 0)){
return (x2, y2, z2);
}
if ((x2 == 0) && (y2 == 0)){
return (x1, y1, z1);
}
if ((x1 == x2) && (y1 == y2)){
(l, lz) = _jMul(x1, z1, x1, z1);
(l, lz) = _jMul(l, lz, 3, 1);
(l, lz) = _jAdd(l, lz, a, 1);
(da, db) = _jMul(y1, z1, 2, 1);
}
else{
(l, lz) = _jSub(y2, z2, y1, z1);
(da, db) = _jSub(x2, z2, x1, z1);
}
(l, lz) = _jDiv(l, lz, da, db);
(x3, da) = _jMul(l, lz, l, lz);
(x3, da) = _jSub(x3, da, x1, z1);
(x3, da) = _jSub(x3, da, x2, z2);
(y3, db) = _jSub(x1, z1, x3, da);
(y3, db) = _jMul(y3, db, l, lz );
(y3, db) = _jSub(y3, db, y1, z1 );
if (da != db){
x3 = mulmod(x3, db, n);
y3 = mulmod(y3, da, n);
z3 = mulmod(da, db, n);
}
else{
z3 = da;
}
}
/// Adds P and Q in affine coordinates
/// Works perfectly
function ellipticCurveAdditionAffine(uint256 x1, uint256 y1, uint256 x2, uint256 y2) constant returns (uint256 x3, uint256 y3){
if((x1 == 0) && (y1 == 0)){
return (x2, y2);
}
if((x2 == 0) && (y2 == 0)){
return (x1, y1);
}
if(x1 == x2){
// if((y1 + y2) % n == 0){
// return (0, 1);
// }
if (y1 == y2){
uint256 numerator = addmod(mulmod(3, mulmod(x1, x1, n), n), a, n);
uint256 denominator = mulmod(2, y1, n);
}
}
else{
numerator = addmod(y2, n - y1, n);
denominator = addmod(x2, n - x1, n);
}
x3 = addmod(addmod(mulmod(numerator * modularInverse(denominator), numerator * modularInverse(denominator), n), n - x1, n), n - x2, n);
y3 = addmod(addmod(mulmod(numerator * modularInverse(denominator), x1, n), mulmod(numerator * modularInverse(denominator), n - x3, n), n), n - y1, n);
(x3, y3);
}
/// Doubles (Px, Py, Pz) in jacobian coordinates
/// Works perfectly
function ellipticCurveDoublingJacobian(uint256 x1, uint256 y1, uint256 z1) constant returns(uint256 x3, uint256 y3, uint256 z3){
(x3, y3, z3) = ellipticCurveAdditionJacobian(x1, y1, z1, x1, y1, z1);
}
/// Doubles (Px, Py) in affine coordinates
/// Works perfectly
function ellipticCurveDoublingAffine(uint256 x1, uint256 y1) constant returns(uint256 x3, uint256 y3){
(x3, y3) = ellipticCurveAdditionAffine(x1, y1, x1, y1);
}
/// Multiplies P in jacobian coordinates by scalar k
/// Works perfectly
function ellipticCurveMultiplicationJacobian(uint256 k, uint256 x1, uint256 y1, uint256 z1) constant returns(uint256 x3,uint256 y3,uint256 z3){
uint256 remaining = k;
uint256 px = x1;
uint256 py = y1;
uint256 pz = z1;
uint256 acx = 0;
uint256 acy = 0;
uint256 acz = 1;
if (k == 0){
return (0, 0, 1);
}
while (remaining != 0){
if ((remaining & 1) != 0){
(acx, acy, acz) = ellipticCurveAdditionJacobian(acx, acy, acz, px, py, pz);
}
remaining = remaining / 2;
(px, py, pz) = ellipticCurveDoublingJacobian(px, py, pz);
}
(x3, y3, z3) = (acx, acy, acz);
}
/// Multiplies P in affine coordinates by scalar k
/// Works perfectly
function ellipticCurveMultiplicationAffine(uint256 k, uint256 x1, uint256 y1) constant returns(uint256 x3,uint256 y3){
uint256 remaining = k;
uint256 px = x1;
uint256 py = y1;
uint256 acx = 0;
uint256 acy = 0;
if (k == 0){
return (0, 0);
}
while (remaining != 0){
if ((remaining & 1) != 0){
(acx, acy) = ellipticCurveAdditionAffine(acx, acy, px, py);
}
remaining = remaining / 2;
(px, py) = ellipticCurveDoublingAffine(px, py);
}
(x3, y3) = (acx, acy);
}
/// Finds all primes under limit
/// Works perfectly
function sieveOfEratosthenes(uint256 limit) constant returns (uint256[] memory primesList){
require(limit >= 2);
uint256[] memory primes = new uint256[](limit + 1);
for (uint256 i = 2; i < limit; i++){
primes[i] = i;
}
i = 2;
uint256 k = limit - 2;
while(i**2 <= limit){
if (primes[i] != 0){
for (uint256 j = 2; primes[i] * j <= limit; j++){
if (primes[primes[i] * j] != 0) {
delete primes[primes[i] * j];
k--;
}
}
}
i++;
}
j = 0;
primesList = new uint256[](k);
for (i = 2; i <= limit; i++){
if (primes[i] != 0){
primesList[j] = primes[i];
j++;
}
}
}
/// Bool for primality test
/// Works perfectly
function primalityTest(uint256 x) constant returns (bool){
if (x > 2 && x % 2 == 0){
return false;
}
uint256 rootCeiling = squareRoot(x) + 1;
for (uint256 i = 3; i < rootCeiling; i += 2){
if (x % i == 0){
return false;
}
}
return true;
}
/// New functions squareRoot and ceiling and elliptic curve operations
/// Works perfectly
function squareRoot(uint256 number) constant returns (uint256 root) {
uint256 z = (number + 1) / 2;
root = number;
while (z < root) {
root = z;
z = ((number / z) + z) / 2;
}
}
/// Finds the gcd of 2 numbers
/// Works perfectly
function greatestCommonDivisor(uint256 x, uint256 y) constant returns (uint256){
uint256 r = x % y;
if (r != 0){
return greatestCommonDivisor(y, r);
} else {
return y;
}
}
/// Converts a point (Px, Py, Pz) expressed in Jacobian coordinates to (Px', Py', 1).
/// Mutates P.
/// Removed Pz'
/// Returns (Px', Py')
/// Works perfectly
function convertJacobianToAffine(uint256 x1, uint256 y1, uint256 z1) constant returns (uint256 x3, uint256 y3){
x3 = mulmod(x1, modularInverse(z1)**2, n);
y3 = mulmod(y1, mulmod(modularInverse(z1), modularInverse(z1)**2, n), n);
}
// Calculates the quadratic residue such that quadraticesidue**2 = number (modulo n)
// Works but not perfectly
function quadraticResidue(uint256 number) constant returns (uint256 quadraticesidue){
uint256 s = 0;
uint256 q = n - 1;
while (q & 1 == 0){
q = q / 2;
s++;
}
if (s == 1){
quadraticesidue = number**((n + 1) / 4) % n;
if (quadraticesidue**2 % n == number){
return quadraticesidue;
}
return 0;
}
uint256 z = 1;
while ((z + 1)**((n - 1) / 2) % n != n - 1){
uint256 c = z**q % n;
uint256 r = number**((q + 1) / 2) % n;
uint256 t = number**q % n;
uint256 m = s;
}
while ( t != 1){
uint256 tt = t;
uint256 i = 0;
while (tt != 1){
tt = tt**2 % n;
i++;
if (i == m){
return 0;
}
}
uint256 h = c**(2**(m - i - 1)) % n;
r = mulmod(r, h, n);
t = mulmod(t, mulmod(h, h, n), n);
c = mulmod(h, h, n);
m = i;
}
if (mulmod(r, r, n) == number){
return r;
}
return 0;
}
/// Generates a random point on the curve
/// Works but not perfectly
function pointGenerator() constant returns (uint256 x, uint256 y){
x = uint256(keccak256(block.timestamp)) % n;
y = 0;
while (x != 0 && y == 0){
uint256 RHS = addmod(addmod(mulmod(x, mulmod(x, x, n), n), mulmod(a, x, n), n), b, n);
y = quadraticResidue(RHS);
(x, y);
}
}
/// Checks if a point modulo n lies on the curve
/// No need for z1, only checks affine coordinates
/// Works perfectly
function onCurveChecker(uint256 x1, uint256 y1) constant returns (bool){
if (0 == x1 || x1 == n || 0 == y1 || y1 == n){ // Point at infinity
return false;
}
uint256 LHS = mulmod(y1, y1, n);
uint256 RHS = addmod(addmod(mulmod(mulmod(x1, x1, n), x1, n), x1 * a, n), b, n);
return LHS == RHS;
}
/// Factorize a composite number over the elliptic curve
/// Not working yet
function lenstraFactorization(uint256 limit) constant returns (uint256 primeFactor){
require(limit >= 2);
uint256[] memory primes = new uint256[](limit + 1);
for (uint256 i = 2; i < limit; i++){
primes[i] = i;
}
i = 2;
uint256 k = limit - 2;
while(i**2 <= limit){
if (primes[i] != 0){
for (uint256 j = 2; primes[i] * j <= limit; j++){
if (primes[primes[i] * j] != 0) {
delete primes[primes[i] * j];
k--;
}
}
}
i++;
}
j = 0;
uint256[] memory primesList = new uint256[](k);
for (i = 2; i <= limit; i++){
if (primes[i] != 0){
primesList[j] = primes[i];
j++;
}
}
uint256 g = n;
uint256 x1 = 16;
uint256 y1 = 5;
uint256 z1 = 1;
while (g == n){
g = greatestCommonDivisor(4 * a**3 + 27 * b**2, n);
}
if (g > 1){
return g;
}
for (uint256 h = 0; h <= primesList.length; k++){
primeFactor = primesList[h];
while (primeFactor < limit){
(x1, y1, z1) = ellipticCurveMultiplicationJacobian(k, x1, y1, z1);
if (z1 > 1){
return greatestCommonDivisor(z1, n);
}
primeFactor = h * primeFactor;
}
}
}
/// Find the discrete logarithm over F23 under addition
/// Works perfectly
function ellipticCurveDiscreteLogarithmJacobian(uint256 x1, uint256 y1, uint256 z1) constant returns (uint256 x3,uint256 y3,uint256 z3, uint256 discreteLogarithm){
uint256 Qx = 4;
uint256 Qy = 19;
uint256 Qz = 18;
for (uint256 k = 1; k <= 10; k++){
(x3, y3, z3) = ellipticCurveMultiplicationJacobian(k, x1, y1, z1);
if (x3 == Qx && y3 == Qy && z3 == Qz){
discreteLogarithm = k;
break;
}
}
(x3,y3,z3);
}
/// Find the discrete logarithm over F23 under addition
/// Works perfectly
function ellipticCurveDiscreteLogarithmAffine(uint256 x1, uint256 y1) constant returns (uint256 x3,uint256 y3, uint256 discreteLogarithm){
uint256 Qx = 20;
uint256 Qy = 20;
for (uint256 k = 1; k <= 10; k++){
(x3, y3) = ellipticCurveMultiplicationAffine(k, x1, y1);
if (x3 == Qx && y3 == Qy){
discreteLogarithm = k;
break;
}
}
(x3,y3);
}
}