summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/LuaBigInt.cc285
1 files changed, 285 insertions, 0 deletions
diff --git a/src/LuaBigInt.cc b/src/LuaBigInt.cc
index 18ddeea..53b9f5e 100644
--- a/src/LuaBigInt.cc
+++ b/src/LuaBigInt.cc
@@ -8,6 +8,7 @@ enum BigInt_functions_t {
enum BigInt_methods_t {
BIGINT_SET,
+ BIGINT_SET2EXPT,
BIGINT_TOSTRING,
BIGINT_ADD,
@@ -15,6 +16,43 @@ enum BigInt_methods_t {
BIGINT_MUL,
BIGINT_DIV,
BIGINT_MOD,
+ BIGINT_SHL,
+ BIGINT_SHR,
+
+ BIGINT_DO_ADD,
+ BIGINT_DO_SUB,
+ BIGINT_DO_MUL,
+ BIGINT_DO_DIV,
+ BIGINT_DO_MOD,
+ BIGINT_DO_SHL,
+ BIGINT_DO_SHR,
+
+ BIGINT_UNM,
+ BIGINT_DO_NEG,
+
+ BIGINT_SQRT,
+ BIGINT_DO_SQRT,
+
+ BIGINT_GCD,
+ BIGINT_LCM,
+
+ BIGINT_EQ,
+ BIGINT_LT,
+ BIGINT_LE,
+
+ BIGINT_MODADD,
+ BIGINT_MODSUB,
+ BIGINT_MODMUL,
+ BIGINT_MODSQR,
+ BIGINT_MODINV,
+ BIGINT_MODPOW,
+
+ BIGINT_DO_MODADD,
+ BIGINT_DO_MODSUB,
+ BIGINT_DO_MODMUL,
+ BIGINT_DO_MODSQR,
+ BIGINT_DO_MODINV,
+ BIGINT_DO_MODPOW,
};
struct lua_functypes_t BigInt_functions[] = {
@@ -24,6 +62,7 @@ struct lua_functypes_t BigInt_functions[] = {
struct lua_functypes_t BigInt_methods[] = {
{ BIGINT_SET, "set", 1, 2, { BLUA_STRING | BLUA_NUMBER, BLUA_NUMBER } },
+ { BIGINT_SET2EXPT, "set2expt", 1, 1, { BLUA_NUMBER } },
{ BIGINT_TOSTRING, "tostring", 0, 1, { BLUA_NUMBER } },
{ BIGINT_ADD, "add", 1, 1, { BLUA_OBJECT } },
@@ -31,6 +70,43 @@ struct lua_functypes_t BigInt_methods[] = {
{ BIGINT_MUL, "mul", 1, 1, { BLUA_OBJECT } },
{ BIGINT_DIV, "div", 1, 1, { BLUA_OBJECT } },
{ BIGINT_MOD, "mod", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_SHL, "shl", 1, 1, { BLUA_NUMBER } },
+ { BIGINT_SHR, "shr", 1, 1, { BLUA_NUMBER } },
+
+ { BIGINT_DO_ADD, "do_add", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_SUB, "do_sub", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_MUL, "do_mul", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_DIV, "do_div", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_MOD, "do_mod", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_SHL, "do_shl", 1, 1, { BLUA_NUMBER } },
+ { BIGINT_DO_SHR, "do_shr", 1, 1, { BLUA_NUMBER } },
+
+ { BIGINT_UNM, "unm", 0, 0, { } },
+ { BIGINT_DO_NEG, "do_neg", 0, 0, { } },
+
+ { BIGINT_SQRT, "sqrt", 0, 0, { } },
+ { BIGINT_DO_SQRT, "do_sqrt", 0, 0, { } },
+
+ { BIGINT_GCD, "gcd", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_LCM, "lcm", 1, 1, { BLUA_OBJECT } },
+
+ { BIGINT_EQ, "eq", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_LT, "lt", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_LE, "le", 1, 1, { BLUA_OBJECT } },
+
+ { BIGINT_MODADD, "modadd", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_MODSUB, "modsub", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_MODMUL, "modmul", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_MODSQR, "modsqr", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_MODINV, "modinv", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_MODPOW, "modpow", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+
+ { BIGINT_DO_MODADD, "do_modadd", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_DO_MODSUB, "do_modsub", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_DO_MODMUL, "do_modmul", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
+ { BIGINT_DO_MODSQR, "do_modsqr", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_MODINV, "do_modinv", 1, 1, { BLUA_OBJECT } },
+ { BIGINT_DO_MODPOW, "do_modpow", 2, 2, { BLUA_OBJECT, BLUA_OBJECT } },
{ -1, 0, 0, 0, 0 },
};
@@ -87,6 +163,9 @@ int sLua_BigInt::BigInt_proceed(Lua & L, int n, BigInt * a, int caller) {
}
r = 0;
break;
+ case BIGINT_SET2EXPT:
+ a->set2expt(L.tonumber());
+ break;
case BIGINT_TOSTRING:
if (n == 3)
radix = L.tonumber(-1);
@@ -138,6 +217,189 @@ int sLua_BigInt::BigInt_proceed(Lua & L, int n, BigInt * a, int caller) {
*c = *a % *b;
r = 1;
break;
+ case BIGINT_SHL:
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = *a << L.tonumber();
+ r = 1;
+ break;
+ case BIGINT_SHR:
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = *a >> L.tonumber();
+ r = 1;
+ break;
+ case BIGINT_DO_ADD:
+ b = L.recast<BigInt>();
+ *a += *b;
+ break;
+ case BIGINT_DO_SUB:
+ b = L.recast<BigInt>();
+ *a -= *b;
+ break;
+ case BIGINT_DO_MUL:
+ b = L.recast<BigInt>();
+ *a *= *b;
+ break;
+ case BIGINT_DO_DIV:
+ b = L.recast<BigInt>();
+ *a /= *b;
+ break;
+ case BIGINT_DO_MOD:
+ b = L.recast<BigInt>();
+ *a %= *b;
+ break;
+ case BIGINT_DO_SHL:
+ *a <<= L.tonumber();
+ break;
+ case BIGINT_DO_SHR:
+ *a >>= L.tonumber();
+ break;
+ case BIGINT_UNM:
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->neg();
+ r = 1;
+ break;
+ case BIGINT_DO_NEG:
+ a->do_neg();
+ break;
+ case BIGINT_SQRT:
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->sqrt();
+ r = 1;
+ break;
+ case BIGINT_DO_SQRT:
+ a->do_sqrt();
+ break;
+ case BIGINT_GCD:
+ b = L.recast<BigInt>();
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->gcd(*b);
+ r = 1;
+ break;
+ case BIGINT_LCM:
+ b = L.recast<BigInt>();
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->lcm(*b);
+ r = 1;
+ break;
+ case BIGINT_EQ:
+ b = L.recast<BigInt>();
+ L.push(*a == *b);
+ r = 1;
+ break;
+ case BIGINT_LT:
+ b = L.recast<BigInt>();
+ L.push(*a < *b);
+ r = 1;
+ break;
+ case BIGINT_LE:
+ b = L.recast<BigInt>();
+ L.push(*a <= *b);
+ r = 1;
+ break;
+ case BIGINT_MODADD:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modadd(*b, *m);
+ r = 1;
+ break;
+ case BIGINT_MODSUB:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modsub(*b, *m);
+ r = 1;
+ break;
+ case BIGINT_MODMUL:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modmul(*b, *m);
+ r = 1;
+ break;
+ case BIGINT_MODSQR:
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modsqr(*m);
+ r = 1;
+ break;
+ case BIGINT_MODINV:
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modinv(*m);
+ r = 1;
+ break;
+ case BIGINT_MODPOW:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ {
+ LuaBigIntFactory cf(c = new BigInt());
+ cf.pushDestruct(L);
+ }
+ *c = a->modpow(*b, *m);
+ r = 1;
+ break;
+ case BIGINT_DO_MODADD:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ a->do_modadd(*b, *m);
+ break;
+ case BIGINT_DO_MODSUB:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ a->do_modsub(*b, *m);
+ break;
+ case BIGINT_DO_MODMUL:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ a->do_modmul(*b, *m);
+ break;
+ case BIGINT_DO_MODSQR:
+ m = L.recast<BigInt>(-1);
+ a->do_modsqr(*m);
+ break;
+ case BIGINT_DO_MODINV:
+ m = L.recast<BigInt>(-1);
+ a->do_modinv(*m);
+ break;
+ case BIGINT_DO_MODPOW:
+ b = L.recast<BigInt>(-2);
+ m = L.recast<BigInt>(-1);
+ a->do_modpow(*b, *m);
+ break;
}
return r;
@@ -176,6 +438,10 @@ void LuaBigIntFactory::pushStatics(Lua & L) {
METAMETHOD_WRAPPER(mul);
METAMETHOD_WRAPPER(mod);
PUSH_METAMETHOD(BigInt, BIGINT_TOSTRING);
+ PUSH_METAMETHOD(BigInt, BIGINT_UNM);
+ PUSH_METAMETHOD(BigInt, BIGINT_EQ);
+ PUSH_METAMETHOD(BigInt, BIGINT_LT);
+ PUSH_METAMETHOD(BigInt, BIGINT_LE);
L.settable(LUA_REGISTRYINDEX);
@@ -190,6 +456,7 @@ void LuaBigIntFactory::pushObjectAndMembers(Lua & L) {
L.setmetatable();
PUSH_METHOD(BigInt, BIGINT_SET);
+ PUSH_METHOD(BigInt, BIGINT_SET2EXPT);
PUSH_METHOD(BigInt, BIGINT_TOSTRING);
PUSH_METHOD(BigInt, BIGINT_ADD);
@@ -197,6 +464,24 @@ void LuaBigIntFactory::pushObjectAndMembers(Lua & L) {
PUSH_METHOD(BigInt, BIGINT_MUL);
PUSH_METHOD(BigInt, BIGINT_DIV);
PUSH_METHOD(BigInt, BIGINT_MOD);
+ PUSH_METHOD(BigInt, BIGINT_SHL);
+ PUSH_METHOD(BigInt, BIGINT_SHR);
+
+ PUSH_METHOD(BigInt, BIGINT_DO_ADD);
+ PUSH_METHOD(BigInt, BIGINT_DO_SUB);
+ PUSH_METHOD(BigInt, BIGINT_DO_MUL);
+ PUSH_METHOD(BigInt, BIGINT_DO_DIV);
+ PUSH_METHOD(BigInt, BIGINT_DO_MOD);
+ PUSH_METHOD(BigInt, BIGINT_DO_SHL);
+ PUSH_METHOD(BigInt, BIGINT_DO_SHR);
+
+ PUSH_METHOD(BigInt, BIGINT_DO_NEG);
+
+ PUSH_METHOD(BigInt, BIGINT_SQRT);
+ PUSH_METHOD(BigInt, BIGINT_DO_SQRT);
+
+ PUSH_METHOD(BigInt, BIGINT_GCD);
+ PUSH_METHOD(BigInt, BIGINT_LCM);
}
void Balau::registerLuaBigInt(Lua & L) {