mpz icon indicating copy to clipboard operation
mpz copied to clipboard

Refactor: optimize gate branching

Open yawn-c111 opened this issue 1 year ago • 0 comments

Related Issue

  • #142

Implementation

Adjusted struct field types to ensure 16-byte memory alignment and C-compatibility on frequently accessed fields.

  • crates/mpz-core/src/block.rs
  • crates/mpz-garble-core/src/encoding/mod.rs
#[repr(C, align(16))]

pub struct Block([u8; 16]);

Move frequently accessed struct fields outside the loop.

  • crates/mpz-garble-core/src/generator.rs
  • crates/mpz-garble-core/src/evaluator.rs
let labels = &mut self.labels;
let gates = &mut self.gates;
while let Some(gate) = gates.next() {
    match gate {
        Gate::Xor { x, y, z, } => {
            let x_label = labels[x.id()];
            let y_label = labels[y.id()];
            labels[z.id()] = x_label ^ y_label;

Omitted field name shadowing.

  • crates/mpz-garble-core/src/generator.rs
  • crates/mpz-garble-core/src/evaluator.rs
Gate::Xor { x, y, z, } => {

Result

Garbling: Approximately 6-9% speed improvement Evaluation: No significant change

image

garble/aes128 image

garble/aes128_batched image

garble/aes128_with_hash image

evaluate/aes128 image

yawn-c111 avatar Sep 20 '24 03:09 yawn-c111