-
Notifications
You must be signed in to change notification settings - Fork 315
/
zigzag_graph.rs
545 lines (470 loc) · 17.5 KB
/
zigzag_graph.rs
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
use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::{Arc, RwLock};
use crate::crypto::feistel::{self, FeistelPrecomputed};
use crate::drgraph::{BucketGraph, Graph};
use crate::hasher::Hasher;
use crate::layered_drgporep::Layerable;
use crate::parameter_cache::ParameterSetMetadata;
use crate::settings;
pub const DEFAULT_EXPANSION_DEGREE: usize = 8;
// Cache of node's parents.
pub type ParentCache = HashMap<usize, Vec<usize>>;
// ZigZagGraph will hold two different (but related) `ParentCache`,
// the first one for the `forward` direction and the second one
// for the `reversed`.
pub type TwoWayParentCache = [ParentCache; 2];
// The cache is hold in an `Arc` to make it available across different
// threads. It is accessed through a `RwLock` to distinguish between
// read an write operations.
pub type ShareableParentCache = Arc<RwLock<TwoWayParentCache>>;
#[derive(Debug, Clone)]
pub struct ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H> + 'static,
{
expansion_degree: usize,
base_graph: G,
pub reversed: bool,
feistel_precomputed: FeistelPrecomputed,
// This parents cache is currently used for the *expanded parents only*, generated
// by the expensive Feistel operations in the ZigZag, it doesn't contain the
// "base" (in the `Graph` terminology) parents, which are cheaper to compute.
// This is not an LRU cache, it holds the first `cache_entries` of the total
// possible `base_graph.size()` (the assumption here is that we either request
// all entries sequentially when encoding or any random entry once when proving
// or verifying, but there's no locality to take advantage of so keep the logic
// as simple as possible).
parents_cache: ShareableParentCache,
// Keep the size of the cache outside the lock to be easily accessible.
cache_entries: usize,
_h: PhantomData<H>,
}
pub type ZigZagBucketGraph<H> = ZigZagGraph<H, BucketGraph<H>>;
impl<'a, H, G> Layerable<H> for ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H> + 'static,
{
}
impl<H, G> ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H>,
{
pub fn new(
base_graph: Option<G>,
nodes: usize,
base_degree: usize,
expansion_degree: usize,
seed: [u32; 7],
) -> Self {
let cache_entries = if settings::SETTINGS.lock().unwrap().maximize_caching {
info!("using parents cache of unlimited size",);
nodes
} else {
0
};
ZigZagGraph {
base_graph: match base_graph {
Some(graph) => graph,
None => G::new(nodes, base_degree, 0, seed),
},
expansion_degree,
reversed: false,
feistel_precomputed: feistel::precompute((expansion_degree * nodes) as feistel::Index),
parents_cache: Arc::new(RwLock::new([
HashMap::with_capacity(cache_entries),
HashMap::with_capacity(cache_entries),
])),
cache_entries,
_h: PhantomData,
}
}
}
impl<H, G> ParameterSetMetadata for ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H> + ParameterSetMetadata,
{
fn identifier(&self) -> String {
format!(
"zigzag_graph::ZigZagGraph{{expansion_degree: {} base_graph: {} }}",
self.expansion_degree,
self.base_graph.identifier()
)
}
fn sector_size(&self) -> u64 {
self.base_graph.sector_size()
}
}
pub trait ZigZag: ::std::fmt::Debug + Clone + PartialEq + Eq {
type BaseHasher: Hasher;
type BaseGraph: Graph<Self::BaseHasher>;
/// zigzag returns a new graph with expansion component inverted and a distinct
/// base DRG graph -- with the direction of drg connections reversed. (i.e. from high-to-low nodes).
/// The name is 'weird', but so is the operation -- hence the choice.
fn zigzag(&self) -> Self;
/// Constructs a new graph.
fn base_graph(&self) -> Self::BaseGraph;
fn expansion_degree(&self) -> usize;
fn reversed(&self) -> bool;
fn expanded_parents(&self, node: usize) -> Vec<usize>;
fn real_index(&self, i: usize) -> usize;
fn new_zigzag(
nodes: usize,
base_degree: usize,
expansion_degree: usize,
seed: [u32; 7],
) -> Self;
}
impl<Z: ZigZag> Graph<Z::BaseHasher> for Z {
fn size(&self) -> usize {
self.base_graph().size()
}
fn degree(&self) -> usize {
self.base_graph().degree() + self.expansion_degree()
}
#[inline]
fn parents(&self, raw_node: usize, parents: &mut [usize]) {
// If graph is reversed, use real_index to convert index to reversed index.
// So we convert a raw reversed node to an unreversed node, calculate its parents,
// then convert the parents to reversed.
self.base_graph()
.parents(self.real_index(raw_node), parents);
for parent in parents.iter_mut().take(self.base_graph().degree()) {
*parent = self.real_index(*parent);
}
// expanded_parents takes raw_node
let expanded_parents = self.expanded_parents(raw_node);
for (ii, value) in expanded_parents.iter().enumerate() {
parents[ii + self.base_graph().degree()] = *value
}
// Pad so all nodes have correct degree.
let current_length = self.base_graph().degree() + expanded_parents.len();
for ii in 0..(self.degree() - current_length) {
if self.reversed() {
parents[ii + current_length] = self.size() - 1
} else {
parents[ii + current_length] = 0
}
}
assert!(parents.len() == self.degree());
parents.sort();
assert!(parents.iter().all(|p| if self.forward() {
*p <= raw_node
} else {
*p >= raw_node
}));
}
fn seed(&self) -> [u32; 7] {
self.base_graph().seed()
}
fn new(nodes: usize, base_degree: usize, expansion_degree: usize, seed: [u32; 7]) -> Self {
Z::new_zigzag(nodes, base_degree, expansion_degree, seed)
}
fn forward(&self) -> bool {
!self.reversed()
}
}
impl<'a, H, G> ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H>,
{
// Assign `expansion_degree` parents to `node` using an invertible function. That
// means we can't just generate random values between `[0, size())`, we need to
// expand the search space (domain) to accommodate every unique parent assignment
// generated here. This can be visualized more clearly as a matrix where the each
// new parent of each new node is assigned a unique `index`:
//
//
// | Parent 1 | Parent 2 | Parent 3 |
//
// | Node 1 | 0 | 1 | 2 |
//
// | Node 2 | 3 | 4 | 5 |
//
// | Node 3 | 6 | 7 | 8 |
//
// | Node 4 | 9 | A | B |
//
// This starting `index` will be shuffled to another position to generate a
// parent-child relationship, e.g., if generating the parents for the second node,
// `permute` would be called with values `[3; 4; 5]` that would be mapped to other
// indexes in the search space of `[0, B]`, say, values `[A; 0; 4]`, that would
// correspond to nodes numbered `[4; 1, 2]` which will become the parents of the
// second node. In a later pass invalid parents like 2, self-referencing, and parents
// with indexes bigger than 2 (if in the `forward` direction, smaller than 2 if the
// inverse), will be removed.
//
// Since `permute` is a bijective function which has the inverse `invert_permute`,
// it is guaranteed that when looking for the parents in the `reversed` direction
// the child `node` used earlier will now actually be the parent of the output
// parents generated before (inverting the relationship). Following the example,
// in the reverse direction, when looking for the parents of, say, node 1,
// `invert_permute` (that maps back the output of `permute` to its input) would
// receive the indexes `[0; 1; 2]`, where the index `0` is guaranteed to map back
// to the index `4` that generated it earlier, corresponding to the node 2, inverting
// in fact the child-parent relationship.
fn correspondent(&self, node: usize, i: usize) -> usize {
let a = (node * self.expansion_degree) as feistel::Index + i as feistel::Index;
let feistel_keys = &[1, 2, 3, 4];
let transformed = if self.reversed {
feistel::invert_permute(
self.size() as feistel::Index * self.expansion_degree as feistel::Index,
a,
feistel_keys,
self.feistel_precomputed,
)
} else {
feistel::permute(
self.size() as feistel::Index * self.expansion_degree as feistel::Index,
a,
feistel_keys,
self.feistel_precomputed,
)
};
transformed as usize / self.expansion_degree
// Collapse the output in the matrix search space to the row of the corresponding
// node (losing the column information, that will be regenerated later when calling
// back this function in the `reversed` direction).
}
// The first cache in `parents_cache` corresponds to the forward direction,
// the second one to the reversed.
fn get_cache_index(&self) -> usize {
if self.forward() {
0
} else {
1
}
}
// Read the `node` entry in the parents cache (which may not exist) for
// the current direction set in the graph and return a copy of it (or
// `None` to signal a cache miss).
fn read_parents_cache(&self, node: usize) -> Option<Vec<usize>> {
// If the index exceeds the cache size don't bother checking.
if node >= self.cache_entries {
return None;
}
let read_lock = self.parents_cache.read().unwrap();
let parents_cache = &(*read_lock)[self.get_cache_index()];
if let Some(parents) = parents_cache.get(&node) {
Some(parents.clone())
} else {
None
}
}
// Save the `parents` of the `node` in its entry of the cache.
fn write_parents_cache(&self, node: usize, parents: Vec<usize>) {
// Don't allow writing more entries than the already allocated space.
if node >= self.cache_entries {
return;
}
let mut write_lock = self.parents_cache.write().unwrap();
let parents_cache = &mut (*write_lock)[self.get_cache_index()];
let old_value = parents_cache.insert(node, parents);
debug_assert_eq!(old_value, None);
// We shouldn't be rewriting entries (with most likely the same values),
// this would be a clear indication of a bug.
}
}
impl<'a, H, G> ZigZag for ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H>,
{
type BaseHasher = H;
type BaseGraph = G;
fn new_zigzag(
nodes: usize,
base_degree: usize,
expansion_degree: usize,
seed: [u32; 7],
) -> Self {
Self::new(None, nodes, base_degree, expansion_degree, seed)
}
/// To zigzag a graph, we just toggle its reversed field.
/// All the real work happens when we calculate node parents on-demand.
// We always share the two caches (forward/reversed) between
// ZigZag graphs even if each graph will use only one of those
// caches (depending of its direction). This allows to propagate
// the caches across different layers, where consecutive even+odd
// layers have inverse directions.
fn zigzag(&self) -> Self {
let mut zigzag = self.clone();
zigzag.reversed = !zigzag.reversed;
zigzag
}
fn base_graph(&self) -> Self::BaseGraph {
self.base_graph.clone()
}
fn expansion_degree(&self) -> usize {
self.expansion_degree
}
fn reversed(&self) -> bool {
self.reversed
}
// TODO: Optimization: Evaluate providing an `all_parents` (and hence
// `all_expanded_parents`) method that would return the entire cache
// in a single lock operation, or at least (if the cache is not big enough)
// it would allow to batch parents calculations with that single lock. Also,
// since there is a reciprocity between forward and reversed parents,
// we would only need to compute the parents in one direction and with
// that fill both caches.
#[inline]
fn expanded_parents(&self, node: usize) -> Vec<usize> {
if let Some(parents) = self.read_parents_cache(node) {
return parents;
}
let parents: Vec<usize> = (0..self.expansion_degree)
.filter_map(|i| {
let other = self.correspondent(node, i);
if self.reversed {
if other > node {
Some(other)
} else {
None
}
} else if other < node {
Some(other)
} else {
None
}
})
.collect();
self.write_parents_cache(node, parents.clone());
parents
}
#[inline]
fn real_index(&self, i: usize) -> usize {
if self.reversed {
(self.size() - 1) - i
} else {
i
}
}
}
impl<H, G> PartialEq for ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H>,
{
fn eq(&self, other: &ZigZagGraph<H, G>) -> bool {
self.base_graph == other.base_graph
&& self.expansion_degree == other.expansion_degree
&& self.reversed == other.reversed
}
}
impl<H, G> Eq for ZigZagGraph<H, G>
where
H: Hasher,
G: Graph<H>,
{
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use crate::drgraph::new_seed;
use crate::hasher::{Blake2sHasher, PedersenHasher, Sha256Hasher};
fn assert_graph_ascending<H: Hasher, G: Graph<H>>(g: G) {
for i in 0..g.size() {
let mut parents = vec![0; g.degree()];
g.parents(i, &mut parents);
for p in parents {
if i == 0 {
assert!(p == i);
} else {
assert!(p < i);
}
}
}
}
fn assert_graph_descending<H: Hasher, G: Graph<H>>(g: G) {
for i in 0..g.size() {
let mut parents = vec![0; g.degree()];
g.parents(i, &mut parents);
for p in parents {
if i == g.size() - 1 {
assert!(p == i);
} else {
assert!(p > i);
}
}
}
}
#[test]
fn zigzag_graph_zigzags_pedersen() {
test_zigzag_graph_zigzags::<PedersenHasher>();
}
#[test]
fn zigzag_graph_zigzags_sha256() {
test_zigzag_graph_zigzags::<Sha256Hasher>();
}
#[test]
fn zigzag_graph_zigzags_blake2s() {
test_zigzag_graph_zigzags::<Blake2sHasher>();
}
fn test_zigzag_graph_zigzags<H: 'static + Hasher>() {
let g = ZigZagBucketGraph::<H>::new_zigzag(50, 5, DEFAULT_EXPANSION_DEGREE, new_seed());
let gz = g.zigzag();
assert_graph_ascending(g);
assert_graph_descending(gz);
}
#[test]
fn expansion_pedersen() {
test_expansion::<PedersenHasher>();
}
#[test]
fn expansion_sha256() {
test_expansion::<Sha256Hasher>();
}
#[test]
fn expansion_blake2s() {
test_expansion::<Blake2sHasher>();
}
fn test_expansion<H: 'static + Hasher>() {
// We need a graph.
let g = ZigZagBucketGraph::<H>::new_zigzag(25, 5, DEFAULT_EXPANSION_DEGREE, new_seed());
// We're going to fully realize the expansion-graph component, in a HashMap.
let gcache = get_all_expanded_parents(&g);
// Here's the zigzag version of the graph.
let gz = g.zigzag();
// And a HashMap to hold the expanded parents.
let gzcache = get_all_expanded_parents(&gz);
for i in 0..gz.size() {
let parents = gzcache.get(&i).unwrap();
// Check to make sure all (expanded) node-parent relationships also exist in reverse,
// in the original graph's Hashmap.
for p in parents {
assert!(gcache[&p].contains(&i));
}
}
// And then do the same check to make sure all (expanded) node-parent relationships from the original
// are present in the zigzag, just reversed.
for i in 0..g.size() {
let parents = g.expanded_parents(i);
for p in parents {
assert!(gzcache[&p].contains(&i));
}
}
// Having checked both ways, we know the graph and its zigzag counterpart have 'expanded' components
// which are each other's inverses. It's important that this be true.
}
fn get_all_expanded_parents<H: 'static + Hasher>(
zigzag_graph: &ZigZagBucketGraph<H>,
) -> HashMap<usize, Vec<usize>> {
let mut parents_map: HashMap<usize, Vec<usize>> = HashMap::new();
for i in 0..zigzag_graph.size() {
parents_map.insert(i, zigzag_graph.expanded_parents(i));
}
assert_eq!(get_cache_size(&zigzag_graph), zigzag_graph.cache_entries);
parents_map
}
fn get_cache_size<H: 'static + Hasher>(zigzag_graph: &ZigZagBucketGraph<H>) -> usize {
let parents_cache_lock = zigzag_graph.parents_cache.read().unwrap();
(*parents_cache_lock)[zigzag_graph.get_cache_index()].len()
}
}