Num icon indicating copy to clipboard operation
Num copied to clipboard

Division fails when number is not truncated

Open AlexisWilke opened this issue 1 year ago • 1 comments

I have a 512 bit number which may be very large or very small (like just 1), but still occupies 8 × uint64_t values.

Want to compute a division like so:

            Num na(a.f_value, a.f_value + 8);
            Num nb(b.f_value, b.f_value + 8);
            Num nd(na / nb);

and I always get an incorrect number.

This is because the bit length is computed assuming no leading zeroes.

size_t bitlength() const {
    if (size() == 0) return 0;
    size_t last = size() - 1;       // <- this assumes there is at least one bit set in the word or the next word MSB is set
    size_t result = word_bitlength((*this)[last]) + last*word_bits();
    return result;
}

I would suggest that you check that assumption like so:

    size_t last = size() - 1;
    while(last > 0 && (*this)[last] == 0) {
        --last;
    }

I tested that small addition in my code and it works as expected. Since the user can create a number with leading zeroes, I think that's a good idea to have such. You could also check in the constructor and prevent such numbers altogether (i.e. auto-truncate on creation of the number).

AlexisWilke avatar Nov 26 '22 18:11 AlexisWilke