hotshot_types/vid/
advz.rs1use std::{fmt::Debug, ops::Range};
10
11use ark_bn254::Bn254;
12use jf_pcs::{
13 prelude::{UnivariateKzgPCS, UnivariateUniversalParams},
14 PolynomialCommitmentScheme,
15};
16use jf_vid::{
17 advz::{
18 self,
19 payload_prover::{LargeRangeProof, SmallRangeProof},
20 },
21 payload_prover::{PayloadProver, Statement},
22 VidDisperse, VidResult, VidScheme,
23};
24use lazy_static::lazy_static;
25use serde::{Deserialize, Serialize};
26use sha2::Sha256;
27
28use crate::constants::SRS_DEGREE;
29
30#[must_use]
49#[memoize::memoize(SharedCache, Capacity: 10)]
50pub fn advz_scheme(num_storage_nodes: usize) -> ADVZScheme {
51 let recovery_threshold = 1 << num_storage_nodes.ilog2();
55
56 #[allow(clippy::panic)]
57 let num_storage_nodes = u32::try_from(num_storage_nodes).unwrap_or_else(|err| {
58 panic!(
59 "num_storage_nodes {num_storage_nodes} should fit into u32; \
60 error: {err}"
61 )
62 });
63
64 #[allow(clippy::panic)]
66 ADVZScheme(
67 Advz::new(num_storage_nodes, recovery_threshold, &*KZG_SRS).unwrap_or_else(|err| {
68 panic!("advz construction failure: (num_storage nodes,recovery_threshold)=({num_storage_nodes},{recovery_threshold}); \
69 error: {err}")
70 })
71 )
72}
73
74pub type ADVZCommitment = <ADVZScheme as VidScheme>::Commit;
76pub type ADVZCommon = <ADVZScheme as VidScheme>::Common;
78pub type ADVZShare = <ADVZScheme as VidScheme>::Share;
80
81#[cfg(not(feature = "gpu-vid"))]
82type Advz = advz::Advz<E, H>;
84#[cfg(feature = "gpu-vid")]
85type Advz = advz::AdvzGPU<'static, E, H>;
87
88#[derive(Clone)]
91pub struct ADVZScheme(Advz);
92
93#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
97pub struct LargeRangeProofType(
98 LargeRangeProof<<UnivariateKzgPCS<E> as PolynomialCommitmentScheme>::Evaluation>,
110);
111
112#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
116pub struct SmallRangeProofType(
117 SmallRangeProof<<UnivariateKzgPCS<E> as PolynomialCommitmentScheme>::Proof>,
121);
122
123lazy_static! {
125 static ref KZG_SRS: UnivariateUniversalParams<E> = {
127 let srs = ark_srs::kzg10::aztec20::setup(SRS_DEGREE)
128 .expect("Aztec SRS failed to load");
129 UnivariateUniversalParams {
130 powers_of_g: srs.powers_of_g,
131 h: srs.h,
132 beta_h: srs.beta_h,
133 powers_of_h: vec![srs.h, srs.beta_h],
134 }
135 };
136}
137
138type E = Bn254;
140type H = Sha256;
142
143impl VidScheme for ADVZScheme {
149 type Commit = <Advz as VidScheme>::Commit;
150 type Share = <Advz as VidScheme>::Share;
151 type Common = <Advz as VidScheme>::Common;
152
153 fn commit_only<B>(&mut self, payload: B) -> VidResult<Self::Commit>
154 where
155 B: AsRef<[u8]>,
156 {
157 self.0.commit_only(payload)
158 }
159
160 fn disperse<B>(&mut self, payload: B) -> VidResult<VidDisperse<Self>>
161 where
162 B: AsRef<[u8]>,
163 {
164 self.0.disperse(payload).map(vid_disperse_conversion)
165 }
166
167 fn verify_share(
168 &self,
169 share: &Self::Share,
170 common: &Self::Common,
171 commit: &Self::Commit,
172 ) -> VidResult<Result<(), ()>> {
173 self.0.verify_share(share, common, commit)
174 }
175
176 fn recover_payload(&self, shares: &[Self::Share], common: &Self::Common) -> VidResult<Vec<u8>> {
177 self.0.recover_payload(shares, common)
178 }
179
180 fn is_consistent(commit: &Self::Commit, common: &Self::Common) -> VidResult<()> {
181 <Advz as VidScheme>::is_consistent(commit, common)
182 }
183
184 fn get_payload_byte_len(common: &Self::Common) -> u32 {
185 <Advz as VidScheme>::get_payload_byte_len(common)
186 }
187
188 fn get_num_storage_nodes(common: &Self::Common) -> u32 {
189 <Advz as VidScheme>::get_num_storage_nodes(common)
190 }
191
192 fn get_multiplicity(common: &Self::Common) -> u32 {
193 <Advz as VidScheme>::get_multiplicity(common)
194 }
195}
196
197impl PayloadProver<LargeRangeProofType> for ADVZScheme {
198 fn payload_proof<B>(&self, payload: B, range: Range<usize>) -> VidResult<LargeRangeProofType>
199 where
200 B: AsRef<[u8]>,
201 {
202 self.0
203 .payload_proof(payload, range)
204 .map(LargeRangeProofType)
205 }
206
207 fn payload_verify(
208 &self,
209 stmt: Statement<'_, Self>,
210 proof: &LargeRangeProofType,
211 ) -> VidResult<Result<(), ()>> {
212 self.0.payload_verify(stmt_conversion(stmt), &proof.0)
213 }
214}
215
216impl PayloadProver<SmallRangeProofType> for ADVZScheme {
217 fn payload_proof<B>(&self, payload: B, range: Range<usize>) -> VidResult<SmallRangeProofType>
218 where
219 B: AsRef<[u8]>,
220 {
221 self.0
222 .payload_proof(payload, range)
223 .map(SmallRangeProofType)
224 }
225
226 fn payload_verify(
227 &self,
228 stmt: Statement<'_, Self>,
229 proof: &SmallRangeProofType,
230 ) -> VidResult<Result<(), ()>> {
231 self.0.payload_verify(stmt_conversion(stmt), &proof.0)
232 }
233}
234
235fn vid_disperse_conversion(vid_disperse: VidDisperse<Advz>) -> VidDisperse<ADVZScheme> {
244 VidDisperse {
245 shares: vid_disperse.shares,
246 common: vid_disperse.common,
247 commit: vid_disperse.commit,
248 }
249}
250
251fn stmt_conversion(stmt: Statement<'_, ADVZScheme>) -> Statement<'_, Advz> {
253 Statement {
254 payload_subslice: stmt.payload_subslice,
255 range: stmt.range,
256 commit: stmt.commit,
257 common: stmt.common,
258 }
259}