X9FieldID.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.asn1.x9;
2
3import java.math.BigInteger;
4
5import org.bouncycastle.asn1.ASN1EncodableVector;
6import org.bouncycastle.asn1.ASN1Integer;
7import org.bouncycastle.asn1.ASN1Object;
8import org.bouncycastle.asn1.ASN1ObjectIdentifier;
9import org.bouncycastle.asn1.ASN1Primitive;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.DERSequence;
12
13/**
14 * ASN.1 def for Elliptic-Curve Field ID structure. See
15 * X9.62, for further details.
16 */
17public class X9FieldID
18    extends ASN1Object
19    implements X9ObjectIdentifiers
20{
21    private ASN1ObjectIdentifier     id;
22    private ASN1Primitive parameters;
23
24    /**
25     * Constructor for elliptic curves over prime fields
26     * <code>F<sub>2</sub></code>.
27     * @param primeP The prime <code>p</code> defining the prime field.
28     */
29    public X9FieldID(BigInteger primeP)
30    {
31        this.id = prime_field;
32        this.parameters = new ASN1Integer(primeP);
33    }
34
35    /**
36     * Constructor for elliptic curves over binary fields
37     * <code>F<sub>2<sup>m</sup></sub></code>.
38     * @param m  The exponent <code>m</code> of
39     * <code>F<sub>2<sup>m</sup></sub></code>.
40     * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> +
41     * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
42     * represents the reduction polynomial <code>f(z)</code>.
43     * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> +
44     * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
45     * represents the reduction polynomial <code>f(z)</code>.
46     * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> +
47     * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
48     * represents the reduction polynomial <code>f(z)</code>..
49     */
50    public X9FieldID(int m, int k1, int k2, int k3)
51    {
52        this.id = characteristic_two_field;
53        ASN1EncodableVector fieldIdParams = new ASN1EncodableVector();
54        fieldIdParams.add(new ASN1Integer(m));
55
56        if (k2 == 0)
57        {
58            fieldIdParams.add(tpBasis);
59            fieldIdParams.add(new ASN1Integer(k1));
60        }
61        else
62        {
63            fieldIdParams.add(ppBasis);
64            ASN1EncodableVector pentanomialParams = new ASN1EncodableVector();
65            pentanomialParams.add(new ASN1Integer(k1));
66            pentanomialParams.add(new ASN1Integer(k2));
67            pentanomialParams.add(new ASN1Integer(k3));
68            fieldIdParams.add(new DERSequence(pentanomialParams));
69        }
70
71        this.parameters = new DERSequence(fieldIdParams);
72    }
73
74    public X9FieldID(
75        ASN1Sequence  seq)
76    {
77        this.id = (ASN1ObjectIdentifier)seq.getObjectAt(0);
78        this.parameters = (ASN1Primitive)seq.getObjectAt(1);
79    }
80
81    public ASN1ObjectIdentifier getIdentifier()
82    {
83        return id;
84    }
85
86    public ASN1Primitive getParameters()
87    {
88        return parameters;
89    }
90
91    /**
92     * Produce a DER encoding of the following structure.
93     * <pre>
94     *  FieldID ::= SEQUENCE {
95     *      fieldType       FIELD-ID.&amp;id({IOSet}),
96     *      parameters      FIELD-ID.&amp;Type({IOSet}{&#64;fieldType})
97     *  }
98     * </pre>
99     */
100    public ASN1Primitive toASN1Primitive()
101    {
102        ASN1EncodableVector v = new ASN1EncodableVector();
103
104        v.add(this.id);
105        v.add(this.parameters);
106
107        return new DERSequence(v);
108    }
109}
110