1/*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package java.security.spec;
26
27import java.math.BigInteger;
28import java.util.Arrays;
29
30/**
31 * This immutable class defines an elliptic curve (EC) prime
32 * finite field.
33 *
34 * @see ECField
35 *
36 * @author Valerie Peng
37 *
38 * @since 1.5
39 */
40public class ECFieldFp implements ECField {
41
42    private BigInteger p;
43
44    /**
45     * Creates an elliptic curve prime finite field
46     * with the specified prime {@code p}.
47     * @param p the prime.
48     * @exception NullPointerException if {@code p} is null.
49     * @exception IllegalArgumentException if {@code p}
50     * is not positive.
51     */
52    public ECFieldFp(BigInteger p) {
53        if (p.signum() != 1) {
54            throw new IllegalArgumentException("p is not positive");
55        }
56        this.p = p;
57    }
58
59    /**
60     * Returns the field size in bits which is size of prime p
61     * for this prime finite field.
62     * @return the field size in bits.
63     */
64    public int getFieldSize() {
65        return p.bitLength();
66    };
67
68    /**
69     * Returns the prime {@code p} of this prime finite field.
70     * @return the prime.
71     */
72    public BigInteger getP() {
73        return p;
74    }
75
76    /**
77     * Compares this prime finite field for equality with the
78     * specified object.
79     * @param obj the object to be compared.
80     * @return true if {@code obj} is an instance
81     * of ECFieldFp and the prime value match, false otherwise.
82     */
83    public boolean equals(Object obj) {
84        if (this == obj)  return true;
85        if (obj instanceof ECFieldFp) {
86            return (p.equals(((ECFieldFp)obj).p));
87        }
88        return false;
89    }
90
91    /**
92     * Returns a hash code value for this prime finite field.
93     * @return a hash code value.
94     */
95    public int hashCode() {
96        return p.hashCode();
97    }
98}
99