// 
// 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 SNTRUPrimeKeyPairGenerator implements AsymmetricCipherKeyPairGenerator
{
    private SNTRUPrimeKeyGenerationParameters params;
    
    public SNTRUPrimeKeyGenerationParameters getParams() {
        return this.params;
    }
    
    @Override
    public void init(final KeyGenerationParameters keyGenerationParameters) {
        this.params = (SNTRUPrimeKeyGenerationParameters)keyGenerationParameters;
    }
    
    @Override
    public AsymmetricCipherKeyPair generateKeyPair() {
        final int p = this.params.getSntrupParams().getP();
        final int q = this.params.getSntrupParams().getQ();
        final int w = this.params.getSntrupParams().getW();
        final byte[] array = new byte[p];
        final byte[] array2 = new byte[p];
        do {
            Utils.getRandomSmallPolynomial(this.params.getRandom(), array);
        } while (!Utils.isInvertiblePolynomialInR3(array, array2, p));
        final byte[] array3 = new byte[p];
        Utils.getRandomShortPolynomial(this.params.getRandom(), array3, p, w);
        final short[] array4 = new short[p];
        Utils.getOneThirdInverseInRQ(array4, array3, p, q);
        final short[] array5 = new short[p];
        Utils.multiplicationInRQ(array5, array4, array, p, q);
        final byte[] array6 = new byte[this.params.getSntrupParams().getPublicKeyBytes()];
        Utils.getEncodedPolynomial(array6, array5, p, q);
        final SNTRUPrimePublicKeyParameters sntruPrimePublicKeyParameters = new SNTRUPrimePublicKeyParameters(this.params.getSntrupParams(), array6);
        final byte[] array7 = new byte[(p + 3) / 4];
        Utils.getEncodedSmallPolynomial(array7, array3, p);
        final byte[] array8 = new byte[(p + 3) / 4];
        Utils.getEncodedSmallPolynomial(array8, array2, p);
        final byte[] bytes = new byte[(p + 3) / 4];
        this.params.getRandom().nextBytes(bytes);
        final byte[] hashWithPrefix = Utils.getHashWithPrefix(new byte[] { 4 }, array6);
        return new AsymmetricCipherKeyPair(sntruPrimePublicKeyParameters, new SNTRUPrimePrivateKeyParameters(this.params.getSntrupParams(), array7, array8, array6, bytes, Arrays.copyOfRange(hashWithPrefix, 0, hashWithPrefix.length / 2)));
    }
}
