dyne / Zenroom

Embedded no-code VM executing human-like language to manipulate data and process cryptographic operations.
https://dev.zenroom.org
GNU Affero General Public License v3.0
186 stars 61 forks source link

O.from_number() does not work for "big" numbers #857

Closed FilippoTrotter closed 1 month ago

FilippoTrotter commented 2 months ago

The O.from_number() method from the file src/zen_octet.c, used to convert an integer into octets, seems to not work correctly with sufficiently large numbers. Specifically, I've noticed the issue arises with numbers larger than 2^25.The problem appears with odd numbers. For example, the following test is passed by the function, which clearly should not happen.

assert(O.from_number(2^25-1)==O.from_number(2^25))
assert(O.from_number(2^25-3) == O.from_number(2^25-4))
matteo-cristino commented 2 months ago

This seems due to the combination of lua_Number as type in output of lua_tointegerx and the floorf function (Could not find any documentation of it online 🤷). Not totally sure why, but replacing lua_Number to lua_Integer and removing the floorf function make it work as intended.

matteo-cristino commented 2 months ago

first draft fix here fix/oct_from_number