ECFieldFp.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package java.security.spec;
19
20import java.math.BigInteger;
21
22import org.apache.harmony.security.internal.nls.Messages;
23
24/**
25 * The parameters specifying a <i>prime finite field</i> of an
26 * elliptic curve.
27 *
28 * @since Android 1.0
29 */
30public class ECFieldFp implements ECField {
31    // Prime
32    private final BigInteger p;
33
34    /**
35     * Creates a new prime finite field of an elliptic curve with the specified
36     * prime {@code p}.
37     *
38     * @param p
39     *            the prime value {@code p}.
40     * @throws IllegalArgumentException
41     *             if {@code p <= zero}.
42     * @since Android 1.0
43     */
44    public ECFieldFp(BigInteger p) {
45        this.p = p;
46
47        if (this.p == null) {
48            throw new NullPointerException(Messages.getString("security.83", "p")); //$NON-NLS-1$ //$NON-NLS-2$
49        }
50        if (this.p.signum() != 1) {
51            throw new IllegalArgumentException(Messages.getString("security.86", "p")); //$NON-NLS-1$ //$NON-NLS-2$
52        }
53    }
54
55    /**
56     * Returns the size of the finite field (in bits).
57     *
58     * @return the size of the finite field (in bits).
59     * @since Android 1.0
60     */
61    public int getFieldSize() {
62        return p.bitLength();
63    }
64
65    /**
66     * Returns the prime value {@code p} for this finite field.
67     *
68     * @return the prime value {@code p} for this finite field.
69     * @since Android 1.0
70     */
71    public BigInteger getP() {
72        return p;
73    }
74
75    /**
76     * Returns whether the specified object is equal to this finite field.
77     *
78     * @param obj
79     *            the object to compare to this finite field.
80     * @return {@code true} if the specified object is equal to this finite field,
81     *         otherwise {@code false}.
82     * @since Android 1.0
83     */
84    public boolean equals(Object obj) {
85        // object equals itself
86        if (this == obj) {
87            return true;
88        }
89        if (obj instanceof ECFieldFp) {
90            return (this.p.equals(((ECFieldFp)obj).p));
91        }
92        return false;
93    }
94
95    /**
96     * Returns the hashcode value for this finite field.
97     *
98     * @return the hashcode value for this finite field.
99     * @since Android 1.0
100     */
101    public int hashCode() {
102        return p.hashCode();
103    }
104}
105