Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/ec/point/ProjectivePoint.java
38923 views
/*1* Copyright (c) 2018, 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.*;2728/**29* Elliptic curve point in projective coordinates (X, Y, Z) where30* an affine point (x, y) is represented using any (X, Y, Z) s.t.31* x = X/Z and y = Y/Z.32*/33public abstract class ProjectivePoint34<T extends IntegerModuloP> implements Point {3536protected final T x;37protected final T y;38protected final T z;3940protected ProjectivePoint(T x, T y, T z) {4142this.x = x;43this.y = y;44this.z = z;45}4647@Override48public IntegerFieldModuloP getField() {49return this.x.getField();50}5152@Override53public Immutable fixed() {54return new Immutable(x.fixed(), y.fixed(), z.fixed());55}5657@Override58public Mutable mutable() {59return new Mutable(x.mutable(), y.mutable(), z.mutable());60}6162public T getX() {63return x;64}6566public T getY() {67return y;68}6970public T getZ() {71return z;72}7374public AffinePoint asAffine() {75IntegerModuloP zInv = z.multiplicativeInverse();76return new AffinePoint(x.multiply(zInv), y.multiply(zInv));77}7879public static class Immutable80extends ProjectivePoint<ImmutableIntegerModuloP>81implements ImmutablePoint {8283public Immutable(ImmutableIntegerModuloP x,84ImmutableIntegerModuloP y,85ImmutableIntegerModuloP z) {86super(x, y, z);87}88}8990public static class Mutable91extends ProjectivePoint<MutableIntegerModuloP>92implements MutablePoint {9394public Mutable(MutableIntegerModuloP x,95MutableIntegerModuloP y,96MutableIntegerModuloP z) {97super(x, y, z);98}99100public Mutable(IntegerFieldModuloP field) {101super(field.get0().mutable(),102field.get0().mutable(),103field.get0().mutable());104}105106@Override107public Mutable conditionalSet(Point p, int set) {108if (!(p instanceof ProjectivePoint)) {109throw new RuntimeException("Incompatible point");110}111@SuppressWarnings("unchecked")112ProjectivePoint<IntegerModuloP> pp =113(ProjectivePoint<IntegerModuloP>) p;114return conditionalSet(pp, set);115}116117private <T extends IntegerModuloP>118Mutable conditionalSet(ProjectivePoint<T> pp, int set) {119120x.conditionalSet(pp.x, set);121y.conditionalSet(pp.y, set);122z.conditionalSet(pp.z, set);123124return this;125}126127@Override128public Mutable setValue(AffinePoint p) {129x.setValue(p.getX());130y.setValue(p.getY());131z.setValue(p.getX().getField().get1());132133return this;134}135136@Override137public Mutable setValue(Point p) {138if (!(p instanceof ProjectivePoint)) {139throw new RuntimeException("Incompatible point");140}141@SuppressWarnings("unchecked")142ProjectivePoint<IntegerModuloP> pp =143(ProjectivePoint<IntegerModuloP>) p;144return setValue(pp);145}146147private <T extends IntegerModuloP>148Mutable setValue(ProjectivePoint<T> pp) {149150x.setValue(pp.x);151y.setValue(pp.y);152z.setValue(pp.z);153154return this;155}156157}158159}160161162