Coverage Report

Created: 2026-03-30 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsBigInt.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsAll.h"
39
40
#ifndef mxCompile
41
static txSlot* fxBigIntCheck(txMachine* the, txSlot* it);
42
static txBigInt* fxIntegerToBigInt(txMachine* the, txSlot* slot);
43
static txBigInt* fxNumberToBigInt(txMachine* the, txSlot* slot);
44
static txBigInt* fxStringToBigInt(txMachine* the, txSlot* slot, txFlag whole);
45
#endif
46
47
#ifndef howmany
48
193k
#define howmany(x, y) (((x) + (y) - 1) / (y))
49
#endif
50
#ifndef MAX
51
532k
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
52
#endif
53
#ifndef MIN
54
789k
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
55
#endif
56
57
const txU4 gxDataOne[1] = { 1 };
58
const txU4 gxDataZero[1] = { 0 };
59
const txBigInt gxBigIntNaN = { .sign=0, .size=0, .data=(txU4*)gxDataZero };
60
const txBigInt gxBigIntOne = { .sign=0, .size=1, .data=(txU4*)gxDataOne };
61
const txBigInt gxBigIntZero = { .sign=0, .size=1, .data=(txU4*)gxDataZero };
62
63
static txBigInt *fxBigInt_fit(txMachine* the, txBigInt *r);
64
65
#ifdef mxMetering
66
static void fxBigInt_meter(txMachine* the, int n);
67
2.43M
#define mxBigInt_meter(N) if (the) fxBigInt_meter(the, N)
68
#else
69
#define mxBigInt_meter(N)
70
#endif
71
72
// BYTE CODE
73
74
#ifndef mxCompile
75
76
void fxBuildBigInt(txMachine* the)
77
20.4k
{
78
20.4k
  txSlot* slot;
79
20.4k
  mxPush(mxObjectPrototype);
80
20.4k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
81
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
82
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
83
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG);
84
20.4k
  slot = fxNextStringXProperty(the, slot, "BigInt", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
85
20.4k
  mxBigIntPrototype = *the->stack;
86
20.4k
  slot = fxBuildHostConstructor(the, mxCallback(fx_BigInt), 1, mxID(_BigInt));
87
20.4k
  mxBigIntConstructor = *the->stack;
88
20.4k
  slot = fxLastProperty(the, slot);
89
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asIntN), 2, mxID(_asIntN), XS_DONT_ENUM_FLAG);
90
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asUintN), 2, mxID(_asUintN), XS_DONT_ENUM_FLAG);
91
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_bitLength), 1, mxID(_bitLength), XS_DONT_ENUM_FLAG);
92
20.4k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_fromArrayBuffer), 1, mxID(_fromArrayBuffer), XS_DONT_ENUM_FLAG);
93
20.4k
  mxPop();
94
20.4k
}
95
96
void fx_BigInt(txMachine* the)
97
676
{
98
676
  if (mxTarget->kind != XS_UNDEFINED_KIND)
99
0
    mxTypeError("new: BigInt");
100
676
  if (mxArgc > 0)
101
676
    *mxResult = *mxArgv(0);
102
676
  fxToPrimitive(the, mxResult, XS_NUMBER_HINT);
103
676
  if (mxResult->kind == XS_NUMBER_KIND) {
104
169
    int fpclass = c_fpclassify(mxResult->value.number);
105
169
    txNumber check = c_trunc(mxResult->value.number);
106
169
    if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE) && (mxResult->value.number == check))
107
151
      fxNumberToBigInt(the, mxResult);
108
18
    else
109
18
      mxRangeError("cannot coerce number to bigint");
110
169
  }
111
507
  else if (mxResult->kind == XS_INTEGER_KIND) {
112
125
    fxIntegerToBigInt(the, mxResult);
113
125
  }
114
382
  else
115
382
    fxToBigInt(the, mxResult, 1);
116
676
}
117
118
txNumber fx_BigInt_asAux(txMachine* the)
119
11.1k
{
120
11.1k
  txNumber value, index;
121
11.1k
  if (mxArgc < 1)
122
5
    index = 0;
123
11.1k
  else {
124
11.1k
    if (mxArgv(0)->kind == XS_UNDEFINED_KIND)
125
526
      index = 0;
126
10.6k
    else {
127
10.6k
      value = fxToNumber(the, mxArgv(0));
128
10.6k
      if (c_isnan(value))
129
21
        index = 0;
130
10.6k
      else {
131
10.6k
        value = c_trunc(value);
132
10.6k
        if (value < 0)
133
14
          mxRangeError("index < 0");
134
10.6k
        index = value;
135
10.6k
        if (index <= 0)
136
2.93k
          index = 0;
137
7.70k
        else if (index > C_MAX_SAFE_INTEGER)
138
4
          index = C_MAX_SAFE_INTEGER;
139
10.6k
        if (value != index)
140
4
          mxRangeError("invalid index");
141
10.6k
      }
142
10.6k
    }
143
11.1k
  }
144
11.1k
  if (mxArgc < 2)
145
9
    mxTypeError("no bigint");
146
11.1k
  return index;
147
11.1k
}
148
149
void fx_BigInt_asIntN(txMachine* the)
150
5.85k
{
151
5.85k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
152
5.85k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
153
5.85k
  txBigInt* result;
154
5.85k
  if (fxBigInt_iszero(arg)) {
155
1.25k
    result = fxBigInt_dup(the, arg);
156
1.25k
  }
157
4.59k
  else {
158
4.59k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
159
4.59k
    txBigInt* mask = fxBigInt_usub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
160
4.59k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
161
4.59k
    if ((arg->sign) && !fxBigInt_iszero(result))
162
2.01k
      result = fxBigInt_usub(the, C_NULL, bits, result);
163
4.59k
    if (index && fxBigInt_comp(result, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index - 1)) >= 0)
164
1.93k
      result = fxBigInt_sub(the, C_NULL, result, bits);
165
4.59k
    result = fxBigInt_fit(the, result);
166
4.59k
  }
167
5.85k
  mxResult->value.bigint = *result;
168
5.85k
  mxResult->kind = XS_BIGINT_KIND;
