package org.bouncycastle.pqc.crypto.mldsa;

import java.security.SecureRandom;
import org.bouncycastle.asn1.cmp.PKIFailureInfo;
import org.bouncycastle.crypto.digests.SHAKEDigest;
import org.bouncycastle.util.Arrays;

/* JADX INFO: loaded from: classes.dex */
class MLDSAEngine {
    public static final int CrhBytes = 64;
    public static final int DilithiumD = 13;
    public static final int DilithiumN = 256;
    public static final int DilithiumPolyT0PackedBytes = 416;
    public static final int DilithiumPolyT1PackedBytes = 320;
    public static final int DilithiumQ = 8380417;
    public static final int DilithiumQinv = 58728449;
    public static final int RndBytes = 32;
    public static final int SeedBytes = 32;
    public static final int TrBytes = 64;
    private final int CryptoBytes;
    private final int CryptoPublicKeyBytes;
    private final int DilithiumBeta;
    private final int DilithiumCTilde;
    private final int DilithiumEta;
    private final int DilithiumGamma1;
    private final int DilithiumGamma2;
    private final int DilithiumK;
    private final int DilithiumL;
    private final int DilithiumOmega;
    private final int DilithiumPolyEtaPackedBytes;
    private final int DilithiumPolyVecHPackedBytes;
    private final int DilithiumPolyW1PackedBytes;
    private final int DilithiumPolyZPackedBytes;
    private final int DilithiumTau;
    private final int PolyUniformGamma1NBlocks;
    private final SecureRandom random;
    final SHAKEDigest shake256Digest = new SHAKEDigest(256);
    private final Symmetric symmetric;

    /* JADX WARN: Code duplicated, block: B:12:0x00d1  */
    /* JADX WARN: Code duplicated, block: B:14:0x00e0  */
    /* JADX WARN: Code duplicated, block: B:16:0x00e4  */
    /* JADX WARN: Code duplicated, block: B:18:0x00eb  */
    MLDSAEngine(int i, SecureRandom secureRandom) {
        int i2;
        int i3;
        switch (i) {
            case 2:
                this.DilithiumK = 4;
                this.DilithiumL = 4;
                this.DilithiumEta = 2;
                this.DilithiumTau = 39;
                this.DilithiumBeta = 78;
                this.DilithiumGamma1 = PKIFailureInfo.unsupportedVersion;
                this.DilithiumGamma2 = 95232;
                this.DilithiumOmega = 80;
                this.DilithiumPolyZPackedBytes = 576;
                this.DilithiumPolyW1PackedBytes = 192;
                this.DilithiumPolyEtaPackedBytes = 96;
                this.DilithiumCTilde = 32;
                this.symmetric = new Symmetric.ShakeSymmetric();
                this.random = secureRandom;
                this.DilithiumPolyVecHPackedBytes = this.DilithiumOmega + this.DilithiumK;
                this.CryptoPublicKeyBytes = (this.DilithiumK * 320) + 32;
                this.CryptoBytes = this.DilithiumCTilde + (this.DilithiumL * this.DilithiumPolyZPackedBytes) + this.DilithiumPolyVecHPackedBytes;
                if (this.DilithiumGamma1 == 131072) {
                    i3 = this.symmetric.stream256BlockBytes + 576;
                } else {
                    if (this.DilithiumGamma1 != 524288) {
                        throw new RuntimeException("Wrong Dilithium Gamma1!");
                    }
                    i3 = this.symmetric.stream256BlockBytes + 640;
                }
                this.PolyUniformGamma1NBlocks = (i3 - 1) / this.symmetric.stream256BlockBytes;
                return;
            case 3:
                this.DilithiumK = 6;
                this.DilithiumL = 5;
                this.DilithiumEta = 4;
                this.DilithiumTau = 49;
                this.DilithiumBeta = 196;
                this.DilithiumGamma1 = PKIFailureInfo.signerNotTrusted;
                this.DilithiumGamma2 = 261888;
                this.DilithiumOmega = 55;
                this.DilithiumPolyZPackedBytes = 640;
                this.DilithiumPolyW1PackedBytes = 128;
                this.DilithiumPolyEtaPackedBytes = 128;
                i2 = 48;
                this.DilithiumCTilde = i2;
                this.symmetric = new Symmetric.ShakeSymmetric();
                this.random = secureRandom;
                this.DilithiumPolyVecHPackedBytes = this.DilithiumOmega + this.DilithiumK;
                this.CryptoPublicKeyBytes = (this.DilithiumK * 320) + 32;
                this.CryptoBytes = this.DilithiumCTilde + (this.DilithiumL * this.DilithiumPolyZPackedBytes) + this.DilithiumPolyVecHPackedBytes;
                if (this.DilithiumGamma1 == 131072) {
                    i3 = this.symmetric.stream256BlockBytes + 576;
                } else {
                    if (this.DilithiumGamma1 != 524288) {
                        throw new RuntimeException("Wrong Dilithium Gamma1!");
                    }
                    i3 = this.symmetric.stream256BlockBytes + 640;
                }
                this.PolyUniformGamma1NBlocks = (i3 - 1) / this.symmetric.stream256BlockBytes;
                return;
            case 4:
            default:
                throw new IllegalArgumentException("The mode " + i + "is not supported by Crystals Dilithium!");
            case 5:
                this.DilithiumK = 8;
                this.DilithiumL = 7;
                this.DilithiumEta = 2;
                this.DilithiumTau = 60;
                this.DilithiumBeta = 120;
                this.DilithiumGamma1 = PKIFailureInfo.signerNotTrusted;
                this.DilithiumGamma2 = 261888;
                this.DilithiumOmega = 75;
                this.DilithiumPolyZPackedBytes = 640;
                this.DilithiumPolyW1PackedBytes = 128;
                this.DilithiumPolyEtaPackedBytes = 96;
                i2 = 64;
                this.DilithiumCTilde = i2;
                this.symmetric = new Symmetric.ShakeSymmetric();
                this.random = secureRandom;
                this.DilithiumPolyVecHPackedBytes = this.DilithiumOmega + this.DilithiumK;
                this.CryptoPublicKeyBytes = (this.DilithiumK * 320) + 32;
                this.CryptoBytes = this.DilithiumCTilde + (this.DilithiumL * this.DilithiumPolyZPackedBytes) + this.DilithiumPolyVecHPackedBytes;
                if (this.DilithiumGamma1 == 131072) {
                    i3 = this.symmetric.stream256BlockBytes + 576;
                } else {
                    if (this.DilithiumGamma1 != 524288) {
                        throw new RuntimeException("Wrong Dilithium Gamma1!");
                    }
                    i3 = this.symmetric.stream256BlockBytes + 640;
                }
                this.PolyUniformGamma1NBlocks = (i3 - 1) / this.symmetric.stream256BlockBytes;
                return;
        }
    }

    private boolean doVerifyInternal(byte[] bArr, byte[] bArr2, int i, SHAKEDigest sHAKEDigest, byte[] bArr3, byte[] bArr4) {
        if (i != this.CryptoBytes) {
            return false;
        }
        PolyVecK polyVecK = new PolyVecK(this);
        PolyVecL polyVecL = new PolyVecL(this);
        if (!Packing.unpackSignature(polyVecL, polyVecK, bArr2, this) || polyVecL.checkNorm(getDilithiumGamma1() - getDilithiumBeta())) {
            return false;
        }
        Poly poly = new Poly(this);
        PolyVecMatrix polyVecMatrix = new PolyVecMatrix(this);
        PolyVecK polyVecK2 = new PolyVecK(this);
        PolyVecK polyVecK3 = new PolyVecK(this);
        PolyVecK polyVecKUnpackPublicKey = Packing.unpackPublicKey(polyVecK2, bArr4, this);
        poly.challenge(bArr2, 0, this.DilithiumCTilde);
        polyVecMatrix.expandMatrix(bArr3);
        polyVecL.polyVecNtt();
        polyVecMatrix.pointwiseMontgomery(polyVecK3, polyVecL);
        poly.polyNtt();
        polyVecKUnpackPublicKey.shiftLeft();
        polyVecKUnpackPublicKey.polyVecNtt();
        polyVecKUnpackPublicKey.pointwisePolyMontgomery(poly, polyVecKUnpackPublicKey);
        polyVecK3.subtract(polyVecKUnpackPublicKey);
        polyVecK3.reduce();
        polyVecK3.invNttToMont();
        polyVecK3.conditionalAddQ();
        polyVecK3.useHint(polyVecK3, polyVecK);
        polyVecK3.packW1(this, bArr, 64);
        sHAKEDigest.update(bArr, 0, (this.DilithiumK * this.DilithiumPolyW1PackedBytes) + 64);
        sHAKEDigest.doFinal(bArr, 0, this.DilithiumCTilde);
        return Arrays.constantTimeAreEqual(this.DilithiumCTilde, bArr2, 0, bArr, 0);
    }

    protected Symmetric GetSymmetric() {
        return this.symmetric;
    }

    void absorbCtx(boolean z, byte[] bArr) {
        if (bArr != null) {
            this.shake256Digest.update(z ? (byte) 1 : (byte) 0);
            this.shake256Digest.update((byte) bArr.length);
            this.shake256Digest.update(bArr, 0, bArr.length);
        }
    }

    byte[] deriveT1(byte[] bArr, byte[] bArr2, byte[] bArr3, byte[] bArr4, byte[] bArr5, byte[] bArr6) {
        PolyVecMatrix polyVecMatrix = new PolyVecMatrix(this);
        PolyVecL polyVecL = new PolyVecL(this);
        PolyVecK polyVecK = new PolyVecK(this);
        PolyVecK polyVecK2 = new PolyVecK(this);
        PolyVecK polyVecK3 = new PolyVecK(this);
        Packing.unpackSecretKey(polyVecK3, polyVecL, polyVecK, bArr6, bArr4, bArr5, this);
        polyVecMatrix.expandMatrix(bArr);
        PolyVecL polyVecL2 = new PolyVecL(this);
        polyVecL.copyTo(polyVecL2);
        polyVecL2.polyVecNtt();
        polyVecMatrix.pointwiseMontgomery(polyVecK2, polyVecL2);
        polyVecK2.reduce();
        polyVecK2.invNttToMont();
        polyVecK2.addPolyVecK(polyVecK);
        polyVecK2.conditionalAddQ();
        polyVecK2.power2Round(polyVecK3);
        return Packing.packPublicKey(polyVecK2, this);
    }

    byte[][] generateKeyPair() {
        byte[] bArr = new byte[32];
        this.random.nextBytes(bArr);
        return generateKeyPairInternal(bArr);
    }

    byte[][] generateKeyPairInternal(byte[] bArr) {
        byte[] bArr2 = new byte[128];
        byte[] bArr3 = new byte[64];
        byte[] bArr4 = new byte[32];
        byte[] bArr5 = new byte[64];
        byte[] bArr6 = new byte[32];
        PolyVecMatrix polyVecMatrix = new PolyVecMatrix(this);
        PolyVecL polyVecL = new PolyVecL(this);
        PolyVecK polyVecK = new PolyVecK(this);
        PolyVecK polyVecK2 = new PolyVecK(this);
        PolyVecK polyVecK3 = new PolyVecK(this);
        this.shake256Digest.update(bArr, 0, 32);
        this.shake256Digest.update((byte) this.DilithiumK);
        this.shake256Digest.update((byte) this.DilithiumL);
        this.shake256Digest.doFinal(bArr2, 0, 128);
        System.arraycopy(bArr2, 0, bArr4, 0, 32);
        System.arraycopy(bArr2, 32, bArr5, 0, 64);
        System.arraycopy(bArr2, 96, bArr6, 0, 32);
        polyVecMatrix.expandMatrix(bArr4);
        polyVecL.uniformEta(bArr5, (short) 0);
        polyVecK.uniformEta(bArr5, (short) this.DilithiumL);
        PolyVecL polyVecL2 = new PolyVecL(this);
        polyVecL.copyTo(polyVecL2);
        polyVecL2.polyVecNtt();
        polyVecMatrix.pointwiseMontgomery(polyVecK2, polyVecL2);
        polyVecK2.reduce();
        polyVecK2.invNttToMont();
        polyVecK2.addPolyVecK(polyVecK);
        polyVecK2.conditionalAddQ();
        polyVecK2.power2Round(polyVecK3);
        byte[] bArrPackPublicKey = Packing.packPublicKey(polyVecK2, this);
        this.shake256Digest.update(bArr4, 0, 32);
        this.shake256Digest.update(bArrPackPublicKey, 0, bArrPackPublicKey.length);
        this.shake256Digest.doFinal(bArr3, 0, 64);
        byte[][] bArrPackSecretKey = Packing.packSecretKey(bArr4, bArr3, bArr6, polyVecK3, polyVecL, polyVecK, this);
        return new byte[][]{bArrPackSecretKey[0], bArrPackSecretKey[1], bArrPackSecretKey[2], bArrPackSecretKey[3], bArrPackSecretKey[4], bArrPackSecretKey[5], bArrPackPublicKey, bArr};
    }

