-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.js
216 lines (188 loc) · 5.48 KB
/
ec2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Extension of Elliptic Curve implementation JSBN
// to support compression and decompression of EC
// points.
// Ported loosely from BouncyCastle's Java EC code
// like the original JSBN.
// Requires jsbn.js, jsbn2.js, rng.js and ec.js
// http://www-cs-students.stanford.edu/~tjw/jsbn/
ECCurveFp.prototype.decodePointHex = function(s)
{
var yIsEven;
switch(parseInt(s.substr(0,2), 16)) { // first byte
case 0:
return this.infinity;
case 2:
yIsEven = false;
case 3:
if(yIsEven == undefined) yIsEven = true;
var len = s.length - 2;
var xHex = s.substr(2, len);
var x = this.fromBigInteger(new BigInteger(xHex,16));
var alpha = x.multiply(x.square().add(this.getA())).add(this.getB());
var beta = alpha.sqrt();
if (beta == null) throw "Invalid point compression";
var betaValue = beta.toBigInteger();
if (betaValue.testBit(0) != yIsEven)
{
// Use the other root
beta = this.fromBigInteger(this.getQ().subtract(betaValue));
}
return new ECPointFp(this,x,beta);
case 4:
case 6:
case 7:
var len = (s.length - 2) / 2;
var xHex = s.substr(2, len);
var yHex = s.substr(len+2, len);
return new ECPointFp(this,
this.fromBigInteger(new BigInteger(xHex, 16)),
this.fromBigInteger(new BigInteger(yHex, 16)));
default: // unsupported
return null;
}
}
ECCurveFp.prototype.encodeCompressedPointHex = function(p)
{
if (p.isInfinity()) return "00";
var xHex = p.getX().toBigInteger().toString(16);
var oLen = this.getQ().toString(16).length;
if ((oLen % 2) != 0) oLen++;
while (xHex.length < oLen)
xHex = "0" + xHex;
var yPrefix;
if(p.getY().toBigInteger().isEven()) yPrefix = "02";
else yPrefix = "03";
return yPrefix + xHex;
}
ECFieldElementFp.prototype.getR = function()
{
if(this.r != undefined) return this.r;
this.r = null;
var bitLength = this.q.bitLength();
if (bitLength > 128)
{
var firstWord = this.q.shiftRight(bitLength - 64);
if (firstWord.intValue() == -1)
{
this.r = BigInteger.ONE.shiftLeft(bitLength).subtract(this.q);
}
}
return this.r;
}
ECFieldElementFp.prototype.modMult = function(x1,x2)
{
return this.modReduce(x1.multiply(x2));
}
ECFieldElementFp.prototype.modReduce = function(x)
{
if (this.getR() != null)
{
var qLen = q.bitLength();
while (x.bitLength() > (qLen + 1))
{
var u = x.shiftRight(qLen);
var v = x.subtract(u.shiftLeft(qLen));
if (!this.getR().equals(BigInteger.ONE))
{
u = u.multiply(this.getR());
}
x = u.add(v);
}
while (x.compareTo(q) >= 0)
{
x = x.subtract(q);
}
}
else
{
x = x.mod(q);
}
return x;
}
ECFieldElementFp.prototype.sqrt = function()
{
if (!this.q.testBit(0)) throw "unsupported";
// p mod 4 == 3
if (this.q.testBit(1))
{
var z = new ECFieldElementFp(this.q,this.x.modPow(this.q.shiftRight(2).add(BigInteger.ONE),this.q));
return z.square().equals(this) ? z : null;
}
// p mod 4 == 1
var qMinusOne = this.q.subtract(BigInteger.ONE);
var legendreExponent = qMinusOne.shiftRight(1);
if (!(this.x.modPow(legendreExponent, this.q).equals(BigInteger.ONE)))
{
return null;
}
var u = qMinusOne.shiftRight(2);
var k = u.shiftLeft(1).add(BigInteger.ONE);
var Q = this.x;
var fourQ = modDouble(modDouble(Q));
var U, V;
do
{
var P;
do
{
P = new BigInteger(this.q.bitLength(), new SecureRandom());
}
while (P.compareTo(this.q) >= 0
|| !(P.multiply(P).subtract(fourQ).modPow(legendreExponent, this.q).equals(qMinusOne)));
var result = this.lucasSequence(P, Q, k);
U = result[0];
V = result[1];
if (this.modMult(V, V).equals(fourQ))
{
// Integer division by 2, mod q
if (V.testBit(0))
{
V = V.add(q);
}
V = V.shiftRight(1);
return new ECFieldElementFp(q,V);
}
}
while (U.equals(BigInteger.ONE) || U.equals(qMinusOne));
return null;
}
ECFieldElementFp.prototype.lucasSequence = function(P,Q,k)
{
var n = k.bitLength();
var s = k.getLowestSetBit();
var Uh = BigInteger.ONE;
var Vl = BigInteger.TWO;
var Vh = P;
var Ql = BigInteger.ONE;
var Qh = BigInteger.ONE;
for (var j = n - 1; j >= s + 1; --j)
{
Ql = this.modMult(Ql, Qh);
if (k.testBit(j))
{
Qh = this.modMult(Ql, Q);
Uh = this.modMult(Uh, Vh);
Vl = this.modReduce(Vh.multiply(Vl).subtract(P.multiply(Ql)));
Vh = this.modReduce(Vh.multiply(Vh).subtract(Qh.shiftLeft(1)));
}
else
{
Qh = Ql;
Uh = this.modReduce(Uh.multiply(Vl).subtract(Ql));
Vh = this.modReduce(Vh.multiply(Vl).subtract(P.multiply(Ql)));
Vl = this.modReduce(Vl.multiply(Vl).subtract(Ql.shiftLeft(1)));
}
}
Ql = this.modMult(Ql, Qh);
Qh = this.modMult(Ql, Q);
Uh = this.modReduce(Uh.multiply(Vl).subtract(Ql));
Vl = this.modReduce(Vh.multiply(Vl).subtract(P.multiply(Ql)));
Ql = this.modMult(Ql, Qh);
for (var j = 1; j <= s; ++j)
{
Uh = this.modMult(Uh, Vl);
Vl = this.modReduce(Vl.multiply(Vl).subtract(Ql.shiftLeft(1)));
Ql = this.modMult(Ql, Ql);
}
return [ Uh, Vl ];
}