// 
// 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 SNTRUPrimeKEMGenerator implements EncapsulatedSecretGenerator
{
    private final SecureRandom random;
    
    public SNTRUPrimeKEMGenerator(final SecureRandom random) {
        this.random = random;
    }
    
    @Override
    public SecretWithEncapsulation generateEncapsulated(final AsymmetricKeyParameter asymmetricKeyParameter) {
        final SNTRUPrimePublicKeyParameters sntruPrimePublicKeyParameters = (SNTRUPrimePublicKeyParameters)asymmetricKeyParameter;
        final SNTRUPrimeParameters parameters = sntruPrimePublicKeyParameters.getParameters();
        final int p = parameters.getP();
        final int q = parameters.getQ();
        final int w = parameters.getW();
        final int roundedPolynomialBytes = parameters.getRoundedPolynomialBytes();
        final byte[] hashWithPrefix = Utils.getHashWithPrefix(new byte[] { 4 }, sntruPrimePublicKeyParameters.getEncoded());
        final byte[] array = new byte[p];
        Utils.getRandomShortPolynomial(this.random, array, p, w);
        final byte[] array2 = new byte[(p + 3) / 4];
        Utils.getEncodedSmallPolynomial(array2, array, p);
        final short[] array3 = new short[p];
        Utils.getDecodedPolynomial(array3, sntruPrimePublicKeyParameters.getEncH(), p, q);
        final short[] array4 = new short[p];
        Utils.multiplicationInRQ(array4, array3, array, p, q);
        final short[] array5 = new short[p];
        Utils.roundPolynomial(array5, array4);
        final byte[] array6 = new byte[roundedPolynomialBytes];
        Utils.getRoundedEncodedPolynomial(array6, array5, p, q);
        final byte[] hashWithPrefix2 = Utils.getHashWithPrefix(new byte[] { 3 }, array2);
        final byte[] array7 = new byte[hashWithPrefix2.length / 2 + hashWithPrefix.length / 2];
        System.arraycopy(hashWithPrefix2, 0, array7, 0, hashWithPrefix2.length / 2);
        System.arraycopy(hashWithPrefix, 0, array7, hashWithPrefix2.length / 2, hashWithPrefix.length / 2);
        final byte[] hashWithPrefix3 = Utils.getHashWithPrefix(new byte[] { 2 }, array7);
        final byte[] array8 = new byte[array6.length + hashWithPrefix3.length / 2];
        System.arraycopy(array6, 0, array8, 0, array6.length);
        System.arraycopy(hashWithPrefix3, 0, array8, array6.length, hashWithPrefix3.length / 2);
        final byte[] hashWithPrefix4 = Utils.getHashWithPrefix(new byte[] { 3 }, array2);
        final byte[] array9 = new byte[hashWithPrefix4.length / 2 + array8.length];
        System.arraycopy(hashWithPrefix4, 0, array9, 0, hashWithPrefix4.length / 2);
        System.arraycopy(array8, 0, array9, hashWithPrefix4.length / 2, array8.length);
        return new SecretWithEncapsulationImpl(Arrays.copyOfRange(Utils.getHashWithPrefix(new byte[] { 1 }, array9), 0, parameters.getSessionKeySize() / 8), array8);
    }
}