    byte[] generateMu(SHAKEDigest sHAKEDigest) {
        byte[] bArr = new byte[64];
        sHAKEDigest.doFinal(bArr, 0, 64);
        return bArr;
    }

    byte[] generateSignature(byte[] bArr, SHAKEDigest sHAKEDigest, byte[] bArr2, byte[] bArr3, byte[] bArr4, byte[] bArr5, byte[] bArr6, byte[] bArr7) {
        PolyVecK polyVecK;
        PolyVecL polyVecL;
        byte[] bArr8 = new byte[this.CryptoBytes];
        byte[] bArr9 = new byte[64];
        PolyVecL polyVecL2 = new PolyVecL(this);
        PolyVecL polyVecL3 = new PolyVecL(this);
        PolyVecL polyVecL4 = new PolyVecL(this);
        PolyVecK polyVecK2 = new PolyVecK(this);
        PolyVecK polyVecK3 = new PolyVecK(this);
        PolyVecK polyVecK4 = new PolyVecK(this);
        PolyVecK polyVecK5 = new PolyVecK(this);
        PolyVecK polyVecK6 = new PolyVecK(this);
        Poly poly = new Poly(this);
        PolyVecMatrix polyVecMatrix = new PolyVecMatrix(this);
        PolyVecK polyVecK7 = polyVecK6;
        Poly poly2 = poly;
        Packing.unpackSecretKey(polyVecK2, polyVecL2, polyVecK3, bArr4, bArr5, bArr6, this);
        byte[] bArrCopyOf = Arrays.copyOf(bArr3, 128);
        int i = 0;
        PolyVecK polyVecK8 = polyVecK2;
        System.arraycopy(bArr7, 0, bArrCopyOf, 32, 32);
        System.arraycopy(bArr, 0, bArrCopyOf, 64, 64);
        sHAKEDigest.update(bArrCopyOf, 0, 128);
        sHAKEDigest.doFinal(bArr9, 0, 64);
        polyVecMatrix.expandMatrix(bArr2);
        polyVecL2.polyVecNtt();
        polyVecK3.polyVecNtt();
        polyVecK8.polyVecNtt();
        int i2 = 0;
        short s = 0;
        while (i2 < 1000) {
            int i3 = i2 + 1;
            short s2 = (short) (s + 1);
            polyVecL3.uniformGamma1(bArr9, s);
            polyVecL3.copyTo(polyVecL4);
            polyVecL4.polyVecNtt();
            polyVecMatrix.pointwiseMontgomery(polyVecK4, polyVecL4);
            polyVecK4.reduce();
            polyVecK4.invNttToMont();
            polyVecK4.conditionalAddQ();
            polyVecK4.decompose(polyVecK5);
            polyVecK4.packW1(this, bArr8, i);
            sHAKEDigest.update(bArr, i, 64);
            sHAKEDigest.update(bArr8, i, this.DilithiumK * this.DilithiumPolyW1PackedBytes);
            sHAKEDigest.doFinal(bArr8, i, this.DilithiumCTilde);
            Poly poly3 = poly2;
            poly3.challenge(bArr8, i, this.DilithiumCTilde);
            poly3.polyNtt();
            polyVecL4.pointwisePolyMontgomery(poly3, polyVecL2);
            polyVecL4.invNttToMont();
            polyVecL4.addPolyVecL(polyVecL3);
            polyVecL4.reduce();
            if (polyVecL4.checkNorm(this.DilithiumGamma1 - this.DilithiumBeta)) {
                polyVecL = polyVecL2;
                polyVecK = polyVecK7;
            } else {
                polyVecK = polyVecK7;
                polyVecK.pointwisePolyMontgomery(poly3, polyVecK3);
                polyVecK.invNttToMont();
                polyVecK5.subtract(polyVecK);
                polyVecK5.reduce();
                polyVecL = polyVecL2;
                if (polyVecK5.checkNorm(this.DilithiumGamma2 - this.DilithiumBeta)) {
                    continue;
                } else {
                    PolyVecK polyVecK9 = polyVecK8;
                    polyVecK.pointwisePolyMontgomery(poly3, polyVecK9);
                    polyVecK.invNttToMont();
                    polyVecK.reduce();
                    if (polyVecK.checkNorm(this.DilithiumGamma2)) {
                        polyVecK8 = polyVecK9;
                    } else {
                        polyVecK5.addPolyVecK(polyVecK);
                        polyVecK5.conditionalAddQ();
                        polyVecK8 = polyVecK9;
                        if (polyVecK.makeHint(polyVecK5, polyVecK4) <= this.DilithiumOmega) {
                            Packing.packSignature(bArr8, polyVecL4, polyVecK, this);
                            return bArr8;
                        }
                    }
                }
            }
            polyVecL2 = polyVecL;
            polyVecK7 = polyVecK;
            poly2 = poly3;
            s = s2;
            i = 0;
            i2 = i3;
        }
        return null;
    }

