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

Change [IU]256 [from,to]_bytes functions to work with Val #893

Merged
merged 1 commit into from
Jun 26, 2023
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
38 changes: 19 additions & 19 deletions soroban-env-common/env.json
Original file line number Diff line number Diff line change
Expand Up @@ -277,31 +277,31 @@
}
],
"return": "U256Object",
"docs": "Convert the four 64-bit words of an u256 (big-endian) to an object containing an u256."
"docs": "Convert the four 64-bit words of a u256 (big-endian) to an object containing a u256."
},
{
"export": "c",
"name": "u256_obj_from_be_bytes",
"name": "u256_val_from_be_bytes",
"args": [
{
"name": "bytes",
"type": "BytesObject"
}
],
"return": "U256Object",
"docs": "Create an U256 object from its representation as a byte array in big endian."
"return": "U256Val",
"docs": "Create a U256 `Val` from its representation as a byte array in big endian."
},
{
"export": "d",
"name": "u256_obj_to_be_bytes",
"name": "u256_val_to_be_bytes",
"args": [
{
"name": "obj",
"type": "U256Object"
"name": "val",
"type": "U256Val"
}
],
"return": "BytesObject",
"docs": "Return the memory representation of this U256 object as a byte array in big endian byte order."
"docs": "Return the memory representation of this U256 `Val` as a byte array in big endian byte order."
},
{
"export": "e",
Expand All @@ -313,7 +313,7 @@
}
],
"return": "u64",
"docs": "Extract the highest 64-bits (bits 192-255) from an object containing an u256."
"docs": "Extract the highest 64-bits (bits 192-255) from an object containing a u256."
},
{
"export": "f",
Expand All @@ -325,7 +325,7 @@
}
],
"return": "u64",
"docs": "Extract bits 128-191 from an object containing an u256."
"docs": "Extract bits 128-191 from an object containing a u256."
},
{
"export": "g",
Expand All @@ -337,7 +337,7 @@
}
],
"return": "u64",
"docs": "Extract bits 64-127 from an object containing an u256."
"docs": "Extract bits 64-127 from an object containing a u256."
},
{
"export": "h",
Expand All @@ -349,7 +349,7 @@
}
],
"return": "u64",
"docs": "Extract the lowest 64-bits (bits 0-63) from an object containing an u256."
"docs": "Extract the lowest 64-bits (bits 0-63) from an object containing a u256."
},
{
"export": "i",
Expand Down Expand Up @@ -377,27 +377,27 @@
},
{
"export": "j",
"name": "i256_obj_from_be_bytes",
"name": "i256_val_from_be_bytes",
"args": [
{
"name": "bytes",
"type": "BytesObject"
}
],
"return": "I256Object",
"docs": "Create an I256 object from its representation as a byte array in big endian."
"return": "I256Val",
"docs": "Create a I256 `Val` from its representation as a byte array in big endian."
},
{
"export": "k",
"name": "i256_obj_to_be_bytes",
"name": "i256_val_to_be_bytes",
"args": [
{
"name": "obj",
"type": "I256Object"
"name": "val",
"type": "I256Val"
}
],
"return": "BytesObject",
"docs": "Return the memory representation of this I256 object as a byte array in big endian byte order."
"docs": "Return the memory representation of this I256 `Val` as a byte array in big endian byte order."
},
{
"export": "l",
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub const ENV_META_V0_SECTION_NAME: &str = "contractenvmetav0";

soroban_env_macros::generate_env_meta_consts!(
ledger_protocol_version: 20,
pre_release_version: 50,
pre_release_version: 51,
);

pub fn get_ledger_protocol_version(interface_version: u64) -> u32 {
Expand Down
20 changes: 10 additions & 10 deletions soroban-env-host/benches/common/cost_types/num_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,23 @@ macro_rules! impl_int256_measure {
let bytes = host
.bytes_new_from_slice(lhs.to_be_bytes().as_slice())
.unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bytes).unwrap();
let lhs_val = host.i256_val_from_be_bytes(bytes).unwrap();
let bytes = host
.bytes_new_from_slice(rhs.to_be_bytes().as_slice())
.unwrap();
let rhs_obj = host.i256_obj_from_be_bytes(bytes).unwrap();
(lhs_obj.into(), rhs_obj.into())
let rhs_val = host.i256_val_from_be_bytes(bytes).unwrap();
(lhs_val, rhs_val)
}

fn new_random_case(host: &Host, rng: &mut StdRng, _input: u64) -> (I256Val, I256Val) {
let mut bytes = [0; 32];
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
let lhs_val = host.i256_val_from_be_bytes(bo).unwrap();
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let rhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
(lhs_obj.into(), rhs_obj.into())
let rhs_val = host.i256_val_from_be_bytes(bo).unwrap();
(lhs_val, rhs_val)
}
}
};
Expand All @@ -75,16 +75,16 @@ macro_rules! impl_int256_measure_rhs_u32 {
let bytes = host
.bytes_new_from_slice(lhs.to_be_bytes().as_slice())
.unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bytes).unwrap();
(lhs_obj.into(), U32Val::from(rhs))
let lhs_val = host.i256_val_from_be_bytes(bytes).unwrap();
(lhs_val, U32Val::from(rhs))
}

