Skip to content

BCMath BigInteger engine is affected by global scale changes #2089

@d1823

Description

@d1823

Hey.

It seems that the BCMath BigInteger engine gets affected by changes to scale, either through the bcscale function or global bcmath.scale. I'm not sure whether it's known, a feature, or a bug, so reporting just in case.

I haven't verified the impact of this thing, so can't say what chunk of supported operations is affected, but here's a small reproducible example based on the values that tripped our production system:

<?php

namespace Tests;

use phpseclib3\Math\BigInteger;
use PHPUnit\Framework\TestCase;

class BcScaleTest extends TestCase
{
    /**
     * @testWith [true, 0]
     *           [false, 1]
     *           [false, 2]
     *           [false, 3]
     *           [false, 12]
     *           [false, 99]
     */
    public function test(bool $expectedResult, int $scale): void
    {
        bcscale($scale);

        $this->assertEquals(
            $expectedResult,
            new BigInteger('115792089210356248762697446949407573530086143415290314195533631308867097853951', 10)->isPrime()
        );
    }
}

A quick look at the implementation of the BCMath engine shows that a bunch of calls to bc functions are missing the explicit 0 scale, but then a quick change setting it everywhere makes the test suite fail in a couple of places, so it seems the fix might be a bit more involved.

Obviously, a workaround for anybody affected is to install the GMP extension.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions