1515 * fields.
1616 */
1717public abstract class EllipticCurve {
18+
1819 private Point basePoint ;
1920 private BigInteger basePointOrder ;
2021 /**
@@ -26,7 +27,7 @@ public abstract class EllipticCurve {
2627 * Every child class must define its own public constructor. These
2728 * constructors must be able to set the coefficients for the curve. They can
2829 * use this constructor to set the value of modulus.
29- *
30+ *
3031 * @param modulus
3132 * The modulus of the field over which the curve is defined.
3233 */
@@ -39,7 +40,7 @@ protected EllipticCurve(BigInteger modulus) {
3940 * constructors must be able to set the coefficients for the curve. They can
4041 * use this constructor to set the values of modulus, basePoint and
4142 * basePointOrder.
42- *
43+ *
4344 * @param modulus
4445 * The modulus of the field over which the curve is defined.
4546 * @param basePointX
@@ -60,7 +61,7 @@ protected EllipticCurve(BigInteger modulus, BigInteger basePointX, BigInteger ba
6061 * result will be null. If one point is not on the curve and the
6162 * calculations would require dividing by 0, the result will be the point at
6263 * infinity.
63- *
64+ *
6465 * @param p
6566 * A point which's coordinates are elements of the field over
6667 * which the curve is defined or the point at infinity.
@@ -69,83 +70,70 @@ protected EllipticCurve(BigInteger modulus, BigInteger basePointX, BigInteger ba
6970 * which the curve is defined or the point at infinity.
7071 */
7172 public Point add (Point p , Point q ) {
73+ if (p .isAtInfinity ()) {
74+ // O + q == q
75+ return q ;
76+ }
7277
73- if (p == null || q == null ) {
74- return null ;
75- } else {
76- if (p .isAtInfinity ()) {
77- // O + q == q
78- return q ;
79- }
80-
81- if (q .isAtInfinity ()) {
82- // p + O == p
83- return p ;
84- }
85-
86- if (this .inverse (p ).equals (q )) {
87- // p == -q <=> -p == q
88- // => p + q = O
89- return new Point ();
90- }
78+ if (q .isAtInfinity ()) {
79+ // p + O == p
80+ return p ;
81+ }
9182
92- return this .additionFormular (p , q );
83+ if (this .inverse (p ).equals (q )) {
84+ // p == -q <=> -p == q
85+ // => p + q = O
86+ return new Point ();
9387 }
88+
89+ return this .additionFormular (p , q );
9490 }
9591
9692 /**
9793 * Returns k*p on this curve. If k or p is null, the result will be null. If
9894 * the point is not on the curve and the calculations would require dividing
9995 * by 0, the result will be the point at infinity.
100- *
96+ *
10197 * @param p
10298 * A point which's coordinates are elements of the field over
10399 * which the curve is defined or the point at infinity.
104100 */
105101 public Point mult (BigInteger k , Point p ) {
106- if (k == null || p == null ) {
107- return null ;
108- } else {
109- if (k .compareTo (BigInteger .ZERO ) < 0 ) {
110- k = k .negate ();
111- p = this .inverse (p );
112- }
113-
114- // Double-and-add
102+ if (k .compareTo (BigInteger .ZERO ) < 0 ) {
103+ k = k .negate ();
104+ p = this .inverse (p );
105+ }
115106
116- Point q = new Point (); // q == O
107+ // Double-and-add
108+ Point q = new Point (); // q == O
117109
118- for (int i = k .bitLength (); i > 0 ; i --) {
110+ for (int i = k .bitLength (); i > 0 ; i --) {
119111
120- q = this .add (q , q );
112+ q = this .add (q , q );
121113
122- if (k .testBit (i - 1 )) {
123- q = this .add (q , p );
124- }
114+ if (k .testBit (i - 1 )) {
115+ q = this .add (q , p );
125116 }
126-
127- return q ;
128117 }
118+
119+ return q ;
129120 }
130121
131122 /**
132123 * Returns the unique point q with the property p + q = O on this curve. If
133124 * p is null the result will be null.
134- *
125+ *
135126 * @param p
136127 * A point which's coordinates are elements of the field over
137128 * which the curve is defined or the point at infinity.
138129 */
139130 public Point inverse (Point p ) {
140- if (p == null ) {
141- return null ;
131+
132+ if (p .isAtInfinity ()) {
133+ // -O == O
134+ return p ;
142135 } else {
143- if (p .isAtInfinity ()) {
144- // -O == O
145- return p ;
146- } else {
147- return this .inverseAffine (p );
148- }
136+ return this .inverseAffine (p );
149137 }
150138 }
151139
@@ -154,7 +142,7 @@ public Point inverse(Point p) {
154142 * are elements of the field over which this curve is defined. Whenever
155143 * possible, this method should be used instead of creating a point via its
156144 * own constructor.
157- *
145+ *
158146 * @param x
159147 * The x coordinate of the point.
160148 * @param y
@@ -164,7 +152,7 @@ public Point inverse(Point p) {
164152
165153 /**
166154 * Returns true iff the point p is on the curve.
167- *
155+ *
168156 * @param p
169157 * An affine point which's coordinates are elements of the field
170158 * over which the curve is defined or the point at infinity.
@@ -174,7 +162,7 @@ public Point inverse(Point p) {
174162 /**
175163 * Returns the unique (affine) point q with the property p + q = O on this
176164 * curve.
177- *
165+ *
178166 * @param p
179167 * An affine point which's coordinates are elements of the field
180168 * over which the curve is defined.
@@ -185,7 +173,7 @@ public Point inverse(Point p) {
185173 * Returns p+q for two affine points p and q, with p != -q. If one point is
186174 * not on the curve and the calculations would require dividing by 0, the
187175 * result will be the point at infinity.
188- *
176+ *
189177 * @param p
190178 * An affine point which's coordinates are elements of the field
191179 * over which the curve is defined.
0 commit comments