fn new_random_case(host: &Host, rng: &mut StdRng, _input: u64) -> (I256Val, U32Val) {
let mut bytes = [0; 32];
rng.fill_bytes(bytes.as_mut_slice());
let bo = host.bytes_new_from_slice(bytes.as_slice()).unwrap();
let lhs_obj = host.i256_obj_from_be_bytes(bo).unwrap();
(lhs_obj.into(), U32Val::from(rng.next_u32()))
let lhs_val = host.i256_val_from_be_bytes(bo).unwrap();
(lhs_val, U32Val::from(rng.next_u32()))
}
}
};
Expand Down
52 changes: 31 additions & 21 deletions soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::{
LedgerEntryData, LedgerKey, LedgerKeyContractCode, PublicKey, ScAddress, ScBytes,
ScErrorType, ScString, ScSymbol, ScVal, TimePoint,
},
AddressObject, Bool, BytesObject, Error, I128Object, I256Object, MapObject, StorageType,
StringObject, SymbolObject, SymbolSmall, SymbolStr, TryFromVal, U128Object, U256Object, U32Val,
U64Val, VecObject, VmCaller, VmCallerEnv, Void, I256, U256,
AddressObject, Bool, BytesObject, ConversionError, Error, I128Object, I256Object, MapObject,
StorageType, StringObject, SymbolObject, SymbolSmall, SymbolStr, TryFromVal, U128Object,
U256Object, U32Val, U64Val, VecObject, VmCaller, VmCallerEnv, Void, I256, U256,
};

use crate::Vm;
Expand Down Expand Up @@ -1214,29 +1214,34 @@ impl VmCallerEnv for Host {
self.add_host_object(u256_from_pieces(hi_hi, hi_lo, lo_hi, lo_lo))
}

