Skip to content

Commit 967f398

Browse files
authored
Merge pull request #436 from filecoin-project/feat/post-challenge-generation
Post challenge generation
2 parents 3d8d35b + 23a1156 commit 967f398

File tree

14 files changed

+966
-766
lines changed

14 files changed

+966
-766
lines changed

storage-proofs/src/beacon_post.rs

Lines changed: 36 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ use serde::de::Deserialize;
66
use serde::ser::Serialize;
77

88
use crate::error::{Error, Result};
9-
use crate::hasher::{Domain, HashFunction, Hasher};
10-
use crate::hvh_post;
9+
use crate::hasher::{Domain, Hasher};
1110
use crate::merkle::MerkleTree;
1211
use crate::parameter_cache::ParameterSetIdentifier;
1312
use crate::proof::ProofScheme;
1413
use crate::vdf::Vdf;
14+
use crate::vdf_post;
1515

1616
#[derive(Clone, Debug)]
1717
pub struct SetupParams<T: Domain, V: Vdf<T>> {
18-
pub setup_params_hvh_post: hvh_post::SetupParams<T, V>,
18+
pub vdf_post_setup_params: vdf_post::SetupParams<T, V>,
1919
pub post_periods_count: usize,
2020
}
2121

2222
#[derive(Clone, Debug)]
2323
pub struct PublicParams<T: Domain, V: Vdf<T>> {
24-
pub pub_params_hvh_post: hvh_post::PublicParams<T, V>,
24+
pub vdf_post_pub_params: vdf_post::PublicParams<T, V>,
2525
pub post_periods_count: usize,
2626
}
2727

2828
impl<T: Domain, V: Vdf<T>> ParameterSetIdentifier for PublicParams<T, V> {
2929
fn parameter_set_identifier(&self) -> String {
3030
format!(
31-
"beacon_post::PublicParams{{pub_params_hvh_post: {}, post_periods_count: {}",
32-
self.pub_params_hvh_post.parameter_set_identifier(),
31+
"beacon_post::PublicParams{{vdf_post_pub_params: {}, post_periods_count: {}",
32+
self.vdf_post_pub_params.parameter_set_identifier(),
3333
self.post_periods_count
3434
)
3535
}
@@ -67,14 +67,14 @@ impl<'a, H: 'a + Hasher> PrivateInputs<'a, H> {
6767
#[derive(Clone, Debug, Serialize, Deserialize)]
6868
pub struct Proof<'a, H: Hasher + 'a, V: Vdf<H::Domain>>(
6969
#[serde(bound(
70-
serialize = "hvh_post::Proof<'a, H, V>: Serialize",
71-
deserialize = "hvh_post::Proof<'a, H, V>: Deserialize<'de>"
70+
serialize = "vdf_post::Proof<'a, H, V>: Serialize",
71+
deserialize = "vdf_post::Proof<'a, H, V>: Deserialize<'de>"
7272
))]
73-
Vec<hvh_post::Proof<'a, H, V>>,
73+
Vec<vdf_post::Proof<'a, H, V>>,
7474
);
7575

7676
impl<'a, H: Hasher + 'a, V: Vdf<H::Domain>> Proof<'a, H, V> {
77-
pub fn proofs(&self) -> &[hvh_post::Proof<'a, H, V>] {
77+
pub fn proofs(&self) -> &[vdf_post::Proof<'a, H, V>] {
7878
&self.0
7979
}
8080
}
@@ -86,10 +86,13 @@ pub struct BeaconPoSt<H: Hasher, V: Vdf<H::Domain>> {
8686
}
8787

