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

package org.bouncycastle.asn1.x9;

import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.math.ec.ECAlgorithms;
import org.bouncycastle.asn1.DERBitString;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import java.math.BigInteger;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.asn1.ASN1Object;

public class X9Curve extends ASN1Object implements X9ObjectIdentifiers
{
    private ECCurve curve;
    private byte[] seed;
    private ASN1ObjectIdentifier fieldIdentifier;
    
    public X9Curve(final ECCurve ecCurve) {
        this(ecCurve, null);
    }
    
    public X9Curve(final ECCurve curve, final byte[] array) {
        this.fieldIdentifier = null;
        this.curve = curve;
        this.seed = Arrays.clone(array);
        this.setFieldIdentifier();
    }
    
    public X9Curve(final X9FieldID x9FieldID, final BigInteger bigInteger, final BigInteger bigInteger2, final ASN1Sequence asn1Sequence) {
        this.fieldIdentifier = null;
        this.fieldIdentifier = x9FieldID.getIdentifier();
        if (this.fieldIdentifier.equals(X9Curve.prime_field)) {
            this.curve = new ECCurve.Fp(((ASN1Integer)x9FieldID.getParameters()).getValue(), new BigInteger(1, ASN1OctetString.getInstance(asn1Sequence.getObjectAt(0)).getOctets()), new BigInteger(1, ASN1OctetString.getInstance(asn1Sequence.getObjectAt(1)).getOctets()), bigInteger, bigInteger2);
        }
        else {
            if (!this.fieldIdentifier.equals(X9Curve.characteristic_two_field)) {
                throw new IllegalArgumentException("This type of ECCurve is not implemented");
            }
            final ASN1Sequence instance = ASN1Sequence.getInstance(x9FieldID.getParameters());
            final int intValueExact = ((ASN1Integer)instance.getObjectAt(0)).intValueExact();
            final ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier)instance.getObjectAt(1);
            int intValueExact2 = 0;
            int intValueExact3 = 0;
            int n;
            if (asn1ObjectIdentifier.equals(X9Curve.tpBasis)) {
                n = ASN1Integer.getInstance(instance.getObjectAt(2)).intValueExact();
            }
            else {
                if (!asn1ObjectIdentifier.equals(X9Curve.ppBasis)) {
                    throw new IllegalArgumentException("This type of EC basis is not implemented");
                }
                final ASN1Sequence instance2 = ASN1Sequence.getInstance(instance.getObjectAt(2));
                n = ASN1Integer.getInstance(instance2.getObjectAt(0)).intValueExact();
                intValueExact2 = ASN1Integer.getInstance(instance2.getObjectAt(1)).intValueExact();
                intValueExact3 = ASN1Integer.getInstance(instance2.getObjectAt(2)).intValueExact();
            }
            this.curve = new ECCurve.F2m(intValueExact, n, intValueExact2, intValueExact3, new BigInteger(1, ASN1OctetString.getInstance(asn1Sequence.getObjectAt(0)).getOctets()), new BigInteger(1, ASN1OctetString.getInstance(asn1Sequence.getObjectAt(1)).getOctets()), bigInteger, bigInteger2);
        }
        if (asn1Sequence.size() == 3) {
            this.seed = ((DERBitString)asn1Sequence.getObjectAt(2)).getBytes();
        }
    }
    
    private void setFieldIdentifier() {
        if (ECAlgorithms.isFpCurve(this.curve)) {
            this.fieldIdentifier = X9Curve.prime_field;
        }
        else {
            if (!ECAlgorithms.isF2mCurve(this.curve)) {
                throw new IllegalArgumentException("This type of ECCurve is not implemented");
            }
            this.fieldIdentifier = X9Curve.characteristic_two_field;
        }
    }
    
    public ECCurve getCurve() {
        return this.curve;
    }
    
    public byte[] getSeed() {
        return Arrays.clone(this.seed);
    }
    
    public boolean hasSeed() {
        return this.seed != null;
    }
    
    @Override
    public ASN1Primitive toASN1Primitive() {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector(3);
        if (this.fieldIdentifier.equals(X9Curve.prime_field)) {
            asn1EncodableVector.add(new X9FieldElement(this.curve.getA()).toASN1Primitive());
            asn1EncodableVector.add(new X9FieldElement(this.curve.getB()).toASN1Primitive());
        }
        else if (this.fieldIdentifier.equals(X9Curve.characteristic_two_field)) {
            asn1EncodableVector.add(new X9FieldElement(this.curve.getA()).toASN1Primitive());
            asn1EncodableVector.add(new X9FieldElement(this.curve.getB()).toASN1Primitive());
        }
        if (this.seed != null) {
            asn1EncodableVector.add(new DERBitString(this.seed));
        }
        return new DERSequence(asn1EncodableVector);
    }
}
