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 avidm;
16pub mod avidm_gf2;
17mod utils;
18
19/// A glorified [`bool`] that leverages compile lints to encourage the caller to
20/// use the result.
21///
22/// Intended as the return type for verification of proofs, signatures, etc.
23/// Recommended for use in the nested [`Result`] pattern: see <https://sled.rs/errors>.
24type VerificationResult = Result<(), ()>;
25
26/// The error type for `VidScheme` methods.
27#[derive(Display, Debug)]
28pub enum VidError {
29    /// invalid args: {0}
30    Argument(String),
31    /// internal error: {0}
32    Internal(anyhow::Error),
33    /// Insufficient shares
34    InsufficientShares,
35    /// Share index out of bound
36    IndexOutOfBound,
37    /// Invalid parameter
38    InvalidParam,
39    /// Invalid VID share
40    InvalidShare,
41}
42
43impl From<Poseidon2Error> for VidError {
44    fn from(err: Poseidon2Error) -> Self {
45        VidError::Internal(err.into())
46    }
47}
48
49impl From<MerkleTreeError> for VidError {
50    fn from(err: MerkleTreeError) -> Self {
51        VidError::Internal(err.into())
52    }
53}
54
55impl From<reed_solomon_simd::Error> for VidError {
56    fn from(err: reed_solomon_simd::Error) -> Self {
57        VidError::Internal(err.into())
58    }
59}
60
61/// Alias
62type VidResult<T> = Result<T, VidError>;
63
64/// Trait definition for a Verifiable Information Dispersal (VID) scheme.
65pub trait VidScheme {
66    /// VID Parameters
67    type Param: Send + Sync + Serialize + for<'a> Deserialize<'a>;
68
69    /// VID Share type
70    type Share: Send + Sync + Serialize + for<'a> Deserialize<'a>;
71
72    /// VID commitment type
73    type Commit: Eq + PartialEq + Send + Sync + Serialize + for<'a> Deserialize<'a>;
74
75    /// Commit to a `payload` without generating shares.
76    fn commit(param: &Self::Param, payload: &[u8]) -> VidResult<Self::Commit>;
77
78    /// Disperse the given `payload` according to the weights in `distribution`.
79    fn disperse(
80        param: &Self::Param,
81        distribution: &[u32],
82        payload: &[u8],
83    ) -> VidResult<(Self::Commit, Vec<Self::Share>)>;
84
85    /// Verify the given VID `share` against the VID `commit`.
86    #[allow(clippy::result_unit_err)]
87    fn verify_share(
88        param: &Self::Param,
89        commit: &Self::Commit,
90        share: &Self::Share,
91    ) -> VidResult<VerificationResult>;
92
93    /// Recover the payload from the given `shares`.
94    fn recover(
95        param: &Self::Param,
96        commit: &Self::Commit,
97        shares: &[Self::Share],
98    ) -> VidResult<Vec<u8>>;
99}