-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
512 lines (457 loc) · 13.8 KB
/
index.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
const {
parseHex,
bigintToHex,
hexToBigint,
bigintToBinary,
bigintToBinaryArray,
binaryToBigint,
bigintToNumber,
numberTobigint,
bigintTolimbs,
limbsToBigint,
bigintToField,
bigintToUtf8,
utf8ToBigint,
} = require('./conversion');
const {
addBigInt,
subBigInt,
mulBigInt,
addModBigInt,
subModBigInt,
mulModBigInt,
absBigInt,
eGcdBigInt,
gcdBigInt,
lcmBigInt,
maxBigInt,
minBigInt,
powModBigInt,
} = require('./function');
function isBinary(str) {
let isBinaryFlag = false;
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === '0' || str[i] === '1') {
count += 1;
}
}
if (count === str.length) {
isBinaryFlag = true;
}
return isBinaryFlag;
}
function isHex(_hex) {
const hexMatch = _hex.match(/^(0x)([\da-fA-F]+)$/);
if (hexMatch == null) return false;
return true;
}
const inferType = value => {
if (value === undefined) throw new Error('Input value was undefined');
if (value === '') throw new Error('Input was empty');
if (Array.isArray(value)) {
// ensure all elements of the array are of the same type:
if (value.map(inferType).every((val, i, arr) => val === arr[0])) return 'limbs';
// infer arrays whcich contain elements of the same type as 'limbs'
}
if (typeof Object(value).valueOf() === 'bigint') {
return 'bigint';
}
if (typeof value === 'object') {
if (value.__limbBitLength === BigInt(0)) {
return 'bigint';
}
return 'limbs';
}
if (typeof value === 'string') {
if (isBinary(value) === true) {
return 'binary';
}
}
if (/^[0-9]+$/.test(value)) return 'number'; // same effect as 'decimal' or 'number'
if (isHex(value) === true) return 'hex';
return 'utf8';
};
const convertToBigint = (value, type, limbBitLength) => {
switch (type) {
default:
throw new Error(`invalid type "${type}"`);
case 'hex':
return hexToBigint(value);
case 'binary':
return binaryToBigint(value);
case 'decimal':
case 'integer':
case 'number':
return numberTobigint(value);
case 'bigint':
return value;
case 'utf8':
return utf8ToBigint(value);
case 'limbs':
if (limbBitLength === undefined) {
throw new Error(`limbBitLength is not defined.`);
} else return limbsToBigint(value, limbBitLength);
}
};
class GeneralNumber {
constructor(value, limbBitLength) {
if (value === undefined) throw new Error('Input value is undefined');
if (value === '') throw new Error('Input is empty');
// if (type) {
// checkType(value, type);
// } else {
const type = inferType(value);
// }
this.__value = convertToBigint(value, type, limbBitLength);
this.__limbBitLength = BigInt(0);
}
get toBigint() {
if (typeof this.__value === 'object') {
return Object.values(this.__value);
}
return this.__value;
}
get toLimbs() {
if (typeof this.__value === 'object') {
return Object.values(this.__value);
}
return this.__value;
}
get binary() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToBinary(this.__value);
}
get binaryArray() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToBinaryArray(this.__value);
}
get decimal() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToNumber(this.__value);
}
get integer() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToNumber(this.__value);
}
get number() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToNumber(this.__value);
}
get bigint() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return this.__value;
}
get utf8() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToUtf8(this.__value);
}
get ascii() {
const type = inferType(this.__value);
this.__value = convertToBigint(this.__value, type, this.__limbBitLength);
this.__limbBitLength = BigInt(0);
return bigintToUtf8(this.__value);
}
get all() {
return this;
}
limbs(_limbBitLength, _numberOfLimbs) {
if (inferType(this.__value) === 'limbs') {
this.__value = limbsToBigint(this.__value, _limbBitLength);
this.__limbBitLength = BigInt(0);
}
let _numberOfLimbsBigint = BigInt(0);
if (!_limbBitLength) throw new Error('limbBitLength not specified');
const _limbBitLengthBigint = new GeneralNumber(_limbBitLength).__value;
if (_numberOfLimbs !== undefined) {
_numberOfLimbsBigint = new GeneralNumber(_numberOfLimbs).__value;
const limbsString = bigintTolimbs(this.__value, _limbBitLengthBigint, _numberOfLimbsBigint);
this.__value = limbsString;
this.__limbBitLength = _limbBitLengthBigint;
return this;
// eslint-disable-next-line no-else-return
} else {
const limbsString = bigintTolimbs(this.__value, _limbBitLengthBigint);
this.__value = limbsString;
this.__limbBitLength = _limbBitLengthBigint;
return this;
}
}
hex(byteLength, butTruncateValueToByteLength = 0) {
if (inferType(this.__value) === 'limbs') {
// eslint-disable-next-line no-undef
this.__value = limbsToBigint(this.__value);
}
let hexString = bigintToHex(this.__value);
if (byteLength) {
if (butTruncateValueToByteLength) {
if (butTruncateValueToByteLength > byteLength)
throw new Error(
`butTruncateValueToByteLength (${butTruncateValueToByteLength}) > byteLength (${byteLength})`,
);
hexString = parseHex(hexString, true, butTruncateValueToByteLength);
}
hexString = parseHex(hexString, true, byteLength);
}
this.__limbBitLength = BigInt(0);
return hexString;
}
field(__valueModulus, noOverflow = true) {
if (inferType(this.__value) === 'limbs') {
this.__value = limbsToBigint(this.__value, this.__limbBitLength);
}
// this._modulus = __valueModulus;
if (!__valueModulus) throw new Error('no field modulus specified');
if (typeof _addend !== 'object') {
// eslint-disable-next-line no-param-reassign
__valueModulus = new GeneralNumber(__valueModulus).__value;
}
const fieldResult = bigintToField(this.__value, __valueModulus, noOverflow);
// this._field = fieldResult;
this.__value = fieldResult;
this.__limbBitLength = BigInt(0);
return this;
}
add(_addend) {
if (typeof _addend !== 'object') {
// eslint-disable-next-line no-param-reassign
_addend = new GeneralNumber(_addend);
}
const result = addBigInt(this.__value, _addend.__value);
this.__value = result;
return this;
}
sub(_minuend) {
if (typeof _minuend !== 'object') {
// eslint-disable-next-line no-param-reassign
_minuend = new GeneralNumber(_minuend);
}
const result = subBigInt(this.__value, _minuend.__value);
this.__value = result;
return this;
}
mul(_multiplicand) {
if (typeof _multiplicand !== 'object') {
// eslint-disable-next-line no-param-reassign
_multiplicand = new GeneralNumber(_multiplicand);
}
const result = mulBigInt(this.__value, _multiplicand.__value);
this.__value = result;
return this;
}
addMod(_addend, _modular) {
if (typeof _addend !== 'object') {
// eslint-disable-next-line no-param-reassign
_addend = new GeneralNumber(_addend);
}
if (typeof _modular !== 'object') {
// eslint-disable-next-line no-param-reassign
_modular = new GeneralNumber(_modular);
}
const result = addModBigInt(this.__value, _addend.__value, _modular.__value);
this.__value = result;
return this;
}
subMod(_minuend, _modular) {
if (typeof _minuend !== 'object') {
// eslint-disable-next-line no-param-reassign
_minuend = new GeneralNumber(_minuend);
}
if (typeof _modular !== 'object') {
// eslint-disable-next-line no-param-reassign
_modular = new GeneralNumber(_modular);
}
const result = subModBigInt(this.__value, _minuend.__value, _modular.__value);
this.__value = result;
return this;
}
mulMod(_multiplicand, _modular) {
if (typeof _multiplicand !== 'object') {
// eslint-disable-next-line no-param-reassign
_multiplicand = new GeneralNumber(_multiplicand);
}
if (typeof _modular !== 'object') {
// eslint-disable-next-line no-param-reassign
_modular = new GeneralNumber(_modular);
}
const result = mulModBigInt(this.__value, _multiplicand.__value, _modular.__value);
this.__value = result;
return this;
}
powMod(_exponent, _modular) {
if (typeof _exponent !== 'object') {
// eslint-disable-next-line no-param-reassign
_exponent = new GeneralNumber(_exponent);
}
if (typeof _modular !== 'object') {
// eslint-disable-next-line no-param-reassign
_modular = new GeneralNumber(_modular);
}
const _base = this.__value;
const result = powModBigInt(_base, _exponent.__value, _modular.__value);
this.__value = result;
return this;
}
abs() {
const result = absBigInt(this.__value);
this.__value = result;
return this;
}
egcd(_secondInput) {
if (typeof _secondInput !== 'object') {
// eslint-disable-next-line no-param-reassign
_secondInput = new GeneralNumber(_secondInput);
}
const result = eGcdBigInt(this.__value, _secondInput.__value);
this.__value = result;
return this;
}
gcd(_secondInput) {
if (typeof _secondInput !== 'object') {
// eslint-disable-next-line no-param-reassign
_secondInput = new GeneralNumber(_secondInput);
}
const result = gcdBigInt(this.__value, _secondInput.__value);
this.__value = result;
return this;
}
lcm(_secondInput) {
if (typeof _secondInput !== 'object') {
// eslint-disable-next-line no-param-reassign
_secondInput = new GeneralNumber(_secondInput);
}
const result = lcmBigInt(this.__value, _secondInput.__value);
this.__value = result;
return this;
}
max(_secondInput) {
if (typeof _secondInput !== 'object') {
// eslint-disable-next-line no-param-reassign
_secondInput = new GeneralNumber(_secondInput);
}
const result = maxBigInt(this.__value, _secondInput.__value);
this.__value = result;
return this;
}
min(_secondInput) {
if (typeof _secondInput !== 'object') {
// eslint-disable-next-line no-param-reassign
_secondInput = new GeneralNumber(_secondInput);
}
const result = minBigInt(this.__value, _secondInput.__value);
this.__value = result;
return this;
}
}
const attachPropertyAll = thing => {
Object.defineProperty(thing, 'all', {
get() {
// eslint-disable-next-line no-use-before-define
return new GeneralObject(thing);
},
configurable: true,
});
};
const generalise = (thing, limbBitLength) => {
if (typeof thing === 'undefined') {
return thing;
}
if (thing instanceof GeneralNumber) {
return thing;
}
if (typeof Object(thing).valueOf() === 'bigint') {
// a bigint is not to be confused with a regular object, and so this check must come first
return new GeneralNumber(thing);
}
if (typeof thing === 'object') {
if (limbBitLength !== undefined) {
return new GeneralNumber(thing, limbBitLength);
}
const result = Array.isArray(thing) ? [] : {};
for (const [key, value] of Object.entries(thing)) {
result[key] = generalise(value);
}
attachPropertyAll(result);
return result;
}
return new GeneralNumber(thing);
};
const convert = (thing, type, args) => {
if (typeof thing !== 'object')
throw new Error(`Attempting to 'convert' something other than an object/array: ${thing}`);
const result = Array.isArray(thing) ? [] : {};
for (const [key, value] of Object.entries(thing)) {
if (value instanceof GeneralNumber) {
// each value is a GeneralNumber
result[key] = typeof value[type] === 'function' ? value[type](...args) : value[type];
} else result[key] = convert(value, type, args);
}
return result;
};
class GeneralObject {
constructor(object) {
this._object = generalise(object);
}
get object() {
return this._object;
}
get binary() {
return convert(this._object, 'binary');
}
get binaryArray() {
return bigintToBinaryArray(this._object, 'binaryArray');
}
get decimal() {
return convert(this._object, 'integer');
}
get integer() {
return convert(this._object, 'decimal');
}
get number() {
return convert(this._object, 'number');
}
get bigInt() {
return convert(this._object, 'bigInt');
}
get utf8() {
return convert(this._object, 'utf8');
}
limbs(limbBitLength, numberOfLimbs) {
return convert(this._object, 'limbs', [
limbBitLength, //
numberOfLimbs,
]);
}
hex(byteLength) {
return convert(this._object, 'hex', [byteLength]);
}
field(modulus) {
return convert(this._object, 'field', [modulus]);
}
}
const GN = GeneralNumber;
const GO = GeneralObject;
const generalize = generalise; // for Americans
module.exports = {
GeneralNumber,
GN,
generalise,
generalize,
GeneralObject,
GO,
convertToBigint,
};