diff --git a/contracts/sui/libs/sui_rlp/sources/decoder.move b/contracts/sui/libs/sui_rlp/sources/decoder.move index 7e794f6b..acd27d51 100644 --- a/contracts/sui/libs/sui_rlp/sources/decoder.move +++ b/contracts/sui/libs/sui_rlp/sources/decoder.move @@ -20,7 +20,6 @@ module sui_rlp::decoder { } else { let length_len = byte - 0xb7; let length_bytes = utils::slice_vector(encoded, 1, length_len as u64); - //debug::print(&length_bytes); let length = utils::from_bytes_u64(&length_bytes); let data_start = (length_len + 1) as u64; utils::slice_vector(encoded, data_start, length) diff --git a/contracts/sui/libs/sui_rlp/sources/encoder.move b/contracts/sui/libs/sui_rlp/sources/encoder.move index 4ea80dbe..ea0dadff 100644 --- a/contracts/sui/libs/sui_rlp/sources/encoder.move +++ b/contracts/sui/libs/sui_rlp/sources/encoder.move @@ -21,14 +21,11 @@ module sui_rlp::encoder { vector::append(&mut result,*bytes); result }; - //std::debug::print(&encoded); encoded } public fun encode_list(list:&vector>,raw:bool):vector{ - //std::debug::print(&b"ENCODELIST".to_string()); - //std::debug::print(list); let mut result=vector::empty(); let mut encoded_list = vector::empty(); let mut list=*list; @@ -40,7 +37,6 @@ module sui_rlp::encoder { vector::append(&mut result,encode(&vector::pop_back(&mut list))); }else{ vector::append(&mut result,vector::pop_back(&mut list)); - //std::debug::print(&result); }; }; @@ -53,21 +49,17 @@ module sui_rlp::encoder { vector::append(&mut encoded_list,result); } else { - let length_bytes = utils::to_bytes_u64(len); - let prefix = (0xf7 + vector::length(&length_bytes)) as u8; - vector::push_back(&mut encoded_list, prefix); - vector::append(&mut encoded_list, length_bytes); - vector::append(&mut encoded_list, result); - - + let length_bytes = utils::to_bytes_u64(len); + let prefix = (0xf7 + vector::length(&length_bytes)) as u8; + vector::push_back(&mut encoded_list, prefix); + vector::append(&mut encoded_list, length_bytes); + vector::append(&mut encoded_list, result); } }else{ vector::push_back(&mut encoded_list,0xc0); }; - //std::debug::print(&b"FINAL_ENCODED_LIST".to_string()); - //std::debug::print(&encoded_list); encoded_list } @@ -77,11 +69,11 @@ module sui_rlp::encoder { let len_u8=(len as u8); vector::push_back(&mut length_info,(offset+len_u8)); }else { - let length_bytes=utils::to_bytes_u64(len); - let length_byte_len=vector::length(&length_bytes); - let length_byte_len=offset+(length_byte_len as u8); - vector::push_back(&mut length_info,length_byte_len); - vector::append(&mut length_info,length_bytes); + let length_bytes=utils::to_bytes_u64(len); + let length_byte_len=vector::length(&length_bytes); + let length_byte_len=offset+(length_byte_len as u8); + vector::push_back(&mut length_info,length_byte_len); + vector::append(&mut length_info,length_bytes); }; length_info } @@ -133,4 +125,6 @@ module sui_rlp::encoder { }; vector[0] } + + } \ No newline at end of file diff --git a/contracts/sui/libs/sui_rlp/sources/utils.move b/contracts/sui/libs/sui_rlp/sources/utils.move index d222da66..e09d9d88 100644 --- a/contracts/sui/libs/sui_rlp/sources/utils.move +++ b/contracts/sui/libs/sui_rlp/sources/utils.move @@ -1,139 +1,99 @@ module sui_rlp::utils { - use std::vector::{Self}; - use std::string::{Self,String}; - use std::bcs; - public fun to_bytes_u32(number: u32): vector { - let mut bytes: vector = vector::empty(); - let mut i:u8=0; - while(i < 4){ - let val =( (number>>(i * 8) & 0xFF) as u8) ; - vector::push_back(&mut bytes,val); - i=i+1; - }; - bytes.reverse(); - bytes - } + use std::bcs; + // Convert bytes to u32 - public fun from_bytes_u32(bytes: &vector): u32 {let mut result = 0; - let mut multiplier = 1; - let length = vector::length(bytes); - - let mut i = length; - while (i > 0) { - i = i - 1; - //std::debug::print(vector::borrow(bytes, i)); - result = result + ((*vector::borrow(bytes, i) as u32) * multiplier); - //std::debug::print(&result); - - if (i > 0) { - multiplier = multiplier * 256 - }; - + public fun from_bytes_u32(bytes: &vector): u32 { + let mut bytes= truncate_zeros(bytes); + bytes.reverse(); + let mut diff= 4-bytes.length(); + while (diff > 0) { + bytes.push_back(0_u8); + diff=diff-1; }; - result + sui::bcs::peel_u32(&mut sui::bcs::new(bytes)) } - fun truncate_zeros(bytes: &vector): vector { - let mut i = 0; - let mut started = false; - let mut result: vector = vector::empty(); - while (i < vector::length(bytes)) { - let val = *vector::borrow(bytes, i) as u8; - if (val > 0 || started) { - started = true; - vector::push_back(&mut result, val); - }; - i = i + 1; + // Convert bytes to u64 + public fun from_bytes_u64(bytes: &vector): u64 { + let mut bytes= truncate_zeros(bytes); + bytes.reverse(); + let mut diff= 8-bytes.length(); + while (diff > 0) { + bytes.push_back(0_u8); + diff=diff-1; }; + sui::bcs::peel_u64(&mut sui::bcs::new(bytes)) - result } - // Convert bytes to u64 - public fun from_bytes_u64(bytes: &vector): u64 { - let bytes = truncate_zeros(bytes); - let mut result = 0; - let mut multiplier = 1; - let length = vector::length(&bytes); - - let mut i = length; - while (i > 0) { - i = i - 1; - //std::debug::print(vector::borrow(bytes, i)); - result = result + ((*vector::borrow(&bytes, i) as u64) * (multiplier)); - //std::debug::print(&result); - if (i > 0) { - multiplier = multiplier * 256 - }; - + // Convert bytes to u128 + public fun from_bytes_u128(bytes: &vector): u128 { + let mut bytes= truncate_zeros(bytes); + bytes.reverse(); + let mut diff= 16-bytes.length(); + while (diff > 0) { + bytes.push_back(0_u8); + diff=diff-1; }; - result + sui::bcs::peel_u128(&mut sui::bcs::new(bytes)) + } public fun to_bytes_u128(number:u128):vector{ - let mut right:u128= 128; - let mut i=1; - while(i < 16){ - right=right << 8; - i=i+1; - - }; - let mut bytes=bcs::to_bytes(&number); - bytes.reverse(); - if (number < right){ - truncate_zeros(&bytes) - }else { - let mut prefix = vector[0]; - prefix.append(truncate_zeros(&bytes)); - prefix - } + let bytes=bcs::to_bytes(&number); + to_signed_bytes(bytes) } + public fun to_bytes_u64(number:u64):vector{ - let mut right:u64= 128; - let mut i=1; - while(i < 8){ - right=right << 8; - i=i+1; + let bytes=bcs::to_bytes(&number); + to_signed_bytes(bytes) + } - }; - let mut bytes=bcs::to_bytes(&number); + public fun to_bytes_u32(number: u32): vector { + let bytes=bcs::to_bytes(&number); + to_signed_bytes(bytes) + } + + fun to_signed_bytes(mut bytes:vector):vector{ bytes.reverse(); - if (number < right){ - truncate_zeros(&bytes) + let truncated=truncate_zeros(&bytes); + let first_byte=*truncated.borrow(0); + + if (first_byte >= 128) { + let mut prefix = vector[0]; + prefix.append(truncated); + prefix + }else { - let mut prefix = vector[0]; - prefix.append(truncate_zeros(&bytes)); - prefix + truncated } + } - // Convert bytes to u128 - public fun from_bytes_u128(bytes: &vector): u128 { - let bytes = truncate_zeros(bytes); - let mut result = 0; - let mut multiplier = 1; - let length = vector::length(&bytes); - - let mut i = length; - while (i > 0) { - i = i - 1; - //std::debug::print(vector::borrow(bytes, i)); - result = result + ((*vector::borrow(&bytes, i) as u128) * multiplier); - //std::debug::print(&result); - - if (i > 0) { - multiplier = multiplier * 256 + fun truncate_zeros(bytes: &vector): vector { + let mut i = 0; + let mut started = false; + let mut result: vector = vector::empty(); + while (i < vector::length(bytes)) { + let val = *vector::borrow(bytes, i) as u8; + if (val > 0 || started) { + started = true; + vector::push_back(&mut result, val); }; - + + i = i + 1; }; + result } + + /* end is exclusive in slice*/ - public fun slice_vector(vec: &vector, start: u64, length: u64): vector { + public fun slice_vector(vec: &vector, start: u64, length: u64): vector { let mut result = vector::empty(); let mut i = 0; while (i < length) { @@ -141,27 +101,23 @@ module sui_rlp::utils { vector::push_back(&mut result, value); i = i + 1; }; - //std::debug::print(&result); result } - + } module sui_rlp::utils_test { use sui_rlp::utils::{Self}; - use std::vector::{Self}; - use std::debug; - use sui::bcs::{Self}; - #[test] + #[test] fun test_u32_conversion() { let num= (122 as u32); let bytes= utils::to_bytes_u32(num); let converted=utils::from_bytes_u32(&bytes); assert!(num==converted,0x01); - + } #[test] @@ -172,7 +128,7 @@ module sui_rlp::utils_test { std::debug::print(&bytes); std::debug::print(&converted); assert!(num==converted,0x01); - + } #[test] @@ -183,7 +139,7 @@ module sui_rlp::utils_test { let converted=utils::from_bytes_u128(&bytes); std::debug::print(&converted); assert!(num==converted,0x01); - + } #[test] @@ -191,16 +147,13 @@ module sui_rlp::utils_test { let data=create_vector(10); let slice= utils::slice_vector(&data,0,3); let expected= create_vector(3); - //debug::print(&expected); - //debug::print(&slice); - //debug::print(&data); assert!(slice==expected,0x01); - + } fun create_vector(n:u8):vector{ - let mut data=vector::empty(); + let mut data=vector::empty(); let mut i=0; while(i < n){ vector::push_back(&mut data,i); diff --git a/contracts/sui/libs/sui_rlp/tests/rlp_tests.move b/contracts/sui/libs/sui_rlp/tests/rlp_tests.move index 2632c074..11ded18d 100644 --- a/contracts/sui/libs/sui_rlp/tests/rlp_tests.move +++ b/contracts/sui/libs/sui_rlp/tests/rlp_tests.move @@ -80,6 +80,80 @@ module sui_rlp::rlp_tests { list } + + #[test] + fun test_encoding_u128(){ + let num:u128=100; + let bytes=x"64"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + //// + /// + let num:u128=200; + let bytes=x"8200c8"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + /// + /// + let num:u128=3000000; + let bytes=x"832dc6c0"; + let encoded= encoder::encode_u128(num); + std::debug::print(&encoded); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=273468273; + let bytes=x"84104ccb71"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=2342312; + let bytes=x"8323bda8"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=1233; + let bytes=x"8204d1"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=412926; + let bytes=x"83064cfe"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=9434628989898; + let bytes=x"860894abb5a3ca"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + let num:u128=92625222222121112; + let bytes=x"88014912261bca8898"; + let encoded= encoder::encode_u128(num); + assert!(encoded==bytes); + let decoded=decoder::decode_u128(&decoder::decode(&encoded)); + assert!(decoded==num); + + + + } +