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/ExtendedHomogeneousPoint.java
41162 views
1
/*
2
* Copyright (c) 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
import java.math.BigInteger;
30
31
/**
32
* Elliptic curve point in extended homogeneous coordinates (X, Y, T, Z) where
33
* an affine point (x, y) is represented using any (X, Y, T, Z) s.t.
34
* x = X/Z, y = Y/Z, and x*y = T/Z.
35
*/
36
public abstract class ExtendedHomogeneousPoint
37
<T extends IntegerModuloP> implements Point {
38
39
protected final T x;
40
protected final T y;
41
protected final T t;
42
protected final T z;
43
44
protected ExtendedHomogeneousPoint(T x, T y, T t, T z) {
45
46
this.x = x;
47
this.y = y;
48
this.t = t;
49
this.z = z;
50
}
51
52
@Override
53
public IntegerFieldModuloP getField() {
54
return this.x.getField();
55
}
56
@Override
57
public Immutable fixed() {
58
return new Immutable(x.fixed(), y.fixed(), t.fixed(), z.fixed());
59
}
60
61
@Override
62
public Mutable mutable() {
63
return new Mutable(x.mutable(), y.mutable(), t.mutable(), z.mutable());
64
}
65
66
public T getX() {
67
return x;
68
}
69
70
public T getY() {
71
return y;
72
}
73
74
public T getT() {
75
return t;
76
}
77
78
public T getZ() {
79
return z;
80
}
81
82
public AffinePoint asAffine() {
83
IntegerModuloP zInv = z.multiplicativeInverse();
84
return new AffinePoint(x.multiply(zInv), y.multiply(zInv));
85
}
86
87
private static
88
<T1 extends IntegerModuloP, T2 extends IntegerModuloP>
89
boolean affineEquals(ExtendedHomogeneousPoint<T1> p1,
90
ExtendedHomogeneousPoint<T2> p2) {
91
MutableIntegerModuloP x1 = p1.getX().mutable().setProduct(p2.getZ());
92
MutableIntegerModuloP x2 = p2.getX().mutable().setProduct(p1.getZ());
93
if (!x1.asBigInteger().equals(x2.asBigInteger())) {
94
return false;
95
}
96
97
MutableIntegerModuloP y1 = p1.getY().mutable().setProduct(p2.getZ());
98
MutableIntegerModuloP y2 = p2.getY().mutable().setProduct(p1.getZ());
99
if (!y1.asBigInteger().equals(y2.asBigInteger())) {
100
return false;
101
}
102
103
return true;
104
}
105
106
public boolean affineEquals(Point p) {
107
if (p instanceof ExtendedHomogeneousPoint) {
108
@SuppressWarnings("unchecked")
109
ExtendedHomogeneousPoint<IntegerModuloP> ehp =
110
(ExtendedHomogeneousPoint<IntegerModuloP>) p;
111
return affineEquals(this, ehp);
112
}
113
114
return asAffine().equals(p.asAffine());
115
}
116
117
public static class Immutable
118
extends ExtendedHomogeneousPoint<ImmutableIntegerModuloP>
119
implements ImmutablePoint {
120
121
public Immutable(ImmutableIntegerModuloP x,
122
ImmutableIntegerModuloP y,
123
ImmutableIntegerModuloP t,
124
ImmutableIntegerModuloP z) {
125
super(x, y, t, z);
126
}
127
}
128
129
public static class Mutable
130
extends ExtendedHomogeneousPoint<MutableIntegerModuloP>
131
implements MutablePoint {
132
133
public Mutable(MutableIntegerModuloP x,
134
MutableIntegerModuloP y,
135
MutableIntegerModuloP t,
136
MutableIntegerModuloP z) {
137
super(x, y, t, z);
138
}
139
140
@Override
141
public Mutable conditionalSet(Point p, int set) {
142
if (!(p instanceof ExtendedHomogeneousPoint)) {
143
throw new RuntimeException("Incompatible point");
144
}
145
@SuppressWarnings("unchecked")
146
ExtendedHomogeneousPoint<IntegerModuloP> ehp =
147
(ExtendedHomogeneousPoint<IntegerModuloP>) p;
148
return conditionalSet(ehp, set);
149
}
150
151
private <T extends IntegerModuloP>
152
Mutable conditionalSet(ExtendedHomogeneousPoint<T> ehp, int set) {
153
154
x.conditionalSet(ehp.x, set);
155
y.conditionalSet(ehp.y, set);
156
t.conditionalSet(ehp.t, set);
157
z.conditionalSet(ehp.z, set);
158
159
return this;
160
}
161
162
@Override
163
public Mutable setValue(AffinePoint p) {
164
x.setValue(p.getX());
165
y.setValue(p.getY());
166
t.setValue(p.getX()).setProduct(p.getY());
167
z.setValue(p.getX().getField().get1());
168
169
return this;
170
}
171
172
@Override
173
public Mutable setValue(Point p) {
174
175
@SuppressWarnings("unchecked")
176
ExtendedHomogeneousPoint<IntegerModuloP> ehp =
177
(ExtendedHomogeneousPoint<IntegerModuloP>) p;
178
179
return setValue(ehp);
180
}
181
182
private <T extends IntegerModuloP>
183
Mutable setValue(ExtendedHomogeneousPoint<T> ehp) {
184
185
x.setValue(ehp.x);
186
y.setValue(ehp.y);
187
t.setValue(ehp.t);
188
z.setValue(ehp.z);
189
190
return this;
191
}
192
}
193
194
}
195
196