forked from iotaledger/kerl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
executable file
·652 lines (439 loc) · 11.9 KB
/
converter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
var BN = require('bn.js');
/**
*
* Conversion functions
*
**/
var RADIX = 3;
var RADIX_BYTES = 256;
var MAX_TRIT_VALUE = 1;
var MIN_TRIT_VALUE = -1;
var BYTE_HASH_LENGTH = 48;
// All possible tryte values
var trytesAlphabet = "9ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// map of all trits representations
var trytesTrits = [
[ 0, 0, 0],
[ 1, 0, 0],
[-1, 1, 0],
[ 0, 1, 0],
[ 1, 1, 0],
[-1, -1, 1],
[ 0, -1, 1],
[ 1, -1, 1],
[-1, 0, 1],
[ 0, 0, 1],
[ 1, 0, 1],
[-1, 1, 1],
[ 0, 1, 1],
[ 1, 1, 1],
[-1, -1, -1],
[ 0, -1, -1],
[ 1, -1, -1],
[-1, 0, -1],
[ 0, 0, -1],
[ 1, 0, -1],
[-1, 1, -1],
[ 0, 1, -1],
[ 1, 1, -1],
[-1, -1, 0],
[ 0, -1, 0],
[ 1, -1, 0],
[-1, 0, 0]
];
/**
* Converts trytes into trits
*
* @method trits
* @param {String|Int} input Tryte value to be converted. Can either be string or int
* @param {Array} state (optional) state to be modified
* @returns {Array} trits
**/
var trits = function( input, state ) {
var trits = state || [];
if (Number.isInteger(input)) {
var absoluteValue = input < 0 ? -input : input;
while (absoluteValue > 0) {
var remainder = absoluteValue % 3;
absoluteValue = Math.floor(absoluteValue / 3);
if (remainder > 1) {
remainder = -1;
absoluteValue++;
}
trits[trits.length] = remainder;
}
if (input < 0) {
for (var i = 0; i < trits.length; i++) {
trits[i] = -trits[i];
}
}
} else {
for (var i = 0; i < input.length; i++) {
var index = trytesAlphabet.indexOf(input.charAt(i));
trits[i * 3] = trytesTrits[index][0];
trits[i * 3 + 1] = trytesTrits[index][1];
trits[i * 3 + 2] = trytesTrits[index][2];
}
}
return trits;
}
/**
* Converts trits into trytes
*
* @method trytes
* @param {Array} trits
* @returns {String} trytes
**/
var trytes = function(trits) {
var trytes = "";
for ( var i = 0; i < trits.length; i += 3 ) {
// Iterate over all possible tryte values to find correct trit representation
for ( var j = 0; j < trytesAlphabet.length; j++ ) {
if ( trytesTrits[ j ][ 0 ] === trits[ i ] && trytesTrits[ j ][ 1 ] === trits[ i + 1 ] && trytesTrits[ j ][ 2 ] === trits[ i + 2 ] ) {
trytes += trytesAlphabet.charAt( j );
break;
}
}
}
return trytes;
}
/**
* Converts trits into an integer value
*
* @method value
* @param {Array} trits
* @returns {int} value
**/
var value = function(trits) {
var returnValue = 0;
for ( var i = trits.length; i-- > 0; ) {
returnValue = returnValue * 3 + trits[ i ];
}
return returnValue;
}
/**
* Converts an integer value to trits
*
* @method value
* @param {Int} value
* @returns {Array} trits
**/
var fromValue = function(value) {
var destination = [];
var absoluteValue = value < 0 ? -value : value;
var i = 0;
while( absoluteValue > 0 ) {
var remainder = ( absoluteValue % RADIX );
absoluteValue = Math.floor( absoluteValue / RADIX );
if ( remainder > MAX_TRIT_VALUE ) {
remainder = MIN_TRIT_VALUE;
absoluteValue++;
}
destination[ i ] = remainder;
i++;
}
if ( value < 0 ) {
for ( var j = 0; j < destination.length; j++ ) {
// switch values
destination[ j ] = destination[ j ] === 0 ? 0: -destination[ j ];
}
}
return destination;
}
/**
* Converts trits into big integer
*
* @method bigIntFromTrits
* @param {Array} trits
* @returns {String} BN
**/
var bigIntFromTrits = function( trits ) {
var value = new BN( 0 );
for (var i = trits.length; i-- > 0; ) {
// Multiply by 3 and add the respective trit value
value = value.mul( new BN( 3 ) ).add( new BN( trits[ i ] ) );
}
// Return byte array
return value.toString( );
}
/**
* Converts a big integer value into trits
*
* @method tritsFromBytes
* @param {Number || String} value
* @param {Number} length
* @returns {Array} trits
**/
var tritsFromBigInt = function( value, length ) {
// if no length provide, set default value
if (!length) length = 243;
var destination = new Array( length ).fill(0);
value = new BN( value );
var absoluteValue = value.abs( );
var i = 0;
while ( absoluteValue.gt( new BN( 0 ) ) ) {
// Remainder is the modulo of 3 (radix)
var remainder = absoluteValue.mod( new BN( RADIX ) ).toNumber();
// Divide the absoluteValue by 3 (radix)
absoluteValue = absoluteValue.div( new BN( RADIX ) );
if ( remainder > MAX_TRIT_VALUE ) {
remainder = MIN_TRIT_VALUE;
absoluteValue = absoluteValue.add( new BN( 1 ) );
}
destination[ i ] = remainder;
i++;
}
if ( value.isNeg() ) {
for ( var i = 0; i < destination.length; i++ ) {
destination[ i ] = destination[ i ] === 0 ? 0 : -destination[ i ];
}
}
return destination;
}
/**
* Converts a big integer value into bytes
*
* @method bytesFromBigInt
* @param {String} value
* @returns {Array} bytes
**/
var bytesFromBigInt = function ( value ) {
var bigInt = new BN( value );
var byteArrayTemp = [];
for ( var i = 0; i < 48; i++ ) {
// right shift
var rsh = bigInt.abs( ).shrn( i * 8 );
var last = byteArrayTemp.length - 1;
// if zero or 128
if ( !rsh.isZero( ) || ( byteArrayTemp[ last ] & 0x80 ) ) {
byteArrayTemp.push( rsh.mod( new BN( 1 << 8 ) ).toNumber( ) );
}
}
var byteArray = byteArrayTemp.reverse( ).map( function ( x ) {
// smaller than or equal to 127
if ( x <= 0x7F ) {
return x;
}
else {
// subtract by 256
return x - 0x100;
}
});
// If negative, reverse
if ( bigInt.isNeg() ) {
byteArray = byteArray.map( function ( x ) {
// Bitwise OR https://www.w3schools.com/jsref/jsref_operators.aspnot
return ~x;
});
byteArray = byteArray.reverse( );
for ( var i = 0; i < byteArray.length; i++ ) {
var add = (byteArray[i] & 0xFF) + 1;
if ( add <= 0x7F ) {
byteArray[ i ] = add;
}
else {
byteArray[ i ] = add - 0x100;
}
if ( byteArray[ i ] !== 0 ) {
break;
}
}
byteArray = byteArray.reverse( );
}
var result = [ ];
var i = 0;
while ( i + byteArray.length < BYTE_HASH_LENGTH ) {
result[ i++ ] = byteArray[ 0 ] < 0 ? -1 : 0;
}
var j = byteArray.length;
for ( ; j-- > 0; ) {
result[ i++ ] = byteArray[ byteArray.length - 1 - j ];
}
return result;
}
/**
* Converts bytes into big integer value
*
* @method bigIntFromBytes
* @param {Array} bytes
* @returns {String} value
**/
var bigIntFromBytes = function (bytes) {
var signum, mag;
if ( bytes.length === 0 ) {
throw new Error('Illegal byte array length of 0.');
}
if (bytes[0] < 0) {
mag = makePositive( bytes );
signum = -1;
}
else {
mag = stripLeadingZeroBytes( bytes );
signum = ( mag.length === 0 ? 0 : 1 );
}
var bigInt = new BN( 0 );
var i = 0;
mag = mag.reverse( );
mag = mag.map(function ( x ) {
if ( x >= 0 ) {
return x;
}
else {
return x + 0x100000000;
}
});
for ( ; i < mag.length; i++ ) {
// shift left
bigInt = bigInt.add(new BN( mag[ i ] ).shln( i * 32 ) );
}
return bigInt.mul( new BN( signum ) ).toString( );
}
/**
* Strips the leading zero bytes from byte array
*
* @method stripLeadingZeroBytes
* @param {Array} bytes
* @returns {Array} result
**/
function stripLeadingZeroBytes(bytes) {
var byteLength = bytes.length;
var keep = 0;
while ( keep < bytes.length && bytes[keep] === 0 ) {
keep++;
}
var intLength = Math.floor( ( byteLength - keep + 3 ) / 4 );
var result = new Array( intLength );
var b = byteLength - 1;
var i = intLength - 1;
for ( ; i >= 0; i-- ) {
result[ i ] = bytes[ b-- ] & 0xff;
var bytesRemaining = b - keep + 1;
var bytesToTransfer = Math.min( 3, bytesRemaining );
var j = 8;
for ( ; j <= 8 * bytesToTransfer; j += 8 ) {
result[ i ] |= ((bytes[ b-- ] & 0xff ) << j );
}
}
return result;
}
/**
* Makes byte array positive
*
* @method makePositive
* @param {Array} bytes
* @returns {Array} result
**/
function makePositive(bytes) {
var keep = 0;
var byteLength = bytes.length;
while ( keep < byteLength && bytes[ keep ] === -1 ) {
keep++;
}
var k = keep;
while ( k < byteLength && bytes[ k ]===0 ) {
k++;
}
var extraByte = (k === byteLength) ? 1 : 0;
var intLength = Math.floor((byteLength - keep + extraByte + 3) / 4);
var result = new Array(intLength);
var b = byteLength - 1;
var i = intLength - 1;
for ( ; i >= 0; i-- ) {
result[ i ] = bytes[ b-- ] & 0xff;
var numBytesToTransfer = Math.min( 3, b - keep + 1 );
if ( numBytesToTransfer < 0 ) {
numBytesToTransfer = 0;
}
var j = 8;
for ( ; j <= 8 * numBytesToTransfer; j += 8 ) {
result[ i ] |= ( ( bytes[ b-- ] & 0xff ) << j );
}
var mask = -1 >>> ( 8 * (3 - numBytesToTransfer ) );
result[ i ] = ~result[ i ] & mask;
}
var i = result.length - 1;
for ( ; i >= 0; i-- ) {
result[ i ] = ( result[ i ] & 0xffffffff ) + 1;
if ( result[ i ] !== 0 ) {
break;
}
}
return result;
}
/**
* Words from Bytes conversion
*
* Credit: https://gist.github.com/artjomb/7ef1ee574a411ba0dd1933c1ef4690d1
*
* @method wordsFromBytes
* @param {Array} ba Byte array
*/
var wordsFromBytes = function (ba) {
ba = ba.map(function (x) {
return x & 0xFF;
});
var wa = [ ];
for ( var i = 0; i < ba.length; i++ ) {
wa[ ( i / 4 ) | 0 ] |= ba[ i ] << ( 24 - 8 * i );
}
return wa;
}
/**
* Bytes from Words conversion
*
* @method bytesFromWords
* @param {Array} wordArray
* @param {int} length
*
*/
var bytesFromWords = function(wordArray, length) {
var result = [],
bytes,
i = 0;
while ( length > 0 ) {
bytes = wordToByteArray(wordArray[ i ], Math.min( 4, length ) );
length -= bytes.length;
result.push( bytes );
i++;
}
return [ ].concat.apply( [ ], result ).map( function ( x ) {
if (x<=127) {
return x;
}
return x - 256;
});
}
/**
* Words to Byte Array
*
* @method wordToByteArray
* @param {Array} word
* @param {int} length
*
*/
function wordToByteArray(word, length) {
var ba = [],
i,
xFF = 0xFF;
if ( length > 0 )
ba.push( word >>> 24 );
if ( length > 1 )
ba.push( ( word >>> 16 ) & xFF );
if ( length > 2 )
ba.push( ( word >>> 8 ) & xFF );
if ( length > 3 )
ba.push( word & xFF );
return ba;
}
module.exports = {
trits : trits,
trytes : trytes,
value : value,
fromValue : fromValue,
tritsFromBigInt : tritsFromBigInt,
bigIntFromTrits : bigIntFromTrits,
bytesFromBigInt : bytesFromBigInt,
bigIntFromBytes : bigIntFromBytes,
wordsFromBytes : wordsFromBytes,
bytesFromWords : bytesFromWords
};