Skip to content

Commit 5f2b9ef

Browse files
committed
garbled circuit commitment
1 parent 2cca9c8 commit 5f2b9ef

1 file changed

Lines changed: 21 additions & 35 deletions

File tree

src/main.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ pub fn custom_circuit(a: Wires, b: Wires, c: Wires) -> Circuit {
1111
circuit
1212
}
1313

14+
pub fn gc_commitment(circuit: &Circuit) -> [u8; 32] {
15+
let garble = circuit.garbled_gates();
16+
let mut v = Vec::new();
17+
for (x, y) in garble.clone() {
18+
if x.is_some() {
19+
v.extend(x.unwrap().0);
20+
v.extend(y.unwrap().0);
21+
}
22+
}
23+
let temp = hash(&v);
24+
let garble_hash = temp.as_bytes();
25+
26+
*garble_hash
27+
}
28+
1429
pub fn garbler1<const N: usize, const M: usize>() -> ([[u8; 32]; N], ([u64; N], [Vec<(Option<S>, Option<S>)>; N])) {
1530
let mut commitments = [[0; 32]; N];
1631
let mut seeds = [0; N];
@@ -23,17 +38,7 @@ pub fn garbler1<const N: usize, const M: usize>() -> ([[u8; 32]; N], ([u64; N],
2338
let c_wires = Fq12::wires_set_labels_rng(&mut r);
2439
let circuit = custom_circuit(a_wires, b_wires, c_wires);
2540
let garble = circuit.garbled_gates();
26-
let mut v = Vec::new();
27-
for (x, y) in garble.clone() {
28-
if x.is_some() {
29-
v.extend(x.unwrap().0);
30-
v.extend(y.unwrap().0);
31-
}
32-
}
33-
let temp = hash(&v);
34-
let garble_hash = temp.as_bytes();
35-
36-
commitments[i] = garble_hash.clone();
41+
commitments[i] = gc_commitment(&circuit);
3742
seeds[i] = seed;
3843
garbles[i] = garble;
3944
}
@@ -80,17 +85,8 @@ pub fn evaluator2<const N: usize, const M: usize, const N_MINUS_M: usize>(commit
8085
let b_wires = Fq12::wires_set_labels_rng(&mut r);
8186
let c_wires = Fq12::wires_set_labels_rng(&mut r);
8287
let circuit = custom_circuit(a_wires, b_wires, c_wires);
83-
let garble = circuit.garbled_gates();
84-
let mut v = Vec::new();
85-
for (x, y) in garble.clone() {
86-
if x.is_some() {
87-
v.extend(x.unwrap().0);
88-
v.extend(y.unwrap().0);
89-
}
90-
}
91-
let temp = hash(&v);
92-
let garble_hash = temp.as_bytes();
93-
assert_eq!(*garble_hash, commitment);
88+
let garble_hash = gc_commitment(&circuit);
89+
assert_eq!(garble_hash, commitment);
9490
seed_index += 1;
9591
}
9692
}
@@ -161,23 +157,13 @@ pub fn test() {
161157
println!("circuit evaluation: {:?}", start.elapsed());
162158

163159
let start = Instant::now();
164-
let garbles = circuit.garbled_gates();
165-
166-
let mut v = Vec::new();
167-
for (x, y) in garbles.clone() {
168-
if x.is_some() {
169-
v.extend(x.unwrap().0);
170-
v.extend(y.unwrap().0);
171-
}
172-
}
173-
174-
let temp = hash(&v);
175-
let garble_hash = temp.as_bytes();
160+
let garble = circuit.garbled_gates();
161+
let garble_hash = gc_commitment(&circuit);
176162
println!("garble_hash: {:?}", garble_hash);
177163
println!("circuit garbling: {:?}", start.elapsed());
178164

179165
let start = Instant::now();
180-
circuit.garble_evaluate(garbles);
166+
circuit.garble_evaluate(garble);
181167
assert_eq!(circuit.0[0].borrow().get_label(), circuit.0[0].borrow().select(true));
182168
println!("circuit garbling evaluation: {:?}", start.elapsed());
183169
println!("final label: {:?}", circuit.0[0].borrow().get_label());

0 commit comments

Comments
 (0)