// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.pqc.crypto.picnic;

import java.io.ByteArrayOutputStream;
import org.bouncycastle.util.Pack;
import org.bouncycastle.util.encoders.Hex;
import java.util.Properties;
import java.io.IOException;
import java.io.DataInputStream;

abstract class LowmcConstants
{
    protected int[] linearMatrices;
    protected int[] roundConstants;
    protected int[] keyMatrices;
    protected KMatrices LMatrix;
    protected KMatrices KMatrix;
    protected KMatrices RConstants;
    protected int[] linearMatrices_full;
    protected int[] keyMatrices_full;
    protected int[] keyMatrices_inv;
    protected int[] linearMatrices_inv;
    protected int[] roundConstants_full;
    protected KMatrices LMatrix_full;
    protected KMatrices LMatrix_inv;
    protected KMatrices KMatrix_full;
    protected KMatrices KMatrix_inv;
    protected KMatrices RConstants_full;
    
    static int[] readArray(final DataInputStream dataInputStream) throws IOException {
        final int[] array = new int[dataInputStream.readInt()];
        for (int i = 0; i != array.length; ++i) {
            array[i] = dataInputStream.readInt();
        }
        return array;
    }
    
    static int[] ReadFromProperty(final Properties properties, final String key, final int n) {
        final byte[] decode = Hex.decode(removeCommas(properties.getProperty(key)));
        final int[] array = new int[n];
        for (int i = 0; i < decode.length / 4; ++i) {
            array[i] = Pack.littleEndianToInt(decode, i * 4);
        }
        return array;
    }
    
    private static byte[] removeCommas(final String s) {
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        for (int i = 0; i != s.length(); ++i) {
            if (s.charAt(i) != ',') {
                byteArrayOutputStream.write(s.charAt(i));
            }
        }
        return byteArrayOutputStream.toByteArray();
    }
    
    private KMatricesWithPointer GET_MAT(final KMatrices kMatrices, final int n) {
        final KMatricesWithPointer kMatricesWithPointer = new KMatricesWithPointer(kMatrices);
        kMatricesWithPointer.setMatrixPointer(n * kMatricesWithPointer.getSize());
        return kMatricesWithPointer;
    }
    
    protected KMatricesWithPointer LMatrix(final PicnicEngine picnicEngine, final int n) {
        if (picnicEngine.stateSizeBits == 128) {
            return this.GET_MAT(this.LMatrix, n);
        }
        if (picnicEngine.stateSizeBits == 129) {
            return this.GET_MAT(this.LMatrix_full, n);
        }
        if (picnicEngine.stateSizeBits == 192) {
            if (picnicEngine.numRounds == 4) {
                return this.GET_MAT(this.LMatrix_full, n);
            }
            return this.GET_MAT(this.LMatrix, n);
        }
        else {
            if (picnicEngine.stateSizeBits == 255) {
                return this.GET_MAT(this.LMatrix_full, n);
            }
            if (picnicEngine.stateSizeBits == 256) {
                return this.GET_MAT(this.LMatrix, n);
            }
            return null;
        }
    }
    
    protected KMatricesWithPointer LMatrixInv(final PicnicEngine picnicEngine, final int n) {
        if (picnicEngine.stateSizeBits == 129) {
            return this.GET_MAT(this.LMatrix_inv, n);
        }
        if (picnicEngine.stateSizeBits == 192 && picnicEngine.numRounds == 4) {
            return this.GET_MAT(this.LMatrix_inv, n);
        }
        if (picnicEngine.stateSizeBits == 255) {
            return this.GET_MAT(this.LMatrix_inv, n);
        }
        return null;
    }
    
    protected KMatricesWithPointer KMatrix(final PicnicEngine picnicEngine, final int n) {
        if (picnicEngine.stateSizeBits == 128) {
            return this.GET_MAT(this.KMatrix, n);
        }
        if (picnicEngine.stateSizeBits == 129) {
            return this.GET_MAT(this.KMatrix_full, n);
        }
        if (picnicEngine.stateSizeBits == 192) {
            if (picnicEngine.numRounds == 4) {
                return this.GET_MAT(this.KMatrix_full, n);
            }
            return this.GET_MAT(this.KMatrix, n);
        }
        else {
            if (picnicEngine.stateSizeBits == 255) {
                return this.GET_MAT(this.KMatrix_full, n);
            }
            if (picnicEngine.stateSizeBits == 256) {
                return this.GET_MAT(this.KMatrix, n);
            }
            return null;
        }
    }
    
    protected KMatricesWithPointer KMatrixInv(final PicnicEngine picnicEngine) {
        final int n = 0;
        if (picnicEngine.stateSizeBits == 129) {
            return this.GET_MAT(this.KMatrix_inv, n);
        }
        if (picnicEngine.stateSizeBits == 192 && picnicEngine.numRounds == 4) {
            return this.GET_MAT(this.KMatrix_inv, n);
        }
        if (picnicEngine.stateSizeBits == 255) {
            return this.GET_MAT(this.KMatrix_inv, n);
        }
        return null;
    }
    
    protected KMatricesWithPointer RConstant(final PicnicEngine picnicEngine, final int n) {
        if (picnicEngine.stateSizeBits == 128) {
            return this.GET_MAT(this.RConstants, n);
        }
        if (picnicEngine.stateSizeBits == 129) {
            return this.GET_MAT(this.RConstants_full, n);
        }
        if (picnicEngine.stateSizeBits == 192) {
            if (picnicEngine.numRounds == 4) {
                return this.GET_MAT(this.RConstants_full, n);
            }
            return this.GET_MAT(this.RConstants, n);
        }
        else {
            if (picnicEngine.stateSizeBits == 255) {
                return this.GET_MAT(this.RConstants_full, n);
            }
            if (picnicEngine.stateSizeBits == 256) {
                return this.GET_MAT(this.RConstants, n);
            }
            return null;
        }
    }
}
