From 436209e6400764817be4126b35712ba07abe3870 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 3 Jan 2016 11:20:53 +1100 Subject: bignum: Use openssl function names --- src/openssl.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 499bcce..991cba5 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -2000,18 +2000,18 @@ sslerr: static const luaL_Reg bn_methods[] = { - { "add", &bn__add }, - { "sub", &bn__sub }, - { "mul", &bn__mul }, - { "idiv", &bn__idiv }, - { "mod", &bn__mod }, - { "pow", &bn__pow }, - { "shl", &bn__shl }, - { "shr", &bn__shr }, - { "tobin", &bn_tobin }, - { "todec", &bn_todec }, - { "tohex", &bn_tohex }, - { NULL, NULL }, + { "add", &bn__add }, + { "sub", &bn__sub }, + { "mul", &bn__mul }, + { "idiv", &bn__idiv }, + { "mod", &bn__mod }, + { "exp", &bn__pow }, + { "lshift", &bn__shl }, + { "rshift", &bn__shr }, + { "tobin", &bn_tobin }, + { "todec", &bn_todec }, + { "tohex", &bn_tohex }, + { NULL, NULL }, }; static const luaL_Reg bn_metatable[] = { -- cgit v1.2.3-59-g8ed1b From b2b301db7ad41238fdd1b6236eb917c11bf0bbbc Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 3 Jan 2016 11:22:14 +1100 Subject: bignum: Bind sqr, nnmod and gcd --- src/openssl.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index 991cba5..e146098 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1814,6 +1814,18 @@ static int bn__mul(lua_State *L) { } /* bn__mul() */ +static int bn_sqr(lua_State *L) { + BIGNUM *r, *a; + + bn_prepops(L, &r, &a, NULL, 1); + + if (!BN_sqr(r, a, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:sqr"); + + return 1; +} /* bn_sqr() */ + + static int bn__idiv(lua_State *L) { BIGNUM *dv, *a, *b; @@ -1844,6 +1856,18 @@ static int bn__mod(lua_State *L) { } /* bn__mod() */ +static int bn_nnmod(lua_State *L) { + BIGNUM *r, *a, *b; + + bn_prepops(L, &r, &a, &b, 0); + + if (!BN_nnmod(r, a, b, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:nnmod"); + + return 1; +} /* bn_nnmod() */ + + static int bn__pow(lua_State *L) { BIGNUM *r, *a, *b; @@ -1856,6 +1880,18 @@ static int bn__pow(lua_State *L) { } /* bn__pow() */ +static int bn_gcd(lua_State *L) { + BIGNUM *r, *a, *b; + + bn_prepops(L, &r, &a, &b, 1); + + if (!BN_gcd(r, a, b, getctx(L))) + return auxL_error(L, auxL_EOPENSSL, "bignum:gcd"); + + return 1; +} /* bn_gcd() */ + + static int bn__shl(lua_State *L) { BIGNUM *r, *a; int n; @@ -2003,9 +2039,12 @@ static const luaL_Reg bn_methods[] = { { "add", &bn__add }, { "sub", &bn__sub }, { "mul", &bn__mul }, + { "sqr", &bn_sqr }, { "idiv", &bn__idiv }, { "mod", &bn__mod }, + { "nnmod", &bn_nnmod }, { "exp", &bn__pow }, + { "gcd", &bn_gcd }, { "lshift", &bn__shl }, { "rshift", &bn__shr }, { "tobin", &bn_tobin }, -- cgit v1.2.3-59-g8ed1b From 7a31bf07493d365f5b44581dc4aaf3c9d48179f0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 3 Jan 2016 11:35:43 +1100 Subject: bignum: bugfix: unm shouldn't modify it's operands --- src/openssl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index e146098..a73917a 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1924,8 +1924,9 @@ static int bn__shr(lua_State *L) { static int bn__unm(lua_State *L) { BIGNUM *a = checksimple(L, 1, BIGNUM_CLASS); + BIGNUM *r = bn_dup(L, a); - BN_set_negative(a, !BN_is_negative(a)); + BN_set_negative(r, !BN_is_negative(a)); return 1; } /* bn__unm() */ -- cgit v1.2.3-59-g8ed1b From 0706e6447e3b6e7b8283686b6f89b46c68f8e1fd Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 3 Jan 2016 11:53:32 +1100 Subject: bignum: Add :isPrime --- src/openssl.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index a73917a..c311ba6 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1976,6 +1976,20 @@ static int bn__gc(lua_State *L) { } /* bn__gc() */ +static int bn_isPrime(lua_State *L) { + BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); + int nchecks = luaL_optinteger(L, 2, BN_prime_checks); + int res = BN_is_prime_ex(bn, nchecks, getctx(L), NULL); + + if (res == -1) + return auxL_error(L, auxL_EOPENSSL, "bignum:isPrime"); + + lua_pushboolean(L, res); + + return 1; +} /* bn_isPrime() */ + + static BIO *getbio(lua_State *); static int bn_todec(lua_State *L) { @@ -2037,21 +2051,22 @@ sslerr: static const luaL_Reg bn_methods[] = { - { "add", &bn__add }, - { "sub", &bn__sub }, - { "mul", &bn__mul }, - { "sqr", &bn_sqr }, - { "idiv", &bn__idiv }, - { "mod", &bn__mod }, - { "nnmod", &bn_nnmod }, - { "exp", &bn__pow }, - { "gcd", &bn_gcd }, - { "lshift", &bn__shl }, - { "rshift", &bn__shr }, - { "tobin", &bn_tobin }, - { "todec", &bn_todec }, - { "tohex", &bn_tohex }, - { NULL, NULL }, + { "add", &bn__add }, + { "sub", &bn__sub }, + { "mul", &bn__mul }, + { "sqr", &bn_sqr }, + { "idiv", &bn__idiv }, + { "mod", &bn__mod }, + { "nnmod", &bn_nnmod }, + { "exp", &bn__pow }, + { "gcd", &bn_gcd }, + { "lshift", &bn__shl }, + { "rshift", &bn__shr }, + { "isPrime", &bn_isPrime }, + { "tobin", &bn_tobin }, + { "todec", &bn_todec }, + { "tohex", &bn_tohex }, + { NULL, NULL }, }; static const luaL_Reg bn_metatable[] = { -- cgit v1.2.3-59-g8ed1b From f3c45dfe948872e18c2685bea57b28f401c49809 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 3 Jan 2016 12:26:31 +1100 Subject: bignum: Add generatePrime as new constructor --- src/openssl.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index c311ba6..1ea5d16 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -223,6 +223,14 @@ static const char *xitoa(char *dst, size_t lim, long i) { } /* xitoa() */ +static _Bool optbool(lua_State *L, int idx, _Bool d) { + if (lua_isnoneornil(L, idx)) + return d; + luaL_checktype(L, idx, LUA_TBOOLEAN); + return lua_toboolean(L, idx); +} /* optbool() */ + + static void *prepudata(lua_State *L, size_t size, const char *tname, int (*gc)(lua_State *)) { void *p = memset(lua_newuserdata(L, size), 0, size); @@ -1976,6 +1984,20 @@ static int bn__gc(lua_State *L) { } /* bn__gc() */ +static int bn_generatePrime(lua_State *L) { + int bits = luaL_checkinteger(L, 1); + _Bool safe = optbool(L, 2, 0); + const BIGNUM *add = lua_isnoneornil(L, 3) ? NULL : checkbig(L, 3); + const BIGNUM *rem = lua_isnoneornil(L, 4) ? NULL : checkbig(L, 4); + BIGNUM *bn = bn_push(L); + + if (!BN_generate_prime_ex(bn, bits, safe, add, rem, NULL)) + return auxL_error(L, auxL_EOPENSSL, "bignum.generatePrime"); + + return 1; +} /* bn_generatePrime() */ + + static int bn_isPrime(lua_State *L) { BIGNUM *bn = checksimple(L, 1, BIGNUM_CLASS); int nchecks = luaL_optinteger(L, 2, BN_prime_checks); @@ -2090,9 +2112,10 @@ static const luaL_Reg bn_metatable[] = { static const luaL_Reg bn_globals[] = { - { "new", &bn_new }, - { "interpose", &bn_interpose }, - { NULL, NULL }, + { "new", &bn_new }, + { "interpose", &bn_interpose }, + { "generatePrime", &bn_generatePrime }, + { NULL, NULL }, }; int luaopen__openssl_bignum(lua_State *L) { -- cgit v1.2.3-59-g8ed1b