subspace_archiving/
piece_reconstructor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use subspace_core_primitives::hashes::blake3_254_hash_to_scalar;
use subspace_core_primitives::pieces::{Piece, RawRecord};
use subspace_core_primitives::segments::ArchivedHistorySegment;
use subspace_erasure_coding::ErasureCoding;
use subspace_kzg::{Commitment, Kzg, Polynomial, Scalar};

/// Reconstructor-related instantiation error
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum ReconstructorError {
    /// Segment size is not bigger than record size
    #[error("Error during data shards reconstruction: {0}")]
    DataShardsReconstruction(String),

    /// Commitment of input piece is invalid.
    #[error("Commitment of input piece is invalid.")]
    InvalidInputPieceCommitment,

    /// Incorrect piece position provided.
    #[error("Incorrect piece position provided.")]
    IncorrectPiecePosition,
}

/// Reconstructor helps to retrieve blocks from archived pieces.
#[derive(Debug, Clone)]
pub struct PiecesReconstructor {
    /// Erasure coding data structure
    erasure_coding: ErasureCoding,
    /// KZG instance
    kzg: Kzg,
}

impl PiecesReconstructor {
    /// Create a new instance
    pub fn new(kzg: Kzg, erasure_coding: ErasureCoding) -> Self {
        Self {
            erasure_coding,
            kzg,
        }
    }

    /// Returns incomplete pieces (witness missing) and polynomial that can be used to generate
    /// necessary witnesses later.
    fn reconstruct_shards(
        &self,
        input_pieces: &[Option<Piece>],
    ) -> Result<(ArchivedHistorySegment, Polynomial), ReconstructorError> {
        let mut reconstructed_pieces = ArchivedHistorySegment::default();

        // Scratch buffer to avoid re-allocation
        let mut tmp_shards_scalars =
            Vec::<Option<Scalar>>::with_capacity(ArchivedHistorySegment::NUM_PIECES);
        // Iterate over the chunks of `ScalarBytes::SAFE_BYTES` bytes of all records
        for record_offset in 0..RawRecord::NUM_CHUNKS {
            // Collect chunks of each record at the same offset
            for maybe_piece in input_pieces.iter() {
                let maybe_scalar = maybe_piece
                    .as_ref()
                    .map(|piece| {
                        piece
                            .record()
                            .get(record_offset)
                            .expect("Statically guaranteed to exist in a piece; qed")
                    })
                    .map(Scalar::try_from)
                    .transpose()
                    .map_err(ReconstructorError::DataShardsReconstruction)?;

                tmp_shards_scalars.push(maybe_scalar);
            }

            self.erasure_coding
                .recover(&tmp_shards_scalars)
                .map_err(ReconstructorError::DataShardsReconstruction)?
                .into_iter()
                .zip(reconstructed_pieces.iter_mut().map(|piece| {
                    piece
                        .record_mut()
                        .get_mut(record_offset)
                        .expect("Statically guaranteed to exist in a piece; qed")
                }))
                .for_each(|(source_scalar, segment_data)| {
                    segment_data.copy_from_slice(&source_scalar.to_bytes());
                });

            tmp_shards_scalars.clear();
        }

        let source_record_commitments = {
            #[cfg(not(feature = "parallel"))]
            let iter = reconstructed_pieces.iter_mut().zip(input_pieces).step_by(2);
            #[cfg(feature = "parallel")]
            let iter = reconstructed_pieces
                .par_iter_mut()
                .zip_eq(input_pieces)
                .step_by(2);

            iter.map(|(piece, maybe_input_piece)| {
                if let Some(input_piece) = maybe_input_piece {
                    Commitment::try_from_bytes(input_piece.commitment())
                        .map_err(|_error| ReconstructorError::InvalidInputPieceCommitment)
                } else {
                    let scalars = {
                        let mut scalars =
                            Vec::with_capacity(piece.record().len().next_power_of_two());

                        for record_chunk in piece.record().iter() {
                            scalars.push(
                                Scalar::try_from(record_chunk)
                                    .map_err(ReconstructorError::DataShardsReconstruction)?,
                            );
                        }

                        // Number of scalars for KZG must be a power of two elements
                        scalars.resize(scalars.capacity(), Scalar::default());

                        scalars
                    };

                    let polynomial = self.kzg.poly(&scalars).expect(
                        "KZG instance must be configured to support this many scalars; qed",
                    );
                    let commitment = self.kzg.commit(&polynomial).expect(
                        "KZG instance must be configured to support this many scalars; qed",
                    );

                    Ok(commitment)
                }
            })
            .collect::<Result<Vec<_>, _>>()?
        };
        let record_commitments = self
            .erasure_coding
            .extend_commitments(&source_record_commitments)
            .expect(
                "Erasure coding instance is deliberately configured to support this input; qed",
            );
        drop(source_record_commitments);

        let record_commitment_hashes = reconstructed_pieces
            .iter_mut()
            .zip(record_commitments)
            .map(|(reconstructed_piece, commitment)| {
                let commitment_bytes = commitment.to_bytes();
                reconstructed_piece
                    .commitment_mut()
                    .copy_from_slice(&commitment_bytes);
                Scalar::try_from(blake3_254_hash_to_scalar(&commitment_bytes))
                    .expect("Create correctly by dedicated hash function; qed")
            })
            .collect::<Vec<_>>();

        let polynomial = self
            .kzg
            .poly(&record_commitment_hashes)
            .expect("Internally produced values must never fail; qed");

        Ok((reconstructed_pieces, polynomial))
    }

    /// Returns all the pieces for a segment using given set of pieces of a segment of the archived
    /// history (any half of all pieces are required to be present, the rest will be recovered
    /// automatically due to use of erasure coding if needed).
    pub fn reconstruct_segment(
        &self,
        segment_pieces: &[Option<Piece>],
    ) -> Result<ArchivedHistorySegment, ReconstructorError> {
        let (mut pieces, polynomial) = self.reconstruct_shards(segment_pieces)?;

        #[cfg(not(feature = "parallel"))]
        let iter = pieces.iter_mut().enumerate();
        #[cfg(feature = "parallel")]
        let iter = pieces.par_iter_mut().enumerate();

        iter.for_each(|(position, piece)| {
            piece.witness_mut().copy_from_slice(
                &self
                    .kzg
                    .create_witness(
                        &polynomial,
                        ArchivedHistorySegment::NUM_PIECES,
                        position as u32,
                    )
                    .expect("Position is statically known to be valid; qed")
                    .to_bytes(),
            );
        });

        Ok(pieces.to_shared())
    }

    /// Returns the missing piece for a segment using given set of pieces of a segment of the archived
    /// history (any half of all pieces are required to be present).
    pub fn reconstruct_piece(
        &self,
        segment_pieces: &[Option<Piece>],
        piece_position: usize,
    ) -> Result<Piece, ReconstructorError> {
        if piece_position >= ArchivedHistorySegment::NUM_PIECES {
            return Err(ReconstructorError::IncorrectPiecePosition);
        }

        // TODO: Early exit if position already exists and doesn't need reconstruction
        let (reconstructed_records, polynomial) = self.reconstruct_shards(segment_pieces)?;

        let mut piece = Piece::from(&reconstructed_records[piece_position]);

        piece.witness_mut().copy_from_slice(
            &self
                .kzg
                .create_witness(
                    &polynomial,
                    ArchivedHistorySegment::NUM_PIECES,
                    piece_position as u32,
                )
                .expect("Position is verified to be valid above; qed")
                .to_bytes(),
        );

        Ok(piece.to_shared())
    }
}