Browse Source

Make function signatures conform to assignment

master
Your Name 2 weeks ago
parent
commit
70da422938
3 changed files with 9 additions and 9 deletions
  1. 5
    4
      src/circuit.rs
  2. 2
    2
      src/main.rs
  3. 2
    3
      tests/test.rs

+ 5
- 4
src/circuit.rs View File

3
 #[derive(Debug, Clone)]
3
 #[derive(Debug, Clone)]
4
 pub struct Circuit {
4
 pub struct Circuit {
5
     pub gates: Vec<Gate>,      // All gates
5
     pub gates: Vec<Gate>,      // All gates
6
+    pub input_bits: Vec<bool>,
6
 }
7
 }
7
 
8
 
8
 impl Circuit {
9
 impl Circuit {
9
-    pub fn eval(self, input_bits: Vec<bool>,)  -> bool {
10
+    pub fn eval(self, )  -> bool {
10
         let mut evaluated_gates = vec![];
11
         let mut evaluated_gates = vec![];
11
 
12
 
12
         for gate in self.gates {
13
         for gate in self.gates {
13
-            let result = gate.eval(&input_bits, &evaluated_gates);
14
+            let result = gate.eval(&self.input_bits, &evaluated_gates);
14
             evaluated_gates.push(result);
15
             evaluated_gates.push(result);
15
         }
16
         }
16
 
17
 
24
         This method should create a circuit that outputs 1 if the first number A (encoded in the first n
25
         This method should create a circuit that outputs 1 if the first number A (encoded in the first n
25
         bits) is greater than the second number B (encoded in the next n bits) .
26
         bits) is greater than the second number B (encoded in the next n bits) .
26
     */
27
     */
27
-    pub fn compare_n_bit_numbers( n: usize) -> Self {
28
+    pub fn compare_n_bit_numbers( n: usize, input_bits: Vec<bool>,) -> Self {
28
 
29
 
29
         /*
30
         /*
30
             base case n=1: 1-bit:
31
             base case n=1: 1-bit:
69
         */
70
         */
70
 
71
 
71
         let gates = create_n_bit_comparator_gates(n);
72
         let gates = create_n_bit_comparator_gates(n);
72
-        return Circuit { gates };
73
+        return Circuit { gates, input_bits };
73
     }
74
     }
74
 }
75
 }
75
 
76
 

+ 2
- 2
src/main.rs View File

33
     input_bits.extend_from_slice(&b_bits);
33
     input_bits.extend_from_slice(&b_bits);
34
 
34
 
35
     // Build and evaluate the comparison circuit
35
     // Build and evaluate the comparison circuit
36
-    let circuit = Circuit::compare_n_bit_numbers(256);
37
-    let circuit_result = circuit.eval(input_bits);
36
+    let circuit = Circuit::compare_n_bit_numbers(256, input_bits);
37
+    let circuit_result = circuit.eval();
38
 
38
 
39
     let a_int = BigUint::from_bytes_le(&a.to_bytes());
39
     let a_int = BigUint::from_bytes_le(&a.to_bytes());
40
     let b_int = BigUint::from_bytes_le(&b.to_bytes());
40
     let b_int = BigUint::from_bytes_le(&b.to_bytes());

+ 2
- 3
tests/test.rs View File

6
     use rand::rngs::OsRng;
6
     use rand::rngs::OsRng;
7
     #[test]
7
     #[test]
8
     fn test_scalar_comparison_via_circuit() {
8
     fn test_scalar_comparison_via_circuit() {
9
-        let circuit = Circuit::compare_n_bit_numbers(256);
10
-
11
         for _ in 0..100 {
9
         for _ in 0..100 {
12
             let a = Scalar::random(&mut OsRng);
10
             let a = Scalar::random(&mut OsRng);
13
             let b = Scalar::random(&mut OsRng);
11
             let b = Scalar::random(&mut OsRng);
22
             input_bits.extend_from_slice(&b_bits);
20
             input_bits.extend_from_slice(&b_bits);
23
 
21
 
24
             // Evaluate the comparison circuit
22
             // Evaluate the comparison circuit
25
-            let circuit_result = circuit.clone().eval(input_bits);
23
+            let circuit = Circuit::compare_n_bit_numbers(256, input_bits);
24
+            let circuit_result = circuit.clone().eval();
26
 
25
 
27
             // Compare expected result using BigUint
26
             // Compare expected result using BigUint
28
             let a_int = BigUint::from_bytes_le(&a.to_bytes());
27
             let a_int = BigUint::from_bytes_le(&a.to_bytes());

Loading…
Cancel
Save