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
22/**
23 * The parameters specifying a <i>prime finite field</i> of an
24 * elliptic curve.
25 */
26public class ECFieldFp implements ECField {
27    // Prime
28    private final BigInteger p;
29
30    /**
31     * Creates a new prime finite field of an elliptic curve with the specified
32     * prime {@code p}.
33     *
34     * @param p
35     *            the prime value {@code p}.
36     * @throws IllegalArgumentException
37     *             if {@code p <= zero}.
38     */
39    public ECFieldFp(BigInteger p) {
40        this.p = p;
41
42        if (this.p == null) {
43            throw new NullPointerException("p == null");
44        }
45        if (this.p.signum() != 1) {
46            throw new IllegalArgumentException("p <= 0");
47        }
48    }
49
50    /**
51     * Returns the size of the finite field (in bits).
52     *
53     * @return the size of the finite field (in bits).
54     */
55    public int getFieldSize() {
56        return p.bitLength();
57    }
58
59    /**
60     * Returns the prime value {@code p} for this finite field.
61     *
62     * @return the prime value {@code p} for this finite field.
63     */
64    public BigInteger getP() {
65        return p;
66    }
67
68    /**
69     * Returns whether the specified object is equal to this finite field.
70     *
71     * @param obj
72     *            the object to compare to this finite field.
73     * @return {@code true} if the specified object is equal to this finite field,
74     *         otherwise {@code false}.
75     */
76    public boolean equals(Object obj) {
77        // object equals itself
78        if (this == obj) {
79            return true;
80        }
81        if (obj instanceof ECFieldFp) {
82            return (this.p.equals(((ECFieldFp)obj).p));
83        }
84        return false;
85    }
86
87    /**
88     * Returns the hashcode value for this finite field.
89     *
90     * @return the hashcode value for this finite field.
91     */
92    public int hashCode() {
93        return p.hashCode();
94    }
95}
96