Skip to main content

aura_effects/
random.rs

1//! Random effect handlers
2//!
3//! This module provides standard implementations of the `RandomEffects` trait
4//! defined in `aura-core`. These handlers can be used by choreographic applications
5//! and other Aura components.
6//!
7//! Note: This module legitimately uses `rand::thread_rng()` as it implements the
8//! RandomEffects trait - this is the effect handler layer where actual system
9//! randomness is provided.
10
11// Allow disallowed methods in effect handler implementations
12#![allow(clippy::disallowed_methods)]
13
14use async_trait::async_trait;
15use aura_core::effects::random::RandomCoreEffects;
16use rand::RngCore;
17
18/// Real random handler using actual cryptographically secure randomness
19#[derive(Debug, Clone)]
20pub struct RealRandomHandler;
21
22impl Default for RealRandomHandler {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28impl RealRandomHandler {
29    /// Create a new real random handler
30    pub fn new() -> Self {
31        Self
32    }
33}
34
35#[async_trait]
36impl RandomCoreEffects for RealRandomHandler {
37    async fn random_bytes(&self, len: usize) -> Vec<u8> {
38        let mut bytes = vec![0u8; len];
39        rand::thread_rng().fill_bytes(&mut bytes);
40        bytes
41    }
42
43    async fn random_bytes_32(&self) -> [u8; 32] {
44        let mut bytes = [0u8; 32];
45        rand::thread_rng().fill_bytes(&mut bytes);
46        bytes
47    }
48
49    async fn random_u64(&self) -> u64 {
50        use rand::Rng;
51        rand::thread_rng().gen()
52    }
53}