src/jdk.crypto.ec/share/classes/sun/security/ec/point/ProjectivePoint.java
author apetcher
Tue, 11 Dec 2018 09:42:45 -0500
changeset 52946 752e57845ad2
permissions -rw-r--r--
8208698: Improved ECC Implementation Summary: New implementation of ECDH and ECDSA forsome prime-order curves Reviewed-by: ascarpino
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52946
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     1
/*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     4
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    10
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    15
 * accompanied this code).
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    16
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    20
 *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    23
 * questions.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    24
 */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    25
package sun.security.ec.point;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    26
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    27
import sun.security.util.math.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    28
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    29
/**
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    30
 * Elliptic curve point in projective coordinates (X, Y, Z) where
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    31
 * an affine point (x, y) is represented using any (X, Y, Z) s.t.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    32
 * x = X/Z and y = Y/Z.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    33
 */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    34
public abstract class ProjectivePoint
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    35
    <T extends IntegerModuloP> implements Point {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    36
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    37
    protected final T x;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    38
    protected final T y;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    39
    protected final T z;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    40
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    41
    protected ProjectivePoint(T x, T y, T z) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    42
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    43
        this.x = x;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    44
        this.y = y;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    45
        this.z = z;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    46
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    47
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    48
    @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    49
    public IntegerFieldModuloP getField() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    50
        return this.x.getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    51
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    52
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    53
    @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    54
    public Immutable fixed() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    55
        return new Immutable(x.fixed(), y.fixed(), z.fixed());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    56
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    57
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    58
    @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    59
    public Mutable mutable() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    60
        return new Mutable(x.mutable(), y.mutable(), z.mutable());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    61
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    62
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    63
    public T getX() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    64
        return x;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    65
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    66
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    67
    public T getY() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    68
        return y;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    69
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    70
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    71
    public T getZ() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    72
        return z;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    73
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    74
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    75
    public AffinePoint asAffine() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    76
        IntegerModuloP zInv = z.multiplicativeInverse();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    77
        return new AffinePoint(x.multiply(zInv), y.multiply(zInv));
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    78
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    79
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    80
    public static class Immutable
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    81
        extends ProjectivePoint<ImmutableIntegerModuloP>
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    82
        implements ImmutablePoint {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    83
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    84
        public Immutable(ImmutableIntegerModuloP x,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    85
                         ImmutableIntegerModuloP y,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    86
                         ImmutableIntegerModuloP z) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    87
            super(x, y, z);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    88
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    89
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    90
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    91
    public static class Mutable
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    92
        extends ProjectivePoint<MutableIntegerModuloP>
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    93
        implements MutablePoint {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    94
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    95
        public Mutable(MutableIntegerModuloP x,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    96
                       MutableIntegerModuloP y,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    97
                       MutableIntegerModuloP z) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    98
            super(x, y, z);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    99
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   100
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   101
        public Mutable(IntegerFieldModuloP field) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   102
            super(field.get0().mutable(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   103
                field.get0().mutable(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   104
                field.get0().mutable());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   105
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   106
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   107
        @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   108
        public Mutable conditionalSet(Point p, int set) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   109
            if (!(p instanceof ProjectivePoint)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   110
                throw new RuntimeException("Incompatible point");
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   111
            }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   112
            @SuppressWarnings("unchecked")
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   113
            ProjectivePoint<IntegerModuloP> pp =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   114
                (ProjectivePoint<IntegerModuloP>) p;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   115
            return conditionalSet(pp, set);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   116
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   117
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   118
        private <T extends IntegerModuloP>
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   119
        Mutable conditionalSet(ProjectivePoint<T> pp, int set) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   120
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   121
            x.conditionalSet(pp.x, set);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   122
            y.conditionalSet(pp.y, set);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   123
            z.conditionalSet(pp.z, set);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   124
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   125
            return this;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   126
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   127
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   128
        @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   129
        public Mutable setValue(AffinePoint p) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   130
            x.setValue(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   131
            y.setValue(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   132
            z.setValue(p.getX().getField().get1());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   133
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   134
            return this;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   135
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   136
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   137
        @Override
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   138
        public Mutable setValue(Point p) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   139
            if (!(p instanceof ProjectivePoint)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   140
                throw new RuntimeException("Incompatible point");
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   141
            }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   142
            @SuppressWarnings("unchecked")
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   143
            ProjectivePoint<IntegerModuloP> pp =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   144
                (ProjectivePoint<IntegerModuloP>) p;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   145
            return setValue(pp);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   146
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   147
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   148
        private <T extends IntegerModuloP>
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   149
        Mutable setValue(ProjectivePoint<T> pp) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   150
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   151
            x.setValue(pp.x);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   152
            y.setValue(pp.y);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   153
            z.setValue(pp.z);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   154
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   155
            return this;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   156
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   157
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   158
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   159
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   160
}