def test_TestSumSHA256(): _, b = skycoin.SKY_cipher_RandByte(256) h1 = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(b, h1) assert h1 != skycoin.cipher_SHA256() _, c = skycoin.SKY_cipher_RandByte(256) h2 = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(c, h2) assert h2 != skycoin.cipher_SHA256() assert h2 == freshSumSHA256(c)
def test_TestAddSHA256(): _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(b, h) _, c = skycoin.SKY_cipher_RandByte(64) i = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(c, i) add = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_AddSHA256(h, i, add) assert err == skycoin.SKY_OK assert add != skycoin.cipher_SHA256() assert add != h assert add != i
def RandSHA256(): err, sha = skycoin.SKY_cipher_RandByte(128) assert err == skycoin.SKY_OK sh = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SumSHA256(sha, sh) assert err == skycoin.SKY_OK return sh
def test_TestSHA256Null(): x = skycoin.cipher_SHA256() _, isNull = skycoin.SKY_cipher_SHA256_Null(x) assert isNull _, b = skycoin.SKY_cipher_RandByte(128) skycoin.SKY_cipher_SumSHA256(b, x) _, isNull = skycoin.SKY_cipher_SHA256_Null(x) assert not isNull
def test_TestXorSHA256(): _, b = skycoin.SKY_cipher_RandByte(128) _, c = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() i = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SumSHA256(b, h) assert err == skycoin.SKY_OK err = skycoin.SKY_cipher_SumSHA256(c, i) assert err == skycoin.SKY_OK temp = skycoin.cipher_SHA256() temp2 = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SHA256_Xor(h, i, temp) == skycoin.SKY_OK assert skycoin.SKY_cipher_SHA256_Xor(i, h, temp2) == skycoin.SKY_OK assert temp != h assert temp != i assert temp != skycoin.cipher_SHA256() assert temp == temp2
def test_TestMerkle(): hashlist = [] h = skycoin.cipher_SHA256() for _ in range(5): hashlist.append(h) for i in range(5): err, data = skycoin.SKY_cipher_RandByte(128) assert err == skycoin.SKY_OK err = skycoin.SKY_cipher_SumSHA256(data, hashlist[i]) assert err == skycoin.SKY_OK
def test_TestSecKeyHashTest(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() secret_key_2 = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) sha_sum_1 = skycoin.cipher_SHA256() _, data = skycoin.SKY_cipher_RandByte(256) skycoin.SKY_cipher_SumSHA256(data, sha_sum_1) value = skycoin.skycoin.SKY_cipher_CheckSecKeyHash(secret_key, sha_sum_1) assert value == skycoin.SKY_OK value = skycoin.skycoin.SKY_cipher_CheckSecKeyHash(secret_key_2, sha_sum_1) assert value == skycoin.SKY_ErrInvalidSecKyVerification
def test_TestSignHash(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() addres = skycoin.cipher__Address() sha_sum = skycoin.cipher_SHA256() sig_1 = skycoin.cipher_Sig() sig_2 = skycoin.cipher_Sig() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) skycoin.SKY_cipher_AddressFromPubKey(public_key, addres) _, data = skycoin.SKY_cipher_RandByte(256) skycoin.SKY_cipher_SumSHA256(data, sha_sum) skycoin.SKY_cipher_SignHash(sha_sum, secret_key, sig_1) assert sig_1 != sig_2
def makeUxBodyWithSecret(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() assert skycoin.SKY_cipher_GenerateKeyPair(p, s) == skycoin.SKY_OK uxb = skycoin.coin__UxBody() err, b = skycoin.SKY_cipher_RandByte(128) assert err == skycoin.SKY_OK h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK assert h.assignTo(uxb.SrcTransaction) == None a = skycoin.cipher__Address() assert skycoin.SKY_cipher_AddressFromPubKey(p, a) == skycoin.SKY_OK uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) return uxb, s
def test_TestPubKeyFromSig(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) sha_sum = skycoin.cipher_SHA256() _, data = skycoin.SKY_cipher_RandByte(256) skycoin.SKY_cipher_SumSHA256(data, sha_sum) sig_1 = skycoin.cipher_Sig() skycoin.SKY_cipher_SignHash(sha_sum, secret_key, sig_1) public_key_2 = skycoin.cipher_PubKey() assert skycoin.SKY_cipher_PubKeyFromSig(sig_1, sha_sum, public_key_2) == skycoin.SKY_OK assert public_key == public_key_2 sig_2 = skycoin.cipher_Sig() assert skycoin.SKY_cipher_PubKeyFromSig( sig_2, sha_sum, public_key_2) == skycoin.SKY_ErrInvalidSigPubKeyRecovery
def test_TestSHA256KnownValue(): vals = [] values = struct_test() values.inputs = b"skycoin" values.outputs = b"5a42c0643bdb465d90bf673b99c14f5fa02db71513249d904573d2b8b63d353d" vals.append(values) values.inputs = b"hello world" values.outputs = b"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" vals.append(values) values.inputs = b"hello world asd awd awd awdapodawpokawpod " values.outputs = b"99d71f95cafe05ea2dddebc35b6083bd5af0e44850c9dc5139b4476c99950be4" for v in vals: b = v.inputs h = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SumSHA256(b, h) assert err == skycoin.SKY_OK _, h = skycoin.SKY_cipher_SHA256_Hex(h) assert h == v.outputs
def test_TestSHA256FromHex(): # Invalid hex hash h = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SHA256FromHex(b"cawcad", h) assert err == skycoin.SKY_ERROR # Truncated hex hash _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(b, h) h_bytes = h.toStr() h1 = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SHA256FromHex(h_bytes[:int(len(h_bytes) / 2)], h1) assert err == skycoin.SKY_ERROR # Valid hex hash h2 = skycoin.cipher_SHA256() err, b = skycoin.SKY_cipher_SHA256_Hex(h) err = skycoin.SKY_cipher_SHA256FromHex(b, h2) assert h == h2 assert err == skycoin.SKY_OK
def freshSumSHA256(b): sha256 = skycoin.cipher_SHA256() skycoin.SKY_cipher_SumSHA256(b, sha256) return sha256
def test_TestUxOutSnapshotHash(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p, s) uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) a = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p, a) uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) uxo = skycoin.coin__UxOut() uxh = skycoin.coin__UxHead() uxh.Time = 100 uxh.BkSeq = 2 uxo.Head = uxh uxo.Body = uxb hn = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo, hn) == skycoin.SKY_OK # snapshot hash should be dependent on every field in body and head # Head Time uxo_2 = uxo uxh.Time = 20 uxo_2.Head = uxh hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Head BkSeq uxo_2 = uxo uxh.BkSeq = 4 uxo_2.Head = uxh hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body SetSrcTransaction uxo_2 = uxo uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Address p_2 = skycoin.cipher_PubKey() s_2 = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p_2, s_2) a_2 = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p_2, a_2) uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Address = a_2 uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Coins uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Coins = int(2) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2 # Body Hours uxo_2 = uxo uxb = skycoin.coin__UxBody() uxb.Hours = int(2) uxo_2.Body = uxb hn_2 = skycoin.cipher_SHA256() assert skycoin.SKY_coin_UxOut_SnapshotHash(uxo_2, hn_2) == skycoin.SKY_OK assert hn != hn_2
def test_TestUxOutCoinHours(): p = skycoin.cipher_PubKey() s = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(p, s) uxb = skycoin.coin__UxBody() _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() assert skycoin.SKY_cipher_SumSHA256(b, h) == skycoin.SKY_OK uxb.SetSrcTransaction(h.toStr()) a = skycoin.cipher__Address() skycoin.SKY_cipher_AddressFromPubKey(p, a) uxb.Address = a uxb.Coins = int(1e6) uxb.Hours = int(100) uxo = skycoin.coin__UxOut() uxh = skycoin.coin__UxHead() uxh.Time = 100 uxh.BkSeq = 2 uxo.Head = uxh uxo.Body = uxb # Less than an hour passed now = uxh.Time + 100 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time assert err == skycoin.SKY_OK # 1 hours passed now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time + uxb.Coins / 1000000 assert err == skycoin.SKY_OK # 6 hours passed now = uxh.Time + 3600 * 6 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time + (uxb.Coins / 1000000) * 6 assert err == skycoin.SKY_OK # Time is backwards (treated as no hours passed) now = uxh.Time // 2 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxh.Time assert err == skycoin.SKY_OK # 1 hour has passed, output has 1.5 coins, should gain 1 coinhour uxb.Coins = 1500000 now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # 2 hours have passed, output has 1.5 coins, should gain 3 coin hours uxb.Coins = 1500000 uxo.Body = uxb now = uxh.Time + 3600 * 2 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 3 assert err == skycoin.SKY_OK # 1 second has passed, output has 3600 coins, should gain 1 coin hour uxb.Coins = 3600000000 uxo.Body = uxb now = uxh.Time + 1 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # 1000000 hours minus 1 second have passed, output has 1 droplet, should gain 0 coin hour uxb.Coins = 1 uxo.Body = uxb now = uxh.Time + 1000000 * 3600 - 1 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours assert err == skycoin.SKY_OK # 1000000 hours have passed, output has 1 droplet, should gain 1 coin hour uxb.Coins = 1 uxo.Body = uxb now = uxh.Time + 1000000 * 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1 assert err == skycoin.SKY_OK # No hours passed, using initial coin hours uxb.Coins = 1000000000 uxb.Hours = 1000 * 1000 uxo.Body = uxb now = uxh.Time err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours assert err == skycoin.SKY_OK # One hour passed, using initial coin hours now = uxh.Time + 3600 err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == uxb.Hours + 1000000000 / 1000000 assert err == skycoin.SKY_OK # No hours passed and no hours to begin with0 uxb.Hours = 0 uxo.Body = uxb now = uxh.Time err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert hours == 0 assert err == skycoin.SKY_OK # Centuries have passed, time-based calculation overflows uint64 # when calculating the whole coin seconds uxb.Coins = 2000000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR # Centuries have passed, time-based calculation overflows uint64 # when calculating the droplet seconds uxb.Coins = 1500000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR # Output would overflow if given more hours, has reached its limit uxb.Coins = 3600000000 uxo.Body = uxb now = 0xFFFFFFFFFFFFFFFF err, hours = skycoin.SKY_coin_UxOut_CoinHours(uxo, now) assert err == skycoin.SKY_ERROR