169
//  txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
170
//  txBigInt* result = fxBigInt_mod(the, C_NULL, fxToBigInt(the, mxArgv(1), 1), bits);
171
//  if (index && fxBigInt_comp(result, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index - 1)) >= 0)
172
//    result = fxBigInt_sub(the, C_NULL, result, bits);
173
//  mxResult->value.bigint = *result;
174
//  mxResult->kind = XS_BIGINT_KIND;
175
5.85k
}
176
177
void fx_BigInt_asUintN(txMachine* the)
178
5.34k
{
179
5.34k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
180
5.34k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
181
5.34k
  txBigInt* result;
182
5.34k
  if (fxBigInt_iszero(arg)) {
183
1.88k
    result = fxBigInt_dup(the, arg);
184
1.88k
  }
185
3.46k
  else {
186
3.46k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
187
3.46k
    txBigInt* mask = fxBigInt_sub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
188
3.46k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
189
3.46k
    if ((arg->sign) && !fxBigInt_iszero(result))
190
1.01k
      result = fxBigInt_usub(the, C_NULL, bits, result);
191
3.46k
    result = fxBigInt_fit(the, result);
192
3.46k
  }
193
5.34k
  mxResult->value.bigint = *result;
194
5.34k
  mxResult->kind = XS_BIGINT_KIND;
195
//  fxToBigInt(the, mxArgv(1), 1);
196
//  mxResult->value.bigint = *fxBigInt_mod(the, C_NULL, &mxArgv(1)->value.bigint, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index));
197
//  mxResult->kind = XS_BIGINT_KIND;
198
5.34k
}
199
200
void fx_BigInt_bitLength(txMachine *the)
201
0
{
202
0
  txBigInt* arg = fxToBigInt(the, mxArgv(0), 1);
203
0
  mxResult->value.integer = fxBigInt_bitsize(arg);;
204
0
  mxResult->kind = XS_INTEGER_KIND;
205
0
}
206
207
void fx_BigInt_fromArrayBuffer(txMachine* the)
208
3
{
209
3
  txSlot* slot;
210
3
  txSlot* arrayBuffer = C_NULL;
211
3
  txSlot* bufferInfo;
212
3
  txBoolean sign = 0;
213
3
  int endian = EndianBig;
214
3
  txInteger length;
215
3
  txBigInt* bigint;
216
3
  txU1 *src, *dst;
217
3
  if (mxArgc < 1)
218
0
    mxTypeError("no argument");
219
3
  slot = mxArgv(0);
220
3
  if (slot->kind == XS_REFERENCE_KIND) {
221
2
    slot = slot->value.reference->next;
222
2
    if (slot && (slot->kind == XS_ARRAY_BUFFER_KIND))
223
2
      arrayBuffer = slot;
224
2
  }
225
3
  if (!arrayBuffer)
226
1
    mxTypeError("argument: not an ArrayBuffer instance");
227
2
  bufferInfo = arrayBuffer->next;
228
2
  length = bufferInfo->value.bufferInfo.length;
229
2
  if ((mxArgc > 1) && fxToBoolean(the, mxArgv(1)))
230
1
    sign = 1;
231
2
  if ((mxArgc > 2) && fxToBoolean(the, mxArgv(2)))
232
0
    endian = EndianLittle;
233
2
  if (sign)
234
1
    length--;
235
2
  if (length <= 0) {
236
2
    mxSyntaxError("argument: invalid ArrayBuffer instance");
237
//    mxResult->value.bigint = gxBigIntNaN;
238
//    mxResult->kind = XS_BIGINT_X_KIND;
239
0
    return;
240
2
  }
241
0
  bigint = fxBigInt_alloc(the, howmany(length, sizeof(txU4)));
242
0
  bigint->data[bigint->size - 1] = 0;
243
0
  src = (txU1*)(arrayBuffer->value.arrayBuffer.address);
244
0
  dst = (txU1*)(bigint->data);
245
0
  if (sign)
246
0
    bigint->sign = *src++;
247
#if mxBigEndian
248
  if (endian != EndianLittle) {
249
#else
250
0
  if (endian != EndianBig) {
251
0
#endif  
252
0
    c_memcpy(dst, src, length);
253
0
  }
254
0
  else {
255
0
    dst += length;
256
0
    while (length > 0) {
257
0
      dst--;
258
0
      *dst = *src;
259
0
      src++;
260
0
      length--;
261
0
    }
262
0
  }
263
0
  length = bigint->size - 1;
264
0
  while (length && (bigint->data[length] == 0))
265
0
    length--;
266
0
  bigint->size = length + 1;
267
0
  mxPullSlot(mxResult);
268
0
}
269
270
void fx_BigInt_prototype_toString(txMachine* the)
271
243k
{
272
243k
  txSlot* slot;
273
243k
  txU4 radix;
274
243k
  slot = fxBigIntCheck(the, mxThis);
275
243k
  if (!slot) mxTypeError("this: not a bigint");
276
243k
  if (mxArgc == 0)
277
65
    radix = 10;
278
243k
  else if (mxIsUndefined(mxArgv(0)))
279
6
    radix = 10;
280
243k
  else {
281
243k
    radix = fxToInteger(the, mxArgv(0));
282
243k
    if ((radix < 2) || (36 < radix))
283
29
      mxRangeError("invalid radix");
284
243k
  }
285
243k
  mxPushSlot(slot);
286
243k
  fxBigintToString(the, the->stack, radix);
287
243k
  mxPullSlot(mxResult);
288
243k
}
289
290
void fx_BigInt_prototype_valueOf(txMachine* the)
291
925
{
292
925
  txSlot* slot = fxBigIntCheck(the, mxThis);
293
925
  if (!slot) mxTypeError("this: not a bigint");
294
19
  mxResult->kind = slot->kind;
295
19
  mxResult->value = slot->value;
296
19
}
297
298
void fxBigInt(txMachine* the, txSlot* slot, txU1 sign, txU2 size, txU4* data)
299
0
{
300
0
  txBigInt* bigint = fxBigInt_alloc(the, size);
301
0
  c_memcpy(bigint->data, data, size * sizeof(txU4));
302
0
  bigint->sign = sign;
303
0
  mxPullSlot(slot);
304
0
}
305
306
void fxBigIntX(txMachine* the, txSlot* slot, txU1 sign, txU2 size, txU4* data)
307
0
{
308
0
  slot->value.bigint.data = data;
309
0
  slot->value.bigint.size = size;
310
0
  slot->value.bigint.sign = sign;
311
0
  slot->kind = XS_BIGINT_X_KIND;
312
0
}
313
314
txSlot* fxBigIntCheck(txMachine* the, txSlot* it)
315
244k
{
316
244k
  txSlot* result = C_NULL;
317
244k
  if ((it->kind == XS_BIGINT_KIND) || (it->kind == XS_BIGINT_X_KIND))
318
243k
    result = it;
319
956
  else if (it->kind == XS_REFERENCE_KIND) {
320
921
    txSlot* instance = it->value.reference;
321
921
    it = instance->next;
322
921
    if ((it) && (it->flag & XS_INTERNAL_FLAG) && ((it->kind == XS_BIGINT_KIND) || (it->kind == XS_BIGINT_X_KIND)))
323
19
      result = it;
324
921
  }
325
244k
  return result;
326
244k
}
327
328
void fxBigIntCoerce(txMachine* the, txSlot* slot)
329
299
{
330
299
  fxToBigInt(the, slot, 1);
331
299
}
332
333
txBoolean fxBigIntCompare(txMachine* the, txBoolean less, txBoolean equal, txBoolean more, txSlot* left, txSlot* right)
334
3.39M
{
335
3.39M
  int result;
336
3.39M
  txNumber delta = 0;
337
3.39M
  if (mxBigIntIsNaN(&left->value.bigint))
338
0
    return less & more & !equal;
339
3.39M
  if (right->kind == XS_STRING_KIND)
340
998k
    fxStringToBigInt(the, right, 1);
341
3.39M
  if ((right->kind != XS_BIGINT_KIND) && (right->kind != XS_BIGINT_X_KIND)) {
342
2.34M
    fxToNumber(the, right);
343
2.34M
    result = c_fpclassify(right->value.number);
344
2.34M
    if (result == C_FP_NAN)
345
115k
      return less & more & !equal;
346
2.22M
    if (result == C_FP_INFINITE)
347
3.15k
      return (right->value.number > 0) ? less : more;
348
2.22M
    delta = right->value.number - c_trunc(right->value.number);
349
2.22M
    fxNumberToBigInt(the, right);
350
2.22M
  }
351
3.28M
  if (mxBigIntIsNaN(&right->value.bigint))
352
826k
    return less & more & !equal;
353
2.45M
  result = fxBigInt_comp(&left->value.bigint, &right->value.bigint);
354
2.45M
  if (result < 0)
355
450k
    return less;
356
2.00M
  if (result > 0)
357
1.99M
    return more;
358
4.72k
  if (delta > 0)
359
2.10k
    return less;
360
2.61k
  if (delta < 0)
361
1.82k
    return more;
362
789
  return equal;
363
2.61k
}
364
365
void fxBigIntDecode(txMachine* the, txSize size)
366
5.55M
{
367
5.55M
  txBigInt* bigint;
368
5.55M
  mxPushUndefined();
369
5.55M
  bigint = &the->stack->value.bigint;
370
5.55M
  bigint->data = fxNewChunk(the, size);
371
5.55M
  bigint->size = size >> 2;
372
5.55M
  bigint->sign = 0;
373
#if mxBigEndian
374
  {
375
    txS1* src = (txS1*)the->code;
376
    txS1* dst = (txS1*)bigint->data;
377
    while (size) {
378
      dst[3] = *src++;
379
      dst[2] = *src++;
380
      dst[1] = *src++;
381
      dst[0] = *src++;
382
      dst += 4;
383
      size--;
384
    }
385
  }
386
#else
387
5.55M
  c_memcpy(bigint->data, the->code, size);
388
5.55M
#endif  
389
5.55M
  the->stack->kind = XS_BIGINT_KIND;
390
5.55M
}
391
392
#endif  
393
394
void fxBigIntEncode(txByte* code, txBigInt* bigint, txSize size)
395
14.6k
{
396
#if mxBigEndian
397
  {
398
    txS1* src = (txS1*)bigint->data;
399
    txS1* dst = (txS1*)code;
400
    while (size) {
401
      dst[3] = *src++;
402
      dst[2] = *src++;
403
      dst[1] = *src++;
404
      dst[0] = *src++;
405
      dst += 4;
406
      size--;
407
    }
408
  }
409
#else
410
14.6k
  c_memcpy(code, bigint->data, size);
411
14.6k
#endif
412
14.6k
}
413
414
#ifndef mxCompile
415
txSlot* fxBigIntToInstance(txMachine* the, txSlot* slot)
416
877k
{
417
877k
  txSlot* instance;
418
877k
  txSlot* internal;
419
877k
  mxPush(mxBigIntPrototype);
420
877k
  instance = fxNewObjectInstance(the);
421
877k
  internal = instance->next = fxNewSlot(the);
422
877k
  internal->flag = XS_INTERNAL_FLAG;
423
877k
  internal->kind = slot->kind;
424
877k
  internal->value = slot->value;
425
877k
  if (the->frame->flag & XS_STRICT_FLAG)
426
1
    instance->flag |= XS_DONT_PATCH_FLAG;
427
877k
  mxPullSlot(slot);
428
877k
  return instance;
429
877k
}
430
#endif
431
432
txSize fxBigIntMeasure(txBigInt* bigint)
433
43.9k
{
434
43.9k
  return bigint->size * sizeof(txU4);
435
43.9k
}
436
437
txSize fxBigIntMaximum(txSize length)
438
97.0k
{
439
97.0k
  return sizeof(txU4) * (1 + (((txSize)c_ceil((txNumber)length * c_log(10) / c_log(2))) / 32));
440
97.0k
}
441
442
txSize fxBigIntMaximumB(txSize length)
443
284
{
444
284
  return sizeof(txU4) * (1 + howmany(1, mxBigIntWordSize) + (length / 32));
445
284
}
446
447
txSize fxBigIntMaximumO(txSize length)
448
214
{
449
214
  return sizeof(txU4) * (1 + howmany(3, mxBigIntWordSize) + ((length * 3) / 32));
450
214
}
451
452
txSize fxBigIntMaximumX(txSize length)
453
172k
{
454
172k
  return sizeof(txU4) * (1 + howmany(4, mxBigIntWordSize) + ((length * 4) / 32));
455
172k
}
456
457
void fxBigIntParse(txBigInt* bigint, txString p, txSize length, txInteger sign)
458
97.0k
{
459
97.0k
  txU4 data[1] = { 0 };
460
97.0k
  txBigInt digit = { .sign=0, .size=1, .data=data };
461
97.0k
  bigint->sign = 0;
462
97.0k
  bigint->size = 1;
463
97.0k
  bigint->data[0] = 0;
464
316k
  while (length) {
465
219k
    char c = *p++;
466
219k
    data[0] = c - '0';
467
219k
    fxBigInt_uadd(NULL, bigint, fxBigInt_umul1(NULL, bigint, bigint, 10), &digit);
468
219k
    length--;
469
219k
  }
470
97.0k
  if ((bigint->size > 1) || (bigint->data[0] != 0))
471
67.8k
    bigint->sign = sign;
472
97.0k
}
473
474
void fxBigIntParseB(txBigInt* bigint, txString p, txSize length)
475
284
{
476
284
  txU4 data[1] = { 0 };
477
284
  txBigInt digit = { .sign=0, .size=1, .data=data };
478
284
  bigint->sign = 0;
479
284
  bigint->size = 1;
480
284
  bigint->data[0] = 0;
481
2.18k
  while (length) {
482
1.89k
    char c = *p++;
483
1.89k
    data[0] = c - '0';
484
1.89k
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 1), &digit);
485
1.89k
    length--;
486
1.89k
  }
487
284
}
488
489
void fxBigIntParseO(txBigInt* bigint, txString p, txSize length)
490
214
{
491
214
  txU4 data[1] = { 0 };
492
214
  txBigInt digit = { .sign=0, .size=1, .data=data };
493
214
  bigint->sign = 0;
494
214
  bigint->size = 1;
495
214
  bigint->data[0] = 0;
496
1.39k
  while (length) {
497
1.17k
    char c = *p++;
498
1.17k
    data[0] = c - '0';
499
1.17k
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 3), &digit);
500
1.17k
    length--;
501
1.17k
  }
502
214
}
503
504
void fxBigIntParseX(txBigInt* bigint, txString p, txSize length)
505
172k
{
506
172k
  txU4 data[1] = { 0 };
507
172k
  txBigInt digit = { .sign=0, .size=1, .data=data };
508
172k
  bigint->sign = 0;
509
172k
  bigint->size = 1;
510
172k
  bigint->data[0] = 0;
511
1.55M
  while (length) {
512
1.38M
    char c = *p++;
513
1.38M
    if (('0' <= c) && (c <= '9'))
514
176k
      data[0] = c - '0';
515
1.20M
    else if (('a' <= c) && (c <= 'f'))
516
347k
      data[0] = 10 + c - 'a';
517
859k
    else
518
859k
      data[0] = 10 + c - 'A';
519
1.38M
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 4), &digit);
520
1.38M
    length--;
521
1.38M
  }
522
172k
}
523
524
#ifndef mxCompile
525
526
void fxBigintToArrayBuffer(txMachine* the, txSlot* slot, txU4 total, txBoolean sign, int endian)
527
21.0k
{
528
21.0k
  txBigInt* bigint = fxToBigInt(the, slot, 1);
529
21.0k
  txU4 length = howmany(fxBigInt_bitsize(bigint), 8);
530
21.0k
  txU4 offset = 0;
531
21.0k
  txU1 *src, *dst;
532
21.0k
  if (length == 0)
533
21.0k
    length = 1;
534
21.0k
  if (length < total) {
535
13.7k
    if (endian == EndianBig)
536
13.7k
      offset = total - length;
537
13.7k
  }
538
7.26k
  else {
539
7.26k
    total = length;
540
7.26k
  }
541
21.0k
  if (sign) {
542
1
    if (total >= 0x7FFFFFFF)
543
0
      mxRangeError("byteLength too big");
544
1
    offset++;
545
1
    total++;
546
1
  }
547
21.0k
  fxConstructArrayBufferResult(the, mxThis, total);
548
21.0k
  src = (txU1*)(bigint->data);
549
21.0k
  dst = (txU1*)(mxResult->value.reference->next->value.arrayBuffer.address);
550
21.0k
  if (sign)
551
1
    *dst = bigint->sign;
552
21.0k
  dst += offset;
553
#if mxBigEndian
554
  if (endian != EndianLittle) {
555
#else
556
21.0k
  if (endian != EndianBig) {
557
1
#endif  
558
1
    c_memcpy(dst, src, length);
559
1
  }
560
21.0k
  else {
561
21.0k
    src += length;
562
42.1k
    while (length > 0) {
563
21.0k
      src--;
564
21.0k
      *dst = *src;
565
21.0k
      dst++;
566
21.0k
      length--;
567
21.0k
    }
568
21.0k
  }
569
21.0k
}
570
571
txNumber fxBigIntToNumber(txMachine* the, txSlot* slot)
572
406
{
573
406
  txBigInt* bigint = &slot->value.bigint;
574
406
  txNumber base = 4294967296;
575
406
  txNumber number = 0;
576
406
  int i;
577
20.7k
  for (i = bigint->size; --i >= 0;)
578
20.3k
    number = (number * base) + bigint->data[i];
579
406
  if (bigint->sign)
580
9
    number = -number;
581
406
  slot->value.number = number;
582
406
  slot->kind = XS_NUMBER_KIND;
583
406
  return number;
584
406
}
585
586
typedef struct {
587
  uint32_t k;
588
  uint32_t base_to_k;
589
} BaseChunk32;
590
591
static const BaseChunk32 base_chunks32[] ICACHE_FLASH_ATTR = {
592
  {31, 0x80000000},
593
  {19, 1162261467},
594
  {15, 1073741824},
595
  {13, 1220703125},
596
  {12, 2176782336},
597
  {11, 1977326743},
598
  {10, 1073741824},
599
  {10, 3486784401},
600
  { 9, 1000000000},   // base 10
601
  { 8, 214358881},
602
  { 8, 429981696},
603
  { 7, 62748517},
604
  { 7, 105413504},
605
  { 7, 170859375},
606
  { 7, 268435456},
607
  { 6, 24137569},
608
  { 6, 34012224},
609
  { 6, 47045881},
610
  { 6, 64000000},
611
  { 6, 85766121},
612
  { 6, 113379904},
613
  { 6, 148035889},
614
  { 6, 191102976},
615
  { 5, 9765625},
616
  { 5, 11881376},
617
  { 5, 14348907},
618
  { 5, 17210368},
619
  { 5, 20511149},
620
  { 5, 24300000},
621
  { 5, 28629151},
622
  { 5, 33554432},
623
  { 5, 39135393},
624
  { 5, 45435424},
625
  { 5, 52521875},
626
  { 5, 60466176}
627
};
628
629
void fxBigintToString(txMachine* the, txSlot* slot, txU4 radix)
630
418k
{
631
418k
  static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz";
632
418k
  txSize length, offset;
633
418k
  txSlot* result;
634
418k
  txSlot* stack;
635
418k
  if (0 == radix) radix = 10;
636
418k
  const BaseChunk32 *bc = base_chunks32 + (radix - 2);
637
638
418k
  if (mxBigIntIsNaN(&slot->value.bigint)) {
639
0
    fxStringX(the, slot, "NaN");
640
0
    return;
641
0
  }
642
418k
  mxBigInt_meter(slot->value.bigint.size);
643
644
418k
  mxPushUndefined();
645
418k
  result = the->stack;
646
  
647
418k
  mxPushSlot(slot);
648
418k
  fxBigInt_dup(the, &the->stack->value.bigint);
649
418k
  stack = the->stack;
650
651
418k
  length = 1 + bc->k + (txSize)c_ceil((txNumber)(stack->value.bigint.size + 1) * 32 * c_log(2) / c_log(radix));
652
418k
  if (stack->value.bigint.sign)
653
83.9k
    length++;
654
418k
  offset = length;
655
418k
  result->value.string = fxNewChunk(the, length);
656
418k
  result->kind = XS_STRING_KIND;
657
658
418k
  result->value.string[--offset] = 0;
659
418k
  int32_t nonZeroWords = stack->value.bigint.size;
660
419k
  do {
661
419k
    uint64_t carry = 0;
662
1.52M
    for (uint32_t i = nonZeroWords - 1, count = nonZeroWords, base_to_k = bc->base_to_k; count > 0; --count) {
663
1.10M
      carry = (carry << 32) | stack->value.bigint.data[i];
664
1.10M
      stack->value.bigint.data[i--] = (uint32_t)(carry / base_to_k);
665
1.10M
      carry %= base_to_k;
666
1.10M
    }
667
419k
    uint32_t remainder = (uint32_t)carry, k = bc->k;
668
9.11M
    do {
669
9.11M
      result->value.string[--offset] = c_read8(gxDigits + (remainder % radix));
670
9.11M
      remainder /= radix;
671
9.11M
    } while (--k);
672
673
839k
    while (nonZeroWords && (0 == stack->value.bigint.data[nonZeroWords - 1]))
674
419k
      nonZeroWords -= 1;
675
419k
  } while (nonZeroWords);
676
677
7.96M
  while (('0' == result->value.string[offset]) && result->value.string[offset + 1])
678
7.55M
    offset++;
679
680
418k
  if (stack->value.bigint.sign)
681
83.9k
    result->value.string[--offset] = '-';
682
418k
  c_memmove(result->value.string, result->value.string + offset, length - offset);
683
684
418k
  mxPop();
685
418k
  mxPop();
686
418k
  mxPullSlot(slot);
687
418k
}
688
689
txS8 fxToBigInt64(txMachine* the, txSlot* slot)
690
206
{
691
206
  return (txS8)fxToBigUint64(the, slot);
692
206
}
693
694
txU8 fxToBigUint64(txMachine* the, txSlot* slot)
695
417
{
696
417
  txBigInt* bigint = fxToBigInt(the, slot, 1);
697
417
  txU8 result = bigint->data[0];
698
417
  if (bigint->size > 1)
699
176
    result |= (txU8)(bigint->data[1]) << 32;
700
417
  if (bigint->sign && result) {
701
36
    result--;
702
36
    result = 0xFFFFFFFFFFFFFFFFll - result;
703
36
  }
704
417
  return result;
705
417
}
706
707
txBigInt* fxIntegerToBigInt(txMachine* the, txSlot* slot)
708
125
{
709
125
  txInteger integer = slot->value.integer;
710
125
  txBigInt* bigint = &slot->value.bigint;
711
125
  txU1 sign = 0;
712
125
  if (integer < 0) {
713
0
    integer = -integer;
714
0
    sign = 1;
715
0
  }
716
125
  bigint->data = fxNewChunk(the, sizeof(txU4));
717
125
  bigint->data[0] = (txU4)integer;
718
125
  bigint->sign = sign;
719
125
  bigint->size = 1;
720
125
  slot->kind = XS_BIGINT_KIND;
721
125
  return bigint;
722
125
}
723
724
txBigInt* fxNumberToBigInt(txMachine* the, txSlot* slot)
725
2.22M
{
726
2.22M
  txBigInt* bigint = &slot->value.bigint;
727
2.22M
  txNumber number = slot->value.number;
728
2.22M
  txNumber limit = 4294967296;
729
2.22M
  txU1 sign = 0;
730
2.22M
  txU2 size = 1;
731
2.22M
  if (number < 0) {
732
1.38M
    number = -number;
733
1.38M
    sign = 1;
734
1.38M
  }
735
2.76M
  while (number >= limit) {
736
543k
    size++;
737
543k
    number /= limit;
738
543k
  }
739
2.22M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
740
2.22M
  bigint->size = size;
741
4.98M
  while (size > 0) {
742
2.76M
    txU4 part = (txU4)number;
743
2.76M
    number -= part;
744
2.76M
    size--;
745
2.76M
    bigint->data[size] = part;
746
2.76M
    number *= limit;
747
2.76M
  }
748
2.22M
  bigint->sign = sign;
749
2.22M
  slot->kind = XS_BIGINT_KIND;
750
2.22M
  return bigint;
751
2.22M
}
752
753
txBigInt* fxStringToBigInt(txMachine* the, txSlot* slot, txFlag whole)
754
1.02M
{
755
1.02M
  txString s = slot->value.string;
756
1.02M
  txString p = fxSkipSpaces(s);
757
1.02M
  txSize offset, length;
758
1.02M
  txInteger sign = 0;
759
1.02M
  txBigInt bigint = {.sign=0, .size=0, .data=C_NULL };
760
1.02M
  char c = *p;
761
1.02M
  if (c == '0') {
762
997k
    char d = *(p + 1);
763
997k
    if (whole && ((d == 'B') || (d == 'b') || (d == 'O') || (d == 'o') || (d == 'X') || (d == 'x'))) {
764
863k
      p += 2;
765
863k
      offset = mxPtrDiff(p - s);
766
863k
      if ((d == 'B') || (d == 'b')) {
767
1.34k
        while (((c = *p)) && ('0' <= c) && (c <= '1'))
768
1.06k
          p++;
769
281
        length = mxPtrDiff(p - s - offset);
770
281
        p = fxSkipSpaces(p);
771
281
        if ((length > 0) && (*p == 0)) {
772
262
          bigint.data = fxNewChunk(the, fxBigIntMaximumB(length));
773
262
          fxBigIntParseB(&bigint, slot->value.string + offset, length);
774
262
        }
775
281
      }
776
863k
      else if ((d == 'O') || (d == 'o')) {
777
268
        while (((c = *p)) && ('0' <= c) && (c <= '7'))
778
237
          p++;
779
31
        length = mxPtrDiff(p - s - offset);
780
31
        p = fxSkipSpaces(p);
781
31
        if ((length > 0) && (*p == 0)) {
782
21
          bigint.data = fxNewChunk(the, fxBigIntMaximumO(length));
783
21
          fxBigIntParseO(&bigint, slot->value.string + offset, length);
784
21
        }
785
31
      }
786
862k
      else if ((d == 'X') || (d == 'x')) {
787
4.51M
        while (((c = *p)) && ((('0' <= c) && (c <= '9')) || (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'))))
788
3.65M
          p++;
789
862k
        length = mxPtrDiff(p - s - offset);
790
862k
        p = fxSkipSpaces(p);
791
862k
        if ((length > 0) && (*p == 0)) {
792
171k
          bigint.data = fxNewChunk(the, fxBigIntMaximumX(length));
793
171k
          fxBigIntParseX(&bigint, slot->value.string + offset, length);
794
171k
        }
795
862k
      }
796
863k
      goto bail;
797
863k
    }
798
997k
  }
799
157k
  if (c == '-') {
800
94
    sign = 1;
801
94
    p++;
802
94
  }
803
157k
  else if (c == '+') {
804
81
    p++;
805
81
  }
806
157k
  offset = mxPtrDiff(p - s);
807
295k
  while (((c = *p)) && ('0' <= c) && (c <= '9'))
808
137k
    p++;
809
157k
  length = mxPtrDiff(p - s - offset);
810
157k
  p = fxSkipSpaces(p);
811
157k
  if (*p == 0) {
812
21.3k
    bigint.data = fxNewChunk(the, fxBigIntMaximum(length));
813
21.3k
    fxBigIntParse(&bigint, slot->value.string + offset, length, sign);
814
21.3k
  }
815
1.02M
bail:
816
1.02M
  if (bigint.data) {
817
193k
    slot->value.bigint = bigint;
818
193k
    slot->kind = XS_BIGINT_KIND;
819
193k
  }
820
826k
  else {
821
826k
    slot->value.bigint = gxBigIntNaN;
822
826k
    slot->kind = XS_BIGINT_X_KIND;
823
826k
  }
824
1.02M
  return &slot->value.bigint;
825
157k
}
826
827
txBigInt* fxToBigInt(txMachine* the, txSlot* slot, txFlag strict)
828
33.3k
{
829
33.4k
again:
830
33.4k
  switch (slot->kind) {
831
2.93k
  case XS_BOOLEAN_KIND:
832
2.93k
    slot->value.bigint = *fxBigInt_dup(the, (txBigInt *)((slot->value.boolean) ? &gxBigIntOne : &gxBigIntZero));
833
2.93k
    slot->kind = XS_BIGINT_KIND;
834
2.93k
    break;
835
13
  case XS_INTEGER_KIND:
836
13
    if (strict)
837
13
      mxTypeError("cannot coerce number to bigint");
838
0
    fxIntegerToBigInt(the, slot); 
839
0
    break;
840
10
  case XS_NUMBER_KIND:
841
10
    if (strict)
842
10
      mxTypeError("cannot coerce number to bigint");
843
0
    fxNumberToBigInt(the, slot);  
844
0
    break;
845
21.5k
  case XS_STRING_KIND:
846
21.5k
  case XS_STRING_X_KIND:
847
21.5k
    fxStringToBigInt(the, slot, 1);
848
21.5k
    if (mxBigIntIsNaN(&slot->value.bigint))
849
79
      mxSyntaxError("cannot coerce string to bigint");
850
21.4k
    break;
851
21.4k
  case XS_BIGINT_KIND:
852
8.75k
  case XS_BIGINT_X_KIND:
853
8.75k
    break;
854
16
  case XS_SYMBOL_KIND:
855
16
    mxTypeError("cannot coerce symbol to bigint");
856
0
    break;
857
111
  case XS_REFERENCE_KIND:
858
111
    fxToPrimitive(the, slot, XS_NUMBER_HINT);
859
111
    goto again;
860
33
  default:
861
33
    mxTypeError("cannot coerce to bigint");
862
0
    break;
863
33.4k
  }
864
33.1k
  return &slot->value.bigint;
865
33.4k
}
866
867
void fxFromBigInt64(txMachine* the, txSlot* slot, txS8 it)
868
252
{
869
252
  txU1 sign = 0;
870
252
  txU8 value = 0;
871
252
  if (it < 0) {
872
89
    if (it & 0x7FFFFFFFFFFFFFFFll)
873
89
      value = -it;
874
0
    else
875
0
      value = (txU8)it;
876
89
    sign = 1;
877
89
  }
878
163
  else
879
163
    value = it;
880
252
  if (value > 0x00000000FFFFFFFFll) {
881
123
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
882
123
    slot->value.bigint.data[0] = (txU4)value;
883
123
    slot->value.bigint.data[1] = (txU4)(value >> 32);
884
123
    slot->value.bigint.size = 2;
885
123
  }
886
129
  else {
887
129
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
888
129
    slot->value.bigint.data[0] = (txU4)value;
889
129
    slot->value.bigint.size = 1;
890
129
  }
891
252
    slot->value.bigint.sign = sign;
892
252
  slot->kind = XS_BIGINT_KIND;
893
252
}
894
895
void fxFromBigUint64(txMachine* the, txSlot* slot, txU8 value)
896
122
{
897
122
  if (value > 0x00000000FFFFFFFFll) {
898
77
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
899
77
    slot->value.bigint.data[0] = (txU4)(value);
900
77
    slot->value.bigint.data[1] = (txU4)(value >> 32);
901
77
    slot->value.bigint.size = 2;
902
77
  }
903
45
  else {
904
45
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
905
45
    slot->value.bigint.data[0] = (txU4)value;
906
45
    slot->value.bigint.size = 1;
907
45
  }
908
122
    slot->value.bigint.sign = 0;
909
122
  slot->kind = XS_BIGINT_KIND;
910
122
}
911
912
#endif
913
914
txBigInt *fxBigInt_alloc(txMachine* the, txU4 size)
915
6.72M
{
916
6.72M
#ifndef mxCompile
917
6.72M
  txBigInt* bigint;
918
6.72M
  if (size > 0xFFFF) {
919
1
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
920
1
  }
921
6.72M
  mxPushUndefined();
922
6.72M
  bigint = &the->stack->value.bigint;
923
6.72M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
924
6.72M
  bigint->size = size;
925
6.72M
  bigint->sign = 0;
926
6.72M
  the->stack->kind = XS_BIGINT_KIND;
927
6.72M
  return bigint;
928
#else
929
  return NULL;
930
#endif
931
6.72M
}
932
933
void fxBigInt_free(txMachine* the, txBigInt *bigint)
934
1.51M
{
935
1.51M
#ifndef mxCompile
936
1.51M
  if (bigint == &the->stack->value.bigint)
937
1.51M
    the->stack++;
938
//  else
939
//    fprintf(stderr, "oops\n");
940
1.51M
#endif
941
1.51M
}
942
943
int fxBigInt_comp(txBigInt *a, txBigInt *b)
944
2.45M
{
945
2.45M
  if (a->sign != b->sign)
946
1.15M
    return(a->sign ? -1: 1);
947
1.30M
  else if (a->sign)
948
439k
    return(fxBigInt_ucomp(b, a));
949
865k
  else
950
865k
    return(fxBigInt_ucomp(a, b));
951
2.45M
}
952
953
int fxBigInt_ucomp(txBigInt *a, txBigInt *b)
954
2.21M
{
955
2.21M
  int i;
956
957
2.21M
  if (a->size != b->size)
958
204k
    return(a->size > b->size ? 1: -1);
959
2.52M
  for (i = a->size; --i >= 0;) {
960
2.06M
    if (a->data[i] != b->data[i])
961
1.55M
      return(a->data[i] > b->data[i] ? 1: -1);
962
2.06M
  }
963
455k
  return(0);
964
2.01M
}
965
966
int fxBigInt_iszero(txBigInt *a)
967
3.59M
{
968
3.59M
  return(a->size == 1 && a->data[0] == 0);
969
3.59M
}
970
971
void
972
fxBigInt_fill0(txBigInt *r)
973
419k
{
974
419k
  c_memset(r->data, 0, r->size * sizeof(txU4));
975
419k
}
976
977
void fxBigInt_copy(txBigInt *a, txBigInt *b)
978
3.54M
{
979
3.54M
  c_memmove(a->data, b->data, b->size * sizeof(txU4));
980
3.54M
  a->size = b->size;
981
3.54M
  a->sign = b->sign;
982
3.54M
}
983
984
txBigInt *fxBigInt_dup(txMachine* the, txBigInt *a)
985
424k
{
986
424k
  txBigInt *r = fxBigInt_alloc(the, a->size);
987
424k
  fxBigInt_copy(r, a);
988
424k
  return(r);
989
424k
}
990
991
int
992
fxBigInt_ffs(txBigInt *a)
993
157k
{
994
157k
  int i;
995
157k
  txU4 w = a->data[a->size - 1];
996
997
4.97M
  for (i = 0; i < (int)mxBigIntWordSize && !(w & ((txU4)1 << (mxBigIntWordSize - 1 - i))); i++)
998
4.81M
    ;
999
157k
  return(i);
1000
157k
}
1001
1002
txBigInt *fxBigInt_fit(txMachine* the, txBigInt *r)
1003
253k
{
1004
253k
  int i = r->size;
1005
271k
  while (i > 0) {
1006
261k
    i--;
1007
261k
    if (r->data[i])
1008
242k
      break;
1009
261k
  }
1010
253k
  r->size = (txU2)(i + 1);
1011
253k
  mxBigInt_meter(r->size);
1012
253k
  return r;
1013
253k
}
1014
1015
1016
#if mxWindows
1017
int
1018
fxBigInt_bitsize(txBigInt *e)
1019
{
1020
  return(e->size * mxBigIntWordSize - fxBigInt_ffs(e));
1021
}
1022
#else
1023
int
1024
fxBigInt_bitsize(txBigInt *a)
1025
45.8k
{
1026
45.8k
  int i = a->size;
1027
45.8k
  txU4 w;
1028
73.5k
  while (i > 0) {
1029
45.8k
    i--;
1030
45.8k
    w = a->data[i];
1031
45.8k
    if (w) {
1032
18.1k
      int c = __builtin_clz(w);
1033
18.1k
      return (i * mxBigIntWordSize) + mxBigIntWordSize - c;
1034
18.1k
    }
1035
45.8k
  }
1036
27.6k
  return 0;
1037
45.8k
}
1038
#endif
1039
1040
int fxBigInt_isset(txBigInt *e, txU4 i)
1041
138k
{
1042
138k
  txU4 w = e->data[i / mxBigIntWordSize];
1043
138k
  return((w & ((txU4)1 << (i % mxBigIntWordSize))) != 0);
1044
138k
}
1045
1046
/*
1047
 * bitwise operations
1048
 */
1049
1050
txBigInt *fxBigInt_not(txMachine* the, txBigInt *r, txBigInt *a)
1051
209k
{
1052
209k
  if (a->sign)
1053
1.82k
    r = fxBigInt_usub(the, r, a, (txBigInt *)&gxBigIntOne);
1054
208k
  else {
1055
208k
    r = fxBigInt_uadd(the, r, a, (txBigInt *)&gxBigIntOne);
1056
208k
    r->sign = 1;
1057
208k
  }
1058
209k
  return(r);
1059
209k
}
1060
1061
txBigInt *fxBigInt_and(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1062
11.5k
{
1063
11.5k
  txBigInt *aa, *bb;
1064
11.5k
  int i;
1065
11.5k
  if (a->sign) {
1066
6.52k
    if (b->sign)
1067
5.99k
      goto AND_MINUS_MINUS;
1068
522
    aa = a;
1069
522
    a = b;
1070
522
    b = aa; 
1071
522
    goto AND_PLUS_MINUS;
1072
6.52k
  }
1073
5.03k
  if (b->sign)
1074
3.24k
    goto AND_PLUS_MINUS;
1075
1.79k
  return fxBigInt_fit(the, fxBigInt_uand(the, r, a, b));
1076
3.76k
AND_PLUS_MINUS:
1077
// GMP: OP1 & -OP2 == OP1 & ~(OP2 - 1)
1078
3.76k
  if (r == NULL)
1079
3.76k
    r = fxBigInt_alloc(the, a->size);
1080
3.76k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1081
3.76k
    if (a->size > bb->size) {
1082
2.88k
    for (i = 0; i < bb->size; i++)
1083
1.56k
      r->data[i] = a->data[i] & ~bb->data[i];
1084
3.46k
    for (; i < a->size; i++)
1085
2.13k
      r->data[i] = a->data[i];
1086
1.32k
    }
1087
2.44k
    else {
1088
5.12k
    for (i = 0; i < a->size; i++)
1089
2.68k
      r->data[i] = a->data[i] & ~bb->data[i];
1090
2.44k
    }
1091
3.76k
    fxBigInt_free(the, bb);
1092
3.76k
  return fxBigInt_fit(the, r);
1093
5.99k
AND_MINUS_MINUS:
1094
// GMP: -((-OP1) & (-OP2)) = -(~(OP1 - 1) & ~(OP2 - 1)) == ~(~(OP1 - 1) & ~(OP2 - 1)) + 1 == ((OP1 - 1) | (OP2 - 1)) + 1
1095
5.99k
  if (r == NULL)
1096
5.99k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1097
5.99k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1098
5.99k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1099
5.99k
  r = fxBigInt_uor(the, r, aa, bb);
1100
5.99k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1101
5.99k
  r->sign = 1;
1102
5.99k
  fxBigInt_free(the, bb);
1103
5.99k
  fxBigInt_free(the, aa);
1104
5.99k
  return fxBigInt_fit(the, r);
1105
5.03k
}
1106
1107
txBigInt *fxBigInt_uand(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1108
16.4k
{
1109
16.4k
  int i;
1110
16.4k
  if (a->size > b->size) {
1111
8.70k
    txBigInt *t = b;
1112
8.70k
    b = a;
1113
8.70k
    a = t;
1114
8.70k
  }
1115
16.4k
  if (r == NULL)
1116
9.71k
    r = fxBigInt_alloc(the, a->size);
1117
6.77k
  else
1118
6.77k
    r->size = a->size;
1119
140k
  for (i = 0; i < a->size; i++)
1120
124k
    r->data[i] = a->data[i] & b->data[i];
1121
16.4k
  return(r);
1122
16.4k
}
1123
1124
txBigInt *fxBigInt_or(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1125
142k
{
1126
142k
  txBigInt *aa, *bb;
1127
142k
  int i;
1128
142k
  mxBigInt_meter(MAX(a->size, b->size));
1129
142k
  if (a->sign) {
1130
140k
    if (b->sign)
1131
6.77k
      goto OR_MINUS_MINUS;
1132
133k
    aa = a;
1133
133k
    a = b;
1134
133k
    b = aa; 
1135
133k
    goto OR_PLUS_MINUS;
1136
140k
  }
1137
2.37k
  if (b->sign)
1138
1.87k
    goto OR_PLUS_MINUS;
1139
492
  return fxBigInt_fit(the, fxBigInt_uor(the, r, a, b));
1140
135k
OR_PLUS_MINUS:
1141
// GMP: -(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) == ~(OP1 | ~(OP2 - 1)) + 1 == (~OP1 & (OP2 - 1)) + 1
1142
135k
  if (r == NULL)
1143
135k
    r = fxBigInt_alloc(the, b->size + 1);
1144
135k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1145
135k
    if (a->size < bb->size) {
1146
259k
    for (i = 0; i < a->size; i++)
1147
129k
      r->data[i] = ~a->data[i] & bb->data[i];
1148
415k
    for (; i < bb->size; i++)
1149
285k
      r->data[i] = bb->data[i];
1150
129k
  }
1151
5.54k
  else {
1152
11.4k
    for (i = 0; i < bb->size; i++)
1153
5.86k
      r->data[i] = ~a->data[i] & bb->data[i];
1154
5.54k
  }
1155
135k
  r->size = bb->size;
1156
135k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1157
135k
  r->sign = 1;
1158
135k
  fxBigInt_free(the, bb);
1159
135k
  return fxBigInt_fit(the, r);
1160
6.77k
OR_MINUS_MINUS:
1161
// GMP: -((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) == ~(~(OP1 - 1) | ~(OP2 - 1)) + 1 = = ((OP1 - 1) & (OP2 - 1)) + 1
1162
6.77k
  if (r == NULL)
1163
6.77k
    r = fxBigInt_alloc(the, MIN(a->size, b->size) + 1);
1164
6.77k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1165
6.77k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1166
6.77k
  r = fxBigInt_uand(the, r, aa, bb);
1167
6.77k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1168
6.77k
  r->sign = 1;
1169
6.77k
  fxBigInt_free(the, bb);
1170
6.77k
  fxBigInt_free(the, aa);
1171
6.77k
  return fxBigInt_fit(the, r);
1172
2.37k
}
1173
1174
txBigInt *fxBigInt_uor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1175
6.49k
{
1176
6.49k
  int i;
1177
6.49k
  if (a->size < b->size) {
1178
2.51k
    txBigInt *t = b;
1179
2.51k
    b = a;
1180
2.51k
    a = t;
1181
2.51k
  }
1182
6.49k
  if (r == NULL)
1183
492
    r = fxBigInt_alloc(the, a->size);
1184
5.99k
  else
1185
5.99k
    r->size = a->size;
1186
14.2k
  for (i = 0; i < b->size; i++)
1187
7.76k
    r->data[i] = a->data[i] | b->data[i];
1188
70.6k
  for (; i < a->size; i++)
1189
64.1k
    r->data[i] = a->data[i];
1190
6.49k
  return(r);
1191
6.49k
}
1192
1193
txBigInt *fxBigInt_xor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1194
78.7k
{
1195
78.7k
  txBigInt *aa, *bb;
1196
78.7k
  if (a->sign) {
1197
1.52k
    if (b->sign)
1198
1.13k
      goto XOR_MINUS_MINUS;
1199
396
    aa = a;
1200
396
    a = b;
1201
396
    b = aa; 
1202
396
    goto XOR_PLUS_MINUS;
1203
1.52k
  }
1204
77.2k
  if (b->sign)
1205
73.3k
    goto XOR_PLUS_MINUS;
1206
3.89k
  return fxBigInt_fit(the, fxBigInt_uxor(the, r, a, b));
1207
73.7k
XOR_PLUS_MINUS:
1208
// GMP: -(OP1 ^ (-OP2)) == -(OP1 ^ ~(OP2 - 1)) == ~(OP1 ^ ~(OP2 - 1)) + 1 == (OP1 ^ (OP2 - 1)) + 1
1209
73.7k
  if (r == NULL)
1210
73.7k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1211
73.7k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1212
73.7k
  r = fxBigInt_uxor(the, r, a, bb);
1213
73.7k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1214
73.7k
  r->sign = 1;
1215
73.7k
  fxBigInt_free(the, bb);
1216
73.7k
  return fxBigInt_fit(the, r);
1217
1.13k
XOR_MINUS_MINUS:
1218
// GMP: (-OP1) ^ (-OP2) == ~(OP1 - 1) ^ ~(OP2 - 1) == (OP1 - 1) ^ (OP2 - 1)
1219
1.13k
  if (r == NULL)
1220
1.13k
    r = fxBigInt_alloc(the, MAX(a->size, b->size));
1221
1.13k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1222
1.13k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1223
1.13k
  r = fxBigInt_uxor(the, r, aa, bb);
1224
1.13k
  fxBigInt_free(the, bb);
1225
1.13k
  fxBigInt_free(the, aa);
1226
1.13k
  return fxBigInt_fit(the, r);
1227
77.2k
}
1228
1229
txBigInt *fxBigInt_uxor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1230
78.7k
{
1231
78.7k
  int i;
1232
78.7k
  if (a->size < b->size) {
1233
1.19k
    txBigInt *t = b;
1234
1.19k
    b = a;
1235
1.19k
    a = t;
1236
1.19k
  }
1237
78.7k
  if (r == NULL)
1238
3.89k
    r = fxBigInt_alloc(the, a->size);
1239
74.8k
  else
1240
74.8k
    r->size = a->size;
1241
162k
  for (i = 0; i < b->size; i++)
1242
83.6k
    r->data[i] = a->data[i] ^ b->data[i];
1243
986k
  for (; i < a->size; i++)
1244
908k
    r->data[i] = a->data[i];
1245
78.7k
  return(r);
1246
78.7k
}
1247
1248
txBigInt *fxBigInt_lsl(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1249
704
{
1250
704
  if (b->sign) {
1251
408
    if (a->sign) {
1252
41
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1253
41
            if (b->data[0])
1254
41
                r = fxBigInt_uadd(the, C_NULL, r, (txBigInt *)&gxBigIntOne);
1255
41
      r->sign = 1;
1256
41
    }
1257
367
    else
1258
367
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1259
408
  }
1260
296
  else {
1261
296
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1262
296
    r->sign = a->sign;
1263
296
  }
1264
704
  return fxBigInt_fit(the, r);
1265
704
}
1266
1267
txBigInt *fxBigInt_ulsl1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1268
1.87M
{
1269
1.87M
  txU4 wsz, bsz;
1270
1.87M
  int n;
1271
1272
1.87M
  wsz = sw / mxBigIntWordSize;
1273
1.87M
  bsz = sw % mxBigIntWordSize;
1274
1.87M
  n = a->size + wsz + ((bsz == 0) ? 0 : 1);
1275
1.87M
  if (r == NULL) 
1276
489k
    r = fxBigInt_alloc(the, n);
1277
1.87M
  if (bsz == 0) {
1278
161k
    c_memmove(&r->data[wsz], a->data, a->size * sizeof(txU4));
1279
161k
    c_memset(r->data, 0, wsz * sizeof(txU4));
1280
161k
    r->size = a->size + wsz;
1281
161k
  }
1282
1.71M
  else {
1283
1.71M
    r->data[a->size + wsz] = a->data[a->size - 1] >> (mxBigIntWordSize - bsz);
1284
1.77M
    for (n = a->size; --n >= 1;)
1285
63.0k
      r->data[n + wsz] = (a->data[n] << bsz) | (a->data[n - 1] >> (mxBigIntWordSize - bsz));
1286
1.71M
    r->data[wsz] = a->data[0] << bsz;
1287
    /* clear the remaining part */
1288
3.73M
    for (n = wsz; --n >= 0;)
1289
2.01M
      r->data[n] = 0;
1290
    /* adjust r */
1291
1.71M
    r->size = a->size + wsz + (r->data[a->size + wsz] == 0 ? 0: 1);
1292
1.71M
  }
1293
1.87M
  return(r);
1294
1.87M
}
1295
1296
txBigInt *fxBigInt_lsr(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1297
11.6k
{
1298
11.6k
  if (b->sign) {
1299
4.63k
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1300
4.63k
    r->sign = a->sign;
1301
4.63k
  }
1302
7.05k
  else {
1303
7.05k
    if (a->sign) {
1304
4.10k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1305
4.10k
            if (b->data[0])
1306
4.09k
                r = fxBigInt_uadd(the, C_NULL, r, (txBigInt *)&gxBigIntOne);
1307
4.10k
      r->sign = 1;
1308
4.10k
    }
1309
2.94k
    else
1310
2.94k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1311
7.05k
  }
1312
11.6k
  return fxBigInt_fit(the, r);
1313
11.6k
}
1314
1315
txBigInt *fxBigInt_ulsr1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1316
63.5k
{
1317
63.5k
  int wsz, bsz;
1318
63.5k
  int i, n;
1319
1320
63.5k
  wsz = sw / mxBigIntWordSize;
1321
63.5k
  bsz = sw % mxBigIntWordSize;
1322
63.5k
  n = a->size - wsz;
1323
63.5k
  if (n <= 0) {
1324
3.02k
    if (r == NULL) r = fxBigInt_alloc(the, 1);
1325
3.02k
    r->size = 1;
1326
3.02k
    r->data[0] = 0;
1327
3.02k
    return(r);
1328
3.02k
  }
1329
  /* 'r' can be the same as 'a' */
1330
60.5k
  if (r == NULL)
1331
4.43k
    r = fxBigInt_alloc(the, n);
1332
60.5k
  if (bsz == 0) {
1333
38.6k
    c_memmove(r->data, &a->data[wsz], n * sizeof(txU4));
1334
38.6k
    r->size = n;
1335
38.6k
  }
1336
21.9k
  else {
1337
44.1k
    for (i = 0; i < n - 1; i++)
1338
22.1k
      r->data[i] = (a->data[i + wsz] >> bsz) | (a->data[i + wsz + 1] << (mxBigIntWordSize - bsz));
1339
21.9k
    r->data[i] = a->data[i + wsz] >> bsz;
1340
21.9k
    r->size = (n > 1 && r->data[n - 1] == 0) ? n - 1: n;
1341
21.9k
  }
1342
60.5k
  return(r);
1343
63.5k
}
1344
1345
txBigInt *fxBigInt_nop(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1346
0
{
1347
0
#ifndef mxCompile
1348
0
  mxTypeError("no such operation");
1349
0
#endif
1350
0
  return C_NULL;
1351
0
}
1352
1353
/*
1354
 * arithmetic
1355
 */
1356
1357
txBigInt *fxBigInt_add(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1358
944k
{
1359
944k
  if ((aa->sign ^ bb->sign) == 0) {
1360
749k
    rr = fxBigInt_uadd(the, rr, aa, bb);
1361
749k
    if (aa->sign)
1362
1.48k
      rr->sign = 1;
1363
749k
  }
1364
194k
  else {
1365
194k
    if (!aa->sign)
1366
1.33k
      rr = fxBigInt_usub(the, rr, aa, bb);
1367
193k
    else
1368
193k
      rr = fxBigInt_usub(the, rr, bb, aa);
1369
194k
  }
1370
944k
  mxBigInt_meter(rr->size);
1371
944k
  return(rr);
1372
944k
}
1373
1374
txBigInt *fxBigInt_dec(txMachine* the, txBigInt *r, txBigInt *a)
1375
0
{
1376
0
  return fxBigInt_sub(the, r, a, (txBigInt *)&gxBigIntOne);
1377
0
}
1378
1379
txBigInt *fxBigInt_inc(txMachine* the, txBigInt *r, txBigInt *a)
1380
940k
{
1381
940k
  return fxBigInt_add(the, r, a, (txBigInt *)&gxBigIntOne);
1382
940k
}
1383
1384
txBigInt *fxBigInt_neg(txMachine* the, txBigInt *r, txBigInt *a)
1385
2.96M
{
1386
2.96M
  if (r == NULL)
1387
2.96M
    r = fxBigInt_alloc(the, a->size);
1388
2.96M
  fxBigInt_copy(r, a);
1389
2.96M
  if (!fxBigInt_iszero(a))
1390
2.57M
    r->sign = !a->sign;
1391
2.96M
  return(r);
1392
2.96M
}
1393
1394
txBigInt *fxBigInt_sub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1395
154k
{
1396
154k
  if ((aa->sign ^ bb->sign) == 0) {
1397
153k
    if (!aa->sign)
1398
153k
      rr = fxBigInt_usub(the, rr, aa, bb);
1399
222
    else
1400
222
      rr = fxBigInt_usub(the, rr, bb, aa);
1401
153k
  }
1402
206
  else {
1403
206
    txU1 sign = aa->sign; /* could be replaced if rr=aa */
1404
206
    rr = fxBigInt_uadd(the, rr, aa, bb);
1405
206
    rr->sign = sign;
1406
206
  }
1407
154k
  mxBigInt_meter(rr->size);
1408
154k
  return(rr);
1409
154k
}
1410
1411
#if __has_builtin(__builtin_add_overflow)
1412
static int fxBigInt_uadd_prim(txU4 *rp, txU4 *ap, txU4 *bp, int an, int bn)
1413
2.78M
{
1414
2.78M
  txU4 c = 0;
1415
2.78M
  int i;
1416
1417
5.57M
  for (i = 0; i < an; i++) {
1418
#ifdef __ets__
1419
  unsigned int r;
1420
  if (__builtin_uadd_overflow(ap[i], bp[i], &r)) {
1421
    rp[i] = r + c;
1422
    c = 1;
1423
  }
1424
  else {
1425
    unsigned int t;
1426
    c = __builtin_uadd_overflow(r, c, &t);
1427
    rp[i] = t;
1428
  }
1429
#else
1430
2.78M
    unsigned int r = rp[i];
1431
2.78M
    c = __builtin_uadd_overflow(ap[i], bp[i], &r) | (txU4)__builtin_uadd_overflow(r, c, &r);
1432
2.78M
    rp[i] = r;
1433
2.78M
#endif
1434
2.78M
  }
1435
2.79M
  for (; c && (i < bn); i++) { {
1436
6.70k
    unsigned int t;
1437
6.70k
    c = __builtin_uadd_overflow(1, bp[i], &t);
1438
6.70k
    rp[i] = t;
1439
6.70k
  }
1440
6.70k
  }
1441
5.11M
  for (; i < bn; i++) {
1442
2.32M
    rp[i] = bp[i];
1443
2.32M
  }
1444
2.78M
  return(c);
1445
2.78M
}
1446
#else
1447
static int fxBigInt_uadd_prim(txU4 *rp, txU4 *ap, txU4 *bp, int an, int bn)
1448
{
1449
  txU4 a, b, t, r, c = 0;
1450
  int i;
1451
1452
  for (i = 0; i < an; i++) {
1453
    a = ap[i];
1454
    b = bp[i];
1455
    t = a + b;
1456
    r = t + c;
1457
    rp[i] = r;
1458
    c = t < a || r < t;
1459
  }
1460
  for (; i < bn; i++) {
1461
    r = bp[i] + c;
1462
    rp[i] = r;
1463
    c = r < c;
1464
  }
1465
  return(c);
1466
}
1467
#endif
1468
1469
txBigInt *fxBigInt_uadd(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1470
2.78M
{
1471
2.78M
  txBigInt *x, *y;
1472
2.78M
  int c;
1473
1474
2.78M
  if (aa->size < bb->size) {
1475
246
    x = aa;
1476
246
    y = bb;
1477
246
  }
1478
2.78M
  else {
1479
2.78M
    x = bb;
1480
2.78M
    y = aa;
1481
2.78M
  }
1482
2.78M
  if (rr == NULL)
1483
961k
    rr = fxBigInt_alloc(the, y->size + 1);
1484
2.78M
  c = fxBigInt_uadd_prim(rr->data, x->data, y->data, x->size, y->size);
1485
  /* CAUTION: rr may equals aa or bb. do not touch until here */
1486
2.78M
  rr->size = y->size;
1487
2.78M
  rr->sign = 0;
1488
2.78M
  if (c != 0)
1489
3.10k
    rr->data[rr->size++] = 1;
1490
2.78M
  return(rr);
1491
2.78M
}
1492
1493
txBigInt *fxBigInt_usub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1494
637k
{
1495
637k
  int i, n;
1496
637k
  txU4 a, b, r, t;
1497
637k
  txU4 *ap, *bp, *rp;
1498
637k
  txU4 c = 0;
1499
1500
637k
  if (rr == NULL)
1501
451k
    rr = fxBigInt_alloc(the, MAX(aa->size, bb->size));
1502
637k
  rr->sign = (aa->size < bb->size ||
1503
573k
        (aa->size == bb->size && fxBigInt_ucomp(aa, bb) < 0));
1504
637k
  if (rr->sign) {
1505
196k
    txBigInt *tt = aa;
1506
196k
    aa = bb;
1507
196k
    bb = tt;
1508
196k
  }
1509
637k
  ap = aa->data;
1510
637k
  bp = bb->data;
1511
637k
  rp = rr->data;
1512
637k
  n = MIN(aa->size, bb->size);
1513
120M
  for (i = 0; i < n; i++) {
1514
120M
    a = ap[i];
1515
120M
    b = bp[i];
1516
120M
    t = a - b;
1517
120M
    r = t - c;
1518
120M
    rp[i] = r;
1519
120M
    c = a < b || r > t;
1520
120M
  }
1521
637k
  if (aa->size >= bb->size) {
1522
637k
    n = aa->size;
1523
2.03M
    for (; i < n; i++) {
1524
1.39M
      t = ap[i];
1525
1.39M
      r = t - c;
1526
1.39M
      rp[i] = r;
1527
1.39M
      c = r > t;
1528
1.39M
    }
1529
637k
  }
1530
0
  else {
1531
0
    n = bb->size;
1532
0
    for (; i < n; i++) {
1533
0
      t = 0 - bp[i];
1534
0
      r = t - c;
1535
0
      rp[i] = r;
1536
0
      c = r > t;
1537
0
    }
1538
0
  }
1539
  /* remove leading 0s */
1540
703k
  while (--i > 0 && rp[i] == 0)
1541
66.3k
    ;
1542
637k
  rr->size = i + 1;
1543
637k
  return(rr);
1544
637k
}
1545
1546
txBigInt *fxBigInt_mul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1547
65.6k
{
1548
65.6k
  rr = fxBigInt_umul(the, rr, aa, bb);
1549
65.6k
  if ((aa->sign != bb->sign) && !fxBigInt_iszero(rr))
1550
448
    rr->sign = 1;
1551
65.6k
  mxBigInt_meter(rr->size);
1552
65.6k
  return(rr);
1553
65.6k
}
1554
1555
txBigInt *fxBigInt_umul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1556
135k
{
1557
135k
  txU8 a, b, r;
1558
135k
  txU4 *ap, *bp, *rp;
1559
135k
  txU4 c = 0;
1560
135k
  int i, j, n, m;
1561
1562
135k
  if (rr == NULL)
1563
65.6k
    rr = fxBigInt_alloc(the, aa->size + bb->size);
1564
135k
  fxBigInt_fill0(rr);
1565
135k
  ap = aa->data;
1566
135k
  bp = bb->data;
1567
135k
  rp = rr->data;
1568
135k
  n = bb->size;
1569
308k
  for (i = 0, j = 0; i < n; i++) {
1570
173k
    b = (txU8)bp[i];
1571
173k
    c = 0;
1572
173k
    m = aa->size;
1573
6.47M
    for (j = 0; j < m; j++) {
1574
6.30M
      a = (txU8)ap[j];
1575
6.30M
      r = a * b + c;
1576
6.30M
      r += (txU8)rp[i + j];
1577
6.30M
      rp[i + j] = mxBigIntLowWord(r);
1578
6.30M
      c = mxBigIntHighWord(r);
1579
6.30M
    }
1580
173k
    rp[i + j] = c;
1581
173k
  }
1582
  /* remove leading 0s */
1583
265k
  for (n = i + j; --n > 0 && rp[n] == 0;)
1584
129k
    ;
1585
135k
  rr->size = n + 1;
1586
135k
  rr->sign = 0;
1587
135k
  return(rr);
1588
135k
}
1589
1590
txBigInt *fxBigInt_umul1(txMachine* the, txBigInt *r, txBigInt *a, txU4 b)
1591
309k
{
1592
309k
  int i, n;
1593
309k
  txU4 c = 0;
1594
309k
  txU4 *ap, *rp;
1595
309k
  txU8 wa, wr;
1596
1597
309k
  if (r == NULL)
1598
12.3k
    r = fxBigInt_alloc(the, a->size + 1);
1599
309k
  ap = a->data;
1600
309k
  rp = r->data;
1601
309k
  n = a->size;
1602
121M
  for (i = 0; i < n; i++) {
1603
120M
    wa = (txU8)ap[i];
1604
120M
    wr = wa * b + c;
1605
120M
    c = mxBigIntHighWord(wr);
1606
120M
    rp[i] = mxBigIntLowWord(wr);
1607
120M
  }
1608
309k
  if (c != 0)
1609
84.6k
    rp[i] = c;
1610
224k
  else {
1611
    /* remove leading 0s */
1612
446k
    while (--i > 0 && rp[i] == 0)
1613
221k
      ;
1614
224k
  }
1615
309k
  r->size = i + 1;
1616
309k
  return(r);
1617
309k
}
1618
1619
txBigInt *fxBigInt_exp(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1620
13.4k
{
1621
13.4k
#ifndef mxCompile
1622
13.4k
  if (b->sign)
1623
2
    mxRangeError("negative exponent");
1624
13.4k
#endif
1625
13.4k
  if (fxBigInt_iszero(b)) {
1626
1.11k
    if (r == NULL)
1627
1.11k
      r = fxBigInt_alloc(the, 1);
1628
0
    else {
1629
0
      r->size = 1;
1630
0
      r->sign = 0;
1631
0
    }
1632
1.11k
    r->data[0] = 1;
1633
1.11k
  }
1634
12.3k
  else {
1635
12.3k
    int i = fxBigInt_bitsize(b) - 1;
1636
12.3k
    int odd = fxBigInt_isset(b, i);
1637
12.3k
    txU4 c = fxBigInt_bitsize(a);
1638
12.3k
    txBigInt *t = fxBigInt_umul1(the, NULL, b, c);
1639
12.3k
    t = fxBigInt_ulsr1(the, t, t, 5);
1640
12.3k
#ifndef mxCompile
1641
12.3k
    if ((t->size > 1) || (t->data[0] > 0xFFFF))
1642
15
      mxRangeError("too big exponent");
1643
12.3k
#endif
1644
12.3k
    c = 2 + t->data[0];
1645
12.3k
    fxBigInt_free(the, t);
1646
12.3k
        if (r == NULL)
1647
12.3k
      r = fxBigInt_alloc(the, c);
1648
12.3k
      t = fxBigInt_alloc(the, c);
1649
12.3k
        fxBigInt_copy(r, a);
1650
138k
    while (i > 0) {
1651
126k
            i--;
1652
126k
      t = fxBigInt_sqr(the, t, r);
1653
126k
      if ((odd = fxBigInt_isset(b, i))) {
1654
69.6k
        r->size = c;
1655
69.6k
        r = fxBigInt_umul(the, r, t, a);
1656
69.6k
      }
1657
56.5k
      else {
1658
56.5k
        txBigInt u = *r;
1659
56.5k
        *r = *t;
1660
56.5k
        *t = u;
1661
56.5k
      }
1662
126k
      t->size = c;
1663
126k
    }
1664
12.3k
    r->sign = a->sign & odd;
1665
12.3k
    fxBigInt_free(the, t);
1666
12.3k
  }
1667
13.4k
  mxBigInt_meter(r->size);
1668
13.4k
  return(r);
1669
13.4k
}
1670
1671
txBigInt *fxBigInt_sqr(txMachine* the, txBigInt *r, txBigInt *a)
1672
126k
{
1673
126k
  int i, j, t;
1674
126k
  txU4 *ap, *rp;
1675
126k
  txU8 uv, t1, t2, t3, ai;
1676
126k
  txU4 c, cc;
1677
126k
  txU4 overflow = 0;  /* overflow flag of 'u' */
1678
1679
126k
  if (r == NULL)
1680
0
    r = fxBigInt_alloc(the, a->size * 2);
1681
126k
  fxBigInt_fill0(r);
1682
126k
  t = a->size;
1683
126k
  ap = a->data;
1684
126k
  rp = r->data;
1685
1686
916k
  for (i = 0; i < t - 1; i++) {
1687
790k
    uv = (txU8)ap[i] * ap[i] + rp[i * 2];
1688
790k
    rp[i * 2] = mxBigIntLowWord(uv);
1689
790k
    c = mxBigIntHighWord(uv);
1690
790k
    cc = 0;
1691
790k
    ai = ap[i];
1692
209M
    for (j = i + 1; j < t; j++) {
1693
208M
      int k = i + j;
1694
208M
      t1 = ai * ap[j];
1695
208M
      t2 = t1 + c + ((txU8)cc << mxBigIntWordSize);  /* 'cc:c' must be <= 2(b-1) so no overflow here */
1696
208M
      t3 = t1 + t2;
1697
208M
      uv = t3 + rp[k];
1698
208M
      cc = t3 < t1 || uv < t3;
1699
208M
      c = (txU4)mxBigIntHighWord(uv);
1700
208M
      rp[k] = mxBigIntLowWord(uv);
1701
208M
    }
1702
790k
    c += overflow;
1703
790k
    rp[i + t] = c;    /* c = u */
1704
790k
    overflow = cc || c < overflow;
1705
790k
  }
1706
  /* the last loop */
1707
126k
  uv = (txU8)ap[i] * ap[i] + rp[i * 2];
1708
126k
  rp[i * 2] = mxBigIntLowWord(uv);
1709
126k
  rp[i + t] = mxBigIntHighWord(uv) + overflow;
1710
1711
  /* remove leading 0s */
1712
241k
  for (i = 2*t; --i > 0 && rp[i] == 0;)
1713
114k
    ;
1714
126k
  r->size = i + 1;
1715
126k
  return(r);
1716
126k
}
1717
1718
txBigInt *fxBigInt_div(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b)
1719
156k
{
1720
156k
#ifndef mxCompile
1721
156k
  if (fxBigInt_iszero(b))
1722
1
    mxRangeError("zero divider");
1723
156k
#endif
1724
156k
  q = fxBigInt_udiv(the, q, a, b, C_NULL);
1725
156k
  if (!fxBigInt_iszero(q)) {
1726
152k
    if (a->sign)
1727
148k
      q->sign = !q->sign;
1728
152k
    if (b->sign)
1729
147k
      q->sign = !q->sign;
1730
152k
  }
1731
156k
  mxBigInt_meter(q->size);
1732
156k
  return(q);
1733
156k
}
1734
1735
txBigInt *fxBigInt_mod(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1736
0
{
1737
0
  txBigInt *q;
1738
0
  mxBigInt_meter(((a->size - b->size) * (a->size + b->size)));
1739
0
  if (r == NULL)
1740
0
    r = fxBigInt_alloc(the, a->sign ? b->size : MIN(a->size, b->size));
1741
0
  q = fxBigInt_udiv(the, NULL, a, b, &r);
1742
0
  if (a->sign) {
1743
0
    if (!fxBigInt_iszero(r))
1744
0
      r = fxBigInt_sub(the, r, b, r);
1745
0
  }
1746
0
  fxBigInt_free(the, q);
1747
0
  mxBigInt_meter(r->size);
1748
0
  return(r);
1749
0
}
1750
1751
txBigInt *fxBigInt_rem(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1752
145k
{
1753
145k
  txBigInt *q;
1754
145k
#ifndef mxCompile
1755
145k
  if (fxBigInt_iszero(b))
1756
3
    mxRangeError("zero divider");
1757
145k
#endif
1758
145k
  mxBigInt_meter(((a->size - b->size) * (a->size + b->size)));
1759
145k
  if (r == NULL)
1760
145k
    r = fxBigInt_alloc(the, MIN(a->size, b->size));
1761
145k
  q = fxBigInt_udiv(the, NULL, a, b, &r);
1762
145k
  if (a->sign) {
1763
141k
    if (!fxBigInt_iszero(r))
1764
141k
            r->sign = !r->sign;
1765
141k
  }
1766
145k
  fxBigInt_free(the, q);
1767
145k
  mxBigInt_meter(r->size);
1768
145k
  return(r);
1769
145k
}
1770
1771
static void fxBigInt_makepoly(txBigInt *r, txBigInt *a, int t)
1772
196k
{
1773
196k
  int n;
1774
196k
  txU4 *rp, *ap;
1775
1776
  /* make up a polynomial a_t*b^n + a_{t-1}*b^{n-1} + ... */
1777
196k
  n = r->size;
1778
196k
  rp = &r->data[n];
1779
196k
  ap = a->data;
1780
627k
  while (--n >= 0) {
1781
430k
    *--rp = t < 0 ? 0: ap[t];
1782
430k
    --t;
1783
430k
  }
1784
196k
}
1785
1786
#if BN_NO_ULDIVMOD
1787
static txU8
1788
div64_32(txU8 a, txU4 b)
1789
{
1790
  txU4 high = (txU4)(a >> 32);
1791
  txU8 r = 0, bb = b, d = 1;
1792
1793
  if (high >= b) {
1794
    high /= b;
1795
    r = (txU8)high << 32;
1796
    a -= (txU8)(high * b) << 32;
1797
  }
1798
  while ((long long)bb > 0 && bb < a) {
1799
    bb += bb;
1800
    d += d;
1801
  }
1802
  do {
1803
    if (a >= bb) {
1804
      a -= bb;
1805
      r += d;
1806
    }
1807
    bb >>= 1;
1808
    d >>= 1;
1809
  } while (d != 0);
1810
  return r;
1811
}
1812
#endif
1813
1814
txBigInt *fxBigInt_udiv(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b, txBigInt **r)
1815
302k
{
1816
302k
  int sw;
1817
302k
  txBigInt *nb, *na, *tb, *a2, *b2, *tb2, *tb3;
1818
302k
  int i, n, t;
1819
302k
  txU4 *qp, *ap, *bp;
1820
302k
#define mk_dword(p, i)  (((txU8)(p)[i] << mxBigIntWordSize) | (p)[i - 1])
1821
1822
302k
  if (fxBigInt_ucomp(a, b) < 0) {
1823
144k
    if (q == NULL) {
1824
144k
      q = fxBigInt_alloc(the, 1);
1825
144k
    }
1826
0
    else {
1827
0
      q->sign = 0;
1828
0
      q->size = 1;
1829
0
    }
1830
144k
    q->data[0] = 0;
1831
144k
    if (r != NULL) {
1832
140k
      if (*r == NULL)
1833
0
        *r = fxBigInt_dup(the, a);
1834
140k
      else
1835
140k
        fxBigInt_copy(*r, a);
1836
140k
      (*r)->sign = 0;
1837
140k
    }
1838
144k
    return(q);
1839
144k
  }
1840
1841
  /* CAUTION: if q is present, it must take account of normalization */
1842
157k
  if (q == NULL)
1843
157k
    q = fxBigInt_alloc(the, a->size - b->size + 2);
1844
157k
  if (r != NULL && *r == NULL)
1845
0
    *r = fxBigInt_alloc(the, b->size);
1846
1847
  /* normalize */
1848
157k
  sw = fxBigInt_ffs(b);
1849
157k
  nb = fxBigInt_ulsl1(the, NULL, b, sw);
1850
157k
  na = fxBigInt_ulsl1(the, NULL, a, sw);
1851
157k
  t = nb->size - 1; /* the size must not change from 'b' */
1852
157k
  n = na->size - 1;
1853
1854
  /* adjust size of q */
1855
157k
  q->size = na->size - nb->size + 1;
1856
157k
  fxBigInt_fill0(q);  /* set 0 to quotient */
1857
1858
  /* process the most significant word */
1859
157k
  tb = fxBigInt_ulsl1(the, NULL, nb, (n - t) * mxBigIntWordSize);  /* y*b^n */
1860
157k
  if (fxBigInt_ucomp(na, tb) >= 0) {
1861
147k
    q->data[q->size - 1]++;
1862
147k
    fxBigInt_sub(C_NULL, na, na, tb);
1863
    /* since nomalization done, must be na < tb here */
1864
147k
  }
1865
1866
  /* prepare the constant for the adjustment: y_t*b + y_{t-1} */
1867
157k
  b2 = fxBigInt_alloc(the, 2);
1868
157k
  fxBigInt_makepoly(b2, nb, t);
1869
  /* and allocate for temporary buffer */
1870
157k
  a2 = fxBigInt_alloc(the, 3);
1871
157k
  tb2 = fxBigInt_alloc(the, 3);
1872
157k
  tb3 = fxBigInt_alloc(the, tb->size + 1);
1873
1874
157k
  qp = &q->data[q->size - 1];
1875
157k
  ap = na->data;
1876
157k
  bp = nb->data;
1877
196k
  for (i = n; i >= t + 1; --i) {
1878
38.6k
    txU4 tq;
1879
1880
    /* the first estimate */
1881
#if BN_NO_ULDIVMOD
1882
    tq = (ap[i] == bp[t]) ? ~(txU4)0: (txU4)div64_32(mk_dword(ap, i), bp[t]);
1883
#else
1884
38.6k
    tq = (ap[i] == bp[t]) ? ~(txU4)0: (txU4)(mk_dword(ap, i) / bp[t]);
1885
38.6k
#endif
1886
1887
    /* adjust */
1888
38.6k
    fxBigInt_makepoly(a2, na, i);
1889
39.0k
    while (fxBigInt_ucomp(fxBigInt_umul1(the, tb2, b2, tq), a2) > 0)
1890
392
      --tq;
1891
1892
    /* next x */
1893
38.6k
    fxBigInt_ulsr1(the, tb, tb, mxBigIntWordSize);
1894
38.6k
    fxBigInt_usub(the, na, na, fxBigInt_umul1(the, tb3, tb, tq));
1895
38.6k
    if (na->sign) {
1896
0
      fxBigInt_add(the, na, na, tb);
1897
0
      --tq;
1898
0
    }
1899
38.6k
    *--qp = tq;
1900
38.6k
  }
1901
157k
  if (r != NULL)
1902
5.11k
    *r = fxBigInt_ulsr1(the, *r, na, sw);
1903
  /* remove leading 0s from q */
1904
168k
  for (i = q->size; --i > 0 && q->data[i] == 0;)
1905
10.4k
    ;
1906
157k
  q->size = i + 1;
1907
  
1908
157k
  fxBigInt_free(the, tb3);
1909
157k
  fxBigInt_free(the, tb2);
1910
157k
  fxBigInt_free(the, a2);
1911
157k
  fxBigInt_free(the, b2);
1912
157k
  fxBigInt_free(the, tb);
1913
157k
  fxBigInt_free(the, na);
1914
157k
  fxBigInt_free(the, nb);
1915
157k
  return(q);
1916
302k
}
1917
1918
#ifdef mxMetering
1919
void fxBigInt_meter(txMachine* the, int n)
1920
2.29M
{
1921
2.29M
  n--;
1922
2.29M
  the->meterIndex += n * XS_BIGINT_METERING;
1923
2.29M
}
1924
#endif
1925