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

Function vec_u128_from_bytes

ciphercore-base/src/bytes.rs:363–402  ·  view source on GitHub ↗
(x: &[u8], st: ScalarType)

Source from the content-addressed store, hash-verified

361}
362
363pub fn vec_u128_from_bytes(x: &[u8], st: ScalarType) -> Result<Vec<u128>> {
364 let mut x_u128s = vec![];
365 match st {
366 BIT => {
367 for byte in x {
368 for i in 0..8 {
369 let bit = ((byte >> i) & 1) as u128;
370 x_u128s.push(bit);
371 }
372 }
373 }
374 _ => {
375 let byte_length = scalar_size_in_bytes(st) as usize;
376 // Whether to look at the leading bit when padding to 8 bytes.
377 let pad_with_sign_bit = st.is_signed() && byte_length < 16;
378 // E.g. 0xFFFFFFFFFFFF0000 if byte_length == 2.
379 let sign_mask = match pad_with_sign_bit {
380 false => 0,
381 true => u128::MAX ^ ((1 << (byte_length * 8)) - 1),
382 };
383 if x.len() % byte_length != 0 {
384 return Err(runtime_error!("Incompatible vector and scalar type"));
385 }
386 for x_slice in x.chunks_exact(byte_length) {
387 let mut res = 0;
388 for (i, xi) in x_slice.iter().enumerate() {
389 res += (*xi as u128) << (i * 8);
390 }
391 if pad_with_sign_bit {
392 let sign_bit = res >> (byte_length * 8 - 1);
393 if sign_bit == 1 {
394 res |= sign_mask;
395 }
396 }
397 x_u128s.push(res);
398 }
399 }
400 }
401 Ok(x_u128s)
402}
403
404#[cfg(test)]
405mod tests {

Callers 7

to_u128Method · 0.85
generalized_subtractFunction · 0.85
generalized_addFunction · 0.85
evaluate_mixed_multiplyFunction · 0.85

Calls 3

scalar_size_in_bytesFunction · 0.85
is_signedMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected