1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use std::path::PathBuf;
use anyhow::{ensure, Result};
use cess_proofs_v1::{constants, with_shape};
use cess_proofs_v1::{PoRepConfig, PoRepProofPartitions, PoStConfig, PoStType, SectorSize};
use serde::{Deserialize, Serialize};
use crate::{get_parameter_data, get_verifying_key_data, ApiVersion, MerkleTreeTrait};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredSealProof {
StackedDrg2KiBV1,
StackedDrg8MiBV1,
StackedDrg32MiBV1,
StackedDrg512MiBV1,
StackedDrg32GiBV1,
StackedDrg64GiBV1,
StackedDrg2KiBV1_1,
StackedDrg8MiBV1_1,
StackedDrg32MiBV1_1,
StackedDrg512MiBV1_1,
StackedDrg32GiBV1_1,
StackedDrg64GiBV1_1,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredAggregationProof {
SnarkPackV1,
}
macro_rules! self_shape {
($name:ident, $selfty:ty, $self:expr, $ret:ty) => {{
fn $name<Tree: 'static + MerkleTreeTrait>(s: $selfty) -> Result<$ret> {
s.as_v1_config().$name::<Tree>()
}
with_shape!(u64::from($self.sector_size()), $name, $self)
}};
}
impl RegisteredSealProof {
pub fn version(self) -> ApiVersion {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg32MiBV1 | StackedDrg512MiBV1
| StackedDrg32GiBV1 | StackedDrg64GiBV1 => ApiVersion::V1_0_0,
StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg32MiBV1_1
| StackedDrg512MiBV1_1 | StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
ApiVersion::V1_1_0
}
}
}
pub fn major_version(self) -> u64 {
self.version().as_semver().major
}
pub fn minor_version(self) -> u64 {
self.version().as_semver().minor
}
pub fn patch_version(self) -> u64 {
self.version().as_semver().patch
}
pub fn sector_size(self) -> SectorSize {
use RegisteredSealProof::*;
let size = match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => constants::SECTOR_SIZE_2_KIB,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => constants::SECTOR_SIZE_8_MIB,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => constants::SECTOR_SIZE_32_MIB,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => constants::SECTOR_SIZE_512_MIB,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => constants::SECTOR_SIZE_32_GIB,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => constants::SECTOR_SIZE_64_GIB,
};
SectorSize(size)
}
pub fn partitions(self) -> u8 {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_2_KIB)
.expect("invalid sector size"),
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_8_MIB)
.expect("invalid sector size"),
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_32_MIB)
.expect("invalid sector size"),
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_512_MIB)
.expect("invalid sector size"),
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_32_GIB)
.expect("invalid sector size"),
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_64_GIB)
.expect("invalid sector size"),
}
}
pub fn single_partition_proof_len(self) -> usize {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg32MiBV1 | StackedDrg512MiBV1
| StackedDrg32GiBV1 | StackedDrg64GiBV1 | StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1
| StackedDrg32MiBV1_1 | StackedDrg512MiBV1_1 | StackedDrg32GiBV1_1
| StackedDrg64GiBV1_1 => cess_proofs_v1::SINGLE_PARTITION_PROOF_LEN,
}
}
fn nonce(self) -> u64 {
#[allow(clippy::match_single_binding)]
match self {
_ => 0,
}
}
fn porep_id(self) -> [u8; 32] {
let mut porep_id = [0; 32];
let registered_proof_id = self as u64;
let nonce = self.nonce();
porep_id[0..8].copy_from_slice(®istered_proof_id.to_le_bytes());
porep_id[8..16].copy_from_slice(&nonce.to_le_bytes());
porep_id
}
pub fn as_v1_config(self) -> PoRepConfig {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg32MiBV1 | StackedDrg512MiBV1
| StackedDrg32GiBV1 | StackedDrg64GiBV1 => {
assert_eq!(self.version(), ApiVersion::V1_0_0);
PoRepConfig {
sector_size: self.sector_size(),
partitions: PoRepProofPartitions(self.partitions()),
porep_id: self.porep_id(),
api_version: self.version(),
}
}
StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg32MiBV1_1
| StackedDrg512MiBV1_1 | StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
assert_eq!(self.version(), ApiVersion::V1_1_0);
PoRepConfig {
sector_size: self.sector_size(),
partitions: PoRepProofPartitions(self.partitions()),
porep_id: self.porep_id(),
api_version: self.version(),
}
}
}
}
pub fn circuit_identifier(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
self_shape!(get_cache_identifier, RegisteredSealProof, self, String)
}
}
}
pub fn cache_verifying_key_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => self_shape!(
get_cache_verifying_key_path,
RegisteredSealProof,
self,
PathBuf
),
}
}
pub fn cache_params_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
self_shape!(get_cache_params_path, RegisteredSealProof, self, PathBuf)
}
}
}
pub fn verifying_key_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
let id = self.circuit_identifier()?;
let params = get_verifying_key_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params
.expect("verifying key cid params failure")
.cid
.clone())
}
}
}
pub fn params_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
let id = self.circuit_identifier()?;
let params = get_parameter_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params.expect("param cid failure").cid.clone())
}
}
}
pub fn into_winning_post(self) -> RegisteredPoStProof {
use RegisteredPoStProof::*;
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => StackedDrgWinning2KiBV1,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => StackedDrgWinning8MiBV1,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => StackedDrgWinning32MiBV1,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => StackedDrgWinning512MiBV1,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => StackedDrgWinning32GiBV1,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => StackedDrgWinning64GiBV1,
}
}
pub fn into_window_post(self) -> RegisteredPoStProof {
use RegisteredPoStProof::*;
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => StackedDrgWindow2KiBV1,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => StackedDrgWindow8MiBV1,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => StackedDrgWindow32MiBV1,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => StackedDrgWindow512MiBV1,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => StackedDrgWindow32GiBV1,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => StackedDrgWindow64GiBV1,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredPoStProof {
StackedDrgWinning2KiBV1,
StackedDrgWinning8MiBV1,
StackedDrgWinning32MiBV1,
StackedDrgWinning512MiBV1,
StackedDrgWinning32GiBV1,
StackedDrgWinning64GiBV1,
StackedDrgWindow2KiBV1,
StackedDrgWindow8MiBV1,
StackedDrgWindow32MiBV1,
StackedDrgWindow512MiBV1,
StackedDrgWindow32GiBV1,
StackedDrgWindow64GiBV1,
}
impl RegisteredPoStProof {
pub fn version(self) -> ApiVersion {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1
| StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => ApiVersion::V1_0_0,
}
}
pub fn major_version(self) -> u64 {
self.version().as_semver().major
}
pub fn minor_version(self) -> u64 {
self.version().as_semver().minor
}
pub fn patch_version(self) -> u64 {
self.version().as_semver().patch
}
pub fn sector_size(self) -> SectorSize {
use RegisteredPoStProof::*;
let size = match self {
StackedDrgWinning2KiBV1 | StackedDrgWindow2KiBV1 => constants::SECTOR_SIZE_2_KIB,
StackedDrgWinning8MiBV1 | StackedDrgWindow8MiBV1 => constants::SECTOR_SIZE_8_MIB,
StackedDrgWinning32MiBV1 | StackedDrgWindow32MiBV1 => constants::SECTOR_SIZE_32_MIB,
StackedDrgWinning512MiBV1 | StackedDrgWindow512MiBV1 => constants::SECTOR_SIZE_512_MIB,
StackedDrgWinning32GiBV1 | StackedDrgWindow32GiBV1 => constants::SECTOR_SIZE_32_GIB,
StackedDrgWinning64GiBV1 | StackedDrgWindow64GiBV1 => constants::SECTOR_SIZE_64_GIB,
};
SectorSize(size)
}
pub fn typ(self) -> PoStType {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => PoStType::Winning,
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => PoStType::Window,
}
}
pub fn single_partition_proof_len(self) -> usize {
match self.version() {
ApiVersion::V1_0_0 => cess_proofs_v1::SINGLE_PARTITION_PROOF_LEN,
_ => panic!("Invalid PoSt api version"),
}
}
pub fn sector_count(self) -> usize {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => constants::WINNING_POST_SECTOR_COUNT,
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => *constants::WINDOW_POST_SECTOR_COUNT
.read()
.expect("window post sector count failure")
.get(&u64::from(self.sector_size()))
.expect("invalid sector size"),
}
}
pub fn as_v1_config(self) -> PoStConfig {
assert_eq!(self.version(), ApiVersion::V1_0_0);
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => PoStConfig {
typ: self.typ(),
sector_size: self.sector_size(),
sector_count: self.sector_count(),
challenge_count: constants::WINNING_POST_CHALLENGE_COUNT,
priority: true,
api_version: self.version(),
},
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => PoStConfig {
typ: self.typ(),
sector_size: self.sector_size(),
sector_count: self.sector_count(),
challenge_count: constants::WINDOW_POST_CHALLENGE_COUNT,
priority: true,
api_version: self.version(),
},
}
}
pub fn circuit_identifier(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
self_shape!(get_cache_identifier, RegisteredPoStProof, self, String)
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn cache_verifying_key_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 => self_shape!(
get_cache_verifying_key_path,
RegisteredPoStProof,
self,
PathBuf
),
_ => panic!("Invalid PoSt api version"),
}
}
pub fn cache_params_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 => {
self_shape!(get_cache_params_path, RegisteredPoStProof, self, PathBuf)
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn verifying_key_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
let id = self.circuit_identifier()?;
let params = get_verifying_key_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params
.expect("verifying key cid params failure")
.cid
.clone())
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn params_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
let id = self.circuit_identifier()?;
let params = get_parameter_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params.expect("params cid failure").cid.clone())
}
_ => panic!("Invalid PoSt api version"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use cess_proofs_v1::MAX_LEGACY_REGISTERED_SEAL_PROOF_ID;
const REGISTERED_SEAL_PROOFS: [RegisteredSealProof; 10] = [
RegisteredSealProof::StackedDrg2KiBV1,
RegisteredSealProof::StackedDrg8MiBV1,
RegisteredSealProof::StackedDrg512MiBV1,
RegisteredSealProof::StackedDrg32GiBV1,
RegisteredSealProof::StackedDrg64GiBV1,
RegisteredSealProof::StackedDrg2KiBV1_1,
RegisteredSealProof::StackedDrg8MiBV1_1,
RegisteredSealProof::StackedDrg512MiBV1_1,
RegisteredSealProof::StackedDrg32GiBV1_1,
RegisteredSealProof::StackedDrg64GiBV1_1,
];
#[test]
fn test_porep_id() {
for rsp in ®ISTERED_SEAL_PROOFS {
test_porep_id_aux(rsp);
}
}
fn test_porep_id_aux(rsp: &RegisteredSealProof) {
let expected_porep_id = match rsp {
RegisteredSealProof::StackedDrg2KiBV1 => {
"0000000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg8MiBV1 => {
"0100000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32MiBV1 => {
"0200000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg512MiBV1 => {
"0300000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32GiBV1 => {
"0400000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg64GiBV1 => {
"0500000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg2KiBV1_1 => {
"0600000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg8MiBV1_1 => {
"0700000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32MiBV1_1 => {
"0800000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg512MiBV1_1 => {
"0900000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32GiBV1_1 => {
"1000000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg64GiBV1_1 => {
"1100000000000000000000000000000000000000000000000000000000000000"
}
};
let hex: String = rsp
.porep_id()
.iter()
.map(|x| format!("{:01$x}", x, 2))
.collect();
assert_eq!(expected_porep_id, &hex);
}
#[test]
fn test_max_initial_porep_id() {
for rsp in ®ISTERED_SEAL_PROOFS {
let mut porep_id_type_bytes = [0u8; 8];
let porep_id = rsp.porep_id();
porep_id_type_bytes.copy_from_slice(&porep_id[..8]);
let porep_type = u64::from_le_bytes(porep_id_type_bytes);
let is_legacy = porep_type <= MAX_LEGACY_REGISTERED_SEAL_PROOF_ID;
match rsp {
RegisteredSealProof::StackedDrg2KiBV1
| RegisteredSealProof::StackedDrg8MiBV1
| RegisteredSealProof::StackedDrg32MiBV1
| RegisteredSealProof::StackedDrg512MiBV1
| RegisteredSealProof::StackedDrg32GiBV1
| RegisteredSealProof::StackedDrg64GiBV1 => assert!(is_legacy),
RegisteredSealProof::StackedDrg2KiBV1_1
| RegisteredSealProof::StackedDrg8MiBV1_1
| RegisteredSealProof::StackedDrg32MiBV1_1
| RegisteredSealProof::StackedDrg512MiBV1_1
| RegisteredSealProof::StackedDrg32GiBV1_1
| RegisteredSealProof::StackedDrg64GiBV1_1 => assert!(!is_legacy),
}
}
}
#[test]
fn test_verifying_key_path() {
for rsp in ®ISTERED_SEAL_PROOFS {
rsp.cache_verifying_key_path()
.expect("failed to get verifying key path");
}
}
#[test]
fn test_verifying_key_cid() {
for rsp in ®ISTERED_SEAL_PROOFS {
rsp.verifying_key_cid()
.expect("failed to get verifying key cid");
}
}
#[test]
fn test_params_path() {
for rsp in ®ISTERED_SEAL_PROOFS {
rsp.cache_params_path().expect("failed to get params path");
}
}
#[test]
fn test_params_cid() {
for rsp in ®ISTERED_SEAL_PROOFS {
rsp.params_cid().expect("failed to get params_cid");
}
}
}