Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/point/ExtendedHomogeneousPoint.java
41162 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.security.ec.point;2526import sun.security.util.math.*;2728import java.math.BigInteger;2930/**31* Elliptic curve point in extended homogeneous coordinates (X, Y, T, Z) where32* an affine point (x, y) is represented using any (X, Y, T, Z) s.t.33* x = X/Z, y = Y/Z, and x*y = T/Z.34*/35public abstract class ExtendedHomogeneousPoint36<T extends IntegerModuloP> implements Point {3738protected final T x;39protected final T y;40protected final T t;41protected final T z;4243protected ExtendedHomogeneousPoint(T x, T y, T t, T z) {4445this.x = x;46this.y = y;47this.t = t;48this.z = z;49}5051@Override52public IntegerFieldModuloP getField() {53return this.x.getField();54}55@Override56public Immutable fixed() {57return new Immutable(x.fixed(), y.fixed(), t.fixed(), z.fixed());58}5960@Override61public Mutable mutable() {62return new Mutable(x.mutable(), y.mutable(), t.mutable(), z.mutable());63}6465public T getX() {66return x;67}6869public T getY() {70return y;71}7273public T getT() {74return t;75}7677public T getZ() {78return z;79}8081public AffinePoint asAffine() {82IntegerModuloP zInv = z.multiplicativeInverse();83return new AffinePoint(x.multiply(zInv), y.multiply(zInv));84}8586private static87<T1 extends IntegerModuloP, T2 extends IntegerModuloP>88boolean affineEquals(ExtendedHomogeneousPoint<T1> p1,89ExtendedHomogeneousPoint<T2> p2) {90MutableIntegerModuloP x1 = p1.getX().mutable().setProduct(p2.getZ());91MutableIntegerModuloP x2 = p2.getX().mutable().setProduct(p1.getZ());92if (!x1.asBigInteger().equals(x2.asBigInteger())) {93return false;94}9596MutableIntegerModuloP y1 = p1.getY().mutable().setProduct(p2.getZ());97MutableIntegerModuloP y2 = p2.getY().mutable().setProduct(p1.getZ());98if (!y1.asBigInteger().equals(y2.asBigInteger())) {99return false;100}101102return true;103}104105public boolean affineEquals(Point p) {106if (p instanceof ExtendedHomogeneousPoint) {107@SuppressWarnings("unchecked")108ExtendedHomogeneousPoint<IntegerModuloP> ehp =109(ExtendedHomogeneousPoint<IntegerModuloP>) p;110return affineEquals(this, ehp);111}112113return asAffine().equals(p.asAffine());114}115116public static class Immutable117extends ExtendedHomogeneousPoint<ImmutableIntegerModuloP>118implements ImmutablePoint {119120public Immutable(ImmutableIntegerModuloP x,121ImmutableIntegerModuloP y,122ImmutableIntegerModuloP t,123ImmutableIntegerModuloP z) {124super(x, y, t, z);125}126}127128public static class Mutable129extends ExtendedHomogeneousPoint<MutableIntegerModuloP>130implements MutablePoint {131132public Mutable(MutableIntegerModuloP x,133MutableIntegerModuloP y,134MutableIntegerModuloP t,135MutableIntegerModuloP z) {136super(x, y, t, z);137}138139@Override140public Mutable conditionalSet(Point p, int set) {141if (!(p instanceof ExtendedHomogeneousPoint)) {142throw new RuntimeException("Incompatible point");143}144@SuppressWarnings("unchecked")145ExtendedHomogeneousPoint<IntegerModuloP> ehp =146(ExtendedHomogeneousPoint<IntegerModuloP>) p;147return conditionalSet(ehp, set);148}149150private <T extends IntegerModuloP>151Mutable conditionalSet(ExtendedHomogeneousPoint<T> ehp, int set) {152153x.conditionalSet(ehp.x, set);154y.conditionalSet(ehp.y, set);155t.conditionalSet(ehp.t, set);156z.conditionalSet(ehp.z, set);157158return this;159}160161@Override162public Mutable setValue(AffinePoint p) {163x.setValue(p.getX());164y.setValue(p.getY());165t.setValue(p.getX()).setProduct(p.getY());166z.setValue(p.getX().getField().get1());167168return this;169}170171@Override172public Mutable setValue(Point p) {173174@SuppressWarnings("unchecked")175ExtendedHomogeneousPoint<IntegerModuloP> ehp =176(ExtendedHomogeneousPoint<IntegerModuloP>) p;177178return setValue(ehp);179}180181private <T extends IntegerModuloP>182Mutable setValue(ExtendedHomogeneousPoint<T> ehp) {183184x.setValue(ehp.x);185y.setValue(ehp.y);186t.setValue(ehp.t);187z.setValue(ehp.z);188189return this;190}191}192193}194195196