summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js
Unidiff
Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js83
1 files changed, 18 insertions, 65 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js b/frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js
index 19f19c2..0d76b9c 100644
--- a/frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js
+++ b/frontend/gamma/js/Clipperz/Crypto/ECC/BinaryField/Curve.js
@@ -1,25 +1,23 @@
1/* 1/*
2 2
3Copyright 2008-2011 Clipperz Srl 3Copyright 2008-2013 Clipperz Srl
4 4
5This file is part of Clipperz Community Edition. 5This file is part of Clipperz, the online password manager.
6Clipperz Community Edition is an online password manager.
7For further information about its features and functionalities please 6For further information about its features and functionalities please
8refer to http://www.clipperz.com. 7refer to http://www.clipperz.com.
9 8
10* Clipperz Community Edition is free software: you can redistribute 9* Clipperz is free software: you can redistribute it and/or modify it
11 it and/or modify it under the terms of the GNU Affero General Public 10 under the terms of the GNU Affero General Public License as published
12 License as published by the Free Software Foundation, either version 11 by the Free Software Foundation, either version 3 of the License, or
13 3 of the License, or (at your option) any later version. 12 (at your option) any later version.
14 13
15* Clipperz Community Edition is distributed in the hope that it will 14* Clipperz is distributed in the hope that it will be useful, but
16 be useful, but WITHOUT ANY WARRANTY; without even the implied 15 WITHOUT ANY WARRANTY; without even the implied warranty of
17 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU Affero General Public License for more details. 17 See the GNU Affero General Public License for more details.
19 18
20* You should have received a copy of the GNU Affero General Public 19* You should have received a copy of the GNU Affero General Public
21 License along with Clipperz Community Edition. If not, see 20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
22 <http://www.gnu.org/licenses/>.
23 21
24*/ 22*/
25 23
@@ -102,22 +100,13 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
102 'add': function(aPointA, aPointB) { 100 'add': function(aPointA, aPointB) {
103 var result; 101 var result;
104 102
105//console.log(">>> ECC.BinaryField.Curve.add");
106 if (aPointA.isZero()) { 103 if (aPointA.isZero()) {
107//console.log("--- pointA == zero");
108 result = aPointB; 104 result = aPointB;
109 } else if (aPointB.isZero()) { 105 } else if (aPointB.isZero()) {
110//console.log("--- pointB == zero");
111 result = aPointA; 106 result = aPointA;
112 } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { 107 } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) {
113//console.log("compare A.x - B.x: ", aPointA.x().compare(aPointB.x()));
114//console.log("compare A.y - B.y: ", (aPointA.y().compare(aPointB.y()) != 0));
115//console.log("compare B.x.isZero(): ", aPointB.x().isZero());
116
117//console.log("--- result = zero");
118 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); 108 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O});
119 } else { 109 } else {
120//console.log("--- result = ELSE");
121 varf2m; 110 varf2m;
122 var x, y; 111 var x, y;
123 var lambda; 112 var lambda;
@@ -131,7 +120,6 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
131 f2m = this.finiteField(); 120 f2m = this.finiteField();
132 121
133 if (aPointA.x().compare(aPointB.x()) != 0) { 122 if (aPointA.x().compare(aPointB.x()) != 0) {
134//console.log(" a.x != b.x");
135 lambda =f2m._fastMultiply( 123 lambda =f2m._fastMultiply(
136 f2m._add(aY, bY), 124 f2m._add(aY, bY),
137 f2m._inverse(f2m._add(aX, bX)) 125 f2m._inverse(f2m._add(aX, bX))
@@ -141,25 +129,17 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
141 f2m._overwriteAdd(x, aX); 129 f2m._overwriteAdd(x, aX);
142 f2m._overwriteAdd(x, bX); 130 f2m._overwriteAdd(x, bX);
143 } else { 131 } else {
144//console.log(" a.x == b.x");
145 lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); 132 lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX)));
146//console.log(" lambda: " + lambda.asString(16));
147 x = f2m._add(this.a()._value, f2m._square(lambda)); 133 x = f2m._add(this.a()._value, f2m._square(lambda));
148//console.log(" x (step 1): " + x.asString(16));
149 f2m._overwriteAdd(x, lambda); 134 f2m._overwriteAdd(x, lambda);
150//console.log(" x (step 2): " + x.asString(16));
151 } 135 }
152 136
153 y = f2m._fastMultiply(f2m._add(bX, x), lambda); 137 y = f2m._fastMultiply(f2m._add(bX, x), lambda);
154//console.log(" y (step 1): " + y.asString(16));
155 f2m._overwriteAdd(y, x); 138 f2m._overwriteAdd(y, x);
156//console.log(" y (step 2): " + y.asString(16));
157 f2m._overwriteAdd(y, bY); 139 f2m._overwriteAdd(y, bY);
158//console.log(" y (step 3): " + y.asString(16));
159 140
160 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) 141 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)})
161 } 142 }
162//console.log("<<< ECC.BinaryField.Curve.add");
163 143
164 return result; 144 return result;
165 }, 145 },
@@ -197,7 +177,6 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
197 f2m = this.finiteField(); 177 f2m = this.finiteField();
198 178
199 if (aPointA.x().compare(aPointB.x()) != 0) { 179 if (aPointA.x().compare(aPointB.x()) != 0) {
200//console.log(" a.x != b.x");
201 lambda =f2m._fastMultiply( 180 lambda =f2m._fastMultiply(
202 f2m._add(aY, bY), 181 f2m._add(aY, bY),
203 f2m._inverse(f2m._add(aX, bX)) 182 f2m._inverse(f2m._add(aX, bX))
@@ -207,28 +186,20 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
207 f2m._overwriteAdd(x, aX); 186 f2m._overwriteAdd(x, aX);
208 f2m._overwriteAdd(x, bX); 187 f2m._overwriteAdd(x, bX);
209 } else { 188 } else {
210//console.log(" a.x == b.x");
211 lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); 189 lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX)));
212//console.log(" lambda: " + lambda.asString(16));
213 x = f2m._add(this.a()._value, f2m._square(lambda)); 190 x = f2m._add(this.a()._value, f2m._square(lambda));
214//console.log(" x (step 1): " + x.asString(16));
215 f2m._overwriteAdd(x, lambda); 191 f2m._overwriteAdd(x, lambda);
216//console.log(" x (step 2): " + x.asString(16));
217 } 192 }
218 193
219 y = f2m._fastMultiply(f2m._add(bX, x), lambda); 194 y = f2m._fastMultiply(f2m._add(bX, x), lambda);
220//console.log(" y (step 1): " + y.asString(16));
221 f2m._overwriteAdd(y, x); 195 f2m._overwriteAdd(y, x);
222//console.log(" y (step 2): " + y.asString(16));
223 f2m._overwriteAdd(y, bY); 196 f2m._overwriteAdd(y, bY);
224//console.log(" y (step 3): " + y.asString(16));
225 197
226 // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) 198 // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)})
227 aPointA._x._value = x; 199 aPointA._x._value = x;
228 aPointA._y._value = y; 200 aPointA._y._value = y;
229 201
230 } 202 }
231//console.log("<<< ECC.BinaryField.Curve.add");
232 203
233 return result; 204 return result;
234 }, 205 },
@@ -250,13 +221,11 @@ Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, {
250 k = aValue; 221 k = aValue;
251 Q = aPoint; 222 Q = aPoint;
252 } else { 223 } else {
253MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!"); 224 Clipperz.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!");
254 k = aValue.negate(); 225 k = aValue.negate();
255 Q = this.negate(aPoint); 226 Q = this.negate(aPoint);
256 } 227 }
257 228
258//console.log("k: " + k.toString(16));
259//console.log("k.bitSize: " + k.bitSize());
260 for (i=k.bitSize()-1; i>=0; i--) { 229 for (i=k.bitSize()-1; i>=0; i--) {
261 result = this.add(result, result); 230 result = this.add(result, result);
262 // this.overwriteAdd(result, result); 231 // this.overwriteAdd(result, result);
@@ -265,7 +234,7 @@ MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not w
265 // this.overwriteAdd(result, Q); 234 // this.overwriteAdd(result, Q);
266 } 235 }
267 236
268 // if (countIndex==100) {console.log("multiply.break"); break;} else countIndex++; 237 // if (countIndex==100) {Clipperz.log("multiply.break"); break;} else countIndex++;
269 } 238 }
270 } 239 }
271//console.profileEnd(); 240//console.profileEnd();
@@ -279,14 +248,14 @@ MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not w
279 var deferredResult; 248 var deferredResult;
280 var result; 249 var result;
281 250
282MochiKit.Logging.logDebug(">>> deferredMultiply - value: " + aValue + ", point: " + aPoint); 251Clipperz.log(">>> deferredMultiply - value: " + aValue + ", point: " + aPoint);
283//console.profile("ECC.Curve.multiply"); 252//console.profile("ECC.Curve.multiply");
284 deferredResult = new MochiKit.Async.Deferred(); 253 deferredResult = new MochiKit.Async.Deferred();
285//deferredResult.addCallback(function(res) {console.profile("ECC.Curve.deferredMultiply"); return res;} ); 254//deferredResult.addCallback(function(res) {console.profile("ECC.Curve.deferredMultiply"); return res;} );
286//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 1: " + res); return res;}); 255//deferredResult.addBoth(function(res) {Clipperz.logDebug("# 1: " + res); return res;});
287 256
288 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); 257 result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O});
289//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 2: " + res); return res;}); 258//deferredResult.addBoth(function(res) {Clipperz.logDebug("# 2: " + res); return res;});
290 259
291 if (aValue.isZero() == false) { 260 if (aValue.isZero() == false) {
292 var k, Q; 261 var k, Q;
@@ -297,18 +266,13 @@ MochiKit.Logging.logDebug(">>> deferredMultiply - value: " + aValue + ", point:
297 k = aValue; 266 k = aValue;
298 Q = aPoint; 267 Q = aPoint;
299 } else { 268 } else {
300MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!"); 269 Clipperz.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not work with negative values!!!!");
301 k = aValue.negate(); 270 k = aValue.negate();
302 Q = this.negate(aPoint); 271 Q = this.negate(aPoint);
303 } 272 }
304 273
305//console.log("k: " + k.toString(16));
306//console.log("k.bitSize: " + k.bitSize());
307 274
308//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 3: " + res); return res;});
309 for (i=k.bitSize()-1; i>=0; i--) { 275 for (i=k.bitSize()-1; i>=0; i--) {
310//MochiKit.Logging.logDebug("====> " + i);
311//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 4 > i = " + i + ": " + res); return res;});
312 deferredResult.addMethod(this, "addTwice"); 276 deferredResult.addMethod(this, "addTwice");
313 //# result = this.add(result, result); 277 //# result = this.add(result, result);
314 // this.overwriteAdd(result, result); 278 // this.overwriteAdd(result, result);
@@ -318,16 +282,10 @@ MochiKit.Logging.logError("The Clipperz.Crypto.ECC.BinaryFields.Value does not w
318 // this.overwriteAdd(result, Q); 282 // this.overwriteAdd(result, Q);
319 } 283 }
320 if (i%20 == 0) {deferredResult.addCallback(MochiKit.Async.wait, 0.1);} 284 if (i%20 == 0) {deferredResult.addCallback(MochiKit.Async.wait, 0.1);}
321
322 // if (countIndex==100) {console.log("multiply.break"); break;} else countIndex++;
323//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 4 < i = " + i + ": " + res); return res;});
324 } 285 }
325//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 4: " + res); return res;});
326 } 286 }
327//#console.profileEnd(); 287//#console.profileEnd();
328//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 5: " + res); return res;});
329//deferredResult.addBoth(function(res) {console.profileEnd(); return res;}); 288//deferredResult.addBoth(function(res) {console.profileEnd(); return res;});
330//deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("# 6: " + res); return res;});
331 deferredResult.callback(result); 289 deferredResult.callback(result);
332 290
333 //# return result; 291 //# return result;
@@ -441,14 +399,12 @@ MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, {
441 varresult; 399 varresult;
442 400
443 if (aValue.bitSize() > 1140) { 401 if (aValue.bitSize() > 1140) {
444 MochiKit.Logging.logWarning("ECC.StandarCurves.B571.finiteField().module: falling back to default implementation"); 402 Clipperz.logWarning("ECC.StandarCurves.B571.finiteField().module: falling back to default implementation");
445 result = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule(aValue); 403 result = Clipperz.Crypto.ECC.StandardCurves._B571.finiteField().slowModule(aValue);
446 } else { 404 } else {
447 varC, T; 405 varC, T;
448 var i; 406 var i;
449 407
450//console.log(">>> binaryField.finiteField.(improved)module");
451 // C = aValue.value().slice(0);
452 C = aValue._value.slice(0); 408 C = aValue._value.slice(0);
453 for (i=35; i>=18; i--) { 409 for (i=35; i>=18; i--) {
454 T = C[i]; 410 T = C[i];
@@ -464,7 +420,6 @@ MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, {
464 } 420 }
465 421
466 result = new Clipperz.Crypto.ECC.BinaryField.Value(C); 422 result = new Clipperz.Crypto.ECC.BinaryField.Value(C);
467//console.log("<<< binaryField.finiteField.(improved)module");
468 } 423 }
469 424
470 return result; 425 return result;
@@ -507,13 +462,12 @@ MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, {
507 varresult; 462 varresult;
508 463
509 if (aValue.bitSize() > 564) { 464 if (aValue.bitSize() > 564) {
510 MochiKit.Logging.logWarning("ECC.StandarCurves.B283.finiteField().module: falling back to default implementation"); 465 Clipperz.logWarning("ECC.StandarCurves.B283.finiteField().module: falling back to default implementation");
511 result = Clipperz.Crypto.ECC.StandardCurves._B283.finiteField().slowModule(aValue); 466 result = Clipperz.Crypto.ECC.StandardCurves._B283.finiteField().slowModule(aValue);
512 } else { 467 } else {
513 varC, T; 468 varC, T;
514 var i; 469 var i;
515 470
516//console.log(">>> binaryField.finiteField.(improved)module");
517 C = aValue._value.slice(0); 471 C = aValue._value.slice(0);
518 for (i=17; i>=9; i--) { 472 for (i=17; i>=9; i--) {
519 T = C[i]; 473 T = C[i];
@@ -529,7 +483,6 @@ MochiKit.Base.update(Clipperz.Crypto.ECC.StandardCurves, {
529 } 483 }
530 484
531 result = new Clipperz.Crypto.ECC.BinaryField.Value(C); 485 result = new Clipperz.Crypto.ECC.BinaryField.Value(C);
532//console.log("<<< binaryField.finiteField.(improved)module");
533 } 486 }
534 487
535 return result; 488 return result;