Coverage Report

Created: 2026-03-07 07:04

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-2025  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
194k
#define howmany(x, y) (((x) + (y) - 1) / (y))
49
#endif
50
#ifndef MAX
51
539k
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
52
#endif
53
#ifndef MIN
54
902k
#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.08M
#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
28.3k
{
78
28.3k
  txSlot* slot;
79
28.3k
  mxPush(mxObjectPrototype);
80
28.3k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
81
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
82
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG);
83
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG);
84
28.3k
  slot = fxNextStringXProperty(the, slot, "BigInt", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
85
28.3k
  mxBigIntPrototype = *the->stack;
86
28.3k
  slot = fxBuildHostConstructor(the, mxCallback(fx_BigInt), 1, mxID(_BigInt));
87
28.3k
  mxBigIntConstructor = *the->stack;
88
28.3k
  slot = fxLastProperty(the, slot);
89
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asIntN), 2, mxID(_asIntN), XS_DONT_ENUM_FLAG);
90
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_asUintN), 2, mxID(_asUintN), XS_DONT_ENUM_FLAG);
91
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_bitLength), 1, mxID(_bitLength), XS_DONT_ENUM_FLAG);
92
28.3k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_BigInt_fromArrayBuffer), 1, mxID(_fromArrayBuffer), XS_DONT_ENUM_FLAG);
93
28.3k
  mxPop();
94
28.3k
}
95
96
void fx_BigInt(txMachine* the)
97
701
{
98
701
  if (mxTarget->kind != XS_UNDEFINED_KIND)
99
0
    mxTypeError("new: BigInt");
100
701
  if (mxArgc > 0)
101
701
    *mxResult = *mxArgv(0);
102
701
  fxToPrimitive(the, mxResult, XS_NUMBER_HINT);
103
701
  if (mxResult->kind == XS_NUMBER_KIND) {
104
168
    int fpclass = c_fpclassify(mxResult->value.number);
105
168
    txNumber check = c_trunc(mxResult->value.number);
106
168
    if ((fpclass != C_FP_NAN) && (fpclass != C_FP_INFINITE) && (mxResult->value.number == check))
107
149
      fxNumberToBigInt(the, mxResult);
108
19
    else
109
19
      mxRangeError("cannot coerce number to bigint");
110
168
  }
111
533
  else if (mxResult->kind == XS_INTEGER_KIND) {
112
125
    fxIntegerToBigInt(the, mxResult);
113
125
  }
114
408
  else
115
408
    fxToBigInt(the, mxResult, 1);
116
701
}
117
118
txNumber fx_BigInt_asAux(txMachine* the)
119
10.9k
{
120
10.9k
  txNumber value, index;
121
10.9k
  if (mxArgc < 1)
122
3
    index = 0;
123
10.9k
  else {
124
10.9k
    if (mxArgv(0)->kind == XS_UNDEFINED_KIND)
125
1.03k
      index = 0;
126
9.87k
    else {
127
9.87k
      value = fxToNumber(the, mxArgv(0));
128
9.87k
      if (c_isnan(value))
129
17
        index = 0;
130
9.85k
      else {
131
9.85k
        value = c_trunc(value);
132
9.85k
        if (value < 0)
133
13
          mxRangeError("index < 0");
134
9.84k
        index = value;
135
9.84k
        if (index <= 0)
136
2.56k
          index = 0;
137
7.27k
        else if (index > C_MAX_SAFE_INTEGER)
138
4
          index = C_MAX_SAFE_INTEGER;
139
9.84k
        if (value != index)
140
4
          mxRangeError("invalid index");
141
9.84k
      }
142
9.87k
    }
143
10.9k
  }
144
10.8k
  if (mxArgc < 2)
145
7
    mxTypeError("no bigint");
146
10.8k
  return index;
147
10.8k
}
148
149
void fx_BigInt_asIntN(txMachine* the)
150
5.87k
{
151
5.87k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
152
5.87k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
153
5.87k
  txBigInt* result;
154
5.87k
  if (fxBigInt_iszero(arg)) {
155
1.07k
    result = fxBigInt_dup(the, arg);
156
1.07k
  }
157
4.80k
  else {
158
4.80k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
159
4.80k
    txBigInt* mask = fxBigInt_usub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
160
4.80k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
161
4.80k
    if ((arg->sign) && !fxBigInt_iszero(result))
162
2.28k
      result = fxBigInt_usub(the, C_NULL, bits, result);
163
4.80k
    if (index && fxBigInt_comp(result, fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index - 1)) >= 0)
164
2.20k
      result = fxBigInt_sub(the, C_NULL, result, bits);
165
4.80k
    result = fxBigInt_fit(the, result);
166
4.80k
  }
167
5.87k
  mxResult->value.bigint = *result;
168
5.87k
  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.87k
}
176
177
void fx_BigInt_asUintN(txMachine* the)
178
5.03k
{
179
5.03k
  txInteger index = (txInteger)fx_BigInt_asAux(the);
180
5.03k
  txBigInt* arg = fxToBigInt(the, mxArgv(1), 1);
181
5.03k
  txBigInt* result;
182
5.03k
  if (fxBigInt_iszero(arg)) {
183
1.85k
    result = fxBigInt_dup(the, arg);
184
1.85k
  }
185
3.18k
  else {
186
3.18k
    txBigInt* bits = fxBigInt_ulsl1(the, C_NULL, (txBigInt *)&gxBigIntOne, index);
187
3.18k
    txBigInt* mask = fxBigInt_sub(the, C_NULL, bits, (txBigInt *)&gxBigIntOne);
188
3.18k
    result = fxBigInt_uand(the, C_NULL, arg, mask);
189
3.18k
    if ((arg->sign) && !fxBigInt_iszero(result))
190
465
      result = fxBigInt_usub(the, C_NULL, bits, result);
191
3.18k
    result = fxBigInt_fit(the, result);
192
3.18k
  }
193
5.03k
  mxResult->value.bigint = *result;
194
5.03k
  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.03k
}
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
73
    radix = 10;
278
243k
  else if (mxIsUndefined(mxArgv(0)))
279
15
    radix = 10;
280
243k
  else {
281
243k
    radix = fxToInteger(the, mxArgv(0));
282
243k
    if ((radix < 2) || (36 < radix))
283
20
      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
923
{
292
923
  txSlot* slot = fxBigIntCheck(the, mxThis);
293
923
  if (!slot) mxTypeError("this: not a bigint");
294
18
  mxResult->kind = slot->kind;
295
18
  mxResult->value = slot->value;
296
18
}
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
922
    txSlot* instance = it->value.reference;
321
922
    it = instance->next;
322
922
    if ((it) && (it->flag & XS_INTERNAL_FLAG) && ((it->kind == XS_BIGINT_KIND) || (it->kind == XS_BIGINT_X_KIND)))
323
20
      result = it;
324
922
  }
325
244k
  return result;
