Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: optimize gate branching #184

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/mpz-core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rand::{distributions::Standard, prelude::Distribution, CryptoRng, Rng};
use serde::{Deserialize, Serialize};

/// A block of 128 bits
#[repr(transparent)]
#[repr(C, align(16))]
#[derive(Copy, Clone, Debug, Default, PartialEq, Serialize, Deserialize, Pod, Zeroable)]
pub struct Block([u8; 16]);

Expand Down
1 change: 1 addition & 0 deletions crates/mpz-garble-core/src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl<const N: usize, S: LabelState> Index<usize> for Labels<N, S> {
}

/// Encoded bit label.
#[repr(transparent)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Label(Block);

Expand Down
43 changes: 17 additions & 26 deletions crates/mpz-garble-core/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,20 @@ where
/// Evaluates the next encrypted gate in the circuit.
#[inline]
pub fn next(&mut self, encrypted_gate: EncryptedGate) {
while let Some(gate) = self.gates.next() {
let labels = &mut self.labels;
let gates = &mut self.gates;
while let Some(gate) = gates.next() {
match gate {
Gate::Xor {
x: node_x,
y: node_y,
z: node_z,
} => {
let x = self.labels[node_x.id()];
let y = self.labels[node_y.id()];
self.labels[node_z.id()] = x ^ y;
Gate::Xor { x, y, z, } => {
let x_label = labels[x.id()];
let y_label = labels[y.id()];
labels[z.id()] = x_label ^ y_label;
}
Gate::And {
x: node_x,
y: node_y,
z: node_z,
} => {
let x = self.labels[node_x.id()];
let y = self.labels[node_y.id()];
let z = and_gate(self.cipher, &x, &y, &encrypted_gate, self.gid);
self.labels[node_z.id()] = z;
Gate::And { x, y, z, } => {
let x_label = labels[x.id()];
let y_label = labels[y.id()];
let z_label = and_gate(self.cipher, &x_label, &y_label, &encrypted_gate, self.gid);
labels[z.id()] = z_label;

self.gid += 2;
self.counter += 1;
Expand All @@ -222,17 +216,14 @@ where
hasher.update(&encrypted_gate.to_bytes());
}

// If we have more AND gates to evaluate, return.
if self.wants_gates() {
// Directly check the condition instead of calling the method
if self.counter != self.and_count {
return;
}
}
Gate::Inv {
x: node_x,
z: node_z,
} => {
let x = self.labels[node_x.id()];
self.labels[node_z.id()] = x;
Gate::Inv { x, z } => {
let x_label = labels[x.id()];
labels[z.id()] = x_label;
}
}
}
Expand Down
40 changes: 17 additions & 23 deletions crates/mpz-garble-core/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,23 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
while let Some(gate) = self.gates.next() {
// Cache the labels slice locally for faster access
let labels = &mut self.labels;
let gates = &mut self.gates;

while let Some(gate) = gates.next() {
match gate {
Gate::Xor {
x: node_x,
y: node_y,
z: node_z,
} => {
let x_0 = self.labels[node_x.id()];
let y_0 = self.labels[node_y.id()];
self.labels[node_z.id()] = x_0 ^ y_0;
Gate::Xor { x, y, z, } => {
let x_0 = labels[x.id()];
let y_0 = labels[y.id()];
labels[z.id()] = x_0 ^ y_0;
}
Gate::And {
x: node_x,
y: node_y,
z: node_z,
} => {
let x_0 = self.labels[node_x.id()];
let y_0 = self.labels[node_y.id()];
Gate::And { x, y, z, } => {
let x_0 = labels[x.id()];
let y_0 = labels[y.id()];
let (z_0, encrypted_gate) =
and_gate(self.cipher, &x_0, &y_0, &self.delta, self.gid);
self.labels[node_z.id()] = z_0;
labels[z.id()] = z_0;

self.gid += 2;
self.counter += 1;
Expand All @@ -294,12 +290,10 @@ where

return Some(encrypted_gate);
}
Gate::Inv {
x: node_x,
z: node_z,
} => {
let x_0 = self.labels[node_x.id()];
self.labels[node_z.id()] = x_0 ^ self.delta;
Gate::Inv { x,
z, } => {
let x_0 = labels[x.id()];
labels[z.id()] = x_0 ^ self.delta;
}
}
}
Expand Down