MCPcopy Create free account
hub / github.com/ciphermodelabs/ciphercore / vec_u64_from_bytes

Function vec_u64_from_bytes

ciphercore-base/src/bytes.rs:322–361  ·  view source on GitHub ↗

Can return excess zero elements when ScalarType = BIT and the number of bits in bytes is bigger than the actual number of packed bits

(x: &[u8], st: ScalarType)

Source from the content-addressed store, hash-verified

320/// Can return excess zero elements when ScalarType = BIT and
321/// the number of bits in bytes is bigger than the actual number of packed bits
322pub fn vec_u64_from_bytes(x: &[u8], st: ScalarType) -> Result<Vec<u64>> {
323 let mut x_u64s = vec![];
324 match st {
325 BIT => {
326 for byte in x {
327 for i in 0..8 {
328 let bit = ((byte >> i) & 1) as u64;
329 x_u64s.push(bit);
330 }
331 }
332 }
333 _ => {
334 let byte_length = scalar_size_in_bytes(st) as usize;
335 // Whether to look at the leading bit when padding to 8 bytes.
336 let pad_with_sign_bit = st.is_signed() && byte_length < 8;
337 // E.g. 0xFFFFFFFFFFFF0000 if byte_length == 2.
338 let sign_mask = match pad_with_sign_bit {
339 false => 0,
340 true => u64::MAX ^ ((1 << (byte_length * 8)) - 1),
341 };
342 if x.len() % byte_length != 0 {
343 return Err(runtime_error!("Incompatible vector and scalar type"));
344 }
345 for x_slice in x.chunks_exact(byte_length) {
346 let mut res = 0u64;
347 for (i, xi) in x_slice.iter().enumerate() {
348 res += (*xi as u64) << (i * 8);
349 }
350 if pad_with_sign_bit {
351 let sign_bit = res >> (byte_length * 8 - 1);
352 if sign_bit == 1 {
353 res |= sign_mask;
354 }
355 }
356 x_u64s.push(res);
357 }
358 }
359 }
360 Ok(x_u64s)
361}
362
363pub fn vec_u128_from_bytes(x: &[u8], st: ScalarType) -> Result<Vec<u128>> {
364 let mut x_u128s = vec![];

Callers 5

get_random_in_rangeMethod · 0.85
test_vec_u64_from_bytesFunction · 0.85
test_vec_u128_from_bytesFunction · 0.85

Calls 3

scalar_size_in_bytesFunction · 0.85
is_signedMethod · 0.80
pushMethod · 0.45

Tested by 2

test_vec_u64_from_bytesFunction · 0.68
test_vec_u128_from_bytesFunction · 0.68