summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/bike/BikeUtilities.cs
blob: 40bd6d14879df0beb7720fb4077024d1e72d2fdf (plain) (blame)
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
using System;

using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Pqc.Crypto.Bike
{
    internal class BikeUtilities
    {
        internal static int GetHammingWeight(byte[] bytes)
        {
            int hammingWeight = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                hammingWeight += bytes[i];
            }
            return hammingWeight;
        }

        internal static void FromByteArrayToBitArray(byte[] output, byte[] input)
        {
            int max = (output.Length / 8);
            for (int i = 0; i < max; i++)
            {
                for (int j = 0; j != 8; j++)
                {
                    output[i * 8 + j] = (byte)((input[i] >> j) & 1);
                }
            }
            if (output.Length % 8 != 0)
            {
                int off = max * 8;
                int count = 0;
                while (off < output.Length)
                {
                    output[off++] = (byte)((input[max] >> count) & 1);
                    count++;
                }
            }
        }

        internal static void FromBitArrayToByteArray(byte[] output, byte[] input, int inputOff, int inputLen)
        {
            int count = 0;
            int pos = 0;
            while (count < inputLen)
            {
                if (count + 8 >= inputLen)
                {// last set of bits cannot have enough 8 bits
                    int b = input[inputOff + count];
                    for (int j = inputLen - count - 1; j >= 1; j--)
                    { //bin in reversed order
                        b |= input[inputOff + count + j] << j;
                    }
                    output[pos] = (byte)b;
                }
                else
                {
                    int b = input[inputOff + count];
                    for (int j = 7; j >= 1; j--)
                    { //bin in reversed order
                        b |= input[inputOff + count + j] << j;
                    }
                    output[pos] = (byte)b;
                }

                count += 8;
                pos++;
            }
        }

        internal static void GenerateRandomByteArray(byte[] res, uint size, uint weight, IXof digest)
        {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
            Span<byte> buf = stackalloc byte[4];
#else
            byte[] buf = new byte[4];
#endif

            for (int i = (int)weight - 1; i >= 0; i--)
            {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
                digest.Output(buf);
                ulong temp = Pack.LE_To_UInt32(buf);
#else
                digest.Output(buf, 0, 4);
                ulong temp = Pack.LE_To_UInt32(buf, 0);
#endif

                temp = temp * (size - (uint)i) >> 32;
                uint rand_pos = (uint)i + (uint)temp;

                if (CheckBit(res, rand_pos) != 0)
                {
                    rand_pos = (uint)i;
                }
                SetBit(res, rand_pos);
            }
        }

        private static uint CheckBit(byte[] tmp, uint position)
        {
            uint index = position / 8;
            uint pos = position % 8;
            return ((uint)tmp[index] >> (int)pos) & 1U;
        }

        private static void SetBit(byte[] tmp, uint position)
        {
            uint index = position / 8;
            uint pos = position % 8;
            tmp[index] |= (byte)(1 << (int)pos);
        }
    }
}