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§
- ast
- The untyped Abstract Syntax Tree (AST).
- check
- Type-checker, transforming an untyped
crate::ast::Programinto a typedcrate::ast::Program. - circuit
- The
Circuitrepresentation used by the compiler. - compile
- Compiles a
crate::ast::Programto acrate::circuit::Circuit. - env
- Simple helper for lexical scopes used by
crate::check()andcrate::compile(). - eval
- Evaluates a
crate::circuit::Circuitwith inputs supplied by different parties. - literal
- A subset of
crate::ast::Exprthat is used as input / output by ancrate::eval::Evaluator. - parse
- Parses a stream of
crate::scan::Tokensinto an untypedcrate::ast::Program. - scan
- Splits a source code into a stream of
crate::token::Tokens. - token
- Tokens produced by
crate::scan::scan.
Structs§
- Garble
Argument - An input argument for a Garble program and circuit.
- Garble
Program - The result of type-checking and compiling a Garble program.
Enums§
- Compile
Time Error - Errors that can occur during compile time, while a program is scanned, parsed or type-checked.
- Error
- A generic error that combines compile-time and run-time errors.
Functions§
- check
- Scans, parses and type-checks a program.
- compile
- Scans, parses, type-checks and then compiles the
"main"fn of a program to a Boolean circuit. - compile_
with_ constants - Scans, parses, type-checks and then compiles the
"main"fn of a program to a Boolean circuit.
Type Aliases§
- Typed
Expr crate::ast::Exprafter typechecking.- Typed
FnDef crate::ast::FnDefafter typechecking.- Typed
Pattern crate::ast::Patternafter typechecking.- Typed
Program crate::ast::Programafter typechecking.- Typed
Stmt crate::ast::Stmtafter typechecking.- Untyped
Expr crate::ast::Exprwithout any associated type information.- Untyped
FnDef crate::ast::FnDefwithout any associated type information.- Untyped
Pattern crate::ast::Patternwithout any associated type information.- Untyped
Program crate::ast::Programwithout any associated type information.- Untyped
Stmt crate::ast::Stmtwithout any associated type information.