snarkvm_ledger_puzzle/solution_id/
mod.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16mod bytes;
17mod serialize;
18mod string;
19
20pub use string::SOLUTION_ID_PREFIX;
21
22use console::{account::Address, network::prelude::*};
23use snarkvm_algorithms::crypto_hash::sha256d_to_u64;
24
25use core::marker::PhantomData;
26
27/// The solution ID.
28#[derive(Copy, Clone, Eq, PartialEq, Hash)]
29pub struct SolutionID<N: Network>(u64, PhantomData<N>);
30
31impl<N: Network> From<u64> for SolutionID<N> {
32    /// Initializes a new instance of the solution ID.
33    fn from(nonce: u64) -> Self {
34        Self(nonce, PhantomData)
35    }
36}
37
38impl<N: Network> SolutionID<N> {
39    /// Initializes the solution ID from the given epoch hash, address, and counter.
40    pub fn new(epoch_hash: N::BlockHash, address: Address<N>, counter: u64) -> Result<Self> {
41        // Construct the nonce as sha256d(epoch_hash_bytes_le[0..8] || address || counter).
42        let mut bytes_le = Vec::new();
43        let lower_bytes = &epoch_hash.to_bytes_le()?[0..8];
44        bytes_le.extend_from_slice(lower_bytes);
45        bytes_le.extend_from_slice(&address.to_bytes_le()?);
46        bytes_le.extend_from_slice(&counter.to_bytes_le()?);
47        Ok(Self::from(sha256d_to_u64(&bytes_le)))
48    }
49}
50
51impl<N: Network> Deref for SolutionID<N> {
52    type Target = u64;
53
54    fn deref(&self) -> &Self::Target {
55        &self.0
56    }
57}