0.04
This commit is contained in:
+17
-45
@@ -82,64 +82,36 @@ impl Circuit {
|
||||
}
|
||||
|
||||
fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate> {
|
||||
let mut indices: Vec<usize> = vec![0; n];
|
||||
let mut all_gates: Vec<Gate> = Vec::with_capacity(1+3*n);
|
||||
let mut all_gates: Vec<Gate> = Vec::with_capacity(3*n);
|
||||
let mut and_gate_indices: Vec<usize> = vec![0; n];
|
||||
let mut eq_gate_indices: Vec<usize> = Vec::with_capacity(n-1);
|
||||
|
||||
const EMPTY_VEC: Vec<usize> = Vec::new();
|
||||
|
||||
for curr in 0..n {
|
||||
// Gate(A_current > B_current)
|
||||
let gt_gate = Gate::new(GateType::Bigger, vec![], vec![curr, curr + n]);
|
||||
let gt_gate_index = all_gates.len();
|
||||
all_gates.push(gt_gate);
|
||||
|
||||
// print!("( ({}) ", format!("{} > {}", format!("A{}", n-1-curr), format!("B{}", n-1-curr)));
|
||||
|
||||
let mut current_bit_gate_indices: Vec<usize> = Vec::with_capacity(curr+1);
|
||||
current_bit_gate_indices.push(gt_gate_index);
|
||||
// Gate(A_curr > B_curr)
|
||||
let mut and_gate_input_indices = vec!(all_gates.len());
|
||||
println!("GT = {}", all_gates.len());
|
||||
all_gates.push(Gate::new(GateType::Bigger, EMPTY_VEC, vec![curr, curr + n]));
|
||||
|
||||
// Bit to the left of curr. The one at array-index 0 doesn't have one.
|
||||
if curr != 0 {
|
||||
// Gate(A_i = B_i)
|
||||
let eq_gate = Gate::new(GateType::Equal, vec![], vec![curr - 1, curr - 1 + n]);
|
||||
// remember which key-index (see below) this bit belongs to.
|
||||
indices[n - curr] = all_gates.len();
|
||||
all_gates.push(eq_gate);
|
||||
// Gate(A_curr-1 = B_curr-1)
|
||||
eq_gate_indices.push(all_gates.len());
|
||||
println!("EQ = {}", all_gates.len());
|
||||
all_gates.push(Gate::new(GateType::Equal, EMPTY_VEC, vec![curr - 1, curr - 1 + n]));
|
||||
}
|
||||
|
||||
for i in 0..curr {
|
||||
// Translate i to the key'th bit position because the array index does not match the bit index
|
||||
// i.e.
|
||||
//
|
||||
// let input_bits = vec![
|
||||
// A2 A1 A0 <- A_key
|
||||
// 0 1 2 <- i iterates all array indices less than curr
|
||||
// false, false, true,
|
||||
//
|
||||
// B2 B1 B0
|
||||
// 3 4 5
|
||||
// false, true, false
|
||||
// ];
|
||||
let key = n - 1 - i;
|
||||
|
||||
// print!("&& ({}) ", format!("{} = {}", format!("A{}", key), format!("B{}", key)));
|
||||
|
||||
// Index of Gate(A_i = B_i).
|
||||
// You could maybe find a solution without the indices vector but it's insanely cheap anyway and maybe even better
|
||||
let eq_gate_index = indices[key];
|
||||
current_bit_gate_indices.push(eq_gate_index);
|
||||
}
|
||||
and_gate_input_indices.extend(eq_gate_indices.iter());
|
||||
|
||||
// The AND spanning all gates for this bit
|
||||
let and_gate_index = all_gates.len();
|
||||
let and_gate = Gate::new(GateType::And, current_bit_gate_indices, vec![]);
|
||||
|
||||
// println!(")");
|
||||
all_gates.push(and_gate);
|
||||
and_gate_indices.push(and_gate_index);
|
||||
and_gate_indices.push(all_gates.len());
|
||||
println!("&& = {}", all_gates.len());
|
||||
all_gates.push(Gate::new(GateType::And, and_gate_input_indices, EMPTY_VEC));
|
||||
}
|
||||
|
||||
// the OR spanning all ANDs
|
||||
let or_gate = Gate::new(GateType::Or, and_gate_indices, vec![]);
|
||||
let or_gate = Gate::new(GateType::Or, and_gate_indices, EMPTY_VEC);
|
||||
all_gates.push(or_gate);
|
||||
|
||||
return all_gates;
|
||||
|
||||
Reference in New Issue
Block a user