|
14 | 14 | // Ported loosely from BouncyCastle's Java EC code
|
15 | 15 | // Only Fp curves implemented for now
|
16 | 16 |
|
17 |
| -// Requires jsbn.js and jsbn2.js |
18 |
| -var jsbn = require("jsbn"); |
| 17 | +var BigInteger = require("../../deps/forge").jsbn.BigInteger; |
19 | 18 |
|
20 |
| -var BigInteger = jsbn.BigInteger, |
21 |
| - Barrett = BigInteger.prototype.Barrett; |
| 19 | +// ---------------- |
| 20 | +// Helpers |
| 21 | + |
| 22 | +function nbi() { |
| 23 | + return new BigInteger(null); |
| 24 | +} |
| 25 | + |
| 26 | +// ---------------- |
| 27 | +// Barrett modular reduction |
| 28 | + |
| 29 | +// constructor |
| 30 | +function Barrett(m) { |
| 31 | + // setup Barrett |
| 32 | + this.r2 = nbi(); |
| 33 | + this.q3 = nbi(); |
| 34 | + BigInteger.ONE.dlShiftTo(2*m.t,this.r2); |
| 35 | + this.mu = this.r2.divide(m); |
| 36 | + this.m = m; |
| 37 | +} |
| 38 | + |
| 39 | +function barrettConvert(x) { |
| 40 | + if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m); |
| 41 | + else if(x.compareTo(this.m) < 0) return x; |
| 42 | + else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; } |
| 43 | +} |
| 44 | + |
| 45 | +function barrettRevert(x) { return x; } |
| 46 | + |
| 47 | +// x = x mod m (HAC 14.42) |
| 48 | +function barrettReduce(x) { |
| 49 | + x.drShiftTo(this.m.t-1,this.r2); |
| 50 | + if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); } |
| 51 | + this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3); |
| 52 | + this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2); |
| 53 | + while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1); |
| 54 | + x.subTo(this.r2,x); |
| 55 | + while(x.compareTo(this.m) >= 0) x.subTo(this.m,x); |
| 56 | +} |
| 57 | + |
| 58 | +// r = x^2 mod m; x != r |
| 59 | +function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); } |
| 60 | + |
| 61 | +// r = x*y mod m; x,y != r |
| 62 | +function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); } |
| 63 | + |
| 64 | +Barrett.prototype.convert = barrettConvert; |
| 65 | +Barrett.prototype.revert = barrettRevert; |
| 66 | +Barrett.prototype.reduce = barrettReduce; |
| 67 | +Barrett.prototype.mulTo = barrettMulTo; |
| 68 | +Barrett.prototype.sqrTo = barrettSqrTo; |
22 | 69 |
|
23 | 70 | // ----------------
|
24 | 71 | // ECFieldElementFp
|
@@ -58,7 +105,7 @@ function feFpMultiply(b) {
|
58 | 105 | }
|
59 | 106 |
|
60 | 107 | function feFpSquare() {
|
61 |
| - return new ECFieldElementFp(this.p, this.x.square().mod(this.p)); |
| 108 | + return new ECFieldElementFp(this.p, this.x.pow(2).mod(this.p)); |
62 | 109 | }
|
63 | 110 |
|
64 | 111 | function feFpDivide(b) {
|
@@ -167,10 +214,10 @@ function pointFpAdd(b) {
|
167 | 214 | var x1 = this.x.toBigInteger();
|
168 | 215 | var y1 = this.y.toBigInteger();
|
169 | 216 |
|
170 |
| - var v2 = v.square(); |
| 217 | + var v2 = v.pow(2); |
171 | 218 | var v3 = v2.multiply(v);
|
172 | 219 | var x1v2 = x1.multiply(v2);
|
173 |
| - var zu2 = u.square().multiply(this.z); |
| 220 | + var zu2 = u.pow(2).multiply(this.z); |
174 | 221 |
|
175 | 222 | // x3 = v * (z2 * (z1 * u^2 - 2 * x1 * v^2) - v^3)
|
176 | 223 | var x3 = zu2.subtract(x1v2.shiftLeft(1)).multiply(b.z).subtract(v3).multiply(v).mod(this.curve.p);
|
@@ -200,18 +247,18 @@ function pointFpTwice() {
|
200 | 247 | var a = this.curve.a.toBigInteger();
|
201 | 248 |
|
202 | 249 | // w = 3 * x1^2 + a * z1^2
|
203 |
| - var w = x1.square().multiply(THREE); |
| 250 | + var w = x1.pow(2).multiply(THREE); |
204 | 251 | if (!BigInteger.ZERO.equals(a)) {
|
205 |
| - w = w.add(this.z.square().multiply(a)); |
| 252 | + w = w.add(this.z.pow(2).multiply(a)); |
206 | 253 | }
|
207 | 254 | w = w.mod(this.curve.p);
|
208 | 255 | //this.curve.reduce(w);
|
209 | 256 | // 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.p); |
| 257 | + var x3 = w.pow(2).subtract(x1.shiftLeft(3).multiply(y1sqz1)).shiftLeft(1).multiply(y1z1).mod(this.curve.p); |
211 | 258 | // 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.p); |
| 259 | + var y3 = w.multiply(THREE).multiply(x1).subtract(y1sqz1.shiftLeft(1)).shiftLeft(2).multiply(y1sqz1).subtract(w.pow(2).multiply(w)).mod(this.curve.p); |
213 | 260 | // z3 = 8 * (y1 * z1)^3
|
214 |
| - var z3 = y1z1.square().multiply(y1z1).shiftLeft(3).mod(this.curve.p); |
| 261 | + var z3 = y1z1.pow(2).multiply(y1z1).shiftLeft(3).mod(this.curve.p); |
215 | 262 |
|
216 | 263 | return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3);
|
217 | 264 | }
|
|
0 commit comments