summaryrefslogtreecommitdiff
path: root/frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js
Unidiff
Diffstat (limited to 'frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js379
1 files changed, 379 insertions, 0 deletions
diff --git a/frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js b/frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js
new file mode 100644
index 0000000..634772a
--- a/dev/null
+++ b/frontend/delta/js/Clipperz/Crypto/ECC/BinaryField/Value.js
@@ -0,0 +1,379 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24//try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) {
25 //throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!";
26//}
27if (typeof(Clipperz) == 'undefined') { Clipperz = {}; }
28if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; }
29if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; }
30if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; }
31
32Clipperz.Crypto.ECC.BinaryField.Value = function(aValue, aBase, aBitSize) {
33 if (aValue.constructor == String) {
34 varvalue;
35 varstringLength;
36 var numberOfWords;
37 vari,c;
38
39 if (aBase != 16) {
40 throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase;
41 }
42
43 value = aValue.replace(/ /g, '');
44 stringLength = value.length;
45 numberOfWords = Math.ceil(stringLength / 8);
46 this._value = new Array(numberOfWords);
47
48 c = numberOfWords;
49 for (i=0; i<c; i++) {
50 varword;
51
52 if (i < (c-1)) {
53 word = parseInt(value.substr(stringLength-((i+1)*8), 8), 16);
54 } else {
55 word = parseInt(value.substr(0, stringLength-(i*8)), 16);
56 }
57
58 this._value[i] = word;
59 }
60 } else if (aValue.constructor == Array) {
61 var itemsToCopy;
62
63 itemsToCopy = aValue.length;
64 while (aValue[itemsToCopy - 1] == 0) {
65 itemsToCopy --;
66 }
67
68 this._value = aValue.slice(0, itemsToCopy);
69 } else if (aValue.constructor == Number) {
70 this._value = [aValue];
71 } else {
72 // throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType;
73 }
74
75 this._bitSize == aBitSize || null;
76
77 return this;
78}
79
80Clipperz.Crypto.ECC.BinaryField.Value.prototype = MochiKit.Base.update(null, {
81
82 'value': function() {
83 return this._value;
84 },
85
86 //-----------------------------------------------------------------------------
87
88 'wordSize': function() {
89 return this._value.length
90 },
91
92 //-----------------------------------------------------------------------------
93
94 'clone': function() {
95 return new Clipperz.Crypto.ECC.BinaryField.Value(this._value.slice(0), null, this._bitSize);
96 },
97
98 //-----------------------------------------------------------------------------
99
100 'isZero': function() {
101 return (this.compare(Clipperz.Crypto.ECC.BinaryField.Value.O) == 0);
102 },
103
104 //-----------------------------------------------------------------------------
105
106 'asString': function(aBase) {
107 varresult;
108 var i,c;
109
110 if (aBase != 16) {
111 throw Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase;
112 }
113
114 result = "";
115 c = this.wordSize();
116 for (i=0; i<c; i++) {
117 varwordAsString;
118
119 // wordAsString = ("00000000" + this.value()[i].toString(16));
120 wordAsString = ("00000000" + this._value[i].toString(16));
121 wordAsString = wordAsString.substring(wordAsString.length - 8);
122 result = wordAsString + result;
123 }
124
125 result = result.replace(/^(00)*/, "");
126
127 if (result == "") {
128 result = "0";
129 }
130
131 return result;
132 },
133
134 //-----------------------------------------------------------------------------
135
136 'shiftLeft': function(aNumberOfBitsToShift) {
137 //this method seems like it is never called. :-(
138 return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this._value, aNumberOfBitsToShift));
139 },
140
141 //-----------------------------------------------------------------------------
142
143 'bitSize': function() {
144 if (this._bitSize == null) {
145 this._bitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(this._value);
146 }
147
148 return this._bitSize;
149 },
150
151 //-----------------------------------------------------------------------------
152
153 'isBitSet': function(aBitPosition) {
154 return Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(this._value, aBitPosition);
155 },
156
157 //-----------------------------------------------------------------------------
158
159 'xor': function(aValue) {
160 return new Clipperz.Crypto.ECC.BinaryField.Value(Clipperz.Crypto.ECC.BinaryField.Value._xor(this._value, aValue._value));
161 },
162
163 //-----------------------------------------------------------------------------
164
165 'compare': function(aValue) {
166 return Clipperz.Crypto.ECC.BinaryField.Value._compare(this._value, aValue._value);
167 },
168
169 //-----------------------------------------------------------------------------
170 __syntaxFix__: "syntax fix"
171});
172
173Clipperz.Crypto.ECC.BinaryField.Value.O = new Clipperz.Crypto.ECC.BinaryField.Value('0', 16);
174Clipperz.Crypto.ECC.BinaryField.Value.I = new Clipperz.Crypto.ECC.BinaryField.Value('1', 16);
175
176Clipperz.Crypto.ECC.BinaryField.Value._xor = function(a, b, aFirstItemOffset) {
177 var result;
178 var resultSize;
179 var i,c;
180 var firstItemOffset;
181
182 firstItemOffset = aFirstItemOffset || 0;
183 resultSize = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset;
184
185 result = new Array(resultSize);
186
187 c = firstItemOffset;
188 for (i=0; i<c; i++) {
189 result[i] = a[i];
190 }
191
192 c = resultSize;
193 for (i=firstItemOffset; i<c; i++) {
194 result[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0);
195 }
196
197 return result;
198};
199
200Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor = function(a, b, aFirstItemOffset) {
201 var i,c;
202 var firstItemOffset;
203
204 firstItemOffset = aFirstItemOffset || 0;
205
206 c = Math.max((a.length - firstItemOffset), b.length) + firstItemOffset;
207 for (i=firstItemOffset; i<c; i++) {
208 a[i] = (((a[i] || 0) ^ (b[i - firstItemOffset] || 0)) >>> 0);
209 }
210};
211
212Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft = function(aWordArray, aNumberOfBitsToShift) {
213 var numberOfWordsToShift;
214 varnumberOfBitsToShift;
215 var result;
216 varoverflowValue;
217 var nextOverflowValue;
218 vari,c;
219
220 numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32);
221 numberOfBitsToShift = aNumberOfBitsToShift % 32;
222
223 result = new Array(aWordArray.length + numberOfWordsToShift);
224
225 c = numberOfWordsToShift;
226 for (i=0; i<c; i++) {
227 result[i] = 0;
228 }
229
230 overflowValue = 0;
231 nextOverflowValue = 0;
232
233 c = aWordArray.length;
234 for (i=0; i<c; i++) {
235 varvalue;
236 varresultWord;
237
238 // value = this.value()[i];
239 value = aWordArray[i];
240
241 if (numberOfBitsToShift > 0) {
242 nextOverflowValue = (value >>> (32 - numberOfBitsToShift));
243 value = value & (0xffffffff >>> numberOfBitsToShift);
244 resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0);
245 } else {
246 resultWord = value;
247 }
248
249 result[i+numberOfWordsToShift] = resultWord;
250 overflowValue = nextOverflowValue;
251 }
252
253 if (overflowValue != 0) {
254 result[aWordArray.length + numberOfWordsToShift] = overflowValue;
255 }
256
257 return result;
258};
259
260Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft = function(aWordArray, aNumberOfBitsToShift) {
261 var numberOfWordsToShift;
262 varnumberOfBitsToShift;
263 var result;
264 varoverflowValue;
265 vari,c;
266
267 numberOfWordsToShift = Math.floor(aNumberOfBitsToShift / 32);
268 numberOfBitsToShift = aNumberOfBitsToShift % 32;
269
270 result = new Array(aWordArray.length + numberOfWordsToShift);
271
272 c = numberOfWordsToShift;
273 for (i=0; i<c; i++) {
274 result[i] = 0;
275 }
276
277 overflowValue = 0;
278 nextOverflowValue = 0;
279
280 c = aWordArray.length;
281 for (i=0; i<c; i++) {
282 varvalue;
283 varresultWord;
284
285 // value = this.value()[i];
286 value = aWordArray[i];
287
288 if (numberOfBitsToShift > 0) {
289 var nextOverflowValue;
290
291 nextOverflowValue = (value >>> (32 - numberOfBitsToShift));
292 value = value & (0xffffffff >>> numberOfBitsToShift);
293 resultWord = (((value << numberOfBitsToShift) | overflowValue) >>> 0);
294 } else {
295 resultWord = value;
296 }
297
298 result[i+numberOfWordsToShift] = resultWord;
299 overflowValue = nextOverflowValue;
300 }
301
302 if (overflowValue != 0) {
303 result[aWordArray.length + numberOfWordsToShift] = overflowValue;
304 }
305
306 return result;
307};
308
309Clipperz.Crypto.ECC.BinaryField.Value._bitSize = function(aWordArray) {
310 varresult;
311 varnotNullElements;
312 var mostValuableWord;
313 var matchingBitsInMostImportantWord;
314 var mask;
315 var i,c;
316
317 notNullElements = aWordArray.length;
318
319 if ((aWordArray.length == 1) && (aWordArray[0] == 0)) {
320 result = 0;
321 } else {
322 notNullElements --;
323 while((notNullElements > 0) && (aWordArray[notNullElements] == 0)) {
324 notNullElements --;
325 }
326
327 result = notNullElements * 32;
328 mostValuableWord = aWordArray[notNullElements];
329
330 matchingBits = 32;
331 mask = 0x80000000;
332
333 while ((matchingBits > 0) && ((mostValuableWord & mask) == 0)) {
334 matchingBits --;
335 mask >>>= 1;
336 }
337
338 result += matchingBits;
339 }
340
341 return result;
342};
343
344Clipperz.Crypto.ECC.BinaryField.Value._isBitSet = function(aWordArray, aBitPosition) {
345 var result;
346 varbyteIndex;
347 var bitIndexInSelectedByte;
348
349 byteIndex = Math.floor(aBitPosition / 32);
350 bitIndexInSelectedByte = aBitPosition % 32;
351
352 if (byteIndex <= aWordArray.length) {
353 result = ((aWordArray[byteIndex] & (1 << bitIndexInSelectedByte)) != 0);
354 } else {
355 result = false;
356 }
357
358 return result;
359};
360
361Clipperz.Crypto.ECC.BinaryField.Value._compare = function(a,b) {
362 varresult;
363 var i,c;
364
365 result = MochiKit.Base.compare(a.length, b.length);
366
367 c = a.length;
368 for (i=0; (i<c) && (result==0); i++) {
369 result = MochiKit.Base.compare(a[c-i-1], b[c-i-1]);
370 }
371
372 return result;
373};
374
375
376Clipperz.Crypto.ECC.BinaryField.Value['exception']= {
377 'UnsupportedBase': new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedBase"),
378 'UnsupportedConstructorValueType':new MochiKit.Base.NamedError("Clipperz.Crypto.ECC.BinaryField.Value.exception.UnsupportedConstructorValueType")
379};