src/java.base/share/classes/sun/security/util/math/IntegerModuloP.java
author apetcher
Tue, 08 May 2018 09:21:51 -0400
changeset 50052 d213d70182a9
permissions -rw-r--r--
8181594: Efficient and constant-time modular arithmetic Summary: Field arithmetic library for crypto algorithms like Poly1305 and X25519 Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50052
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     1
/*
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     4
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    10
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    15
 * accompanied this code).
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    16
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    20
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    23
 * questions.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    24
 */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    25
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    26
package sun.security.util.math;
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    27
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    28
import java.math.BigInteger;
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    29
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    30
/**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    31
 * The base interface for integers modulo a prime value. Objects of this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    32
 * type may be either mutable or immutable, and subinterfaces can be used
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    33
 * to specify that an object is mutable or immutable. This type should never
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    34
 * be used to declare local/member variables, but it may be used for
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    35
 * formal parameters of a method. None of the methods in this interface
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    36
 * modify the value of arguments or this.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    37
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    38
 * The behavior of this interface depends on the particular implementation.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    39
 * For example, some implementations only support a limited number of add
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    40
 * operations before each multiply operation. See the documentation of the
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    41
 * implementation for details.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    42
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    43
 * @see ImmutableIntegerModuloP
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    44
 * @see MutableIntegerModuloP
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    45
 */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    46
public interface IntegerModuloP {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    47
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    48
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    49
     * Get the field associated with this element.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    50
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    51
     * @return the field
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    52
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    53
    IntegerFieldModuloP getField();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    54
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    55
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    56
     * Get the canonical value of this element as a BigInteger. This value
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    57
     * will always be in the range [0, p), where p is the prime that defines
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    58
     * the field. This method performs reduction and other computation to
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    59
     * produce the result.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    60
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    61
     * @return the value as a BigInteger
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    62
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    63
    BigInteger asBigInteger();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    64
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    65
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    66
     * Return this value as a fixed (immutable) element. This method will
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    67
     * copy the underlying representation if the object is mutable.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    68
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    69
     * @return a fixed element with the same value
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    70
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    71
    ImmutableIntegerModuloP fixed();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    72
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    73
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    74
     * Return this value as a mutable element. This method will always copy
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    75
     * the underlying representation.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    76
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    77
     * @return a mutable element with the same value
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    78
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    79
    MutableIntegerModuloP mutable();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    80
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    81
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    82
     * Add this field element with the supplied element and return the result.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    83
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    84
     * @param b the sumand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    85
     * @return this + b
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    86
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    87
    ImmutableIntegerModuloP add(IntegerModuloP b);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    88
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    89
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    90
     * Compute the additive inverse of the field element
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    91
     * @return the addditiveInverse (0 - this)
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    92
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    93
    ImmutableIntegerModuloP additiveInverse();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    94
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    95
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    96
     * Multiply this field element with the supplied element and return the
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    97
     * result.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    98
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    99
     * @param b the multiplicand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   100
     * @return this * b
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   101
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   102
    ImmutableIntegerModuloP multiply(IntegerModuloP b);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   103
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   104
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   105
     * Perform an addition modulo a power of two and return the little-endian
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   106
     * encoding of the result. The value is (this' + b') % 2^(8 * len),
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   107
     * where this' and b' are the canonical integer values equivalent to
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   108
     * this and b.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   109
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   110
     * @param b the sumand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   111
     * @param len the length of the desired array
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   112
     * @return a byte array of length len containing the result
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   113
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   114
    default byte[] addModPowerTwo(IntegerModuloP b, int len) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   115
        byte[] result = new byte[len];
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   116
        addModPowerTwo(b, result);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   117
        return result;
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   118
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   119
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   120
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   121
     * Perform an addition modulo a power of two and store the little-endian
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   122
     * encoding of the result in the supplied array. The value is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   123
     * (this' + b') % 2^(8 * result.length), where this' and b' are the
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   124
     * canonical integer values equivalent to this and b.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   125
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   126
     * @param b the sumand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   127
     * @param result an array which stores the result upon return
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   128
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   129
    void addModPowerTwo(IntegerModuloP b, byte[] result);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   130
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   131
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   132
     * Returns the little-endian encoding of this' % 2^(8 * len), where this'
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   133
     * is the canonical integer value equivalent to this.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   134
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   135
     * @param len the length of the desired array
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   136
     * @return a byte array of length len containing the result
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   137
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   138
    default byte[] asByteArray(int len) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   139
        byte[] result = new byte[len];
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   140
        asByteArray(result);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   141
        return result;
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   142
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   143
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   144
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   145
     * Places the little-endian encoding of this' % 2^(8 * result.length)
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   146
     * into the supplied array, where this' is the canonical integer value
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   147
     * equivalent to this.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   148
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   149
     * @param result an array which stores the result upon return
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   150
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   151
    void asByteArray(byte[] result);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   152
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   153
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   154
     * Compute the multiplicative inverse of this field element.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   155
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   156
     * @return the multiplicative inverse (1 / this)
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   157
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   158
    default ImmutableIntegerModuloP multiplicativeInverse() {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   159
        return pow(getField().getSize().subtract(BigInteger.valueOf(2)));
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   160
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   161
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   162
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   163
     * Subtract the supplied element from this one and return the result.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   164
     * @param b the subtrahend
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   165
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   166
     * @return the difference (this - b)
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   167
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   168
    default ImmutableIntegerModuloP subtract(IntegerModuloP b) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   169
        return add(b.additiveInverse());
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   170
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   171
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   172
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   173
     * Calculate the square of this element and return the result. This method
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   174
     * should be used instead of a.multiply(a) because implementations may
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   175
     * include optimizations that only apply to squaring.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   176
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   177
     * @return the product (this * this)
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   178
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   179
    default ImmutableIntegerModuloP square() {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   180
        return multiply(this);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   181
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   182
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   183
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   184
     * Calculate the power this^b and return the result.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   185
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   186
     * @param b the exponent
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   187
     * @return the value of this^b
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   188
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   189
    default ImmutableIntegerModuloP pow(BigInteger b) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   190
        //Default implementation is square and multiply
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   191
        MutableIntegerModuloP y = getField().get1().mutable();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   192
        MutableIntegerModuloP x = mutable();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   193
        int bitLength = b.bitLength();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   194
        for (int bit = 0; bit < bitLength; bit++) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   195
            if (b.testBit(bit)) {
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   196
                // odd
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   197
                y.setProduct(x);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   198
            }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   199
            x.setSquare();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   200
        }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   201
        return y.fixed();
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   202
    }
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   203
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   204
}
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   205