Skip to content

Commit

Permalink
feat: Add a Method to Support Dynamic Element Sizes in Sequence (#44)
Browse files Browse the repository at this point in the history
* feat: add a method to support dynamic element sizes in `Sequence`

Signed-off-by: Tarek <[email protected]>

* update from_raw_parts to use from_raw_parts_with_size

Signed-off-by: Tarek <[email protected]>

---------

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser authored Oct 28, 2024
1 parent d1389ec commit a2bb3c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/sequence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,30 @@ impl<'a> Sequence<'a> {
/// and between `1` and `32` bytes (inclusive) if `is_signed` is `false`.
pub fn from_raw_parts<T>(slice: &'a [T], is_signed: bool) -> Self {
let element_size = core::mem::size_of::<T>();
Self::from_raw_parts_with_size(slice, element_size, is_signed)
}

/// Converts a slice of any type to a Sequence by calling `from_raw_parts` on it.
///
/// The `is_signed` parameter is used to determine whether the data is interpreted as a signed value or not.
/// The `element_size` parameter specifies the size of each element in bytes.
pub fn from_raw_parts_with_size<T>(
slice: &'a [T],
element_size: usize,
is_signed: bool,
) -> Self {
assert!(element_size > 0);
if is_signed {
assert!(element_size > 0);
assert!(element_size <= 16);
} else {
assert!(element_size > 0);
assert!(element_size <= 32);
}
let len = std::mem::size_of_val(slice);
assert_eq!(
len % element_size,
0,
"raw data length should be a multiple of element size"
);
let data_slice = unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const u8, len) };
Sequence {
data_slice,
Expand Down
24 changes: 24 additions & 0 deletions src/sequence/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,30 @@ fn we_can_convert_a_slice_of_scalars_to_a_sequence_with_correct_data() {
);
}

#[test]
fn we_can_convert_a_slice_of_fixed_size_binary_to_a_sequence_with_correct_data() {
let element_size = 4;
let s = [
[0x01u8, 0x02u8, 0x03u8, 0x04u8],
[0x05u8, 0x06u8, 0x07u8, 0x08u8],
[0x09u8, 0x0Au8, 0x0Bu8, 0x0Cu8],
];

let d = Sequence::from_raw_parts_with_size(&s[..], element_size, false);

assert_eq!(d.element_size, element_size);
assert_eq!(d.len(), 3);

assert_eq!(
d.data_slice,
[
0x01u8, 0x02u8, 0x03u8, 0x04u8, 0x05u8, 0x06u8, 0x07u8, 0x08u8, 0x09u8, 0x0Au8, 0x0Bu8,
0x0Cu8
]
);
assert!(!d.is_signed);
}

#[test]
#[cfg(feature = "arkworks")]
fn we_can_convert_a_slice_of_arkworks_bigint_to_the_same_values_as_scalars() {
Expand Down

0 comments on commit a2bb3c4

Please sign in to comment.