Skip to content

Commit

Permalink
feat(target_chains/starknet): add utils for decoding signed integers,…
Browse files Browse the repository at this point in the history
… move array_felt252_to_bytes31 to utils
  • Loading branch information
Riateche committed Apr 22, 2024
1 parent 67132c0 commit a1e4fc0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
64 changes: 64 additions & 0 deletions target_chains/starknet/contracts/src/util.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,67 @@ pub fn u64_byte_reverse(value: u64) -> u64 {
pub trait UnwrapWithFelt252<T, E> {
fn unwrap_with_felt252(self: Result<T, E>) -> T;
}

/// Reinterpret `u64` as `i64` as if it was a two's complement binary representation.
pub fn u64_as_i64(value: u64) -> i64 {
if value < 0x8000000000000000 {
value.try_into().unwrap()
} else {
let value: i128 = value.into();
(value - 0x10000000000000000).try_into().unwrap()
}
}

/// Reinterpret `u32` as `i32` as if it was a two's complement binary representation.
pub fn u32_as_i32(value: u32) -> i32 {
if value < 0x80000000 {
value.try_into().unwrap()
} else {
let value: i64 = value.into();
(value - 0x100000000).try_into().unwrap()
}
}

pub fn array_felt252_to_bytes31(mut input: Array<felt252>) -> Array<bytes31> {
let mut output = array![];
loop {
match input.pop_front() {
Option::Some(v) => { output.append(v.try_into().unwrap()); },
Option::None => { break; },
}
};
output
}

#[cfg(test)]
mod tests {
use super::{u64_as_i64, u32_as_i32};

#[test]
fn test_u64_as_i64() {
assert!(u64_as_i64(0) == 0);
assert!(u64_as_i64(1) == 1);
assert!(u64_as_i64(2) == 2);
assert!(u64_as_i64(3) == 3);
assert!(u64_as_i64(9223372036854775806) == 9223372036854775806);
assert!(u64_as_i64(9223372036854775807) == 9223372036854775807);
assert!(u64_as_i64(9223372036854775808) == -9223372036854775808);
assert!(u64_as_i64(9223372036854775809) == -9223372036854775807);
assert!(u64_as_i64(18446744073709551614) == -2);
assert!(u64_as_i64(18446744073709551615) == -1);
}

#[test]
fn test_u32_as_i32() {
assert!(u32_as_i32(0) == 0);
assert!(u32_as_i32(1) == 1);
assert!(u32_as_i32(2) == 2);
assert!(u32_as_i32(3) == 3);
assert!(u32_as_i32(2147483646) == 2147483646);
assert!(u32_as_i32(2147483647) == 2147483647);
assert!(u32_as_i32(2147483648) == -2147483648);
assert!(u32_as_i32(2147483649) == -2147483647);
assert!(u32_as_i32(4294967294) == -2);
assert!(u32_as_i32(4294967295) == -1);
}
}
15 changes: 2 additions & 13 deletions target_chains/starknet/contracts/tests/wormhole.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use snforge_std::{declare, ContractClassTrait, start_prank, stop_prank, CheatTarget};
use pyth::wormhole::{IWormholeDispatcher, IWormholeDispatcherTrait, ParseAndVerifyVmError};
use pyth::reader::{ByteArray, ByteArrayImpl, ReaderImpl};
use pyth::util::UnwrapWithFelt252;
use pyth::util::{UnwrapWithFelt252, array_felt252_to_bytes31};
use core::starknet::ContractAddress;
use core::panic_with_felt252;

Expand Down Expand Up @@ -94,17 +94,6 @@ fn test_submit_guardian_set_rejects_empty() {
dispatcher.submit_new_guardian_set(1, array![]).unwrap_with_felt252();
}

fn array_left252_to_bytes31(mut input: Array<felt252>) -> Array<bytes31> {
let mut output = array![];
loop {
match input.pop_front() {
Option::Some(v) => { output.append(v.try_into().unwrap()); },
Option::None => { break; },
}
};
output
}

fn deploy(owner: ContractAddress, guardians: Array<felt252>) -> IWormholeDispatcher {
let mut args = array![];
(owner, guardians).serialize(ref args);
Expand Down Expand Up @@ -300,5 +289,5 @@ fn good_vm1() -> ByteArray {
52685537088250779930155363779405986390839624071318818148325576008719597568,
14615204155786886573933667335033405822686404253588533,
];
ByteArrayImpl::new(array_left252_to_bytes31(bytes), 22)
ByteArrayImpl::new(array_felt252_to_bytes31(bytes), 22)
}

0 comments on commit a1e4fc0

Please sign in to comment.