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
613
614
615
616
617
618
619
620
621
622
//! Static layer aggregate circuit.
use crate::circuit::circuit_connections::CrossCircuitConnections;
use crate::circuit::{base_circuit, dyn_layers::CircuitLayerIter, CircuitId, GateIdx};
use crate::common::BitVec;
use crate::protocols::{Gate, Plain};
use crate::{GateId, SubCircuitGate};
use either::Either;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::ops::Range;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(bound = "G: serde::Serialize + serde::de::DeserializeOwned,\
    Idx: Ord + Eq + Hash + serde::Serialize + serde::de::DeserializeOwned")]
pub struct Circuit<G, Idx> {
    main_input_gates: Vec<GateId<Idx>>,
    main_output_gates: Vec<GateId<Idx>>,
    sub_circuits: Vec<SubCircuit<G, Idx>>,
    sc_map: HashMap<CircuitId, usize>,
    layers: Vec<Layer>,
    cross_circuit_incoming: CrossCircuitConnections<Idx>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Layer {
    sub_circuit_layers: Vec<CircuitId>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SubCircuit<G, Idx> {
    simd_size: Option<NonZeroUsize>,
    gate_count: usize,
    interactive_count: usize,
    layers: Vec<ScLayer<G, Idx>>,
}

#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct ScLayer<G, Idx> {
    non_interactive: ScLayerGates<G, Idx>,
    interactive: ScLayerGates<G, Idx>,
    incoming_idx: Vec<GateId<Idx>>,
}

#[derive(Debug, Clone)]
pub struct ExecutableScLayer<'c, G, Idx> {
    pub(crate) sc_id: CircuitId,
    layer: &'c ScLayer<G, Idx>,
}

#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ScLayerGates<G, Idx> {
    gates: Vec<G>,
    idx: Vec<GateId<Idx>>,
    incoming: Vec<Range<Idx>>,
    potential_cross_circ: BitVec,
}

#[derive(Debug, Clone)]
pub struct LayerIterator<'a, G, Idx> {
    circ: &'a Circuit<G, Idx>,
    layer_iter_state: Vec<usize>,
    current_layer: usize,
}

impl<G, Idx> Circuit<G, Idx> {
    pub fn gate_count(&self) -> usize {
        self.sc_map
            .values()
            .map(|mapped_id| self.sub_circuits[*mapped_id].gate_count)
            .sum()
    }

    pub fn interactive_count(&self) -> usize {
        self.sc_map
            .values()
            .map(|mapped_id| self.sub_circuits[*mapped_id].interactive_count)
            .sum()
    }

    pub fn interactive_count_times_simd(&self) -> usize {
        self.sc_map
            .values()
            .map(|mapped_id| {
                let sc = &self.sub_circuits[*mapped_id];
                sc.interactive_count * sc.simd_size.map(NonZeroUsize::get).unwrap_or(1)
            })
            .sum()
    }

    pub fn sub_circuit_count(&self) -> usize {
        self.sc_map.len()
    }

    pub fn gate_counts(&self) -> impl Iterator<Item = (usize, Option<NonZeroUsize>)> + '_ {
        // Return gate counts in correct order
        (0..self.sc_map.len() as CircuitId).map(|sc_id| {
            let sc = self.get_circ(sc_id);
            (sc.gate_count, sc.simd_size)
        })
    }

    pub fn input_count(&self) -> usize {
        self.main_input_gates.len()
    }

    pub fn output_count(&self) -> usize {
        self.main_output_gates.len()
    }

    pub fn input_gates(&self) -> &[GateId<Idx>] {
        &self.main_input_gates
    }

    pub fn output_gates(&self) -> &[GateId<Idx>] {
        &self.main_output_gates
    }

    pub(crate) fn get_circ(&self, sc_id: CircuitId) -> &SubCircuit<G, Idx> {
        let mapped_id = self.sc_map[&sc_id];
        &self.sub_circuits[mapped_id]
    }
}

impl<G: Clone + PartialEq, Idx: GateIdx> Circuit<G, Idx> {
    fn add_layer<P>(
        &mut self,
        (sc_id, simd_size, sc_layer): (
            CircuitId,
            Option<NonZeroUsize>,
            base_circuit::CircuitLayer<G, Idx>,
        ),
        circ: &super::dyn_layers::Circuit<P, G, Idx>,
        layer_ptrs: &[usize],
        splits: &mut HashMap<usize, Vec<usize>>,
    ) where
        G: Gate<P>,
    {
        let mapped_sc_id = self.sc_map[&sc_id];
        let new_sc_layer = ScLayer::from_base_layer(sc_id, sc_layer, circ);
        match self.sub_circuits.get_mut(mapped_sc_id) {
            None => {
                self.sub_circuits
                    .push(SubCircuit::new(new_sc_layer, simd_size));
            }
            Some(sc) => {
                let layer = layer_ptrs[sc_id as usize];
                if let Some(precomp_layer) = sc.layers.get(layer) {
                    if &new_sc_layer != precomp_layer {
                        let mut split_sc = sc.clone();
                        for split in splits.get(&mapped_sc_id).unwrap_or(&vec![]) {
                            let split_sc = &self.sub_circuits[*split];
                            let split_precomp_layer = &split_sc.layers[layer];
                            if &new_sc_layer == split_precomp_layer {
                                self.sc_map.insert(sc_id, *split);
                                return;
                            }
                        }
                        // truncate everything beginning from precomp_layer which is not identical
                        // to sc_layer
                        split_sc.truncate(layer);
                        split_sc.push_layer(new_sc_layer);
                        // Update sc_map to so sc_id points to new sc
                        self.sc_map.insert(sc_id, self.sub_circuits.len());
                        splits
                            .entry(mapped_sc_id)
                            .or_default()
                            .push(self.sub_circuits.len());
                        self.sub_circuits.push(split_sc);
                    }
                } else {
                    sc.push_layer(new_sc_layer);
                }
            }
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (G, SubCircuitGate<Idx>)> + Clone + '_ {
        let layer_iter = LayerIterator::new(self);
        layer_iter.flatten().flat_map(|layer| {
            layer
                .iter()
                .map(move |(g, id)| (g, SubCircuitGate::new(layer.sc_id, id)))
        })
    }

    pub fn iter_with_parents(
        &self,
    ) -> impl Iterator<
        Item = (
            G,
            SubCircuitGate<Idx>,
            impl Iterator<Item = SubCircuitGate<Idx>> + '_,
        ),
    > + '_ {
        let layer_iter = LayerIterator::new(self);
        layer_iter
            .flatten()
            .flat_map(move |layer| layer.iter_with_parents(self))
    }

    pub fn interactive_iter(&self) -> impl Iterator<Item = (G, SubCircuitGate<Idx>)> + Clone + '_ {
        let layer_iter = LayerIterator::new(self);
        layer_iter.flatten().flat_map(|layer| {
            layer
                .layer
                .interactive
                .iter()
                .map(move |(g, id)| (g, SubCircuitGate::new(layer.sc_id, id)))
        })
    }

    pub fn interactive_with_parents_iter(
        &self,
    ) -> impl Iterator<
        Item = (
            G,
            SubCircuitGate<Idx>,
            impl Iterator<Item = SubCircuitGate<Idx>> + '_,
        ),
    > + '_ {
        let layer_iter = LayerIterator::new(self);
        layer_iter.flatten().flat_map(|layer| {
            let sc_id = layer.sc_id;
            layer
                .clone()
                .interactive_iter()
                .zip(layer.interactive_parents_iter(self))
                .map(move |((g, id), parents)| (g, SubCircuitGate::new(sc_id, id), parents))
        })
    }
}

