libgmp lua pre init

master
wipedlife 2021-06-26 03:02:01 +03:00
parent 2c8609c99e
commit 42d119c773
11 changed files with 300 additions and 53 deletions

1
.gitignore vendored
View File

@ -52,3 +52,4 @@ m4/lt~obsolete.m4
# (which is called by configure script))
Makefile
src/Example.jpg
Example.jpg

View File

@ -28,6 +28,9 @@
/* Define to 1 if you have the `gd' library (-lgd). */
#define HAVE_LIBGD 1
/* Define to 1 if you have the `gmp' library (-lgmp). */
#define HAVE_LIBGMP 1
/* Define to 1 if you have the `iniparser' library (-liniparser). */
#define HAVE_LIBINIPARSER 1

3
config.h.in vendored
View File

@ -27,6 +27,9 @@
/* Define to 1 if you have the `gd' library (-lgd). */
#undef HAVE_LIBGD
/* Define to 1 if you have the `gmp' library (-lgmp). */
#undef HAVE_LIBGMP
/* Define to 1 if you have the `iniparser' library (-liniparser). */
#undef HAVE_LIBINIPARSER

39
configure vendored
View File

@ -4322,6 +4322,45 @@ _ACEOF
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lgmp" >&5
$as_echo_n "checking for main in -lgmp... " >&6; }
if ${ac_cv_lib_gmp_main+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lgmp $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
return main ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_gmp_main=yes
else
ac_cv_lib_gmp_main=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_gmp_main" >&5
$as_echo "$ac_cv_lib_gmp_main" >&6; }
if test "x$ac_cv_lib_gmp_main" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_LIBGMP 1
_ACEOF
LIBS="-lgmp $LIBS"
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -lz" >&5
$as_echo_n "checking for main in -lz... " >&6; }
if ${ac_cv_lib_z_main+:} false; then :

View File

@ -2,7 +2,7 @@
AUTOMAKE_OPTIONS = subdir-objects
lib_LIBRARIES = libbacteria.a
libbacteria_a_SOURCES = cryptocoins.c cryptocoins.h json_rpc.c macros.h\
lua/afirst.c lua/libcrypto.c lua/libjson.c lua/libencdec.c lua/libimages.c \
lua/afirst.c lua/libcrypto.c lua/libjson.c lua/libencdec.c lua/libimages.c lua/libgmp.c \
encdec/async/x25519.c encdec/sync/AES.c \
images/images.c
libbacteria_a_CFLAGS = -g -I /usr/include/luajit-2.1/ -I$(topdir)/libbacteria/ -I$(topdir)/luasql/src -L$(topdir)/luasql/src -I/usr/include/lua5.1/

View File

@ -1,17 +1,39 @@
#include<gmp.h>
#include<stdio.h>
void mul(mpz_t result, const mpz_t param, unsigned long n){
unsigned long i;
mpz_mul_ui(result, param, n);
for(i = 1; i<n;i++){
mpz_add_ui(result,result, i*7);// unsigned long add to mpz_t;
mpz_add(result,result,result);//mpz_t add to mpz_t
}
}
int main(int cn, char**v){
if(cn != 3) return fprintf(stderr,"%s a b\n",v[0]);
mpf_t x, y, c;
mpf_init (c);
mpf_init (x); /* use default precision */
mpf_init2 (y, 256); /* precision at least 256 bits */
mpf_set_str(x, v[1], 10);
mpf_set_str(y, v[2], 10);
mpf_init_set_str(x, v[1], 0);
mpf_init_set_str(y, v[2], 0);
mpf_div(c, x,y);
mpf_out_str(stdout, 10, 65535, c);
// mpf_out_str(stdout, 0, 65535, c);
gmp_printf("%.Ff\n", c);
puts("");
/* Unless the program is about to exit, do ... */
mpf_clear (x);
mpf_clear (y);
mpf_clear(c);
//
mpz_t r,n;
mpz_init(r);
mpz_init_set_str(n, "123321", 0);
mul(r,n,/*65535*/ 1000);
gmp_printf("%Zd\n", r);
mpz_clear(r);
mpz_clear(n);
}

