From 8625607521129fbcc43a4e89f99dd27a81d032b0 Mon Sep 17 00:00:00 2001 From: teor Date: Thu, 11 Jan 2024 09:14:19 +1000 Subject: [PATCH] Check returned solutions are unique --- components/equihash/src/tromp.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/components/equihash/src/tromp.rs b/components/equihash/src/tromp.rs index 42f0eb09d8..fac2d895f3 100644 --- a/components/equihash/src/tromp.rs +++ b/components/equihash/src/tromp.rs @@ -37,7 +37,7 @@ extern "C" { } /// Performs a single equihash solver run with equihash parameters `p` and hash state `curr_state`. -/// Returns zero or more solutions. +/// Returns zero or more unique solutions. /// /// # SAFETY /// @@ -95,12 +95,16 @@ unsafe fn worker(eq: *mut CEqui, p: verify::Params, curr_state: &State) -> Vec>(); assert_eq!(chunks.remainder().len(), 0); + // Sometimes the solver returns identical solutions. + solutions.sort(); + solutions.dedup(); + solutions }; @@ -122,7 +126,7 @@ unsafe fn worker(eq: *mut CEqui, p: verify::Params, curr_state: &State) -> Vec( input: &[u8], mut next_nonce: impl FnMut() -> Option<[u8; N]>, @@ -182,7 +186,7 @@ pub fn solve_200_9( /// the supplied partial `input`. Between each run, generates a new nonce of length `N` using the /// `next_nonce` function. /// -/// Returns zero or more compressed solutions. +/// Returns zero or more unique compressed solutions. pub fn solve_200_9_compressed( input: &[u8], next_nonce: impl FnMut() -> Option<[u8; N]>, @@ -191,10 +195,16 @@ pub fn solve_200_9_compressed( const DIGIT_BITS: usize = 200 / (9 + 1); let solutions = solve_200_9(input, next_nonce); - solutions + let mut solutions: Vec> = solutions .iter() .map(|solution| get_minimal_from_indices(solution, DIGIT_BITS)) - .collect() + .collect(); + + // Just in case the solver returns solutions that become the same when compressed. + solutions.sort(); + solutions.dedup(); + + solutions } // Rough translation of GetMinimalFromIndices() from: