1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] #![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] #![allow(recursion_depth_exceeding_limit)] mod compiler;
53mod constraints;
54mod err;
55mod generator;
56mod program;
57mod rand;
58mod register;
59mod scheduler;
60mod siphash;
61
62use crate::compiler::{Architecture, Executable};
63use crate::program::Program;
64use rand_core::Rng;
65
66pub use crate::err::{CompilerError, Error};
67pub use crate::rand::SipRand;
68pub use crate::siphash::SipState;
69
70#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
72#[non_exhaustive]
73pub enum RuntimeOption {
74 InterpretOnly,
76 CompileOnly,
78 #[default]
81 TryCompile,
82}
83
84#[derive(Debug, Copy, Clone, Eq, PartialEq)]
86#[non_exhaustive]
87pub enum Runtime {
88 Interpret,
90 Compiled,
92}
93
94#[derive(Debug)]
99pub struct HashX {
100 register_key: SipState,
106
107 program: RuntimeProgram,
114}
115
116#[derive(Debug)]
121enum RuntimeProgram {
122 Interpret(Program),
124 Compiled(Executable),
126}
127
128impl HashX {
129 pub const FULL_SIZE: usize = 32;
131
132 pub fn new(seed: &[u8]) -> Result<Self, Error> {
134 HashXBuilder::new().build(seed)
135 }
136
137 pub fn runtime(&self) -> Runtime {
144 match &self.program {
145 RuntimeProgram::Interpret(_) => Runtime::Interpret,
146 RuntimeProgram::Compiled(_) => Runtime::Compiled,
147 }
148 }
149
150 pub fn hash_to_u64(&self, input: u64) -> u64 {
152 self.hash_to_regs(input).digest(self.register_key)[0]
153 }
154
155 pub fn hash_to_bytes(&self, input: u64) -> [u8; Self::FULL_SIZE] {
158 let words = self.hash_to_regs(input).digest(self.register_key);
159 let mut bytes = [0_u8; Self::FULL_SIZE];
160 for word in 0..words.len() {
161 bytes[word * 8..(word + 1) * 8].copy_from_slice(&words[word].to_le_bytes());
162 }
163 bytes
164 }
165
166 #[inline(always)]
168 fn hash_to_regs(&self, input: u64) -> register::RegisterFile {
169 let mut regs = register::RegisterFile::new(self.register_key, input);
170 match &self.program {
171 RuntimeProgram::Interpret(program) => program.interpret(&mut regs),
172 RuntimeProgram::Compiled(executable) => executable.invoke(&mut regs),
173 }
174 regs
175 }
176}
177
178#[derive(Default, Debug, Clone, Eq, PartialEq)]
180pub struct HashXBuilder {
181 runtime: RuntimeOption,
183}
184
185impl HashXBuilder {
186 pub fn new() -> Self {
191 Default::default()
192 }
193
194 pub fn runtime(&mut self, runtime: RuntimeOption) -> &mut Self {
196 self.runtime = runtime;
197 self
198 }
199
200 pub fn build(&self, seed: &[u8]) -> Result<HashX, Error> {
202 let (key0, key1) = SipState::pair_from_seed(seed);
203 let mut rng = SipRand::new(key0);
204 self.build_from_rng(&mut rng, key1)
205 }
206
207 pub fn build_from_rng<R: Rng>(
210 &self,
211 rng: &mut R,
212 register_key: SipState,
213 ) -> Result<HashX, Error> {
214 let program = Program::generate(rng)?;
215 self.build_from_program(program, register_key)
216 }
217
218 fn build_from_program(&self, program: Program, register_key: SipState) -> Result<HashX, Error> {
225 Ok(HashX {
226 register_key,
227 program: match self.runtime {
228 RuntimeOption::InterpretOnly => RuntimeProgram::Interpret(program),
229 RuntimeOption::CompileOnly => {
230 RuntimeProgram::Compiled(Architecture::compile((&program).into())?)
231 }
232 RuntimeOption::TryCompile => match Architecture::compile((&program).into()) {
233 Ok(exec) => RuntimeProgram::Compiled(exec),
234 Err(_) => RuntimeProgram::Interpret(program),
235 },
236 },
237 })
238 }
239}