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

Method generate_random_number_const

ciphercore-base/src/random.rs:284–306  ·  view source on GitHub ↗

Generates a random number from 0..2^(8 * NEED_BYTES).

(
        &mut self,
        aes: &mut Crypter,
    )

Source from the content-addressed store, hash-verified

282
283 // Generates a random number from 0..2^(8 * NEED_BYTES).
284 fn generate_random_number_const<const NEED_BYTES: usize>(
285 &mut self,
286 aes: &mut Crypter,
287 ) -> Result<u64> {
288 let mut res = [0u8; 8];
289 // Note: sometimes we copy garbage bytes, but they are discarded later.
290 res.copy_from_slice(&self.buffer[self.next_byte..self.next_byte + 8]);
291
292 let use_bytes = std::cmp::min(self.current_buffer_size - self.next_byte, NEED_BYTES);
293 if use_bytes == NEED_BYTES {
294 self.next_byte += use_bytes;
295 } else {
296 self.generate_one_batch(aes)?;
297 self.next_byte = NEED_BYTES - use_bytes;
298 res[use_bytes..NEED_BYTES].copy_from_slice(&self.buffer[..self.next_byte]);
299 }
300 let mask = if NEED_BYTES == 8 {
301 u64::MAX
302 } else {
303 (1 << (NEED_BYTES * 8)) - 1
304 };
305 Ok(u64::from_le_bytes(res) & mask)
306 }
307
308 // Generates a random number from 0..2^(8 * need_bytes).
309 fn generate_random_number(&mut self, aes: &mut Crypter, need_bytes: usize) -> Result<u64> {

Callers

nothing calls this directly

Calls 1

generate_one_batchMethod · 0.80

Tested by

no test coverage detected