@@ -6,30 +6,30 @@ use serde::de::Deserialize;
6
6
use serde:: ser:: Serialize ;
7
7
8
8
use crate :: error:: { Error , Result } ;
9
- use crate :: hasher:: { Domain , HashFunction , Hasher } ;
10
- use crate :: hvh_post;
9
+ use crate :: hasher:: { Domain , Hasher } ;
11
10
use crate :: merkle:: MerkleTree ;
12
11
use crate :: parameter_cache:: ParameterSetIdentifier ;
13
12
use crate :: proof:: ProofScheme ;
14
13
use crate :: vdf:: Vdf ;
14
+ use crate :: vdf_post;
15
15
16
16
#[ derive( Clone , Debug ) ]
17
17
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 > ,
19
19
pub post_periods_count : usize ,
20
20
}
21
21
22
22
#[ derive( Clone , Debug ) ]
23
23
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 > ,
25
25
pub post_periods_count : usize ,
26
26
}
27
27
28
28
impl < T : Domain , V : Vdf < T > > ParameterSetIdentifier for PublicParams < T , V > {
29
29
fn parameter_set_identifier ( & self ) -> String {
30
30
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( ) ,
33
33
self . post_periods_count
34
34
)
35
35
}
@@ -67,14 +67,14 @@ impl<'a, H: 'a + Hasher> PrivateInputs<'a, H> {
67
67
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
68
68
pub struct Proof < ' a , H : Hasher + ' a , V : Vdf < H :: Domain > > (
69
69
#[ 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>"
72
72
) ) ]
73
- Vec < hvh_post :: Proof < ' a , H , V > > ,
73
+ Vec < vdf_post :: Proof < ' a , H , V > > ,
74
74
) ;
75
75
76
76
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 > ] {
78
78
& self . 0
79
79
}
80
80
}
@@ -86,10 +86,13 @@ pub struct BeaconPoSt<H: Hasher, V: Vdf<H::Domain>> {
86
86
}
87
87
88
88
#[ derive( Clone , Debug , Default ) ]
89
- struct Beacon {
89
+ pub struct Beacon {
90
90
count : usize ,
91
91
}
92
92
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.
93
96
impl Beacon {
94
97
pub fn get < T : Domain > ( & mut self , t : usize ) -> T {
95
98
// TODO: actual beacon
@@ -118,7 +121,7 @@ where
118
121
119
122
fn setup ( sp : & SetupParams < H :: Domain , V > ) -> Result < PublicParams < H :: Domain , V > > {
120
123
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 ) ?,
122
125
post_periods_count : sp. post_periods_count ,
123
126
} )
124
127
}
@@ -128,8 +131,7 @@ where
128
131
pub_inputs : & ' b PublicInputs < H :: Domain > ,
129
132
priv_inputs : & ' b PrivateInputs < ' a , H > ,
130
133
) -> 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 ;
133
135
let post_periods_count = pub_params. post_periods_count ;
134
136
135
137
if priv_inputs. replicas . len ( ) != sectors_count {
@@ -140,113 +142,57 @@ where
140
142
return Err ( Error :: MalformedInput ) ;
141
143
}
142
144
143
- let mut proofs_hvh_post = Vec :: with_capacity ( post_periods_count) ;
145
+ let mut proofs_vdf_post = Vec :: with_capacity ( post_periods_count) ;
144
146
145
147
let mut beacon = Beacon :: default ( ) ;
146
148
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 {
173
150
// Run Beacon
174
151
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 ( ) ) ;
179
152
180
153
// 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 ,
184
157
commitments : pub_inputs. commitments . clone ( ) ,
185
158
} ;
186
159
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 ) ;
189
161
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 ,
194
166
) ?) ;
195
167
}
196
168
197
- Ok ( Proof ( proofs_hvh_post ) )
169
+ Ok ( Proof ( proofs_vdf_post ) )
198
170
}
199
171
200
172
fn verify (
201
173
pub_params : & PublicParams < H :: Domain , V > ,
202
174
pub_inputs : & PublicInputs < H :: Domain > ,
203
175
proof : & Proof < H , V > ,
204
176
) -> Result < bool > {
205
- let challenge_count = pub_params. pub_params_hvh_post . challenge_count ;
206
177
let post_periods_count = pub_params. post_periods_count ;
207
178
208
- // HVH Post Verification
179
+ // VDF PoSt Verification
209
180
210
181
let mut beacon = Beacon :: default ( ) ;
211
182
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 {
235
184
// Generate challenges
236
185
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 ( ) ) ;
240
186
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 ,
244
190
commitments : pub_inputs. commitments . clone ( ) ,
245
191
} ;
246
192
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 ,
250
196
& proof. 0 [ t] ,
251
197
) ? {
252
198
return Ok ( false ) ;
@@ -257,33 +203,6 @@ where
257
203
}
258
204
}
259
205
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
-
287
206
#[ cfg( test) ]
288
207
mod tests {
289
208
use super :: * ;
@@ -301,7 +220,7 @@ mod tests {
301
220
let rng = & mut XorShiftRng :: from_seed ( [ 0x3dbe6259 , 0x8d313d76 , 0x3237db17 , 0xe5bc0654 ] ) ;
302
221
303
222
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 > {
305
224
challenge_count : 10 ,
306
225
sector_size : 1024 * 32 ,
307
226
post_epochs : 3 ,
0 commit comments