Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/point/ProjectivePoint.java
41162 views
1
/*
2
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
package sun.security.ec.point;
26
27
import sun.security.util.math.*;
28
29
/**
30
* Elliptic curve point in projective coordinates (X, Y, Z) where
31
* an affine point (x, y) is represented using any (X, Y, Z) s.t.
32
* x = X/Z and y = Y/Z.
33
*/
34
public abstract class ProjectivePoint
35
<T extends IntegerModuloP> implements Point {
36
37
protected final T x;
38
protected final T y;
39
protected final T z;
40
41
protected ProjectivePoint(T x, T y, T z) {
42
43
this.x = x;
44
this.y = y;
45
this.z = z;
46
}
47
48
@Override
49
public IntegerFieldModuloP getField() {
50
return this.x.getField();
51
}
52
53
@Override
54
public Immutable fixed() {
55
return new Immutable(x.fixed(), y.fixed(), z.fixed());
56
}
57
58
@Override
59
public Mutable mutable() {
60
return new Mutable(x.mutable(), y.mutable(), z.mutable());
61
}
62
63
public T getX() {
64
return x;
65
}
66
67
public T getY() {
68
return y;
69
}
70
71
public T getZ() {
72
return z;
73
}
74
75
public AffinePoint asAffine() {
76
IntegerModuloP zInv = z.multiplicativeInverse();
77
return new AffinePoint(x.multiply(zInv), y.multiply(zInv));
78
}
79
80
private static
81
<T1 extends IntegerModuloP, T2 extends IntegerModuloP>
82
boolean affineEquals(ProjectivePoint<T1> p1,
83
ProjectivePoint<T2> p2) {
84
MutableIntegerModuloP x1 = p1.getX().mutable().setProduct(p2.getZ());
85
MutableIntegerModuloP x2 = p2.getX().mutable().setProduct(p1.getZ());
86
if (!x1.asBigInteger().equals(x2.asBigInteger())) {
87
return false;
88
}
89
90
MutableIntegerModuloP y1 = p1.getY().mutable().setProduct(p2.getZ());
91
MutableIntegerModuloP y2 = p2.getY().mutable().setProduct(p1.getZ());
92
if (!y1.asBigInteger().equals(y2.asBigInteger())) {
93
return false;
94
}
95
96
return true;
97
}
98
99
public boolean affineEquals(Point p) {
100
if (p instanceof ProjectivePoint) {
101
@SuppressWarnings("unchecked")
102
ProjectivePoint<IntegerModuloP> pp =
103
(ProjectivePoint<IntegerModuloP>) p;
104
return affineEquals(this, pp);
105
}
106
107
return asAffine().equals(p.asAffine());
108
}
109
110
public static class Immutable
111
extends ProjectivePoint<ImmutableIntegerModuloP>
112
implements ImmutablePoint {
113
114
public Immutable(ImmutableIntegerModuloP x,
115
ImmutableIntegerModuloP y,
116
ImmutableIntegerModuloP z) {
117
super(x, y, z);
118
}
119
}
120
121
public static class Mutable
122
extends ProjectivePoint<MutableIntegerModuloP>
123
implements MutablePoint {
124
125
public Mutable(MutableIntegerModuloP x,
126
MutableIntegerModuloP y,
127
MutableIntegerModuloP z) {
128
super(x, y, z);
129
}
130
131
public Mutable(IntegerFieldModuloP field) {
132
super(field.get0().mutable(),
133
field.get0().mutable(),
134
field.get0().mutable());
135
}
136
137
@Override
138
public Mutable conditionalSet(Point p, int set) {
139
if (!(p instanceof ProjectivePoint)) {
140
throw new RuntimeException("Incompatible point");
141
}
142
@SuppressWarnings("unchecked")
143
ProjectivePoint<IntegerModuloP> pp =
144
(ProjectivePoint<IntegerModuloP>) p;
145
return conditionalSet(pp, set);
146
}
147
148
private <T extends IntegerModuloP>
149
Mutable conditionalSet(ProjectivePoint<T> pp, int set) {
150
151
x.conditionalSet(pp.x, set);
152
y.conditionalSet(pp.y, set);
153
z.conditionalSet(pp.z, set);
154
155
return this;
156
}
157
158
@Override
159
public Mutable setValue(AffinePoint p) {
160
x.setValue(p.getX());
161
y.setValue(p.getY());
162
z.setValue(p.getX().getField().get1());
163
164
return this;
165
}
166
167
@Override
168
public Mutable setValue(Point p) {
169
if (!(p instanceof ProjectivePoint)) {
170
throw new RuntimeException("Incompatible point");
171
}
172
@SuppressWarnings("unchecked")
173
ProjectivePoint<IntegerModuloP> pp =
174
(ProjectivePoint<IntegerModuloP>) p;
175
return setValue(pp);
176
}
177
178
private <T extends IntegerModuloP>
179
Mutable setValue(ProjectivePoint<T> pp) {
180
181
x.setValue(pp.x);
182
y.setValue(pp.y);
183
z.setValue(pp.z);
184
185
return this;
186
}
187
188
}
189
190
}
191
192