LargeInteger constructor using (sign, value) might lead to incorrect values
thofer-os opened this issue · 2 comments
Hi,
While running some tests on this library, I've stumbled across an issue in the implementation of the multiple-precision addition.
Adding 1 to 0x1fffffff erroneously results in 0x30000000, as can be verified here: https://jsfiddle.net/j8qoL5g4/35/
This stems from an error with the bitwise shift right operation in https://github.com/verificatum/verificatum-vjsc/blob/master/src/js/verificatum/arithm/li.js#L620
Indeed, since the max value for a word is having all M4_WORDSIZE bits set, the modulus should be 1 << (M4_WORDSIZE+1).
Therefore, the carry value is doubled.
Hi Douglas,
Thank you for the discussion via email!
I'll attempt to document it here for anyone who might have a similar issue.
To summarize, there is no issue in the implementation of the multi-precision addition (HAC 14.7), contrary to what I incorrectly deduced from the sample code above.
Using the hexstring constructor, the results match the expectations.
There is an issue in the (sign: number, value: number[])
constructo however, which accepts numbers >= 2**28
, while the rest of the code assumes each number in the value array is < 2**28
leading to incorrect behaviour.
This can be seen in https://jsfiddle.net/szn4hd7j/73/
Therefore, I'll update the title of this issue and as discussed, I'll try and offer a patch to make the constructor more misuse-resistant.
Thanks again!