    int getCryptoPublicKeyBytes() {
        return this.CryptoPublicKeyBytes;
    }

    int getDilithiumBeta() {
        return this.DilithiumBeta;
    }

    int getDilithiumCTilde() {
        return this.DilithiumCTilde;
    }

    int getDilithiumEta() {
        return this.DilithiumEta;
    }

    int getDilithiumGamma1() {
        return this.DilithiumGamma1;
    }

    int getDilithiumGamma2() {
        return this.DilithiumGamma2;
    }

    int getDilithiumK() {
        return this.DilithiumK;
    }

    int getDilithiumL() {
        return this.DilithiumL;
    }

    int getDilithiumOmega() {
        return this.DilithiumOmega;
    }

    int getDilithiumPolyEtaPackedBytes() {
        return this.DilithiumPolyEtaPackedBytes;
    }

    int getDilithiumPolyW1PackedBytes() {
        return this.DilithiumPolyW1PackedBytes;
    }

    int getDilithiumPolyZPackedBytes() {
        return this.DilithiumPolyZPackedBytes;
    }

    int getDilithiumTau() {
        return this.DilithiumTau;
    }

    int getPolyUniformGamma1NBlocks() {
        return this.PolyUniformGamma1NBlocks;
    }

    SHAKEDigest getShake256Digest() {
        return new SHAKEDigest(this.shake256Digest);
    }

    void initSign(byte[] bArr, boolean z, byte[] bArr2) {
        this.shake256Digest.update(bArr, 0, 64);
        absorbCtx(z, bArr2);
    }

    void initVerify(byte[] bArr, byte[] bArr2, boolean z, byte[] bArr3) {
        byte[] bArr4 = new byte[64];
        this.shake256Digest.update(bArr, 0, bArr.length);
        this.shake256Digest.update(bArr2, 0, bArr2.length);
        this.shake256Digest.doFinal(bArr4, 0, 64);
        this.shake256Digest.update(bArr4, 0, 64);
        absorbCtx(z, bArr3);
    }

    byte[] signInternal(byte[] bArr, int i, byte[] bArr2, byte[] bArr3, byte[] bArr4, byte[] bArr5, byte[] bArr6, byte[] bArr7) {
        SHAKEDigest sHAKEDigest = new SHAKEDigest(this.shake256Digest);
        sHAKEDigest.update(bArr, 0, i);
        return generateSignature(generateMu(sHAKEDigest), sHAKEDigest, bArr2, bArr3, bArr4, bArr5, bArr6, bArr7);
    }

    boolean verifyInternal(byte[] bArr, int i, SHAKEDigest sHAKEDigest, byte[] bArr2, byte[] bArr3) {
        byte[] bArr4 = new byte[Math.max((this.DilithiumK * this.DilithiumPolyW1PackedBytes) + 64, this.DilithiumCTilde)];
        sHAKEDigest.doFinal(bArr4, 0);
        return doVerifyInternal(bArr4, bArr, i, sHAKEDigest, bArr2, bArr3);
    }

    boolean verifyInternalMu(byte[] bArr) {
        byte[] bArr2 = new byte[64];
        this.shake256Digest.doFinal(bArr2, 0);
        return Arrays.constantTimeAreEqual(bArr2, bArr);
    }

    boolean verifyInternalMuSignature(byte[] bArr, byte[] bArr2, int i, SHAKEDigest sHAKEDigest, byte[] bArr3, byte[] bArr4) {
        byte[] bArr5 = new byte[Math.max((this.DilithiumK * this.DilithiumPolyW1PackedBytes) + 64, this.DilithiumCTilde)];
        System.arraycopy(bArr, 0, bArr5, 0, bArr.length);
        return doVerifyInternal(bArr5, bArr2, i, sHAKEDigest, bArr3, bArr4);
    }
}