impl<P: Plain, G: Gate<P>, Idx: GateIdx> super::dyn_layers::Circuit<P, G, Idx> {
    pub fn precompute_layers(self) -> Circuit<G, Idx> {
        let layer_iter = CircuitLayerIter::new(&self);
        let mut res_circ = Circuit {
            main_input_gates: self.get_circ(0).input_gates().to_vec(),
            main_output_gates: self.get_circ(0).output_gates().to_vec(),
            sub_circuits: vec![],
            sc_map: self.circ_map.clone(),
            layers: vec![],
            cross_circuit_incoming: self.connections.clone_incoming(),
        };
        let mut layer_ptrs = vec![0; self.circ_map.len()];
        let mut splits = HashMap::default();

        for layer in layer_iter {
            let mut layer_ids = Vec::with_capacity(layer.sc_layers.len());
            for sc_layer in layer.sc_layers {
                let sc_id = sc_layer.0;
                res_circ.add_layer(sc_layer, &self, &layer_ptrs, &mut splits);
                layer_ptrs[sc_id as usize] += 1;
                layer_ids.push(sc_id);
            }
            res_circ.layers.push(Layer {
                sub_circuit_layers: layer_ids,
            });
        }
        res_circ
    }
}

impl<G, Idx> SubCircuit<G, Idx> {
    pub(crate) fn simd_size(&self) -> Option<NonZeroUsize> {
        self.simd_size
    }
}

impl<G: Clone, Idx: GateIdx> SubCircuit<G, Idx> {
    fn new(layer: ScLayer<G, Idx>, simd_size: Option<NonZeroUsize>) -> Self {
        Self {
            simd_size,
            gate_count: layer.len(),
            interactive_count: layer.interactive.len(),
            layers: vec![layer],
        }
    }

    fn push_layer(&mut self, layer: ScLayer<G, Idx>) {
        self.gate_count += layer.len();
        self.interactive_count += layer.interactive.len();
        self.layers.push(layer);
    }

    fn truncate(&mut self, len: usize) {
        let removed_gates: usize = self.layers[len..].iter().map(|layer| layer.len()).sum();
        let removed_interactive_gates: usize = self.layers[len..]
            .iter()
            .map(|layer| layer.interactive_count())
            .sum();
        self.gate_count -= removed_gates;
        self.interactive_count -= removed_interactive_gates;
        self.layers.truncate(len);
    }
}

impl<'c, G: Clone, Idx: GateIdx> ExecutableScLayer<'c, G, Idx> {
    pub fn len(&self) -> usize {
        self.layer.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn interactive_count(&self) -> usize {
        self.layer.interactive_count()
    }

    fn iter<'this>(&'this self) -> impl Iterator<Item = (G, GateId<Idx>)> + Clone + 'c
    where
        'c: 'this,
    {
        self.non_interactive_iter().chain(self.interactive_iter())
    }