fn u256_obj_from_be_bytes(
fn u256_val_from_be_bytes(
&self,
vmcaller: &mut VmCaller<Self::VmUserState>,
bytes: BytesObject,
) -> Result<U256Object, HostError> {
) -> Result<U256Val, HostError> {
let num = self.visit_obj(bytes, move |b: &ScBytes| {
Ok(U256::from_be_bytes(self.fixed_length_bytes_from_slice(
"U256 bytes",
b.as_slice(),
)?))
})?;
self.add_host_object(num)
U256Val::try_from_val(self, &num).map_err(|_| ConversionError.into())
}

fn u256_obj_to_be_bytes(
fn u256_val_to_be_bytes(
&self,
vmcaller: &mut VmCaller<Self::VmUserState>,
obj: U256Object,
val: U256Val,
) -> Result<BytesObject, HostError> {
let scb = self.visit_obj(obj, move |u: &U256| {
self.scbytes_from_slice(&u.to_be_bytes())
})?;
self.add_host_object(scb)
if let Ok(so) = U256Small::try_from(val) {
self.add_host_object(self.scbytes_from_slice(&U256::from(so).to_be_bytes())?)
} else {
let obj = val.try_into()?;
let scb = self.visit_obj(obj, move |u: &U256| {
self.scbytes_from_slice(&u.to_be_bytes())
})?;
self.add_host_object(scb)
}
}

fn obj_to_u256_hi_hi(
Expand Down Expand Up @@ -1294,29 +1299,34 @@ impl VmCallerEnv for Host {
self.add_host_object(i256_from_pieces(hi_hi, hi_lo, lo_hi, lo_lo))
}

fn i256_obj_from_be_bytes(
fn i256_val_from_be_bytes(
&self,
vmcaller: &mut VmCaller<Self::VmUserState>,
bytes: BytesObject,
) -> Result<I256Object, HostError> {
) -> Result<I256Val, HostError> {
let num = self.visit_obj(bytes, move |b: &ScBytes| {
Ok(I256::from_be_bytes(self.fixed_length_bytes_from_slice(
"I256 bytes",
b.as_slice(),
)?))
})?;
self.add_host_object(num)
I256Val::try_from_val(self, &num).map_err(|_| ConversionError.into())
}

fn i256_obj_to_be_bytes(
fn i256_val_to_be_bytes(
&self,
vmcaller: &mut VmCaller<Self::VmUserState>,
obj: I256Object,
val: I256Val,
) -> Result<BytesObject, HostError> {
let scb = self.visit_obj(obj, move |i: &I256| {
self.scbytes_from_slice(&i.to_be_bytes())
})?;
self.add_host_object(scb)
if let Ok(so) = I256Small::try_from(val) {
self.add_host_object(self.scbytes_from_slice(&I256::from(so).to_be_bytes())?)
} else {
let obj = val.try_into()?;
let scb = self.visit_obj(obj, move |i: &I256| {
self.scbytes_from_slice(&i.to_be_bytes())
})?;
self.add_host_object(scb)
}
}

fn obj_to_i256_hi_hi(
Expand Down
8 changes: 4 additions & 4 deletions soroban-env-host/src/test/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ fn test_i256_bytes_roundtrip() -> Result<(), HostError> {
let host = Host::default();
let num = I256::from_words(-4353239472894, 6576786237846);
let bo = host.bytes_new_from_slice(num.to_be_bytes().as_slice())?;
let obj = host.i256_obj_from_be_bytes(bo)?;
let bo_back = host.i256_obj_to_be_bytes(obj)?;
let val = host.i256_val_from_be_bytes(bo)?;
let bo_back = host.i256_val_to_be_bytes(val)?;

let mut buf = [0; 32];
host.bytes_copy_to_slice(bo_back, U32Val::from(0), buf.as_mut_slice())?;
Expand All @@ -408,8 +408,8 @@ fn test_u256_bytes_roundtrip() -> Result<(), HostError> {
let host = Host::default();
let num = U256::from_words(4353239472894, 6576786237846);
let bo = host.bytes_new_from_slice(num.to_be_bytes().as_slice())?;
let obj = host.u256_obj_from_be_bytes(bo)?;
let bo_back = host.u256_obj_to_be_bytes(obj)?;
let val = host.u256_val_from_be_bytes(bo)?;
let bo_back = host.u256_val_to_be_bytes(val)?;

let mut buf = [0; 32];
host.bytes_copy_to_slice(bo_back, U32Val::from(0), buf.as_mut_slice())?;
Expand Down
Binary file modified soroban-test-wasms/wasm-workspace/opt/auth_test_contract.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_f32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_complex.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fannkuch.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fib.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_hostile.wasm
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.