-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoords.cpp
163 lines (133 loc) · 3.66 KB
/
coords.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#include "coords.h"
void ExtendedPoint::initAll(bool minusOne)
{
reset(X);
reset(Y);
reset(Z);
reset(T);
isMinus1 = minusOne;
}
ExtendedPoint::ExtendedPoint(bool minusOne)
{
initAll(minusOne);
}
ExtendedPoint::ExtendedPoint(mpz_t x,mpz_t y,mpz_t N,bool minusOne)
{
initAll(minusOne);
fromAffine(x,y,N);
}
ExtendedPoint::ExtendedPoint(mpz_t N,bool minusOne)
{
initAll(minusOne);
infinity(N);
}
void ExtendedPoint::infinity(mpz_t N)
{
mpz_t x,y;
mpz_init_set_ui(x,0);
mpz_init_set_ui(y,1);
fromAffine(x,y,N);
mpz_clrs(x,y);
}
void ExtendedPoint::fromAffine(mpz_t x,mpz_t y,mpz_t N)
{
mpz_t z,t;
mpz_init_set_ui(z,1);
mpz_init(t);
mpz_mul(t,x,y);
to_mont_repr(x,N);
to_mont_repr(y,N);
to_mont_repr(z,N);
to_mont_repr(t,N);
mpz_to_biguint(X,x);
mpz_to_biguint(Y,y);
mpz_to_biguint(Z,z);
mpz_to_biguint(T,t);
mpz_clrs(t,z);
}
void ExtendedPoint::toAffine(mpz_t x,mpz_t y,mpz_t N,mpz_t invB)
{
mpz_t z,fact;
mpz_intz(z,fact);
mpz_set_ui(fact,0);
biguint_to_mpz(x,X);
biguint_to_mpz(y,Y);
biguint_to_mpz(z,Z);
from_mont_repr(x,N,invB);
from_mont_repr(y,N,invB);
from_mont_repr(z,N,invB);
bool ret = try_invert_mod(fact,z,N);
mpz_clear(z);
if (ret)
{
mpz_mul(x,x,fact);
mpz_mul(y,y,fact);
mpz_mod(x,x,N);
mpz_mod(y,y,N);
}
else throw fact;
}
// Zvoli vhodnou strategii vypoctu podle poctu nactenych typu krivek
computeStrategy chooseStrategy(int edwardsRead,int twistedRead,int& usableCurves)
{
int curvesRead = edwardsRead+twistedRead;
if (curvesRead <= 0)
{
cout << "ERROR: No curves read." << endl;
usableCurves = 0;
return csNone;
}
if (edwardsRead > 0 && twistedRead < edwardsRead && edwardsRead%CURVES_PER_BLOCK == 0)
{
cout << "INFO: Using " << edwardsRead << " Edwards curves." << endl;
usableCurves = edwardsRead;
return csEdwards;
}
if (twistedRead > 0 && edwardsRead < twistedRead && twistedRead%CURVES_PER_BLOCK == 0)
{
cout << "INFO: Using " << twistedRead << " twisted Edwards curves." << endl;
usableCurves = twistedRead;
return csTwisted;
}
if (twistedRead*edwardsRead > 0 && twistedRead == edwardsRead && curvesRead%(2*CURVES_PER_BLOCK) == 0)
{
cout << "INFO: Using " << edwardsRead << " Edwards curves and " << twistedRead << " twisted Edwards curves." << endl;
usableCurves = curvesRead;
return csMixed;
}
cout << "ERROR: Inappropriate number of curves: " << edwardsRead << " Edwards curves, " << twistedRead << " twisted Edwards curves." << endl;
usableCurves = 0;
return csNone;
}
computeStrategy readCurves(Generator* source,mpz_t zN,ExtendedPoint** pInit,int& edwards,int& twisted,int &usableCurves)
{
computeStrategy strat = computeStrategy::csNone;
vector<ExtendedPoint> v;
cout << "Loading curves..." << endl;
ReducedPoint P;
while (source->next_base_point(P,zN))
{
// Vytvor bod v Extended souradnicich z redukovanych afinnich bodu modulo N
v.push_back(ExtendedPoint(P.X.get(),P.Y.get(),zN,source->getA() == -1));
}
twisted = source->countTwisted();
edwards = source->countEdwards();
cout << "Curve generation finished." << endl;
// Prekroucene Edwardsovy krivky prijdou na zacatek
std::sort(v.begin(), v.end(), [](const ExtendedPoint& a, const ExtendedPoint & b) -> bool { return a.isMinus1 && !b.isMinus1; });
usableCurves = 0;
strat = chooseStrategy(edwards,twisted,usableCurves);
if (strat == csNone) goto read_finish;
// Prekopiruj body z vektoru do pameti
*pInit = new ExtendedPoint[usableCurves];
std::copy((strat == csEdwards ? v.begin()+twisted : v.begin()),v.begin()+usableCurves,*pInit);
// Jsme hotovi
read_finish:
v.clear();
return strat;
}