• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / math / 22080649078

16 Feb 2026 11:27PM UTC coverage: 98.989%. Remained the same
22080649078

push

github

BenMorel
Add changelog entry

1371 of 1385 relevant lines covered (98.99%)

2347.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.07
/src/BigInteger.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Math;
6

7
use Brick\Math\Exception\DivisionByZeroException;
8
use Brick\Math\Exception\IntegerOverflowException;
9
use Brick\Math\Exception\InvalidArgumentException;
10
use Brick\Math\Exception\MathException;
11
use Brick\Math\Exception\NegativeNumberException;
12
use Brick\Math\Exception\NoInverseException;
13
use Brick\Math\Exception\NumberFormatException;
14
use Brick\Math\Exception\RandomSourceException;
15
use Brick\Math\Exception\RoundingNecessaryException;
16
use Brick\Math\Internal\Calculator;
17
use Brick\Math\Internal\CalculatorRegistry;
18
use LogicException;
19
use Override;
20
use Throwable;
21

22
use function assert;
23
use function bin2hex;
24
use function chr;
25
use function count_chars;
26
use function filter_var;
27
use function hex2bin;
28
use function in_array;
29
use function intdiv;
30
use function is_string;
31
use function ltrim;
32
use function ord;
33
use function preg_match;
34
use function preg_quote;
35
use function random_bytes;
36
use function str_repeat;
37
use function strlen;
38
use function strtolower;
39
use function substr;
40

41
use const FILTER_VALIDATE_INT;
42

43
/**
44
 * An arbitrarily large integer number.
45
 *
46
 * This class is immutable.
47
 */
