summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/crystals/dilithium/Packing.cs
blob: 0f1fec1d2ba45288cc77d56fd22d0b581af4a922 (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
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
using Org.BouncyCastle.Utilities;
using System;

namespace Org.BouncyCastle.Pqc.Crypto.Crystals.Dilithium
{
    internal class Packing
    {

        public static byte[] PackPublicKey(PolyVecK t1, DilithiumEngine Engine)
        {
            byte[] output = new byte[Engine.CryptoPublicKeyBytes - DilithiumEngine.SeedBytes];

            for (int i = 0; i < Engine.K; i++)
            {
                Array.Copy(t1.Vec[i].PolyT1Pack(), 0, output, i * DilithiumEngine.PolyT1PackedBytes, DilithiumEngine.PolyT1PackedBytes );
            }
            return output;
        }

        public static PolyVecK UnpackPublicKey(PolyVecK t1, byte[] pk, DilithiumEngine Engine)
        {
            int i;
            for (i = 0; i < Engine.K; ++i)
            {
                t1.Vec[i].PolyT1Unpack(Arrays.CopyOfRange(pk, i * DilithiumEngine.PolyT1PackedBytes, DilithiumEngine.SeedBytes + (i + 1) * DilithiumEngine.PolyT1PackedBytes));
            }

            return t1;
        }

        public static void PackSecretKey(byte[] t0_, byte[] s1_, byte[] s2_, PolyVecK t0, PolyVecL s1, PolyVecK s2, DilithiumEngine Engine)
        {
            int i;
            

            for (i = 0; i < Engine.L; ++i)
            {
                s1.Vec[i].PolyEtaPack(s1_, i * Engine.PolyEtaPackedBytes);
            }

            for (i = 0; i < Engine.K; ++i)
            {
                s2.Vec[i].PolyEtaPack(s2_, i * Engine.PolyEtaPackedBytes);
            }

            for (i = 0; i < Engine.K; ++i)
            {
                t0.Vec[i].PolyT0Pack(t0_,i * DilithiumEngine.PolyT0PackedBytes);
            }
        }

        public static void UnpackSecretKey(PolyVecK t0, PolyVecL s1, PolyVecK s2, byte[] t0Enc, byte[] s1Enc, byte[] s2Enc, DilithiumEngine Engine)
        {
            int i;
            for (i = 0; i < Engine.L; ++i)
            {
                s1.Vec[i].PolyEtaUnpack(s1Enc,i * Engine.PolyEtaPackedBytes);
            }
            for (i = 0; i < Engine.K; ++i)
            {
                s2.Vec[i].PolyEtaUnpack(s2Enc,i * Engine.PolyEtaPackedBytes);
            }
            for (i = 0; i < Engine.K; ++i)
            {
                t0.Vec[i].PolyT0Unpack(t0Enc,i * DilithiumEngine.PolyT0PackedBytes);
            }
        }

        public static void PackSignature(byte[] sig, byte[] c, PolyVecL z, PolyVecK h, DilithiumEngine engine)
        {
            int i, j, k, end = 0;

            Array.Copy(c, 0, sig, 0, DilithiumEngine.SeedBytes);
            end += DilithiumEngine.SeedBytes;

            for (i = 0; i < engine.L; ++i)
            {
                z.Vec[i].PackZ(sig, end + i * engine.PolyZPackedBytes);
            }
            end += engine.L * engine.PolyZPackedBytes;

            for (i = 0; i < engine.Omega + engine.K; ++i)
            {
                sig[end + i] = 0;
            }


            k = 0;
            for (i = 0; i < engine.K; ++i)
            {
                for (j = 0; j < DilithiumEngine.N; ++j)
                {
                    if (h.Vec[i].Coeffs[j] != 0)
                    {
                        sig[end + k++] = (byte)j;
                    }
                }
                sig[end + engine.Omega + i] = (byte)k;
            }
            //Console.WriteLine("sig = " + Convert.ToHexString(sig));

        }

        public static bool UnpackSignature(PolyVecL z, PolyVecK h, byte[] sig, DilithiumEngine Engine)
        {
            int i, j, k;
            
            int end = DilithiumEngine.SeedBytes;
            for (i = 0; i < Engine.L; ++i)
            {
                z.Vec[i].UnpackZ(Arrays.CopyOfRange(sig, end + i * Engine.PolyZPackedBytes, end + (i + 1) * Engine.PolyZPackedBytes));
            }
            end += Engine.L * Engine.PolyZPackedBytes;

            k = 0;
            for (i = 0; i < Engine.K; ++i)
            {
                for (j = 0; j < DilithiumEngine.N; ++j)
                {
                    h.Vec[i].Coeffs[j] = 0;
                }

                if ((sig[end + Engine.Omega + i] & 0xFF) < k || (sig[end + Engine.Omega + i] & 0xFF) > Engine.Omega)
                {
                    return false;
                }

                for (j = k; j < (sig[end + Engine.Omega + i] & 0xFF); ++j)
                {
                    if (j > k && (sig[end + j] & 0xFF) <= (sig[end + j - 1] & 0xFF))
                    {
                        return false;
                    }
                    h.Vec[i].Coeffs[sig[end + j] & 0xFF] = 1;
                }

                k = (int)(sig[end + Engine.Omega + i]);
            }
            for (j = k; j < Engine.Omega; ++j)
            {
                if ((sig[end + j] & 0xFF) != 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}