uint64_t calc_generator(struct crypto777_generator *gen,uint64_t stake,char *email,uint32_t timestamp,uint64_t prevmetric,bits384 prevgen,uint64_t numrounds) { uint64_t hit; if ( timestamp == 0 ) timestamp = (uint32_t)time(NULL); memset(gen,0,sizeof(*gen)); strncpy(gen->email,email,sizeof(gen->email)-1); gen->timestamp = timestamp; hit = SaM(&gen->hash,(uint8_t *)&prevgen,(int32_t)sizeof(prevgen),(uint8_t *)email,(int32_t)strlen(email)); if ( (gen->hit= (stake / (hit + 1))) > MAX_CRYPTO777_HIT ) { printf("clip to max\n"); gen->hit = MAX_CRYPTO777_HIT; } gen->metric = (gen->hit + prevmetric); return(gen->metric); }
struct crypto777_block *sign_block(struct crypto777_node *nn,struct crypto777_generator *gen,uint8_t *rawblock,int32_t rawblocklen,uint32_t blocknum,uint32_t timestamp) { struct crypto777_block *block; uint32_t blocksize; bits384 blockhash; blocksize = (sizeof(struct crypto777_block) + rawblocklen); if ( (blocksize & 0xf) != 0 ) blocksize += 0x10 - (blocksize & 0xf); block = calloc(1,blocksize); if ( timestamp == 0 ) timestamp = (uint32_t)time(NULL); block->blocksize = blocksize, block->rawblocklen = rawblocklen, block->blocknum = blocknum, block->timestamp = timestamp; block->gen = *gen; if ( rawblock != 0 && rawblocklen != 0 ) memcpy(block->rawblock,rawblock,rawblocklen); SaM(&blockhash,(uint8_t *)block,blocksize,0,0); block->hash = blockhash; //calc_SaM(&block->blocksig,blockhash.bytes,sizeof(blockhash),nn->broadcast.shared_curve25519.bytes,sizeof(bits256),SAM_MAGIC_NUMBER); block->sig.txid = nn->nodeid; return(block); }
struct crypto777_packet *crypto777_packet(struct crypto777_link *conn,uint32_t *timestamp,uint8_t *msg,int32_t len,int32_t leverage,int32_t maxmillis,uint64_t dest64bits) { static uint32_t rseed = 1234; uint64_t hit = SAMHIT_LIMIT; int32_t defnumrounds=NUMROUNDS_NONCE,numrounds,codersize,netsize,allocsize = MAX_UDPSIZE; struct crypto777_packet *bufs; struct SaMhdr *hdr; bits384 checksig; if ( *timestamp == 0 ) *timestamp = (uint32_t)time(NULL); netsize = (allocsize - 2*sizeof(uint32_t)); if ( conn->my64bits == conn->send.nxt64bits && conn->recv.nxt64bits == dest64bits && memcmp(conn->send.prevmsg,msg,len) == 0 && *timestamp == conn->send.prevtimestamp ) while ( conn->send.prevtimestamp == time(NULL) ) msleep(250), fprintf(stderr,". "), *timestamp = (uint32_t)time(NULL); if ( Debuglevel > 0 ) printf("(%llu %llu) packet.(%s) timestamp.%u [%llx] len.%d -> %llu\n",(long long)conn->send.nxt64bits,(long long)conn->recv.nxt64bits,(conn->send.nxt64bits != dest64bits) ? (char *)&msg[sizeof(struct SaMhdr)] : "ENCRYPTED",*timestamp,*(long long *)msg,len,(long long)dest64bits); codersize = (sizeof(*bufs->coder) + sizeof(uint16_t)*(0x100 + 2)); bufs = calloc(1,sizeof(struct crypto777_packet) + codersize + (2 * sizeof(struct crypto777_bits)) + (sizeof(struct SaMhdr) + len) + MAX_CRYPTO777_MSG); //bufs->DL.ptr = bufs; bufs->coder = (struct ramcoder *)bufs->space, memset(bufs->coder,0,codersize); bufs->plaintext = (struct crypto777_bits *)&bufs->space[codersize], _init_HUFF(&bufs->plainH,netsize,bufs->plaintext->bits); bufs->encrypted = &bufs->plaintext[1], _init_HUFF(&bufs->encryptedH,netsize,&bufs->encrypted->bits); bufs->origmsg = (uint8_t *)&bufs->plaintext[2]; bufs->recvbuf = &bufs->origmsg[sizeof(struct SaMhdr) + len]; // must be last! if ( dest64bits != 0 && conn->send.nxt64bits != 0 ) calc_sha256cat(bufs->coderseed.bytes,(uint8_t *)timestamp,sizeof(*timestamp),conn->shared_curve25519.bytes,sizeof(conn->shared_curve25519)); bufs->encrypted->plaintext_timestamp = bufs->plaintext->plaintext_timestamp = *timestamp; bufs->origlen = len; //printf("hdr.%ld len.%d\n",sizeof(struct SaMhdr),len); if ( conn->recv.nxt64bits == dest64bits && maxmillis > 0 ) { hdr = (struct SaMhdr *)bufs->origmsg; memcpy(&bufs->origmsg[sizeof(*hdr)],msg,len); hdr->timestamp = *timestamp, hdr->leverage = leverage, hdr->numrounds = defnumrounds; bufs->threshold = SaM_threshold(leverage); len += (int32_t)(sizeof(*hdr) - sizeof(hdr->sig)); //printf("+= %d | %ld = (%ld - %ld)\n",len,sizeof(*hdr) - sizeof(hdr->sig),sizeof(*hdr),sizeof(hdr->sig)); /*if ( (hit = SaMnonce(&hdr->sig,&hdr->nonce,&bufs->origmsg[sizeof(hdr->sig)],len,bufs->threshold,rseed,maxmillis)) == 0 ) { printf("crypto777_packet cant find nonce: numrounds.%d leverage.%d\n",hdr->numrounds,leverage); free(bufs); return(0); }*/printf("deprecated SaMnonce\n"); while ( 1 ) sleep(60); if ( Debuglevel > 0 ) printf(">>>>>>>>>>>> outbound timestamp.%u nonce.%u hit.%llu (%s) | sig.%llx\n",hdr->timestamp,hdr->nonce,(long long)hit,&bufs->origmsg[sizeof(*hdr)],(long long)hdr->sig.txid); rseed = (uint32_t)(hdr->sig.txid ^ hit); len += sizeof(hdr->sig); if ( dest64bits != 0 ) { ramcoder_encoder(bufs->coder,1,bufs->origmsg,len,&bufs->plainH,&bufs->coderseed); if ( (bufs->newlen= hconv_bitlen(bufs->plainH.bitoffset)) >= bufs->plainH.allocsize ) { printf("crypto777_packet overflow: newlen.%d doesnt fit into %d\n",bufs->newlen,bufs->plainH.allocsize); free(bufs); return(0); } else _randombytes(&bufs->plainH.buf[bufs->newlen],bufs->plainH.allocsize - bufs->newlen,(uint32_t)hdr->sig.txid); if ( dest64bits != 0 ) crypto777_encrypt(&bufs->encryptedH,&bufs->plainH,netsize,&conn->send,*timestamp); } conn->send.prevlen = bufs->origlen, memcpy(conn->send.prevmsg,msg,bufs->origlen), conn->send.prevtimestamp = *timestamp; } else if ( conn->send.nxt64bits == dest64bits && milliseconds() > conn->recv.blacklist ) { if ( dest64bits != 0 ) { if ( dest64bits != 0 ) memcpy(bufs->encryptedH.buf,msg,len), crypto777_encrypt(&bufs->plainH,&bufs->encryptedH,len,&conn->recv,*timestamp); else memcpy(bufs->plainH.buf,msg,len); hseek(&bufs->plainH,len << 3,SEEK_SET), hrewind(&bufs->plainH); bufs->recvlen = ramcoder_decoder(bufs->coder,1,bufs->recvbuf,len,&bufs->plainH,&bufs->coderseed); } else memcpy(bufs->recvbuf,msg,len), bufs->recvlen = len; hdr = (struct SaMhdr *)bufs->recvbuf; leverage = hdr->leverage, numrounds = hdr->numrounds; if ( Debuglevel > 0 ) printf("<<<<<<<< nonce.%u len.%d leverage.%d numrounds.%d\n",hdr->nonce,len,leverage,(int)numrounds); if ( leverage < CRYPTO777_MAXLEVERAGE && numrounds == defnumrounds ) { bufs->threshold = SaM_threshold(leverage); len = bufs->recvlen - sizeof(hdr->sig); hit = SaM(&checksig,&bufs->recvbuf[sizeof(hdr->sig)],len,0,0); len -= (sizeof(*hdr) - sizeof(hdr->sig)); if ( hit >= bufs->threshold || memcmp(checksig.bytes,hdr->sig.bytes,sizeof(checksig)) != 0 ) { conn->recv.blacklist = (milliseconds() + 10000); printf("crypto777_packet invalid AUTH: hit %llu >= threshold.%llu || sig mismatch.%d (%llx v %llx) from %llu leverage.%d numrounds.%d\n",(long long)hit,(long long)bufs->threshold,memcmp(checksig.bytes,hdr->sig.bytes,sizeof(checksig)),(long long)checksig.txid,(long long)hdr->sig.txid,(long long)conn->recv.nxt64bits,hdr->leverage,hdr->numrounds); free(bufs); return(0); } else if ( Debuglevel > 0 ) printf("AUTHENTICATED packet from %llu len.%d leverage.%d numrounds.%d | hit %.2f%%\n",(long long)conn->recv.nxt64bits,len,leverage,numrounds,100.*(double)hit/bufs->threshold); } else printf("AUTH failure from %llu: hit.%llu >= threshold.%llu leverage.%d numrounds.%d\n",(long long)conn->recv.nxt64bits,(long long)hit,(long long)bufs->threshold,leverage,numrounds); } else printf("crypto777_packet warning invalid dest64bits %llu\n",(long long)dest64bits); return(bufs); }