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 test_TestHashRipemd160(): r160 = skycoin.cipher_Ripemd160() _, b = skycoin.SKY_cipher_RandByte(128) assert skycoin.SKY_cipher_HashRipemd160(b, r160) == skycoin.SKY_OK _, b = skycoin.SKY_cipher_RandByte(160) r = skycoin.cipher_Ripemd160() skycoin.SKY_cipher_HashRipemd160(b, r) assert r != skycoin.cipher_Ripemd160() _, b = skycoin.SKY_cipher_RandByte(256) r2 = skycoin.cipher_Ripemd160() skycoin.SKY_cipher_HashRipemd160(b, r2) assert r2 != skycoin.cipher_Ripemd160() assert r2 == freshSumRipemd160(b)
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_TestPubKeyFromSecKey(): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() skycoin.SKY_cipher_GenerateKeyPair(public_key, secret_key) public_key_2 = skycoin.cipher_PubKey() skycoin.SKY_cipher_PubKeyFromSecKey(secret_key, public_key_2) assert public_key == public_key_2 secret_key_2 = skycoin.cipher_SecKey() assert skycoin.SKY_cipher_PubKeyFromSecKey( secret_key_2, public_key) == skycoin.SKY_ErrPubKeyFromNullSecKey _, data = skycoin.SKY_cipher_RandByte(99) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(31) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey
def test_GenerateDeterministicKeyPairsSeed(): error, seed = skycoin.SKY_cipher_RandByte(32) assert error == 0 secKeys = skycoin.cipher_SecKeys() error, newseed = skycoin.SKY_cipher_GenerateDeterministicKeyPairsSeed(seed, 2, secKeys) assert error == 0 assert secKeys.count == 2
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_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_Sha256XorEncrypt(): encrypt = skycoin.encrypt__Sha256Xor() error, data = skycoin.SKY_cipher_RandByte(32) assert error == 0 assert len( data ) == 32 pwd = "pwd" error, encrypted = skycoin.SKY_encrypt_Sha256Xor_Encrypt( encrypt, data, pwd ) assert error == 0 error, decrypted = skycoin.SKY_encrypt_Sha256Xor_Decrypt( encrypt, encrypted, pwd ) assert error == 0 assert data == decrypted
def test_TestPubKeyVerify(): # Random bytes should not be valid, most of the time failed = False for _ in range(10): public_key = skycoin.cipher_PubKey() _, data = skycoin.SKY_cipher_RandByte(33) skycoin.SKY_cipher_NewPubKey(data, public_key) if skycoin.SKY_cipher_PubKey_Verify(public_key) is not None: failed = True break assert failed is True
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_TestSHA256Hex(): h = skycoin.cipher_SHA256() _, b = skycoin.SKY_cipher_RandByte(32) skycoin.SKY_cipher_SHA256_Set(h, b) _, s = skycoin.SKY_cipher_SHA256_Hex(h) h2 = skycoin.cipher_SHA256() err = skycoin.SKY_cipher_SHA256FromHex(s, h2) assert err == skycoin.SKY_OK assert h == h2 _, s2 = skycoin.SKY_cipher_SHA256_Hex(h2) assert s2 == s
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_TestSecKeyFromHex(): sk = skycoin.cipher_SecKey() # Invalid hex err = skycoin.SKY_cipher_SecKeyFromHex(b"", sk) assert err == skycoin.SKY_ErrInvalidLengthSecKey err = skycoin.SKY_cipher_SecKeyFromHex(b"cascs", sk) assert err == skycoin.SKY_ErrInvalidSecKey # INvalid hex length err, b = skycoin.SKY_cipher_RandByte(32) p = skycoin.cipher_SecKey() err = skycoin.SKY_cipher_NewSecKey(b, p) assert err == skycoin.SKY_OK
def test_TestNewPubKey(): public_key = skycoin.cipher_PubKey() _, data = skycoin.SKY_cipher_RandByte(31) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidLengthPubKey _, data = skycoin.SKY_cipher_RandByte(32) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidLengthPubKey _, data = skycoin.SKY_cipher_RandByte(34) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidLengthPubKey _, data = skycoin.SKY_cipher_RandByte(0) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidLengthPubKey _, data = skycoin.SKY_cipher_RandByte(100) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidLengthPubKey _, data = skycoin.SKY_cipher_RandByte(33) assert skycoin.SKY_cipher_NewPubKey( data, public_key) == skycoin.SKY_ErrInvalidPubKey pubkey = skycoin.cipher_PubKey() seckey = skycoin.cipher_SecKey() err = skycoin.SKY_cipher_GenerateKeyPair(pubkey, seckey) assert err == skycoin.SKY_OK ptemp = pubkey.toStr() pubkey2 = skycoin.cipher_PubKey() err = skycoin.SKY_cipher_NewPubKey(ptemp, pubkey2) assert err == skycoin.SKY_OK assert pubkey == pubkey2
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 test_TestGenerateDeterministicKeyPair(): # deterministic key pairs are useless as is because we can't # generate pair n+1, only pair 0 public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() _, seed = skycoin.SKY_cipher_RandByte(32) skycoin.SKY_cipher_GenerateDeterministicKeyPair(seed, public_key, secret_key) assert skycoin.SKY_cipher_PubKey_Verify(public_key) == skycoin.SKY_OK assert skycoin.SKY_cipher_SecKey_Verify(secret_key) == skycoin.SKY_OK skycoin.SKY_cipher_GenerateDeterministicKeyPair(seed, public_key, secret_key) assert skycoin.SKY_cipher_PubKey_Verify(public_key) == skycoin.SKY_OK assert skycoin.SKY_cipher_SecKey_Verify(secret_key) == skycoin.SKY_OK
def test_encrypt_ScryptChacha20poly1305Encrypt(): encrypt_settings = skycoin.encrypt__ScryptChacha20poly1305() encrypt_settings.N = 2 encrypt_settings.R = 8 encrypt_settings.P = 1 encrypt_settings.KeyLen = 32 error, data = skycoin.SKY_cipher_RandByte(32) assert error == 0 assert len( data ) == 32 error, encrypted = skycoin.SKY_encrypt_ScryptChacha20poly1305_Encrypt(encrypt_settings, data, "password") assert error == 0 error, decrypted = skycoin.SKY_encrypt_ScryptChacha20poly1305_Decrypt(encrypt_settings, encrypted, "password") assert error == 0 assert data == decrypted
def test_TestAddressBulk(): for _ in range(1024): public_key = skycoin.cipher_PubKey() secret_key = skycoin.cipher_SecKey() addres_1 = skycoin.cipher__Address() address_2 = skycoin.cipher__Address() _, data = skycoin.SKY_cipher_RandByte(32) skycoin.SKY_cipher_GenerateDeterministicKeyPair( data, public_key, secret_key) skycoin.SKY_cipher_AddressFromPubKey(public_key, addres_1) err = skycoin.SKY_cipher_Address_Verify(addres_1, public_key) assert err == skycoin.SKY_OK _, addres_str = skycoin.SKY_cipher_Address_String(addres_1) err = skycoin.SKY_cipher_DecodeBase58Address(addres_str, address_2) assert err == skycoin.SKY_OK assert addres_1 == address_2
def test_GenerateDeterministicKeyPairs(): error, seed = skycoin.SKY_cipher_RandByte(32) assert error == 0 secKeys = skycoin.cipher_SecKeys() error = skycoin.SKY_cipher_GenerateDeterministicKeyPairs(seed, 2, secKeys) assert error == 0 length = secKeys.count assert length == 2 secKey = secKeys.getAt(0) address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromSecKey(secKey, address) assert error == 0 secKey = secKeys.getAt(1) address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromSecKey(secKey, address) assert error == 0
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_GenerateKeyPairs(): error, data = skycoin.SKY_cipher_RandByte(32) assert error == 0 pubkey = skycoin.cipher_PubKey() seckey = skycoin.cipher_SecKey() error = skycoin.SKY_cipher_GenerateDeterministicKeyPair(data, pubkey, seckey) assert error == 0 address = skycoin.cipher__Address() error = skycoin.SKY_cipher_AddressFromPubKey(pubkey, address) assert error == 0 error = skycoin.SKY_cipher_Address_Verify(address, pubkey) assert error == 0 error, address_string = skycoin.SKY_cipher_Address_String( address ) assert error == 0 address2 = skycoin.cipher__Address() error = skycoin.SKY_cipher_DecodeBase58Address( address_string, address2 ) assert error == 0 assert address.isEqual(address2)
def test_TestSHA256Set(): h = skycoin.cipher_SHA256() _, b = skycoin.SKY_cipher_RandByte(33) assert skycoin.SKY_cipher_SHA256_Set(h, b) == skycoin.SKY_ErrInvalidLengthSHA256 _, b = skycoin.SKY_cipher_RandByte(100) assert skycoin.SKY_cipher_SHA256_Set(h, b) == skycoin.SKY_ErrInvalidLengthSHA256 _, b = skycoin.SKY_cipher_RandByte(31) assert skycoin.SKY_cipher_SHA256_Set(h, b) == skycoin.SKY_ErrInvalidLengthSHA256 _, b = skycoin.SKY_cipher_RandByte(0) assert skycoin.SKY_cipher_SHA256_Set(h, b) == skycoin.SKY_ErrInvalidLengthSHA256 _, b = skycoin.SKY_cipher_RandByte(32) assert skycoin.SKY_cipher_SHA256_Set(h, b) == skycoin.SKY_OK _, b = skycoin.SKY_cipher_RandByte(32) skycoin.SKY_cipher_SHA256_Set(h, b) assert h.toStr()[:] == b
def test_TestRipemd160Set(): h = skycoin.cipher_Ripemd160() _, b = skycoin.SKY_cipher_RandByte(21) assert skycoin.SKY_cipher_Ripemd160_Set(h, b) == skycoin.SKY_ErrInvalidLengthRipemd160 _, b = skycoin.SKY_cipher_RandByte(100) assert skycoin.SKY_cipher_Ripemd160_Set(h, b) == skycoin.SKY_ErrInvalidLengthRipemd160 _, b = skycoin.SKY_cipher_RandByte(19) assert skycoin.SKY_cipher_Ripemd160_Set(h, b) == skycoin.SKY_ErrInvalidLengthRipemd160 _, b = skycoin.SKY_cipher_RandByte(0) assert skycoin.SKY_cipher_Ripemd160_Set(h, b) == skycoin.SKY_ErrInvalidLengthRipemd160 _, b = skycoin.SKY_cipher_RandByte(20) assert skycoin.SKY_cipher_Ripemd160_Set(h, b) == skycoin.SKY_OK _, b1 = skycoin.SKY_cipher_RandByte(20) skycoin.SKY_cipher_Ripemd160_Set(h, b1) assert h.compareToString(str(b))
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 test_TestMustNewSecKey(): secret_key = skycoin.cipher_SecKey() _, data = skycoin.SKY_cipher_RandByte(31) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(33) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(34) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(0) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(100) assert skycoin.SKY_cipher_NewSecKey( data, secret_key) == skycoin.SKY_ErrInvalidLengthSecKey _, data = skycoin.SKY_cipher_RandByte(32) assert skycoin.SKY_cipher_NewSecKey(data, secret_key) == skycoin.SKY_OK assert secret_key.toStr() == data
def test_TestNewSig(): sig = skycoin.cipher_Sig() _, data = skycoin.SKY_cipher_RandByte(64) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_ErrInvalidLengthSig _, data = skycoin.SKY_cipher_RandByte(66) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_ErrInvalidLengthSig _, data = skycoin.SKY_cipher_RandByte(67) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_ErrInvalidLengthSig _, data = skycoin.SKY_cipher_RandByte(0) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_ErrInvalidLengthSig _, data = skycoin.SKY_cipher_RandByte(100) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_ErrInvalidLengthSig _, data = skycoin.SKY_cipher_RandByte(65) assert skycoin.SKY_cipher_NewSig(data, sig) == skycoin.SKY_OK assert sig.toStr() == data
def test_TestDoubleSHA256(): _, b = skycoin.SKY_cipher_RandByte(128) h = skycoin.cipher_SHA256() skycoin.SKY_cipher_DoubleSHA256(b, h) assert h != skycoin.cipher_SHA256() assert h != freshSumSHA256(b)
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