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

package org.bouncycastle.crypto.params;

import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.x9.X9ECParameters;
import java.math.BigInteger;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECConstants;

public class ECDomainParameters implements ECConstants
{
    private final ECCurve curve;
    private final byte[] seed;
    private final ECPoint G;
    private final BigInteger n;
    private final BigInteger h;
    private BigInteger hInv;
    
    public ECDomainParameters(final X9ECParameters x9ECParameters) {
        this(x9ECParameters.getCurve(), x9ECParameters.getG(), x9ECParameters.getN(), x9ECParameters.getH(), x9ECParameters.getSeed());
    }
    
    public ECDomainParameters(final ECCurve ecCurve, final ECPoint ecPoint, final BigInteger bigInteger) {
        this(ecCurve, ecPoint, bigInteger, ECDomainParameters.ONE, null);
    }
    
    public ECDomainParameters(final ECCurve ecCurve, final ECPoint ecPoint, final BigInteger bigInteger, final BigInteger bigInteger2) {
        this(ecCurve, ecPoint, bigInteger, bigInteger2, null);
    }
    
    public ECDomainParameters(final ECCurve curve, final ECPoint ecPoint, final BigInteger n, final BigInteger h, final byte[] array) {
        this.hInv = null;
        if (curve == null) {
            throw new NullPointerException("curve");
        }
        if (n == null) {
            throw new NullPointerException("n");
        }
        this.curve = curve;
        this.G = validatePublicPoint(curve, ecPoint);
        this.n = n;
        this.h = h;
        this.seed = Arrays.clone(array);
    }
    
    public ECCurve getCurve() {
        return this.curve;
    }
    
    public ECPoint getG() {
        return this.G;
    }
    
    public BigInteger getN() {
        return this.n;
    }
    
    public BigInteger getH() {
        return this.h;
    }
    
    public synchronized BigInteger getHInv() {
        if (this.hInv == null) {
            this.hInv = BigIntegers.modOddInverseVar(this.n, this.h);
        }
        return this.hInv;
    }
    
    public byte[] getSeed() {
        return Arrays.clone(this.seed);
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ECDomainParameters)) {
            return false;
        }
        final ECDomainParameters ecDomainParameters = (ECDomainParameters)o;
        return this.curve.equals(ecDomainParameters.curve) && this.G.equals(ecDomainParameters.G) && this.n.equals(ecDomainParameters.n);
    }
    
    @Override
    public int hashCode() {
        return ((4 * 257 ^ this.curve.hashCode()) * 257 ^ this.G.hashCode()) * 257 ^ this.n.hashCode();
    }
    
    public BigInteger validatePrivateScalar(final BigInteger bigInteger) {
        if (null == bigInteger) {
            throw new NullPointerException("Scalar cannot be null");
        }
        if (bigInteger.compareTo(ECConstants.ONE) < 0 || bigInteger.compareTo(this.getN()) >= 0) {
            throw new IllegalArgumentException("Scalar is not in the interval [1, n - 1]");
        }
        return bigInteger;
    }
    
    public ECPoint validatePublicPoint(final ECPoint ecPoint) {
        return validatePublicPoint(this.getCurve(), ecPoint);
    }
    
    static ECPoint validatePublicPoint(final ECCurve ecCurve, ECPoint normalize) {
        if (null == normalize) {
            throw new NullPointerException("Point cannot be null");
        }
        normalize = ECAlgorithms.importPoint(ecCurve, normalize).normalize();
        if (normalize.isInfinity()) {
            throw new IllegalArgumentException("Point at infinity");
        }
        if (!normalize.isValid()) {
            throw new IllegalArgumentException("Point not on curve");
        }
        return normalize;
    }
}
