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

package org.bouncycastle.pqc.crypto.snova;

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

public class SnovaKeyPairGenerator implements AsymmetricCipherKeyPairGenerator
{
    private SnovaEngine engine;
    private static final int seedLength = 48;
    static final int publicSeedLength = 16;
    static final int privateSeedLength = 32;
    private SnovaParameters params;
    private SecureRandom random;
    private boolean initialized;
    
    @Override
    public void init(final KeyGenerationParameters keyGenerationParameters) {
        final SnovaKeyGenerationParameters snovaKeyGenerationParameters = (SnovaKeyGenerationParameters)keyGenerationParameters;
        this.params = snovaKeyGenerationParameters.getParameters();
        this.random = snovaKeyGenerationParameters.getRandom();
        this.initialized = true;
        this.engine = new SnovaEngine(this.params);
    }
    
    @Override
    public AsymmetricCipherKeyPair generateKeyPair() {
        if (!this.initialized) {
            throw new IllegalStateException("SNOVA key pair generator not initialized");
        }
        final byte[] bytes = new byte[48];
        this.random.nextBytes(bytes);
        final byte[] array = new byte[this.params.getPublicKeyLength()];
        byte[] array2 = new byte[this.params.getPrivateKeyLength()];
        final byte[] copyOfRange = Arrays.copyOfRange(bytes, 0, 16);
        final byte[] copyOfRange2 = Arrays.copyOfRange(bytes, 16, bytes.length);
        final SnovaKeyElements snovaKeyElements = new SnovaKeyElements(this.params);
        System.arraycopy(copyOfRange, 0, array, 0, copyOfRange.length);
        this.engine.genMap1T12Map2(snovaKeyElements, copyOfRange, copyOfRange2);
        this.engine.genP22(array, copyOfRange.length, snovaKeyElements.T12, snovaKeyElements.map1.p21, snovaKeyElements.map2.f12);
        System.arraycopy(copyOfRange, 0, array, 0, copyOfRange.length);
        if (this.params.isSkIsSeed()) {
            array2 = bytes;
        }
        else {
            final int o = this.params.getO();
            final int lsq = this.params.getLsq();
            final int v = this.params.getV();
            final int n = o * this.params.getAlpha() * lsq * 4 + v * o * lsq + (o * v * v + o * v * o + o * o * v) * lsq;
            final byte[] array3 = new byte[n];
            SnovaKeyElements.copy4d(snovaKeyElements.map2.f21, array3, SnovaKeyElements.copy4d(snovaKeyElements.map2.f12, array3, SnovaKeyElements.copy4d(snovaKeyElements.map2.f11, array3, SnovaKeyElements.copy3d(snovaKeyElements.T12, array3, SnovaKeyElements.copy3d(snovaKeyElements.map1.qAlpha2, array3, SnovaKeyElements.copy3d(snovaKeyElements.map1.qAlpha1, array3, SnovaKeyElements.copy3d(snovaKeyElements.map1.bAlpha, array3, SnovaKeyElements.copy3d(snovaKeyElements.map1.aAlpha, array3, 0))))))));
            GF16Utils.encodeMergeInHalf(array3, n, array2);
            System.arraycopy(bytes, 0, array2, array2.length - 48, 48);
        }
        return new AsymmetricCipherKeyPair(new SnovaPublicKeyParameters(this.params, array), new SnovaPrivateKeyParameters(this.params, array2));
    }
}
