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§

ast
The untyped Abstract Syntax Tree (AST).
check
Type-checker, transforming an untyped crate::ast::Program into a typed crate::ast::Program.
circuit
The Circuit representation used by the compiler.
compile
Compiles a crate::ast::Program to a crate::circuit::Circuit.
env
Simple helper for lexical scopes used by crate::check() and crate::compile().
eval
Evaluates a crate::circuit::Circuit with inputs supplied by different parties.
literal
A subset of crate::ast::Expr that is used as input / output by an crate::eval::Evaluator.
parse
Parses a stream of crate::scan::Tokens into an untyped crate::ast::Program.
scan
Splits a source code into a stream of crate::token::Tokens.
token
Tokens produced by crate::scan::scan.

Structs§

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

Enums§

CompileTimeError
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§

TypedExpr
crate::ast::Expr after typechecking.
TypedFnDef
crate::ast::FnDef after typechecking.
TypedPattern
crate::ast::Pattern after typechecking.
TypedProgram
crate::ast::Program after typechecking.
TypedStmt
crate::ast::Stmt after typechecking.
UntypedExpr
crate::ast::Expr without any associated type information.
UntypedFnDef
crate::ast::FnDef without any associated type information.
UntypedPattern
crate::ast::Pattern without any associated type information.
UntypedProgram
crate::ast::Program without any associated type information.
UntypedStmt
crate::ast::Stmt without any associated type information.