From 65248290ed3a412e9ce0caa7204b1a42fc9fe192 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 22:37:58 +1100 Subject: bignum: exposing existing math operators as methods --- src/openssl.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index afd1f61..304ff9a 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1941,6 +1941,12 @@ sslerr: static const luaL_Reg bn_methods[] = { + { "add", &bn__add }, + { "sub", &bn__sub }, + { "mul", &bn__mul }, + { "div", &bn__div }, + { "mod", &bn__mod }, + { "pow", &bn__pow }, { "tobin", &bn_tobin }, { NULL, NULL }, }; -- cgit v1.2.3-59-g8ed1b From 67dca5b835148d971127a4c882a8de456cffd868 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 22:59:23 +1100 Subject: bignum: Add shl (lshift), shr (rshift) functions --- src/openssl.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index 304ff9a..a1a7c58 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1856,6 +1856,36 @@ static int bn__pow(lua_State *L) { } /* bn__pow() */ +static int bn__shl(lua_State *L) { + BIGNUM *r, *a; + int n; + + a = checkbig(L, 1); + n = luaL_checkinteger(L, 2); + r = bn_push(L); + + if (!BN_lshift(r, a, n)) + return auxL_error(L, auxL_EOPENSSL, "bignum:__shl"); + + return 1; +} /* bn__shl() */ + + +static int bn__shr(lua_State *L) { + BIGNUM *r, *a; + int n; + + a = checkbig(L, 1); + n = luaL_checkinteger(L, 2); + r = bn_push(L); + + if (!BN_rshift(r, a, n)) + return auxL_error(L, auxL_EOPENSSL, "bignum:__shr"); + + return 1; +} /* bn__shr() */ + + static int bn__unm(lua_State *L) { BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS); @@ -1947,6 +1977,8 @@ static const luaL_Reg bn_methods[] = { { "div", &bn__div }, { "mod", &bn__mod }, { "pow", &bn__pow }, + { "shl", &bn__shl }, + { "shr", &bn__shr }, { "tobin", &bn_tobin }, { NULL, NULL }, }; @@ -1959,6 +1991,8 @@ static const luaL_Reg bn_metatable[] = { { "__mod", &bn__mod }, { "__pow", &bn__pow }, { "__unm", &bn__unm }, + { "__shl", &bn__shl }, + { "__shr", &bn__shr }, { "__eq", &bn__eq }, { "__lt", &bn__lt }, { "__le", &bn__le }, -- cgit v1.2.3-59-g8ed1b From e1ccaebacd4e8f277222d2177e7c02fb31ea0bf2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 23:06:11 +1100 Subject: bignum: Rename tostring function to todec. Expose as ":todec" method --- src/openssl.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index a1a7c58..cb74506 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1941,7 +1941,7 @@ static int bn__gc(lua_State *L) { static BIO *getbio(lua_State *); -static int bn__tostring(lua_State *L) { +static int bn_todec(lua_State *L) { BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); char *txt = NULL; BIO *bio; @@ -1966,8 +1966,8 @@ static int bn__tostring(lua_State *L) { sslerr: OPENSSL_free(txt); - return auxL_error(L, auxL_EOPENSSL, "bignum:__tostring"); -} /* bn__tostring() */ + return auxL_error(L, auxL_EOPENSSL, "bignum:todec"); +} /* bn_todec() */ static const luaL_Reg bn_methods[] = { @@ -1980,6 +1980,7 @@ static const luaL_Reg bn_methods[] = { { "shl", &bn__shl }, { "shr", &bn__shr }, { "tobin", &bn_tobin }, + { "todec", &bn_todec }, { NULL, NULL }, }; @@ -1997,7 +1998,7 @@ static const luaL_Reg bn_metatable[] = { { "__lt", &bn__lt }, { "__le", &bn__le }, { "__gc", &bn__gc }, - { "__tostring", &bn__tostring }, + { "__tostring", &bn_todec }, { NULL, NULL }, }; -- cgit v1.2.3-59-g8ed1b From dce3be261d53b6148e9f599e1a18aeae63e26ff4 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 23:08:07 +1100 Subject: bignum: Add tohex function --- src/openssl.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index cb74506..d6bec90 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1970,6 +1970,35 @@ sslerr: } /* bn_todec() */ +static int bn_tohex(lua_State *L) { + BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); + char *txt = NULL; + BIO *bio; + BUF_MEM *buf; + + if (!(txt = BN_bn2hex(bn))) + goto sslerr; + + /* use GC-visible BIO as temporary buffer */ + bio = getbio(L); + + if (BIO_puts(bio, txt) < 0) + goto sslerr; + + OPENSSL_free(txt); + txt = NULL; + + BIO_get_mem_ptr(bio, &buf); + lua_pushlstring(L, buf->data, buf->length); + + return 1; +sslerr: + OPENSSL_free(txt); + + return auxL_error(L, auxL_EOPENSSL, "bignum:tohex"); +} /* bn_tohex() */ + + static const luaL_Reg bn_methods[] = { { "add", &bn__add }, { "sub", &bn__sub }, @@ -1981,6 +2010,7 @@ static const luaL_Reg bn_methods[] = { { "shr", &bn__shr }, { "tobin", &bn_tobin }, { "todec", &bn_todec }, + { "tohex", &bn_tohex }, { NULL, NULL }, }; -- cgit v1.2.3-59-g8ed1b From 2fa327bd7c95f797100bfe45fba956b290c309aa Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 18 Dec 2015 22:46:15 +1100 Subject: bignum: Move div operator to idiv operator (breaks API) --- src/openssl.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/openssl.c b/src/openssl.c index d6bec90..f03c129 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1814,16 +1814,16 @@ static int bn__mul(lua_State *L) { } /* bn__mul() */ -static int bn__div(lua_State *L) { - BIGNUM *r, *a, *b; +static int bn__idiv(lua_State *L) { + BIGNUM *dv, *a, *b; - bn_prepops(L, &r, &a, &b, 0); + bn_prepops(L, &dv, &a, &b, 0); - if (!BN_div(r, NULL, a, b, getctx(L))) - return auxL_error(L, auxL_EOPENSSL, "bignum:__div"); + if (!BN_div(dv, NULL, a, b, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:__idiv"); return 1; -} /* bn__div() */ +} /* bn__idiv() */ static int bn__mod(lua_State *L) { @@ -2003,7 +2003,7 @@ static const luaL_Reg bn_methods[] = { { "add", &bn__add }, { "sub", &bn__sub }, { "mul", &bn__mul }, - { "div", &bn__div }, + { "idiv", &bn__idiv }, { "mod", &bn__mod }, { "pow", &bn__pow }, { "shl", &bn__shl }, @@ -2018,7 +2018,7 @@ static const luaL_Reg bn_metatable[] = { { "__add", &bn__add }, { "__sub", &bn__sub }, { "__mul", &bn__mul }, - { "__div", &bn__div }, + { "__idiv", &bn__idiv }, { "__mod", &bn__mod }, { "__pow", &bn__pow }, { "__unm", &bn__unm }, -- cgit v1.2.3-59-g8ed1b