From fb08628f17893280da43c70f0919687feca09265 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 15 Nov 2015 20:44:09 +1100 Subject: Implement lua rounding style for __mod operator --- src/openssl.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index 9dbcda7..69115ce 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1809,6 +1809,12 @@ static int bn__mod(lua_State *L) { if (!BN_mod(r, a, b, getctx(L))) return auxL_error(L, auxL_EOPENSSL, "bignum:__mod"); + /* lua has different rounding behaviour for mod than C */ + if (!BN_is_zero(r) && (BN_is_negative(a) ^ BN_is_negative(b))) { + if (!BN_add(r, r, b)) + return auxL_error(L, auxL_EOPENSSL, "bignum:__mod"); + } + return 1; } /* bn__mod() */ -- cgit v1.2.3-59-g8ed1b