Browse Source

0.04

master
Your Name 2 weeks ago
parent
commit
588bfe15fe
1 changed files with 17 additions and 45 deletions
  1. 17
    45
      src/circuit.rs

+ 17
- 45
src/circuit.rs View File

82
 }
82
 }
83
 
83
 
84
 fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate> {
84
 fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate> {
85
-    let mut indices: Vec<usize> = vec![0; n];
86
-    let mut all_gates: Vec<Gate> = Vec::with_capacity(1+3*n);
85
+    let mut all_gates: Vec<Gate> = Vec::with_capacity(3*n);
87
     let mut and_gate_indices: Vec<usize> = vec![0; n];
86
     let mut and_gate_indices: Vec<usize> = vec![0; n];
87
+    let mut eq_gate_indices: Vec<usize> = Vec::with_capacity(n-1);
88
 
88
 
89
-    for curr in 0..n {
90
-        // Gate(A_current > B_current)
91
-        let gt_gate = Gate::new(GateType::Bigger, vec![], vec![curr, curr + n]);
92
-        let gt_gate_index = all_gates.len();
93
-        all_gates.push(gt_gate);
94
-
95
-        // print!("( ({}) ", format!("{} > {}", format!("A{}", n-1-curr), format!("B{}", n-1-curr)));
89
+    const EMPTY_VEC: Vec<usize> = Vec::new();
96
 
90
 
97
-        let mut current_bit_gate_indices: Vec<usize> = Vec::with_capacity(curr+1);
98
-        current_bit_gate_indices.push(gt_gate_index);
91
+    for curr in 0..n {
92
+        // Gate(A_curr > B_curr)
93
+        let mut and_gate_input_indices = vec!(all_gates.len());
94
+        println!("GT = {}", all_gates.len());
95
+        all_gates.push(Gate::new(GateType::Bigger, EMPTY_VEC, vec![curr, curr + n]));
99
 
96
 
100
         // Bit to the left of curr. The one at array-index 0 doesn't have one.
97
         // Bit to the left of curr. The one at array-index 0 doesn't have one.
101
         if curr != 0 {
98
         if curr != 0 {
102
-            // Gate(A_i = B_i)
103
-            let eq_gate = Gate::new(GateType::Equal, vec![], vec![curr - 1, curr - 1 + n]);
104
-            // remember which key-index (see below) this bit belongs to.
105
-            indices[n - curr] = all_gates.len();
106
-            all_gates.push(eq_gate);
99
+            // Gate(A_curr-1 = B_curr-1)
100
+            eq_gate_indices.push(all_gates.len());
101
+            println!("EQ = {}", all_gates.len());
102
+            all_gates.push(Gate::new(GateType::Equal, EMPTY_VEC, vec![curr - 1, curr - 1 + n]));
107
         }
103
         }
108
 
104
 
109
-        for i in 0..curr {
110
-            // Translate i to the key'th bit position because the array index does not match the bit index
111
-            // i.e.
112
-            //
113
-            //  let input_bits = vec![
114
-            //         A2     A1    A0    <- A_key
115
-            //          0      1     2    <- i iterates all array indices less than curr
116
-            //        false, false, true,
117
-            //
118
-            //         B2     B1    B0
119
-            //          3      4     5
120
-            //        false, true, false
121
-            //  ];
122
-            let key = n - 1 - i;
123
-
124
-            // print!("&& ({}) ", format!("{} = {}", format!("A{}", key), format!("B{}", key)));
125
-
126
-            // Index of Gate(A_i = B_i). 
127
-            // You could maybe find a solution without the indices vector but it's insanely cheap anyway and maybe even better
128
-            let eq_gate_index = indices[key];
129
-            current_bit_gate_indices.push(eq_gate_index);
130
-        }
105
+        and_gate_input_indices.extend(eq_gate_indices.iter());
131
 
106
 
132
         // The AND spanning all gates for this bit
107
         // The AND spanning all gates for this bit
133
-        let and_gate_index = all_gates.len();
134
-        let and_gate = Gate::new(GateType::And, current_bit_gate_indices, vec![]);
135
-
136
-        // println!(")");
137
-        all_gates.push(and_gate);
138
-        and_gate_indices.push(and_gate_index);
108
+        and_gate_indices.push(all_gates.len());
109
+        println!("&& = {}", all_gates.len());
110
+        all_gates.push(Gate::new(GateType::And, and_gate_input_indices, EMPTY_VEC));
139
     }
111
     }
140
 
112
 
141
     // the OR spanning all ANDs
113
     // the OR spanning all ANDs
142
-    let or_gate = Gate::new(GateType::Or, and_gate_indices, vec![]);
114
+    let or_gate = Gate::new(GateType::Or, and_gate_indices, EMPTY_VEC);
143
     all_gates.push(or_gate);
115
     all_gates.push(or_gate);
144
 
116
 
145
     return all_gates;
117
     return all_gates;

Loading…
Cancel
Save