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

feat: Add a Method to Support Dynamic Element Sizes in Sequence #44

Merged
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
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);
tareknaser marked this conversation as resolved.
Show resolved Hide resolved
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
Loading