326
244k
}
327
328
void fxBigIntCoerce(txMachine* the, txSlot* slot)
329
319
{
330
319
  fxToBigInt(the, slot, 1);
331
319
}
332
333
txBoolean fxBigIntCompare(txMachine* the, txBoolean less, txBoolean equal, txBoolean more, txSlot* left, txSlot* right)
334
3.04M
{
335
3.04M
  int result;
336
3.04M
  txNumber delta = 0;
337
3.04M
  if (mxBigIntIsNaN(&left->value.bigint))
338
0
    return less & more & !equal;
339
3.04M
  if (right->kind == XS_STRING_KIND)
340
965k
    fxStringToBigInt(the, right, 1);
341
3.04M
  if ((right->kind != XS_BIGINT_KIND) && (right->kind != XS_BIGINT_X_KIND)) {
342
2.01M
    fxToNumber(the, right);
343
2.01M
    result = c_fpclassify(right->value.number);
344
2.01M
    if (result == C_FP_NAN)
345
111k
      return less & more & !equal;
346
1.90M
    if (result == C_FP_INFINITE)
347
2.38k
      return (right->value.number > 0) ? less : more;
348
1.90M
    delta = right->value.number - c_trunc(right->value.number);
349
1.90M
    fxNumberToBigInt(the, right);
350
1.90M
  }
351
2.92M
  if (mxBigIntIsNaN(&right->value.bigint))
352
793k
    return less & more & !equal;
353
2.13M
  result = fxBigInt_comp(&left->value.bigint, &right->value.bigint);
354
2.13M
  if (result < 0)
355
530k
    return less;
356
1.60M
    if (result > 0)
357
1.59M
        return more;
358
5.70k
  if (delta > 0)
359
2.60k
    return less;
360
3.09k
  if (delta < 0)
361
2.01k
    return more;
362
1.08k
  return equal;
363
3.09k
}
364
365
void fxBigIntDecode(txMachine* the, txSize size)
366
5.56M
{
367
5.56M
  txBigInt* bigint;
368
5.56M
  mxPushUndefined();
369
5.56M
  bigint = &the->stack->value.bigint;
370
5.56M
  bigint->data = fxNewChunk(the, size);
371
5.56M
  bigint->size = size >> 2;
372
5.56M
  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.56M
  c_memcpy(bigint->data, the->code, size);
388
5.56M
#endif  
389
5.56M
  the->stack->kind = XS_BIGINT_KIND;
390
5.56M
}
391
392
#endif  
393
394
void fxBigIntEncode(txByte* code, txBigInt* bigint, txSize size)
395
16.3k
{
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
16.3k
  c_memcpy(code, bigint->data, size);
411
16.3k
#endif
412
16.3k
}
413
414
#ifndef mxCompile
415
txSlot* fxBigIntToInstance(txMachine* the, txSlot* slot)
416
1.17M
{
417
1.17M
  txSlot* instance;
418
1.17M
  txSlot* internal;
419
1.17M
  mxPush(mxBigIntPrototype);
420
1.17M
  instance = fxNewObjectInstance(the);
421
1.17M
  internal = instance->next = fxNewSlot(the);
422
1.17M
  internal->flag = XS_INTERNAL_FLAG;
423
1.17M
  internal->kind = slot->kind;
424
1.17M
  internal->value = slot->value;
425
1.17M
  if (the->frame->flag & XS_STRICT_FLAG)
426
1
    instance->flag |= XS_DONT_PATCH_FLAG;
427
1.17M
  mxPullSlot(slot);
428
1.17M
  return instance;
429
1.17M
}
430
#endif
431
432
txSize fxBigIntMeasure(txBigInt* bigint)
433
49.1k
{
434
49.1k
  return bigint->size * sizeof(txU4);
435
49.1k
}
436
437
txSize fxBigIntMaximum(txSize length)
438
104k
{
439
104k
  return sizeof(txU4) * (1 + (((txSize)c_ceil((txNumber)length * c_log(10) / c_log(2))) / 32));
440
104k
}
441
442
txSize fxBigIntMaximumB(txSize length)
443
287
{
444
287
  return sizeof(txU4) * (1 + howmany(1, mxBigIntWordSize) + (length / 32));
445
287
}
446
447
txSize fxBigIntMaximumO(txSize length)
448
277
{
449
277
  return sizeof(txU4) * (1 + howmany(3, mxBigIntWordSize) + ((length * 3) / 32));
450
277
}
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
104k
{
459
104k
  txU4 data[1] = { 0 };
460
104k
  txBigInt digit = { .sign=0, .size=1, .data=data };
461
104k
  bigint->sign = 0;
462
104k
  bigint->size = 1;
463
104k
  bigint->data[0] = 0;
464
355k
  while (length) {
465
251k
    char c = *p++;
466
251k
    data[0] = c - '0';
467
251k
    fxBigInt_uadd(NULL, bigint, fxBigInt_umul1(NULL, bigint, bigint, 10), &digit);
468
251k
    length--;
469
251k
  }
470
104k
  if ((bigint->size > 1) || (bigint->data[0] != 0))
471
76.0k
    bigint->sign = sign;
472
104k
}
473
474
void fxBigIntParseB(txBigInt* bigint, txString p, txSize length)
475
287
{
476
287
  txU4 data[1] = { 0 };
477
287
  txBigInt digit = { .sign=0, .size=1, .data=data };
478
287
  bigint->sign = 0;
479
287
  bigint->size = 1;
480
287
  bigint->data[0] = 0;
481
2.28k
  while (length) {
482
2.00k
    char c = *p++;
483
2.00k
    data[0] = c - '0';
484
2.00k
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 1), &digit);
485
2.00k
    length--;
486
2.00k
  }
487
287
}
488
489
void fxBigIntParseO(txBigInt* bigint, txString p, txSize length)
490
277
{
491
277
  txU4 data[1] = { 0 };
492
277
  txBigInt digit = { .sign=0, .size=1, .data=data };
493
277
  bigint->sign = 0;
494
277
  bigint->size = 1;
495
277
  bigint->data[0] = 0;
496
1.78k
  while (length) {
497
1.50k
    char c = *p++;
498
1.50k
    data[0] = c - '0';
499
1.50k
    fxBigInt_uadd(NULL, bigint, fxBigInt_ulsl1(NULL, bigint, bigint, 3), &digit);
500
1.50k
    length--;
501
1.50k
  }
502
277
}
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
348k
      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
400
{
573
400
  txBigInt* bigint = &slot->value.bigint;
574
400
  txNumber base = 4294967296;
575
400
  txNumber number = 0;
576
400
  int i;
577
20.7k
  for (i = bigint->size; --i >= 0;)
578
20.3k
    number = (number * base) + bigint->data[i];
579
400
  if (bigint->sign)
580
7
    number = -number;
581
400
  slot->value.number = number;
582
400
  slot->kind = XS_NUMBER_KIND;
583
400
  return number;
584
400
}
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, 470427017},
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, 20537907},
620
    { 5, 24300000},
621
    { 5, 28430241},
622
    { 5, 32768000},
623
    { 5, 39135393},
624
    { 5, 45435424},
625
    { 5, 52521875},
626
    { 5, 60466176}
