Crate garble_lang

source ·
Expand description

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);

Modules§

Structs§

  • An input argument for a Garble program and circuit.
  • The result of type-checking and compiling a Garble program.

Enums§

  • Errors that can occur during compile time, while a program is scanned, parsed or type-checked.
  • A generic error that combines compile-time and run-time errors.

Functions§

  • Scans, parses and type-checks a program.
  • Scans, parses, type-checks and then compiles the "main" fn of a program to a boolean circuit.
  • Scans, parses, type-checks and then compiles the "main" fn of a program to a boolean circuit.

Type Aliases§