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/ed/EdECOperations.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
26
package sun.security.ec.ed;
27
28
import sun.security.ec.point.*;
29
import sun.security.util.math.IntegerModuloP;
30
import sun.security.util.math.MutableIntegerModuloP;
31
32
import java.util.function.Function;
33
34
/*
35
* Base class for Edwards curve ECC implementations.
36
*/
37
public abstract class EdECOperations {
38
39
// Curve-specific base point multiplication.
40
public abstract Point basePointMultiply(byte[] s);
41
42
// Decode curve-specifics to the affinePoint
43
public abstract <T extends Throwable>
44
AffinePoint decodeAffinePoint(Function<String, T> exception,
45
int xLSB, IntegerModuloP y) throws T;
46
47
// Curve specific point from an X,Y point
48
public abstract ImmutablePoint of(AffinePoint p);
49
50
/*
51
* Generic method for taking two classes implementing MutablePoint to be
52
* called by the curve-specific setSum()
53
*/
54
public MutablePoint setSum(MutablePoint p1, MutablePoint p2) {
55
MutableIntegerModuloP t1 = p2.getField().get1().mutable();
56
MutableIntegerModuloP t2 = p2.getField().get1().mutable();
57
MutableIntegerModuloP t3 = p2.getField().get1().mutable();
58
return setSum(p1, p2, t1, t2, t3);
59
}
60
61
/*
62
* Generic method for taking a class implementing MutablePoint with a
63
* scalar to returning the point product using curve-specific methods.
64
*/
65
public MutablePoint setProduct(MutablePoint p1, byte[] s) {
66
MutablePoint p = p1.mutable();
67
p1.setValue(getNeutral());
68
MutablePoint addResult = getNeutral().mutable();
69
MutableIntegerModuloP t1 = p.getField().get0().mutable();
70
MutableIntegerModuloP t2 = p.getField().get0().mutable();
71
MutableIntegerModuloP t3 = p.getField().get0().mutable();
72
73
for (int i = 0; i < s.length * 8; i++) {
74
addResult.setValue(p1);
75
setSum(addResult, p, t1, t2, t3);
76
int swap = bitAt(s, i);
77
p1.conditionalSet(addResult, swap);
78
setDouble(p, t1, t2);
79
}
80
81
return p1;
82
}
83
84
// Abstract method for constructing the neutral point on the curve
85
protected abstract ImmutablePoint getNeutral();
86
87
88
// Abstract method for Curve-specific point addition
89
protected abstract MutablePoint setSum(MutablePoint p1, MutablePoint p2,
90
MutableIntegerModuloP t1,
91
MutableIntegerModuloP t2,
92
MutableIntegerModuloP t3);
93
// Abstract method for Curve-specific point doubling
94
protected abstract MutablePoint setDouble(MutablePoint p,
95
MutableIntegerModuloP t1,
96
MutableIntegerModuloP t2);
97
98
private static int bitAt(byte[] arr, int index) {
99
int byteIndex = index / 8;
100
int bitIndex = index % 8;
101
return (arr[byteIndex] & (1 << bitIndex)) >> bitIndex;
102
}
103
}
104
105