1. /*
  2. * @(#)ECFieldFp.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. import java.util.Arrays;
  10. /**
  11. * This immutable class defines an elliptic curve (EC) prime
  12. * finite field.
  13. *
  14. * @see ECField
  15. *
  16. * @author Valerie Peng
  17. * @version 1.3, 12/19/03
  18. *
  19. * @since 1.5
  20. */
  21. public class ECFieldFp implements ECField {
  22. private BigInteger p;
  23. /**
  24. * Creates an elliptic curve prime finite field
  25. * with the specified prime <code>p</code>.
  26. * @param p the prime.
  27. * @exception NullPointerException if <code>p</code> is null.
  28. * @exception IllegalArgumentException if <code>p</code>
  29. * is not positive.
  30. */
  31. public ECFieldFp(BigInteger p) {
  32. if (p.signum() != 1) {
  33. throw new IllegalArgumentException("p is not positive");
  34. }
  35. this.p = p;
  36. }
  37. /**
  38. * Returns the field size in bits which is size of prime p
  39. * for this prime finite field.
  40. * @return the field size in bits.
  41. */
  42. public int getFieldSize() {
  43. return p.bitLength();
  44. };
  45. /**
  46. * Returns the prime <code>p</code> of this prime finite field.
  47. * @return the prime.
  48. */
  49. public BigInteger getP() {
  50. return p;
  51. }
  52. /**
  53. * Compares this prime finite field for equality with the
  54. * specified object.
  55. * @param obj the object to be compared.
  56. * @return true if <code>obj</code> is an instance
  57. * of ECFieldFp and the prime value match, false otherwise.
  58. */
  59. public boolean equals(Object obj) {
  60. if (this == obj) return true;
  61. if (obj instanceof ECFieldFp) {
  62. return (p.equals(((ECFieldFp)obj).p));
  63. }
  64. return false;
  65. }
  66. /**
  67. * Returns a hash code value for this prime finite field.
  68. * @return a hash code value.
  69. */
  70. public int hashCode() {
  71. return p.hashCode();
  72. }
  73. }