-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathECPoint.java
297 lines (260 loc) · 7.18 KB
/
ECPoint.java
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
//package ecc.elliptic;
import java.math.BigInteger;
import java.util.Arrays;
import java.io.*;
public class ECPoint
{
public final static BigInteger TWO = new BigInteger("2");
public final static BigInteger THREE = new BigInteger("3");
private EllipticCurve mother;
private BigInteger x, y;
private boolean iszero;
private ECPoint[] fastcache = null; //fastcache is an array of ECPoints
private ECPoint[] cache = null; //Even cache is an ECPoint array
public void fastCache()
{
try
{
if(fastcache == null)
{
fastcache = new ECPoint[256]; //fastcache initialised to 256 EC Points.
fastcache[0] = new ECPoint(mother); //First point is null.
for(int i = 1; i < fastcache.length; i++) //From 1 to 256
{
fastcache[i] = fastcache[i-1].add(this); //add the point repeatedly (Cumulative sum). P,2P,...
}
}
}
catch (NoCommonMotherException e)
{
System.out.println("ECPoint.fastcache: THIS CANNOT HAPPEN!!!");
}
}
/** Constructs a point on an elliptic curve.
*@param mother The elliptic curve on wich the point is surposed to lie
*@param x the x coordinate of the point
*@param y the y coordinate of the point
*@exception Throws a NotOnMotherException if (x,y) is not on the mother curve.*/
public ECPoint(EllipticCurve mother, BigInteger x, BigInteger y) throws NotOnMotherException
{
this.mother = mother;
this.x = x;
this.y = y;
if (!mother.onCurve(this)) throw new NotOnMotherException(this);
iszero = false;
}
/** Decompresses a compressed point stored in a byte-array into a new ECPoint.
*@param bytes the array of bytes to be decompressed
*@param mother the EllipticCurve the decompressed point is supposed to lie on.*/
public ECPoint(byte[] bytes, EllipticCurve mother)
{
this.mother = mother;
if (bytes[0] == 2)
{
iszero = true;
return;
}
boolean ymt = false;
if(bytes[0] != 0)
ymt = true;
bytes[0] = 0;
x = new BigInteger(bytes);
if(mother.getPPODBF() == null) System.out.println("Fuck dig!!!");
y = x.multiply(x).add(mother.geta()).multiply(x).add(mother.getb()).modPow(mother.getPPODBF(),mother.getp());
if(ymt != y.testBit(0))
{
y = mother.getp().subtract(y);
}
iszero = false;
}
/** IMPORTANT this renders the values of x and y to be null! Use this constructor
*only to create instances of a Zero class!*/
public ECPoint(EllipticCurve e){
x = y = BigInteger.ZERO;
mother = e;
iszero = true;
}
public byte[] compress()
{
byte[] cmp = new byte[mother.getPCS()];
if (iszero)
{
cmp[0] = 2;
}
byte[] xb = x.toByteArray();
System.arraycopy(xb, 0, cmp, mother.getPCS()-xb.length, xb.length);
if(y.testBit(0)) cmp[0] = 1;
return cmp;
}
/** Adds another elliptic curve point to this point.
*@param q The point to be added
*@return the sum of this point on the argument
*@exception Throws a NoCommonMotherException if the two points don't lie on the same elliptic curve.*/
public ECPoint add(ECPoint q) throws NoCommonMotherException
{
if (!hasCommonMother(q)) throw new NoCommonMotherException();
if (this.iszero) return q;
else if (q.isZero()) return this;
BigInteger y1 = y;
BigInteger y2 = q.gety();
BigInteger x1 = x;
BigInteger x2 = q.getx();
BigInteger alpha;
if (x2.compareTo(x1) == 0)
{
if (!(y2.compareTo(y1) == 0)) return new ECPoint(mother);
else
{
alpha = ((x1.modPow(TWO,mother.getp())).multiply(THREE)).add(mother.geta());
alpha = (alpha.multiply((TWO.multiply(y1)).modInverse(mother.getp()))).mod(mother.getp());
}
}
else
{
alpha = ((y2.subtract(y1)).multiply((x2.subtract(x1)).modInverse(mother.getp()))).mod(mother.getp());
}
BigInteger x3,y3;
x3 = (((alpha.modPow(TWO,mother.getp())).subtract(x2)).subtract(x1)).mod(mother.getp());
y3 = ((alpha.multiply(x1.subtract(x3))).subtract(y1)).mod(mother.getp());
try
{
return new ECPoint(mother,x3,y3);
}
catch (NotOnMotherException e)
{
System.out.println("Error in add!!! Result not on mother!");
return null;
}
}
public ECPoint multiply(BigInteger coef)
{
/*try{
ECPoint result = new ECPoint(mother); //result is initialised to null
byte[] coefb = coef.toByteArray(); //coefb is the number to be multiplied. i.e. q in p*q. called as p.mul(q)
if(fastcache != null) //fastcache of this, i.e. p
{
for(int i = 0; i < coefb.length; i++)
{
result = result.times256().add(fastcache[coefb[i]&255]); // result=result*256+fastcache(coefb[i]&255)
}
return result;
}
if(cache == null) //If cache is empty, populate it upto 16
{
cache = new ECPoint[16];
cache[0] = new ECPoint(mother);
for(int i = 1; i < cache.length; i++)
{
cache[i] = cache[i-1].add(this);
}
}
for(int i = 0; i < coefb.length; i++)
{
result = result.times16().add(cache[(coefb[i]>>4)&15]).times16().add(cache[coefb[i]&15]);
}
return result;
}
catch (NoCommonMotherException e)
{
System.out.println("Error in pow!!!");
return null;
}*/
//
//BigInteger R0=this.getx();
//BigInteger R1=R0.add(R0);
//Convert multiplier into binary.
String BitNum=coef.toString(2);
// System.out.println(coef+"in bin form is"+BitNum);
// System.out.println("length of bitnum string is"+BitNum.length());
int nk=coef.bitCount(); //nk in paper.
// System.out.println(coef+" .bitcount is "+ nk);
ECPoint result=this;
for(int i=nk-1;i>0;i--)
{
try
{
result=result.add(result);
if(coef.testBit(i))
result=result.add(this);
}
catch(Exception e)
{
System.out.println("Error in multiplying");
}
}
/*if(!coef.testBit(i))
{
R1=R0.add(R1);
R0=R0.add(R0);
}
else
{
R0=R0.add(R1);
R1=R1.add(R1);
}*/
//ECPoint result=new ECPoint(this.mother);
//result.x=R0;
//BigInteger y0=R0.multiply(R0).add(this.mother.geta()).multiply(R0).add(this.mother.getb()).modPow(this.mother.getPPODBF(),this.mother.getp());
//result.y=y0;
return result;
}
private ECPoint times16()
{
try
{
ECPoint result = this;
for(int i = 0; i < 4; i++)
{
result = result.add(result);
}
return result;
}
catch (Exception e)
{
System.out.println("ECPoint.times16: THIS CANNOT HAPPEN!!!");
return null;
}
}
private ECPoint times256()
{
try
{
ECPoint result = this;
for(int i = 0; i < 8; i++)
{
result = result.add(result);
}
return result;
}
catch (Exception e)
{
System.out.println("ECPoint.times256: THIS CANNOT HAPPEN!!!");
return null;
}
}
public BigInteger getx()
{
return x;
}
public BigInteger gety()
{
return y;
}
public EllipticCurve getMother()
{
return mother;
}
public String toString()
{
return "(" + x.toString() + ", " + y.toString() + ")";
}
public boolean hasCommonMother(ECPoint p)
{
if (this.mother.equals(p.getMother())) return true;
else return false;
}
public boolean isZero()
{
return iszero;
}
}