1package org.bouncycastle.jce.spec;
2
3import java.math.BigInteger;
4import java.security.spec.ECFieldF2m;
5import java.security.spec.ECFieldFp;
6import java.security.spec.ECPoint;
7import java.security.spec.EllipticCurve;
8
9import org.bouncycastle.math.ec.ECCurve;
10
11/**
12 * specification signifying that the curve parameters can also be
13 * referred to by name.
14 */
15public class ECNamedCurveSpec
16    extends java.security.spec.ECParameterSpec
17{
18    private String  name;
19
20    private static EllipticCurve convertCurve(
21        ECCurve  curve,
22        byte[]   seed)
23    {
24        if (curve instanceof ECCurve.Fp)
25        {
26            return new EllipticCurve(new ECFieldFp(((ECCurve.Fp)curve).getQ()), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
27        }
28        else
29        {
30            ECCurve.F2m curveF2m = (ECCurve.F2m)curve;
31            int ks[];
32
33            if (curveF2m.isTrinomial())
34            {
35                ks = new int[] { curveF2m.getK1() };
36
37                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
38            }
39            else
40            {
41                ks = new int[] { curveF2m.getK3(), curveF2m.getK2(), curveF2m.getK1() };
42
43                return new EllipticCurve(new ECFieldF2m(curveF2m.getM(), ks), curve.getA().toBigInteger(), curve.getB().toBigInteger(), seed);
44            }
45        }
46
47    }
48
49    private static ECPoint convertPoint(
50        org.bouncycastle.math.ec.ECPoint  g)
51    {
52        return new ECPoint(g.getX().toBigInteger(), g.getY().toBigInteger());
53    }
54
55    public ECNamedCurveSpec(
56        String                              name,
57        ECCurve                             curve,
58        org.bouncycastle.math.ec.ECPoint    g,
59        BigInteger                          n)
60    {
61        super(convertCurve(curve, null), convertPoint(g), n, 1);
62
63        this.name = name;
64    }
65
66    public ECNamedCurveSpec(
67        String          name,
68        EllipticCurve   curve,
69        ECPoint         g,
70        BigInteger      n)
71    {
72        super(curve, g, n, 1);
73
74        this.name = name;
75    }
76
77    public ECNamedCurveSpec(
78        String                              name,
79        ECCurve                             curve,
80        org.bouncycastle.math.ec.ECPoint    g,
81        BigInteger                          n,
82        BigInteger                          h)
83    {
84        super(convertCurve(curve, null), convertPoint(g), n, h.intValue());
85
86        this.name = name;
87    }
88
89    public ECNamedCurveSpec(
90        String          name,
91        EllipticCurve   curve,
92        ECPoint         g,
93        BigInteger      n,
94        BigInteger      h)
95    {
96        super(curve, g, n, h.intValue());
97
98        this.name = name;
99    }
100
101    public ECNamedCurveSpec(
102        String                              name,
103        ECCurve                             curve,
104        org.bouncycastle.math.ec.ECPoint    g,
105        BigInteger                          n,
106        BigInteger                          h,
107        byte[]                              seed)
108    {
109        super(convertCurve(curve, seed), convertPoint(g), n, h.intValue());
110
111        this.name = name;
112    }
113
114    /**
115     * return the name of the curve the EC domain parameters belong to.
116     */
117    public String getName()
118    {
119        return name;
120    }
121}
122