    fn iter_with_parents(
        self,
        circ: &'c Circuit<G, Idx>,
    ) -> impl Iterator<
        Item = (
            G,
            SubCircuitGate<Idx>,
            impl Iterator<Item = SubCircuitGate<Idx>> + 'c,
        ),
    > + 'c {
        let sc_id = self.sc_id;
        let non_interactive = self.non_interactive_iter().zip(
            self.clone()
                .non_interactive_parents_iter(circ)
                .map(Either::Left),
        );
        let interactive = self
            .interactive_iter()
            .zip(self.interactive_parents_iter(circ).map(Either::Right));
        non_interactive
            .chain(interactive)
            .map(move |((g, idx), parents)| (g, SubCircuitGate::new(sc_id, idx), parents))
    }

    pub fn interactive_iter(&self) -> impl Iterator<Item = (G, GateId<Idx>)> + Clone + 'c {
        self.layer
            .interactive
            .gates
            .iter()
            .cloned()
            .zip(self.layer.interactive.idx.iter().cloned())
    }

    pub fn interactive_gates(&self) -> &'c [G] {
        &self.layer.interactive.gates
    }

    pub fn non_interactive_gates(&self) -> &'c [G] {
        &self.layer.non_interactive.gates
    }

    pub fn interactive_indices(&self) -> &'c [GateId<Idx>] {
        bytemuck::cast_slice(&self.layer.interactive.idx[..])
    }

    pub fn non_interactive_indices(&self) -> &'c [GateId<Idx>] {
        bytemuck::cast_slice(&self.layer.non_interactive.idx[..])
    }

    pub fn interactive_parents_iter(
        self,
        circ: &'c Circuit<G, Idx>,
    ) -> impl Iterator<Item = impl Iterator<Item = SubCircuitGate<Idx>> + 'c> + 'c {
        let sc_id = self.sc_id;
        self.layer
            .interactive
            .incoming
            .iter()
            .zip(self.layer.interactive.potential_cross_circ.iter().by_vals())
            .enumerate()
            .map(move |(idx, (inc_range, cross_circuit))| {
                if cross_circuit {
                    let gate = SubCircuitGate::new(sc_id, self.layer.interactive.idx[idx]);
                    Either::Left(circ.cross_circuit_incoming.parent_gates(gate))
                } else {
                    Either::Right(
                        self.layer.incoming_idx[inc_range.start.index()..inc_range.end.index()]
                            .iter()
                            .map(move |id| SubCircuitGate::new(sc_id, *id)),
                    )
                }
            })
    }

    #[inline]
    pub fn non_interactive_iter(&self) -> impl Iterator<Item = (G, GateId<Idx>)> + Clone + 'c {
        self.layer
            .non_interactive
            .gates
            .iter()
            .cloned()
            .zip(self.layer.non_interactive.idx.iter().cloned())
    }

    #[inline]
    pub fn non_interactive_parents_iter(
        self,
        circ: &'c Circuit<G, Idx>,
    ) -> impl Iterator<Item = impl Iterator<Item = SubCircuitGate<Idx>> + 'c> + 'c {
        let sc_id = self.sc_id;
        self.layer
            .non_interactive
            .incoming
            .iter()
            .zip(
                self.layer
                    .non_interactive
                    .potential_cross_circ
                    .iter()
                    .by_vals(),
            )
            .enumerate()
            .map(move |(idx, (inc_range, cross_circuit))| {
                if cross_circuit {
                    let gate = SubCircuitGate::new(sc_id, self.layer.non_interactive.idx[idx]);
                    Either::Left(circ.cross_circuit_incoming.parent_gates(gate))
                } else {
                    Either::Right(
                        self.layer.incoming_idx[inc_range.start.index()..inc_range.end.index()]
                            .iter()
                            .map(move |id| SubCircuitGate::new(sc_id, *id)),
                    )
                }
            })
    }
}

impl<G, Idx: GateIdx> ScLayer<G, Idx> {
    pub fn len(&self) -> usize {
        self.interactive.len() + self.non_interactive.len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn interactive_count(&self) -> usize {
        self.interactive.len()
    }

    fn from_base_layer<P>(
        sc_id: CircuitId,
        base_layer: base_circuit::CircuitLayer<G, Idx>,
        circ: &super::dyn_layers::Circuit<P, G, Idx>,
    ) -> Self
    where
        G: Gate<P>,
    {
        let incoming_cnt_guess =
            (base_layer.interactive_len() + base_layer.non_interactive_len()) * 2;
        let mut incoming_idx = Vec::with_capacity(incoming_cnt_guess);
        let non_interactive = ScLayerGates::from_base(
            sc_id,
            (
                base_layer.non_interactive_gates,
                base_layer.non_interactive_ids,
            ),
            &mut incoming_idx,
            circ,
        );
        let interactive = ScLayerGates::from_base(
            sc_id,
            (base_layer.interactive_gates, base_layer.interactive_ids),
            &mut incoming_idx,
            circ,
        );

        Self {
            non_interactive,
            interactive,
            incoming_idx,
            // _plain: PhantomData,
        }
    }
}

impl<G, Idx> ScLayerGates<G, Idx> {
    fn len(&self) -> usize {
        self.gates.len()
    }
}

impl<G, Idx: GateIdx> ScLayerGates<G, Idx> {
    fn from_base<P>(
        sc_id: CircuitId,
        (gates, idx): (Vec<G>, Vec<GateId<Idx>>),
        incoming_idx: &mut Vec<GateId<Idx>>,
        circ: &super::dyn_layers::Circuit<P, G, Idx>,
    ) -> Self
    where
        G: Gate<P>,
    {
        let mut incoming = Vec::with_capacity(gates.len());
        let mut potential_cross_circ = BitVec::with_capacity(gates.len());

        for id in &idx {
            let start = Idx::new(incoming_idx.len());
            incoming_idx.extend(circ.get_circ(sc_id).parent_gates(*id));
            let end = Idx::new(incoming_idx.len());
            incoming.push(start..end);
            let has_cross_connection = circ
                .connections
                .parent_gates(SubCircuitGate::new(sc_id, *id))
                .next()
                .is_some();
            potential_cross_circ.push(has_cross_connection);
        }

        Self {
            gates,
            idx,
            incoming,
            potential_cross_circ,
        }
    }
}

impl<G: Clone, Idx: GateIdx> ScLayerGates<G, Idx> {
    fn iter(&self) -> impl Iterator<Item = (G, GateId<Idx>)> + Clone + '_ {
        self.gates.iter().cloned().zip(self.idx.iter().cloned())
    }
}

