src/java.base/share/classes/sun/security/util/math/MutableIntegerModuloP.java
author apetcher
Tue, 08 May 2018 09:21:51 -0400
changeset 50052 d213d70182a9
child 52942 746602d9682f
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.nio.ByteBuffer;
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
 * An interface for mutable integers modulo a prime value. This interface
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    32
 * should be used to improve performance and avoid the allocation of a large
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    33
 * number of temporary objects.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    34
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    35
 * Methods in this interface that modify the value also return the modified
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    36
 * element. This structure enables fluent expressions like:
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    37
 * a.setSum(b).setProduct(c).setDifference(d).setSquare()
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    38
 *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    39
 */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    40
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    41
public interface MutableIntegerModuloP extends IntegerModuloP {
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
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    44
     * Swap the value of this with the value of b when swap has the value 1.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    45
     * No change is made to either element when swap has the value 0. The
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    46
     * result is undefined when swap has a value other than 0 or 1. The swap
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    47
     * parameter is an int (rather than boolean) to allow the implementation
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    48
     * to perform the swap using branch-free integer arithmetic.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    49
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    50
     * @param b the element to conditionally swap with
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    51
     * @param swap an int that determines whether to swap
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
    void conditionalSwapWith(MutableIntegerModuloP b, int swap);
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
     * Set the value of this element equal to the value of the supplied
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    57
     * element. The argument is not modified.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    58
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    59
     * @param v the element whose value should be copied to this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    60
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    61
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    62
    MutableIntegerModuloP setValue(IntegerModuloP v);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    63
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
     * Set the value equal to the little-endian unsigned integer stored at the
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    66
     * specified position in an array. The range of accepted values is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    67
     * implementation-specific. This method also takes a byte which is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    68
     * interpreted as an additional high-order byte of the number.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    69
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    70
     * @param v an array containing a little-endian unsigned integer
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    71
     * @param offset the starting position of the integer
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    72
     * @param length the number of bytes to read
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    73
     * @param highByte the high-order byte of the number
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    74
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    75
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    76
    MutableIntegerModuloP setValue(byte[] v, int offset, int length,
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    77
                                   byte highByte);
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
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    80
     * Set the value equal to the little-endian unsigned integer stored in a
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    81
     * buffer. The range of accepted values is implementation-specific.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    82
     * This method also takes a byte which is interpreted as an additional
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    83
     * high-order byte of the number.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    84
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    85
     * @param buf a buffer containing a little-endian unsigned integer
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    86
     * @param length the number of bytes to read
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    87
     * @param highByte the high-order byte of the number
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    88
     * @return this
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
    MutableIntegerModuloP setValue(ByteBuffer buf, int length, byte highByte);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    91
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
     * Set the value of this element equal to this * this.
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
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    96
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
    97
    MutableIntegerModuloP setSquare();
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
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   100
     * Set the value of this element equal to this + b. The argument is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   101
     * not modified.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   102
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   103
     * @param b the sumand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   104
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   105
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   106
    MutableIntegerModuloP setSum(IntegerModuloP b);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   107
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   108
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   109
     * Set the value of this element equal to this - b. The argument is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   110
     * not modified.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   111
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   112
     * @param b the subtrahend
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   113
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   114
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   115
    MutableIntegerModuloP setDifference(IntegerModuloP b);
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   116
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   117
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   118
     * Set the value of this element equal to this * b. The argument is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   119
     * not modified.
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
     * @param b the multiplicand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   122
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   123
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   124
    MutableIntegerModuloP setProduct(IntegerModuloP 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
    /**
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   127
     * Set the value of this element equal to this * v. The argument is
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   128
     * not modified.
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   129
     *
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   130
     * @param v the small multiplicand
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   131
     * @return this
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   132
     */
d213d70182a9 8181594: Efficient and constant-time modular arithmetic
apetcher
parents:
diff changeset
   133
    MutableIntegerModuloP setProduct(SmallValue v);
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