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

package org.bouncycastle.crypto.signers;

import org.bouncycastle.crypto.params.GOST3410Parameters;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.Arrays;
import java.math.BigInteger;
import org.bouncycastle.crypto.params.GOST3410PublicKeyParameters;
import org.bouncycastle.crypto.CryptoServicesRegistrar;
import org.bouncycastle.crypto.params.GOST3410PrivateKeyParameters;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.CipherParameters;
import java.security.SecureRandom;
import org.bouncycastle.crypto.params.GOST3410KeyParameters;
import org.bouncycastle.crypto.DSAExt;

public class GOST3410Signer implements DSAExt
{
    GOST3410KeyParameters key;
    SecureRandom random;
    
    @Override
    public void init(final boolean b, CipherParameters parameters) {
        if (b) {
            SecureRandom random = null;
            if (parameters instanceof ParametersWithRandom) {
                final ParametersWithRandom parametersWithRandom = (ParametersWithRandom)parameters;
                random = parametersWithRandom.getRandom();
                parameters = parametersWithRandom.getParameters();
            }
            this.key = (GOST3410PrivateKeyParameters)parameters;
            this.random = CryptoServicesRegistrar.getSecureRandom(random);
        }
        else {
            this.key = (GOST3410PublicKeyParameters)parameters;
            this.random = null;
        }
        CryptoServicesRegistrar.checkConstraints(Utils.getDefaultProperties("GOST3410", this.key, b));
    }
    
    @Override
    public BigInteger getOrder() {
        return this.key.getParameters().getQ();
    }
    
    @Override
    public BigInteger[] generateSignature(final byte[] array) {
        final BigInteger val = new BigInteger(1, Arrays.reverse(array));
        final GOST3410Parameters parameters = this.key.getParameters();
        BigInteger randomBigInteger;
        do {
            randomBigInteger = BigIntegers.createRandomBigInteger(parameters.getQ().bitLength(), this.random);
        } while (randomBigInteger.compareTo(parameters.getQ()) >= 0);
        final BigInteger mod = parameters.getA().modPow(randomBigInteger, parameters.getP()).mod(parameters.getQ());
        return new BigInteger[] { mod, randomBigInteger.multiply(val).add(((GOST3410PrivateKeyParameters)this.key).getX().multiply(mod)).mod(parameters.getQ()) };
    }
    
    @Override
    public boolean verifySignature(final byte[] array, final BigInteger bigInteger, final BigInteger bigInteger2) {
        final BigInteger bigInteger3 = new BigInteger(1, Arrays.reverse(array));
        final GOST3410Parameters parameters = this.key.getParameters();
        final BigInteger value = BigInteger.valueOf(0L);
        if (value.compareTo(bigInteger) >= 0 || parameters.getQ().compareTo(bigInteger) <= 0) {
            return false;
        }
        if (value.compareTo(bigInteger2) >= 0 || parameters.getQ().compareTo(bigInteger2) <= 0) {
            return false;
        }
        final BigInteger modPow = bigInteger3.modPow(parameters.getQ().subtract(new BigInteger("2")), parameters.getQ());
        return parameters.getA().modPow(bigInteger2.multiply(modPow).mod(parameters.getQ()), parameters.getP()).multiply(((GOST3410PublicKeyParameters)this.key).getY().modPow(parameters.getQ().subtract(bigInteger).multiply(modPow).mod(parameters.getQ()), parameters.getP())).mod(parameters.getP()).mod(parameters.getQ()).equals(bigInteger);
    }
}
