Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/point/ProjectivePoint.java
41162 views
/*1* Copyright (c) 2018, 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.*;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}7879private static80<T1 extends IntegerModuloP, T2 extends IntegerModuloP>81boolean affineEquals(ProjectivePoint<T1> p1,82ProjectivePoint<T2> p2) {83MutableIntegerModuloP x1 = p1.getX().mutable().setProduct(p2.getZ());84MutableIntegerModuloP x2 = p2.getX().mutable().setProduct(p1.getZ());85if (!x1.asBigInteger().equals(x2.asBigInteger())) {86return false;87}8889MutableIntegerModuloP y1 = p1.getY().mutable().setProduct(p2.getZ());90MutableIntegerModuloP y2 = p2.getY().mutable().setProduct(p1.getZ());91if (!y1.asBigInteger().equals(y2.asBigInteger())) {92return false;93}9495return true;96}9798public boolean affineEquals(Point p) {99if (p instanceof ProjectivePoint) {100@SuppressWarnings("unchecked")101ProjectivePoint<IntegerModuloP> pp =102(ProjectivePoint<IntegerModuloP>) p;103return affineEquals(this, pp);104}105106return asAffine().equals(p.asAffine());107}108109public static class Immutable110extends ProjectivePoint<ImmutableIntegerModuloP>111implements ImmutablePoint {112113public Immutable(ImmutableIntegerModuloP x,114ImmutableIntegerModuloP y,115ImmutableIntegerModuloP z) {116super(x, y, z);117}118}119120public static class Mutable121extends ProjectivePoint<MutableIntegerModuloP>122implements MutablePoint {123124public Mutable(MutableIntegerModuloP x,125MutableIntegerModuloP y,126MutableIntegerModuloP z) {127super(x, y, z);128}129130public Mutable(IntegerFieldModuloP field) {131super(field.get0().mutable(),132field.get0().mutable(),133field.get0().mutable());134}135136@Override137public Mutable conditionalSet(Point p, int set) {138if (!(p instanceof ProjectivePoint)) {139throw new RuntimeException("Incompatible point");140}141@SuppressWarnings("unchecked")142ProjectivePoint<IntegerModuloP> pp =143(ProjectivePoint<IntegerModuloP>) p;144return conditionalSet(pp, set);145}146147private <T extends IntegerModuloP>148Mutable conditionalSet(ProjectivePoint<T> pp, int set) {149150x.conditionalSet(pp.x, set);151y.conditionalSet(pp.y, set);152z.conditionalSet(pp.z, set);153154return this;155}156157@Override158public Mutable setValue(AffinePoint p) {159x.setValue(p.getX());160y.setValue(p.getY());161z.setValue(p.getX().getField().get1());162163return this;164}165166@Override167public Mutable setValue(Point p) {168if (!(p instanceof ProjectivePoint)) {169throw new RuntimeException("Incompatible point");170}171@SuppressWarnings("unchecked")172ProjectivePoint<IntegerModuloP> pp =173(ProjectivePoint<IntegerModuloP>) p;174return setValue(pp);175}176177private <T extends IntegerModuloP>178Mutable setValue(ProjectivePoint<T> pp) {179180x.setValue(pp.x);181y.setValue(pp.y);182z.setValue(pp.z);183184return this;185}186187}188189}190191192