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

Method recursively_generate_value

ciphercore-base/src/random.rs:257–281  ·  view source on GitHub ↗
(&mut self, aes: &mut Crypter, tp: Type)

Source from the content-addressed store, hash-verified

255 }
256
257 fn recursively_generate_value(&mut self, aes: &mut Crypter, tp: Type) -> Result<Value> {
258 match tp {
259 Type::Scalar(_) | Type::Array(_, _) => {
260 let bit_size = get_size_in_bits(tp)?;
261 let byte_size = (bit_size + 7) / 8;
262 // the last random byte should contain bits_to_flush zeros
263 let bits_to_flush = 8 * byte_size - bit_size;
264 let mut bytes = self.generate_random_bytes(aes, byte_size)?;
265 // Remove unused random bits
266 if !bytes.is_empty() {
267 *bytes.last_mut().unwrap() >>= bits_to_flush;
268 }
269 Ok(Value::from_bytes(bytes))
270 }
271 Type::Tuple(_) | Type::Vector(_, _) | Type::NamedTuple(_) => {
272 let ts = get_types_vector(tp)?;
273 let mut v = vec![];
274 for sub_t in ts {
275 let value = self.recursively_generate_value(aes, (*sub_t).clone())?;
276 v.push(value);
277 }
278 Ok(Value::from_vector(v))
279 }
280 }
281 }
282
283 // Generates a random number from 0..2^(8 * NEED_BYTES).
284 fn generate_random_number_const<const NEED_BYTES: usize>(

Callers 1

output_valueMethod · 0.80

Calls 6

get_size_in_bitsFunction · 0.85
get_types_vectorFunction · 0.85
generate_random_bytesMethod · 0.80
is_emptyMethod · 0.80
cloneMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected