diff --git a/src/cc/CCinclude.h b/src/cc/CCinclude.h index b7186e317..1af6ef4d1 100644 --- a/src/cc/CCinclude.h +++ b/src/cc/CCinclude.h @@ -30,6 +30,8 @@ #define SMALLVAL 0.000000000000001 +union _bits256 { uint8_t bytes[32]; uint16_t ushorts[16]; uint32_t uints[8]; uint64_t ulongs[4]; uint64_t txid; }; +typedef union _bits256 bits256; struct CCcontract_info { @@ -79,4 +81,10 @@ void SetCCtxids(std::vector > &addressIndex uint64_t AddNormalinputs(CMutableTransaction &mtx,CPubKey mypk,uint64_t total,int32_t maxinputs); uint64_t CCutxovalue(char *coinaddr,uint256 utxotxid,int32_t utxovout); +// curve25519 and sha256 +bits256 curve25519_shared(bits256 privkey,bits256 otherpub); +bits256 curve25519_basepoint9(); +bits256 curve25519(bits256 mysecret,bits256 basepoint); +void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); + #endif diff --git a/src/cc/dice.cpp b/src/cc/dice.cpp index ab6e9bdf7..b54caaa24 100644 --- a/src/cc/dice.cpp +++ b/src/cc/dice.cpp @@ -26,25 +26,25 @@ 2. and 3. can be done in mempool */ -void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len); -void ed25519_create_keypair(uint8_t *,uint8_t *,uint8_t *); -void ed25519_key_exchange(uint8_t *,uint8_t *,uint8_t *); - -uint256 DiceHashEntropy(uint256 &entropy,uint256 txidseed) // assumes little endian CPU and max 1 vout per txid used +uint256 DiceHashEntropy(uint256 &entropy,uint256 txidpriv) // assumes little endian CPU and max 1 vout per txid used { - int32_t i; uint8_t tmp256[32],tmpseed[32],txidpub[32],txidpriv[32],mypriv[32],mypub[32],myseed[32],ssecret[32],ssecret2[32]; uint256 hentropy; + int32_t i; bits256 tmp256,txidpub,mypriv,mypub,ssecret,ssecret2; uint256 hentropy; memset(&hentropy,0,32); - ed25519_create_keypair(txidpub,txidpriv,(uint8_t *)&txidseed); - Myprivkey(tmp256); - vcalc_sha256(0,tmpseed,tmp256,32); - ed25519_create_keypair(mypub,mypriv,tmpseed); - ed25519_key_exchange(ssecret,txidpub,mypriv); - ed25519_key_exchange(ssecret2,mypub,txidpriv); - if ( memcmp(ssecret,ssecret2,32) == 0 ) + txidpriv.bytes[0] &= 0xf8, txidpriv.bytes[31] &= 0x7f, txidpriv.bytes[31] |= 0x40; + txidpub = curve25519(txidpriv,curve25519_basepoint9()); + + Myprivkey(tmp256.bytes); + vcalc_sha256(0,mypriv.bytes,tmp256.bytes,32); + mypriv.bytes[0] &= 0xf8, mypriv.bytes[31] &= 0x7f, mypriv.bytes[31] |= 0x40; + mypub = curve25519(mypriv,curve25519_basepoint9()); + + ssecret = curve25519_shared(txidpub,mypriv); + ssecret2 = curve25519_shared(mypub,txidpriv); + if ( memcmp(ssecret.bytes,ssecret2.bytes,32) == 0 ) { - vcalc_sha256(0,(uint8_t *)&entropy,ssecret,32); + vcalc_sha256(0,(uint8_t *)&entropy,ssecret.bytes,32); vcalc_sha256(0,(uint8_t *)&hentropy,(uint8_t *)&entropy,32); - } + } else fprintf(stderr,"shared secrets dont match\n"); return(hentropy); }