vid/
lib.rs

1// Copyright (c) 2024 Espresso Systems (espressosys.com)
2// This file is part of the HotShot-VID library.
3
4// You should have received a copy of the MIT License
5// along with the HotShot-VID library. If not, see <https://mit-license.org/>.
6
7//! Verifiable Information Retrieval (VID).
8#![deny(missing_docs)]
9
10use displaydoc::Display;
11use jf_merkle_tree::MerkleTreeError;
12use jf_poseidon2::Poseidon2Error;
13use serde::{Deserialize, Serialize};
14
15pub mod avid_m;
16mod utils;
17
18/// A glorified [`bool`] that leverages compile lints to encourage the caller to
19/// use the result.
20///
21/// Intended as the return type for verification of proofs, signatures, etc.
22/// Recommended for use in the nested [`Result`] pattern: see <https://sled.rs/errors>.
23type VerificationResult = Result<(), ()>;
24
25/// The error type for `VidScheme` methods.
26#[derive(Display, Debug)]
27pub enum VidError {
28    /// invalid args: {0}
29    Argument(String),
30    /// internal error: {0}
31    Internal(anyhow::Error),
32    /// Insufficient shares
33    InsufficientShares,
34    /// Share index out of bound
35    IndexOutOfBound,
36    /// Invalid parameter
37    InvalidParam,
38    /// Invalid VID share
39    InvalidShare,
40}
41
42impl From<Poseidon2Error> for VidError {
43    fn from(err: Poseidon2Error) -> Self {
44        VidError::Internal(err.into())
45    }
46}
47
48impl From<MerkleTreeError> for VidError {
49    fn from(err: MerkleTreeError) -> Self {
50        VidError::Internal(err.into())
51    }
52}
53
54/// Alias
55type VidResult<T> = Result<T, VidError>;
56
57/// Trait definition for a Verifiable Information Dispersal (VID) scheme.
58pub trait VidScheme {
59    /// VID Parameters
60    type Param: Send + Sync + Serialize + for<'a> Deserialize<'a>;
61
62    /// VID Share type
63    type Share: Send + Sync + Serialize + for<'a> Deserialize<'a>;
64
65    /// VID commitment type
66    type Commit: Eq + PartialEq + Send + Sync + Serialize + for<'a> Deserialize<'a>;
67
68    /// Commit to a `payload` without generating shares.
69    fn commit(param: &Self::Param, payload: &[u8]) -> VidResult<Self::Commit>;
70
71    /// Disperse the given `payload` according to the weights in `distribution`.
72    fn disperse(
73        param: &Self::Param,
74        distribution: &[u32],
75        payload: &[u8],
76    ) -> VidResult<(Self::Commit, Vec<Self::Share>)>;
77
78    /// Verify the given VID `share` against the VID `commit`.
79    #[allow(clippy::result_unit_err)]
80    fn verify_share(
81        param: &Self::Param,
82        commit: &Self::Commit,
83        share: &Self::Share,
84    ) -> VidResult<VerificationResult>;
85
86    /// Recover the payload from the given `shares`.
87    fn recover(
88        param: &Self::Param,
89        commit: &Self::Commit,
90        shares: &[Self::Share],
91    ) -> VidResult<Vec<u8>>;
92}