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