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
#![deny(clippy::string_slice)] // See arti#2571
48
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
49

            
50
mod bucket_array;
51
mod collision;
52
mod err;
53
mod solution;
54
mod solver;
55

            
56
// Export bucket_array::mem API only to the fuzzer.
57
// (This is not stable; you should not use it except for testing.)
58
#[cfg(feature = "bucket-array")]
59
pub use bucket_array::mem::{BucketArray, BucketArrayMemory, BucketArrayPair, Count, Uninit};
60

            
61
use hashx::{HashX, HashXBuilder};
62

            
63
pub use hashx::{Runtime, RuntimeOption};
64

            
65
pub use err::{Error, HashError};
66
pub use solution::{Solution, SolutionArray, SolutionByteArray, SolutionItem, SolutionItemArray};
67
pub use solver::SolverMemory;
68

            
69
/// One Equi-X instance, customized for a challenge string
70
///
71
/// This includes pre-computed state that depends on the
72
/// puzzle's challenge as well as any options set via [`EquiXBuilder`].
73
#[derive(Debug)]
74
pub struct EquiX {
75
    /// HashX instance generated for this puzzle's challenge string
76
    hash: HashX,
77
}
78

            
79
impl EquiX {
80
    /// Make a new [`EquiX`] instance with a challenge string and
81
    /// default options.
82
    ///
83
    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
84
    /// for a small fraction of challenge values. Those challenges must be
85
    /// skipped by solvers and rejected by verifiers.
86
1215
    pub fn new(challenge: &[u8]) -> Result<Self, Error> {
87
1215
        EquiXBuilder::new().build(challenge)
88
1215
    }
89

            
90
    /// Check which actual program runtime is in effect.
91
    ///
92
    /// By default we try to generate machine code at runtime to accelerate the
93
    /// hash function, but we fall back to an interpreter if this fails. The
94
    /// compiler can be disabled entirely using [`RuntimeOption::InterpretOnly`]
95
    /// and [`EquiXBuilder`].
96
    pub fn runtime(&self) -> Runtime {
97
        self.hash.runtime()
98
    }
99

            
100
    /// Check a [`Solution`] against this particular challenge.
101
    ///
102
    /// Having a [`Solution`] instance guarantees that the order of items
103
    /// has already been checked. This only needs to check hash tree sums.
104
    /// Returns either `Ok` or [`Error::HashSum`].
105
10314
    pub fn verify(&self, solution: &Solution) -> Result<(), Error> {
106
10314
        solution::check_all_tree_sums(&self.hash, solution)
107
10314
    }
108

            
109
    /// Search for solutions using this particular challenge.
110
    ///
111
    /// Returns a buffer with a variable number of solutions.
112
    /// Memory for the solver is allocated dynamically and not reused.
113
864
    pub fn solve(&self) -> SolutionArray {
114
864
        let mut mem = SolverMemory::new();
115
864
        self.solve_with_memory(&mut mem)
116
864
    }
117

            
118
    /// Search for solutions, using the provided [`SolverMemory`].
119
    ///
120
    /// Returns a buffer with a variable number of solutions.
121
    ///
122
    /// Allows reuse of solver memory. Preferred for callers which may perform
123
    /// several solve operations in rapid succession, such as in the common case
124
    /// of layering an effort adjustment protocol above Equi-X.
125
2025
    pub fn solve_with_memory(&self, mem: &mut SolverMemory) -> SolutionArray {
126
2025
        let mut result = Default::default();
127
2025
        solver::find_solutions(&self.hash, mem, &mut result);
128
2025
        result
129
2025
    }
130
}
131

            
132
/// Builder for creating [`EquiX`] instances with custom settings
133
#[derive(Debug, Clone, Eq, PartialEq)]
134
pub struct EquiXBuilder {
135
    /// Inner [`HashXBuilder`] for options related to our hash function
136
    hash: HashXBuilder,
137
}
138

            
139
impl EquiXBuilder {
140
    /// Create a new [`EquiXBuilder`] with default settings.
141
    ///
142
    /// Immediately calling [`Self::build()`] would be equivalent to using
143
    /// [`EquiX::new()`].
144
2619
    pub fn new() -> Self {
145
2619
        Self {
146
2619
            hash: HashXBuilder::new(),
147
2619
        }
148
2619
    }
149

            
150
    /// Select a new [`RuntimeOption`].
151
    pub fn runtime(&mut self, runtime: RuntimeOption) -> &mut Self {
152
        self.hash.runtime(runtime);
153
        self
154
    }
155

            
156
    /// Build an [`EquiX`] instance with a challenge string and the
157
    /// selected options.
158
    ///
159
    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
160
    /// for a small fraction of challenge values. Those challenges must be
161
    /// skipped by solvers and rejected by verifiers.
162
3024
    pub fn build(&self, challenge: &[u8]) -> Result<EquiX, Error> {
163
3024
        match self.hash.build(challenge) {
164
162
            Err(e) => Err(Error::Hash(e)),
165
2862
            Ok(hash) => Ok(EquiX { hash }),
166
        }
167
3024
    }
168

            
169
    /// Search for solutions to a particular challenge.
170
    ///
171
    /// Each solve invocation returns zero or more solutions.
172
    /// Memory for the solver is allocated dynamically and not reused.
173
    ///
174
    /// It's normal for this to fail with a [`HashError::ProgramConstraints`]
175
    /// for a small fraction of challenge values. Those challenges must be
176
    /// skipped by solvers and rejected by verifiers.
177
    pub fn solve(&self, challenge: &[u8]) -> Result<SolutionArray, Error> {
178
        Ok(self.build(challenge)?.solve())
179
    }
180

            
181
    /// Check a [`Solution`] against a particular challenge string.
182
    ///
183
    /// Having a [`Solution`] instance guarantees that the order of items
184
    /// has already been checked. This only needs to check hash tree sums.
185
    /// Returns either `Ok` or [`Error::HashSum`].
186
648
    pub fn verify(&self, challenge: &[u8], solution: &Solution) -> Result<(), Error> {
187
648
        self.build(challenge)?.verify(solution)
188
648
    }
189

            
190
    /// Check a [`SolutionItemArray`].
191
    ///
192
    /// Returns an error if the array is not a well formed [`Solution`] or it's
193
    /// not suitable for the given challenge.
194
    pub fn verify_array(&self, challenge: &[u8], array: &SolutionItemArray) -> Result<(), Error> {
195
        // Check Solution validity before we even construct the instance
196
        self.verify(challenge, &Solution::try_from_array(array)?)
197
    }
198

            
199
    /// Check a [`SolutionByteArray`].
200
    ///
201
    /// Returns an error if the array is not a well formed [`Solution`] or it's
202
    /// not suitable for the given challenge.
203
    pub fn verify_bytes(&self, challenge: &[u8], array: &SolutionByteArray) -> Result<(), Error> {
204
        self.verify(challenge, &Solution::try_from_bytes(array)?)
205
    }
206
}
207

            
208
impl Default for EquiXBuilder {
209
1404
    fn default() -> Self {
210
1404
        Self::new()
211
1404
    }
212
}
213

            
214
/// Search for solutions, using default [`EquiXBuilder`] options.
215
///
216
/// Each solve invocation returns zero or more solutions.
217
/// Memory for the solver is allocated dynamically and not reused.
218
///
219
/// It's normal for this to fail with a [`HashError::ProgramConstraints`] for
220
/// a small fraction of challenge values. Those challenges must be skipped
221
/// by solvers and rejected by verifiers.
222
pub fn solve(challenge: &[u8]) -> Result<SolutionArray, Error> {
223
    Ok(EquiX::new(challenge)?.solve())
224
}
225

            
226
/// Check a [`Solution`] against a particular challenge.
227
///
228
/// Having a [`Solution`] instance guarantees that the order of items
229
/// has already been checked. This only needs to check hash tree sums.
230
/// Returns either `Ok` or [`Error::HashSum`].
231
///
232
/// Uses default [`EquiXBuilder`] options.
233
81
pub fn verify(challenge: &[u8], solution: &Solution) -> Result<(), Error> {
234
81
    EquiX::new(challenge)?.verify(solution)
235
81
}
236

            
237
/// Check a [`SolutionItemArray`].
238
///
239
/// Returns an error if the array is not a well formed [`Solution`] or it's
240
/// not suitable for the given challenge.
241
///
242
/// Uses default [`EquiXBuilder`] options.
243
108
pub fn verify_array(challenge: &[u8], array: &SolutionItemArray) -> Result<(), Error> {
244
    // Check Solution validity before we even construct the instance
245
108
    verify(challenge, &Solution::try_from_array(array)?)
246
108
}
247

            
248
/// Check a [`SolutionByteArray`].
249
///
250
/// Returns an error if the array is not a well formed [`Solution`] or it's
251
/// not suitable for the given challenge.
252
///
253
/// Uses default [`EquiXBuilder`] options.
254
pub fn verify_bytes(challenge: &[u8], array: &SolutionByteArray) -> Result<(), Error> {
255
    // Check Solution validity before we even construct the instance
256
    verify(challenge, &Solution::try_from_bytes(array)?)
257
}