Skip to main content

luct_core/
tiling.rs

1mod checkpoint;
2mod data_tile;
3mod tile;
4
5use crate::tree::{ProofGenerationError, ProofValidationError};
6pub use checkpoint::{Checkpoint, ParseCheckpointError};
7pub use data_tile::{DataTile, DataTileId};
8use itertools::Itertools;
9use thiserror::Error;
10pub use tile::{Tile, TileId};
11
12#[derive(Debug, Clone, PartialEq, Eq, Error)]
13pub enum TilingError {
14    #[error("Can not fetch tiles from non tiling log")]
15    NonTilingLog,
16
17    #[error("The tile that was returned by the log is malformed")]
18    MalformedTile,
19
20    #[error("The SCT has no leaf index")]
21    LeafIndexMissing,
22
23    #[error("Failed to generate audit proof: {0}")]
24    AuditProofGenerationError(ProofGenerationError),
25
26    #[error("Failed to generate consistency proof: {0}")]
27    ConsistencyProofGenerationError(ProofGenerationError),
28
29    #[error("Failed to validate a consistency path: {0}")]
30    ConsistencyProofError(ProofValidationError),
31
32    #[error("Failed to validate an audit path: {0}")]
33    AuditProofError(ProofValidationError),
34}
35
36/// Turn an index into a url as specified in the tiling spec, i.e. "1234067" to "x001/x234/067"
37fn index_to_url(idx: u64) -> String {
38    let idx = idx.to_string();
39
40    let leading_zeros = (3 - idx.len() % 3) % 3;
41
42    let num_segments = (idx.len() + leading_zeros) / 3;
43
44    std::iter::repeat_n('0', leading_zeros)
45        .chain(idx.chars())
46        .chunks(3)
47        .into_iter()
48        .map(|chunk| chunk.collect::<String>())
49        .enumerate()
50        .map(|(idx, segment)| {
51            if idx != num_segments - 1 {
52                format!("x{segment}")
53            } else {
54                segment
55            }
56        })
57        .join("/")
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_index_to_url() {
66        // Example from the spec
67        assert_eq!(index_to_url(1234067), "x001/x234/067");
68
69        assert_eq!(index_to_url(0), "000");
70        assert_eq!(index_to_url(1), "001");
71        assert_eq!(index_to_url(1000), "x001/000");
72        assert_eq!(index_to_url(1001), "x001/001");
73
74        assert_eq!(index_to_url(87654321), "x087/x654/321");
75        assert_eq!(index_to_url(987654321), "x987/x654/321");
76        assert_eq!(index_to_url(1987654321), "x001/x987/x654/321");
77    }
78}