vector_commit/lib.rs
1//! `vector-commit` is a collection of traits for use in a vector commitment (VC) scheme.
2//! A vector of data (the dataset) is committed to with a binding property (you cannot change the dataset after commitment).
3//! One can then generate proofs of inclusion for the data to the commitment. These proofs can then
4//! verify that the data was indeed in the dataset committed to by the VC scheme.
5//!
6//! Most VC schemes aim to generate constant or logarithmic sized proofs with efficient verification.
7//! Some VC scheme require a trusted setup in which parameters are generated for proving/verification.
8//! The binding property of these schemes is reliant on no one knowing the secret used in the trusted setup.
9use std::{error::Error, fmt::Debug};
10
11use ark_ff::{PrimeField, Zero};
12use rand::RngCore;
13
14pub mod kzg_amortized;
15
16/// The proving and verification parameters for the VC scheme
17pub trait VCUniversalParams {
18 /// Outputs the maximum number of items that can be committed to with this key
19 fn max_size(&self) -> usize;
20}
21
22/// The dataset that a VC scheme will work over. This should include any preprocessing that is required
23pub trait VCPreparedData {
24 type Item: Clone + Zero;
25 type Error: Debug;
26
27 fn from_vec(data: Vec<Self::Item>) -> Self;
28
29 fn set_evaluation(&mut self, index: usize, value: Self::Item) -> Result<(), Self::Error>;
30
31 fn get(&self, index: usize) -> Option<&Self::Item>;
32
33 fn get_all(&self) -> Vec<(usize, Self::Item)>;
34
35 /// Return the max amount of data that can be stored in this data
36 fn max_size(&self) -> usize;
37}
38
39/// A vector commitment schemes allows committing to a vector of data and generating proofs of inclusion.
40pub trait VectorCommitment {
41 /// The universal parameters for the vector commitment scheme.
42 /// CURRENTLY this API does not support differing committing, proving and verifying keys
43 type UniversalParams: VCUniversalParams;
44
45 /// The vector dataset that has gone through preparation to use with the Vector Commitment.
46 type PreparedData: VCPreparedData;
47
48 /// The Commitment to a vector.
49 type Commitment: PartialEq + Clone;
50
51 /// The proof for a single member of a vector.
52 type Proof;
53
54 /// The proof for multiple members of a vector.
55 type BatchProof;
56
57 /// The error type for the scheme.
58 type Error: Error + Debug;
59
60 /// Constructs the Universal parameters for the scheme, which allows committing
61 /// and proving inclusion of vectors up to `max_items` items
62 fn setup<R: RngCore>(
63 max_items: usize,
64 rng: &mut R,
65 ) -> Result<Self::UniversalParams, Self::Error>;
66
67 /// Commit a prepared data vector (`data`) to the `key` UniversalParams.
68 fn commit(
69 key: &Self::UniversalParams,
70 data: &Self::PreparedData,
71 ) -> Result<Self::Commitment, Self::Error>;
72
73 /// Prove that a piece of data exists inside of `commitment`. The `index` represents the index
74 /// of the data inside of `data`.
75 fn prove(
76 key: &Self::UniversalParams,
77 commitment: &Self::Commitment,
78 index: usize,
79 data: &Self::PreparedData,
80 ) -> Result<Self::Proof, Self::Error>;
81
82 /// Generate all proofs of the dataset using the Feist-Khovratovich techique
83 fn prove_all(
84 key: &Self::UniversalParams,
85 commitment: &Self::Commitment,
86 data: &Self::PreparedData,
87 ) -> Result<Self::BatchProof, Self::Error>;
88
89 /// Verify that the `proof` is valid with respect to the `key` and `commitment`
90 fn verify(
91 key: &Self::UniversalParams,
92 commitment: &Self::Commitment,
93 proof: &Self::Proof,
94 ) -> Result<bool, Self::Error>;
95
96 /// Verify multiple proofs are valid
97 /// TODO: Keep this as boolean return value, or number of valid proofs? Once compression is implemeneted then will be boolean
98 fn verify_batch(
99 key: &Self::UniversalParams,
100 commitment: &Self::Commitment,
101 proof: &Self::BatchProof,
102 ) -> Result<bool, Self::Error>;
103
104 /// Converts a commitment to `PreparedData::Item`
105 fn convert_commitment_to_data(
106 commit: &Self::Commitment,
107 ) -> <Self::PreparedData as VCPreparedData>::Item;
108}