Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sui eth-rlp fixes #404

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contracts/sui/libs/sui_rlp/sources/decoder.move
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 12 additions & 18 deletions contracts/sui/libs/sui_rlp/sources/encoder.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<vector<u8>>,raw:bool):vector<u8>{
//std::debug::print(&b"ENCODELIST".to_string());
//std::debug::print(list);
let mut result=vector::empty();
let mut encoded_list = vector::empty<u8>();
let mut list=*list;
Expand All @@ -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);
};

};
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down Expand Up @@ -133,4 +125,6 @@ module sui_rlp::encoder {
};
vector<u8>[0]
}


}
193 changes: 73 additions & 120 deletions contracts/sui/libs/sui_rlp/sources/utils.move
Original file line number Diff line number Diff line change
@@ -1,167 +1,123 @@
module sui_rlp::utils {
use std::vector::{Self};
use std::string::{Self,String};
use std::bcs;
public fun to_bytes_u32(number: u32): vector<u8> {
let mut bytes: vector<u8> = 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<u8>): 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<u8>): 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<u8>): vector<u8> {
let mut i = 0;
let mut started = false;
let mut result: vector<u8> = 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<u8>): 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<u8>): 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<u8>): 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<u8>{
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<u8>[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<u8>{
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<u8> {
let bytes=bcs::to_bytes(&number);
to_signed_bytes(bytes)
}

fun to_signed_bytes(mut bytes:vector<u8>):vector<u8>{
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<u8>[0];
prefix.append(truncated);
prefix

}else {
let mut prefix = vector<u8>[0];
prefix.append(truncate_zeros(&bytes));
prefix
truncated
}

}

// Convert bytes to u128
public fun from_bytes_u128(bytes: &vector<u8>): 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<u8>): vector<u8> {
let mut i = 0;
let mut started = false;
let mut result: vector<u8> = 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<u8>, start: u64, length: u64): vector<u8> {
public fun slice_vector(vec: &vector<u8>, start: u64, length: u64): vector<u8> {
let mut result = vector::empty<u8>();
let mut i = 0;
while (i < length) {
let value = *vector::borrow(vec, start + i);
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]
Expand All @@ -172,7 +128,7 @@ module sui_rlp::utils_test {
std::debug::print(&bytes);
std::debug::print(&converted);
assert!(num==converted,0x01);

}

#[test]
Expand All @@ -183,24 +139,21 @@ module sui_rlp::utils_test {
let converted=utils::from_bytes_u128(&bytes);
std::debug::print(&converted);
assert!(num==converted,0x01);

}

#[test]
fun test_vector_slice() {
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<u8>{
let mut data=vector::empty();
let mut data=vector::empty();
let mut i=0;
while(i < n){
vector::push_back(&mut data,i);
Expand Down
Loading
Loading