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

package org.bouncycastle.pqc.crypto.crystals.dilithium;

import org.bouncycastle.util.Arrays;

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