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#![warn(clippy::cognitive_complexity)]
15#![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#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![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)]
47mod compiler;
50mod constraints;
51mod err;
52mod generator;
53mod program;
54mod rand;
55mod register;
56mod scheduler;
57mod siphash;
58
59use crate::compiler::{Architecture, Executable};
60use crate::program::Program;
61use rand_core::RngCore;
62
63pub use crate::err::{CompilerError, Error};
64pub use crate::rand::SipRand;
65pub use crate::siphash::SipState;
66
67#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
69#[non_exhaustive]
70pub enum RuntimeOption {
71 InterpretOnly,
73 CompileOnly,
75 #[default]
78 TryCompile,
79}
80
81#[derive(Debug, Copy, Clone, Eq, PartialEq)]
83#[non_exhaustive]
84pub enum Runtime {
85 Interpret,
87 Compiled,
89}
90
91#[derive(Debug)]
96pub struct HashX {
97 register_key: SipState,
103
104 program: RuntimeProgram,
111}
112
113#[derive(Debug)]
118enum RuntimeProgram {
119 Interpret(Program),
121 Compiled(Executable),
123}
124
125impl HashX {
126 pub const FULL_SIZE: usize = 32;
128
129 pub fn new(seed: &[u8]) -> Result<Self, Error> {
131 HashXBuilder::new().build(seed)
132 }
133
134 pub fn runtime(&self) -> Runtime {
141 match &self.program {
142 RuntimeProgram::Interpret(_) => Runtime::Interpret,
143 RuntimeProgram::Compiled(_) => Runtime::Compiled,
144 }
145 }
146
147 pub fn hash_to_u64(&self, input: u64) -> u64 {
149 self.hash_to_regs(input).digest(self.register_key)[0]
150 }
151
152 pub fn hash_to_bytes(&self, input: u64) -> [u8; Self::FULL_SIZE] {
155 let words = self.hash_to_regs(input).digest(self.register_key);
156 let mut bytes = [0_u8; Self::FULL_SIZE];
157 for word in 0..words.len() {
158 bytes[word * 8..(word + 1) * 8].copy_from_slice(&words[word].to_le_bytes());
159 }
160 bytes
161 }
162
163 #[inline(always)]
165 fn hash_to_regs(&self, input: u64) -> register::RegisterFile {
166 let mut regs = register::RegisterFile::new(self.register_key, input);
167 match &self.program {
168 RuntimeProgram::Interpret(program) => program.interpret(&mut regs),
169 RuntimeProgram::Compiled(executable) => executable.invoke(&mut regs),
170 }
171 regs
172 }
173}
174
175#[derive(Default, Debug, Clone, Eq, PartialEq)]
177pub struct HashXBuilder {
178 runtime: RuntimeOption,
180}
181
182impl HashXBuilder {
183 pub fn new() -> Self {
188 Default::default()
189 }
190
191 pub fn runtime(&mut self, runtime: RuntimeOption) -> &mut Self {
193 self.runtime = runtime;
194 self
195 }
196
197 pub fn build(&self, seed: &[u8]) -> Result<HashX, Error> {
199 let (key0, key1) = SipState::pair_from_seed(seed);
200 let mut rng = SipRand::new(key0);
201 self.build_from_rng(&mut rng, key1)
202 }
203
204 pub fn build_from_rng<R: RngCore>(
207 &self,
208 rng: &mut R,
209 register_key: SipState,
210 ) -> Result<HashX, Error> {
211 let program = Program::generate(rng)?;
212 self.build_from_program(program, register_key)
213 }
214
215 fn build_from_program(&self, program: Program, register_key: SipState) -> Result<HashX, Error> {
222 Ok(HashX {
223 register_key,
224 program: match self.runtime {
225 RuntimeOption::InterpretOnly => RuntimeProgram::Interpret(program),
226 RuntimeOption::CompileOnly => {
227 RuntimeProgram::Compiled(Architecture::compile((&program).into())?)
228 }
229 RuntimeOption::TryCompile => match Architecture::compile((&program).into()) {
230 Ok(exec) => RuntimeProgram::Compiled(exec),
231 Err(_) => RuntimeProgram::Interpret(program),
232 },
233 },
234 })
235 }
236}