48
final readonly class BigInteger extends BigNumber
49
{
50
    /**
51
     * The value, as a string of digits with optional leading minus sign.
52
     *
53
     * No leading zeros must be present.
54
     * No leading minus sign must be present if the number is zero.
55
     */
56
    private string $value;
57

58
    /**
59
     * Protected constructor. Use a factory method to obtain an instance.
60
     *
61
     * @param string $value A string of digits, with optional leading minus sign.
62
     *
63
     * @pure
64
     */
65
    protected function __construct(string $value)
66
    {
67
        $this->value = $value;
36,209✔
68
    }
69

70
    /**
71
     * Creates a number from a string in a given base.
72
     *
73
     * The string can optionally be prefixed with the `+` or `-` sign.
74
     *
75
     * Bases greater than 36 are not supported by this method, as there is no clear consensus on which of the lowercase
76
     * or uppercase characters should come first. Instead, this method accepts any base up to 36, and does not
77
     * differentiate lowercase and uppercase characters, which are considered equal.
78
     *
79
     * For bases greater than 36, and/or custom alphabets, use the fromArbitraryBase() method.
80
     *
81
     * @param non-empty-string $number The number to convert, in the given base.
82
     * @param int<2, 36>       $base   The base of the number, between 2 and 36.
83
     *
84
     * @throws NumberFormatException    If the number is empty, or contains invalid chars for the given base.
85
     * @throws InvalidArgumentException If the base is out of range.
86
     *
87
     * @pure
88
     */
89
    public static function fromBase(string $number, int $base): BigInteger
90
    {
91
        if ($base < 2 || $base > 36) { // @phpstan-ignore smaller.alwaysFalse, greater.alwaysFalse, booleanOr.alwaysFalse
2,093✔
92
            throw InvalidArgumentException::baseOutOfRange($base);
15✔
93
        }
94

95
        if ($number === '') { // @phpstan-ignore identical.alwaysFalse
2,078✔
96
            throw NumberFormatException::emptyNumber();
3✔
97
        }
98

99
        $originalNumber = $number;
2,075✔
100

101
        if ($number[0] === '-') {
2,075✔
102
            $sign = '-';
27✔
103
            $number = substr($number, 1);
27✔
104
        } elseif ($number[0] === '+') {
2,048✔
105
            $sign = '';
27✔
106
            $number = substr($number, 1);
27✔
107
        } else {
108
            $sign = '';
2,021✔
109
        }
110

111
        if ($number === '') {
2,075✔
112
            throw NumberFormatException::invalidFormat($originalNumber);
6✔
113
        }
114

115
        $number = ltrim($number, '0');
2,069✔
116

117
        if ($number === '') {
2,069✔
118
            // The result will be the same in any base, avoid further calculation.
119
            return BigInteger::zero();
84✔
120
        }
121

122
        if ($number === '1') {
1,988✔
123
            // The result will be the same in any base, avoid further calculation.
124
            return new BigInteger($sign . '1');
75✔
125
        }
126

127
        $pattern = '/[^' . substr(Calculator::ALPHABET, 0, $base) . ']/';
1,916✔
128

129
        if (preg_match($pattern, strtolower($number), $matches) === 1) {
1,916✔
130
            throw NumberFormatException::charNotValidInBase($matches[0], $base);
111✔
131
        }
132

133
        if ($base === 10) {
1,805✔
134
            // The number is usable as is, avoid further calculation.
135
            return new BigInteger($sign . $number);
24✔
136
        }
137

138
        $result = CalculatorRegistry::get()->fromBase($number, $base);
1,781✔
139

140
        return new BigInteger($sign . $result);
1,781✔
141
    }
142

143
    /**
144
     * Parses a string containing an integer in an arbitrary base, using a custom alphabet.
145
     *
146
     * This method is byte-oriented: the alphabet is interpreted as a sequence of single-byte characters.
147
     * Multibyte UTF-8 characters are not supported.
148
     *
149
     * Because this method accepts any single-byte character, including dash, it does not handle negative numbers.
150
     *
151
     * @param non-empty-string $number   The number to parse.
152
     * @param non-empty-string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
153
     *
154
     * @throws NumberFormatException    If the given number is empty or contains invalid chars for the given alphabet.
155
     * @throws InvalidArgumentException If the alphabet does not contain at least 2 chars, or contains duplicates.
156
     *
157
     * @pure
158
     */
159
    public static function fromArbitraryBase(string $number, string $alphabet): BigInteger
160
    {
161
        $base = strlen($alphabet);
423✔
162

163
        if ($base < 2) {
423✔
164
            throw InvalidArgumentException::alphabetTooShort();
6✔
165
        }
166

167
        if (strlen(count_chars($alphabet, 3)) !== $base) {
417✔
168
            throw InvalidArgumentException::duplicateCharsInAlphabet();
9✔
169
        }
170

171
        if ($number === '') { // @phpstan-ignore identical.alwaysFalse
408✔
172
            throw NumberFormatException::emptyNumber();
3✔
173
        }
174

175
        $pattern = '/[^' . preg_quote($alphabet, '/') . ']/';
405✔
176

177
        if (preg_match($pattern, $number, $matches) === 1) {
405✔
178
            throw NumberFormatException::charNotInAlphabet($matches[0]);
27✔
179
        }
180

181
        $number = CalculatorRegistry::get()->fromArbitraryBase($number, $alphabet, $base);
378✔
182

183
        return new BigInteger($number);
378✔
184
    }
185

186
    /**
187
     * Translates a string of bytes containing the binary representation of a BigInteger into a BigInteger.
188
     *
189
     * The input string is assumed to be in big-endian byte-order: the most significant byte is in the zeroth element.
190
     *
191
     * If `$signed` is true, the input is assumed to be in two's-complement representation, and the leading bit is
192
     * interpreted as a sign bit. If `$signed` is false, the input is interpreted as an unsigned number, and the
193
     * resulting BigInteger will always be positive or zero.
194
     *
195
     * This method can be used to retrieve a number exported by `toBytes()`, as long as the `$signed` flags match.
196
     *
197
     * @param non-empty-string $value  The byte string.
198
     * @param bool             $signed Whether to interpret as a signed number in two's-complement representation with a leading
199
     *                                 sign bit.
200
     *
201
     * @throws NumberFormatException If the string is empty.
202
     *
203
     * @pure
204
     */
205
    public static function fromBytes(string $value, bool $signed = true): BigInteger
206
    {
207
        if ($value === '') { // @phpstan-ignore identical.alwaysFalse
1,224✔
208
            throw NumberFormatException::emptyByteString();
3✔
209
        }
210

211
        $twosComplement = false;
1,221✔
212

213
        if ($signed) {
1,221✔
214
            $x = ord($value[0]);
984✔
215

216
            if (($twosComplement = ($x >= 0x80))) {
984✔
217
                $value = ~$value;
906✔
218
            }
219
        }
220

221
        $number = self::fromBase(bin2hex($value), 16);
1,221✔
222

223
        if ($twosComplement) {
1,221✔
224
            return $number->plus(1)->negated();
906✔
225
        }
226

227
        return $number;
315✔
228
    }
229

230
    /**
231
     * Generates a pseudo-random number in the range 0 to 2^bitCount - 1.
232
     *
233
     * Using the default random bytes generator, this method is suitable for cryptographic use.
234
     *
235
     * @param non-negative-int             $bitCount             The number of bits.
236
     * @param (callable(int): string)|null $randomBytesGenerator A function that accepts a number of bytes, and returns
237
     *                                                           a string of random bytes of the given length. Defaults
238
     *                                                           to the `random_bytes()` function.
239
     *
240
     * @throws InvalidArgumentException If $bitCount is negative.
241
     * @throws RandomSourceException    If random byte generation fails.
242
     */
243
    public static function randomBits(int $bitCount, ?callable $randomBytesGenerator = null): BigInteger
244
    {
245
        if ($bitCount < 0) { // @phpstan-ignore smaller.alwaysFalse
183✔
246
            throw InvalidArgumentException::negativeBitCount();
3✔
247
        }
248

249
        if ($bitCount === 0) {
180✔
250
            return BigInteger::zero();
3✔
251
        }
252

253
        /** @var int<1, max> $byteLength */
254
        $byteLength = intdiv($bitCount - 1, 8) + 1;
177✔
255

256
        $extraBits = ($byteLength * 8 - $bitCount);
177✔
257
        $bitmask = chr(0xFF >> $extraBits);
177✔
258

259
        $randomBytes = self::randomBytes($byteLength, $randomBytesGenerator);
177✔
260
        $randomBytes[0] = $randomBytes[0] & $bitmask;
159✔
261

262
        return self::fromBytes($randomBytes, false);
159✔
263
    }
264

265
    /**
266
     * Generates a pseudo-random number between `$min` and `$max`, inclusive.
267
     *
268
     * Using the default random bytes generator, this method is suitable for cryptographic use.
269
     *
270
     * @param BigNumber|int|string         $min                  The lower bound. Must be convertible to a BigInteger.
271
     * @param BigNumber|int|string         $max                  The upper bound. Must be convertible to a BigInteger.
272
     * @param (callable(int): string)|null $randomBytesGenerator A function that accepts a number of bytes, and returns
273
     *                                                           a string of random bytes of the given length. Defaults
274
     *                                                           to the `random_bytes()` function.
275
     *
276
     * @throws MathException            If one of the parameters cannot be converted to a BigInteger.
277
     * @throws InvalidArgumentException If `$min` is greater than `$max`.
278
     * @throws RandomSourceException    If random byte generation fails.
279
     */
280
    public static function randomRange(
281
        BigNumber|int|string $min,
282
        BigNumber|int|string $max,
283
        ?callable $randomBytesGenerator = null,
284
    ): BigInteger {
285
        $min = BigInteger::of($min);
93✔
286
        $max = BigInteger::of($max);
93✔
287

288
        if ($min->isGreaterThan($max)) {
93✔
289
            throw InvalidArgumentException::minGreaterThanMax();
3✔
290
        }
291

292
        if ($min->isEqualTo($max)) {
90✔
293
            return $min;
3✔
294
        }
295

296
        $diff = $max->minus($min);
87✔
297
        $bitLength = $diff->getBitLength();
87✔
298

299
        // try until the number is in range (50% to 100% chance of success)
300
        do {
301
            $randomNumber = self::randomBits($bitLength, $randomBytesGenerator);
87✔
302
        } while ($randomNumber->isGreaterThan($diff));
78✔
303

304
        return $randomNumber->plus($min);
78✔
305
    }
306

307
    /**
308
     * Returns a BigInteger representing zero.
309
     *
310
     * @pure
311
     */
312
    public static function zero(): BigInteger
313
    {
314
        /** @var BigInteger|null $zero */
315
        static $zero;
246✔
316

317
        if ($zero === null) {
246✔
318
            $zero = new BigInteger('0');
×
319
        }
320

321
        return $zero;
246✔
322
    }
323

324
    /**
325
     * Returns a BigInteger representing one.
326
     *
327
     * @pure
328
     */
329
    public static function one(): BigInteger
330
    {
331
        /** @var BigInteger|null $one */
332
        static $one;
600✔
333

334
        if ($one === null) {
600✔
335
            $one = new BigInteger('1');
×
336
        }
337

338
        return $one;
600✔
339
    }
340

341
    /**
342
     * Returns a BigInteger representing ten.
343
     *
344
     * @pure
345
     */
346
    public static function ten(): BigInteger
347
    {
348
        /** @var BigInteger|null $ten */
349
        static $ten;
6✔
350

351
        if ($ten === null) {
6✔
352
            $ten = new BigInteger('10');
3✔
353
        }
354

355
        return $ten;
6✔
356
    }
357

358
    /**
359
     * Returns the greatest common divisor of the given numbers.
360
     *
361
     * The GCD is always positive, unless all numbers are zero, in which case it is zero.
362
     *
363
     * @param BigNumber|int|string $a    The first number. Must be convertible to a BigInteger.
364
     * @param BigNumber|int|string ...$n The additional numbers. Each number must be convertible to a BigInteger.
365
     *
366
     * @throws MathException If one of the parameters cannot be converted to a BigInteger.
367
     *
368
     * @pure
369
     */
370
    public static function gcdAll(BigNumber|int|string $a, BigNumber|int|string ...$n): BigInteger
371
    {
372
        $result = BigInteger::of($a)->abs();
1,587✔
373

374
        foreach ($n as $next) {
1,587✔
375
            $result = $result->gcd(BigInteger::of($next));
1,560✔
376

377
            if ($result->isEqualTo(1)) {
1,560✔
378
                return $result;
345✔
379
            }
380
        }
381

382
        return $result;
1,242✔
383
    }
384

385
    /**
386
     * Returns the least common multiple of the given numbers.
387
     *
388
     * The LCM is always positive, unless one of the numbers is zero, in which case it is zero.
389
     *
390
     * @param BigNumber|int|string $a    The first number. Must be convertible to a BigInteger.
391
     * @param BigNumber|int|string ...$n The additional numbers. Each number must be convertible to a BigInteger.
392
     *
393
     * @throws MathException If one of the parameters cannot be converted to a BigInteger.
394
     *
395
     * @pure
396
     */
397
    public static function lcmAll(BigNumber|int|string $a, BigNumber|int|string ...$n): BigInteger
398
    {
399
        $result = BigInteger::of($a)->abs();
324✔
400

401
        foreach ($n as $next) {
324✔
402
            $result = $result->lcm(BigInteger::of($next));
297✔
403

404
            if ($result->isZero()) {
297✔
405
                return $result;
51✔
406
            }
407
        }
408

409
        return $result;
273✔
410
    }
411

412
    /**
413
     * Returns the sum of this number and the given one.
414
     *
415
     * @param BigNumber|int|string $that The number to add. Must be convertible to a BigInteger.
416
     *
417
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
418
     *
419
     * @pure
420
     */
421
    public function plus(BigNumber|int|string $that): BigInteger
422
    {
423
        $that = BigInteger::of($that);
1,611✔
424

425
        if ($that->isZero()) {
1,611✔
426
            return $this;
21✔
427
        }
428

429
        if ($this->isZero()) {
1,590✔
430
            return $that;
57✔
431
        }
432

433
        $value = CalculatorRegistry::get()->add($this->value, $that->value);
1,548✔
434

435
        return new BigInteger($value);
1,548✔
436
    }
437

438
    /**
439
     * Returns the difference of this number and the given one.
440
     *
441
     * @param BigNumber|int|string $that The number to subtract. Must be convertible to a BigInteger.
442
     *
443
     * @throws MathException If the number is not valid, or is not convertible to a BigInteger.
444
     *
445
     * @pure
446
     */
447
    public function minus(BigNumber|int|string $that): BigInteger
448
    {
449
        $that = BigInteger::of($that);
306✔
450

451
        if ($that->isZero()) {
306✔
452
            return $this;
27✔
453
        }
454

455
        if ($this->isZero()) {
279✔
456
            return $that->negated();
9✔
457
        }
458

459
        $value = CalculatorRegistry::get()->sub($this->value, $that->value);
270✔
460

461
        return new BigInteger($value);
270✔
462
    }
463

464
    /**
465
     * Returns the product of this number and the given one.
466
     *
467
     * @param BigNumber|int|string $that The multiplier. Must be convertible to a BigInteger.
468
     *
469
     * @throws MathException If the multiplier is not valid, or is not convertible to a BigInteger.
470
     *
471
     * @pure
472
     */
473
    public function multipliedBy(BigNumber|int|string $that): BigInteger
474
    {
475
        $that = BigInteger::of($that);
1,155✔
476

477
        if ($that->isOne()) {
1,155✔
478
            return $this;
360✔
479
        }
480

481
        if ($this->isOne()) {
1,140✔
482
            return $that;
261✔
483
        }
484

485
        $value = CalculatorRegistry::get()->mul($this->value, $that->value);
1,020✔
486

487
        return new BigInteger($value);
1,020✔
488
    }
489

490
    /**
491
     * Returns the result of the division of this number by the given one.
492
     *
493
     * @param BigNumber|int|string $that         The divisor. Must be convertible to a BigInteger.
494
     * @param RoundingMode         $roundingMode An optional rounding mode, defaults to Unnecessary.
495
     *
496
     * @throws MathException              If the divisor is not valid, or is not convertible to a BigInteger.
497
     * @throws DivisionByZeroException    If the divisor is zero.
498
     * @throws RoundingNecessaryException If RoundingMode::Unnecessary is used and the remainder is not zero.
499
     *
500
     * @pure
501
     */
502
    public function dividedBy(BigNumber|int|string $that, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigInteger
503
    {
504
        $that = BigInteger::of($that);
1,977✔
505

506
        if ($that->isZero()) {
1,968✔
507
            throw DivisionByZeroException::divisionByZero();
6✔
508
        }
509

510
        if ($that->isOne()) {
1,962✔
511
            return $this;
3✔
512
        }
513

514
        if ($that->isMinusOne()) {
1,959✔
515
            return $this->negated();
6✔
516
        }
517

518
        $result = CalculatorRegistry::get()->divRound($this->value, $that->value, $roundingMode);
1,953✔
519

520
        if ($result === null) {
1,953✔
521
            throw RoundingNecessaryException::integerDivisionNotExact();
117✔
522
        }
523

524
        return new BigInteger($result);
1,860✔
525
    }
526

527
    /**
528
     * Returns this number exponentiated to the given value.
529
     *
530
     * @param non-negative-int $exponent
531
     *
532
     * @throws InvalidArgumentException If the exponent is negative.
533
     *
534
     * @pure
535
     */
536
    public function power(int $exponent): BigInteger
537
    {
538
        if ($exponent === 0) {
1,875✔
539
            return BigInteger::one();
27✔
540
        }
541

542
        if ($exponent === 1) {
1,848✔
543
            return $this;
204✔
544
        }
545

546
        if ($exponent < 0) { // @phpstan-ignore smaller.alwaysFalse
1,644✔
547
            throw InvalidArgumentException::negativeExponent();
3✔
548
        }
549

550
        return new BigInteger(CalculatorRegistry::get()->pow($this->value, $exponent));
1,641✔
551
    }
552

553
    /**
554
     * Returns the quotient of the division of this number by the given one.
555
     *
556
     * Examples:
557
     *
558
     * - `7` quotient `3` returns `2`
559
     * - `7` quotient `-3` returns `-2`
560
     * - `-7` quotient `3` returns `-2`
561
     * - `-7` quotient `-3` returns `2`
562
     *
563
     * @param BigNumber|int|string $that The divisor. Must be convertible to a BigInteger.
564
     *
565
     * @throws MathException           If the divisor is not valid, or is not convertible to a BigInteger.
566
     * @throws DivisionByZeroException If the divisor is zero.
567
     *
568
     * @pure
569
     */
570
    public function quotient(BigNumber|int|string $that): BigInteger
571
    {
572
        $that = BigInteger::of($that);
2,943✔
573

574
        if ($that->isZero()) {
2,943✔
575
            throw DivisionByZeroException::divisionByZero();
3✔
576
        }
577

578
        if ($that->isOne()) {
2,940✔
579
            return $this;
1,524✔
580
        }
581

582
        if ($that->isMinusOne()) {
1,770✔
583
            return $this->negated();
9✔
584
        }
585

586
        $quotient = CalculatorRegistry::get()->divQ($this->value, $that->value);
1,761✔
587

588
        return new BigInteger($quotient);
1,761✔
589
    }
590

591
    /**
592
     * Returns the remainder of the division of this number by the given one.
593
     *
594
     * The remainder, when non-zero, has the same sign as the dividend.
595
     *
596
     * Examples:
597
     *
598
     * - `7` remainder `3` returns `1`
599
     * - `7` remainder `-3` returns `1`
600
     * - `-7` remainder `3` returns `-1`
601
     * - `-7` remainder `-3` returns `-1`
602
     *
603
     * @param BigNumber|int|string $that The divisor. Must be convertible to a BigInteger.
604
     *
605
     * @throws MathException           If the divisor is not valid, or is not convertible to a BigInteger.
606
     * @throws DivisionByZeroException If the divisor is zero.
607
     *
608
     * @pure
609
     */
610
    public function remainder(BigNumber|int|string $that): BigInteger
611
    {
612
        $that = BigInteger::of($that);
273✔
613

614
        if ($that->isZero()) {
273✔
615
            throw DivisionByZeroException::divisionByZero();
3✔
616
        }
617

618
        if ($that->isOne() || $that->isMinusOne()) {
270✔
619
            return BigInteger::zero();
33✔
620
        }
621

622
        $remainder = CalculatorRegistry::get()->divR($this->value, $that->value);
237✔
623

624
        return new BigInteger($remainder);
237✔
625
    }
626

627
    /**
628
     * Returns the quotient and remainder of the division of this number by the given one.
629
     *
630
     * Examples:
631
     *
632
     * - `7` quotientAndRemainder `3` returns [`2`, `1`]
633
     * - `7` quotientAndRemainder `-3` returns [`-2`, `1`]
634
     * - `-7` quotientAndRemainder `3` returns [`-2`, `-1`]
635
     * - `-7` quotientAndRemainder `-3` returns [`2`, `-1`]
636
     *
637
     * @param BigNumber|int|string $that The divisor. Must be convertible to a BigInteger.
638
     *
639
     * @return array{BigInteger, BigInteger} An array containing the quotient and the remainder.
640
     *
641
     * @throws MathException           If the divisor is not valid, or is not convertible to a BigInteger.
642
     * @throws DivisionByZeroException If the divisor is zero.
643
     *
644
     * @pure
645
     */
646
    public function quotientAndRemainder(BigNumber|int|string $that): array
647
    {
648
        $that = BigInteger::of($that);
147✔
649

650
        if ($that->isZero()) {
147✔
651
            throw DivisionByZeroException::divisionByZero();
3✔
652
        }
653

654
        [$quotient, $remainder] = CalculatorRegistry::get()->divQR($this->value, $that->value);
144✔
655

656
        return [
144✔
657
            new BigInteger($quotient),
144✔
658
            new BigInteger($remainder),
144✔
659
        ];
144✔
660
    }
661

662
    /**
663
     * Returns this number modulo the given one.
664
     *
665
     * The result is always non-negative, and is the unique value `r` such that `0 <= r < m`
666
     * and `this - r` is a multiple of `m`.
667
     *
668
     * This is also known as Euclidean modulo. Unlike `remainder()`, which can return negative values
669
     * when the dividend is negative, `mod()` always returns a non-negative result.
670
     *
671
     * Examples:
672
     *
673
     * - `7` mod `3` returns `1`
674
     * - `-7` mod `3` returns `2`
675
     *
676
     * @param BigNumber|int|string $modulus The modulus. Must be convertible to a BigInteger.
677
     *
678
     * @throws MathException            If the modulus is not valid, or is not convertible to a BigInteger.
679
     * @throws InvalidArgumentException If the modulus is negative.
680
     * @throws DivisionByZeroException  If the modulus is zero.
681
     *
682
     * @pure
683
     */
684
    public function mod(BigNumber|int|string $modulus): BigInteger
685
    {
686
        $modulus = BigInteger::of($modulus);
105✔
687

688
        if ($modulus->isZero()) {
105✔
689
            throw DivisionByZeroException::zeroModulus();
3✔
690
        }
691

692
        if ($modulus->isNegative()) {
102✔
693
            throw InvalidArgumentException::negativeModulus();
3✔
694
        }
695

696
        $value = CalculatorRegistry::get()->mod($this->value, $modulus->value);
99✔
697

698
        return new BigInteger($value);
99✔
699
    }
700

701
    /**
702
     * Returns the modular multiplicative inverse of this BigInteger modulo $modulus.
703
     *
704
     * @param BigNumber|int|string $modulus The modulus. Must be convertible to a BigInteger.
705
     *
706
     * @throws MathException            If the modulus is not valid, or is not convertible to a BigInteger.
707
     * @throws InvalidArgumentException If the modulus is negative.
708
     * @throws DivisionByZeroException  If the modulus is zero.
709
     * @throws NoInverseException       If this BigInteger has no multiplicative inverse mod m (that is, this BigInteger
710
     *                                  is not relatively prime to m).
711
     *
712
     * @pure
713
     */
714
    public function modInverse(BigNumber|int|string $modulus): BigInteger
715
    {
716
        $modulus = BigInteger::of($modulus);
75✔
717

718
        if ($modulus->isZero()) {
75✔
719
            throw DivisionByZeroException::zeroModulus();
6✔
720
        }
721

722
        if ($modulus->isNegative()) {
69✔
723
            throw InvalidArgumentException::negativeModulus();
3✔
724
        }
725

726
        if ($modulus->isOne()) {
66✔
727
            return BigInteger::zero();
3✔
728
        }
729

730
        $value = CalculatorRegistry::get()->modInverse($this->value, $modulus->value);
63✔
731

732
        if ($value === null) {
63✔
733
            throw NoInverseException::noModularInverse();
15✔
734
        }
735

736
        return new BigInteger($value);
48✔
737
    }
738

739
    /**
740
     * Returns this number raised into power with modulo.
741
     *
742
     * This operation requires a non-negative exponent and a strictly positive modulus.
743
     *
744
     * @param BigNumber|int|string $exponent The exponent. Must be convertible to a BigInteger.
745
     * @param BigNumber|int|string $modulus  The modulus. Must be convertible to a BigInteger.
746
     *
747
     * @throws MathException            If the exponent or modulus is not valid, or is not convertible to a BigInteger.
748
     * @throws InvalidArgumentException If the exponent or modulus is negative.
749
     * @throws DivisionByZeroException  If the modulus is zero.
750
     *
751
     * @pure
752
     */
753
    public function modPow(BigNumber|int|string $exponent, BigNumber|int|string $modulus): BigInteger
754
    {
755
        $exponent = BigInteger::of($exponent);
185✔
756
        $modulus = BigInteger::of($modulus);
185✔
757

758
        if ($modulus->isZero()) {
185✔
759
            throw DivisionByZeroException::zeroModulus();
3✔
760
        }
761

762
        if ($modulus->isNegative()) {
182✔
763
            throw InvalidArgumentException::negativeModulus();
3✔
764
        }
765

766
        if ($exponent->isNegative()) {
179✔
767
            throw InvalidArgumentException::negativeExponent();
3✔
768
        }
769

770
        $result = CalculatorRegistry::get()->modPow($this->value, $exponent->value, $modulus->value);
176✔
771

772
        return new BigInteger($result);
176✔
773
    }
774

775
    /**
776
     * Returns the greatest common divisor of this number and the given one.
777
     *
778
     * The GCD is always positive, unless both operands are zero, in which case it is zero.
779
     *
780
     * @param BigNumber|int|string $that The operand. Must be convertible to a BigInteger.
781
     *
782
     * @throws MathException If the operand is not valid, or is not convertible to a BigInteger.
783
     *
784
     * @pure
785
     */
786
    public function gcd(BigNumber|int|string $that): BigInteger
787
    {
788
        $that = BigInteger::of($that);
5,187✔
789

790
        if ($that->isZero()) {
5,187✔
791
            return $this->abs();
72✔
792
        }
793

794
        if ($this->isZero()) {
5,115✔
795
            return $that->abs();
105✔
796
        }
797

798
        $value = CalculatorRegistry::get()->gcd($this->value, $that->value);
5,010✔
799

800
        return new BigInteger($value);
5,010✔
801
    }
802

803
    /**
804
     * Returns the least common multiple of this number and the given one.
805
     *
806
     * The LCM is always positive, unless at least one operand is zero, in which case it is zero.
807
     *
808
     * @param BigNumber|int|string $that The operand. Must be convertible to a BigInteger.
809
     *
810
     * @throws MathException If the operand is not valid, or is not convertible to a BigInteger.
811
     *
812
     * @pure
813
     */
814
    public function lcm(BigNumber|int|string $that): BigInteger
815
    {
816
        $that = BigInteger::of($that);
561✔
817

818
        if ($this->isZero() || $that->isZero()) {
561✔
819
            return BigInteger::zero();
99✔
820
        }
821

822
        $value = CalculatorRegistry::get()->lcm($this->value, $that->value);
462✔
823

824
        return new BigInteger($value);
462✔
825
    }
826

827
    /**
828
     * Returns the integer square root of this number, rounded according to the given rounding mode.
829
     *
830
     * @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
831
     *
832
     * @throws NegativeNumberException    If this number is negative.
833
     * @throws RoundingNecessaryException If RoundingMode::Unnecessary is used, and the number is not a perfect square.
834
     *
835
     * @pure
836
     */
837
    public function sqrt(RoundingMode $roundingMode = RoundingMode::Unnecessary): BigInteger
838
    {
839
        if ($this->isNegative()) {
10,623✔
840
            throw NegativeNumberException::squareRootOfNegativeNumber();
3✔
841
        }
842

843
        $calculator = CalculatorRegistry::get();
10,620✔
844

845
        $sqrt = $calculator->sqrt($this->value);
10,620✔
846

847
        // For Down and Floor (equivalent for non-negative numbers), return floor sqrt
848
        if ($roundingMode === RoundingMode::Down || $roundingMode === RoundingMode::Floor) {
10,620✔
849
            return new BigInteger($sqrt);
2,130✔
850
        }
851

852
        // Check if the sqrt is exact
853
        $s2 = $calculator->mul($sqrt, $sqrt);
8,490✔
854
        $remainder = $calculator->sub($this->value, $s2);
8,490✔
855

856
        if ($remainder === '0') {
8,490✔
857
            // sqrt is exact
858
            return new BigInteger($sqrt);
5,385✔
859
        }
860

861
        // sqrt is not exact
862
        if ($roundingMode === RoundingMode::Unnecessary) {
3,105✔
863
            throw RoundingNecessaryException::integerSquareRootNotExact();
390✔
864
        }
865

866
        // For Up and Ceiling (equivalent for non-negative numbers), round up
867
        if ($roundingMode === RoundingMode::Up || $roundingMode === RoundingMode::Ceiling) {
2,715✔
868
            return new BigInteger($calculator->add($sqrt, '1'));
780✔
869
        }
870

871
        // For Half* modes, compare our number to the midpoint of the interval [s², (s+1)²[.
872
        // The midpoint is s² + s + 0.5. Comparing n >= s² + s + 0.5 with remainder = n − s²
873
        // is equivalent to comparing 2*remainder >= 2*s + 1.
874
        $twoRemainder = $calculator->mul($remainder, '2');
1,935✔
875
        $threshold = $calculator->add($calculator->mul($sqrt, '2'), '1');
1,935✔
876
        $cmp = $calculator->cmp($twoRemainder, $threshold);
1,935✔
877

878
        // We're supposed to increment (round up) when:
879
        //   - HalfUp, HalfCeiling => $cmp >= 0
880
        //   - HalfDown, HalfFloor => $cmp > 0
881
        //   - HalfEven => $cmp > 0 || ($cmp === 0 && $sqrt % 2 === 1)
882
        // But 2*remainder is always even and 2*s + 1 is always odd, so $cmp is never zero.
883
        // Therefore, all Half* modes simplify to:
884
        if ($cmp > 0) {
1,935✔
885
            $sqrt = $calculator->add($sqrt, '1');
990✔
886
        }
887

888
        return new BigInteger($sqrt);
1,935✔
889
    }
890

891
    #[Override]
892
    public function negated(): static
893
    {
894
        return new BigInteger(CalculatorRegistry::get()->neg($this->value));
3,852✔
895
    }
896

897
    /**
898
     * Returns the integer bitwise-and combined with another integer.
899
     *
900
     * This method returns a negative BigInteger if and only if both operands are negative.
901
     *
902
     * @param BigNumber|int|string $that The operand. Must be convertible to a BigInteger.
903
     *
904
     * @throws MathException If the operand is not valid, or is not convertible to a BigInteger.
905
     *
906
     * @pure
907
     */
908
    public function and(BigNumber|int|string $that): BigInteger
909
    {
910
        $that = BigInteger::of($that);
153✔
911

912
        return new BigInteger(CalculatorRegistry::get()->and($this->value, $that->value));
153✔
913
    }
914

915
    /**
916
     * Returns the integer bitwise-or combined with another integer.
917
     *
918
     * This method returns a negative BigInteger if and only if either of the operands is negative.
919
     *
920
     * @param BigNumber|int|string $that The operand. Must be convertible to a BigInteger.
921
     *
922
     * @throws MathException If the operand is not valid, or is not convertible to a BigInteger.
923
     *
924
     * @pure
925
     */
926
    public function or(BigNumber|int|string $that): BigInteger
927
    {
928
        $that = BigInteger::of($that);
138✔
929

930
        return new BigInteger(CalculatorRegistry::get()->or($this->value, $that->value));
138✔
931
    }
932

933
    /**
934
     * Returns the integer bitwise-xor combined with another integer.
935
     *
936
     * This method returns a negative BigInteger if and only if exactly one of the operands is negative.
937
     *
938
     * @param BigNumber|int|string $that The operand. Must be convertible to a BigInteger.
939
     *
940
     * @throws MathException If the operand is not valid, or is not convertible to a BigInteger.
941
     *
942
     * @pure
943
     */
944
    public function xor(BigNumber|int|string $that): BigInteger
945
    {
946
        $that = BigInteger::of($that);
144✔
947

948
        return new BigInteger(CalculatorRegistry::get()->xor($this->value, $that->value));
144✔
949
    }
950

951
    /**
952
     * Returns the bitwise-not of this BigInteger.
953
     *
954
     * @pure
955
     */
956
    public function not(): BigInteger
957
    {
958
        return $this->negated()->minus(1);
57✔
959
    }
960

961
    /**
962
     * Returns the integer left shifted by a given number of bits.
963
     *
964
     * @pure
965
     */
966
    public function shiftedLeft(int $bits): BigInteger
967
    {
968
        if ($bits === 0) {
594✔
969
            return $this;
6✔
970
        }
971

972
        if ($bits < 0) {
588✔
973
            return $this->shiftedRight(-$bits);
198✔
974
        }
975

976
        return $this->multipliedBy(BigInteger::of(2)->power($bits));
390✔
977
    }
978

979
    /**
980
     * Returns the integer right shifted by a given number of bits.
981
     *
982
     * @pure
983
     */
984
    public function shiftedRight(int $bits): BigInteger
985
    {
986
        if ($bits === 0) {
1,518✔
987
            return $this;
66✔
988
        }
989

990
        if ($bits < 0) {
1,452✔
991
            return $this->shiftedLeft(-$bits);
195✔
992
        }
993

994
        $operand = BigInteger::of(2)->power($bits);
1,257✔
995

996
        if ($this->isPositiveOrZero()) {
1,257✔
997
            return $this->quotient($operand);
672✔
998
        }
999

1000
        return $this->dividedBy($operand, RoundingMode::Up);
585✔
1001
    }
1002

1003
    /**
1004
     * Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.
1005
     *
1006
     * For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation.
1007
     * Computes (ceil(log2(this < 0 ? -this : this+1))).
1008
     *
1009
     * @return non-negative-int
1010
     *
1011
     * @pure
1012
     */
1013
    public function getBitLength(): int
1014
    {
1015
        if ($this->isZero()) {
330✔
1016
            return 0;
12✔
1017
        }
1018

1019
        if ($this->isNegative()) {
324✔
1020
            return $this->abs()->minus(1)->getBitLength();
120✔
1021
        }
1022

1023
        return strlen($this->toBase(2));
318✔
1024
    }
1025

1026
    /**
1027
     * Returns the index of the rightmost (lowest-order) one bit in this BigInteger.
1028
     *
1029
     * Returns -1 if this BigInteger contains no one bits.
1030
     *
1031
     * @return -1|non-negative-int
1032
     *
1033
     * @pure
1034
     */
1035
    public function getLowestSetBit(): int
1036
    {
1037
        $n = $this;
81✔
1038
        $bitLength = $this->getBitLength();
81✔
1039

1040
        for ($i = 0; $i <= $bitLength; $i++) {
81✔
1041
            if ($n->isOdd()) {
81✔
1042
                return $i;
78✔
1043
            }
1044

1045
            $n = $n->shiftedRight(1);
51✔
1046
        }
1047

1048
        return -1;
3✔
1049
    }
1050

1051
    /**
1052
     * Returns true if and only if the designated bit is set.
1053
     *
1054
     * Computes ((this & (1<<bitIndex)) != 0).
1055
     *
1056
     * @param non-negative-int $bitIndex The bit to test, 0-based.
1057
     *
1058
     * @throws InvalidArgumentException If the bit to test is negative.
1059
     *
1060
     * @pure
1061
     */
1062
    public function isBitSet(int $bitIndex): bool
1063
    {
1064
        if ($bitIndex < 0) { // @phpstan-ignore smaller.alwaysFalse
873✔
1065
            throw InvalidArgumentException::negativeBitIndex();
3✔
1066
        }
1067

1068
        return $this->shiftedRight($bitIndex)->isOdd();
870✔
1069
    }
1070

1071
    /**
1072
     * Returns whether this number is even.
1073
     *
1074
     * @pure
1075
     */
1076
    public function isEven(): bool
1077
    {
1078
        return in_array($this->value[-1], ['0', '2', '4', '6', '8'], true);
60✔
1079
    }
1080

1081
    /**
1082
     * Returns whether this number is odd.
1083
     *
1084
     * @pure
1085
     */
1086
    public function isOdd(): bool
1087
    {
1088
        return in_array($this->value[-1], ['1', '3', '5', '7', '9'], true);
1,011✔
1089
    }
1090

1091
    #[Override]
1092
    public function compareTo(BigNumber|int|string $that): int
1093
    {
1094
        $that = BigNumber::of($that);
3,059✔
1095

1096
        if ($that instanceof BigInteger) {
3,059✔
1097
            return CalculatorRegistry::get()->cmp($this->value, $that->value);
3,005✔
1098
        }
1099

1100
        return -$that->compareTo($this);
120✔
1101
    }
1102

1103
    #[Override]
1104
    public function getSign(): int
1105
    {
1106
        return ($this->value === '0') ? 0 : (($this->value[0] === '-') ? -1 : 1);
22,187✔
1107
    }
1108

1109
    #[Override]
1110
    public function toBigInteger(): BigInteger
1111
    {
1112
        return $this;
24,734✔
1113
    }
1114

1115
    #[Override]
1116
    public function toBigDecimal(): BigDecimal
1117
    {
1118
        return self::newBigDecimal($this->value);
4,950✔
1119
    }
1120

1121
    #[Override]
1122
    public function toBigRational(): BigRational
1123
    {
1124
        return self::newBigRational($this, BigInteger::one(), false, false);
528✔
1125
    }
1126

1127
    #[Override]
1128
    public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigDecimal
1129
    {
1130
        return $this->toBigDecimal()->toScale($scale, $roundingMode);
12✔
1131
    }
1132

1133
    #[Override]
1134
    public function toInt(): int
1135
    {
1136
        $intValue = filter_var($this->value, FILTER_VALIDATE_INT);
90✔
1137

1138
        if ($intValue === false) {
90✔
1139
            throw IntegerOverflowException::integerOutOfRange($this);
18✔
1140
        }
1141

1142
        return $intValue;
72✔
1143
    }
1144

1145
    #[Override]
1146
    public function toFloat(): float
1147
    {
1148
        return (float) $this->value;
183✔
1149
    }
1150

1151
    /**
1152
     * Returns a string representation of this number in the given base.
1153
     *
1154
     * The output will always be lowercase for bases greater than 10.
1155
     *
1156
     * @param int<2, 36> $base
1157
     *
1158
     * @throws InvalidArgumentException If the base is out of range.
1159
     *
1160
     * @pure
1161
     */
1162
    public function toBase(int $base): string
1163
    {
1164
        if ($base === 10) {
1,302✔
1165
            return $this->value;
12✔
1166
        }
1167

1168
        if ($base < 2 || $base > 36) { // @phpstan-ignore smaller.alwaysFalse, greater.alwaysFalse, booleanOr.alwaysFalse
1,290✔
1169
            throw InvalidArgumentException::baseOutOfRange($base);
15✔
1170
        }
1171

1172
        return CalculatorRegistry::get()->toBase($this->value, $base);
1,275✔
1173
    }
1174

1175
    /**
1176
     * Returns a string representation of this number in an arbitrary base with a custom alphabet.
1177
     *
1178
     * This method is byte-oriented: the alphabet is interpreted as a sequence of single-byte characters.
1179
     * Multibyte UTF-8 characters are not supported.
1180
     *
1181
     * Because this method accepts any single-byte character, including dash, it does not handle negative numbers;
1182
     * a NegativeNumberException will be thrown when attempting to call this method on a negative number.
1183
     *
1184
     * @param non-empty-string $alphabet The alphabet, for example '01' for base 2, or '01234567' for base 8.
1185
     *
1186
     * @throws InvalidArgumentException If the alphabet does not contain at least 2 chars, or contains duplicates.
1187
     * @throws NegativeNumberException  If this number is negative.
1188
     *
1189
     * @pure
1190
     */
1191
    public function toArbitraryBase(string $alphabet): string
1192
    {
1193
        $base = strlen($alphabet);
144✔
1194

1195
        if ($base < 2) {
144✔
1196
            throw InvalidArgumentException::alphabetTooShort();
6✔
1197
        }
1198

1199
        if (strlen(count_chars($alphabet, 3)) !== $base) {
138✔
1200
            throw InvalidArgumentException::duplicateCharsInAlphabet();
9✔
1201
        }
1202

1203
        if ($this->isNegative()) {
129✔
1204
            throw NegativeNumberException::toArbitraryBaseOfNegativeNumber();
3✔
1205
        }
1206

1207
        return CalculatorRegistry::get()->toArbitraryBase($this->value, $alphabet, $base);
126✔
1208
    }
1209

1210
    /**
1211
     * Returns a string of bytes containing the binary representation of this BigInteger.
1212
     *
1213
     * The string is in big-endian byte-order: the most significant byte is in the zeroth element.
1214
     *
1215
     * If `$signed` is true, the output will be in two's-complement representation, and a sign bit will be prepended to
1216
     * the output. If `$signed` is false, no sign bit will be prepended, and this method will throw an exception if the
1217
     * number is negative.
1218
     *
1219
     * The string will contain the minimum number of bytes required to represent this BigInteger, including a sign bit
1220
     * if `$signed` is true.
1221
     *
1222
     * This representation is compatible with the `fromBytes()` factory method, as long as the `$signed` flags match.
1223
     *
1224
     * @param bool $signed Whether to output a signed number in two's-complement representation with a leading sign bit.
1225
     *
1226
     * @throws NegativeNumberException If $signed is false, and the number is negative.
1227
     *
1228
     * @pure
1229
     */
1230
    public function toBytes(bool $signed = true): string
1231
    {
1232
        if (! $signed && $this->isNegative()) {
534✔
1233
            throw NegativeNumberException::unsignedBytesOfNegativeNumber();
3✔
1234
        }
1235

1236
        $hex = $this->abs()->toBase(16);
531✔
1237

1238
        if (strlen($hex) % 2 !== 0) {
531✔
1239
            $hex = '0' . $hex;
219✔
1240
        }
1241

1242
        $baseHexLength = strlen($hex);
531✔
1243

1244
        if ($signed) {
531✔
1245
            if ($this->isNegative()) {
492✔
1246
                $bin = hex2bin($hex);
453✔
1247
                assert($bin !== false);
1248

1249
                /** @var non-empty-string $hex */
1250
                $hex = bin2hex(~$bin);
453✔
1251
                $hex = self::fromBase($hex, 16)->plus(1)->toBase(16);
453✔
1252

1253
                $hexLength = strlen($hex);
453✔
1254

1255
                if ($hexLength < $baseHexLength) {
453✔
1256
                    $hex = str_repeat('0', $baseHexLength - $hexLength) . $hex;
72✔
1257
                }
1258

1259
                if ($hex[0] < '8') {
453✔
1260
                    $hex = 'FF' . $hex;
114✔
1261
                }
1262
            } else {
1263
                if ($hex[0] >= '8') {
39✔
1264
                    $hex = '00' . $hex;
21✔
1265
                }
1266
            }
1267
        }
1268

1269
        $result = hex2bin($hex);
531✔
1270
        assert($result !== false);
1271

1272
        return $result;
531✔
1273
    }
1274

1275
    /**
1276
     * @return numeric-string
1277
     */
1278
    #[Override]
1279
    public function toString(): string
1280
    {
1281
        /** @var numeric-string */
1282
        return $this->value;
30,981✔
1283
    }
1284

1285
    /**
1286
     * This method is required for serializing the object and SHOULD NOT be accessed directly.
1287
     *
1288
     * @internal
1289
     *
1290
     * @return array{value: string}
1291
     */
1292
    public function __serialize(): array
1293
    {
1294
        return ['value' => $this->value];
6✔
1295
    }
1296

1297
    /**
1298
     * This method is only here to allow unserializing the object and cannot be accessed directly.
1299
     *
1300
     * @internal
1301
     *
1302
     * @param array{value: string} $data
1303
     *
1304
     * @throws LogicException
1305
     */
1306
    public function __unserialize(array $data): void
1307
    {
1308
        /** @phpstan-ignore isset.initializedProperty */
1309
        if (isset($this->value)) {
9✔
1310
            throw new LogicException('__unserialize() is an internal function, it must not be called directly.');
3✔
1311
        }
1312

1313
        /** @phpstan-ignore deadCode.unreachable */
1314
        $this->value = $data['value'];
6✔
1315
    }
1316

1317
    #[Override]
1318
    protected static function from(BigNumber $number): static
1319
    {
1320
        return $number->toBigInteger();
24,872✔
1321
    }
1322

1323
    /**
1324
     * Returns random bytes from the provided generator or from random_bytes().
1325
     *
1326
     * @param int                          $byteLength           The number of requested bytes.
1327
     * @param (callable(int): string)|null $randomBytesGenerator The random bytes generator, or null to use random_bytes().
1328
     *
1329
     * @throws RandomSourceException If random byte generation fails.
1330
     */
1331
    private static function randomBytes(int $byteLength, ?callable $randomBytesGenerator): string
1332
    {
1333
        if ($randomBytesGenerator === null) {
177✔
1334
            $randomBytesGenerator = random_bytes(...);
×
1335
        }
1336

1337
        try {
1338
            $randomBytes = $randomBytesGenerator($byteLength);
177✔
1339
        } catch (Throwable $e) {
6✔
1340
            throw RandomSourceException::randomSourceFailure($e);
6✔
1341
        }
1342

1343
        /** @phpstan-ignore function.alreadyNarrowedType (Defensive runtime check for user-provided callbacks) */
1344
        if (! is_string($randomBytes)) {
171✔
1345
            throw RandomSourceException::invalidRandomBytesType($randomBytes);
6✔
1346
        }
1347

1348
        if (strlen($randomBytes) !== $byteLength) {
165✔
1349
            throw RandomSourceException::invalidRandomBytesLength($byteLength, strlen($randomBytes));
6✔
1350
        }
1351

1352
        return $randomBytes;
159✔
1353
    }
1354

1355
    /**
1356
     * @pure
1357
     */
1358
    private function isOne(): bool
1359
    {
1360
        return $this->value === '1';
5,556✔
1361
    }
1362

1363
    /**
1364
     * @pure
1365
     */
1366
    private function isMinusOne(): bool
1367
    {
1368
        return $this->value === '-1';
3,861✔
1369
    }
1370
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc