fast
This commit is contained in:
+9
-16
@@ -1,5 +1,3 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::{gate::Gate, gate_type::GateType};
|
use crate::{gate::Gate, gate_type::GateType};
|
||||||
|
|
||||||
|
|
||||||
@@ -82,10 +80,12 @@ impl Circuit {
|
|||||||
|
|
||||||
|
|
||||||
fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate>{
|
fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate>{
|
||||||
let mut indices = HashMap::<String, usize>::new();
|
let mut indices: Vec<usize> = vec![0; n];
|
||||||
let mut all_gates: Vec<Gate> = vec!();
|
let mut all_gates: Vec<Gate> = vec!();
|
||||||
let mut and_gate_indices: Vec<usize> = vec!();
|
let mut and_gate_indices: Vec<usize> = vec!();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
rec_n_bit_comperator_gates(0, n, &mut all_gates, &mut and_gate_indices, &mut indices, );
|
rec_n_bit_comperator_gates(0, n, &mut all_gates, &mut and_gate_indices, &mut indices, );
|
||||||
|
|
||||||
// the OR spanning all ANDs
|
// the OR spanning all ANDs
|
||||||
@@ -96,7 +96,7 @@ fn create_n_bit_comparator_gates(n: usize) -> Vec<Gate>{
|
|||||||
return all_gates;
|
return all_gates;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rec_n_bit_comperator_gates(curr: usize, max: usize, all_gates: &mut Vec<Gate>, and_gate_incides: &mut Vec<usize>, indices: &mut HashMap<String, usize>){
|
fn rec_n_bit_comperator_gates(curr: usize, max: usize, all_gates: &mut Vec<Gate>, and_gate_incides: &mut Vec<usize>, indices: &mut Vec<usize>){
|
||||||
// Incrementing gate index
|
// Incrementing gate index
|
||||||
let a_curr_gt_b_curr_gate_index = all_gates.len();
|
let a_curr_gt_b_curr_gate_index = all_gates.len();
|
||||||
|
|
||||||
@@ -111,27 +111,20 @@ fn rec_n_bit_comperator_gates(curr: usize, max: usize, all_gates: &mut Vec<Gate>
|
|||||||
let mut this_recursion_gate_indices: Vec<usize> = vec!(a_curr_gt_b_curr_gate_index);
|
let mut this_recursion_gate_indices: Vec<usize> = vec!(a_curr_gt_b_curr_gate_index);
|
||||||
|
|
||||||
for i in 0..curr {
|
for i in 0..curr {
|
||||||
// A_i
|
let key = max-1-i;
|
||||||
let a_i = format!("A{}", max-1-i);
|
|
||||||
//B_i
|
|
||||||
let b_i = format!("B{}", max-1-i);
|
|
||||||
|
|
||||||
// A_i=B_i
|
|
||||||
let a_i_eq_b_i = format!("{} = {}", a_i, b_i);
|
|
||||||
// The curr-1'th equality gate is the only one that doesn't exist yet
|
|
||||||
if i == curr-1 {
|
if i == curr-1 {
|
||||||
// Gate(A_current > B_current)
|
// Gate(A_current > B_current)
|
||||||
let a_i_eq_b_i_gate = Gate::new(GateType::Equal, vec!(), vec!(i, i+max));
|
let a_i_eq_b_i_gate = Gate::new(GateType::Equal, vec!(), vec!(i, i+max));
|
||||||
let a_curr_gt_b_curr_gate_index = all_gates.len();
|
let a_curr_gt_b_curr_gate_index = all_gates.len();
|
||||||
indices.insert(a_i_eq_b_i.clone(), a_curr_gt_b_curr_gate_index);
|
indices[key] = a_curr_gt_b_curr_gate_index;
|
||||||
all_gates.push(a_i_eq_b_i_gate);
|
all_gates.push(a_i_eq_b_i_gate);
|
||||||
}
|
}
|
||||||
let eq_gate_index = indices.get(&a_i_eq_b_i).unwrap();
|
let eq_gate_index = indices[key];
|
||||||
|
|
||||||
//print!("&& ({}) ", a_i_eq_b_i);
|
//print!("&& ({}) ", format!("{} = {}", format!("A{}", key), format!("B{}", key)));
|
||||||
|
|
||||||
// Index of this equality gate
|
// Index of this equality gate
|
||||||
this_recursion_gate_indices.push(*eq_gate_index);
|
this_recursion_gate_indices.push(eq_gate_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
let and_curr_index = all_gates.len();
|
let and_curr_index = all_gates.len();
|
||||||
|
|||||||
+16
-4
@@ -1,9 +1,20 @@
|
|||||||
use apet_ex1::{circuit::Circuit, scalar_to_bits::scalar_to_bits};
|
use apet_ex1::circuit::Circuit;
|
||||||
use curve25519_dalek::Scalar;
|
|
||||||
use num_bigint::BigUint;
|
|
||||||
use rand::rngs::OsRng;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let input_bits = vec![
|
||||||
|
// A2 A1 A0
|
||||||
|
// 0 1 2
|
||||||
|
false, false, true,
|
||||||
|
// B2 B1 B0
|
||||||
|
// 3 4 5
|
||||||
|
false, true, false
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
let c = Circuit::compare_n_bit_numbers(input_bits, 3);
|
||||||
|
let res = c.eval();
|
||||||
|
println!("1 > 2 ? {}", res);
|
||||||
|
/*
|
||||||
let a = Scalar::random(&mut OsRng);
|
let a = Scalar::random(&mut OsRng);
|
||||||
let b = Scalar::random(&mut OsRng);
|
let b = Scalar::random(&mut OsRng);
|
||||||
// Convert to bit representation
|
// Convert to bit representation
|
||||||
@@ -23,4 +34,5 @@ fn main() {
|
|||||||
let b_int = BigUint::from_bytes_le(&b.to_bytes());
|
let b_int = BigUint::from_bytes_le(&b.to_bytes());
|
||||||
|
|
||||||
println!("{}", circuit_result == (a_int > b_int))
|
println!("{}", circuit_result == (a_int > b_int))
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user