summary refs log tree commit diff
path: root/crypto/src/pqc/crypto/picnic/LowmcConstants.cs
blob: ed6756d50acd286df9b5fbaed655558cec2703ce (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
using System;

namespace Org.BouncyCastle.Pqc.Crypto.Picnic
{
    internal abstract class LowmcConstants
    {
        internal KMatrices _LMatrix;
        internal KMatrices _KMatrix;
        internal KMatrices RConstants;

        internal KMatrices LMatrix_full;
        internal KMatrices LMatrix_inv;
        internal KMatrices KMatrix_full;
        internal KMatrices KMatrix_inv;
        internal KMatrices RConstants_full;

        /// <summary>Return a pointer to the r-th matrix.</summary>
        /// <remarks>The caller must know the dimensions.</remarks>
        private KMatricesWithPointer GET_MAT(KMatrices m, int r)
        {
            KMatricesWithPointer mwp = new KMatricesWithPointer(m);
            mwp.SetMatrixPointer(r * mwp.GetSize());
            return mwp;
        }

        /// <summary>Return the LowMC linear matrix for this round.</summary>
        internal KMatricesWithPointer LMatrix(PicnicEngine engine, int round)
        {
            switch (engine.stateSizeBits)
            {
            case 128:
            case 256:
                return GET_MAT(_LMatrix, round);
            case 129:
            case 255:
                return GET_MAT(LMatrix_full, round);
            case 192:
                return GET_MAT(engine.numRounds == 4 ? LMatrix_full : _LMatrix, round);
            default:
                return null;
            }
        }

        /// <summary>Return the LowMC inverse linear layer matrix for this round.</summary>
        internal KMatricesWithPointer LMatrixInv(PicnicEngine engine, int round)
        {
            switch (engine.stateSizeBits)
            {
            case 129:
            case 255:
                return GET_MAT(LMatrix_inv, round);
            case 192:
                return engine.numRounds == 4 ? GET_MAT(LMatrix_inv, round) : null;
            default:
                return null;
            }
        }

        /// <summary>Return the LowMC key matrix for this round.</summary>
        internal KMatricesWithPointer KMatrix(PicnicEngine engine, int round)
        {
            switch (engine.stateSizeBits)
            {
            case 128:
            case 256:
                return GET_MAT(_KMatrix, round);
            case 129:
            case 255:
                return GET_MAT(KMatrix_full, round);
            case 192:
                return GET_MAT(engine.numRounds == 4 ? KMatrix_full : _KMatrix, round);
            default:
                return null;
            }
        }

        /// <summary>Return the LowMC inverse key matrix for this round.</summary>
        internal KMatricesWithPointer KMatrixInv(PicnicEngine engine, int round)
        {
            switch (engine.stateSizeBits)
            {
            case 129:
            case 255:
                return GET_MAT(KMatrix_inv, round);
            case 192:
                return engine.numRounds == 4 ? GET_MAT(KMatrix_inv, round) : null;
            default:
                return null;
            }
        }

        /// <summary>Return the LowMC round constant for this round.</summary>
        internal KMatricesWithPointer RConstant(PicnicEngine engine, int round)
        {
            switch (engine.stateSizeBits)
            {
            case 128:
            case 256:
                return GET_MAT(RConstants, round);
            case 129:
            case 255:
                return GET_MAT(RConstants_full, round);
            case 192:
                return GET_MAT(engine.numRounds == 4 ? RConstants_full : RConstants, round);
            default:
                return null;
            }
        }
    }
}