-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoint.java
More file actions
162 lines (140 loc) · 3.68 KB
/
Point.java
File metadata and controls
162 lines (140 loc) · 3.68 KB
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.Stan.Crypt.ECC;
import java.math.BigInteger;
/**
*
* @author Stan
*/
public class Point {
private BigInteger x, y;
static BigInteger zp, a, b, o;
static final BigInteger THREE = new BigInteger("3");
static final BigInteger TWO = new BigInteger("2");
static final BigInteger ONE = new BigInteger("1");
static final BigInteger ZERO = new BigInteger("0");
static Point res = new Point();
public Point(Curve curve) {
this.x = curve.getG().getX();
this.y = curve.getG().getY();
Point.zp = curve.getZp();
Point.o = curve.getZp();
Point.a = curve.getA();
Point.b = curve.getB();
}
public Point() {
}
public Point(String x, String y) {
this.x = new BigInteger(x, 16);
this.y = new BigInteger(y, 16);
}
public Point(String point) {
String position[] = point.split("\\|");
this.x = new BigInteger(position[0], 16);
this.y = new BigInteger(position[1], 16);
}
public void equal(Point point) {
this.x = point.getX();
this.y = point.getY();
Point.zp = point.getZp();
}
public Point factors(BigInteger n, Point g) {
Point temp = g;
res = g;
int len = n.bitLength();
for (int i = 1; i < len; i++) {
temp = temp.pointAdd(temp);
if (n.testBit(i)) {
res = res.pointAdd(temp);
}
}
if (n.testBit(0)) {
res = res.pointAdd(g);
}
return res;
}
public Point pointAdd(Point q) {
BigInteger xp = this.x;
BigInteger yp = this.y;
BigInteger xq = q.getX();
BigInteger yq = q.getY();
if ((xp.equals(xq) && !yp.equals(yq))
|| (xp.equals(xq) && yp.equals(yq) && yp.equals(ZERO)))
return methodO();
if ((xp.equals(o) || xq.equals(o)))
return methodOAdd(xp, xq, yp, yq);
if (xp.equals(xq) && yp.equals(yq) && !yp.equals(o))
return methodTwo(xp, xq, yp, yq);
else
return methodOne(xp, xq, yp, yq);
}
// Adding distinct points P and Q (1)
// When P = (xP,yP) and Q = (xQ,yQ) and P Q, P -Q,
// P + Q = R(xR, yR) with xR = s2 - xP - xQ and yR = s(xP - xR) - yP
// where s = (yP - yQ) / (xP - xQ)
private Point methodOne(BigInteger xp, BigInteger xq, BigInteger yp,
BigInteger yq) {
Point res = new Point();
BigInteger temp = (xp.subtract(xq)).modInverse(zp);
BigInteger s = (yp.subtract(yq)).multiply(temp).mod(zp);
res.setX(s.multiply(s).subtract(xp).subtract(xq).mod(zp));
res.setY(s.multiply(xp.subtract(res.getX())).subtract(yp).mod(zp));
return res;
}
// Doubling the point P (2)
// When yP is not O,2P = R(xR, yR) with xR = s2 - 2xP and yR = s(xP - xR)
// -yP
// where s = (3xP2 + a) / (2yP )
private Point methodTwo(BigInteger xp, BigInteger xq, BigInteger yp,
BigInteger yq) {
Point res = new Point();
BigInteger s = (THREE.multiply(xp.multiply(xp)).add(a)).mod(zp)
.multiply(yp.add(yp).modInverse(zp)).mod(zp);
res.setX(s.multiply(s).subtract(xp).subtract(xq).mod(zp));
res.setY(s.multiply(xp.subtract(res.getX())).subtract(yp).mod(zp));
return res;
}
private Point methodO() {
Point res = new Point();
res.setX(o);
res.setY(o);
return res;
}
private Point methodOAdd(BigInteger xp, BigInteger xq, BigInteger yp,
BigInteger yq) {
Point res = new Point();
if (xp.equals(o)) {
res.setX(xq);
res.setY(yq);
}
if (xq.equals(o)) {
res.setX(xp);
res.setY(yp);
}
return res;
}
public void setX(BigInteger x) {
this.x = x;
}
public void setY(BigInteger y) {
this.y = y;
}
public void setZp(BigInteger zp) {
Point.zp = zp;
}
public BigInteger getX() {
return x;
}
public BigInteger getY() {
return y;
}
@Override
public String toString() {
return x.toString(16) + "|" + y.toString(16);
}
public BigInteger getZp() {
return zp;
}
}