1. /*
  2. * @(#)ECPoint.java 1.3 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security.spec;
  8. import java.math.BigInteger;
  9. /**
  10. * This immutable class represents a point on an elliptic curve (EC)
  11. * in affine coordinates. Other coordinate systems can
  12. * extend this class to represent this point in other
  13. * coordinates.
  14. *
  15. * @author Valerie Peng
  16. * @version 1.3, 12/19/03
  17. *
  18. * @since 1.5
  19. */
  20. public class ECPoint {
  21. private final BigInteger x;
  22. private final BigInteger y;
  23. /**
  24. * This defines the point at infinity.
  25. */
  26. public static final ECPoint POINT_INFINITY = new ECPoint();
  27. // private constructor for constructing point at infinity
  28. private ECPoint() {
  29. this.x = null;
  30. this.y = null;
  31. }
  32. /**
  33. * Creates an ECPoint from the specified affine x-coordinate
  34. * <code>x</code> and affine y-coordinate <code>y</code>.
  35. * @param x the affine x-coordinate.
  36. * @param y the affine y-coordinate.
  37. * @exception NullPointerException if <code>x</code> or
  38. * <code>y</code> is null.
  39. */
  40. public ECPoint(BigInteger x, BigInteger y) {
  41. if ((x==null) || (y==null)) {
  42. throw new NullPointerException("affine coordinate x or y is null");
  43. }
  44. this.x = x;
  45. this.y = y;
  46. }
  47. /**
  48. * Returns the affine x-coordinate <code>x</code>.
  49. * Note: POINT_INFINITY has a null affine x-coordinate.
  50. * @return the affine x-coordinate.
  51. */
  52. public BigInteger getAffineX() {
  53. return x;
  54. }
  55. /**
  56. * Returns the affine y-coordinate <code>y</code>.
  57. * Note: POINT_INFINITY has a null affine y-coordinate.
  58. * @return the affine y-coordinate.
  59. */
  60. public BigInteger getAffineY() {
  61. return y;
  62. }
  63. /**
  64. * Compares this elliptic curve point for equality with
  65. * the specified object.
  66. * @param obj the object to be compared.
  67. * @return true if <code>obj</code> is an instance of
  68. * ECPoint and the affine coordinates match, false otherwise.
  69. */
  70. public boolean equals(Object obj) {
  71. if (this == obj) return true;
  72. if (this == POINT_INFINITY) return false;
  73. if (obj instanceof ECPoint) {
  74. return ((x.equals(((ECPoint)obj).x)) &&
  75. (y.equals(((ECPoint)obj).y)));
  76. }
  77. return false;
  78. }
  79. /**
  80. * Returns a hash code value for this elliptic curve point.
  81. * @return a hash code value.
  82. */
  83. public int hashCode() {
  84. if (this == POINT_INFINITY) return 0;
  85. return x.hashCode() << 5 + y.hashCode();
  86. }
  87. }