randomx_rust_wrapper/
flags.rs

1/*
2 * Copyright 2024 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use bitflags::bitflags;
18
19use crate::bindings::flags::*;
20
21bitflags! {
22    /// Flags to configure RandomX behaviour.
23    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
24    pub struct RandomXFlags: u32 {
25        const DEFAULT = randomx_flags_RANDOMX_FLAG_DEFAULT;
26        const LARGE_PAGES = randomx_flags_RANDOMX_FLAG_LARGE_PAGES;
27        const HARD_AES = randomx_flags_RANDOMX_FLAG_HARD_AES;
28        const FULL_MEM = randomx_flags_RANDOMX_FLAG_FULL_MEM;
29        const FLAG_JIT = randomx_flags_RANDOMX_FLAG_JIT;
30        const FLAG_SECURE = randomx_flags_RANDOMX_FLAG_SECURE;
31        const FLAG_ARGON2_SSSE3 = randomx_flags_RANDOMX_FLAG_ARGON2_SSSE3;
32        const FLAG_ARGON2_AVX2 = randomx_flags_RANDOMX_FLAG_ARGON2_AVX2;
33        const FLAG_ARGON2 = randomx_flags_RANDOMX_FLAG_ARGON2;
34    }
35}
36
37impl RandomXFlags {
38    pub fn is_fast_mode(&self) -> bool {
39        self.contains(RandomXFlags::FULL_MEM)
40    }
41
42    pub fn is_light_mode(&self) -> bool {
43        !self.is_fast_mode()
44    }
45
46    pub fn is_large_pages(&self) -> bool {
47        self.contains(RandomXFlags::LARGE_PAGES)
48    }
49
50    /// (from the RandomX doc) Returns the recommended flags to be used.
51    ///
52    /// Does not include:
53    /// * FLAG_LARGE_PAGES
54    /// * FLAG_FULL_MEM
55    /// * FLAG_SECURE
56    ///
57    /// The above flags need to be set manually, if required.
58    pub fn recommended() -> Self {
59        let recommended = unsafe { randomx_get_flags() };
60
61        // this unwrap is safe b/c the randomx_get_flags function will return only
62        // existing flags
63        RandomXFlags::from_bits(recommended).unwrap()
64    }
65
66    pub fn recommended_full_mem() -> Self {
67        let mut recommended = Self::recommended();
68        recommended.insert(RandomXFlags::FULL_MEM);
69
70        recommended
71    }
72}
73
74impl Default for RandomXFlags {
75    fn default() -> RandomXFlags {
76        RandomXFlags::DEFAULT
77    }
78}