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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]

//! This crate contains the types for manipulating the intermediate representation
//! for Sunscreen's compiler backend.

mod error;
mod literal;
mod operation;
mod validation;

use petgraph::{
    algo::is_isomorphic_matching,
    algo::toposort,
    algo::tred::*,
    dot::Dot,
    graph::{Graph, NodeIndex},
    stable_graph::{Edges, Neighbors, StableGraph},
    visit::{IntoNeighbors, IntoNodeIdentifiers},
    Directed, Direction,
};
use serde::{Deserialize, Serialize};

pub use error::*;
pub use literal::*;
pub use operation::*;
pub use seal_fhe::SecurityLevel;

use IRTransform::*;
use TransformNodeIndex::*;

use std::collections::HashSet;

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
/**
 * Sunscreen supports the BFV scheme.
 */
pub enum SchemeType {
    /**
     *
     * # Remarks
     * [BFV](https://eprint.iacr.org/2012/144.pdf) is a leveled scheme on polynomials in a cyclotomic
     * ring. The coefficients of a plaintext form a 2x(N/2) matrix (where N is the polynomial degree).
     * Sunscreen automatically chooses the polynomial degree depending on the FHE program. Each coefficient is
     * an integer mod p (p is a scheme parameter and is the plaintext modulus). One can encode several different
     * meanings onto these coefficients:
     *
     * * An integer x modulo p by setting the x^0 term to x and the remaining terms to 0 (i.e. scalar encoder).
     * This encoding requires p be the desired maximum representable value. Overflow causes wrapping as
     * one would expect. This encoding is generally inefficient.
     * * An integer x decomposed into digits, where each digit is a coefficient in the plaintext polynomial.
     * One may represent numbers larger than p with this technique. P should be chosen to accomodate the number
     * of operations one wishes to perform so that no digit overflows under addition and multiplication. Overflow
     * causes weird answers. Since this encoding typically allows for a smaller plaintext modulo, Sunscreen
     * can choose parameters that result in low latency.
     * * A 2x(N/2) Batched vector of integers modulo p. Overflow wraps lane-wise, as expected. This encoding
     * generally maximizes throughput when calulating many numbers. While the representation forms a matrix,
     * multiplication and addition both execute element-wise; multiplication is *not* defined as matrix multiplation.
     * This Batched computation is also referred to on the literature as batching.
     *
     * Each of these encoding schemes supports both signed and unsigned values.
     *
     * Under BFV, each homomorphic operation introduces noise, with ciphertext-ciphertext multiplication
     * creating the most by a quadratic margin. Additionally, multiplication is the slowest operation. To
     * reduce noise under repeated multiplications, Sunscreen will automatically insert relinearization operations.
     *
     * After some number of operations (parameter-dependent), ciphertexts contain too much noise and
     * decryption results in garbled data. Sunscreen automatically chooses the parameters to accomodate
     * the noise growth in a given FHE program at the expense of execution speed.
     *
     * One can think of parameters as a tradeoff between accomodating more noise and faster execution. For a given security
     * level, there are several possible parameter sets. These sets are ordered from accomodating the smallest
     * level of noise to largest. Moving from one set to the next results in every operation requiring ~4x the
     * runtime, but also results in 2x the Batched lanes. Thus, when using Batched plaintexts, the amortized
     * throughput resulting from using the next parameter set is 2x lower than the previous. The smallest 2
     * parameter sets fail to even generate relinearization keys and fail to even perform a single multiplication
     * when using batching, while the largest can perform over 25 multiplications with batching.
     *
     * When using Batched, Sunscreen supports rotating column Batched lanes left and right and switching the rows
     * of the matrix.
     *
     * Pros:
     * * Most efficient way to do integer artithmetic.
     * * Exact values.
     * * Good ciphertext expansion when using batching.
     * * Galois keys (needed if FHE program does rotations or row swapping) can be compactly generated.
     * * Relinearization keys (needed if FHE program does multiplications) can be compactly generated.
     *
     * Cons:
     * * Bootstrapping not natively supported and isn't fast if one does implement it.
     * * Some operations (e.g. comparison, division) are not easy to implement and any implementation
     * will be approximate and/or particular to the scheme parameters.
     */
    Bfv,
}

impl Into<u8> for SchemeType {
    /**
     * Creates a serializable byte representation of the scheme type.
     */
    fn into(self) -> u8 {
        match self {
            Self::Bfv => 0,
        }
    }
}

impl TryFrom<u8> for SchemeType {
    type Error = Error;

    /**
     * Converts a serialized scheme type back into a [`SchemeType`].
     */
    fn try_from(val: u8) -> Result<Self> {
        Ok(match val {
            0 => Self::Bfv,
            _ => Err(Error::InvalidSchemeType)?,
        })
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
/**
 * The type of output from an Fhe Program's graph node.
 */
pub enum OutputType {
    /**
     * The output is a plaintext.
     */
    Plaintext,

    /**
     * The output is a ciphertext.
     */
    Ciphertext,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
/**
 * Contains information about a node in the FHE program graph.
 */
pub struct NodeInfo {
    /**
     * The operation this node represents.
     */
    pub operation: Operation,
}

impl ToString for NodeInfo {
    fn to_string(&self) -> String {
        format!("{:#?}", self.operation)
    }
}

impl NodeInfo {
    /**
     * Creates a new NodeInfo from the given operation.
     */
    pub fn new(operation: Operation) -> Self {
        Self { operation }
    }

    /**
     * Gets the output type for the current node.
     */
    pub fn output_type(&self) -> OutputType {
        match self.operation {
            Operation::InputPlaintext(_) => OutputType::Plaintext,
            Operation::Literal(_) => OutputType::Plaintext,
            _ => OutputType::Ciphertext,
        }
    }
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
/**
 * Contains information about an edge between nodes in the FHE program graph.
 */
pub enum EdgeInfo {
    /**
     * The source node is the left input to a binary operation.
     */
    LeftOperand,

    /**
     * The source node is the right input to fa binary operation.
     */
    RightOperand,

    /**
     * The source node is the single input to a unary operation.
     */
    UnaryOperand,
}

type IRGraph = StableGraph<NodeInfo, EdgeInfo>;

#[derive(Debug, Clone, Serialize, Deserialize)]
/**
 * The intermediate representation for an FHE program used in the compiler back-end.
 *
 * Other modules may transform these using the [forward_traverse](`Self::forward_traverse`)
 * and [reverse_traverse](`Self::reverse_traverse`) methods, or iterate over the graph
 * member for analysis or execution.
 *
 * The graph construction methods `append_*` take NodeIndex types as arguments. These
 * indices must refer to other nodes in the graph.
 */
pub struct FheProgram {
    /**
     * The scheme type this FHE program will run under.
     */
    pub scheme: SchemeType,

    /**
     * The underlying dependency graph.
     */
    pub graph: IRGraph,
}

impl PartialEq for FheProgram {
    fn eq(&self, b: &Self) -> bool {
        is_isomorphic_matching(
            &Graph::from(self.graph.clone()),
            &Graph::from(b.graph.clone()),
            |n1, n2| n1 == n2,
            |e1, e2| e1 == e2,
        )
    }
}

impl FheProgram {
    /**
     * Create a new new empty intermediate representation.
     */
    pub fn new(scheme: SchemeType) -> Self {
        Self {
            scheme,
            graph: StableGraph::new(),
        }
    }

    /**
     * Write this graph into graphviz dot format. The returned
     * string contains the file's contents.
     */
    pub fn render(&self) -> String {
        let data = Dot::with_attr_getters(
            &self.graph,
            &[
                petgraph::dot::Config::NodeNoLabel,
                petgraph::dot::Config::EdgeNoLabel,
            ],
            &|_, e| format!("label=\"{:?}\"", e.weight()),
            &|_, n| {
                let (index, info) = n;

                match info.operation {
                    Operation::Literal(Literal::Plaintext(_)) => {
                        format!("label=\"Id: {} Op: Plaintext literal\"", index.index())
                    }
                    _ => {
                        format!("label=\"Id: {} Op: {:?}\"", index.index(), info)
                    }
                }
            },
        );

        format!("{:?}", data)
    }

    fn append_2_input_node(
        &mut self,
        operation: Operation,
        x: NodeIndex,
        y: NodeIndex,
    ) -> NodeIndex {
        let new_node = self.graph.add_node(NodeInfo::new(operation));

        self.graph.update_edge(x, new_node, EdgeInfo::LeftOperand);
        self.graph.update_edge(y, new_node, EdgeInfo::RightOperand);

        new_node
    }

    fn append_1_input_node(&mut self, operation: Operation, x: NodeIndex) -> NodeIndex {
        let new_node = self.graph.add_node(NodeInfo::new(operation));

        self.graph.update_edge(x, new_node, EdgeInfo::UnaryOperand);

        new_node
    }

    fn append_0_input_node(&mut self, operation: Operation) -> NodeIndex {
        let new_node = self.graph.add_node(NodeInfo::new(operation));

        new_node
    }

    /**
     * Appends a negate operation that depends on operand `x`.
     */
    pub fn append_negate(&mut self, x: NodeIndex) -> NodeIndex {
        self.append_1_input_node(Operation::Negate, x)
    }

    /**
     * Appends a multiply operation that depends on the operands `x` and `y`.
     */
    pub fn append_multiply(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::Multiply, x, y)
    }

    /**
     * Appends a multiply operation that depends on the operands `x` and `y`.
     */
    pub fn append_multiply_plaintext(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::MultiplyPlaintext, x, y)
    }

    /**
     * Appends an add operation that depends on the operands `x` and `y`.
     */
    pub fn append_add(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::Add, x, y)
    }

    /**
     * Appends a subtract operation that depends on the operands `x` and `y`.
     */
    pub fn append_sub(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::Sub, x, y)
    }

    /**
     * Appends an input ciphertext with the given name.
     */
    pub fn append_input_ciphertext(&mut self, id: usize) -> NodeIndex {
        self.append_0_input_node(Operation::InputCiphertext(id))
    }

    /**
     * Appends an input plaintext with the given name.
     */
    pub fn append_input_plaintext(&mut self, id: usize) -> NodeIndex {
        self.append_0_input_node(Operation::InputPlaintext(id))
    }

    /**
     * Appends a constant literal unencrypted.
     *
     * * `value`: The integer or floating-point value in the literal.
     */
    pub fn append_input_literal(&mut self, value: Literal) -> NodeIndex {
        self.append_0_input_node(Operation::Literal(value))
    }

    /**
     * Sppends a node designating `x` as an output of the FHE program.
     */
    pub fn append_output_ciphertext(&mut self, x: NodeIndex) -> NodeIndex {
        self.append_1_input_node(Operation::OutputCiphertext, x)
    }

    /**
     * Appends an operation that relinearizes `x`.
     */
    pub fn append_relinearize(&mut self, x: NodeIndex) -> NodeIndex {
        self.append_1_input_node(Operation::Relinearize, x)
    }

    /**
     * Appends an operation that rotates ciphertext `x` left by the literal node at `y` places.
     *
     * # Remarks
     * Recall that BFV has 2 rows in a Batched vector. This rotates each row.
     * CKKS has one large vector.
     */
    pub fn append_rotate_left(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::ShiftLeft, x, y)
    }

    /**
     * Appends an operation that rotates ciphertext `x` right by the literal node at `y` places.
     *      
     * # Remarks
     * Recall that BFV has 2 rows in a Batched vector. This rotates each row.
     * CKKS has one large vector.
     */
    pub fn append_rotate_right(&mut self, x: NodeIndex, y: NodeIndex) -> NodeIndex {
        self.append_2_input_node(Operation::ShiftRight, x, y)
    }

    /**
     * A specialized topological DAG traversal that allows the following graph
     * mutations during traversal:
     * * Delete the current node
     * * Insert nodoes after current node
     * * Add new nodes with no dependencies
     *
     * Any other graph mutation will likely result in unvisited nodes.
     *
     * * `callback`: A closure that receives the current node index and an object allowing
     *   you to make graph queryes. This closure returns a transform list.
     *   [`forward_traverse`](Self::forward_traverse) will apply these transformations
     *   before continuing the traversal.
     */
    pub fn forward_traverse<F>(&mut self, callback: F)
    where
        F: FnMut(GraphQuery, NodeIndex) -> TransformList,
    {
        self.traverse(true, callback);
    }

    /**
     * A specialized reverse topological DAG traversal that allows the following graph
     * mutations during traversal:
     * * Delete the current node
     * * Insert nodoes after current node
     * * Add new nodes with no dependencies
     *
     * Any other graph mutation will likely result in unvisited nodes.
     *
     * * `callback`: A closure that receives the current node index and an object allowing
     *   you to make graph queryes. This closure returns a transform list.
     *   [`reverse_traverse`](Self::reverse_traverse) will apply these transformations
     *   before continuing the traversal.
     */
    pub fn reverse_traverse<F>(&mut self, callback: F)
    where
        F: FnMut(GraphQuery, NodeIndex) -> TransformList,
    {
        self.traverse(false, callback);
    }

    /**
     * Remove the given node.
     */
    pub fn remove_node(&mut self, id: NodeIndex) {
        self.graph.remove_node(id);
    }

    fn traverse<F>(&mut self, forward: bool, mut callback: F)
    where
        F: FnMut(GraphQuery, NodeIndex) -> TransformList,
    {
        let mut ready: HashSet<NodeIndex> = HashSet::new();
        let mut visited: HashSet<NodeIndex> = HashSet::new();
        let prev_direction = if forward {
            Direction::Incoming
        } else {
            Direction::Outgoing
        };
        let next_direction = if forward {
            Direction::Outgoing
        } else {
            Direction::Incoming
        };

        let mut ready_nodes: Vec<NodeIndex> = self
            .graph
            .node_identifiers()
            .filter(|&x| {
                self.graph
                    .neighbors_directed(x, prev_direction)
                    .next()
                    .is_none()
            })
            .collect();

        for i in &ready_nodes {
            ready.insert(*i);
        }

        while let Some(n) = ready_nodes.pop() {
            visited.insert(n);

            // Remember the next nodes from the current node in case it gets deletes.
            let next_nodes: Vec<NodeIndex> =
                self.graph.neighbors_directed(n, next_direction).collect();

            let mut transforms = callback(GraphQuery(self), n);

            // Apply the transforms the callback produced
            transforms.apply(self);

            let node_ready = |n: NodeIndex| {
                self.graph
                    .neighbors_directed(n, prev_direction)
                    .all(|m| visited.contains(&m))
            };

            // If the node still exists, push all its ready dependents
            if self.graph.contains_node(n) {
                for i in self.graph.neighbors_directed(n, next_direction) {
                    if !ready.contains(&i) && node_ready(i) {
                        ready.insert(i);
                        ready_nodes.push(i);
                    }
                }
            }

            // Iterate through the next nodes that existed before visitin this node.
            for i in next_nodes {
                if !ready.contains(&i) && node_ready(i) {
                    ready.insert(i);
                    ready_nodes.push(i);
                }
            }

            // Iterate through any sources/sinks the callback may have added.
            let sources = self.graph.node_identifiers().filter(|&x| {
                self.graph
                    .neighbors_directed(x, prev_direction)
                    .next()
                    .is_none()
            });

            for i in sources {
                if !ready.contains(&i) {
                    ready.insert(i);
                    ready_nodes.push(i);
                }
            }
        }
    }

    /**
     * Returns the node indices of output ciphertexts
     */
    pub fn get_outputs(&self) -> impl Iterator<Item = NodeIndex> + '_ {
        self.graph
            .node_indices()
            .filter(|g| match self.graph[*g].operation {
                Operation::OutputCiphertext => true,
                _ => false,
            })
    }

    /**
     * Returns the number of inputs ciphertexts this FHE program takes.
     */
    pub fn num_inputs(&self) -> usize {
        self.graph
            .node_weights()
            .filter(|n| {
                if let Operation::InputCiphertext(_) = n.operation {
                    true
                } else {
                    false
                }
            })
            .count()
    }

    /**
     * Runs tree shaking and returns a derived FheProgram with only
     * dependencies required to run the requested nodes.
     *
     * * `nodes`: indices specifying a set of nodes in the graph. Prune return a new
     *   [`FheProgram`] containing nodes in the transitive closure
     *   of this set.
     */
    pub fn prune(&self, nodes: &[NodeIndex]) -> FheProgram {
        let mut compact_graph = Graph::from(self.graph.clone());
        compact_graph.reverse();

        let topo = toposort(&compact_graph, None).unwrap();
        let (res, revmap) = dag_to_toposorted_adjacency_list(&compact_graph, &topo);
        let (_, closure) = dag_transitive_reduction_closure(&res);

        let mut closure_set = HashSet::new();

        let mut visit: Vec<NodeIndex> = vec![];

        for n in nodes {
            let mapped_id = revmap[n.index()];
            visit.push(mapped_id);
            closure_set.insert(mapped_id);
        }

        while visit.len() > 0 {
            let node = visit.pop().expect("Fatal error: prune queue was empty.");

            for edge in closure.neighbors(node) {
                if !closure_set.contains(&edge) {
                    closure_set.insert(edge);
                    visit.push(edge);
                }
            }
        }

        compact_graph.reverse();

        let pruned = compact_graph.filter_map(
            |id, n| {
                // Don't prune input nodes.
                let is_input = match n.operation {
                    Operation::InputPlaintext(_) => true,
                    Operation::InputCiphertext(_) => true,
                    _ => false,
                };

                if closure_set.contains(&revmap[id.index()]) || is_input {
                    Some(n.clone())
                } else {
                    None
                }
            },
            |_, e| Some(e.clone()),
        );

        Self {
            scheme: self.scheme,
            graph: StableGraph::from(pruned),
        }
    }

    /**
     * Validates this [`FheProgram`] for correctness.
     */
    pub fn validate(&self) -> Result<()> {
        let errors = validation::validate_ir(self);

        if errors.len() > 0 {
            return Err(Error::IRError(errors));
        }

        Ok(())
    }

    /**
     * Whether or not this FHE program needs relin keys to run. Needed for relinearization.
     */
    pub fn requires_relin_keys(&self) -> bool {
        self.graph.node_weights().any(|n| {
            if let Operation::Relinearize = n.operation {
                true
            } else {
                false
            }
        })
    }

    /**
     * Whether or not this FHE program requires Galois keys to run. Needed for rotation and row swap
     * operations.
     */
    pub fn requires_galois_keys(&self) -> bool {
        self.graph.node_weights().any(|n| match n.operation {
            Operation::ShiftRight => true,
            Operation::ShiftLeft => true,
            Operation::SwapRows => true,
            _ => false,
        })
    }
}

/**
 * A wrapper for ascertaining the structure of the underlying [`FheProgram`].
 * This type is used in [`FheProgram::forward_traverse`] and
 * [`FheProgram::reverse_traverse`] callbacks.
 */
pub struct GraphQuery<'a>(&'a FheProgram);

impl<'a> GraphQuery<'a> {
    /**
     * Creates a new [`GraphQuery`] from a reference to an [`FheProgram`].
     */
    pub fn new(ir: &'a FheProgram) -> Self {
        Self(ir)
    }

    /**
     * Returns the [`NodeInfo`] for the graph node with the given index `x`.
     */
    pub fn get_node(&self, x: NodeIndex) -> &NodeInfo {
        &self.0.graph[x]
    }

    /**
     * Returns the children or parents of the node with index `x`.` If direction is
     * [`Direction::Outgoing`], this will return the children. If the direction is
     * [`Direction::Incoming`], this will return the parents.
     *
     * Typically, you want children writing forward traversal compiler passes and
     * parents when writing reverse traversal compiler passes.
     */
    pub fn neighbors_directed(&self, x: NodeIndex, direction: Direction) -> Neighbors<EdgeInfo> {
        self.0.graph.neighbors_directed(x, direction)
    }

    /**
     * Returns incoming our outgoing edges for the node with index `x`.`
     *
     * Typically, you want outgoing writing forward traversal compiler passes and
     * incoming when writing reverse traversal compiler passes.
     */
    pub fn edges_directed(&self, x: NodeIndex, direction: Direction) -> Edges<EdgeInfo, Directed> {
        self.0.graph.edges_directed(x, direction)
    }
}

#[derive(Debug, Clone)]
/**
 * A transform for an [`FheProgram`]. Callbacks in
 * [`FheProgram::forward_traverse`] and
 * [`FheProgram::reverse_traverse`] should emit these to update the
 * graph.
 *
 * Each of these variants use a [`TransformNodeIndex`] to reference either a node that
 * currently exists in the graph (i.e. [`TransformNodeIndex::NodeIndex`]), or a node that
 * will result from a previous transform in the [`TransformList`]. I.e. [`TransformNodeIndex::DeferredIndex`]
 */
pub enum IRTransform {
    /**
     * Appends an add node.
     */
    AppendAdd(TransformNodeIndex, TransformNodeIndex),

    /**
     * Appends a multiply node.
     */
    AppendMultiply(TransformNodeIndex, TransformNodeIndex),

    /**
     * Appends an input ciphertext
     */
    AppendInputCiphertext(usize),

    /**
     * Appends an input plaintext
     */
    AppendInputPlaintext(usize),

    /**
     * Appends an output ciphertext node.
     */
    AppendOutputCiphertext(TransformNodeIndex),

    /**
     * Appends a relinearize node.
     */
    AppendRelinearize(TransformNodeIndex),

    /**
     * Appends a subtract node.
     */
    AppendSub(TransformNodeIndex, TransformNodeIndex),

    /**
     * Removes a node.
     */
    RemoveNode(TransformNodeIndex),

    /**
     * Appends a negate node.
     */
    AppendNegate(TransformNodeIndex),

    /**
     * Remove a graph edge between two nodes..
     */
    RemoveEdge(TransformNodeIndex, TransformNodeIndex),

    /**
     * Add a graph edge between two nodes.
     */
    AddEdge(TransformNodeIndex, TransformNodeIndex, EdgeInfo),
}

/**
 * Transforms can refer to nodes that already exist in the graph or nodes that don't
 * yet exist in the graph, but will be inserted in a previous transform.
 */
