equix/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
2#![doc = include_str!("../README.md")]
3// @@ begin lint list maintained by maint/add_warning @@
4#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6#![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_duration_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)] // This can reasonably be done for explicitness
39#![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43#![allow(clippy::needless_lifetimes)] // See arti#1765
44//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
45
46mod bucket_array;
47mod collision;
48mod err;
49mod solution;
50mod solver;
51
52// Export bucket_array::mem API only to the fuzzer.
53// (This is not stable; you should not use it except for testing.)
54#[cfg(feature = "bucket-array")]
55pub use bucket_array::mem::{BucketArray, BucketArrayMemory, BucketArrayPair, Count, Uninit};
56
57use hashx::{HashX, HashXBuilder};
58
59pub use hashx::{Runtime, RuntimeOption};
60
61pub use err::{Error, HashError};
62pub use solution::{Solution, SolutionArray, SolutionByteArray, SolutionItem, SolutionItemArray};
63pub use solver::SolverMemory;
64
65/// One Equi-X instance, customized for a challenge string
66///
67/// This includes pre-computed state that depends on the
68/// puzzle's challenge as well as any options set via [`EquiXBuilder`].
69#[derive(Debug)]
70pub struct EquiX {
71    /// HashX instance generated for this puzzle's challenge string
72    hash: HashX,
73}
74
75impl EquiX {
76    /// Make a new [`EquiX`] instance with a challenge string and
77    /// default options.
78    ///
79    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
80    /// for a small fraction of challenge values. Those challenges must be
81    /// skipped by solvers and rejected by verifiers.
82    pub fn new(challenge: &[u8]) -> Result<Self, Error> {
83        EquiXBuilder::new().build(challenge)
84    }
85
86    /// Check which actual program runtime is in effect.
87    ///
88    /// By default we try to generate machine code at runtime to accelerate the
89    /// hash function, but we fall back to an interpreter if this fails. The
90    /// compiler can be disabled entirely using [`RuntimeOption::InterpretOnly`]
91    /// and [`EquiXBuilder`].
92    pub fn runtime(&self) -> Runtime {
93        self.hash.runtime()
94    }
95
96    /// Check a [`Solution`] against this particular challenge.
97    ///
98    /// Having a [`Solution`] instance guarantees that the order of items
99    /// has already been checked. This only needs to check hash tree sums.
100    /// Returns either `Ok` or [`Error::HashSum`].
101    pub fn verify(&self, solution: &Solution) -> Result<(), Error> {
102        solution::check_all_tree_sums(&self.hash, solution)
103    }
104
105    /// Search for solutions using this particular challenge.
106    ///
107    /// Returns a buffer with a variable number of solutions.
108    /// Memory for the solver is allocated dynamically and not reused.
109    pub fn solve(&self) -> SolutionArray {
110        let mut mem = SolverMemory::new();
111        self.solve_with_memory(&mut mem)
112    }
113
114    /// Search for solutions, using the provided [`SolverMemory`].
115    ///
116    /// Returns a buffer with a variable number of solutions.
117    ///
118    /// Allows reuse of solver memory. Preferred for callers which may perform
119    /// several solve operations in rapid succession, such as in the common case
120    /// of layering an effort adjustment protocol above Equi-X.
121    pub fn solve_with_memory(&self, mem: &mut SolverMemory) -> SolutionArray {
122        let mut result = Default::default();
123        solver::find_solutions(&self.hash, mem, &mut result);
124        result
125    }
126}
127
128/// Builder for creating [`EquiX`] instances with custom settings
129#[derive(Debug, Clone, Eq, PartialEq)]
130pub struct EquiXBuilder {
131    /// Inner [`HashXBuilder`] for options related to our hash function
132    hash: HashXBuilder,
133}
134
135impl EquiXBuilder {
136    /// Create a new [`EquiXBuilder`] with default settings.
137    ///
138    /// Immediately calling [`Self::build()`] would be equivalent to using
139    /// [`EquiX::new()`].
140    pub fn new() -> Self {
141        Self {
142            hash: HashXBuilder::new(),
143        }
144    }
145
146    /// Select a new [`RuntimeOption`].
147    pub fn runtime(&mut self, runtime: RuntimeOption) -> &mut Self {
148        self.hash.runtime(runtime);
149        self
150    }
151
152    /// Build an [`EquiX`] instance with a challenge string and the
153    /// selected options.
154    ///
155    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
156    /// for a small fraction of challenge values. Those challenges must be
157    /// skipped by solvers and rejected by verifiers.
158    pub fn build(&self, challenge: &[u8]) -> Result<EquiX, Error> {
159        match self.hash.build(challenge) {
160            Err(e) => Err(Error::Hash(e)),
161            Ok(hash) => Ok(EquiX { hash }),
162        }
163    }
164
165    /// Search for solutions to a particular challenge.
166    ///
167    /// Each solve invocation returns zero or more solutions.
168    /// Memory for the solver is allocated dynamically and not reused.
169    ///
170    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
171    /// for a small fraction of challenge values. Those challenges must be
172    /// skipped by solvers and rejected by verifiers.
173    pub fn solve(&self, challenge: &[u8]) -> Result<SolutionArray, Error> {
174        Ok(self.build(challenge)?.solve())
175    }
176
177    /// Check a [`Solution`] against a particular challenge string.
178    ///
179    /// Having a [`Solution`] instance guarantees that the order of items
180    /// has already been checked. This only needs to check hash tree sums.
181    /// Returns either `Ok` or [`Error::HashSum`].
182    pub fn verify(&self, challenge: &[u8], solution: &Solution) -> Result<(), Error> {
183        self.build(challenge)?.verify(solution)
184    }
185
186    /// Check a [`SolutionItemArray`].
187    ///
188    /// Returns an error if the array is not a well formed [`Solution`] or it's
189    /// not suitable for the given challenge.
190    pub fn verify_array(&self, challenge: &[u8], array: &SolutionItemArray) -> Result<(), Error> {
191        // Check Solution validity before we even construct the instance
192        self.verify(challenge, &Solution::try_from_array(array)?)
193    }
194
195    /// Check a [`SolutionByteArray`].
196    ///
197    /// Returns an error if the array is not a well formed [`Solution`] or it's
198    /// not suitable for the given challenge.
199    pub fn verify_bytes(&self, challenge: &[u8], array: &SolutionByteArray) -> Result<(), Error> {
200        self.verify(challenge, &Solution::try_from_bytes(array)?)
201    }
202}
203
204impl Default for EquiXBuilder {
205    fn default() -> Self {
206        Self::new()
207    }
208}
209
210/// Search for solutions, using default [`EquiXBuilder`] options.
211///
212/// Each solve invocation returns zero or more solutions.
213/// Memory for the solver is allocated dynamically and not reused.
214///
215/// It's normal for this to fail with a [`HashError::ProgramConstraints`] for
216/// a small fraction of challenge values. Those challenges must be skipped
217/// by solvers and rejected by verifiers.
218pub fn solve(challenge: &[u8]) -> Result<SolutionArray, Error> {
219    Ok(EquiX::new(challenge)?.solve())
220}
221
222/// Check a [`Solution`] against a particular challenge.
223///
224/// Having a [`Solution`] instance guarantees that the order of items
225/// has already been checked. This only needs to check hash tree sums.
226/// Returns either `Ok` or [`Error::HashSum`].
227///
228/// Uses default [`EquiXBuilder`] options.
229pub fn verify(challenge: &[u8], solution: &Solution) -> Result<(), Error> {
230    EquiX::new(challenge)?.verify(solution)
231}
232
233/// Check a [`SolutionItemArray`].
234///
235/// Returns an error if the array is not a well formed [`Solution`] or it's
236/// not suitable for the given challenge.
237///
238/// Uses default [`EquiXBuilder`] options.
239pub fn verify_array(challenge: &[u8], array: &SolutionItemArray) -> Result<(), Error> {
240    // Check Solution validity before we even construct the instance
241    verify(challenge, &Solution::try_from_array(array)?)
242}
243
244/// Check a [`SolutionByteArray`].
245///
246/// Returns an error if the array is not a well formed [`Solution`] or it's
247/// not suitable for the given challenge.
248///
249/// Uses default [`EquiXBuilder`] options.
250pub fn verify_bytes(challenge: &[u8], array: &SolutionByteArray) -> Result<(), Error> {
251    // Check Solution validity before we even construct the instance
252    verify(challenge, &Solution::try_from_bytes(array)?)
253}