diff options
author | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-15 01:38:01 +0400 |
---|---|---|
committer | Roberto Tyley <roberto.tyley@gmail.com> | 2014-07-26 11:23:17 +0400 |
commit | 7cb752aaf746dc0b473afeb9e892b7fbc12666c5 (patch) | |
tree | cc4f91ddc18332b5adbe82e3fcb040d976c90105 /core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util | |
parent | 551830f8ea5177042af2c7dd1fc90888bc67387d (diff) |
Execute become-spongy.sh
https://github.com/rtyley/spongycastle/blob/3040af/become-spongy.sh
Diffstat (limited to 'core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util')
3 files changed, 859 insertions, 0 deletions
diff --git a/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/ComputeInField.java b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/ComputeInField.java new file mode 100644 index 00000000..1e4b6c0f --- /dev/null +++ b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/ComputeInField.java @@ -0,0 +1,490 @@ +package org.spongycastle.pqc.crypto.rainbow.util; + +/** + * This class offers different operations on matrices in field GF2^8. + * <p> + * Implemented are functions: + * - finding inverse of a matrix + * - solving linear equation systems using the Gauss-Elimination method + * - basic operations like matrix multiplication, addition and so on. + */ + +public class ComputeInField +{ + + private short[][] A; // used by solveEquation and inverse + short[] x; + + /** + * Constructor with no parameters + */ + public ComputeInField() + { + } + + + /** + * This function finds a solution of the equation Bx = b. + * Exception is thrown if the linear equation system has no solution + * + * @param B this matrix is the left part of the + * equation (B in the equation above) + * @param b the right part of the equation + * (b in the equation above) + * @return x the solution of the equation if it is solvable + * null otherwise + * @throws RuntimeException if LES is not solvable + */ + public short[] solveEquation(short[][] B, short[] b) + { + try + { + + if (B.length != b.length) + { + throw new RuntimeException( + "The equation system is not solvable"); + } + + /** initialize **/ + // this matrix stores B and b from the equation B*x = b + // b is stored as the last column. + // B contains one column more than rows. + // In this column we store a free coefficient that should be later subtracted from b + A = new short[B.length][B.length + 1]; + // stores the solution of the LES + x = new short[B.length]; + + /** copy B into the global matrix A **/ + for (int i = 0; i < B.length; i++) + { // rows + for (int j = 0; j < B[0].length; j++) + { // cols + A[i][j] = B[i][j]; + } + } + + /** copy the vector b into the global A **/ + //the free coefficient, stored in the last column of A( A[i][b.length] + // is to be subtracted from b + for (int i = 0; i < b.length; i++) + { + A[i][b.length] = GF2Field.addElem(b[i], A[i][b.length]); + } + + /** call the methods for gauss elimination and backward substitution **/ + computeZerosUnder(false); // obtain zeros under the diagonal + substitute(); + + return x; + + } + catch (RuntimeException rte) + { + return null; // the LES is not solvable! + } + } + + /** + * This function computes the inverse of a given matrix using the Gauss- + * Elimination method. + * <p> + * An exception is thrown if the matrix has no inverse + * + * @param coef the matrix which inverse matrix is needed + * @return inverse matrix of the input matrix. + * If the matrix is singular, null is returned. + * @throws RuntimeException if the given matrix is not invertible + */ + public short[][] inverse(short[][] coef) + { + try + { + /** Initialization: **/ + short factor; + short[][] inverse; + A = new short[coef.length][2 * coef.length]; + if (coef.length != coef[0].length) + { + throw new RuntimeException( + "The matrix is not invertible. Please choose another one!"); + } + + /** prepare: Copy coef and the identity matrix into the global A. **/ + for (int i = 0; i < coef.length; i++) + { + for (int j = 0; j < coef.length; j++) + { + //copy the input matrix coef into A + A[i][j] = coef[i][j]; + } + // copy the identity matrix into A. + for (int j = coef.length; j < 2 * coef.length; j++) + { + A[i][j] = 0; + } + A[i][i + A.length] = 1; + } + + /** Elimination operations to get the identity matrix from the left side of A. **/ + // modify A to get 0s under the diagonal. + computeZerosUnder(true); + + // modify A to get only 1s on the diagonal: A[i][j] =A[i][j]/A[i][i]. + for (int i = 0; i < A.length; i++) + { + factor = GF2Field.invElem(A[i][i]); + for (int j = i; j < 2 * A.length; j++) + { + A[i][j] = GF2Field.multElem(A[i][j], factor); + } + } + + //modify A to get only 0s above the diagonal. + computeZerosAbove(); + + // copy the result (the second half of A) in the matrix inverse. + inverse = new short[A.length][A.length]; + for (int i = 0; i < A.length; i++) + { + for (int j = A.length; j < 2 * A.length; j++) + { + inverse[i][j - A.length] = A[i][j]; + } + } + return inverse; + + } + catch (RuntimeException rte) + { + // The matrix is not invertible! A new one should be generated! + return null; + } + } + + /** + * Elimination under the diagonal. + * This function changes a matrix so that it contains only zeros under the + * diagonal(Ai,i) using only Gauss-Elimination operations. + * <p/> + * It is used in solveEquaton as well as in the function for + * finding an inverse of a matrix: {@link}inverse. Both of them use the + * Gauss-Elimination Method. + * <p/> + * The result is stored in the global matrix A + * + * @param usedForInverse This parameter shows if the function is used by the + * solveEquation-function or by the inverse-function and according + * to this creates matrices of different sizes. + * @throws RuntimeException in case a multiplicative inverse of 0 is needed + */ + private void computeZerosUnder(boolean usedForInverse) + throws RuntimeException + { + + //the number of columns in the global A where the tmp results are stored + int length; + short tmp = 0; + + //the function is used in inverse() - A should have 2 times more columns than rows + if (usedForInverse) + { + length = 2 * A.length; + } + //the function is used in solveEquation - A has 1 column more than rows + else + { + length = A.length + 1; + } + + //elimination operations to modify A so that that it contains only 0s under the diagonal + for (int k = 0; k < A.length - 1; k++) + { // the fixed row + for (int i = k + 1; i < A.length; i++) + { // rows + short factor1 = A[i][k]; + short factor2 = GF2Field.invElem(A[k][k]); + + //The element which multiplicative inverse is needed, is 0 + //in this case is the input matrix not invertible + if (factor2 == 0) + { + throw new RuntimeException("Matrix not invertible! We have to choose another one!"); + } + + for (int j = k; j < length; j++) + {// columns + // tmp=A[k,j] / A[k,k] + tmp = GF2Field.multElem(A[k][j], factor2); + // tmp = A[i,k] * A[k,j] / A[k,k] + tmp = GF2Field.multElem(factor1, tmp); + // A[i,j]=A[i,j]-A[i,k]/A[k,k]*A[k,j]; + A[i][j] = GF2Field.addElem(A[i][j], tmp); + } + } + } + } + + /** + * Elimination above the diagonal. + * This function changes a matrix so that it contains only zeros above the + * diagonal(Ai,i) using only Gauss-Elimination operations. + * <p/> + * It is used in the inverse-function + * The result is stored in the global matrix A + * + * @throws RuntimeException in case a multiplicative inverse of 0 is needed + */ + private void computeZerosAbove() + throws RuntimeException + { + short tmp = 0; + for (int k = A.length - 1; k > 0; k--) + { // the fixed row + for (int i = k - 1; i >= 0; i--) + { // rows + short factor1 = A[i][k]; + short factor2 = GF2Field.invElem(A[k][k]); + if (factor2 == 0) + { + throw new RuntimeException("The matrix is not invertible"); + } + for (int j = k; j < 2 * A.length; j++) + { // columns + // tmp = A[k,j] / A[k,k] + tmp = GF2Field.multElem(A[k][j], factor2); + // tmp = A[i,k] * A[k,j] / A[k,k] + tmp = GF2Field.multElem(factor1, tmp); + // A[i,j] = A[i,j] - A[i,k] / A[k,k] * A[k,j]; + A[i][j] = GF2Field.addElem(A[i][j], tmp); + } + } + } + } + + + /** + * This function uses backward substitution to find x + * of the linear equation system (LES) B*x = b, + * where A a triangle-matrix is (contains only zeros under the diagonal) + * and b is a vector + * <p/> + * If the multiplicative inverse of 0 is needed, an exception is thrown. + * In this case is the LES not solvable + * + * @throws RuntimeException in case a multiplicative inverse of 0 is needed + */ + private void substitute() + throws RuntimeException + { + + // for the temporary results of the operations in field + short tmp, temp; + + temp = GF2Field.invElem(A[A.length - 1][A.length - 1]); + if (temp == 0) + { + throw new RuntimeException("The equation system is not solvable"); + } + + /** backward substitution **/ + x[A.length - 1] = GF2Field.multElem(A[A.length - 1][A.length], temp); + for (int i = A.length - 2; i >= 0; i--) + { + tmp = A[i][A.length]; + for (int j = A.length - 1; j > i; j--) + { + temp = GF2Field.multElem(A[i][j], x[j]); + tmp = GF2Field.addElem(tmp, temp); + } + + temp = GF2Field.invElem(A[i][i]); + if (temp == 0) + { + throw new RuntimeException("Not solvable equation system"); + } + x[i] = GF2Field.multElem(tmp, temp); + } + } + + + /** + * This function multiplies two given matrices. + * If the given matrices cannot be multiplied due + * to different sizes, an exception is thrown. + * + * @param M1 -the 1st matrix + * @param M2 -the 2nd matrix + * @return A = M1*M2 + * @throws RuntimeException in case the given matrices cannot be multiplied + * due to different dimensions. + */ + public short[][] multiplyMatrix(short[][] M1, short[][] M2) + throws RuntimeException + { + + if (M1[0].length != M2.length) + { + throw new RuntimeException("Multiplication is not possible!"); + } + short tmp = 0; + A = new short[M1.length][M2[0].length]; + for (int i = 0; i < M1.length; i++) + { + for (int j = 0; j < M2.length; j++) + { + for (int k = 0; k < M2[0].length; k++) + { + tmp = GF2Field.multElem(M1[i][j], M2[j][k]); + A[i][k] = GF2Field.addElem(A[i][k], tmp); + } + } + } + return A; + } + + /** + * This function multiplies a given matrix with a one-dimensional array. + * <p> + * An exception is thrown, if the number of columns in the matrix and + * the number of rows in the one-dim. array differ. + * + * @param M1 the matrix to be multiplied + * @param m the one-dimensional array to be multiplied + * @return M1*m + * @throws RuntimeException in case of dimension inconsistency + */ + public short[] multiplyMatrix(short[][] M1, short[] m) + throws RuntimeException + { + if (M1[0].length != m.length) + { + throw new RuntimeException("Multiplication is not possible!"); + } + short tmp = 0; + short[] B = new short[M1.length]; + for (int i = 0; i < M1.length; i++) + { + for (int j = 0; j < m.length; j++) + { + tmp = GF2Field.multElem(M1[i][j], m[j]); + B[i] = GF2Field.addElem(B[i], tmp); + } + } + return B; + } + + /** + * Addition of two vectors + * + * @param vector1 first summand, always of dim n + * @param vector2 second summand, always of dim n + * @return addition of vector1 and vector2 + * @throws RuntimeException in case the addition is impossible + * due to inconsistency in the dimensions + */ + public short[] addVect(short[] vector1, short[] vector2) + { + if (vector1.length != vector2.length) + { + throw new RuntimeException("Multiplication is not possible!"); + } + short rslt[] = new short[vector1.length]; + for (int n = 0; n < rslt.length; n++) + { + rslt[n] = GF2Field.addElem(vector1[n], vector2[n]); + } + return rslt; + } + + /** + * Multiplication of column vector with row vector + * + * @param vector1 column vector, always n x 1 + * @param vector2 row vector, always 1 x n + * @return resulting n x n matrix of multiplication + * @throws RuntimeException in case the multiplication is impossible due to + * inconsistency in the dimensions + */ + public short[][] multVects(short[] vector1, short[] vector2) + { + if (vector1.length != vector2.length) + { + throw new RuntimeException("Multiplication is not possible!"); + } + short rslt[][] = new short[vector1.length][vector2.length]; + for (int i = 0; i < vector1.length; i++) + { + for (int j = 0; j < vector2.length; j++) + { + rslt[i][j] = GF2Field.multElem(vector1[i], vector2[j]); + } + } + return rslt; + } + + /** + * Multiplies vector with scalar + * + * @param scalar galois element to multiply vector with + * @param vector vector to be multiplied + * @return vector multiplied with scalar + */ + public short[] multVect(short scalar, short[] vector) + { + short rslt[] = new short[vector.length]; + for (int n = 0; n < rslt.length; n++) + { + rslt[n] = GF2Field.multElem(scalar, vector[n]); + } + return rslt; + } + + /** + * Multiplies matrix with scalar + * + * @param scalar galois element to multiply matrix with + * @param matrix 2-dim n x n matrix to be multiplied + * @return matrix multiplied with scalar + */ + public short[][] multMatrix(short scalar, short[][] matrix) + { + short[][] rslt = new short[matrix.length][matrix[0].length]; + for (int i = 0; i < matrix.length; i++) + { + for (int j = 0; j < matrix[0].length; j++) + { + rslt[i][j] = GF2Field.multElem(scalar, matrix[i][j]); + } + } + return rslt; + } + + /** + * Adds the n x n matrices matrix1 and matrix2 + * + * @param matrix1 first summand + * @param matrix2 second summand + * @return addition of matrix1 and matrix2; both having the dimensions n x n + * @throws RuntimeException in case the addition is not possible because of + * different dimensions of the matrices + */ + public short[][] addSquareMatrix(short[][] matrix1, short[][] matrix2) + { + if (matrix1.length != matrix2.length || matrix1[0].length != matrix2[0].length) + { + throw new RuntimeException("Addition is not possible!"); + } + + short[][] rslt = new short[matrix1.length][matrix1.length];// + for (int i = 0; i < matrix1.length; i++) + { + for (int j = 0; j < matrix2.length; j++) + { + rslt[i][j] = GF2Field.addElem(matrix1[i][j], matrix2[i][j]); + } + } + return rslt; + } + +} diff --git a/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/GF2Field.java b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/GF2Field.java new file mode 100644 index 00000000..675f0ec5 --- /dev/null +++ b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/GF2Field.java @@ -0,0 +1,139 @@ +package org.spongycastle.pqc.crypto.rainbow.util; + +/** + * This class provides the basic operations like addition, multiplication and + * finding the multiplicative inverse of an element in GF2^8. + * <p> + * The operations are implemented using the irreducible polynomial + * 1+x^2+x^3+x^6+x^8 ( 1 0100 1101 = 0x14d ) + * <p> + * This class makes use of lookup tables(exps and logs) for implementing the + * operations in order to increase the efficiency of Rainbow. + */ +public class GF2Field +{ + + public static final int MASK = 0xff; + + /* + * this lookup table is needed for multiplication and computing the + * multiplicative inverse + */ + static final short exps[] = {1, 2, 4, 8, 16, 32, 64, 128, 77, 154, 121, 242, + 169, 31, 62, 124, 248, 189, 55, 110, 220, 245, 167, 3, 6, 12, 24, + 48, 96, 192, 205, 215, 227, 139, 91, 182, 33, 66, 132, 69, 138, 89, + 178, 41, 82, 164, 5, 10, 20, 40, 80, 160, 13, 26, 52, 104, 208, + 237, 151, 99, 198, 193, 207, 211, 235, 155, 123, 246, 161, 15, 30, + 60, 120, 240, 173, 23, 46, 92, 184, 61, 122, 244, 165, 7, 14, 28, + 56, 112, 224, 141, 87, 174, 17, 34, 68, 136, 93, 186, 57, 114, 228, + 133, 71, 142, 81, 162, 9, 18, 36, 72, 144, 109, 218, 249, 191, 51, + 102, 204, 213, 231, 131, 75, 150, 97, 194, 201, 223, 243, 171, 27, + 54, 108, 216, 253, 183, 35, 70, 140, 85, 170, 25, 50, 100, 200, + 221, 247, 163, 11, 22, 44, 88, 176, 45, 90, 180, 37, 74, 148, 101, + 202, 217, 255, 179, 43, 86, 172, 21, 42, 84, 168, 29, 58, 116, 232, + 157, 119, 238, 145, 111, 222, 241, 175, 19, 38, 76, 152, 125, 250, + 185, 63, 126, 252, 181, 39, 78, 156, 117, 234, 153, 127, 254, 177, + 47, 94, 188, 53, 106, 212, 229, 135, 67, 134, 65, 130, 73, 146, + 105, 210, 233, 159, 115, 230, 129, 79, 158, 113, 226, 137, 95, 190, + 49, 98, 196, 197, 199, 195, 203, 219, 251, 187, 59, 118, 236, 149, + 103, 206, 209, 239, 147, 107, 214, 225, 143, 83, 166, 1}; + + /* + * this lookup table is needed for multiplication and computing the + * multiplicative inverse + */ + static final short logs[] = {0, 0, 1, 23, 2, 46, 24, 83, 3, 106, 47, 147, + 25, 52, 84, 69, 4, 92, 107, 182, 48, 166, 148, 75, 26, 140, 53, + 129, 85, 170, 70, 13, 5, 36, 93, 135, 108, 155, 183, 193, 49, 43, + 167, 163, 149, 152, 76, 202, 27, 230, 141, 115, 54, 205, 130, 18, + 86, 98, 171, 240, 71, 79, 14, 189, 6, 212, 37, 210, 94, 39, 136, + 102, 109, 214, 156, 121, 184, 8, 194, 223, 50, 104, 44, 253, 168, + 138, 164, 90, 150, 41, 153, 34, 77, 96, 203, 228, 28, 123, 231, 59, + 142, 158, 116, 244, 55, 216, 206, 249, 131, 111, 19, 178, 87, 225, + 99, 220, 172, 196, 241, 175, 72, 10, 80, 66, 15, 186, 190, 199, 7, + 222, 213, 120, 38, 101, 211, 209, 95, 227, 40, 33, 137, 89, 103, + 252, 110, 177, 215, 248, 157, 243, 122, 58, 185, 198, 9, 65, 195, + 174, 224, 219, 51, 68, 105, 146, 45, 82, 254, 22, 169, 12, 139, + 128, 165, 74, 91, 181, 151, 201, 42, 162, 154, 192, 35, 134, 78, + 188, 97, 239, 204, 17, 229, 114, 29, 61, 124, 235, 232, 233, 60, + 234, 143, 125, 159, 236, 117, 30, 245, 62, 56, 246, 217, 63, 207, + 118, 250, 31, 132, 160, 112, 237, 20, 144, 179, 126, 88, 251, 226, + 32, 100, 208, 221, 119, 173, 218, 197, 64, 242, 57, 176, 247, 73, + 180, 11, 127, 81, 21, 67, 145, 16, 113, 187, 238, 191, 133, 200, + 161}; + + /** + * This function calculates the sum of two elements as an operation in GF2^8 + * + * @param x the first element that is to be added + * @param y the second element that should be add + * @return the sum of the two elements x and y in GF2^8 + */ + public static short addElem(short x, short y) + { + return (short)(x ^ y); + } + + /** + * This function computes the multiplicative inverse of a given element in + * GF2^8 The 0 has no multiplicative inverse and in this case 0 is returned. + * + * @param x the element which multiplicative inverse is to be computed + * @return the multiplicative inverse of the given element, in case it + * exists or 0, otherwise + */ + public static short invElem(short x) + { + if (x == 0) + { + return 0; + } + return (exps[255 - logs[x]]); + } + + /** + * This function multiplies two elements in GF2^8. If one of the two + * elements is 0, 0 is returned. + * + * @param x the first element to be multiplied. + * @param y the second element to be multiplied. + * @return the product of the two input elements in GF2^8. + */ + public static short multElem(short x, short y) + { + if (x == 0 || y == 0) + { + return 0; + } + else + { + return (exps[(logs[x] + logs[y]) % 255]); + } + } + + /** + * This function returns the values of exps-lookup table which correspond to + * the input + * + * @param x the index in the lookup table exps + * @return exps-value, corresponding to the input + */ + public static short getExp(short x) + { + return exps[x]; + } + + /** + * This function returns the values of logs-lookup table which correspond to + * the input + * + * @param x the index in the lookup table logs + * @return logs-value, corresponding to the input + */ + public static short getLog(short x) + { + return logs[x]; + } + + +} diff --git a/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/RainbowUtil.java b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/RainbowUtil.java new file mode 100644 index 00000000..1e4f40f4 --- /dev/null +++ b/core/src/main/java/org/spongycastle/pqc/crypto/rainbow/util/RainbowUtil.java @@ -0,0 +1,230 @@ +package org.spongycastle.pqc.crypto.rainbow.util; + +/** + * This class is needed for the conversions while encoding and decoding, as well as for + * comparison between arrays of some dimensions + */ +public class RainbowUtil +{ + + /** + * This function converts an one-dimensional array of bytes into a + * one-dimensional array of int + * + * @param in the array to be converted + * @return out + * the one-dimensional int-array that corresponds the input + */ + public static int[] convertArraytoInt(byte[] in) + { + int[] out = new int[in.length]; + for (int i = 0; i < in.length; i++) + { + out[i] = in[i] & GF2Field.MASK; + } + return out; + } + + /** + * This function converts an one-dimensional array of bytes into a + * one-dimensional array of type short + * + * @param in the array to be converted + * @return out + * one-dimensional short-array that corresponds the input + */ + public static short[] convertArray(byte[] in) + { + short[] out = new short[in.length]; + for (int i = 0; i < in.length; i++) + { + out[i] = (short)(in[i] & GF2Field.MASK); + } + return out; + } + + /** + * This function converts a matrix of bytes into a matrix of type short + * + * @param in the matrix to be converted + * @return out + * short-matrix that corresponds the input + */ + public static short[][] convertArray(byte[][] in) + { + short[][] out = new short[in.length][in[0].length]; + for (int i = 0; i < in.length; i++) + { + for (int j = 0; j < in[0].length; j++) + { + out[i][j] = (short)(in[i][j] & GF2Field.MASK); + } + } + return out; + } + + /** + * This function converts a 3-dimensional array of bytes into a 3-dimensional array of type short + * + * @param in the array to be converted + * @return out + * short-array that corresponds the input + */ + public static short[][][] convertArray(byte[][][] in) + { + short[][][] out = new short[in.length][in[0].length][in[0][0].length]; + for (int i = 0; i < in.length; i++) + { + for (int j = 0; j < in[0].length; j++) + { + for (int k = 0; k < in[0][0].length; k++) + { + out[i][j][k] = (short)(in[i][j][k] & GF2Field.MASK); + } + } + } + return out; + } + + /** + * This function converts an array of type int into an array of type byte + * + * @param in the array to be converted + * @return out + * the byte-array that corresponds the input + */ + public static byte[] convertIntArray(int[] in) + { + byte[] out = new byte[in.length]; + for (int i = 0; i < in.length; i++) + { + out[i] = (byte)in[i]; + } + return out; + } + + + /** + * This function converts an array of type short into an array of type byte + * + * @param in the array to be converted + * @return out + * the byte-array that corresponds the input + */ + public static byte[] convertArray(short[] in) + { + byte[] out = new byte[in.length]; + for (int i = 0; i < in.length; i++) + { + out[i] = (byte)in[i]; + } + return out; + } + + /** + * This function converts a matrix of type short into a matrix of type byte + * + * @param in the matrix to be converted + * @return out + * the byte-matrix that corresponds the input + */ + public static byte[][] convertArray(short[][] in) + { + byte[][] out = new byte[in.length][in[0].length]; + for (int i = 0; i < in.length; i++) + { + for (int j = 0; j < in[0].length; j++) + { + out[i][j] = (byte)in[i][j]; + } + } + return out; + } + + /** + * This function converts a 3-dimensional array of type short into a 3-dimensional array of type byte + * + * @param in the array to be converted + * @return out + * the byte-array that corresponds the input + */ + public static byte[][][] convertArray(short[][][] in) + { + byte[][][] out = new byte[in.length][in[0].length][in[0][0].length]; + for (int i = 0; i < in.length; i++) + { + for (int j = 0; j < in[0].length; j++) + { + for (int k = 0; k < in[0][0].length; k++) + { + out[i][j][k] = (byte)in[i][j][k]; + } + } + } + return out; + } + + /** + * Compare two short arrays. No null checks are performed. + * + * @param left the first short array + * @param right the second short array + * @return the result of the comparison + */ + public static boolean equals(short[] left, short[] right) + { + if (left.length != right.length) + { + return false; + } + boolean result = true; + for (int i = left.length - 1; i >= 0; i--) + { + result &= left[i] == right[i]; + } + return result; + } + + /** + * Compare two two-dimensional short arrays. No null checks are performed. + * + * @param left the first short array + * @param right the second short array + * @return the result of the comparison + */ + public static boolean equals(short[][] left, short[][] right) + { + if (left.length != right.length) + { + return false; + } + boolean result = true; + for (int i = left.length - 1; i >= 0; i--) + { + result &= equals(left[i], right[i]); + } + return result; + } + + /** + * Compare two three-dimensional short arrays. No null checks are performed. + * + * @param left the first short array + * @param right the second short array + * @return the result of the comparison + */ + public static boolean equals(short[][][] left, short[][][] right) + { + if (left.length != right.length) + { + return false; + } + boolean result = true; + for (int i = left.length - 1; i >= 0; i--) + { + result &= equals(left[i], right[i]); + } + return result; + } + +} |