Skip to content

Commit

Permalink
Merge pull request ElementsProject#8 from sg777/master
Browse files Browse the repository at this point in the history
memory leaks handled
  • Loading branch information
jl777 committed Nov 23, 2017
2 parents 27f03eb + f9930ea commit f310ece
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 15 deletions.
Binary file modified privatebet/bet
Binary file not shown.
109 changes: 95 additions & 14 deletions privatebet/bet.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ bits256 *allshares=NULL;
uint8_t sharenrs[256];
struct rpcrequest_info *LP_garbage_collector;
struct enc_share { uint8_t share[sizeof(bits256)+crypto_box_NONCEBYTES+crypto_box_ZEROBYTES]; };
struct enc_share *g_shares=NULL;
//struct enc_share *g_shares=NULL;
struct enc_share g_shares[CARDS777_MAXPLAYERS*CARDS777_MAXPLAYERS*CARDS777_MAXCARDS];

bits256 v_hash[CARDS777_MAXCARDS][CARDS777_MAXCARDS];
bits256 g_hash[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS];
uint32_t LP_rand()
Expand Down Expand Up @@ -305,17 +307,76 @@ int main(int argc,const char *argv[])
else
{
printf("no argjson, default to testmode\n");

while ( testmode != 1 )

#if 1 //using threads
pthread_t player_t[CARDS777_MAXPLAYERS],dcv_t,bvv_t;
OS_randombytes((uint8_t *)&range,sizeof(range));
OS_randombytes((uint8_t *)&numplayers,sizeof(numplayers));
range = (range % 52) + 1;
numplayers = (numplayers % (CARDS777_MAXPLAYERS-1)) + 2;
printf("\nnumplayers=%d, numcards=%d\n",numplayers,range);

for(i=0;i<numplayers && 0;i++){
if ( OS_thread_create(&player_t[i],NULL,(void *)BET_player,(void *)i) != 0 )
{
printf("error launching BET_clientloop\n");
exit(-1);
}
printf("\n:%d",i);
}
i=0;
if ( OS_thread_create(&player_t[i],NULL,(void *)BET_player,(void *)&i) != 0 )
{
printf("error launching BET_clientloop\n");
exit(-1);
}
i=1;
if ( OS_thread_create(&player_t[i],NULL,(void *)BET_player,(void *)&i) != 0 )
{
printf("error launching BET_clientloop\n");
exit(-1);
}
/*
if ( OS_thread_create(&dcv_t,NULL,(void *)BET_dcv,(void *)BET) != 0 )
{
printf("error launching BET_clientloop\n");
exit(-1);
}
if ( OS_thread_create(&bvv_t,NULL,(void *)BET_bvv,(void *)BET) != 0 )
{
printf("error launching BET_clientloop\n");
exit(-1);
}
*/
for(i=0;i<numplayers;i++){
if(pthread_join(player_t[i],NULL)){
printf("\nError in joining the main thread for player thread %d",i);
}
}
/*
if(pthread_join(dcv_t,NULL)){
printf("\nError in joining the main thread for DCV thread");
}
if(pthread_join(bvv_t,NULL)){
printf("\nError in joining the main thread for BVV thread");
}
*/



#endif
testmode=1;
while ( testmode != 1 )
{

OS_randombytes((uint8_t *)&range,sizeof(range));
OS_randombytes((uint8_t *)&numplayers,sizeof(numplayers));
range = (range % 52) + 1;
numplayers = (numplayers % (CARDS777_MAXPLAYERS-1)) + 2;
printf("\nnumplayers=%d, numcards=%d\n",numplayers,range);
//numplayers=2,range=2;
sg777_players_init(numplayers,range,rand256(0));
sg777_players_init(numplayers,range,rand256(0));
continue;
for (i=0; i<numplayers; i++)
privkeys[i] = curve25519_keypair(&pubkeys[i]);
Expand Down Expand Up @@ -461,6 +522,8 @@ void blinding_vendor(bits256 *blindings,bits256 *blindedcards,bits256 *finalcard
allshares[j*numplayers*numcards + (i*numplayers + playerid)] = cardshares[j];
}
}
free(recover);
free(cardshares);
// when all players have submitted their finalcards, blinding vendor can send encrypted allshares for each player, see cards777.c
}

