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

package org.bouncycastle.pqc.crypto.ntruprime;

import org.bouncycastle.pqc.crypto.util.SecretWithEncapsulationImpl;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.SecretWithEncapsulation;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import java.security.SecureRandom;
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;

public class NTRULPRimeKEMGenerator implements EncapsulatedSecretGenerator
{
    private final SecureRandom random;
    
    public NTRULPRimeKEMGenerator(final SecureRandom random) {
        this.random = random;
    }
    
    @Override
    public SecretWithEncapsulation generateEncapsulated(final AsymmetricKeyParameter asymmetricKeyParameter) {
        final NTRULPRimePublicKeyParameters ntrulpRimePublicKeyParameters = (NTRULPRimePublicKeyParameters)asymmetricKeyParameter;
        final NTRULPRimeParameters parameters = ntrulpRimePublicKeyParameters.getParameters();
        final int p = parameters.getP();
        final int q = parameters.getQ();
        final int w = parameters.getW();
        final int roundedPolynomialBytes = parameters.getRoundedPolynomialBytes();
        final int tau0 = parameters.getTau0();
        final int tau2 = parameters.getTau1();
        final byte[] hashWithPrefix = Utils.getHashWithPrefix(new byte[] { 4 }, ntrulpRimePublicKeyParameters.getEncoded());
        final byte[] array = new byte[256];
        Utils.getRandomInputs(this.random, array);
        final byte[] array2 = new byte[32];
        Utils.getEncodedInputs(array2, array);
        final short[] array3 = new short[p];
        Utils.getRoundedDecodedPolynomial(array3, ntrulpRimePublicKeyParameters.getRoundEncA(), p, q);
        final short[] array4 = new short[p];
        Utils.generatePolynomialInRQFromSeed(array4, ntrulpRimePublicKeyParameters.getSeed(), p, q);
        final byte[] hashWithPrefix2 = Utils.getHashWithPrefix(new byte[] { 5 }, array2);
        final byte[] copyOfRange = Arrays.copyOfRange(hashWithPrefix2, 0, hashWithPrefix2.length / 2);
        final int[] array5 = new int[p];
        Utils.expand(array5, copyOfRange);
        final byte[] array6 = new byte[p];
        Utils.sortGenerateShortPolynomial(array6, array5, p, w);
        final short[] array7 = new short[p];
        Utils.multiplicationInRQ(array7, array4, array6, p, q);
        final short[] array8 = new short[p];
        Utils.roundPolynomial(array8, array7);
        final byte[] array9 = new byte[roundedPolynomialBytes];
        Utils.getRoundedEncodedPolynomial(array9, array8, p, q);
        final short[] array10 = new short[p];
        Utils.multiplicationInRQ(array10, array3, array6, p, q);
        final byte[] array11 = new byte[256];
        Utils.top(array11, array10, array, q, tau0, tau2);
        final byte[] array12 = new byte[128];
        Utils.getTopEncodedPolynomial(array12, array11);
        final byte[] array13 = new byte[array2.length + hashWithPrefix.length / 2];
        System.arraycopy(array2, 0, array13, 0, array2.length);
        System.arraycopy(hashWithPrefix, 0, array13, array2.length, hashWithPrefix.length / 2);
        final byte[] hashWithPrefix3 = Utils.getHashWithPrefix(new byte[] { 2 }, array13);
        final byte[] array14 = new byte[array9.length + array12.length + hashWithPrefix3.length / 2];
        System.arraycopy(array9, 0, array14, 0, array9.length);
        System.arraycopy(array12, 0, array14, array9.length, array12.length);
        System.arraycopy(hashWithPrefix3, 0, array14, array9.length + array12.length, hashWithPrefix3.length / 2);
        final byte[] array15 = new byte[array2.length + array14.length];
        System.arraycopy(array2, 0, array15, 0, array2.length);
        System.arraycopy(array14, 0, array15, array2.length, array14.length);
        return new SecretWithEncapsulationImpl(Arrays.copyOfRange(Utils.getHashWithPrefix(new byte[] { 1 }, array15), 0, parameters.getSessionKeySize() / 8), array14);
    }
}
