Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BigEndianConversions.java « linearalgebra « math « pqc « bouncycastle « org « java « main « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 90926f67f7e5d151c630f748315c35372d977b9c (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package org.bouncycastle.pqc.math.linearalgebra;


/**
 * This is a utility class containing data type conversions using big-endian
 * byte order.
 *
 * @see LittleEndianConversions
 */
public final class BigEndianConversions
{

    /**
     * Default constructor (private).
     */
    private BigEndianConversions()
    {
        // empty
    }

    /**
     * Convert an integer to an octet string of length 4 according to IEEE 1363,
     * Section 5.5.3.
     *
     * @param x the integer to convert
     * @return the converted integer
     */
    public static byte[] I2OSP(int x)
    {
        byte[] result = new byte[4];
        result[0] = (byte)(x >>> 24);
        result[1] = (byte)(x >>> 16);
        result[2] = (byte)(x >>> 8);
        result[3] = (byte)x;
        return result;
    }

    /**
     * Convert an integer to an octet string according to IEEE 1363, Section
     * 5.5.3. Length checking is performed.
     *
     * @param x    the integer to convert
     * @param oLen the desired length of the octet string
     * @return an octet string of length <tt>oLen</tt> representing the
     *         integer <tt>x</tt>, or <tt>null</tt> if the integer is
     *         negative
     * @throws ArithmeticException if <tt>x</tt> can't be encoded into <tt>oLen</tt>
     * octets.
     */
    public static byte[] I2OSP(int x, int oLen)
        throws ArithmeticException
    {
        if (x < 0)
        {
            return null;
        }
        int octL = IntegerFunctions.ceilLog256(x);
        if (octL > oLen)
        {
            throw new ArithmeticException(
                "Cannot encode given integer into specified number of octets.");
        }
        byte[] result = new byte[oLen];
        for (int i = oLen - 1; i >= oLen - octL; i--)
        {
            result[i] = (byte)(x >>> (8 * (oLen - 1 - i)));
        }
        return result;
    }

    /**
     * Convert an integer to an octet string of length 4 according to IEEE 1363,
     * Section 5.5.3.
     *
     * @param input  the integer to convert
     * @param output byte array holding the output
     * @param outOff offset in output array where the result is stored
     */
    public static void I2OSP(int input, byte[] output, int outOff)
    {
        output[outOff++] = (byte)(input >>> 24);
        output[outOff++] = (byte)(input >>> 16);
        output[outOff++] = (byte)(input >>> 8);
        output[outOff] = (byte)input;
    }

    /**
     * Convert an integer to an octet string of length 8 according to IEEE 1363,
     * Section 5.5.3.
     *
     * @param input the integer to convert
     * @return the converted integer
     */
    public static byte[] I2OSP(long input)
    {
        byte[] output = new byte[8];
        output[0] = (byte)(input >>> 56);
        output[1] = (byte)(input >>> 48);
        output[2] = (byte)(input >>> 40);
        output[3] = (byte)(input >>> 32);
        output[4] = (byte)(input >>> 24);
        output[5] = (byte)(input >>> 16);
        output[6] = (byte)(input >>> 8);
        output[7] = (byte)input;
        return output;
    }

    /**
     * Convert an integer to an octet string of length 8 according to IEEE 1363,
     * Section 5.5.3.
     *
     * @param input  the integer to convert
     * @param output byte array holding the output
     * @param outOff offset in output array where the result is stored
     */
    public static void I2OSP(long input, byte[] output, int outOff)
    {
        output[outOff++] = (byte)(input >>> 56);
        output[outOff++] = (byte)(input >>> 48);
        output[outOff++] = (byte)(input >>> 40);
        output[outOff++] = (byte)(input >>> 32);
        output[outOff++] = (byte)(input >>> 24);
        output[outOff++] = (byte)(input >>> 16);
        output[outOff++] = (byte)(input >>> 8);
        output[outOff] = (byte)input;
    }

    /**
     * Convert an integer to an octet string of the specified length according
     * to IEEE 1363, Section 5.5.3. No length checking is performed (i.e., if
     * the integer cannot be encoded into <tt>length</tt> octets, it is
     * truncated).
     *
     * @param input  the integer to convert
     * @param output byte array holding the output
     * @param outOff offset in output array where the result is stored
     * @param length the length of the encoding
     */
    public static void I2OSP(int input, byte[] output, int outOff, int length)
    {
        for (int i = length - 1; i >= 0; i--)
        {
            output[outOff + i] = (byte)(input >>> (8 * (length - 1 - i)));
        }
    }

    /**
     * Convert an octet string to an integer according to IEEE 1363, Section
     * 5.5.3.
     *
     * @param input the byte array holding the octet string
     * @return an integer representing the octet string <tt>input</tt>, or
     *         <tt>0</tt> if the represented integer is negative or too large
     *         or the byte array is empty
     * @throws ArithmeticException if the length of the given octet string is larger than 4.
     */
    public static int OS2IP(byte[] input)
    {
        if (input.length > 4)
        {
            throw new ArithmeticException("invalid input length");
        }
        if (input.length == 0)
        {
            return 0;
        }
        int result = 0;
        for (int j = 0; j < input.length; j++)
        {
            result |= (input[j] & 0xff) << (8 * (input.length - 1 - j));
        }
        return result;
    }

    /**
     * Convert a byte array of length 4 beginning at <tt>offset</tt> into an
     * integer.
     *
     * @param input the byte array
     * @param inOff the offset into the byte array
     * @return the resulting integer
     */
    public static int OS2IP(byte[] input, int inOff)
    {
        int result = (input[inOff++] & 0xff) << 24;
        result |= (input[inOff++] & 0xff) << 16;
        result |= (input[inOff++] & 0xff) << 8;
        result |= input[inOff] & 0xff;
        return result;
    }

    /**
     * Convert an octet string to an integer according to IEEE 1363, Section
     * 5.5.3.
     *
     * @param input the byte array holding the octet string
     * @param inOff the offset in the input byte array where the octet string
     *              starts
     * @param inLen the length of the encoded integer
     * @return an integer representing the octet string <tt>bytes</tt>, or
     *         <tt>0</tt> if the represented integer is negative or too large
     *         or the byte array is empty
     */
    public static int OS2IP(byte[] input, int inOff, int inLen)
    {
        if ((input.length == 0) || input.length < inOff + inLen - 1)
        {
            return 0;
        }
        int result = 0;
        for (int j = 0; j < inLen; j++)
        {
            result |= (input[inOff + j] & 0xff) << (8 * (inLen - j - 1));
        }
        return result;
    }

    /**
     * Convert a byte array of length 8 beginning at <tt>inOff</tt> into a
     * long integer.
     *
     * @param input the byte array
     * @param inOff the offset into the byte array
     * @return the resulting long integer
     */
    public static long OS2LIP(byte[] input, int inOff)
    {
        long result = ((long)input[inOff++] & 0xff) << 56;
        result |= ((long)input[inOff++] & 0xff) << 48;
        result |= ((long)input[inOff++] & 0xff) << 40;
        result |= ((long)input[inOff++] & 0xff) << 32;
        result |= ((long)input[inOff++] & 0xff) << 24;
        result |= (input[inOff++] & 0xff) << 16;
        result |= (input[inOff++] & 0xff) << 8;
        result |= input[inOff] & 0xff;
        return result;
    }

    /**
     * Convert an int array into a byte array.
     *
     * @param input the int array
     * @return the converted array
     */
    public static byte[] toByteArray(final int[] input)
    {
        byte[] result = new byte[input.length << 2];
        for (int i = 0; i < input.length; i++)
        {
            I2OSP(input[i], result, i << 2);
        }
        return result;
    }

    /**
     * Convert an int array into a byte array of the specified length. No length
     * checking is performed (i.e., if the last integer cannot be encoded into
     * <tt>length % 4</tt> octets, it is truncated).
     *
     * @param input  the int array
     * @param length the length of the converted array
     * @return the converted array
     */
    public static byte[] toByteArray(final int[] input, int length)
    {
        final int intLen = input.length;
        byte[] result = new byte[length];
        int index = 0;
        for (int i = 0; i <= intLen - 2; i++, index += 4)
        {
            I2OSP(input[i], result, index);
        }
        I2OSP(input[intLen - 1], result, index, length - index);
        return result;
    }

    /**
     * Convert a byte array into an int array.
     *
     * @param input the byte array
     * @return the converted array
     */
    public static int[] toIntArray(byte[] input)
    {
        final int intLen = (input.length + 3) / 4;
        final int lastLen = input.length & 0x03;
        int[] result = new int[intLen];

        int index = 0;
        for (int i = 0; i <= intLen - 2; i++, index += 4)
        {
            result[i] = OS2IP(input, index);
        }
        if (lastLen != 0)
        {
            result[intLen - 1] = OS2IP(input, index, lastLen);
        }
        else
        {
            result[intLen - 1] = OS2IP(input, index);
        }

        return result;
    }

}