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

package org.bouncycastle.pqc.crypto.mldsa;

import org.bouncycastle.util.Arrays;

class Packing
{
    static byte[] packPublicKey(final PolyVecK polyVecK, final MLDSAEngine mldsaEngine) {
        final byte[] array = new byte[mldsaEngine.getCryptoPublicKeyBytes() - 32];
        for (int i = 0; i < mldsaEngine.getDilithiumK(); ++i) {
            System.arraycopy(polyVecK.getVectorIndex(i).polyt1Pack(), 0, array, i * 320, 320);
        }
        return array;
    }
    
    static PolyVecK unpackPublicKey(final PolyVecK polyVecK, final byte[] array, final MLDSAEngine mldsaEngine) {
        for (int i = 0; i < mldsaEngine.getDilithiumK(); ++i) {
            polyVecK.getVectorIndex(i).polyt1Unpack(Arrays.copyOfRange(array, i * 320, (i + 1) * 320));
        }
        return polyVecK;
    }
    
    static byte[][] packSecretKey(final byte[] array, final byte[] array2, final byte[] array3, final PolyVecK polyVecK, final PolyVecL polyVecL, final PolyVecK polyVecK2, final MLDSAEngine mldsaEngine) {
        final byte[][] array4 = { array, array3, array2, new byte[mldsaEngine.getDilithiumL() * mldsaEngine.getDilithiumPolyEtaPackedBytes()], null, null };
        for (int i = 0; i < mldsaEngine.getDilithiumL(); ++i) {
            polyVecL.getVectorIndex(i).polyEtaPack(array4[3], i * mldsaEngine.getDilithiumPolyEtaPackedBytes());
        }
        array4[4] = new byte[mldsaEngine.getDilithiumK() * mldsaEngine.getDilithiumPolyEtaPackedBytes()];
        for (int j = 0; j < mldsaEngine.getDilithiumK(); ++j) {
            polyVecK2.getVectorIndex(j).polyEtaPack(array4[4], j * mldsaEngine.getDilithiumPolyEtaPackedBytes());
        }
        array4[5] = new byte[mldsaEngine.getDilithiumK() * 416];
        for (int k = 0; k < mldsaEngine.getDilithiumK(); ++k) {
            polyVecK.getVectorIndex(k).polyt0Pack(array4[5], k * 416);
        }
        return array4;
    }
    
    static void unpackSecretKey(final PolyVecK polyVecK, final PolyVecL polyVecL, final PolyVecK polyVecK2, final byte[] array, final byte[] array2, final byte[] array3, final MLDSAEngine mldsaEngine) {
        for (int i = 0; i < mldsaEngine.getDilithiumL(); ++i) {
            polyVecL.getVectorIndex(i).polyEtaUnpack(array2, i * mldsaEngine.getDilithiumPolyEtaPackedBytes());
        }
        for (int j = 0; j < mldsaEngine.getDilithiumK(); ++j) {
            polyVecK2.getVectorIndex(j).polyEtaUnpack(array3, j * mldsaEngine.getDilithiumPolyEtaPackedBytes());
        }
        for (int k = 0; k < mldsaEngine.getDilithiumK(); ++k) {
            polyVecK.getVectorIndex(k).polyt0Unpack(array, k * 416);
        }
    }
    
    static void packSignature(final byte[] array, final PolyVecL polyVecL, final PolyVecK polyVecK, final MLDSAEngine mldsaEngine) {
        int dilithiumCTilde = mldsaEngine.getDilithiumCTilde();
        for (int i = 0; i < mldsaEngine.getDilithiumL(); ++i) {
            polyVecL.getVectorIndex(i).zPack(array, dilithiumCTilde);
            dilithiumCTilde += mldsaEngine.getDilithiumPolyZPackedBytes();
        }
        for (int j = 0; j < mldsaEngine.getDilithiumOmega() + mldsaEngine.getDilithiumK(); ++j) {
            array[dilithiumCTilde + j] = 0;
        }
        int n = 0;
        for (int k = 0; k < mldsaEngine.getDilithiumK(); ++k) {
            for (int l = 0; l < 256; ++l) {
                if (polyVecK.getVectorIndex(k).getCoeffIndex(l) != 0) {
                    array[dilithiumCTilde + n++] = (byte)l;
                }
            }
            array[dilithiumCTilde + mldsaEngine.getDilithiumOmega() + k] = (byte)n;
        }
    }
    
    static boolean unpackSignature(final PolyVecL polyVecL, final PolyVecK polyVecK, final byte[] array, final MLDSAEngine mldsaEngine) {
        final int dilithiumCTilde = mldsaEngine.getDilithiumCTilde();
        for (int i = 0; i < mldsaEngine.getDilithiumL(); ++i) {
            polyVecL.getVectorIndex(i).zUnpack(Arrays.copyOfRange(array, dilithiumCTilde + i * mldsaEngine.getDilithiumPolyZPackedBytes(), dilithiumCTilde + (i + 1) * mldsaEngine.getDilithiumPolyZPackedBytes()));
        }
        final int n = dilithiumCTilde + mldsaEngine.getDilithiumL() * mldsaEngine.getDilithiumPolyZPackedBytes();
        int n2 = 0;
        for (int j = 0; j < mldsaEngine.getDilithiumK(); ++j) {
            for (int k = 0; k < 256; ++k) {
                polyVecK.getVectorIndex(j).setCoeffIndex(k, 0);
            }
            if ((array[n + mldsaEngine.getDilithiumOmega() + j] & 0xFF) < n2 || (array[n + mldsaEngine.getDilithiumOmega() + j] & 0xFF) > mldsaEngine.getDilithiumOmega()) {
                return false;
            }
            for (int l = n2; l < (array[n + mldsaEngine.getDilithiumOmega() + j] & 0xFF); ++l) {
                if (l > n2 && (array[n + l] & 0xFF) <= (array[n + l - 1] & 0xFF)) {
                    return false;
                }
                polyVecK.getVectorIndex(j).setCoeffIndex(array[n + l] & 0xFF, 1);
            }
            n2 = array[n + mldsaEngine.getDilithiumOmega() + j];
        }
        for (int n3 = n2; n3 < mldsaEngine.getDilithiumOmega(); ++n3) {
            if ((array[n + n3] & 0xFF) != 0x0) {
                return false;
            }
        }
        return true;
    }
}
