|
19 | 19 | public class ECJPAKECurve |
20 | 20 | { |
21 | 21 | private final ECCurve.Fp curve; |
22 | | - private final BigInteger a; |
23 | | - private final BigInteger b; |
24 | | - private final BigInteger q; |
25 | | - private final BigInteger h; |
26 | | - private final BigInteger n; |
27 | 22 | private final ECPoint g; |
28 | 23 |
|
29 | 24 | /** |
@@ -54,116 +49,123 @@ public class ECJPAKECurve |
54 | 49 | * @throws NullPointerException if any argument is null |
55 | 50 | * @throws IllegalArgumentException if any of the above validations fail |
56 | 51 | */ |
57 | | - public ECJPAKECurve(BigInteger a, BigInteger b, BigInteger q, BigInteger h, BigInteger n, ECPoint g, ECCurve.Fp curve) |
| 52 | + public ECJPAKECurve(BigInteger q, BigInteger a, BigInteger b, BigInteger n, BigInteger h, BigInteger g_x, BigInteger g_y) |
58 | 53 | { |
| 54 | + ECJPAKEUtil.validateNotNull(a, "a"); |
| 55 | + ECJPAKEUtil.validateNotNull(b, "b"); |
| 56 | + ECJPAKEUtil.validateNotNull(q, "q"); |
| 57 | + ECJPAKEUtil.validateNotNull(n, "n"); |
| 58 | + ECJPAKEUtil.validateNotNull(h, "h"); |
| 59 | + ECJPAKEUtil.validateNotNull(g_x, "g_x"); |
| 60 | + ECJPAKEUtil.validateNotNull(g_y, "g_y"); |
| 61 | + |
59 | 62 | /* |
60 | 63 | * Don't skip the checks on user-specified groups. |
61 | 64 | */ |
62 | | - this(a, b, q, h, n, g, curve, false); |
| 65 | + |
| 66 | + /* |
| 67 | + * Note that these checks do not guarantee that n and q are prime. |
| 68 | + * We just have reasonable certainty that they are prime. |
| 69 | + */ |
| 70 | + if (!q.isProbablePrime(20)) |
| 71 | + { |
| 72 | + throw new IllegalArgumentException("Field size q must be prime"); |
| 73 | + } |
| 74 | + |
| 75 | + if (a.compareTo(BigInteger.ZERO) < 0 || a.compareTo(q) >= 0) |
| 76 | + { |
| 77 | + throw new IllegalArgumentException("The parameter 'a' is not in the field [0, q-1]"); |
| 78 | + } |
| 79 | + |
| 80 | + if (b.compareTo(BigInteger.ZERO) < 0 || b.compareTo(q) >= 0) |
| 81 | + { |
| 82 | + throw new IllegalArgumentException("The parameter 'b' is not in the field [0, q-1]"); |
| 83 | + } |
| 84 | + |
| 85 | + BigInteger d = calculateDeterminant(q, a, b); |
| 86 | + if (d.equals(BigInteger.ZERO)) |
| 87 | + { |
| 88 | + throw new IllegalArgumentException("The curve is singular, i.e the discriminant is equal to 0 mod q."); |
| 89 | + } |
| 90 | + |
| 91 | + if (!n.isProbablePrime(20)) |
| 92 | + { |
| 93 | + throw new IllegalArgumentException("The order n must be prime"); |
| 94 | + } |
| 95 | + |
| 96 | + /* |
| 97 | + * TODO It's expensive to calculate the actual total number of points. Probably the best that could be done is |
| 98 | + * checking that the point count is within the Hasse bound? |
| 99 | + */ |
| 100 | +// BigInteger totalPoints = n.multiply(h); |
| 101 | + |
| 102 | + ECCurve.Fp curve = new ECCurve.Fp(q, a, b, n, h); |
| 103 | + ECPoint g = curve.createPoint(g_x, g_y); |
| 104 | + |
| 105 | + if (!g.isValid()) |
| 106 | + { |
| 107 | + throw new IllegalArgumentException("The base point G does not lie on the curve."); |
| 108 | + } |
| 109 | + |
| 110 | + this.curve = curve; |
| 111 | + this.g = g; |
63 | 112 | } |
64 | 113 |
|
65 | 114 | /** |
66 | 115 | * Internal package-private constructor used by the pre-approved |
67 | 116 | * groups in {@link ECJPAKECurves}. |
68 | 117 | * These pre-approved curves can avoid the expensive checks. |
69 | 118 | */ |
70 | | - ECJPAKECurve(BigInteger a, BigInteger b, BigInteger q, BigInteger h, BigInteger n, ECPoint g, ECCurve.Fp curve, boolean skipChecks) |
| 119 | + ECJPAKECurve(ECCurve.Fp curve, ECPoint g) |
71 | 120 | { |
72 | | - ECJPAKEUtil.validateNotNull(a, "a"); |
73 | | - ECJPAKEUtil.validateNotNull(b, "b"); |
74 | | - ECJPAKEUtil.validateNotNull(q, "q"); |
75 | | - ECJPAKEUtil.validateNotNull(h, "h"); |
76 | | - ECJPAKEUtil.validateNotNull(n, "n"); |
77 | | - ECJPAKEUtil.validateNotNull(g, "g"); |
78 | 121 | ECJPAKEUtil.validateNotNull(curve, "curve"); |
| 122 | + ECJPAKEUtil.validateNotNull(g, "g"); |
| 123 | + ECJPAKEUtil.validateNotNull(curve.getOrder(), "n"); |
| 124 | + ECJPAKEUtil.validateNotNull(curve.getCofactor(), "h"); |
79 | 125 |
|
80 | | - if (!skipChecks) |
81 | | - { |
| 126 | + this.curve = curve; |
| 127 | + this.g = g; |
| 128 | + } |
82 | 129 |
|
83 | | - /* |
84 | | - * Note that these checks do not guarantee that n and q are prime. |
85 | | - * We just have reasonable certainty that they are prime. |
86 | | - */ |
87 | | - if (!q.isProbablePrime(20)) |
88 | | - { |
89 | | - throw new IllegalArgumentException("Field size q must be prime"); |
90 | | - } |
91 | | - |
92 | | - if (!n.isProbablePrime(20)) |
93 | | - { |
94 | | - throw new IllegalArgumentException("The order n must be prime"); |
95 | | - } |
96 | | - |
97 | | - if ((a.pow(3).multiply(BigInteger.valueOf(4)).add(b.pow(2).multiply(BigInteger.valueOf(27))).mod(q)) == BigInteger.valueOf(0)) |
98 | | - { |
99 | | - throw new IllegalArgumentException("The curve is singular, i.e the discriminant is equal to 0 mod q."); |
100 | | - } |
101 | | - |
102 | | - if (!g.isValid()) |
103 | | - { |
104 | | - throw new IllegalArgumentException("The base point G does not lie on the curve."); |
105 | | - } |
106 | | - |
107 | | - BigInteger totalPoints = n.multiply(h); |
108 | | - if (!totalPoints.equals(curve.getOrder())) |
109 | | - { |
110 | | - throw new IllegalArgumentException("n is not equal to the order of your curve"); |
111 | | - } |
112 | | - |
113 | | - if (a.compareTo(BigInteger.ZERO) == -1 || a.compareTo(q.subtract(BigInteger.ONE)) == 1) |
114 | | - { |
115 | | - throw new IllegalArgumentException("The parameter 'a' is not in the field [0, q-1]"); |
116 | | - } |
117 | | - |
118 | | - if (b.compareTo(BigInteger.ZERO) == -1 || b.compareTo(q.subtract(BigInteger.ONE)) == 1) |
119 | | - { |
120 | | - throw new IllegalArgumentException("The parameter 'b' is not in the field [0, q-1]"); |
121 | | - } |
122 | | - } |
| 130 | + public ECCurve.Fp getCurve() |
| 131 | + { |
| 132 | + return curve; |
| 133 | + } |
123 | 134 |
|
124 | | - this.a = a; |
125 | | - this.b = b; |
126 | | - this.h = h; |
127 | | - this.n = n; |
128 | | - this.q = q; |
129 | | - this.g = g; |
130 | | - this.curve = curve; |
| 135 | + public ECPoint getG() |
| 136 | + { |
| 137 | + return g; |
131 | 138 | } |
132 | 139 |
|
133 | 140 | public BigInteger getA() |
134 | 141 | { |
135 | | - return a; |
| 142 | + return curve.getA().toBigInteger(); |
136 | 143 | } |
137 | 144 |
|
138 | 145 | public BigInteger getB() |
139 | 146 | { |
140 | | - return b; |
| 147 | + return curve.getB().toBigInteger(); |
141 | 148 | } |
142 | 149 |
|
143 | 150 | public BigInteger getN() |
144 | 151 | { |
145 | | - return n; |
| 152 | + return curve.getOrder(); |
146 | 153 | } |
147 | 154 |
|
148 | 155 | public BigInteger getH() |
149 | 156 | { |
150 | | - return h; |
| 157 | + return curve.getCofactor(); |
151 | 158 | } |
152 | 159 |
|
153 | 160 | public BigInteger getQ() |
154 | 161 | { |
155 | | - return q; |
| 162 | + return curve.getQ(); |
156 | 163 | } |
157 | 164 |
|
158 | | - public ECPoint getG() |
| 165 | + private static BigInteger calculateDeterminant(BigInteger q, BigInteger a, BigInteger b) |
159 | 166 | { |
160 | | - return g; |
| 167 | + BigInteger a3x4 = a.multiply(a).mod(q).multiply(a).mod(q).shiftLeft(2); |
| 168 | + BigInteger b2x27 = b.multiply(b).mod(q).multiply(BigInteger.valueOf(27)); |
| 169 | + return a3x4.add(b2x27).mod(q); |
161 | 170 | } |
162 | | - |
163 | | - public ECCurve.Fp getCurve() |
164 | | - { |
165 | | - return curve; |
166 | | - } |
167 | | - |
168 | | - |
169 | 171 | } |
0 commit comments