8888
#[derive(Clone, Debug, Default)]
89-
struct Beacon {
89+
pub struct Beacon {
9090
count: usize,
9191
}
9292

93+
// TODO: We should make Beacon a trait and parameterize BeaconPoSt on that trait.
94+
// This will allow for multiple Beacon implementations, particularly for tests.
95+
// `Beacon::get(…)` should never block for values of `t` which are in the past.
9396
impl Beacon {
9497
pub fn get<T: Domain>(&mut self, t: usize) -> T {
9598
// TODO: actual beacon
@@ -118,7 +121,7 @@ where
118121

119122
fn setup(sp: &SetupParams<H::Domain, V>) -> Result<PublicParams<H::Domain, V>> {
120123
Ok(PublicParams {
121-
pub_params_hvh_post: hvh_post::HvhPost::<H, V>::setup(&sp.setup_params_hvh_post)?,
124+
vdf_post_pub_params: vdf_post::VDFPoSt::<H, V>::setup(&sp.vdf_post_setup_params)?,
122125
post_periods_count: sp.post_periods_count,
123126
})
124127
}
@@ -128,8 +131,7 @@ where
128131
pub_inputs: &'b PublicInputs<H::Domain>,
129132
priv_inputs: &'b PrivateInputs<'a, H>,
130133
) -> Result<Proof<'a, H, V>> {
131-
let sectors_count = pub_params.pub_params_hvh_post.sectors_count;
132-
let challenge_count = pub_params.pub_params_hvh_post.challenge_count;
134+
let sectors_count = pub_params.vdf_post_pub_params.sectors_count;
133135
let post_periods_count = pub_params.post_periods_count;
134136

135137
if priv_inputs.replicas.len() != sectors_count {
@@ -140,113 +142,57 @@ where
140142
return Err(Error::MalformedInput);
141143
}
142144

143-
let mut proofs_hvh_post = Vec::with_capacity(post_periods_count);
145+
let mut proofs_vdf_post = Vec::with_capacity(post_periods_count);
144146

145147
let mut beacon = Beacon::default();
146148

147-
// First (t = 0)
148-
{
149-
// Run Beacon
150-
let r = beacon.get::<H::Domain>(0);
151-
152-
// Generate challenges
153-
let challenges = derive_challenges::<H>(challenge_count, 0, &[], r.as_ref());
154-
155-
// TODO: avoid cloining
156-
let pub_inputs_hvh_post = hvh_post::PublicInputs {
157-
commitments: pub_inputs.commitments.clone(),
158-
challenges,
159-
};
160-
161-
let priv_inputs_hvh_post =
162-
hvh_post::PrivateInputs::<'a, H>::new(priv_inputs.replicas, priv_inputs.trees);
163-
164-
proofs_hvh_post.push(hvh_post::HvhPost::prove(
165-
&pub_params.pub_params_hvh_post,
166-
&pub_inputs_hvh_post,
167-
&priv_inputs_hvh_post,
168-
)?);
169-
}
170-
171-
// The rest (t = 1..post_periods_count)
172-
for t in 1..post_periods_count {
149+
for t in 0..post_periods_count {
173150
// Run Beacon
174151
let r = beacon.get::<H::Domain>(t);
175-
let x = extract_post_input::<H, V>(&proofs_hvh_post[t - 1]);
176-
177-
// Generate challenges
178-
let challenges = derive_challenges::<H>(challenge_count, t, x.as_ref(), r.as_ref());
179152

180153
// Generate proof
181-
// TODO: avoid cloining
182-
let pub_inputs_hvh_post = hvh_post::PublicInputs {
183-
challenges,
154+
// TODO: avoid cloning
155+
let pub_inputs_vdf_post = vdf_post::PublicInputs {
156+
challenge_seed: r,
184157
commitments: pub_inputs.commitments.clone(),
185158
};
186159

187-
let priv_inputs_hvh_post =
188-
hvh_post::PrivateInputs::new(priv_inputs.replicas, priv_inputs.trees);
160+
let priv_inputs_vdf_post = vdf_post::PrivateInputs::new(priv_inputs.trees);
189161

190-
proofs_hvh_post.push(hvh_post::HvhPost::prove(
191-
&pub_params.pub_params_hvh_post,
192-
&pub_inputs_hvh_post,
193-
&priv_inputs_hvh_post,
162+
proofs_vdf_post.push(vdf_post::VDFPoSt::prove(
163+
&pub_params.vdf_post_pub_params,
164+
&pub_inputs_vdf_post,
165+
&priv_inputs_vdf_post,
194166
)?);
195167
}
196168

197-
Ok(Proof(proofs_hvh_post))
169+
Ok(Proof(proofs_vdf_post))
198170
}
199171

200172
fn verify(
201173
pub_params: &PublicParams<H::Domain, V>,
202174
pub_inputs: &PublicInputs<H::Domain>,
203175
proof: &Proof<H, V>,
204176
) -> Result<bool> {
205-
let challenge_count = pub_params.pub_params_hvh_post.challenge_count;
206177
let post_periods_count = pub_params.post_periods_count;
207178

208-
// HVH Post Verification
179+
// VDF PoSt Verification
209180

210181
let mut beacon = Beacon::default();
211182

212-
// First (t = 0)
213-
{
214-
let r = beacon.get::<H::Domain>(0);
215-
// Generate challenges
216-
let challenges = derive_challenges::<H>(challenge_count, 0, &[], r.as_ref());
217-
218-
// TODO: avoid cloining
219-
let pub_inputs_hvh_post = hvh_post::PublicInputs {
220-
challenges,
221-
commitments: pub_inputs.commitments.clone(),
222-
};
223-
224-
if !hvh_post::HvhPost::verify(
225-
&pub_params.pub_params_hvh_post,
226-
&pub_inputs_hvh_post,
227-
&proof.0[0],
228-
)? {
229-
return Ok(false);
230-
}
231-
}
232-
233-
// The rest (t = 1..post_periods_count)
234-
for t in 1..post_periods_count {
183+
for t in 0..post_periods_count {
235184
// Generate challenges
236185
let r = beacon.get::<H::Domain>(t);
237-
let x = extract_post_input::<H, V>(&proof.0[t - 1]);
238-
239-
let challenges = derive_challenges::<H>(challenge_count, t, x.as_ref(), r.as_ref());
240186

241-
// TODO: avoid cloining
242-
let pub_inputs_hvh_post = hvh_post::PublicInputs {
243-
challenges,
187+
// TODO: avoid cloning
188+
let pub_inputs_vdf_post = vdf_post::PublicInputs {
189+
challenge_seed: r,
244190
commitments: pub_inputs.commitments.clone(),
245191
};
246192

247-
if !hvh_post::HvhPost::verify(
248-
&pub_params.pub_params_hvh_post,
249-
&pub_inputs_hvh_post,
193+
if !vdf_post::VDFPoSt::verify(
194+
&pub_params.vdf_post_pub_params,
195+
&pub_inputs_vdf_post,
250196
&proof.0[t],
251197
)? {
252198
return Ok(false);
@@ -257,33 +203,6 @@ where
257203
}
258204
}
259205

260-
fn extract_post_input<H: Hasher, V: Vdf<H::Domain>>(proof: &hvh_post::Proof<H, V>) -> H::Domain {
261-
let leafs: Vec<u8> = proof.porep_proofs.iter().fold(Vec::new(), |mut acc, p| {
262-
acc.extend(p.leafs().into_iter().fold(
263-
Vec::new(),
264-
|mut inner_acc: Vec<u8>, leaf: &H::Domain| {
265-
inner_acc.extend(leaf.as_ref());
266-
inner_acc
267-
},
268-
));
269-
acc
270-
});
271-
272-
H::Function::hash(&leafs)
273-
}
274-
275-
fn derive_challenges<H: Hasher>(count: usize, t: usize, x: &[u8], r: &[u8]) -> Vec<H::Domain> {
276-
(0..count)
277-
.map(|i| {
278-
let mut i_bytes = [0u8; 32];
279-
LittleEndian::write_u32(&mut i_bytes[0..4], t as u32);
280-
LittleEndian::write_u32(&mut i_bytes[4..8], i as u32);
281-
282-
H::Function::hash(&[x, r, &i_bytes].concat())
283-
})
284-
.collect()
285-
}
286-
287206
#[cfg(test)]
288207
mod tests {
289208
use super::*;
@@ -301,7 +220,7 @@ mod tests {
301220
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
302221

303222
let sp = SetupParams::<PedersenDomain, vdf_sloth::Sloth> {
304-
setup_params_hvh_post: hvh_post::SetupParams::<PedersenDomain, vdf_sloth::Sloth> {
223+
vdf_post_setup_params: vdf_post::SetupParams::<PedersenDomain, vdf_sloth::Sloth> {
305224
challenge_count: 10,
306225
sector_size: 1024 * 32,
307226
post_epochs: 3,

0 commit comments

Comments
 (0)