Small BigInteger feature request


#1

I’m using a BigInteger as a set of flags in my audio thread. When using more than 128 bits the assignment operator always does a free then malloc even if the two BigInteger objects are the same size. Is there anything wrong with this suggested tweak:

BigInteger& BigInteger::operator= (const BigInteger& other)
{
    if (this != &other)
    {
        highestBit = other.getHighestBit();
        const size_t newAllocatedSize = (size_t) jmax ((size_t) numPreallocatedInts, sizeNeededToHold (highestBit));

        if (newAllocatedSize <= numPreallocatedInts)
            heapAllocation.free();
        else if (allocatedSize != newAllocatedSize)
            heapAllocation.malloc (newAllocatedSize);

        allocatedSize = newAllocatedSize;
        
        memcpy (getValues(), other.getValues(), sizeof (uint32) * allocatedSize);
        negative = other.negative;
    }

    return *this;
}

#2

Yeah that looks pretty useful. Just to be sure though, you mean:

    if (newAllocatedSize <= numPreallocatedInts)
        heapAllocation.free();
    else if (allocatedSize != newAllocatedSize)
        heapAllocation.malloc (newAllocatedSize);

instead of what you wrote:

    if (allocatedSize <= numPreallocatedInts)
        heapAllocation.free();
    else if (allocatedSize != newAllocatedSize)
        heapAllocation.malloc (newAllocatedSize);

Right? Or am i missing something?


#3

Of course, thanks for the catch. I’ve edited the original post.


#4

OK I’ve added this to the develop branch.


#5

Has this change been unit-tested against possible regressions in RSAKey?


#6

One downside is that it may be faster, which is obviously a negative thing for this use case :slight_smile:


#7

Yes, I ran our RSA Key unit tests. I have a project where I have more involved unit tests for RSAKey and the unit tests passed.


#8

I get once in a while problems with the RSA classes now, are you 100% sure that there is no difference regarding this change?

EDIT:

Or maybe also because of the previous change?


#9

A few weeks ago a change in the BigInteger class has broken the RSA algorithms, but the issue has been solved now


#10

Yes i know, thats why i am so skeptical, was it really solved, 100% ?

EDIT: I’m 95% sure this was an false-alarm, but i will keep an eye on that :wink: