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

package org.bouncycastle.pqc.crypto.mlkem;

import org.bouncycastle.util.Arrays;

public class MLKEMPrivateKeyParameters extends MLKEMKeyParameters
{
    public static final int BOTH = 0;
    public static final int SEED_ONLY = 1;
    public static final int EXPANDED_KEY = 2;
    final byte[] s;
    final byte[] hpk;
    final byte[] nonce;
    final byte[] t;
    final byte[] rho;
    final byte[] seed;
    private final int prefFormat;
    
    public MLKEMPrivateKeyParameters(final MLKEMParameters mlkemParameters, final byte[] array, final byte[] array2, final byte[] array3, final byte[] array4, final byte[] array5) {
        this(mlkemParameters, array, array2, array3, array4, array5, null);
    }
    
    public MLKEMPrivateKeyParameters(final MLKEMParameters mlkemParameters, final byte[] array, final byte[] array2, final byte[] array3, final byte[] array4, final byte[] array5, final byte[] array6) {
        super(true, mlkemParameters);
        this.s = Arrays.clone(array);
        this.hpk = Arrays.clone(array2);
        this.nonce = Arrays.clone(array3);
        this.t = Arrays.clone(array4);
        this.rho = Arrays.clone(array5);
        this.seed = Arrays.clone(array6);
        this.prefFormat = 0;
    }
    
    public MLKEMPrivateKeyParameters(final MLKEMParameters mlkemParameters, final byte[] array) {
        this(mlkemParameters, array, null);
    }
    
    public MLKEMPrivateKeyParameters(final MLKEMParameters mlkemParameters, final byte[] array, final MLKEMPublicKeyParameters mlkemPublicKeyParameters) {
        super(true, mlkemParameters);
        final MLKEMEngine engine = mlkemParameters.getEngine();
        if (array.length == 64) {
            final byte[][] generateKemKeyPairInternal = engine.generateKemKeyPairInternal(Arrays.copyOfRange(array, 0, 32), Arrays.copyOfRange(array, 32, array.length));
            this.s = generateKemKeyPairInternal[2];
            this.hpk = generateKemKeyPairInternal[3];
            this.nonce = generateKemKeyPairInternal[4];
            this.t = generateKemKeyPairInternal[0];
            this.rho = generateKemKeyPairInternal[1];
            this.seed = generateKemKeyPairInternal[5];
        }
        else {
            final int n = 0;
            this.s = Arrays.copyOfRange(array, 0, engine.getKyberIndCpaSecretKeyBytes());
            final int n2 = n + engine.getKyberIndCpaSecretKeyBytes();
            this.t = Arrays.copyOfRange(array, n2, n2 + engine.getKyberIndCpaPublicKeyBytes() - 32);
            int n3 = n2 + (engine.getKyberIndCpaPublicKeyBytes() - 32);
            this.rho = Arrays.copyOfRange(array, n3, n3 + 32);
            n3 += 32;
            this.hpk = Arrays.copyOfRange(array, n3, n3 + 32);
            n3 += 32;
            this.nonce = Arrays.copyOfRange(array, n3, n3 + 32);
            this.seed = null;
        }
        if (mlkemPublicKeyParameters != null && (!Arrays.constantTimeAreEqual(this.t, mlkemPublicKeyParameters.t) || !Arrays.constantTimeAreEqual(this.rho, mlkemPublicKeyParameters.rho))) {
            throw new IllegalArgumentException("passed in public key does not match private values");
        }
        this.prefFormat = ((this.seed == null) ? 2 : 0);
    }
    
    private MLKEMPrivateKeyParameters(final MLKEMPrivateKeyParameters mlkemPrivateKeyParameters, final int prefFormat) {
        super(true, mlkemPrivateKeyParameters.getParameters());
        this.s = mlkemPrivateKeyParameters.s;
        this.t = mlkemPrivateKeyParameters.t;
        this.rho = mlkemPrivateKeyParameters.rho;
        this.hpk = mlkemPrivateKeyParameters.hpk;
        this.nonce = mlkemPrivateKeyParameters.nonce;
        this.seed = mlkemPrivateKeyParameters.seed;
        this.prefFormat = prefFormat;
    }
    
    public MLKEMPrivateKeyParameters getParametersWithFormat(final int n) {
        if (this.prefFormat == n) {
            return this;
        }
        switch (n) {
            case 0:
            case 1: {
                if (this.seed == null) {
                    throw new IllegalStateException("no seed available");
                }
                break;
            }
            case 2: {
                break;
            }
            default: {
                throw new IllegalArgumentException("unknown format");
            }
        }
        return new MLKEMPrivateKeyParameters(this, n);
    }
    
    public int getPreferredFormat() {
        return this.prefFormat;
    }
    
    public byte[] getEncoded() {
        return Arrays.concatenate(new byte[][] { this.s, this.t, this.rho, this.hpk, this.nonce });
    }
    
    public byte[] getHPK() {
        return Arrays.clone(this.hpk);
    }
    
    public byte[] getNonce() {
        return Arrays.clone(this.nonce);
    }
    
    public byte[] getPublicKey() {
        return MLKEMPublicKeyParameters.getEncoded(this.t, this.rho);
    }
    
    public MLKEMPublicKeyParameters getPublicKeyParameters() {
        return new MLKEMPublicKeyParameters(this.getParameters(), this.t, this.rho);
    }
    
    public byte[] getRho() {
        return Arrays.clone(this.rho);
    }
    
    public byte[] getS() {
        return Arrays.clone(this.s);
    }
    
    public byte[] getT() {
        return Arrays.clone(this.t);
    }
    
    public byte[] getSeed() {
        return Arrays.clone(this.seed);
    }
}
