diff --git a/crates/mpz-core/src/block.rs b/crates/mpz-core/src/block.rs index 2f7a0105..f1420de4 100644 --- a/crates/mpz-core/src/block.rs +++ b/crates/mpz-core/src/block.rs @@ -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]); diff --git a/crates/mpz-garble-core/src/encoding/mod.rs b/crates/mpz-garble-core/src/encoding/mod.rs index 772888de..86e505b0 100644 --- a/crates/mpz-garble-core/src/encoding/mod.rs +++ b/crates/mpz-garble-core/src/encoding/mod.rs @@ -272,6 +272,7 @@ impl Index for Labels { } /// Encoded bit label. +#[repr(transparent)] #[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)] pub struct Label(Block); diff --git a/crates/mpz-garble-core/src/evaluator.rs b/crates/mpz-garble-core/src/evaluator.rs index f503ef0e..abc62577 100644 --- a/crates/mpz-garble-core/src/evaluator.rs +++ b/crates/mpz-garble-core/src/evaluator.rs @@ -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; @@ -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; } } } diff --git a/crates/mpz-garble-core/src/generator.rs b/crates/mpz-garble-core/src/generator.rs index cfcc99f9..b9dc7950 100644 --- a/crates/mpz-garble-core/src/generator.rs +++ b/crates/mpz-garble-core/src/generator.rs @@ -255,27 +255,23 @@ where #[inline] fn next(&mut self) -> Option { - 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; @@ -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; } } }