| 159 | } |
| 160 | |
| 161 | pub(super) fn output_permutation(&mut self, input: u64, n: u64) -> Result<Value> { |
| 162 | if n > 2u64.pow(30) { |
| 163 | return Err(runtime_error!("n should be less than 2^30")); |
| 164 | } |
| 165 | // For small n, probability of failure is very low, so we can use `n` as the buffer size. |
| 166 | // For larger n, we start from `BUFFER_SIZE` right away. |
| 167 | let initial_buffer_size = usize::min(BUFFER_SIZE, n as usize); |
| 168 | let mut session = PrfSession::new(input, initial_buffer_size)?; |
| 169 | let mut a: Vec<u64> = (0..n).collect(); |
| 170 | for i in 1..n { |
| 171 | let j = session.generate_u32_in_range(&mut self.aes, i as u32 + 1)?; |
| 172 | a.swap(i as usize, j as usize); |
| 173 | } |
| 174 | Value::from_flattened_array_u64(&a, UINT64) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // Helper struct for `Prf` that produces random values of various types from random bytes that |