Skip to main content

Building Custom Applications

C++ Application

Step 1: Write your application

// my_app.cpp
#include <ligetron/api.h>
#include <ligetron/bn254fr.h>

int main(int argc, char* argv[]) {
// Your ZK logic here
bn254fr_t x;
bn254fr_alloc(x);
bn254fr_set_u64(x, 42);

// Add constraint
bn254fr_assert_equal(x, x);

bn254fr_free(x);
return 0;
}

Step 2: Compile to WASM

# Make sure Emscripten is activated
source /path/to/emsdk/emsdk_env.sh

# Compile
em++ -O2 \
-I/path/to/sdk/cpp/include \
-L/path/to/sdk/cpp/build \
my_app.cpp \
-o my_app.wasm \
-lligetron

Step 3: Generate proof

./webgpu_prover '{
"program": "my_app.wasm",
"args": []
}'

Rust Application

Step 1: Create Cargo project

cargo new my_zk_app --lib
cd my_zk_app

Step 2: Add dependency

# Cargo.toml
[dependencies]
ligetron = { path = "/path/to/ligetron/sdk/rust" }

[lib]
crate-type = ["cdylib"]

Step 3: Write your application

// src/lib.rs
use ligetron::*;

#[no_mangle]
pub extern "C" fn main() {
let args = get_args();

// Your ZK logic here
let mut x = bn254fr::Bn254Fr::new();
x.set_u64(42);

// Add constraint
x.assert_equal(&x);
}

Step 4: Build to WASM

First, ensure you have the WebAssembly target installed:

rustup target add wasm32-wasip1

Then build:

cargo build --target wasm32-wasip1 --release

Step 5: Generate proof

./webgpu_prover '{
"program": "target/wasm32-wasip1/release/my_zk_app.wasm",
"args": []
}'

Advanced Example: Private Input Verification

This example shows how to verify a computation with private inputs:

#include <ligetron/api.h>
#include <ligetron/bn254fr.h>

// Prove: I know x such that x^2 = y (without revealing x)
int main(int argc, char* argv[]) {
// argv[1] = private input (x)
// argv[2] = public output (y)

bn254fr_t x, y, x_squared;
bn254fr_alloc(x);
bn254fr_alloc(y);
bn254fr_alloc(x_squared);

// Load inputs
bn254fr_set_bytes_little(x, (unsigned char*)argv[1], 32);
bn254fr_set_bytes_little(y, (unsigned char*)argv[2], 32);

// Compute x^2
bn254fr_mulmod(x_squared, x, x);

// Assert x^2 == y
bn254fr_assert_equal(x_squared, y);

bn254fr_free(x);
bn254fr_free(y);
bn254fr_free(x_squared);

return 0;
}

Generate proof:

./webgpu_prover '{
"program": "square_proof.wasm",
"private-indices": [1],
"args": [
{"hex": "0x05"},
{"hex": "0x19"}
]
}'

This proves that the prover knows a value x = 5 such that x^2 = 25, without revealing x to the verifier.