@@ -27,42 +27,42 @@ var BigInteger = jsbn.BigInteger,
27
27
function ECFieldElementFp ( q , x ) {
28
28
this . x = x ;
29
29
// TODO if(x.compareTo(q) >= 0) error
30
- this . q = q ;
30
+ this . p = q ;
31
31
}
32
32
33
33
function feFpEquals ( other ) {
34
34
if ( other === this ) {
35
35
return true ;
36
36
}
37
- return ( this . q . equals ( other . q ) && this . x . equals ( other . x ) ) ;
37
+ return ( this . p . equals ( other . p ) && this . x . equals ( other . x ) ) ;
38
38
}
39
39
40
40
function feFpToBigInteger ( ) {
41
41
return this . x ;
42
42
}
43
43
44
44
function feFpNegate ( ) {
45
- return new ECFieldElementFp ( this . q , this . x . negate ( ) . mod ( this . q ) ) ;
45
+ return new ECFieldElementFp ( this . p , this . x . negate ( ) . mod ( this . p ) ) ;
46
46
}
47
47
48
48
function feFpAdd ( b ) {
49
- return new ECFieldElementFp ( this . q , this . x . add ( b . toBigInteger ( ) ) . mod ( this . q ) ) ;
49
+ return new ECFieldElementFp ( this . p , this . x . add ( b . toBigInteger ( ) ) . mod ( this . p ) ) ;
50
50
}
51
51
52
52
function feFpSubtract ( b ) {
53
- return new ECFieldElementFp ( this . q , this . x . subtract ( b . toBigInteger ( ) ) . mod ( this . q ) ) ;
53
+ return new ECFieldElementFp ( this . p , this . x . subtract ( b . toBigInteger ( ) ) . mod ( this . p ) ) ;
54
54
}
55
55
56
56
function feFpMultiply ( b ) {
57
- return new ECFieldElementFp ( this . q , this . x . multiply ( b . toBigInteger ( ) ) . mod ( this . q ) ) ;
57
+ return new ECFieldElementFp ( this . p , this . x . multiply ( b . toBigInteger ( ) ) . mod ( this . p ) ) ;
58
58
}
59
59
60
60
function feFpSquare ( ) {
61
- return new ECFieldElementFp ( this . q , this . x . square ( ) . mod ( this . q ) ) ;
61
+ return new ECFieldElementFp ( this . p , this . x . square ( ) . mod ( this . p ) ) ;
62
62
}
63
63
64
64
function feFpDivide ( b ) {
65
- return new ECFieldElementFp ( this . q , this . x . multiply ( b . toBigInteger ( ) . modInverse ( this . q ) ) . mod ( this . q ) ) ;
65
+ return new ECFieldElementFp ( this . p , this . x . multiply ( b . toBigInteger ( ) . modInverse ( this . p ) ) . mod ( this . p ) ) ;
66
66
}
67
67
68
68
ECFieldElementFp . prototype . equals = feFpEquals ;
@@ -95,7 +95,7 @@ function ECPointFp(curve, x, y, z) {
95
95
96
96
function pointFpGetX ( ) {
97
97
if ( ! this . zinv ) {
98
- this . zinv = this . z . modInverse ( this . curve . q ) ;
98
+ this . zinv = this . z . modInverse ( this . curve . p ) ;
99
99
}
100
100
var r = this . x . toBigInteger ( ) . multiply ( this . zinv ) ;
101
101
this . curve . reduce ( r ) ;
@@ -104,7 +104,7 @@ function pointFpGetX() {
104
104
105
105
function pointFpGetY ( ) {
106
106
if ( ! this . zinv ) {
107
- this . zinv = this . z . modInverse ( this . curve . q ) ;
107
+ this . zinv = this . z . modInverse ( this . curve . p ) ;
108
108
}
109
109
var r = this . y . toBigInteger ( ) . multiply ( this . zinv ) ;
110
110
this . curve . reduce ( r ) ;
@@ -123,12 +123,12 @@ function pointFpEquals(other) {
123
123
}
124
124
var u , v ;
125
125
// u = Y2 * Z1 - Y1 * Z2
126
- u = other . y . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . y . toBigInteger ( ) . multiply ( other . z ) ) . mod ( this . curve . q ) ;
126
+ u = other . y . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . y . toBigInteger ( ) . multiply ( other . z ) ) . mod ( this . curve . p ) ;
127
127
if ( ! u . equals ( BigInteger . ZERO ) ) {
128
128
return false ;
129
129
}
130
130
// v = X2 * Z1 - X1 * Z2
131
- v = other . x . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . x . toBigInteger ( ) . multiply ( other . z ) ) . mod ( this . curve . q ) ;
131
+ v = other . x . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . x . toBigInteger ( ) . multiply ( other . z ) ) . mod ( this . curve . p ) ;
132
132
return v . equals ( BigInteger . ZERO ) ;
133
133
}
134
134
@@ -152,9 +152,9 @@ function pointFpAdd(b) {
152
152
}
153
153
154
154
// u = Y2 * Z1 - Y1 * Z2
155
- var u = b . y . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . y . toBigInteger ( ) . multiply ( b . z ) ) . mod ( this . curve . q ) ;
155
+ var u = b . y . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . y . toBigInteger ( ) . multiply ( b . z ) ) . mod ( this . curve . p ) ;
156
156
// v = X2 * Z1 - X1 * Z2
157
- var v = b . x . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . x . toBigInteger ( ) . multiply ( b . z ) ) . mod ( this . curve . q ) ;
157
+ var v = b . x . toBigInteger ( ) . multiply ( this . z ) . subtract ( this . x . toBigInteger ( ) . multiply ( b . z ) ) . mod ( this . curve . p ) ;
158
158
159
159
if ( BigInteger . ZERO . equals ( v ) ) {
160
160
if ( BigInteger . ZERO . equals ( u ) ) {
@@ -173,11 +173,11 @@ function pointFpAdd(b) {
173
173
var zu2 = u . square ( ) . multiply ( this . z ) ;
174
174
175
175
// x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
176
- var x3 = zu2 . subtract ( x1v2 . shiftLeft ( 1 ) ) . multiply ( b . z ) . subtract ( v3 ) . multiply ( v ) . mod ( this . curve . q ) ;
176
+ var x3 = zu2 . subtract ( x1v2 . shiftLeft ( 1 ) ) . multiply ( b . z ) . subtract ( v3 ) . multiply ( v ) . mod ( this . curve . p ) ;
177
177
// y3 = z2 * (3 * x1 * u * v^2 - y1 * v^3 - z1 * u^3) + u * v^3
178
- var y3 = x1v2 . multiply ( THREE ) . multiply ( u ) . subtract ( y1 . multiply ( v3 ) ) . subtract ( zu2 . multiply ( u ) ) . multiply ( b . z ) . add ( u . multiply ( v3 ) ) . mod ( this . curve . q ) ;
178
+ var y3 = x1v2 . multiply ( THREE ) . multiply ( u ) . subtract ( y1 . multiply ( v3 ) ) . subtract ( zu2 . multiply ( u ) ) . multiply ( b . z ) . add ( u . multiply ( v3 ) ) . mod ( this . curve . p ) ;
179
179
// z3 = v^3 * z1 * z2
180
- var z3 = v3 . multiply ( this . z ) . multiply ( b . z ) . mod ( this . curve . q ) ;
180
+ var z3 = v3 . multiply ( this . z ) . multiply ( b . z ) . mod ( this . curve . p ) ;
181
181
182
182
return new ECPointFp ( this . curve , this . curve . fromBigInteger ( x3 ) , this . curve . fromBigInteger ( y3 ) , z3 ) ;
183
183
}
@@ -196,22 +196,22 @@ function pointFpTwice() {
196
196
var y1 = this . y . toBigInteger ( ) ;
197
197
198
198
var y1z1 = y1 . multiply ( this . z ) ;
199
- var y1sqz1 = y1z1 . multiply ( y1 ) . mod ( this . curve . q ) ;
199
+ var y1sqz1 = y1z1 . multiply ( y1 ) . mod ( this . curve . p ) ;
200
200
var a = this . curve . a . toBigInteger ( ) ;
201
201
202
202
// w = 3 * x1^2 + a * z1^2
203
203
var w = x1 . square ( ) . multiply ( THREE ) ;
204
204
if ( ! BigInteger . ZERO . equals ( a ) ) {
205
205
w = w . add ( this . z . square ( ) . multiply ( a ) ) ;
206
206
}
207
- w = w . mod ( this . curve . q ) ;
207
+ w = w . mod ( this . curve . p ) ;
208
208
//this.curve.reduce(w);
209
209
// x3 = 2 * y1 * z1 * (w^2 - 8 * x1 * y1^2 * z1)
210
- var x3 = w . square ( ) . subtract ( x1 . shiftLeft ( 3 ) . multiply ( y1sqz1 ) ) . shiftLeft ( 1 ) . multiply ( y1z1 ) . mod ( this . curve . q ) ;
210
+ var x3 = w . square ( ) . subtract ( x1 . shiftLeft ( 3 ) . multiply ( y1sqz1 ) ) . shiftLeft ( 1 ) . multiply ( y1z1 ) . mod ( this . curve . p ) ;
211
211
// y3 = 4 * y1^2 * z1 * (3 * w * x1 - 2 * y1^2 * z1) - w^3
212
- var y3 = w . multiply ( THREE ) . multiply ( x1 ) . subtract ( y1sqz1 . shiftLeft ( 1 ) ) . shiftLeft ( 2 ) . multiply ( y1sqz1 ) . subtract ( w . square ( ) . multiply ( w ) ) . mod ( this . curve . q ) ;
212
+ var y3 = w . multiply ( THREE ) . multiply ( x1 ) . subtract ( y1sqz1 . shiftLeft ( 1 ) ) . shiftLeft ( 2 ) . multiply ( y1sqz1 ) . subtract ( w . square ( ) . multiply ( w ) ) . mod ( this . curve . p ) ;
213
213
// z3 = 8 * (y1 * z1)^3
214
- var z3 = y1z1 . square ( ) . multiply ( y1z1 ) . shiftLeft ( 3 ) . mod ( this . curve . q ) ;
214
+ var z3 = y1z1 . square ( ) . multiply ( y1z1 ) . shiftLeft ( 3 ) . mod ( this . curve . p ) ;
215
215
216
216
return new ECPointFp ( this . curve , this . curve . fromBigInteger ( x3 ) , this . curve . fromBigInteger ( y3 ) , z3 ) ;
217
217
}
@@ -293,16 +293,16 @@ ECPointFp.prototype.multiplyTwo = pointFpMultiplyTwo;
293
293
// ECCurveFp
294
294
295
295
// constructor
296
- function ECCurveFp ( q , a , b ) {
297
- this . q = q ;
296
+ function ECCurveFp ( p , a , b ) {
297
+ this . p = p ;
298
298
this . a = this . fromBigInteger ( a ) ;
299
299
this . b = this . fromBigInteger ( b ) ;
300
300
this . infinity = new ECPointFp ( this , null , null ) ;
301
- this . reducer = new Barrett ( this . q ) ;
301
+ this . reducer = new Barrett ( this . p ) ;
302
302
}
303
303
304
- function curveFpGetQ ( ) {
305
- return this . q ;
304
+ function curveFpgetP ( ) {
305
+ return this . p ;
306
306
}
307
307
308
308
function curveFpGetA ( ) {
@@ -317,15 +317,29 @@ function curveFpEquals(other) {
317
317
if ( other === this ) {
318
318
return true ;
319
319
}
320
- return ( this . q . equals ( other . q ) && this . a . equals ( other . a ) && this . b . equals ( other . b ) ) ;
320
+ return ( this . p . equals ( other . p ) && this . a . equals ( other . a ) && this . b . equals ( other . b ) ) ;
321
+ }
322
+
323
+ function curveFpContains ( pt ) {
324
+ // y^2 = x^3 + a*x + b mod p
325
+ var x = pt . getX ( ) . toBigInteger ( ) ,
326
+ y = pt . getY ( ) . toBigInteger ( ) ,
327
+ a = this . a . toBigInteger ( ) ,
328
+ b = this . b . toBigInteger ( ) ,
329
+ p = this . p ;
330
+
331
+ var left = y . pow ( 2 ) . mod ( p ) ,
332
+ right = x . pow ( 3 ) . add ( a . multiply ( x ) ) . add ( b ) . mod ( p )
333
+
334
+ return left . equals ( right ) ;
321
335
}
322
336
323
337
function curveFpGetInfinity ( ) {
324
338
return this . infinity ;
325
339
}
326
340
327
341
function curveFpFromBigInteger ( x ) {
328
- return new ECFieldElementFp ( this . q , x ) ;
342
+ return new ECFieldElementFp ( this . p , x ) ;
329
343
}
330
344
331
345
function curveReduce ( x ) {
@@ -364,7 +378,7 @@ function curveFpEncodePointHex(p) {
364
378
}
365
379
var xHex = p . getX ( ) . toBigInteger ( ) . toString ( 16 ) ;
366
380
var yHex = p . getY ( ) . toBigInteger ( ) . toString ( 16 ) ;
367
- var oLen = this . getQ ( ) . toString ( 16 ) . length ;
381
+ var oLen = this . getP ( ) . toString ( 16 ) . length ;
368
382
if ( ( oLen % 2 ) !== 0 ) {
369
383
oLen ++ ;
370
384
}
@@ -377,10 +391,11 @@ function curveFpEncodePointHex(p) {
377
391
return "04" + xHex + yHex ;
378
392
}
379
393
380
- ECCurveFp . prototype . getQ = curveFpGetQ ;
394
+ ECCurveFp . prototype . getP = curveFpgetP ;
381
395
ECCurveFp . prototype . getA = curveFpGetA ;
382
396
ECCurveFp . prototype . getB = curveFpGetB ;
383
397
ECCurveFp . prototype . equals = curveFpEquals ;
398
+ ECCurveFp . prototype . contains = curveFpContains ;
384
399
ECCurveFp . prototype . getInfinity = curveFpGetInfinity ;
385
400
ECCurveFp . prototype . fromBigInteger = curveFpFromBigInteger ;
386
401
ECCurveFp . prototype . reduce = curveReduce ;
0 commit comments