13 unstable releases (5 breaking)

0.6.1 Mar 19, 2025
0.5.0 Aug 28, 2024
0.4.0 Jul 29, 2024
0.2.0 Jan 23, 2024
0.1.2 Jul 27, 2022

#92 in Programming languages

Download history 1/week @ 2024-12-23 8/week @ 2025-01-06 2/week @ 2025-01-13 3/week @ 2025-01-27 39/week @ 2025-02-03 40/week @ 2025-02-10 34/week @ 2025-02-17 40/week @ 2025-02-24 20/week @ 2025-03-03 11/week @ 2025-03-10 141/week @ 2025-03-17 33/week @ 2025-03-24 36/week @ 2025-03-31

221 downloads per month
Used in 3 crates (via tandem_garble_interop)

MIT license

445KB
10K SLoC

A purely functional programming language with a Rust-like syntax that compiles to logic gates for secure multi-party computation.

Garble programs always terminate and are compiled into a combination of Boolean AND / XOR / NOT gates. These Boolean circuits can either be executed directly (mostly for testing purposes) or passed to a multi-party computation engine.

use garble_lang::{compile, literal::Literal, token::UnsignedNumType::U32};

// Compile and type-check a simple program to add the inputs of 3 parties:
let code = "pub fn main(x: u32, y: u32, z: u32) -> u32 { x + y + z }";
let prg = compile(code).map_err(|e| e.prettify(&code)).unwrap();

// We can evaluate the circuit directly, useful for testing purposes:
let mut eval = prg.evaluator();
eval.set_u32(2);
eval.set_u32(10);
eval.set_u32(100);
let output = eval.run().map_err(|e| e.prettify(&code)).unwrap();
assert_eq!(u32::try_from(output).map_err(|e| e.prettify(&code)).unwrap(), 2 + 10 + 100);

// Or we can run the compiled circuit in an MPC engine, simulated using `prg.circuit.eval()`:
let x = prg.parse_arg(0, "2").unwrap().as_bits();
let y = prg.parse_arg(1, "10").unwrap().as_bits();
let z = prg.parse_arg(2, "100").unwrap().as_bits();
let output = prg.circuit.eval(&[x, y, z]); // use your own MPC engine here instead
let result = prg.parse_output(&output).unwrap();
assert_eq!("112", result.to_string());

// Input arguments can also be constructed directly as literals:
let x = prg.literal_arg(0, Literal::NumUnsigned(2, U32)).unwrap().as_bits();
let y = prg.literal_arg(1, Literal::NumUnsigned(10, U32)).unwrap().as_bits();
let z = prg.literal_arg(2, Literal::NumUnsigned(100, U32)).unwrap().as_bits();
let output = prg.circuit.eval(&[x, y, z]); // use your own MPC engine here instead
let result = prg.parse_output(&output).unwrap();
assert_eq!(Literal::NumUnsigned(112, U32), result);

The Garble Programming Language

Garble is a simple programming language for Multi-Party Computation. Garble programs are compiled to Boolean circuits and always terminate, making them ideal for Garbled Circuits. Garble is statically typed, low-level, purely functional and uses a Rust-like syntax. Garble is much simpler than Rust though, making it easy to learn and simple to integrate into MPC engines.

// A program for solving Yao's Millionaires' problem in Garble:

enum Richest {
    IsA,
    IsB,
    Tie,
}

pub fn main(a: u64, b: u64) -> Richest {
    if a > b {
        Richest::IsA
    } else if b > a {
        Richest::IsB
    } else {
        Richest::Tie
    }
}

To learn more about Garble, check out the website.

Dependencies

~0–2.5MB
~44K SLoC