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