View File

@ -2,6 +2,7 @@
#include "libencdec.h"
#include "libjson.h"
#include "libimages.h"
#include "libgmp.h"
void runAllLuaFilesInDir(lua_State *L, const char *path) {
DIR *dir;
@ -47,6 +48,7 @@ int start_lua(void) {
luaopen_rpc(L);
luaopen_encdec(L);
luaopen_libimages(L);
luaopen_liblgmp(L);
// luaopen_sql(L);
// initLuaSubmodules(L, "./luasubmodules");
lua_loadscript(L, init_lua_file_path);

View File

@ -0,0 +1,113 @@
#include"libgmp.h"
#include<stdlib.h>
//static void gmpZToBuf(mpz_t v, char * buf, size_t bufSize){
// gmp_snprintf(buf, bufSize, "%Zd", v);
//}
int luaopen_liblgmp(lua_State *L){
luaL_openlib(L, "lgmp", lgmp, 0);
return 1;
}
/*Integer*/
#define GETMP(prefix,name,where)\
mp##prefix * l##name = (mp##prefix *)lua_touserdata(L, where);
INITLUAFUNC(mpz_init){
mpz_t * rt = malloc(sizeof(mpz_t) * 1);
mpz_init(*rt);
lua_pushlightuserdata(L, rt);
return 1;
}
INITLUAFUNC(mpz_init_str){
mpz_t * rt = malloc(sizeof(mpz_t) * 1);
const char * val = (const char*)luaL_checkstring(L, 2);
mpz_init_set_str(*rt,val,0);
lua_pushlightuserdata(L, rt);
return 1;
}
INITLUAFUNC(mpz_add){
GETMP(z_t, a,1);
GETMP(z_t, b,2);
GETMP(z_t, c,3);
mpz_add(*la,*lb,*lc);
return 0;
}
INITLUAFUNC(mpz_add_ui){
GETMP(z_t, a,1);
GETMP(z_t, b,2);
int c = luaL_checknumber(L,3);
mpz_add_ui(*la,*lb,c);
return 0;
}
INITLUAFUNC(mpz_getstr){
GETMP(z_t, a,1);
int c = luaL_checknumber(L,2);
char buf[c];
gmp_snprintf(buf,c, "%Zd", *la);
lua_pushstring(L,buf);
return 1;
}
INITLUAFUNC(mpz_clear){
GETMP(z_t, a,1);
if( *la == NULL )luaL_error(L,"Invalid mpz pointer");
mpz_clear(*la);
free(la);
return 0;
}
/*Float*/
INITLUAFUNC(mpf_init){
}
INITLUAFUNC(mpf_add){
}
INITLUAFUNC(mpf_add_ui){
}
INITLUAFUNC(mpf_getstr){
}
INITLUAFUNC(mpf_init_str){
}
INITLUAFUNC(mpf_clear){
}
/*Rational*/
INITLUAFUNC(mpq_init){
}
INITLUAFUNC(mpq_add){
}
INITLUAFUNC(mpq_add_ui){
}
INITLUAFUNC(mpq_init_str){
}
INITLUAFUNC(mpq_getstr){
}
INITLUAFUNC(mpq_clear){
}

View File

@ -0,0 +1,50 @@
#pragma once
#include"lua.h"
#include<gmp.h>
INITLUAFUNC(mpz_init);
INITLUAFUNC(mpz_init_str);
INITLUAFUNC(mpz_getstr);
INITLUAFUNC(mpz_add);
INITLUAFUNC(mpz_add_ui);
INITLUAFUNC(mpz_clear);
INITLUAFUNC(mpf_init);
INITLUAFUNC(mpf_add);
INITLUAFUNC(mpf_add_ui);
INITLUAFUNC(mpf_getstr);
INITLUAFUNC(mpf_init_str);
INITLUAFUNC(mpf_clear);
INITLUAFUNC(mpq_init);
INITLUAFUNC(mpq_add);
INITLUAFUNC(mpq_add_ui);
INITLUAFUNC(mpq_getstr);
INITLUAFUNC(mpq_init_str);
INITLUAFUNC(mpq_clear);
static const struct luaL_reg lgmp [] = {
LUAPAIR(mpz_init)
LUAPAIR(mpz_init_str)
LUAPAIR(mpz_getstr)
LUAPAIR(mpz_add)
LUAPAIR(mpz_add_ui)
LUAPAIR(mpz_clear)
LUAPAIR(mpf_init)
LUAPAIR(mpf_add)
LUAPAIR(mpf_add_ui)
LUAPAIR(mpf_getstr)
LUAPAIR(mpf_init_str)
LUAPAIR(mpf_clear)
LUAPAIR(mpq_init)
LUAPAIR(mpq_add)
LUAPAIR(mpq_add_ui)
LUAPAIR(mpq_getstr)
LUAPAIR(mpq_init_str)
LUAPAIR(mpq_clear)
{NULL,NULL}
};
int luaopen_liblgmp(lua_State *L);

BIN
src/Example.jpg vendored

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

View File

@ -34,17 +34,20 @@ local function check_leak_memory(fcoin,scoin)
--print(tmp)
print(tmp['result'])
end
--tgst=cryptocoins.gettable(t,'tgst')
--tdash=cryptocoins.gettable(t,'tdash')
tgst = bacteria.coins['tgst']
tdash = bacteria.coins['tdash']
check_leak_memory(tgst,tdash)
local function CryptocoinsTest()
--tgst=cryptocoins.gettable(t,'tgst')
--tdash=cryptocoins.gettable(t,'tdash')
tgst = bacteria.coins['tgst']
tdash = bacteria.coins['tdash']
check_leak_memory(tgst,tdash)
-- example in luasubmodules/bencdec.lua
--key,iv=bacteria_aes.genKeyIV()
-- example in luasubmodules/bencdec.lua
--key,iv=bacteria_aes.genKeyIV()
end
msg="Hello AES_ECB, AES_CBC, ChaCha20! "
local function addChar(ch,time)
ret=""
while time > 0 do
@ -53,10 +56,7 @@ local function addChar(ch,time)
end
return ret
end
msg=msg .. addChar("s",666)
b=bacteria_aes.new() --("mysmallkey") -- ("mykey","myiv") 32,16 bytes
--print("key:", b:getKey(), "IV: ",b:getIV())
local function checkAllTypes(b,msg)
-- b:encrypt(msg)
-- b:decrypt( b:getAESData_rawEnc() )
@ -83,50 +83,57 @@ local function checkAllTypes(b,msg)
end
print("AES check!")
checkAllTypes(b,"is example message aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafasdjasdjkfsdfjasdfjiaodfasdfjiasdijfasidfadfiaojsdijfoasdfiaojsdfiojasdfijasdfuhasdufhasdiufhasidufashdfiasudhfiasudhfiuasdfihuSOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOFSDJKFASDJFASJDFQJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
print("\n\n\n\n\n\n\n\n\n\nx25519 + AES check!\n\n\n\n\n\n\n\n\n\n")
k1 = bacteria_aes:newKeyPair()
k2 = bacteria_aes:newKeyPair()
k1:initKeyPair()
k2:initKeyPair()
pub1 = k1:getPubKey()
pub2 = k2:getPubKey()
priv2 = k2:getPrivKey()
local function EncDecTest()
msg="Hello AES_ECB, AES_CBC, ChaCha20! "
msg=msg .. addChar("s",666)
b=bacteria_aes.new() --("mysmallkey") -- ("mykey","myiv") 32,16 bytes
--print("key:", b:getKey(), "IV: ",b:getIV())
print("AES check!")
checkAllTypes(b,"is example message aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafasdjasdjkfsdfjasdfjiaodfasdfjiasdijfasidfadfiaojsdijfoasdfiaojsdfiojasdfijasdfuhasdufhasdiufhasidufashdfiasudhfiasudhfiuasdfihuSOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOFSDJKFASDJFASJDFQJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
print("\n\n\n\n\n\n\n\n\n\nx25519 + AES check!\n\n\n\n\n\n\n\n\n\n")
k1 = bacteria_aes:newKeyPair()
k2 = bacteria_aes:newKeyPair()
k1:initKeyPair()
k2:initKeyPair()
pub1 = k1:getPubKey()
pub2 = k2:getPubKey()
priv2 = k2:getPrivKey()
print("Pub1: ", string.tohex(pub1), string.fromhex(string.tohex(pub1)) == pub1 )
print("Pub2: ", string.tohex(pub2), string.fromhex(string.tohex(pub2)) == pub2 )
print("Pub1: ", string.tohex(pub1), string.fromhex(string.tohex(pub1)) == pub1 )
print("Pub2: ", string.tohex(pub2), string.fromhex(string.tohex(pub2)) == pub2 )
k3 = bacteria_aes:newKeyPair()
k3:initKeyPair(pub2,priv2)
--print("Pub1: ", pub1)
--print("Priv2: ", priv2)
--print("Pub2: ", pub2)
k3 = bacteria_aes:newKeyPair()
k3:initKeyPair(pub2,priv2)
--print("Pub1: ", pub1)
--print("Priv2: ", priv2)
--print("Pub2: ", pub2)
pub3 = k3:getPubKey()
priv3 = k3:getPrivKey()
pub3 = k3:getPubKey()
priv3 = k3:getPrivKey()
--print("Pub3: ", pub3)
--print("Priv3: ", priv3)
--print("Pub3: ", pub3)
--print("Priv3: ", priv3)
shared0=k1:getSharedKey(pub2)
shared1=k2:getSharedKey(pub1)
shared2=k3:getSharedKey(pub1)
--print("Shared0:", shared0)
--print("Shared1:", shared1)
shared0=k1:getSharedKey(pub2)
shared1=k2:getSharedKey(pub1)
shared2=k3:getSharedKey(pub1)
--print("Shared0:", shared0)
--print("Shared1:", shared1)
aes1=bacteria_aes:new(shared0, "123456789012345")
aes=bacteria_aes:new(shared1, "123456789012345")
aes1:encrypt("TestMsg W10013291825328197ASHFASDF8932ASDF8532BUSAFD893251BSDFA78532BFW783125HBSFAD789aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafasdjasdjkfsdfjasdfjiaodfasdfjiasdijfasidfadfiaojsdijfoasdfiaojsdfiojasdfijasdfuhasdufhasdiufhasidufashdfiasudhfiasudhfiuasdfihuSOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOFSDJKFASDJFASJDFQJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
--print("Encrypted")
aes:decrypt(aes1:getAESData_rawEnc())
aesdata_dec,saesdata_dec=aes:getAESData_dec()
print("Decrypted: ", aesdata_dec)
aes1=bacteria_aes:new(shared0, "123456789012345")
aes=bacteria_aes:new(shared1, "123456789012345")
aes1:encrypt("TestMsg W10013291825328197ASHFASDF8932ASDF8532BUSAFD893251BSDFA78532BFW783125HBSFAD789aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaafasdjasdjkfsdfjasdfjiaodfasdfjiasdijfasidfadfiaojsdijfoasdfiaojsdfiojasdfijasdfuhasdufhasdiufhasidufashdfiasudhfiasudhfiuasdfihuSOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOFSDJKFASDJFASJDFQJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
--print("Encrypted")
aes:decrypt(aes1:getAESData_rawEnc())
aesdata_dec,saesdata_dec=aes:getAESData_dec()
print("Decrypted: ", aesdata_dec)
k1.clear()
k2.clear()
k3.clear()
k1.clear()
k2.clear()
k3.clear()
end
local function checkGD(width,height,quality)
width = width or 120
@ -171,7 +178,14 @@ local function checkGD(width,height,quality)
--print((c), "size: ",s)
images.gdFree(img)
end
checkGD(120,120,100)
--CryptocoinsTest()
--checkGD(120,120,100)
--EncDecTest()
print("GMP")
myZ = lgmp.mpz_init()
lgmp.mpz_add_ui(myZ,myZ, 1024)
lgmp.mpz_add_ui(myZ,myZ, 4201)
print("New value is:",lgmp.mpz_getstr(myZ, 128))
lgmp.mpz_clear(myZ)