#[derive(Debug, Clone, Copy)]
pub enum TransformNodeIndex {
    /**
     * This node index refers to a pre-existing node in the graph.
     */
    NodeIndex(NodeIndex),

    /**
     * This node index refers to a node in the [`TransformList`] that has not yet been
     * added to the graph.
     */
    DeferredIndex(DeferredIndex),
}

/**
 * The index type of a node that exists in a transform list, but does not yet exist in
 * the intermediate representation graph.
 */
pub type DeferredIndex = usize;

impl Into<TransformNodeIndex> for DeferredIndex {
    fn into(self) -> TransformNodeIndex {
        TransformNodeIndex::DeferredIndex(self)
    }
}

impl Into<TransformNodeIndex> for NodeIndex {
    fn into(self) -> TransformNodeIndex {
        TransformNodeIndex::NodeIndex(self)
    }
}

/**
 * A list of tranformations to be applied to the [`FheProgram`] graph.
 */
pub struct TransformList {
    transforms: Vec<IRTransform>,
    inserted_node_ids: Vec<Option<NodeIndex>>,
}

impl Default for TransformList {
    fn default() -> Self {
        Self::new()
    }
}

impl TransformList {
    /**
     * Creates an empty transform list.
     */
    pub fn new() -> Self {
        Self {
            transforms: vec![],
            inserted_node_ids: vec![],
        }
    }

    /**
     * Pushes a transform into the list and returns the index of the pushed transform
     * suitable for use in [`TransformNodeIndex::DeferredIndex`].
     */
    pub fn push(&mut self, transform: IRTransform) -> DeferredIndex {
        self.transforms.push(transform);

        self.transforms.len() - 1
    }

    /**
     * Applies every transform in the list to the given graph. Resoves any deferred
     * indices after placing nodes in the graph.
     *
     * # Panics
     * If any deferred index is out of bounds or refers to a previous operation that didn't
     * result in a node being added, this function will panic. For example, if an [`IRTransform::AppendAdd`]
     * refers to the index of a [`IRTransform::RemoveEdge`] transform, a panic will result.
     */
    pub fn apply(&mut self, ir: &mut FheProgram) {
        for t in self.transforms.clone().iter() {
            let inserted_node_id = match t {
                AppendAdd(x, y) => {
                    self.apply_2_input(ir, *x, *y, |ir, x, y| Some(ir.append_add(x, y)))
                }
                AppendMultiply(x, y) => {
                    self.apply_2_input(ir, *x, *y, |ir, x, y| Some(ir.append_multiply(x, y)))
                }
                AppendInputCiphertext(id) => Some(ir.append_input_ciphertext(*id)),
                AppendInputPlaintext(id) => Some(ir.append_input_plaintext(*id)),
                AppendOutputCiphertext(x) => {
                    self.apply_1_input(ir, *x, |ir, x| Some(ir.append_output_ciphertext(x)))
                }
                AppendRelinearize(x) => {
                    self.apply_1_input(ir, *x, |ir, x| Some(ir.append_relinearize(x)))
                }
                AppendSub(x, y) => {
                    self.apply_2_input(ir, *x, *y, |ir, x, y| Some(ir.append_sub(x, y)))
                }
                RemoveNode(x) => {
                    let x = self.materialize_index(*x);

                    ir.remove_node(x);

                    None
                }
                AppendNegate(x) => self.apply_1_input(ir, *x, |ir, x| Some(ir.append_negate(x))),
                RemoveEdge(x, y) => {
                    let x = self.materialize_index(*x);
                    let y = self.materialize_index(*y);

                    ir.graph.remove_edge(
                        ir.graph
                            .find_edge(x, y)
                            .expect("Fatal error: attempted to remove nonexistent edge."),
                    );

                    None
                }
                AddEdge(x, y, edge_info) => {
                    let x = self.materialize_index(*x);
                    let y = self.materialize_index(*y);

                    ir.graph.add_edge(x, y, *edge_info);

                    None
                }
            };

            self.inserted_node_ids.push(inserted_node_id);
        }
    }

    fn apply_1_input<F>(
        &mut self,
        ir: &mut FheProgram,
        x: TransformNodeIndex,
        callback: F,
    ) -> Option<NodeIndex>
    where
        F: FnOnce(&mut FheProgram, NodeIndex) -> Option<NodeIndex>,
    {
        let x = self.materialize_index(x);

        callback(ir, x)
    }

    fn apply_2_input<F>(
        &mut self,
        ir: &mut FheProgram,
        x: TransformNodeIndex,
        y: TransformNodeIndex,
        callback: F,
    ) -> Option<NodeIndex>
    where
        F: FnOnce(&mut FheProgram, NodeIndex, NodeIndex) -> Option<NodeIndex>,
    {
        let x = self.materialize_index(x);
        let y = self.materialize_index(y);

        callback(ir, x, y)
    }

    fn materialize_index(&self, x: TransformNodeIndex) -> NodeIndex {
        match x {
            NodeIndex(x) => x,
            DeferredIndex(x) => self.inserted_node_ids[x]
                .expect(&format!("Fatal error: No such deferred node index :{}", x)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_simple_dag() -> FheProgram {
        let mut ir = FheProgram::new(SchemeType::Bfv);

        let ct = ir.append_input_ciphertext(0);
        let l1 = ir.append_input_literal(Literal::from(7i64));
        let add = ir.append_add(ct, l1);
        let l2 = ir.append_input_literal(Literal::from(5u64));
        ir.append_multiply(add, l2);

        ir
    }

    #[test]
    fn can_build_simple_dag() {
        let ir = create_simple_dag();

        assert_eq!(ir.graph.node_count(), 5);

        let nodes = ir
            .graph
            .node_identifiers()
            .map(|i| (i, &ir.graph[i]))
            .collect::<Vec<(NodeIndex, &NodeInfo)>>();

        assert_eq!(nodes[0].1.operation, Operation::InputCiphertext(0));
        assert_eq!(
            nodes[1].1.operation,
            Operation::Literal(Literal::from(7i64))
        );
        assert_eq!(nodes[2].1.operation, Operation::Add);
        assert_eq!(
            nodes[3].1.operation,
            Operation::Literal(Literal::from(5u64))
        );
        assert_eq!(nodes[4].1.operation, Operation::Multiply);

        assert_eq!(
            ir.graph
                .neighbors_directed(nodes[0].0, Direction::Outgoing)
                .next()
                .unwrap(),
            nodes[2].0
        );
        assert_eq!(
            ir.graph
                .neighbors_directed(nodes[1].0, Direction::Outgoing)
                .next()
                .unwrap(),
            nodes[2].0
        );
        assert_eq!(
            ir.graph
                .neighbors_directed(nodes[2].0, Direction::Outgoing)
                .next()
                .unwrap(),
            nodes[4].0
        );
        assert_eq!(
            ir.graph
                .neighbors_directed(nodes[3].0, Direction::Outgoing)
                .next()
                .unwrap(),
            nodes[4].0
        );
        assert_eq!(
            ir.graph
                .neighbors_directed(nodes[4].0, Direction::Outgoing)
                .next(),
            None
        );
    }

    #[test]
    fn can_forward_traverse() {
        let mut ir = create_simple_dag();

        let mut visited = vec![];

        ir.forward_traverse(|_, n| {
            visited.push(n);
            TransformList::default()
        });

        assert_eq!(
            visited,
            vec![
                NodeIndex::from(3),
                NodeIndex::from(1),
                NodeIndex::from(0),
                NodeIndex::from(2),
                NodeIndex::from(4)
            ]
        );
    }

    #[test]
    fn can_reverse_traverse() {
        let mut ir = create_simple_dag();

        let mut visited = vec![];

        ir.reverse_traverse(|_, n| {
            visited.push(n);
            TransformList::default()
        });

        assert_eq!(
            visited,
            vec![
                NodeIndex::from(4),
                NodeIndex::from(2),
                NodeIndex::from(0),
                NodeIndex::from(1),
                NodeIndex::from(3)
            ]
        );
    }

    #[test]
    fn can_delete_during_traversal() {
        let mut ir = create_simple_dag();

        let mut visited = vec![];

        ir.reverse_traverse(|_, n| {
            visited.push(n);
            // Delete the addition
            if n.index() == 2 {
                let mut transforms = TransformList::new();
                transforms.push(RemoveNode(n.into()));

                transforms
            } else {
                TransformList::default()
            }
        });

        assert_eq!(
            visited,
            vec![
                NodeIndex::from(4),
                NodeIndex::from(2),
                NodeIndex::from(0),
                NodeIndex::from(1),
                NodeIndex::from(3)
            ]
        );
    }

    #[test]
    fn can_append_during_traversal() {
        let mut ir = create_simple_dag();

        let mut visited = vec![];

        ir.forward_traverse(|_, n| {
            visited.push(n);

            // Delete the addition
            if n.index() == 2 {
                let mut transforms = TransformList::new();
                transforms.push(AppendMultiply(n.into(), NodeIndex::from(1).into()));

                transforms
            } else {
                TransformList::default()
            }
        });

        assert_eq!(
            visited,
            vec![
                NodeIndex::from(3),
                NodeIndex::from(1),
                NodeIndex::from(0),
                NodeIndex::from(2),
                NodeIndex::from(4),
                NodeIndex::from(5),
            ]
        );
    }

    #[test]
    fn can_prune_ir() {
        let mut ir = FheProgram::new(SchemeType::Bfv);

        let ct = ir.append_input_ciphertext(0);
        let l1 = ir.append_input_literal(Literal::from(7i64));
        let add = ir.append_add(ct, l1);
        let l2 = ir.append_input_literal(Literal::from(5u64));
        ir.append_multiply(add, l2);

        let pruned = ir.prune(&vec![add]);

        let mut expected_ir = FheProgram::new(SchemeType::Bfv);
        let ct = expected_ir.append_input_ciphertext(0);
        let l1 = expected_ir.append_input_literal(Literal::from(7i64));
        expected_ir.append_add(ct, l1);

        assert_eq!(pruned, expected_ir);
    }

    #[test]
    fn can_prune_graph_with_removed_nodes() {
        let mut ir = FheProgram::new(SchemeType::Bfv);

        let ct = ir.append_input_ciphertext(0);
        let rem = ir.append_input_ciphertext(1);
        ir.remove_node(rem);
        let l1 = ir.append_input_literal(Literal::from(7i64));
        let rem = ir.append_input_ciphertext(1);
        ir.remove_node(rem);
        let add = ir.append_add(ct, l1);
        let rem = ir.append_input_ciphertext(1);
        ir.remove_node(rem);
        let l2 = ir.append_input_literal(Literal::from(5u64));
        ir.append_multiply(add, l2);
        let rem = ir.append_input_ciphertext(1);
        ir.remove_node(rem);

        let pruned = ir.prune(&vec![add]);

        let mut expected_ir = FheProgram::new(SchemeType::Bfv);
        let ct = expected_ir.append_input_ciphertext(0);
        let l1 = expected_ir.append_input_literal(Literal::from(7i64));
        expected_ir.append_add(ct, l1);

        assert_eq!(pruned, expected_ir);
    }

    #[test]
    fn can_prune_with_multiple_nodes() {
        let mut ir = FheProgram::new(SchemeType::Bfv);

        let ct1 = ir.append_input_ciphertext(0);
        let ct2 = ir.append_input_ciphertext(1);
        let ct3 = ir.append_input_ciphertext(2);
        let neg1 = ir.append_negate(ct1);
        let neg2 = ir.append_negate(ct2);
        let neg3 = ir.append_negate(ct3);
        let o1 = ir.append_output_ciphertext(neg1);
        ir.append_output_ciphertext(neg2);
        ir.append_output_ciphertext(neg3);

        let pruned = ir.prune(&vec![o1, neg2]);

        let mut expected_ir = FheProgram::new(SchemeType::Bfv);
        let ct1 = expected_ir.append_input_ciphertext(0);
        let ct2 = expected_ir.append_input_ciphertext(1);
        let _ct3 = expected_ir.append_input_ciphertext(2);
        let neg1 = expected_ir.append_negate(ct1);
        expected_ir.append_negate(ct2);
        expected_ir.append_output_ciphertext(neg1);

        assert_eq!(pruned, expected_ir);
    }

    #[test]
    fn pruning_empty_node_list_results_in_inputs_only() {
        let mut ir = FheProgram::new(SchemeType::Bfv);

        let ct1 = ir.append_input_ciphertext(0);
        let ct2 = ir.append_input_ciphertext(1);
        let ct3 = ir.append_input_ciphertext(2);
        let neg1 = ir.append_negate(ct1);
        let neg2 = ir.append_negate(ct2);
        let neg3 = ir.append_negate(ct3);
        ir.append_output_ciphertext(neg1);
        ir.append_output_ciphertext(neg2);
        ir.append_output_ciphertext(neg3);

        let pruned = ir.prune(&vec![]);

        let mut expected_ir = FheProgram::new(SchemeType::Bfv);
        let _ct1 = expected_ir.append_input_ciphertext(0);
        let _ct2 = expected_ir.append_input_ciphertext(1);
        let _ct3 = expected_ir.append_input_ciphertext(2);

        assert_eq!(pruned, expected_ir);
    }

    #[test]
    fn can_roundtrip_scheme_type() {
        for s in [SchemeType::Bfv] {
            let s_2: u8 = s.into();
            let s_2 = SchemeType::try_from(s_2).unwrap();

            assert_eq!(s, s_2);
        }
    }
}