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