diff --git a/Cargo.toml b/Cargo.toml index 02173e1b0..97bc6edcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ ipld-dagpb = "0.2.1" itertools = "0.13.0" jsonrpsee = { version = "0.23.2" } log = { version = "0.4.21", default-features = false } +merkletree = "0.23" multihash-codetable = { version = "0.1.1", default-features = false } pairing = "0.23" polkavm = "0.9.3" diff --git a/lib/polka-storage-proofs/Cargo.toml b/lib/polka-storage-proofs/Cargo.toml index 56d8eebc2..c41de57fc 100644 --- a/lib/polka-storage-proofs/Cargo.toml +++ b/lib/polka-storage-proofs/Cargo.toml @@ -15,6 +15,8 @@ pairing = { workspace = true } # Crates are only imported on feature 'std'. bellperson = { workspace = true, optional = true } blstrs = { workspace = true, optional = true } +merkletree = { workspace = true, optional = true } +storage-proofs-core = { workspace = true, optional = true } # Crates are only imported on feature 'substrate'. codec = { workspace = true, features = ["derive"], optional = true } @@ -28,5 +30,5 @@ workspace = true [features] default = ["std"] -std = ["dep:bellperson", "dep:blstrs"] +std = ["dep:bellperson", "dep:blstrs", "dep:merkletree", "dep:storage-proofs-core"] substrate = ["dep:codec", "dep:scale-info"] diff --git a/lib/polka-storage-proofs/src/filecoin/mod.rs b/lib/polka-storage-proofs/src/filecoin/mod.rs new file mode 100644 index 000000000..967b394fe --- /dev/null +++ b/lib/polka-storage-proofs/src/filecoin/mod.rs @@ -0,0 +1,35 @@ +//! This is a basement.... +#![cfg(feature = "std")] + +mod types; + +use std::path::PathBuf; + +use storage_proofs_core::merkle::BinaryMerkleTree; + +pub use crate::filecoin::types::*; + +/// TODO +/// References: +/// - +/// - +pub fn compute_comm_d(sector_size: SectorSize, piece_info: &[PieceInfo]) -> Result { + todo!() +} + +/// TODO +/// References: +/// - +/// - +pub fn compute_comm_r( + graph: &StackedBucketGraph, + num_layers: usize, + data: &[u8], // instead of: mut data: Data<'_>, ??? + data_tree: Option>, // why optional? + // The directory where the files we operate on are stored. + // cache_path: PathBuf, // do we need a cache path in our implementation? + replica_path: PathBuf, // sufficient instead of duplicate location in Data? + label_configs: Labels, +) -> Result { + todo!() +} diff --git a/lib/polka-storage-proofs/src/filecoin/types.rs b/lib/polka-storage-proofs/src/filecoin/types.rs new file mode 100644 index 000000000..4206a6bf3 --- /dev/null +++ b/lib/polka-storage-proofs/src/filecoin/types.rs @@ -0,0 +1,79 @@ +//! Filecoin type definitions to make it no-std compatible. + +use merkletree::store::StoreConfig; + +pub type ProverId = [u8; 32]; +pub type Commitment = [u8; 32]; +pub type Ticket = [u8; 32]; +pub type ReplicaId = Fr; + +#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Eq, Ord)] +pub struct PaddedBytesAmount(pub u64); + +#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Eq, Ord)] +pub struct UnpaddedBytesAmount(pub u64); + +#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct PieceInfo { + pub commitment: Commitment, + pub size: UnpaddedBytesAmount, +} + +impl Debug for PieceInfo { + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { + fmt.debug_struct("PieceInfo") + .field("commitment", &hex::encode(self.commitment)) + .field("size", &self.size) + .finish() + } +} + +impl PieceInfo { + pub fn new(size: UnpaddedBytesAmount) -> Result { + Ok(PieceInfo { commitment: [0u8; 32], size }) + } +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct SectorSize(pub u64); + +impl From for SectorSize { + fn from(size: u64) -> Self { + SectorSize(size) + } +} + +impl From for UnpaddedBytesAmount { + fn from(x: SectorSize) -> Self { + UnpaddedBytesAmount(to_unpadded_bytes(x.0)) + } +} + +impl From for PaddedBytesAmount { + fn from(x: SectorSize) -> Self { + PaddedBytesAmount(x.0) + } +} + +impl From for u64 { + fn from(x: SectorSize) -> Self { + x.0 + } +} + +pub type Labels(Vec); + +// TODO: Already defined in `pallet_proof::graphs::stacked`. +pub type BucketGraphSeed = [u8; 28]; + +pub struct BucketGraph { + base_degree: usize, + seed: BucketGraphSeed, + nodes: usize, +} + +pub struct StackedBucketGraph { + base_graph: BucketGraph, + feistel_keys: [feistel::Index; 4], + feistel_precomputed: feistel::Precomputed, +} diff --git a/lib/polka-storage-proofs/src/lib.rs b/lib/polka-storage-proofs/src/lib.rs index e1e931d24..a0d97ba57 100644 --- a/lib/polka-storage-proofs/src/lib.rs +++ b/lib/polka-storage-proofs/src/lib.rs @@ -3,5 +3,7 @@ #![cfg_attr(not(feature = "std"), no_std)] mod groth16; +mod filecoin; pub use groth16::*; +pub use filecoin::*;