627
};
628
629
void fxBigintToString(txMachine* the, txSlot* slot, txU4 radix)
630
308k
{
631
308k
  static const char gxDigits[] ICACHE_FLASH_ATTR = "0123456789abcdefghijklmnopqrstuvwxyz";
632
308k
  txSize length, offset;
633
308k
  txSlot* result;
634
308k
  txSlot* stack;
635
308k
  if (0 == radix) radix = 10;
636
308k
  const BaseChunk32 *bc = base_chunks32 + (radix - 2);
637
638
308k
  if (mxBigIntIsNaN(&slot->value.bigint)) {
639
0
    fxStringX(the, slot, "NaN");
640
0
    return;
641
0
  }
642
308k
  mxBigInt_meter(slot->value.bigint.size);
643
644
308k
  mxPushUndefined();
645
308k
  result = the->stack;
646
  
647
308k
  mxPushSlot(slot);
648
308k
  fxBigInt_dup(the, &the->stack->value.bigint);
649
308k
  stack = the->stack;
650
651
308k
  length = 1 + bc->k + (txSize)c_ceil((txNumber)(stack->value.bigint.size + 1) * 32 * c_log(2) / c_log(radix));
652
308k
  if (stack->value.bigint.sign)
653
83.9k
    length++;
654
308k
  offset = length;
655
308k
  result->value.string = fxNewChunk(the, length);
656
308k
  result->kind = XS_STRING_KIND;
657
658
308k
  result->value.string[--offset] = 0;
659
308k
  int32_t nonZeroWords = stack->value.bigint.size;
660
309k
  do {
661
309k
    uint64_t carry = 0;
662
1.99M
    for (uint32_t i = stack->value.bigint.size - 1, count = nonZeroWords, base_to_k = bc->base_to_k; count > 0; --count) {
663
1.68M
      carry = (carry << 32) | stack->value.bigint.data[i];
664
1.68M
      stack->value.bigint.data[i--] = (uint32_t)(carry / base_to_k);
665
1.68M
      carry %= base_to_k;
666
1.68M
    }
667
309k
    uint32_t remainder = (uint32_t)carry, k = bc->k;
668
8.12M
    do {
669
8.12M
      result->value.string[--offset] = c_read8(gxDigits + (remainder % radix));
670
8.12M
            remainder /= radix;
671
8.12M
        } while (--k);
672
673
619k
    while (nonZeroWords && (0 == stack->value.bigint.data[stack->value.bigint.size - nonZeroWords]))
674
309k
      nonZeroWords -= 1;
675
309k
  } while (nonZeroWords);
676
677
7.39M
  while (('0' == result->value.string[offset]) && result->value.string[offset + 1])
678
7.09M
    offset++;
679
680
308k
  if (stack->value.bigint.sign)
681
83.9k
    result->value.string[--offset] = '-';
682
308k
  c_memmove(result->value.string, result->value.string + offset, length - offset);
683
684
308k
  mxPop();
685
308k
  mxPop();
686
308k
  mxPullSlot(slot);
687
308k
}
688
689
txS8 fxToBigInt64(txMachine* the, txSlot* slot)
690
229
{
691
229
  return (txS8)fxToBigUint64(the, slot);
692
229
}
693
694
txU8 fxToBigUint64(txMachine* the, txSlot* slot)
695
447
{
696
447
  txBigInt* bigint = fxToBigInt(the, slot, 1);
697
447
  txU8 result = bigint->data[0];
698
447
  if (bigint->size > 1)
699
177
    result |= (txU8)(bigint->data[1]) << 32;
700
447
  if (bigint->sign && result) {
701
39
    result--;
702
39
    result = 0xFFFFFFFFFFFFFFFFll - result;
703
39
  }
704
447
  return result;
705
447
}
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
1.90M
{
726
1.90M
  txBigInt* bigint = &slot->value.bigint;
727
1.90M
  txNumber number = slot->value.number;
728
1.90M
  txNumber limit = 4294967296;
729
1.90M
  txU1 sign = 0;
730
1.90M
  txU2 size = 1;
731
1.90M
  if (number < 0) {
732
1.27M
    number = -number;
733
1.27M
    sign = 1;
734
1.27M
  }
735
2.14M
  while (number >= limit) {
736
248k
    size++;
737
248k
    number /= limit;
738
248k
  }
739
1.90M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
740
1.90M
  bigint->size = size;
741
4.05M
  while (size > 0) {
742
2.14M
    txU4 part = (txU4)number;
743
2.14M
    number -= part;
744
2.14M
    size--;
745
2.14M
    bigint->data[size] = part;
746
2.14M
    number *= limit;
747
2.14M
  }
748
1.90M
  bigint->sign = sign;
749
1.90M
  slot->kind = XS_BIGINT_KIND;
750
1.90M
  return bigint;
751
1.90M
}
752
753
txBigInt* fxStringToBigInt(txMachine* the, txSlot* slot, txFlag whole)
754
986k
{
755
986k
  txString s = slot->value.string;
756
986k
  txString p = fxSkipSpaces(s);
757
986k
  txSize offset, length;
758
986k
  txInteger sign = 0;
759
986k
  txBigInt bigint = {.sign=0, .size=0, .data=C_NULL };
760
986k
  char c = *p;
761
986k
  if (c == '0') {
762
964k
    char d = *(p + 1);
763
964k
    if (whole && ((d == 'B') || (d == 'b') || (d == 'O') || (d == 'o') || (d == 'X') || (d == 'x'))) {
764
829k
      p += 2;
765
829k
      offset = mxPtrDiff(p - s);
766
829k
      if ((d == 'B') || (d == 'b')) {
767
1.46k
        while (((c = *p)) && ('0' <= c) && (c <= '1'))
768
1.17k
          p++;
769
288
        length = mxPtrDiff(p - s - offset);
770
288
        p = fxSkipSpaces(p);
771
288
        if ((length > 0) && (*p == 0)) {
772
269
          bigint.data = fxNewChunk(the, fxBigIntMaximumB(length));
773
269
          fxBigIntParseB(&bigint, slot->value.string + offset, length);
774
269
        }
775
288
      }
776
829k
      else if ((d == 'O') || (d == 'o')) {
777
313
        while (((c = *p)) && ('0' <= c) && (c <= '7'))
778
275
          p++;
779
38
        length = mxPtrDiff(p - s - offset);
780
38
        p = fxSkipSpaces(p);
781
38
        if ((length > 0) && (*p == 0)) {
782
26
          bigint.data = fxNewChunk(the, fxBigIntMaximumO(length));
783
26
          fxBigIntParseO(&bigint, slot->value.string + offset, length);
784
26
        }
785
38
      }
786
829k
      else if ((d == 'X') || (d == 'x')) {
787
5.38M
        while (((c = *p)) && ((('0' <= c) && (c <= '9')) || (('a' <= c) && (c <= 'f')) || (('A' <= c) && (c <= 'F'))))
788
4.55M
          p++;
789
829k
        length = mxPtrDiff(p - s - offset);
790
829k
        p = fxSkipSpaces(p);
791
829k
        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
829k
      }
796
829k
      goto bail;
797
829k
    }
798
964k
  }
799
157k
  if (c == '-') {
800
80
    sign = 1;
801
80
    p++;
802
80
  }
803
157k
  else if (c == '+') {
804
146
    p++;
805
146
  }
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
986k
bail:
816
986k
  if (bigint.data) {
817
193k
    slot->value.bigint = bigint;
818
193k
    slot->kind = XS_BIGINT_KIND;
819
193k
  }
820
793k
  else {
821
793k
    slot->value.bigint = gxBigIntNaN;
822
793k
    slot->kind = XS_BIGINT_X_KIND;
823
793k
  }
824
986k
  return &slot->value.bigint;
825
157k
}
826
827
txBigInt* fxToBigInt(txMachine* the, txSlot* slot, txFlag strict)
828
33.1k
{
829
33.2k
again:
830
33.2k
  switch (slot->kind) {
831
3.16k
  case XS_BOOLEAN_KIND:
832
3.16k
    slot->value.bigint = *fxBigInt_dup(the, (txBigInt *)((slot->value.boolean) ? &gxBigIntOne : &gxBigIntZero));
833
3.16k
    slot->kind = XS_BIGINT_KIND;
834
3.16k
    break;
835
11
  case XS_INTEGER_KIND:
836
11
    if (strict)
837
11
      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
78
      mxSyntaxError("cannot coerce string to bigint");
850
21.5k
    break;
851
21.5k
  case XS_BIGINT_KIND:
852
8.27k
  case XS_BIGINT_X_KIND:
853
8.27k
    break;
854
16
  case XS_SYMBOL_KIND:
855
16
    mxTypeError("cannot coerce symbol to bigint");
856
0
    break;
857
113
  case XS_REFERENCE_KIND:
858
113
    fxToPrimitive(the, slot, XS_NUMBER_HINT);
859
113
    goto again;
860
24
  default:
861
24
    mxTypeError("cannot coerce to bigint");
862
0
    break;
863
33.2k
  }
864
32.9k
  return &slot->value.bigint;
865
33.2k
}
866
867
void fxFromBigInt64(txMachine* the, txSlot* slot, txS8 it)
868
204
{
869
204
  txU1 sign = 0;
870
204
  txU8 value = 0;
871
204
  if (it < 0) {
872
75
    if (it & 0x7FFFFFFFFFFFFFFFll)
873
75
      value = -it;
874
0
    else
875
0
      value = (txU8)it;
876
75
    sign = 1;
877
75
  }
878
129
  else
879
129
    value = it;
880
204
  if (value > 0x00000000FFFFFFFFll) {
881
109
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
882
109
    slot->value.bigint.data[0] = (txU4)value;
883
109
    slot->value.bigint.data[1] = (txU4)(value >> 32);
884
109
    slot->value.bigint.size = 2;
885
109
  }
886
95
  else {
887
95
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
888
95
    slot->value.bigint.data[0] = (txU4)value;
889
95
    slot->value.bigint.size = 1;
890
95
  }
891
204
    slot->value.bigint.sign = sign;
892
204
  slot->kind = XS_BIGINT_KIND;
893
204
}
894
895
void fxFromBigUint64(txMachine* the, txSlot* slot, txU8 value)
896
129
{
897
129
  if (value > 0x00000000FFFFFFFFll) {
898
81
    slot->value.bigint.data = fxNewChunk(the, 2 * sizeof(txU4));
899
81
    slot->value.bigint.data[0] = (txU4)(value);
900
81
    slot->value.bigint.data[1] = (txU4)(value >> 32);
901
81
    slot->value.bigint.size = 2;
902
81
  }
903
48
  else {
904
48
    slot->value.bigint.data = fxNewChunk(the, sizeof(txU4));
905
48
    slot->value.bigint.data[0] = (txU4)value;
906
48
    slot->value.bigint.size = 1;
907
48
  }
908
129
    slot->value.bigint.sign = 0;
909
129
  slot->kind = XS_BIGINT_KIND;
910
129
}
911
912
#endif
913
914
txBigInt *fxBigInt_alloc(txMachine* the, txU4 size)
915
6.87M
{
916
6.87M
#ifndef mxCompile
917
6.87M
  txBigInt* bigint;
918
6.87M
  if (size > 0xFFFF) {
919
2
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
920
2
  }
921
6.87M
  mxPushUndefined();
922
6.87M
  bigint = &the->stack->value.bigint;
923
6.87M
  bigint->data = fxNewChunk(the, fxMultiplyChunkSizes(the, size, sizeof(txU4)));
924
6.87M
  bigint->size = size;
925
6.87M
  bigint->sign = 0;
926
6.87M
  the->stack->kind = XS_BIGINT_KIND;
927
6.87M
  return bigint;
928
#else
929
  return NULL;
930
#endif
931
6.87M
}
932
933
void fxBigInt_free(txMachine* the, txBigInt *bigint)
934
2.35M
{
935
2.35M
#ifndef mxCompile
936
2.35M
  if (bigint == &the->stack->value.bigint)
937
2.35M
    the->stack++;
938
//  else
939
//    fprintf(stderr, "oops\n");
940
2.35M
#endif
941
2.35M
}
942
943
int fxBigInt_comp(txBigInt *a, txBigInt *b)
944
2.13M
{
945
2.13M
  if (a->sign != b->sign)
946
1.20M
    return(a->sign ? -1: 1);
947
933k
  else if (a->sign)
948
358k
    return(fxBigInt_ucomp(b, a));
949
574k
  else
950
574k
    return(fxBigInt_ucomp(a, b));
951
2.13M
}
952
953
int fxBigInt_ucomp(txBigInt *a, txBigInt *b)
954
2.20M
{
955
2.20M
  int i;
956
957
2.20M
  if (a->size != b->size)
958
207k
    return(a->size > b->size ? 1: -1);
959
3.11M
  for (i = a->size; --i >= 0;) {
960
2.41M
    if (a->data[i] != b->data[i])
961
1.29M
      return(a->data[i] > b->data[i] ? 1: -1);
962
2.41M
  }
963
699k
  return(0);
964
1.99M
}
965
966
int fxBigInt_iszero(txBigInt *a)
967
3.31M
{
968
3.31M
  return(a->size == 1 && a->data[0] == 0);
969
3.31M
}
970
971
void
972
fxBigInt_fill0(txBigInt *r)
973
555k
{
974
555k
  c_memset(r->data, 0, r->size * sizeof(txU4));
975
555k
}
976
977
void fxBigInt_copy(txBigInt *a, txBigInt *b)
978
3.04M
{
979
3.04M
  c_memmove(a->data, b->data, b->size * sizeof(txU4));
980
3.04M
  a->size = b->size;
981
3.04M
  a->sign = b->sign;
982
3.04M
}
983
984
txBigInt *fxBigInt_dup(txMachine* the, txBigInt *a)
985
314k
{
986
314k
  txBigInt *r = fxBigInt_alloc(the, a->size);
987
314k
  fxBigInt_copy(r, a);
988
314k
  return(r);
989
314k
}
990
991
int
992
fxBigInt_ffs(txBigInt *a)
993
279k
{
994
279k
  int i;
995
279k
  txU4 w = a->data[a->size - 1];
996
997
8.87M
  for (i = 0; i < (int)mxBigIntWordSize && !(w & ((txU4)1 << (mxBigIntWordSize - 1 - i))); i++)
998
8.59M
    ;
999
279k
  return(i);
1000
279k
}
1001
1002
txBigInt *fxBigInt_fit(txMachine* the, txBigInt *r)
1003
258k
{
1004
258k
  int i = r->size;
1005
278k
  while (i > 0) {
1006
267k
    i--;
1007
267k
    if (r->data[i])
1008
247k
      break;
1009
267k
  }
1010
258k
  r->size = (txU2)(i + 1);
1011
258k
  mxBigInt_meter(r->size);
1012
258k
  return r;
1013
258k
}
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
47.6k
{
1026
47.6k
  int i = a->size;
1027
47.6k
  txU4 w;
1028
76.1k
  while (i > 0) {
1029
47.6k
    i--;
1030
47.6k
    w = a->data[i];
1031
47.6k
    if (w) {
1032
19.2k
      int c = __builtin_clz(w);
1033
19.2k
      return (i * mxBigIntWordSize) + mxBigIntWordSize - c;
1034
19.2k
    }
1035
47.6k
  }
1036
28.4k
  return 0;
1037
47.6k
}
1038
#endif
1039
1040
int fxBigInt_isset(txBigInt *e, txU4 i)
1041
148k
{
1042
148k
  txU4 w = e->data[i / mxBigIntWordSize];
1043
148k
  return((w & ((txU4)1 << (i % mxBigIntWordSize))) != 0);
1044
148k
}
1045
1046
/*
1047
 * bitwise operations
1048
 */
1049
1050
txBigInt *fxBigInt_not(txMachine* the, txBigInt *r, txBigInt *a)
1051
115k
{
1052
115k
  if (a->sign)
1053
1.81k
    r = fxBigInt_usub(the, r, a, (txBigInt *)&gxBigIntOne);
1054
113k
  else {
1055
113k
    r = fxBigInt_uadd(the, r, a, (txBigInt *)&gxBigIntOne);
1056
113k
    r->sign = 1;
1057
113k
  }
1058
115k
  return(r);
1059
115k
}
1060
1061
txBigInt *fxBigInt_and(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1062
12.8k
{
1063
12.8k
  txBigInt *aa, *bb;
1064
12.8k
  int i;
1065
12.8k
  if (a->sign) {
1066
6.68k
    if (b->sign)
1067
6.13k
      goto AND_MINUS_MINUS;
1068
546
    aa = a;
1069
546
    a = b;
1070
546
    b = aa; 
1071
546
    goto AND_PLUS_MINUS;
1072
6.68k
  }
1073
6.16k
  if (b->sign)
1074
4.60k
    goto AND_PLUS_MINUS;
1075
1.56k
  return fxBigInt_fit(the, fxBigInt_uand(the, r, a, b));
1076
5.15k
AND_PLUS_MINUS:
1077
// GMP: OP1 & -OP2 == OP1 & ~(OP2 - 1)
1078
5.15k
  if (r == NULL)
1079
5.15k
    r = fxBigInt_alloc(the, a->size);
1080
5.15k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1081
5.15k
    if (a->size > bb->size) {
1082
3.57k
    for (i = 0; i < bb->size; i++)
1083
1.90k
      r->data[i] = a->data[i] & ~bb->data[i];
1084
4.16k
    for (; i < a->size; i++)
1085
2.49k
      r->data[i] = a->data[i];
1086
1.67k
    }
1087
3.48k
    else {
1088
7.44k
    for (i = 0; i < a->size; i++)
1089
3.96k
      r->data[i] = a->data[i] & ~bb->data[i];
1090
3.48k
    }
1091
5.15k
    fxBigInt_free(the, bb);
1092
5.15k
  return fxBigInt_fit(the, r);
1093
6.13k
AND_MINUS_MINUS:
1094
// GMP: -((-OP1) & (-OP2)) = -(~(OP1 - 1) & ~(OP2 - 1)) == ~(~(OP1 - 1) & ~(OP2 - 1)) + 1 == ((OP1 - 1) | (OP2 - 1)) + 1
1095
6.13k
  if (r == NULL)
1096
6.13k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1097
6.13k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1098
6.13k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1099
6.13k
  r = fxBigInt_uor(the, r, aa, bb);
1100
6.13k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1101
6.13k
  r->sign = 1;
1102
6.13k
  fxBigInt_free(the, bb);
1103
6.13k
  fxBigInt_free(the, aa);
1104
6.13k
  return fxBigInt_fit(the, r);
1105
6.16k
}
1106
1107
txBigInt *fxBigInt_uand(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1108
16.6k
{
1109
16.6k
  int i;
1110
16.6k
  if (a->size > b->size) {
1111
8.61k
    txBigInt *t = b;
1112
8.61k
    b = a;
1113
8.61k
    a = t;
1114
8.61k
  }
1115
16.6k
  if (r == NULL)
1116
9.41k
    r = fxBigInt_alloc(the, a->size);
1117
7.25k
  else
1118
7.25k
    r->size = a->size;
1119
141k
  for (i = 0; i < a->size; i++)
1120
124k
    r->data[i] = a->data[i] & b->data[i];
1121
16.6k
  return(r);
1122
16.6k
}
1123
1124
txBigInt *fxBigInt_or(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1125
145k
{
1126
145k
  txBigInt *aa, *bb;
1127
145k
  int i;
1128
145k
  mxBigInt_meter(MAX(a->size, b->size));
1129
145k
  if (a->sign) {
1130
143k
    if (b->sign)
1131
7.25k
      goto OR_MINUS_MINUS;
1132
135k
    aa = a;
1133
135k
    a = b;
1134
135k
    b = aa; 
1135
135k
    goto OR_PLUS_MINUS;
1136
143k
  }
1137
2.19k
  if (b->sign)
1138
1.76k
    goto OR_PLUS_MINUS;
1139
428
  return fxBigInt_fit(the, fxBigInt_uor(the, r, a, b));
1140
137k
OR_PLUS_MINUS:
1141
// GMP: -(OP1 | (-OP2)) = -(OP1 | ~(OP2 - 1)) == ~(OP1 | ~(OP2 - 1)) + 1 == (~OP1 & (OP2 - 1)) + 1
1142
137k
  if (r == NULL)
1143
137k
    r = fxBigInt_alloc(the, b->size + 1);
1144
137k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1145
137k
    if (a->size < bb->size) {
1146
262k
    for (i = 0; i < a->size; i++)
1147
131k
      r->data[i] = ~a->data[i] & bb->data[i];
1148
401k
    for (; i < bb->size; i++)
1149
270k
      r->data[i] = bb->data[i];
1150
131k
  }
1151
6.35k
  else {
1152
13.0k
    for (i = 0; i < bb->size; i++)
1153
6.64k
      r->data[i] = ~a->data[i] & bb->data[i];
1154
6.35k
  }
1155
137k
  r->size = bb->size;
1156
137k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1157
137k
  r->sign = 1;
1158
137k
  fxBigInt_free(the, bb);
1159
137k
  return fxBigInt_fit(the, r);
1160
7.25k
OR_MINUS_MINUS:
1161
// GMP: -((-OP1) | (-OP2)) = -(~(OP1 - 1) | ~(OP2 - 1)) == ~(~(OP1 - 1) | ~(OP2 - 1)) + 1 = = ((OP1 - 1) & (OP2 - 1)) + 1
1162
7.25k
  if (r == NULL)
1163
7.25k
    r = fxBigInt_alloc(the, MIN(a->size, b->size) + 1);
1164
7.25k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1165
7.25k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1166
7.25k
  r = fxBigInt_uand(the, r, aa, bb);
1167
7.25k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1168
7.25k
  r->sign = 1;
1169
7.25k
  fxBigInt_free(the, bb);
1170
7.25k
  fxBigInt_free(the, aa);
1171
7.25k
  return fxBigInt_fit(the, r);
1172
2.19k
}
1173
1174
txBigInt *fxBigInt_uor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1175
6.56k
{
1176
6.56k
  int i;
1177
6.56k
  if (a->size < b->size) {
1178
2.61k
    txBigInt *t = b;
1179
2.61k
    b = a;
1180
2.61k
    a = t;
1181
2.61k
  }
1182
6.56k
  if (r == NULL)
1183
428
    r = fxBigInt_alloc(the, a->size);
1184
6.13k
  else
1185
6.13k
    r->size = a->size;
1186
14.3k
  for (i = 0; i < b->size; i++)
1187
7.78k
    r->data[i] = a->data[i] | b->data[i];
1188
50.3k
  for (; i < a->size; i++)
1189
43.7k
    r->data[i] = a->data[i];
1190
6.56k
  return(r);
1191
6.56k
}
1192
1193
txBigInt *fxBigInt_xor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1194
79.1k
{
1195
79.1k
  txBigInt *aa, *bb;
1196
79.1k
  if (a->sign) {
1197
1.69k
    if (b->sign)
1198
1.17k
      goto XOR_MINUS_MINUS;
1199
517
    aa = a;
1200
517
    a = b;
1201
517
    b = aa; 
1202
517
    goto XOR_PLUS_MINUS;
1203
1.69k
  }
1204
77.4k
  if (b->sign)
1205
73.7k
    goto XOR_PLUS_MINUS;
1206
3.73k
  return fxBigInt_fit(the, fxBigInt_uxor(the, r, a, b));
1207
74.2k
XOR_PLUS_MINUS:
1208
// GMP: -(OP1 ^ (-OP2)) == -(OP1 ^ ~(OP2 - 1)) == ~(OP1 ^ ~(OP2 - 1)) + 1 == (OP1 ^ (OP2 - 1)) + 1
1209
74.2k
  if (r == NULL)
1210
74.2k
    r = fxBigInt_alloc(the, MAX(a->size, b->size) + 1);
1211
74.2k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1212
74.2k
  r = fxBigInt_uxor(the, r, a, bb);
1213
74.2k
  r = fxBigInt_uadd(the, r, r, (txBigInt *)&gxBigIntOne);
1214
74.2k
  r->sign = 1;
1215
74.2k
  fxBigInt_free(the, bb);
1216
74.2k
  return fxBigInt_fit(the, r);
1217
1.17k
XOR_MINUS_MINUS:
1218
// GMP: (-OP1) ^ (-OP2) == ~(OP1 - 1) ^ ~(OP2 - 1) == (OP1 - 1) ^ (OP2 - 1)
1219
1.17k
  if (r == NULL)
1220
1.17k
    r = fxBigInt_alloc(the, MAX(a->size, b->size));
1221
1.17k
  aa = fxBigInt_usub(the, NULL, a, (txBigInt *)&gxBigIntOne);
1222
1.17k
  bb = fxBigInt_usub(the, NULL, b, (txBigInt *)&gxBigIntOne);
1223
1.17k
  r = fxBigInt_uxor(the, r, aa, bb);
1224
1.17k
  fxBigInt_free(the, bb);
1225
1.17k
  fxBigInt_free(the, aa);
1226
1.17k
  return fxBigInt_fit(the, r);
1227
77.4k
}
1228
1229
txBigInt *fxBigInt_uxor(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1230
79.1k
{
1231
79.1k
  int i;
1232
79.1k
  if (a->size < b->size) {
1233
1.22k
    txBigInt *t = b;
1234
1.22k
    b = a;
1235
1.22k
    a = t;
1236
1.22k
  }
1237
79.1k
  if (r == NULL)
1238
3.73k
    r = fxBigInt_alloc(the, a->size);
1239
75.4k
  else
1240
75.4k
    r->size = a->size;
1241
163k
  for (i = 0; i < b->size; i++)
1242
84.0k
    r->data[i] = a->data[i] ^ b->data[i];
1243
983k
  for (; i < a->size; i++)
1244
904k
    r->data[i] = a->data[i];
1245
79.1k
  return(r);
1246
79.1k
}
1247
1248
txBigInt *fxBigInt_lsl(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1249
941
{
1250
941
  if (b->sign) {
1251
662
    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
621
    else
1258
621
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1259
662
  }
1260
279
  else {
1261
279
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1262
279
    r->sign = a->sign;
1263
279
  }
1264
941
  return fxBigInt_fit(the, r);
1265
941
}
1266
1267
txBigInt *fxBigInt_ulsl1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1268
2.24M
{
1269
2.24M
  txU4 wsz, bsz;
1270
2.24M
  int n;
1271
1272
2.24M
  wsz = sw / mxBigIntWordSize;
1273
2.24M
  bsz = sw % mxBigIntWordSize;
1274
2.24M
  n = a->size + wsz + ((bsz == 0) ? 0 : 1);
1275
2.24M
  if (r == NULL) 
1276
855k
    r = fxBigInt_alloc(the, n);
1277
2.24M
  if (bsz == 0) {
1278
283k
    c_memmove(&r->data[wsz], a->data, a->size * sizeof(txU4));
1279
283k
    c_memset(r->data, 0, wsz * sizeof(txU4));
1280
283k
    r->size = a->size + wsz;
1281
283k
  }
1282
1.96M
  else {
1283
1.96M
    r->data[a->size + wsz] = a->data[a->size - 1] >> (mxBigIntWordSize - bsz);
1284
2.00M
    for (n = a->size; --n >= 1;)
1285
49.1k
      r->data[n + wsz] = (a->data[n] << bsz) | (a->data[n - 1] >> (mxBigIntWordSize - bsz));
1286
1.96M
    r->data[wsz] = a->data[0] << bsz;
1287
    /* clear the remaining part */
1288
3.67M
    for (n = wsz; --n >= 0;)
1289
1.71M
      r->data[n] = 0;
1290
    /* adjust r */
1291
1.96M
    r->size = a->size + wsz + (r->data[a->size + wsz] == 0 ? 0: 1);
1292
1.96M
  }
1293
2.24M
  return(r);
1294
2.24M
}
1295
1296
txBigInt *fxBigInt_lsr(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1297
12.6k
{
1298
12.6k
  if (b->sign) {
1299
4.83k
    r = fxBigInt_ulsl1(the, r, a, b->data[0]);
1300
4.83k
    r->sign = a->sign;
1301
4.83k
  }
1302
7.82k
  else {
1303
7.82k
    if (a->sign) {
1304
4.27k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1305
4.27k
            if (b->data[0])
1306
4.26k
                r = fxBigInt_uadd(the, C_NULL, r, (txBigInt *)&gxBigIntOne);
1307
4.27k
      r->sign = 1;
1308
4.27k
    }
1309
3.54k
    else
1310
3.54k
      r = fxBigInt_ulsr1(the, r, a, b->data[0]);
1311
7.82k
  }
1312
12.6k
  return fxBigInt_fit(the, r);
1313
12.6k
}
1314
1315
txBigInt *fxBigInt_ulsr1(txMachine* the, txBigInt *r, txBigInt *a, txU4 sw)
1316
311k
{
1317
311k
  int wsz, bsz;
1318
311k
  int i, n;
1319
1320
311k
  wsz = sw / mxBigIntWordSize;
1321
311k
  bsz = sw % mxBigIntWordSize;
1322
311k
  n = a->size - wsz;
1323
311k
  if (n <= 0) {
1324
3.35k
    if (r == NULL) r = fxBigInt_alloc(the, 1);
1325
3.35k
    r->size = 1;
1326
3.35k
    r->data[0] = 0;
1327
3.35k
    return(r);
1328
3.35k
  }
1329
  /* 'r' can be the same as 'a' */
1330
307k
  if (r == NULL)
1331
5.12k
    r = fxBigInt_alloc(the, n);
1332
307k
  if (bsz == 0) {
1333
162k
    c_memmove(r->data, &a->data[wsz], n * sizeof(txU4));
1334
162k
    r->size = n;
1335
162k
  }
1336
144k
  else {
1337
166k
    for (i = 0; i < n - 1; i++)
1338
21.2k
      r->data[i] = (a->data[i + wsz] >> bsz) | (a->data[i + wsz + 1] << (mxBigIntWordSize - bsz));
1339
144k
    r->data[i] = a->data[i + wsz] >> bsz;
1340
144k
    r->size = (n > 1 && r->data[n - 1] == 0) ? n - 1: n;
1341
144k
  }
1342
307k
  return(r);
1343
311k
}
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
725k
{
1359
725k
  if ((aa->sign ^ bb->sign) == 0) {
1360
530k
    rr = fxBigInt_uadd(the, rr, aa, bb);
1361
530k
    if (aa->sign)
1362
1.50k
      rr->sign = 1;
1363
530k
  }
1364
194k
  else {
1365
194k
    if (!aa->sign)
1366
1.41k
      rr = fxBigInt_usub(the, rr, aa, bb);
1367
193k
    else
1368
193k
      rr = fxBigInt_usub(the, rr, bb, aa);
1369
194k
  }
1370
725k
  mxBigInt_meter(rr->size);
1371
725k
  return(rr);
1372
725k
}
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
721k
{
1381
721k
  return fxBigInt_add(the, r, a, (txBigInt *)&gxBigIntOne);
1382
721k
}
1383
1384
txBigInt *fxBigInt_neg(txMachine* the, txBigInt *r, txBigInt *a)
1385
2.71M
{
1386
2.71M
  if (r == NULL)
1387
2.71M
    r = fxBigInt_alloc(the, a->size);
1388
2.71M
  fxBigInt_copy(r, a);
1389
2.71M
  if (!fxBigInt_iszero(a))
1390
2.47M
    r->sign = !a->sign;
1391
2.71M
  return(r);
1392
2.71M
}
1393
1394
txBigInt *fxBigInt_sub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1395
154k
{
1396
154k
  if ((aa->sign ^ bb->sign) == 0) {
1397
154k
    if (!aa->sign)
1398
154k
      rr = fxBigInt_usub(the, rr, aa, bb);
1399
213
    else
1400
213
      rr = fxBigInt_usub(the, rr, bb, aa);
1401
154k
  }
1402
203
  else {
1403
203
    txU1 sign = aa->sign; /* could be replaced if rr=aa */
1404
203
    rr = fxBigInt_uadd(the, rr, aa, bb);
1405
203
    rr->sign = sign;
1406
203
  }
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.51M
{
1414
2.51M
  txU4 c = 0;
1415
2.51M
  int i;
1416
1417
5.02M
  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.51M
    unsigned int r = rp[i];
1431
2.51M
    c = __builtin_uadd_overflow(ap[i], bp[i], &r) | (txU4)__builtin_uadd_overflow(r, c, &r);
1432
2.51M
    rp[i] = r;
1433
2.51M
#endif
1434
2.51M
  }
1435
2.52M
  for (; c && (i < bn); i++) { {
1436
7.74k
    unsigned int t;
1437
7.74k
    c = __builtin_uadd_overflow(1, bp[i], &t);
1438
7.74k
    rp[i] = t;
1439
7.74k
  }
1440
7.74k
  }
1441
5.68M
  for (; i < bn; i++) {
1442
3.16M
    rp[i] = bp[i];
1443
3.16M
  }
1444
2.51M
  return(c);
1445
2.51M
}
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.51M
{
1471
2.51M
  txBigInt *x, *y;
1472
2.51M
  int c;
1473
1474
2.51M
  if (aa->size < bb->size) {
1475
82
    x = aa;
1476
82
    y = bb;
1477
82
  }
1478
2.51M
  else {
1479
2.51M
    x = bb;
1480
2.51M
    y = aa;
1481
2.51M
  }
1482
2.51M
  if (rr == NULL)
1483
648k
    rr = fxBigInt_alloc(the, y->size + 1);
1484
2.51M
  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.51M
  rr->size = y->size;
1487
2.51M
  rr->sign = 0;
1488
2.51M
  if (c != 0)
1489
3.53k
    rr->data[rr->size++] = 1;
1490
2.51M
  return(rr);
1491
2.51M
}
1492
1493
txBigInt *fxBigInt_usub(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1494
767k
{
1495
767k
  int i, n;
1496
767k
  txU4 a, b, r, t;
1497
767k
  txU4 *ap, *bp, *rp;
1498
767k
  txU4 c = 0;
1499
1500
767k
  if (rr == NULL)
1501
457k
    rr = fxBigInt_alloc(the, MAX(aa->size, bb->size));
1502
767k
  rr->sign = (aa->size < bb->size ||
1503
703k
        (aa->size == bb->size && fxBigInt_ucomp(aa, bb) < 0));
1504
767k
  if (rr->sign) {
1505
196k
    txBigInt *tt = aa;
1506
196k
    aa = bb;
1507
196k
    bb = tt;
1508
196k
  }
1509
767k
  ap = aa->data;
1510
767k
  bp = bb->data;
1511
767k
  rp = rr->data;
1512
767k
  n = MIN(aa->size, bb->size);
1513
121M
  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
767k
  if (aa->size >= bb->size) {
1522
767k
    n = aa->size;
1523
2.15M
    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
767k
  }
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
975k
  while (--i > 0 && rp[i] == 0)
1541
208k
    ;
1542
767k
  rr->size = i + 1;
1543
767k
  return(rr);
1544
767k
}
1545
1546
txBigInt *fxBigInt_mul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1547
65.1k
{
1548
65.1k
  rr = fxBigInt_umul(the, rr, aa, bb);
1549
65.1k
  if ((aa->sign != bb->sign) && !fxBigInt_iszero(rr))
1550
458
    rr->sign = 1;
1551
65.1k
  mxBigInt_meter(rr->size);
1552
65.1k
  return(rr);
1553
65.1k
}
1554
1555
txBigInt *fxBigInt_umul(txMachine* the, txBigInt *rr, txBigInt *aa, txBigInt *bb)
1556
141k
{
1557
141k
  txU8 a, b, r;
1558
141k
  txU4 *ap, *bp, *rp;
1559
141k
  txU4 c = 0;
1560
141k
  int i, j, n, m;
1561
1562
141k
  if (rr == NULL)
1563
65.1k
    rr = fxBigInt_alloc(the, aa->size + bb->size);
1564
141k
  fxBigInt_fill0(rr);
1565
141k
  ap = aa->data;
1566
141k
  bp = bb->data;
1567
141k
  rp = rr->data;
1568
141k
  n = bb->size;
1569
320k
  for (i = 0, j = 0; i < n; i++) {
1570
179k
    b = (txU8)bp[i];
1571
179k
    c = 0;
1572
179k
    m = aa->size;
1573
6.48M
    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
179k
    rp[i + j] = c;
1581
179k
  }
1582
  /* remove leading 0s */
1583
276k
  for (n = i + j; --n > 0 && rp[n] == 0;)
1584
135k
    ;
1585
141k
  rr->size = n + 1;
1586
141k
  rr->sign = 0;
1587
141k
  return(rr);
1588
141k
}
1589
1590
txBigInt *fxBigInt_umul1(txMachine* the, txBigInt *r, txBigInt *a, txU4 b)
1591
590k
{
1592
590k
  int i, n;
1593
590k
  txU4 c = 0;
1594
590k
  txU4 *ap, *rp;
1595
590k
  txU8 wa, wr;
1596
1597
590k
  if (r == NULL)
1598
13.3k
    r = fxBigInt_alloc(the, a->size + 1);
1599
590k
  ap = a->data;
1600
590k
  rp = r->data;
1601
590k
  n = a->size;
1602
122M
  for (i = 0; i < n; i++) {
1603
122M
    wa = (txU8)ap[i];
1604
122M
    wr = wa * b + c;
1605
122M
    c = mxBigIntHighWord(wr);
1606
122M
    rp[i] = mxBigIntLowWord(wr);
1607
122M
  }
1608
590k
  if (c != 0)
1609
335k
    rp[i] = c;
1610
254k
  else {
1611
    /* remove leading 0s */
1612
476k
    while (--i > 0 && rp[i] == 0)
1613
221k
      ;
1614
254k
  }
1615
590k
  r->size = i + 1;
1616
590k
  return(r);
1617
590k
}
1618
1619
txBigInt *fxBigInt_exp(txMachine* the, txBigInt *r, txBigInt *a, txBigInt *b)
1620
13.8k
{
1621
13.8k
#ifndef mxCompile
1622
13.8k
  if (b->sign)
1623
2
    mxRangeError("negative exponent");
1624
13.8k
#endif
1625
13.8k
  if (fxBigInt_iszero(b)) {
1626
574
    if (r == NULL)
1627
574
      r = fxBigInt_alloc(the, 1);
1628
0
    else {
1629
0
      r->size = 1;
1630
0
      r->sign = 0;
1631
0
    }
1632
574
    r->data[0] = 1;
1633
574
  }
1634
13.3k
  else {
1635
13.3k
    int i = fxBigInt_bitsize(b) - 1;
1636
13.3k
    int odd = fxBigInt_isset(b, i);
1637
13.3k
    txU4 c = fxBigInt_bitsize(a);
1638
13.3k
    txBigInt *t = fxBigInt_umul1(the, NULL, b, c);
1639
13.3k
    t = fxBigInt_ulsr1(the, t, t, 5);
1640
13.3k
#ifndef mxCompile
1641
13.3k
    if ((t->size > 1) || (t->data[0] > 0xFFFF))
1642
18
      mxRangeError("too big exponent");
1643
13.2k
#endif
1644
13.2k
    c = 2 + t->data[0];
1645
13.2k
    fxBigInt_free(the, t);
1646
13.2k
        if (r == NULL)
1647
13.2k
      r = fxBigInt_alloc(the, c);
1648
13.2k
      t = fxBigInt_alloc(the, c);
1649
13.2k
        fxBigInt_copy(r, a);
1650
148k
    while (i > 0) {
1651
135k
            i--;
1652
135k
      t = fxBigInt_sqr(the, t, r);
1653
135k
      if ((odd = fxBigInt_isset(b, i))) {
1654
75.9k
        r->size = c;
1655
75.9k
        r = fxBigInt_umul(the, r, t, a);
1656
75.9k
      }
1657
59.4k
      else {
1658
59.4k
        txBigInt u = *r;
1659
59.4k
        *r = *t;
1660
59.4k
        *t = u;
1661
59.4k
      }
1662
135k
      t->size = c;
1663
135k
    }
1664
13.2k
    r->sign = a->sign & odd;
1665
13.2k
    fxBigInt_free(the, t);
1666
13.2k
  }
1667
13.8k
  mxBigInt_meter(r->size);
1668
13.8k
  return(r);
1669
13.8k
}
1670
1671
txBigInt *fxBigInt_sqr(txMachine* the, txBigInt *r, txBigInt *a)
1672
135k
{
1673
135k
  int i, j, t;
1674
135k
  txU4 *ap, *rp;
1675
135k
  txU8 uv, t1, t2, t3, ai;
1676
135k
  txU4 c, cc;
1677
135k
  txU4 overflow = 0;  /* overflow flag of 'u' */
1678
1679
135k
  if (r == NULL)
1680
0
    r = fxBigInt_alloc(the, a->size * 2);
1681
135k
  fxBigInt_fill0(r);
1682
135k
  t = a->size;
1683
135k
  ap = a->data;
1684
135k
  rp = r->data;
1685
1686
925k
  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
135k
  uv = (txU8)ap[i] * ap[i] + rp[i * 2];
1708
135k
  rp[i * 2] = mxBigIntLowWord(uv);
1709
135k
  rp[i + t] = mxBigIntHighWord(uv) + overflow;
1710
1711
  /* remove leading 0s */
1712
259k
  for (i = 2*t; --i > 0 && rp[i] == 0;)
1713
123k
    ;
1714
135k
  r->size = i + 1;
1715
135k
  return(r);
1716
135k
}
1717
1718
txBigInt *fxBigInt_div(txMachine* the, txBigInt *q, txBigInt *a, txBigInt *b)
1719
157k
{
1720
157k
#ifndef mxCompile
1721
157k
  if (fxBigInt_iszero(b))
1722
1
    mxRangeError("zero divider");
1723
157k
#endif
1724
157k
  q = fxBigInt_udiv(the, q, a, b, C_NULL);
1725
157k
  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
157k
  mxBigInt_meter(q->size);
1732
157k
  return(q);
1733
157k
}
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
127k
{
1753
127k
  txBigInt *q;
1754
127k
#ifndef mxCompile
1755
127k
  if (fxBigInt_iszero(b))
1756
2
    mxRangeError("zero divider");
1757
127k
#endif
1758
127k
  mxBigInt_meter(((a->size - b->size) * (a->size + b->size)));
1759
127k
  if (r == NULL)
1760
127k
    r = fxBigInt_alloc(the, MIN(a->size, b->size));
1761
127k
  q = fxBigInt_udiv(the, NULL, a, b, &r);
1762
127k
  if (a->sign) {
1763
123k
    if (!fxBigInt_iszero(r))
1764
1.64k
            r->sign = !r->sign;
1765
123k
  }
1766
127k
  fxBigInt_free(the, q);
1767
127k
  mxBigInt_meter(r->size);
1768
127k
  return(r);
1769
127k
}
1770
1771
static void fxBigInt_makepoly(txBigInt *r, txBigInt *a, int t)
1772
442k
{
1773
442k
  int n;
1774
442k
  txU4 *rp, *ap;
1775
1776
  /* make up a polynomial a_t*b^n + a_{t-1}*b^{n-1} + ... */
1777
442k
  n = r->size;
1778
442k
  rp = &r->data[n];
1779
442k
  ap = a->data;
1780
1.48M
  while (--n >= 0) {
1781
1.04M
    *--rp = t < 0 ? 0: ap[t];
1782
1.04M
    --t;
1783
1.04M
  }
1784
442k
}
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
284k
{
1816
284k
  int sw;
1817
284k
  txBigInt *nb, *na, *tb, *a2, *b2, *tb2, *tb3;
1818
284k
  int i, n, t;
1819
284k
  txU4 *qp, *ap, *bp;
1820
284k
#define mk_dword(p, i)  (((txU8)(p)[i] << mxBigIntWordSize) | (p)[i - 1])
1821
1822
284k
  if (fxBigInt_ucomp(a, b) < 0) {
1823
5.17k
    if (q == NULL) {
1824
5.17k
      q = fxBigInt_alloc(the, 1);
1825
5.17k
    }
1826
0
    else {
1827
0
      q->sign = 0;
1828
0
      q->size = 1;
1829
0
    }
1830
5.17k
    q->data[0] = 0;
1831
5.17k
    if (r != NULL) {
1832
904
      if (*r == NULL)
1833
0
        *r = fxBigInt_dup(the, a);
1834
904
      else
1835
904
        fxBigInt_copy(*r, a);
1836
904
      (*r)->sign = 0;
1837
904
    }
1838
5.17k
    return(q);
1839
5.17k
  }
1840
1841
  /* CAUTION: if q is present, it must take account of normalization */
1842
279k
  if (q == NULL)
1843
279k
    q = fxBigInt_alloc(the, a->size - b->size + 2);
1844
279k
  if (r != NULL && *r == NULL)
1845
0
    *r = fxBigInt_alloc(the, b->size);
1846
1847
  /* normalize */
1848
279k
  sw = fxBigInt_ffs(b);
1849
279k
  nb = fxBigInt_ulsl1(the, NULL, b, sw);
1850
279k
  na = fxBigInt_ulsl1(the, NULL, a, sw);
1851
279k
  t = nb->size - 1; /* the size must not change from 'b' */
1852
279k
  n = na->size - 1;
1853
1854
  /* adjust size of q */
1855
279k
  q->size = na->size - nb->size + 1;
1856
279k
  fxBigInt_fill0(q);  /* set 0 to quotient */
1857
1858
  /* process the most significant word */
1859
279k
  tb = fxBigInt_ulsl1(the, NULL, nb, (n - t) * mxBigIntWordSize);  /* y*b^n */
1860
279k
  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
279k
  b2 = fxBigInt_alloc(the, 2);
1868
279k
  fxBigInt_makepoly(b2, nb, t);
1869
  /* and allocate for temporary buffer */
1870
279k
  a2 = fxBigInt_alloc(the, 3);
1871
279k
  tb2 = fxBigInt_alloc(the, 3);
1872
279k
  tb3 = fxBigInt_alloc(the, tb->size + 1);
1873
1874
279k
  qp = &q->data[q->size - 1];
1875
279k
  ap = na->data;
1876
279k
  bp = nb->data;
1877
442k
  for (i = n; i >= t + 1; --i) {
1878
162k
    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
162k
    tq = (ap[i] == bp[t]) ? ~(txU4)0: (txU4)(mk_dword(ap, i) / bp[t]);
1885
162k
#endif
1886
1887
    /* adjust */
1888
162k
    fxBigInt_makepoly(a2, na, i);
1889
163k
    while (fxBigInt_ucomp(fxBigInt_umul1(the, tb2, b2, tq), a2) > 0)
1890
392
      --tq;
1891
1892
    /* next x */
1893
162k
    fxBigInt_ulsr1(the, tb, tb, mxBigIntWordSize);
1894
162k
    fxBigInt_usub(the, na, na, fxBigInt_umul1(the, tb3, tb, tq));
1895
162k
    if (na->sign) {
1896
0
      fxBigInt_add(the, na, na, tb);
1897
0
      --tq;
1898
0
    }
1899
162k
    *--qp = tq;
1900
162k
  }
1901
279k
  if (r != NULL)
1902
126k
    *r = fxBigInt_ulsr1(the, *r, na, sw);
1903
  /* remove leading 0s from q */
1904
411k
  for (i = q->size; --i > 0 && q->data[i] == 0;)
1905
132k
    ;
1906
279k
  q->size = i + 1;
1907
  
1908
279k
  fxBigInt_free(the, tb3);
1909
279k
  fxBigInt_free(the, tb2);
1910
279k
  fxBigInt_free(the, a2);
1911
279k
  fxBigInt_free(the, b2);
1912
279k
  fxBigInt_free(the, tb);
1913
279k
  fxBigInt_free(the, na);
1914
279k
  fxBigInt_free(the, nb);
1915
279k
  return(q);
1916
284k
}
1917
1918
#ifdef mxMetering
1919
void fxBigInt_meter(txMachine* the, int n)
1920
1.93M
{
1921
1.93M
  n--;
1922
1.93M
  the->meterIndex += n * XS_BIGINT_METERING;
1923
1.93M
}
1924
#endif
1925