summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/pqc/crypto/rainbow/util/RainbowUtil.java
blob: 2b073b1b7ff09ba0b77feb54534955654e8614a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.bouncycastle.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;
    }

}