The loops relied on unsigned integer overflow, which is not immediately obvious. Replace them with less clever variants that are clearer. Also implement bn_compare using std::memcmp.