subspace_core_primitives/
solutions.rs

1//! Solutions-related data structures and functions.
2
3use crate::pieces::{PieceOffset, Record, RecordCommitment, RecordWitness};
4use crate::pos::PosProof;
5use crate::sectors::SectorIndex;
6use crate::segments::{HistorySize, SegmentIndex};
7use crate::{PublicKey, ScalarBytes};
8use core::array::TryFromSliceError;
9use core::fmt;
10use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into};
11use num_traits::WrappingSub;
12use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
13use scale_info::TypeInfo;
14#[cfg(feature = "serde")]
15use serde::{Deserialize, Serialize};
16#[cfg(feature = "serde")]
17use serde::{Deserializer, Serializer};
18#[cfg(feature = "serde")]
19use serde_big_array::BigArray;
20use static_assertions::const_assert;
21
22// TODO: Add related methods to `SolutionRange`.
23/// Type of solution range.
24pub type SolutionRange = u64;
25
26/// Computes the following:
27/// ```text
28/// MAX * slot_probability / chunks * s_buckets / pieces
29/// ```
30pub const fn pieces_to_solution_range(pieces: u64, slot_probability: (u64, u64)) -> SolutionRange {
31    let solution_range = SolutionRange::MAX
32        // Account for slot probability
33        / slot_probability.1 * slot_probability.0
34        // Now take probability of hitting occupied s-bucket in a piece into account
35        / Record::NUM_CHUNKS as u64
36        * Record::NUM_S_BUCKETS as u64;
37
38    // Take number of pieces into account
39    solution_range / pieces
40}
41
42/// Computes the following:
43/// ```text
44/// MAX * slot_probability / chunks * s_buckets / solution_range
45/// ```
46pub const fn solution_range_to_pieces(
47    solution_range: SolutionRange,
48    slot_probability: (u64, u64),
49) -> u64 {
50    let pieces = SolutionRange::MAX
51        // Account for slot probability
52        / slot_probability.1 * slot_probability.0
53        // Now take probability of hitting occupied s-bucket in sector into account
54        / Record::NUM_CHUNKS as u64
55        * Record::NUM_S_BUCKETS as u64;
56
57    // Take solution range into account
58    pieces / solution_range
59}
60
61// Quick test to ensure functions above are the inverse of each other
62const_assert!(solution_range_to_pieces(pieces_to_solution_range(1, (1, 6)), (1, 6)) == 1);
63const_assert!(solution_range_to_pieces(pieces_to_solution_range(3, (1, 6)), (1, 6)) == 3);
64const_assert!(solution_range_to_pieces(pieces_to_solution_range(5, (1, 6)), (1, 6)) == 5);
65
66/// A Ristretto Schnorr signature as bytes produced by `schnorrkel` crate.
67#[derive(
68    Copy,
69    Clone,
70    PartialEq,
71    Eq,
72    Ord,
73    PartialOrd,
74    Hash,
75    Encode,
76    Decode,
77    TypeInfo,
78    Deref,
79    From,
80    Into,
81    DecodeWithMemTracking,
82)]
83pub struct RewardSignature([u8; RewardSignature::SIZE]);
84
85impl fmt::Debug for RewardSignature {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "{}", hex::encode(self.0))
88    }
89}
90
91#[cfg(feature = "serde")]
92#[derive(Serialize, Deserialize)]
93#[serde(transparent)]
94struct RewardSignatureBinary(#[serde(with = "BigArray")] [u8; RewardSignature::SIZE]);
95
96#[cfg(feature = "serde")]
97#[derive(Serialize, Deserialize)]
98#[serde(transparent)]
99struct RewardSignatureHex(#[serde(with = "hex")] [u8; RewardSignature::SIZE]);
100
101#[cfg(feature = "serde")]
102impl Serialize for RewardSignature {
103    #[inline]
104    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105    where
106        S: Serializer,
107    {
108        if serializer.is_human_readable() {
109            RewardSignatureHex(self.0).serialize(serializer)
110        } else {
111            RewardSignatureBinary(self.0).serialize(serializer)
112        }
113    }
114}
115
116#[cfg(feature = "serde")]
117impl<'de> Deserialize<'de> for RewardSignature {
118    #[inline]
119    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
120    where
121        D: Deserializer<'de>,
122    {
123        Ok(Self(if deserializer.is_human_readable() {
124            RewardSignatureHex::deserialize(deserializer)?.0
125        } else {
126            RewardSignatureBinary::deserialize(deserializer)?.0
127        }))
128    }
129}
130
131impl AsRef<[u8]> for RewardSignature {
132    #[inline]
133    fn as_ref(&self) -> &[u8] {
134        &self.0
135    }
136}
137
138impl RewardSignature {
139    /// Reward signature size in bytes
140    pub const SIZE: usize = 64;
141}
142
143/// Witness for chunk contained within a record.
144#[derive(
145    Copy,
146    Clone,
147    Eq,
148    PartialEq,
149    Hash,
150    Deref,
151    DerefMut,
152    From,
153    Into,
154    Encode,
155    Decode,
156    TypeInfo,
157    MaxEncodedLen,
158    DecodeWithMemTracking,
159)]
160#[repr(transparent)]
161pub struct ChunkWitness([u8; ChunkWitness::SIZE]);
162
163impl fmt::Debug for ChunkWitness {
164    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
165        write!(f, "{}", hex::encode(self.0))
166    }
167}
168
169#[cfg(feature = "serde")]
170#[derive(Serialize, Deserialize)]
171#[serde(transparent)]
172struct ChunkWitnessBinary(#[serde(with = "BigArray")] [u8; ChunkWitness::SIZE]);
173
174#[cfg(feature = "serde")]
175#[derive(Serialize, Deserialize)]
176#[serde(transparent)]
177struct ChunkWitnessHex(#[serde(with = "hex")] [u8; ChunkWitness::SIZE]);
178
179#[cfg(feature = "serde")]
180impl Serialize for ChunkWitness {
181    #[inline]
182    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
183    where
184        S: Serializer,
185    {
186        if serializer.is_human_readable() {
187            ChunkWitnessHex(self.0).serialize(serializer)
188        } else {
189            ChunkWitnessBinary(self.0).serialize(serializer)
190        }
191    }
192}
193
194#[cfg(feature = "serde")]
195impl<'de> Deserialize<'de> for ChunkWitness {
196    #[inline]
197    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
198    where
199        D: Deserializer<'de>,
200    {
201        Ok(Self(if deserializer.is_human_readable() {
202            ChunkWitnessHex::deserialize(deserializer)?.0
203        } else {
204            ChunkWitnessBinary::deserialize(deserializer)?.0
205        }))
206    }
207}
208
209impl Default for ChunkWitness {
210    #[inline]
211    fn default() -> Self {
212        Self([0; Self::SIZE])
213    }
214}
215
216impl TryFrom<&[u8]> for ChunkWitness {
217    type Error = TryFromSliceError;
218
219    #[inline]
220    fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
221        <[u8; Self::SIZE]>::try_from(slice).map(Self)
222    }
223}
224
225impl AsRef<[u8]> for ChunkWitness {
226    #[inline]
227    fn as_ref(&self) -> &[u8] {
228        &self.0
229    }
230}
231
232impl AsMut<[u8]> for ChunkWitness {
233    #[inline]
234    fn as_mut(&mut self) -> &mut [u8] {
235        &mut self.0
236    }
237}
238
239impl ChunkWitness {
240    /// Size of chunk witness in bytes.
241    pub const SIZE: usize = 48;
242}
243
244/// Farmer solution for slot challenge.
245#[derive(Clone, Debug, Eq, PartialEq, Encode, Decode, TypeInfo, DecodeWithMemTracking)]
246#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
247#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
248pub struct Solution<RewardAddress> {
249    /// Public key of the farmer that created the solution
250    pub public_key: PublicKey,
251    /// Address for receiving block reward
252    pub reward_address: RewardAddress,
253    /// Index of the sector where solution was found
254    pub sector_index: SectorIndex,
255    /// Size of the blockchain history at time of sector creation
256    pub history_size: HistorySize,
257    /// Pieces offset within sector
258    pub piece_offset: PieceOffset,
259    /// Record commitment that can use used to verify that piece was included in blockchain history
260    pub record_commitment: RecordCommitment,
261    /// Witness for above record commitment
262    pub record_witness: RecordWitness,
263    /// Chunk at above offset
264    pub chunk: ScalarBytes,
265    /// Witness for above chunk
266    pub chunk_witness: ChunkWitness,
267    /// Proof of space for piece offset
268    pub proof_of_space: PosProof,
269}
270
271impl<RewardAddressA> Solution<RewardAddressA> {
272    /// Transform solution with one reward address type into solution with another compatible
273    /// reward address type.
274    pub fn into_reward_address_format<T, RewardAddressB>(self) -> Solution<RewardAddressB>
275    where
276        RewardAddressA: Into<T>,
277        T: Into<RewardAddressB>,
278    {
279        let Solution {
280            public_key,
281            reward_address,
282            sector_index,
283            history_size,
284            piece_offset,
285            record_commitment,
286            record_witness,
287            chunk,
288            chunk_witness,
289            proof_of_space,
290        } = self;
291        Solution {
292            public_key,
293            reward_address: Into::<T>::into(reward_address).into(),
294            sector_index,
295            history_size,
296            piece_offset,
297            record_commitment,
298            record_witness,
299            chunk,
300            chunk_witness,
301            proof_of_space,
302        }
303    }
304}
305
306impl<RewardAddress> Solution<RewardAddress> {
307    /// Dummy solution for the genesis block
308    pub fn genesis_solution(public_key: PublicKey, reward_address: RewardAddress) -> Self {
309        Self {
310            public_key,
311            reward_address,
312            sector_index: 0,
313            history_size: HistorySize::from(SegmentIndex::ZERO),
314            piece_offset: PieceOffset::default(),
315            record_commitment: RecordCommitment::default(),
316            record_witness: RecordWitness::default(),
317            chunk: ScalarBytes::default(),
318            chunk_witness: ChunkWitness::default(),
319            proof_of_space: PosProof::default(),
320        }
321    }
322}
323
324/// Bidirectional distance metric implemented on top of subtraction
325#[inline(always)]
326pub fn bidirectional_distance<T: WrappingSub + Ord>(a: &T, b: &T) -> T {
327    let diff = a.wrapping_sub(b);
328    let diff2 = b.wrapping_sub(a);
329    // Find smaller diff between 2 directions.
330    diff.min(diff2)
331}