impl<'a, G, Idx> LayerIterator<'a, G, Idx> {
    pub fn new(circ: &'a Circuit<G, Idx>) -> Self {
        let layer_iter_state = vec![0; circ.sc_map.len()];
        Self {
            circ,
            layer_iter_state,
            current_layer: 0,
        }
    }
}

#[derive(Debug, Clone)]
pub struct ScLayerIterator<'c, G, Idx> {
    circ: &'c Circuit<G, Idx>,
    sc_layer: &'c [CircuitId],
    layer_iter_state: Vec<usize>,
    skip_simd: bool,
    skip_scalar: bool,
}

impl<'c, G: Clone, Idx: Clone> ScLayerIterator<'c, G, Idx> {
    /// Returns two iterators (scalar, simd) which only return layers from either scalar or simd
    /// sc's
    pub(crate) fn split_simd(self) -> (Self, Self) {
        let mut scalar = self.clone();
        scalar.skip_simd = true;
        let mut simd = self;
        simd.skip_scalar = true;
        (scalar, simd)
    }
}

impl<'c, G: Clone, Idx: GateIdx> ScLayerIterator<'c, G, Idx> {
    pub(crate) fn interactive_count_times_simd(&self) -> usize {
        self.clone()
            .map(|layer| {
                let simd_size = self
                    .circ
                    .get_circ(layer.sc_id)
                    .simd_size
                    .map(|s| s.get())
                    .unwrap_or(1);
                layer.interactive_count() * simd_size
            })
            .sum()
    }
}

impl<'c, G, Idx: GateIdx> Iterator for ScLayerIterator<'c, G, Idx> {
    type Item = ExecutableScLayer<'c, G, Idx>;

    fn next(&mut self) -> Option<Self::Item> {
        let sc_idx = *self.sc_layer.first()?;
        let sc = self.circ.get_circ(sc_idx);
        self.sc_layer = &self.sc_layer[1..];
        // TODO this recursion could lead to a stack overflow for many parallel sc's
        match (sc.simd_size(), self.skip_scalar, self.skip_simd) {
            (None, true, _) => return self.next(),
            (Some(_), _, true) => return self.next(),
            _ => (),
        }
        let layer = sc.layers.get(self.layer_iter_state[sc_idx as usize]);
        layer.map(|layer| ExecutableScLayer {
            sc_id: sc_idx,
            layer,
        })
    }
}

impl<'c, G, Idx: GateIdx> Iterator for LayerIterator<'c, G, Idx> {
    type Item = ScLayerIterator<'c, G, Idx>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_layer >= self.circ.layers.len() {
            return None;
        }
        let sc_layer = &self.circ.layers[self.current_layer].sub_circuit_layers;
        let layer_iter_state = self.layer_iter_state.clone();
        for sc in sc_layer {
            self.layer_iter_state[*sc as usize] += 1;
        }
        self.current_layer += 1;
        Some(ScLayerIterator {
            circ: self.circ,
            sc_layer,
            layer_iter_state,
            skip_simd: false,
            skip_scalar: false,
        })
    }
}