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

package org.bouncycastle.pqc.crypto.ntruprime;

import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.AsymmetricCipherKeyPairGenerator;

public class NTRULPRimeKeyPairGenerator implements AsymmetricCipherKeyPairGenerator
{
    private NTRULPRimeKeyGenerationParameters params;
    
    public NTRULPRimeKeyGenerationParameters getParams() {
        return this.params;
    }
    
    @Override
    public void init(final KeyGenerationParameters keyGenerationParameters) {
        this.params = (NTRULPRimeKeyGenerationParameters)keyGenerationParameters;
    }
    
    @Override
    public AsymmetricCipherKeyPair generateKeyPair() {
        final int p = this.params.getNtrulprParams().getP();
        final int q = this.params.getNtrulprParams().getQ();
        final int w = this.params.getNtrulprParams().getW();
        final byte[] bytes = new byte[32];
        this.params.getRandom().nextBytes(bytes);
        final short[] array = new short[p];
        Utils.generatePolynomialInRQFromSeed(array, bytes, p, q);
        final byte[] array2 = new byte[p];
        Utils.getRandomShortPolynomial(this.params.getRandom(), array2, p, w);
        final short[] array3 = new short[p];
        Utils.multiplicationInRQ(array3, array, array2, p, q);
        final short[] array4 = new short[p];
        Utils.roundPolynomial(array4, array3);
        final byte[] array5 = new byte[this.params.getNtrulprParams().getPublicKeyBytes() - 32];
        Utils.getRoundedEncodedPolynomial(array5, array4, p, q);
        final NTRULPRimePublicKeyParameters ntrulpRimePublicKeyParameters = new NTRULPRimePublicKeyParameters(this.params.getNtrulprParams(), bytes, array5);
        final byte[] array6 = new byte[(p + 3) / 4];
        Utils.getEncodedSmallPolynomial(array6, array2, p);
        final byte[] bytes2 = new byte[32];
        this.params.getRandom().nextBytes(bytes2);
        final byte[] hashWithPrefix = Utils.getHashWithPrefix(new byte[] { 4 }, ntrulpRimePublicKeyParameters.getEncoded());
        return new AsymmetricCipherKeyPair(ntrulpRimePublicKeyParameters, new NTRULPRimePrivateKeyParameters(this.params.getNtrulprParams(), array6, ntrulpRimePublicKeyParameters.getEncoded(), bytes2, Arrays.copyOfRange(hashWithPrefix, 0, hashWithPrefix.length / 2)));
    }
}