Expand Down Expand Up @@ -611,7 +674,7 @@ bits256 sg777_player_decode(int32_t playerid,int32_t cardID,int numplayers,struc
bits256 decoded,tmp,xoverz,hash,fe,refval,basepoint,*cardshares; int32_t i,j,k,unpermi,M; char str[65];
bits256 *recover=NULL;
struct enc_share temp;
uint8_t **shares;
uint8_t **shares,flag=0;
shares=calloc(numplayers,sizeof(uint8_t*));
for(i=0;i<numplayers;i++)
shares[i]=calloc(sizeof(bits256),sizeof(uint8_t));
Expand All @@ -635,7 +698,7 @@ bits256 sg777_player_decode(int32_t playerid,int32_t cardID,int numplayers,struc
}
gfshare_recoverdata(shares,sharenrs, M,recover->bytes,sizeof(bits256),M);
refval = fmul_donna(blindedcard,crecip_donna(*recover));

#if 1
for (i=0; i<numcards; i++)
{
for (j=0; j<numcards; j++)
Expand All @@ -649,13 +712,26 @@ bits256 sg777_player_decode(int32_t playerid,int32_t cardID,int numplayers,struc
if ( bits256_cmp(decoded,cardprods[j]) == 0 )
{
printf("player.%d decoded card %s value %d\n",playerid,bits256_str(str,decoded),playerprivs[i].bytes[30]);
return(playerprivs[i]);
//return(playerprivs[i]);
tmp=playerprivs[i];
flag=1;
goto end;
}
}
}
}
printf("couldnt decode blindedcard %s\n",bits256_str(str,blindedcard));
memset(tmp.bytes,0,sizeof(tmp));
#endif
end:
free(recover);
for(i=0;i<numplayers;i++){
free(shares[i]);
}
free(shares);
free(cardshares);
if(!flag){
memset(tmp.bytes,0,sizeof(tmp));
printf("couldnt decode blindedcard %s\n",bits256_str(str,blindedcard));
}
return(tmp);
}

Expand All @@ -682,26 +758,30 @@ struct pair256 sg777_blinding_vendor(struct pair256 *keys,struct pair256 b_key,b

gfshare_calc_sharenrs(sharenrs,numplayers,deckid.bytes,sizeof(deckid)); // same for all players for this round
cardshares = calloc(numplayers,sizeof(bits256));
if ( g_shares == 0)
/* if ( g_shares == 0)
g_shares= calloc(numplayers,sizeof(struct enc_share) * numplayers * numcards);

*/


for (i=0; i<numcards; i++)
{
gfshare_calc_shares(cardshares[0].bytes,blindings[i].bytes,sizeof(bits256),sizeof(bits256),M,numplayers,sharenrs,space,sizeof(space));
// create combined allshares
for (j=0; j<numplayers; j++) {
BET_ciphercreate(b_key.priv,keys[j].prod,temp.share,cardshares[j].bytes,sizeof(cardshares[j]));
memcpy(g_shares[j*numplayers*numcards + (i*numplayers + playerid)].share,temp.share,sizeof(temp));
memcpy(g_shares[j*numplayers*numcards + (i*numplayers + playerid)].share,temp.share,sizeof(temp));
}
}
// when all players have submitted their finalcards, blinding vendor can send encrypted allshares for each player, see cards777.c
free(recover);
free(cardshares);
return b_key;
}

void sg777_players_init(int32_t numplayers,int32_t numcards,bits256 deckid)
{
static int32_t decodebad,decodegood,good,bad;
int32_t i,j,k,playerid,errs,unpermi,playererrs,decoded[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],permis[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS]; bits256 playerprivs[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],playercards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS]; char str[65];
int32_t i,j,k,playerid,errs,unpermi,playererrs=0,decoded[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],permis[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS]; bits256 playerprivs[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],playercards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS]; char str[65];
struct pair256 keys[CARDS777_MAXPLAYERS],b_key;
bits256 temp,decoded256,basepoint,cardprods[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],finalcards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],blindingvals[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS],blindedcards[CARDS777_MAXPLAYERS][CARDS777_MAXCARDS];
dekgen_vendor_perm(numcards);
Expand Down Expand Up @@ -760,4 +840,5 @@ void sg777_players_init(int32_t numplayers,int32_t numcards,bits256 deckid)
}
}
printf("numplayers.%d numcards.%d deck %s -> playererrs.%d good.%d bad.%d decode.[good %d, bad %d]\n",numplayers,numcards,bits256_str(str,deckid),playererrs,good,bad,decodegood,decodebad);

}
3 changes: 3 additions & 0 deletions privatebet/bet.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ References

