52946
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 |
public static class Immutable
|
|
81 |
extends ProjectivePoint<ImmutableIntegerModuloP>
|
|
82 |
implements ImmutablePoint {
|
|
83 |
|
|
84 |
public Immutable(ImmutableIntegerModuloP x,
|
|
85 |
ImmutableIntegerModuloP y,
|
|
86 |
ImmutableIntegerModuloP z) {
|
|
87 |
super(x, y, z);
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
public static class Mutable
|
|
92 |
extends ProjectivePoint<MutableIntegerModuloP>
|
|
93 |
implements MutablePoint {
|
|
94 |
|
|
95 |
public Mutable(MutableIntegerModuloP x,
|
|
96 |
MutableIntegerModuloP y,
|
|
97 |
MutableIntegerModuloP z) {
|
|
98 |
super(x, y, z);
|
|
99 |
}
|
|
100 |
|
|
101 |
public Mutable(IntegerFieldModuloP field) {
|
|
102 |
super(field.get0().mutable(),
|
|
103 |
field.get0().mutable(),
|
|
104 |
field.get0().mutable());
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
public Mutable conditionalSet(Point p, int set) {
|
|
109 |
if (!(p instanceof ProjectivePoint)) {
|
|
110 |
throw new RuntimeException("Incompatible point");
|
|
111 |
}
|
|
112 |
@SuppressWarnings("unchecked")
|
|
113 |
ProjectivePoint<IntegerModuloP> pp =
|
|
114 |
(ProjectivePoint<IntegerModuloP>) p;
|
|
115 |
return conditionalSet(pp, set);
|
|
116 |
}
|
|
117 |
|
|
118 |
private <T extends IntegerModuloP>
|
|
119 |
Mutable conditionalSet(ProjectivePoint<T> pp, int set) {
|
|
120 |
|
|
121 |
x.conditionalSet(pp.x, set);
|
|
122 |
y.conditionalSet(pp.y, set);
|
|
123 |
z.conditionalSet(pp.z, set);
|
|
124 |
|
|
125 |
return this;
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public Mutable setValue(AffinePoint p) {
|
|
130 |
x.setValue(p.getX());
|
|
131 |
y.setValue(p.getY());
|
|
132 |
z.setValue(p.getX().getField().get1());
|
|
133 |
|
|
134 |
return this;
|
|
135 |
}
|
|
136 |
|
|
137 |
@Override
|
|
138 |
public Mutable setValue(Point p) {
|
|
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 setValue(pp);
|
|
146 |
}
|
|
147 |
|
|
148 |
private <T extends IntegerModuloP>
|
|
149 |
Mutable setValue(ProjectivePoint<T> pp) {
|
|
150 |
|
|
151 |
x.setValue(pp.x);
|
|
152 |
y.setValue(pp.y);
|
|
153 |
z.setValue(pp.z);
|
|
154 |
|
|
155 |
return this;
|
|
156 |
}
|
|
157 |
|
|
158 |
}
|
|
159 |
|
|
160 |
}
|