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 * A Point on an Elliptic Curve in barycentric (or affine) coordinates.
24 */
25public class ECPoint {
26
27    /**
28     * The point on an Elliptic Curve at infinity.
29     */
30    public static final ECPoint POINT_INFINITY = new ECPoint();
31    // affine X coordinate of this point
32    private final BigInteger affineX;
33    // affine Y coordinate of this point
34    private final BigInteger affineY;
35
36    // Private ctor for POINT_INFINITY
37    private ECPoint() {
38        affineX = null;
39        affineY = null;
40    }
41
42    /**
43     * Creates a new point at the specified coordinates.
44     *
45     * @param affineX
46     *            the x-coordinate.
47     * @param affineY
48     *            the y-coordinate.
49     */
50    public ECPoint(BigInteger affineX, BigInteger affineY) {
51        this.affineX = affineX;
52        if (this.affineX == null) {
53            throw new NullPointerException("affineX == null");
54        }
55        this.affineY = affineY;
56        if (this.affineY == null) {
57            throw new NullPointerException("affineY == null");
58        }
59    }
60
61    /**
62     * Returns the x-coordinate.
63     *
64     * @return the x-coordinate, or {@code null} for the infinite point.
65     */
66    public BigInteger getAffineX() {
67        return affineX;
68    }
69
70    /**
71     * Returns the y-coordinate.
72     *
73     * @return the y-coordinate, or {@code null} fot the infinite point.
74     */
75    public BigInteger getAffineY() {
76        return affineY;
77    }
78
79    /**
80     * Returns whether the specified object and this elliptic curve point are
81     * equal.
82     *
83     * @param other
84     *            the object to compare.
85     * @return {@code true} if the specified object and this elliptic curve
86     *         point are equal, otherwise {@code false}.
87     */
88    public boolean equals(Object other) {
89        if (this == other) {
90            return true;
91        }
92        if (other instanceof ECPoint) {
93            if (this.affineX != null) {
94                ECPoint otherPoint = (ECPoint)other;
95                // no need to check for null in this case
96                return this.affineX.equals(otherPoint.affineX) &&
97                       this.affineY.equals(otherPoint.affineY);
98            } else {
99                return other == POINT_INFINITY;
100            }
101        }
102        return false;
103    }
104
105    /**
106     * Returns the hashcode of this elliptic curve point.
107     *
108     * @return the hashcode of this elliptic curve point.
109     */
110    public int hashCode() {
111        if (this.affineX != null) {
112            return affineX.hashCode() * 31 + affineY.hashCode();
113        }
114        return 11;
115    }
116}
117