diff --git a/crypto777/OS_portable.h b/crypto777/OS_portable.h index 68c87a088..c0a2d540a 100755 --- a/crypto777/OS_portable.h +++ b/crypto777/OS_portable.h @@ -221,6 +221,7 @@ int32_t decode_hex(unsigned char *bytes,int32_t n,char *hex); void reverse_hexstr(char *str); int32_t init_hexbytes_noT(char *hexbytes,uint8_t *message,long len); uint16_t parse_ipaddr(char *ipaddr,char *ip_port); +int32_t bitweight(uint64_t x); long _stripwhite(char *buf,int accept); int32_t is_DST(int32_t datenum); diff --git a/crypto777/SaM.c b/crypto777/SaM.c index 51bcad547..cf6b66dea 100755 --- a/crypto777/SaM.c +++ b/crypto777/SaM.c @@ -229,15 +229,15 @@ int32_t _SaM_test(char *msg,TRIT *testvector,int32_t n,TRIT *checkvals) return(errs); } -int32_t bitweight(uint64_t x) +/*int32_t bitweight(uint64_t x) { int i,wt = 0; for (i=0; i<64; i++) if ( (1LL << i) & x ) wt++; return(wt); -} - +}*/ +int32_t bitweight(uint64_t x); #define SETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] |= (1 << ((bitoffset) & 7))) #define GETBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] & (1 << ((bitoffset) & 7))) #define CLEARBIT(bits,bitoffset) (((uint8_t *)bits)[(bitoffset) >> 3] &= ~(1 << ((bitoffset) & 7))) diff --git a/crypto777/iguana_utils.c b/crypto777/iguana_utils.c index 47af03ad9..e2ec96f91 100755 --- a/crypto777/iguana_utils.c +++ b/crypto777/iguana_utils.c @@ -94,6 +94,15 @@ bits256 bits256_from_compact(uint32_t c) return(x); } +int32_t bitweight(uint64_t x) +{ + int i,wt = 0; + for (i=0; i<64; i++) + if ( (1LL << i) & x ) + wt++; + return(wt); +} + void calc_OP_HASH160(char hexstr[41],uint8_t hash160[20],char *pubkey) { uint8_t sha256[32],buf[4096]; int32_t len; diff --git a/crypto777/ramcoder.c b/crypto777/ramcoder.c index 4865c6172..76d46faac 100755 --- a/crypto777/ramcoder.c +++ b/crypto777/ramcoder.c @@ -132,7 +132,7 @@ int32_t hgetbit(HUFF *hp) //fprintf(stderr,"<-%d ",bit); return(bit); } - printf("hgetbit past EOF: %d >= %d\n",hp->bitoffset,hp->endpos);//, getchar(); + //printf("hgetbit past EOF: %d >= %d\n",hp->bitoffset,hp->endpos);//, getchar(); return(-1); } diff --git a/iguana/SuperNET.c b/iguana/SuperNET.c index 6218bc22d..27aea3d6d 100644 --- a/iguana/SuperNET.c +++ b/iguana/SuperNET.c @@ -88,7 +88,7 @@ void SuperNET_myipaddr(struct supernet_info *myinfo,struct iguana_info *coin,str //printf("myipaddr.%s self.%x your.%x\n",myinfo->ipaddr,myinfo->myaddr.selfipbits,myinfo->myaddr.myipbits); } -int32_t SuperNET_json2bits(struct supernet_info *myinfo,int32_t validpub,uint8_t *serialized,int32_t *complenp,uint8_t *compressed,int32_t maxsize,char *destip,bits256 destpub,cJSON *json) +int32_t SuperNET_json2bits(struct supernet_info *myinfo,int32_t plaintext,int32_t validpub,uint8_t *serialized,int32_t *complenp,uint8_t *compressed,int32_t maxsize,char *destip,bits256 destpub,cJSON *json) { uint16_t apinum; uint32_t ipbits,crc; uint64_t tag; bits256 priv,seed,seed2; char *hexmsg; int32_t n,numbits,len = sizeof(uint32_t); *complenp = -1; @@ -115,13 +115,16 @@ int32_t SuperNET_json2bits(struct supernet_info *myinfo,int32_t validpub,uint8_t } crc = calc_crc32(0,&serialized[sizeof(crc)],len - sizeof(crc)); iguana_rwnum(1,serialized,sizeof(crc),&crc); - //memset(seed.bytes,0,sizeof(seed)); - //int32_t testbits = ramcoder_compress(&compressed[3],maxsize-3,serialized,len,seed); - if ( validpub != 0 ) - priv = myinfo->privkey; - else priv = GENESIS_PRIVKEY; - seed = curve25519_shared(priv,destpub); - vcalc_sha256(0,seed2.bytes,seed.bytes,sizeof(seed)); + if ( plaintext != 0 ) + memset(seed.bytes,0,sizeof(seed)); + else + { + if ( validpub != 0 ) + priv = myinfo->privkey; + else priv = GENESIS_PRIVKEY; + seed = curve25519_shared(priv,destpub); + vcalc_sha256(0,seed2.bytes,seed.bytes,sizeof(seed)); + } //char str[65],str2[65],str3[65],str4[65]; //int32_t i; for (i=0; ivalidpub,&serialized[sizeof(struct iguana_msghdr)],&complen,&compressed[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,addr->pubkey,json); + datalen = SuperNET_json2bits(SuperNET_MYINFO(0),juint(json,"plaintext"),addr->validpub,&serialized[sizeof(struct iguana_msghdr)],&complen,&compressed[sizeof(struct iguana_msghdr)],IGUANA_MAXPACKETSIZE,addr->ipaddr,addr->pubkey,json); printf("SUPERSEND.(%s) -> (%s) delaymillis.%d datalen.%d\n",jsonstr,addr->ipaddr,delaymillis,datalen); if ( datalen >= 0 ) { @@ -245,10 +248,19 @@ int32_t iguana_send_supernet(struct iguana_info *coin,struct iguana_peer *addr,c return(qlen); } +int32_t DHT_dist(bits256 desthash,bits256 hash) +{ + int32_t i,dist = 0; + for (i=0; i<4; i++) + dist += bitweight(desthash.ulongs[i] ^ hash.ulongs[i]); + printf("(dist.%d) ",dist); + return(dist); +} + char *SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,char *hexmsg,int32_t maxdelay) { static int lastpurge; static uint64_t Packetcache[1024]; - bits256 packethash; char retbuf[512]; int32_t i,j,datalen,firstz,iter,n = 0; char *jsonstr=0; + bits256 packethash; char retbuf[512]; int32_t mydist,i,j,datalen,firstz,iter,n = 0; char *jsonstr=0; struct iguana_peer *addr; cJSON *json; if ( myinfo == 0 ) return(clonestr("{\"error\":\"no supernet_info\"}")); @@ -281,6 +293,7 @@ char *SuperNET_DHTsend(struct supernet_info *myinfo,bits256 routehash,char *hexm if ( lastpurge >= sizeof(Packetcache)/sizeof(*Packetcache) ) lastpurge = 0; } + mydist = 256+DHT_dist(packethash,myinfo->myaddr.iphash); for (iter=0; iter<2; iter++) { for (i=0; i %d\n",bits256_str(str,packethash),bits256_str(str2,addr->iphash),bits256_cmp(addr->iphash,packethash)); - if ( bits256_cmp(addr->iphash,packethash) < 0 ) + if ( DHT_dist(packethash,addr->iphash) < mydist ) { iguana_send_supernet(Coins[i],addr,jsonstr,maxdelay==0?0:rand()%maxdelay); n++;