src/jdk.crypto.ec/share/classes/sun/security/ec/ECOperations.java
author jcbeyler
Thu, 14 Mar 2019 09:35:45 -0700
changeset 54128 c0fccca69aff
parent 52946 752e57845ad2
permissions -rw-r--r--
8220628: Move the HeapMonitor library to C++ Summary: Migrate libHeapMonitorTest.c to libHeapMonitorTest.cpp Reviewed-by: cjplummer, sspitsyn
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
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    26
package sun.security.ec;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    27
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    28
import sun.security.ec.point.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    29
import sun.security.util.math.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    30
import sun.security.util.math.intpoly.*;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    31
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    32
import java.math.BigInteger;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    33
import java.security.ProviderException;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    34
import java.security.spec.ECFieldFp;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    35
import java.security.spec.ECParameterSpec;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    36
import java.security.spec.EllipticCurve;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    37
import java.util.Map;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    38
import java.util.Optional;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    39
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    40
/*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    41
 * Elliptic curve point arithmetic for prime-order curves where a=-3.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    42
 * Formulas are derived from "Complete addition formulas for prime order
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    43
 * elliptic curves" by Renes, Costello, and Batina.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    44
 */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    45
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    46
public class ECOperations {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    47
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    48
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    49
     * An exception indicating a problem with an intermediate value produced
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    50
     * by some part of the computation. For example, the signing operation
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    51
     * will throw this exception to indicate that the r or s value is 0, and
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    52
     * that the signing operation should be tried again with a different nonce.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    53
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    54
    static class IntermediateValueException extends Exception {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    55
        private static final long serialVersionUID = 1;
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
    static final Map<BigInteger, IntegerFieldModuloP> fields = Map.of(
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    59
        IntegerPolynomialP256.MODULUS, new IntegerPolynomialP256(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    60
        IntegerPolynomialP384.MODULUS, new IntegerPolynomialP384(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    61
        IntegerPolynomialP521.MODULUS, new IntegerPolynomialP521()
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    62
    );
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    63
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    64
    static final Map<BigInteger, IntegerFieldModuloP> orderFields = Map.of(
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    65
        P256OrderField.MODULUS, new P256OrderField(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    66
        P384OrderField.MODULUS, new P384OrderField(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    67
        P521OrderField.MODULUS, new P521OrderField()
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    68
    );
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    69
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    70
    public static Optional<ECOperations> forParameters(ECParameterSpec params) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    71
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    72
        EllipticCurve curve = params.getCurve();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    73
        if (!(curve.getField() instanceof ECFieldFp)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    74
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    75
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    76
        ECFieldFp primeField = (ECFieldFp) curve.getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    77
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    78
        BigInteger three = BigInteger.valueOf(3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    79
        if (!primeField.getP().subtract(curve.getA()).equals(three)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    80
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    81
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    82
        IntegerFieldModuloP field = fields.get(primeField.getP());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    83
        if (field == null) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    84
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    85
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    86
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    87
        IntegerFieldModuloP orderField = orderFields.get(params.getOrder());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    88
        if (orderField == null) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    89
            return Optional.empty();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    90
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    91
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    92
        ImmutableIntegerModuloP b = field.getElement(curve.getB());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    93
        ECOperations ecOps = new ECOperations(b, orderField);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    94
        return Optional.of(ecOps);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    95
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    96
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    97
    final ImmutableIntegerModuloP b;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    98
    final SmallValue one;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
    99
    final SmallValue two;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   100
    final SmallValue three;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   101
    final SmallValue four;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   102
    final ProjectivePoint.Immutable neutral;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   103
    private final IntegerFieldModuloP orderField;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   104
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   105
    public ECOperations(IntegerModuloP b, IntegerFieldModuloP orderField) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   106
        this.b = b.fixed();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   107
        this.orderField = orderField;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   108
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   109
        this.one = b.getField().getSmallValue(1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   110
        this.two = b.getField().getSmallValue(2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   111
        this.three = b.getField().getSmallValue(3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   112
        this.four = b.getField().getSmallValue(4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   113
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   114
        IntegerFieldModuloP field = b.getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   115
        this.neutral = new ProjectivePoint.Immutable(field.get0(),
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   116
            field.get1(), field.get0());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   117
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   118
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   119
    public IntegerFieldModuloP getField() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   120
        return b.getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   121
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   122
    public IntegerFieldModuloP getOrderField() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   123
        return orderField;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   124
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   125
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   126
    protected ProjectivePoint.Immutable getNeutral() {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   127
        return neutral;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   128
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   129
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   130
    public boolean isNeutral(Point p) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   131
        ProjectivePoint<?> pp = (ProjectivePoint<?>) p;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   132
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   133
        IntegerModuloP z = pp.getZ();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   134
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   135
        IntegerFieldModuloP field = z.getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   136
        int byteLength = (field.getSize().bitLength() + 7) / 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   137
        byte[] zBytes = z.asByteArray(byteLength);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   138
        return allZero(zBytes);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   139
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   140
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   141
    byte[] seedToScalar(byte[] seedBytes)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   142
        throws IntermediateValueException {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   143
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   144
        // Produce a nonce from the seed using FIPS 186-4,section B.5.1:
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   145
        // Per-Message Secret Number Generation Using Extra Random Bits
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   146
        // or
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   147
        // Produce a scalar from the seed using FIPS 186-4, section B.4.1:
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   148
        // Key Pair Generation Using Extra Random Bits
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   149
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   150
        // To keep the implementation simple, sample in the range [0,n)
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   151
        // and throw IntermediateValueException in the (unlikely) event
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   152
        // that the result is 0.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   153
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   154
        // Get 64 extra bits and reduce in to the nonce
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   155
        int seedBits = orderField.getSize().bitLength() + 64;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   156
        if (seedBytes.length * 8 < seedBits) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   157
            throw new ProviderException("Incorrect seed length: " +
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   158
            seedBytes.length * 8 + " < " + seedBits);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   159
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   160
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   161
        // input conversion only works on byte boundaries
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   162
        // clear high-order bits of last byte so they don't influence nonce
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   163
        int lastByteBits = seedBits % 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   164
        if (lastByteBits != 0) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   165
            int lastByteIndex = seedBits / 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   166
            byte mask = (byte) (0xFF >>> (8 - lastByteBits));
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   167
            seedBytes[lastByteIndex] &= mask;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   168
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   169
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   170
        int seedLength = (seedBits + 7) / 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   171
        IntegerModuloP scalarElem =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   172
            orderField.getElement(seedBytes, 0, seedLength, (byte) 0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   173
        int scalarLength = (orderField.getSize().bitLength() + 7) / 8;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   174
        byte[] scalarArr = new byte[scalarLength];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   175
        scalarElem.asByteArray(scalarArr);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   176
        if (ECOperations.allZero(scalarArr)) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   177
            throw new IntermediateValueException();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   178
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   179
        return scalarArr;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   180
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   181
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   182
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   183
     * Compare all values in the array to 0 without branching on any value
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   184
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   185
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   186
    public static boolean allZero(byte[] arr) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   187
        byte acc = 0;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   188
        for (int i = 0; i < arr.length; i++) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   189
            acc |= arr[i];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   190
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   191
        return acc == 0;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   192
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   193
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   194
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   195
     * 4-bit branchless array lookup for projective points.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   196
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   197
    private void lookup4(ProjectivePoint.Immutable[] arr, int index,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   198
        ProjectivePoint.Mutable result, IntegerModuloP zero) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   199
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   200
        for (int i = 0; i < 16; i++) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   201
            int xor = index ^ i;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   202
            int bit3 = (xor & 0x8) >>> 3;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   203
            int bit2 = (xor & 0x4) >>> 2;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   204
            int bit1 = (xor & 0x2) >>> 1;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   205
            int bit0 = (xor & 0x1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   206
            int inverse = bit0 | bit1 | bit2 | bit3;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   207
            int set = 1 - inverse;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   208
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   209
            ProjectivePoint.Immutable pi = arr[i];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   210
            result.conditionalSet(pi, set);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   211
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   212
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   213
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   214
    private void double4(ProjectivePoint.Mutable p, MutableIntegerModuloP t0,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   215
        MutableIntegerModuloP t1, MutableIntegerModuloP t2,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   216
        MutableIntegerModuloP t3, MutableIntegerModuloP t4) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   217
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   218
        for (int i = 0; i < 4; i++) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   219
            setDouble(p, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   220
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   221
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   222
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   223
    /**
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   224
     * Multiply an affine point by a scalar and return the result as a mutable
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   225
     * point.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   226
     *
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   227
     * @param affineP the point
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   228
     * @param s the scalar as a little-endian array
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   229
     * @return the product
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   230
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   231
    public MutablePoint multiply(AffinePoint affineP, byte[] s) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   232
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   233
        // 4-bit windowed multiply with branchless lookup.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   234
        // The mixed addition is faster, so it is used to construct the array
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   235
        // at the beginning of the operation.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   236
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   237
        IntegerFieldModuloP field = affineP.getX().getField();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   238
        ImmutableIntegerModuloP zero = field.get0();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   239
        // temporaries
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   240
        MutableIntegerModuloP t0 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   241
        MutableIntegerModuloP t1 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   242
        MutableIntegerModuloP t2 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   243
        MutableIntegerModuloP t3 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   244
        MutableIntegerModuloP t4 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   245
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   246
        ProjectivePoint.Mutable result = new ProjectivePoint.Mutable(field);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   247
        result.getY().setValue(field.get1().mutable());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   248
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   249
        ProjectivePoint.Immutable[] pointMultiples =
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   250
            new ProjectivePoint.Immutable[16];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   251
        // 0P is neutral---same as initial result value
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   252
        pointMultiples[0] = result.fixed();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   253
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   254
        ProjectivePoint.Mutable ps = new ProjectivePoint.Mutable(field);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   255
        ps.setValue(affineP);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   256
        // 1P = P
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   257
        pointMultiples[1] = ps.fixed();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   258
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   259
        // the rest are calculated using mixed point addition
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   260
        for (int i = 2; i < 16; i++) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   261
            setSum(ps, affineP, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   262
            pointMultiples[i] = ps.fixed();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   263
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   264
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   265
        ProjectivePoint.Mutable lookupResult = ps.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   266
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   267
        for (int i = s.length - 1; i >= 0; i--) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   268
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   269
            double4(result, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   270
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   271
            int high = (0xFF & s[i]) >>> 4;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   272
            lookup4(pointMultiples, high, lookupResult, zero);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   273
            setSum(result, lookupResult, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   274
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   275
            double4(result, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   276
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   277
            int low = 0xF & s[i];
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   278
            lookup4(pointMultiples, low, lookupResult, zero);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   279
            setSum(result, lookupResult, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   280
        }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   281
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   282
        return result;
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   283
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   284
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   285
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   286
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   287
     * Point double
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   288
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   289
    private void setDouble(ProjectivePoint.Mutable p, MutableIntegerModuloP t0,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   290
        MutableIntegerModuloP t1, MutableIntegerModuloP t2,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   291
        MutableIntegerModuloP t3, MutableIntegerModuloP t4) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   292
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   293
        t0.setValue(p.getX()).setSquare();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   294
        t1.setValue(p.getY()).setSquare();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   295
        t2.setValue(p.getZ()).setSquare();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   296
        t3.setValue(p.getX()).setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   297
        t4.setValue(p.getY()).setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   298
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   299
        t3.setSum(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   300
        p.getZ().setProduct(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   301
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   302
        p.getZ().setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   303
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   304
        p.getY().setValue(t2).setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   305
        p.getY().setDifference(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   306
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   307
        p.getX().setValue(p.getY()).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   308
        p.getY().setSum(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   309
        p.getY().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   310
        p.getX().setValue(t1).setDifference(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   311
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   312
        p.getY().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   313
        p.getY().setProduct(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   314
        p.getX().setProduct(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   315
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   316
        t3.setValue(t2).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   317
        t2.setSum(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   318
        p.getZ().setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   319
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   320
        t2.setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   321
        p.getZ().setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   322
        p.getZ().setDifference(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   323
        t3.setValue(p.getZ()).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   324
        p.getZ().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   325
        p.getZ().setSum(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   326
        t0.setProduct(three);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   327
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   328
        t0.setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   329
        t0.setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   330
        p.getY().setSum(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   331
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   332
        t4.setSum(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   333
        p.getZ().setProduct(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   334
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   335
        p.getX().setDifference(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   336
        p.getZ().setValue(t4).setProduct(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   337
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   338
        p.getZ().setProduct(four);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   339
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   340
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   341
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   342
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   343
     * Mixed point addition. This method constructs new temporaries each time
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   344
     * it is called. For better efficiency, the method that reuses temporaries
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   345
     * should be used if more than one sum will be computed.
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   346
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   347
    public void setSum(MutablePoint p, AffinePoint p2) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   348
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   349
        IntegerModuloP zero = p.getField().get0();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   350
        MutableIntegerModuloP t0 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   351
        MutableIntegerModuloP t1 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   352
        MutableIntegerModuloP t2 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   353
        MutableIntegerModuloP t3 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   354
        MutableIntegerModuloP t4 = zero.mutable();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   355
        setSum((ProjectivePoint.Mutable) p, p2, t0, t1, t2, t3, t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   356
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   357
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   358
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   359
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   360
     * Mixed point addition
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   361
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   362
    private void setSum(ProjectivePoint.Mutable p, AffinePoint p2,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   363
        MutableIntegerModuloP t0, MutableIntegerModuloP t1,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   364
        MutableIntegerModuloP t2, MutableIntegerModuloP t3,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   365
        MutableIntegerModuloP t4) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   366
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   367
        t0.setValue(p.getX()).setProduct(p2.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   368
        t1.setValue(p.getY()).setProduct(p2.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   369
        t3.setValue(p2.getX()).setSum(p2.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   370
        t4.setValue(p.getX()).setSum(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   371
        p.getX().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   372
        t3.setProduct(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   373
        t4.setValue(t0).setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   374
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   375
        t3.setDifference(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   376
        t4.setValue(p2.getY()).setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   377
        t4.setSum(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   378
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   379
        p.getY().setValue(p2.getX()).setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   380
        p.getY().setSum(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   381
        t2.setValue(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   382
        p.getZ().setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   383
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   384
        p.getX().setValue(p.getY()).setDifference(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   385
        p.getX().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   386
        p.getZ().setValue(p.getX()).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   387
        p.getX().setSum(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   388
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   389
        p.getZ().setValue(t1).setDifference(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   390
        p.getX().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   391
        p.getY().setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   392
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   393
        t1.setValue(t2).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   394
        t2.setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   395
        t2.setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   396
        p.getY().setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   397
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   398
        p.getY().setDifference(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   399
        p.getY().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   400
        t1.setValue(p.getY()).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   401
        p.getY().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   402
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   403
        t1.setValue(t0).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   404
        t0.setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   405
        t0.setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   406
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   407
        t1.setValue(t4).setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   408
        t2.setValue(t0).setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   409
        p.getY().setValue(p.getX()).setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   410
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   411
        p.getY().setSum(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   412
        p.getX().setProduct(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   413
        p.getX().setDifference(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   414
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   415
        p.getZ().setProduct(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   416
        t1.setValue(t3).setProduct(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   417
        p.getZ().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   418
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   419
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   420
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   421
    /*
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   422
     * Projective point addition
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   423
     */
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   424
    private void setSum(ProjectivePoint.Mutable p, ProjectivePoint.Mutable p2,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   425
        MutableIntegerModuloP t0, MutableIntegerModuloP t1,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   426
        MutableIntegerModuloP t2, MutableIntegerModuloP t3,
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   427
        MutableIntegerModuloP t4) {
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   428
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   429
        t0.setValue(p.getX()).setProduct(p2.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   430
        t1.setValue(p.getY()).setProduct(p2.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   431
        t2.setValue(p.getZ()).setProduct(p2.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   432
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   433
        t3.setValue(p.getX()).setSum(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   434
        t4.setValue(p2.getX()).setSum(p2.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   435
        t3.setProduct(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   436
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   437
        t4.setValue(t0).setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   438
        t3.setDifference(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   439
        t4.setValue(p.getY()).setSum(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   440
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   441
        p.getY().setValue(p2.getY()).setSum(p2.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   442
        t4.setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   443
        p.getY().setValue(t1).setSum(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   444
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   445
        t4.setDifference(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   446
        p.getX().setSum(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   447
        p.getY().setValue(p2.getX()).setSum(p2.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   448
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   449
        p.getX().setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   450
        p.getY().setValue(t0).setSum(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   451
        p.getY().setAdditiveInverse().setSum(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   452
        p.getY().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   453
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   454
        p.getZ().setValue(t2).setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   455
        p.getX().setValue(p.getY()).setDifference(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   456
        p.getZ().setValue(p.getX()).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   457
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   458
        p.getX().setSum(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   459
        p.getX().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   460
        p.getZ().setValue(t1).setDifference(p.getX());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   461
        p.getX().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   462
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   463
        p.getY().setProduct(b);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   464
        t1.setValue(t2).setSum(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   465
        t2.setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   466
        t2.setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   467
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   468
        p.getY().setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   469
        p.getY().setDifference(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   470
        p.getY().setReduced();
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   471
        t1.setValue(p.getY()).setSum(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   472
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   473
        p.getY().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   474
        t1.setValue(t0).setProduct(two);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   475
        t0.setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   476
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   477
        t0.setDifference(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   478
        t1.setValue(t4).setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   479
        t2.setValue(t0).setProduct(p.getY());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   480
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   481
        p.getY().setValue(p.getX()).setProduct(p.getZ());
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   482
        p.getY().setSum(t2);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   483
        p.getX().setProduct(t3);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   484
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   485
        p.getX().setDifference(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   486
        p.getZ().setProduct(t4);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   487
        t1.setValue(t3).setProduct(t0);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   488
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   489
        p.getZ().setSum(t1);
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   490
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   491
    }
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   492
}
752e57845ad2 8208698: Improved ECC Implementation
apetcher
parents:
diff changeset
   493