#include "../../SuperNET/crypto777/OS_portable.h"
#include "../../SuperNET/iguana/exchanges/LP_include.h"
//#include "../../SuperNET/iguana/exchanges/LP_nativeDEX.c"


#if defined(WIN32) || defined(USE_STATIC_NANOMSG)
#include "../../SuperNET/crypto777/nanosrc/nn.h"
#include "../../SuperNET/crypto777/nanosrc/bus.h"
Expand Down
28 changes: 28 additions & 0 deletions privatebet/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,34 @@ int32_t BET_clientupdate(cJSON *argjson,uint8_t *ptr,int32_t recvlen,struct priv
return(-1);
}

void* BET_player(void *_ptr)
{
int playerid=(int)_ptr;
while(1){
printf("\nPlayer %d",playerid);
sleep(5);
}
return NULL;
}

void* BET_dcv(void *_ptr)
{
while(1){
printf("\ndcv");
sleep(5);
}
return NULL;
}

void* BET_bvv(void *_ptr)
{
while(1){
printf("\nbvv");
sleep(5);
}
return NULL;
}

void BET_clientloop(void *_ptr)
{
uint32_t lasttime = 0; int32_t nonz,recvlen,lastChips_paid; uint16_t port=7798; char connectaddr[64],hostip[64]; void *ptr; cJSON *msgjson,*reqjson; struct privatebet_vars *VARS; struct privatebet_info *bet = _ptr;
Expand Down
2 changes: 1 addition & 1 deletion privatebet/m_bet
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export ROOT="$HOME/SuperNET"
export C777="$ROOT/crypto777"
export IGUANA="$ROOT/iguana"
#-DFROM_MARKETMAKER=1
gcc -g -DFROM_PRIVATEBET -o bet bet.c $IGUANA/secp256k1/src/secp256k1.c $IGUANA/mini-gmp.c $IGUANA/exchanges/stats.c $C777/OS_nonportable.c $C777/curve25519-donna.c $C777/OS_portable.c $C777/OS_time.c $C777/SaM.c $C777/bitcoind_RPC.c $C777/cJSON.c $C777/curve25519.c $C777/iguana_OS.c $C777/iguana_utils.c $C777/inet.c $C777/tweetnacl.c ../cli/cli_main.o ../common/json.o ../common/configdir.o ../lightningd/log.o ../common/pseudorand.o ../bitcoin/pubkey.o ../ccan-*.o ../external/jsmn.o ../common/version.o ../common/type_to_string.o ../common/utils.o -lnanomsg -lcurl /$ROOT/agents/libcrypto777.a -lbacktrace -lpthread -lm
gcc -w -g -DFROM_PRIVATEBET -o bet bet.c $IGUANA/secp256k1/src/secp256k1.c $IGUANA/mini-gmp.c $IGUANA/exchanges/stats.c $C777/OS_nonportable.c $C777/curve25519-donna.c $C777/OS_portable.c $C777/OS_time.c $C777/SaM.c $C777/bitcoind_RPC.c $C777/cJSON.c $C777/curve25519.c $C777/iguana_OS.c $C777/iguana_utils.c $C777/inet.c $C777/tweetnacl.c ../cli/cli_main.o ../common/json.o ../common/configdir.o ../lightningd/log.o ../common/pseudorand.o ../bitcoin/pubkey.o ../ccan-*.o ../external/jsmn.o ../common/version.o ../common/type_to_string.o ../common/utils.o -lnanomsg -lcurl /$ROOT/agents/libcrypto777.a -lbacktrace -lpthread -lm

0 comments on commit f310ece

Please sign in to comment.